Exemplo n.º 1
0
  // range format example = (7L*24L*60L*60L*1000L) (1 week)
  public List<NodeHistoryEntity> findSpecificNodeHistoryInRangeTime(String nid, Long range) {
    String hashKey = nid;
    SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MMdd'T'HH:mm:ss.SSS'Z'");

    Long currentTimeMilli = (new Date()).getTime();
    Long rangeTimeMilli = (new Date()).getTime() - range;
    dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
    String currentTime = dateFormatter.format(currentTimeMilli);
    String rangeTime = dateFormatter.format(rangeTimeMilli);

    NodeHistory nhKey = new NodeHistory();
    nhKey.setNodeID(hashKey);
    List<NodeHistory> latestNodeHistory;

    Condition rangeKeyCondition =
        new Condition()
            .withComparisonOperator(ComparisonOperator.BETWEEN.toString())
            .withAttributeValueList(
                new AttributeValue().withS(currentTime), new AttributeValue().withS(rangeTime));

    DynamoDBQueryExpression<NodeHistory> queryExpression =
        new DynamoDBQueryExpression<NodeHistory>()
            .withHashKeyValues(nhKey)
            .withRangeKeyCondition("Time", rangeKeyCondition);

    latestNodeHistory = mapper.query(NodeHistory.class, queryExpression);

    return this.transform(latestNodeHistory);
  }
Exemplo n.º 2
0
  private void createNodeHistoryItems(
      NodeHistory nHistoryDB, String nid, String time, String values) {
    nHistoryDB.setNodeID(nid);
    nHistoryDB.setTime(time);
    nHistoryDB.setSAValues(values);

    mapper.save(nHistoryDB);

    String hashKey = nid;
    String rangeKey = time;
    NodeHistory nhKey = new NodeHistory();
    nhKey.setNodeID(hashKey);

    Condition rangeKeyCondition =
        new Condition()
            .withComparisonOperator(ComparisonOperator.EQ.toString())
            .withAttributeValueList(new AttributeValue().withS(rangeKey));
    DynamoDBQueryExpression<NodeHistory> queryExpression =
        new DynamoDBQueryExpression<NodeHistory>()
            .withHashKeyValues(nhKey)
            .withRangeKeyCondition("Time", rangeKeyCondition);

    List<NodeHistory> latestNodeHistory = mapper.query(NodeHistory.class, queryExpression);

    System.out.println("Item created: ");
    for (NodeHistory nh : latestNodeHistory) {
      System.out.format(
          "SerialNumber=%s, Time=%s, SAValues=%s\n",
          nh.getNodeID(), nh.getTime(), nh.getSAValues());
    }
  }
Exemplo n.º 3
0
  private List<NodeHistoryEntity> transform(List<NodeHistory> target) {
    List<NodeHistoryEntity> list = new ArrayList<>();
    for (NodeHistory item : target) {
      NodeHistoryEntity temp = new NodeHistoryEntity();
      temp.setNID(item.getNodeID());
      temp.setSAValues(item.getSAValues());
      temp.setTime(item.getTime());

      list.add(temp);
    }
    return list;
  }
Exemplo n.º 4
0
  private void deleteNodeHistoryItem(String nid, String time) {
    DynamoDBMapperConfig config =
        new DynamoDBMapperConfig(DynamoDBMapperConfig.ConsistentReads.CONSISTENT);
    NodeHistory updatedItem;
    NodeHistory deletedItem;

    if (time != null) {
      updatedItem = mapper.load(NodeHistory.class, nid, time, config);
      mapper.delete(updatedItem);
      deletedItem =
          mapper.load(NodeHistory.class, updatedItem.getNodeID(), updatedItem.getTime(), config);
    } else {
      updatedItem = mapper.load(NodeHistory.class, nid, config);
      mapper.delete(updatedItem);
      deletedItem = mapper.load(NodeHistory.class, updatedItem.getNodeID(), config);
    }

    if (deletedItem == null) System.out.println("Done - The item is deleted.");
    else System.out.println("Fail - The item is still remained.");
  }
Exemplo n.º 5
0
  private void printNodeHistoryItems(String nid, String time) {

    String hashKey = nid;
    String rangeKey = time;
    NodeHistory nhKey = new NodeHistory();
    nhKey.setNodeID(hashKey);
    List<NodeHistory> latestNodeHistory;

    if (time != null) {
      Condition rangeKeyCondition =
          new Condition()
              .withComparisonOperator(ComparisonOperator.EQ.toString())
              .withAttributeValueList(new AttributeValue().withS(rangeKey));
      DynamoDBQueryExpression<NodeHistory> queryExpression =
          new DynamoDBQueryExpression<NodeHistory>()
              .withHashKeyValues(nhKey)
              .withRangeKeyCondition("Time", rangeKeyCondition);
      latestNodeHistory = mapper.query(NodeHistory.class, queryExpression);
    } else {
      DynamoDBQueryExpression<NodeHistory> queryExpression =
          new DynamoDBQueryExpression<NodeHistory>().withHashKeyValues(nhKey);
      latestNodeHistory = mapper.query(NodeHistory.class, queryExpression);
    }

    for (NodeHistory nh : latestNodeHistory) {
      System.out.format(
          "SerialNumber=%s, Time=%s, SAValues=%s\n",
          nh.getNodeID(), nh.getTime(), nh.getSAValues());
    }
  }
Exemplo n.º 6
0
  public List<NodeHistoryEntity> findNodeHistoriesbyNodeID(String id) {
    DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
    scanExpression.addFilterCondition(
        "NodeID",
        new Condition()
            .withComparisonOperator(ComparisonOperator.EQ)
            .withAttributeValueList(new AttributeValue().withS(id)));
    List<NodeHistory> scanResult = mapper.scan(NodeHistory.class, scanExpression);

    List<NodeHistoryEntity> list = new ArrayList<>();

    for (NodeHistory n : scanResult) {
      NodeHistoryEntity nhe = new NodeHistoryEntity();

      nhe.setNID(n.getNodeID());
      nhe.setTime(n.getTime());
      nhe.setSAValues(n.getSAValues());

      list.add(nhe);
    }

    return list;
  }
Exemplo n.º 7
0
  private void updateNodeHistoryItem(String nid, String time, String values) {
    NodeHistory itemRetrieved;

    if (time != null) {
      itemRetrieved = mapper.load(NodeHistory.class, nid, time);
      itemRetrieved.setTime(time);
    } else itemRetrieved = mapper.load(NodeHistory.class, nid);

    itemRetrieved.setSAValues(values);
    System.out.println("Item updated: ");
    System.out.format(
        "SerialNumber=%s, Time=%s, SAValues=%s\n",
        itemRetrieved.getNodeID(), itemRetrieved.getTime(), itemRetrieved.getSAValues());
  }
Exemplo n.º 8
0
  private List<NodeHistoryEntity> findNodeHistories(String nid, String time) {
    String hashKey = nid;
    String rangeKey = time;
    NodeHistory nhKey = new NodeHistory();
    nhKey.setNodeID(hashKey);
    List<NodeHistory> latestNodeHistory;

    if (time != null) {
      Condition rangeKeyCondition =
          new Condition()
              .withComparisonOperator(ComparisonOperator.EQ.toString())
              .withAttributeValueList(new AttributeValue().withS(rangeKey));
      DynamoDBQueryExpression<NodeHistory> queryExpression =
          new DynamoDBQueryExpression<NodeHistory>()
              .withHashKeyValues(nhKey)
              .withRangeKeyCondition("Time", rangeKeyCondition);
      latestNodeHistory = mapper.query(NodeHistory.class, queryExpression);
    } else {
      DynamoDBQueryExpression<NodeHistory> queryExpression =
          new DynamoDBQueryExpression<NodeHistory>().withHashKeyValues(nhKey);
      latestNodeHistory = mapper.query(NodeHistory.class, queryExpression);
    }

    List<NodeHistoryEntity> list = new ArrayList<>();

    for (NodeHistory n : latestNodeHistory) {
      NodeHistoryEntity ret = new NodeHistoryEntity();

      ret.setNID(n.getNodeID());
      ret.setTime(n.getTime());
      ret.setSAValues(n.getSAValues());

      list.add(ret);
    }

    return list;
  }