com.google.datastore.v1.Key.PathElement Java Examples

The following examples show how to use com.google.datastore.v1.Key.PathElement. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DatastoreHelper.java    From google-cloud-datastore with Apache License 2.0 6 votes vote down vote up
private int comparePathElement(PathElement thisElement, PathElement otherElement) {
  int result = thisElement.getKind().compareTo(otherElement.getKind());
  if (result != 0) {
    return result;
  }
  if (thisElement.getIdTypeCase() == IdTypeCase.ID) {
    if (otherElement.getIdTypeCase() != IdTypeCase.ID) {
      return -1;
    }
    return Long.valueOf(thisElement.getId()).compareTo(otherElement.getId());
  }
  if (otherElement.getIdTypeCase() == IdTypeCase.ID) {
    return 1;
  }

  return thisElement.getName().compareTo(otherElement.getName());
}
 
Example #2
Source File: DatastoreHelper.java    From google-cloud-datastore with Apache License 2.0 6 votes vote down vote up
@Override
public int compare(Key thisKey, Key otherKey) {
  if (!thisKey.getPartitionId().equals(otherKey.getPartitionId())) {
    throw new IllegalArgumentException("Cannot compare keys with different partition ids.");
  }

  Iterator<PathElement> thisPath = thisKey.getPathList().iterator();
  Iterator<PathElement> otherPath = otherKey.getPathList().iterator();
  while (thisPath.hasNext()) {
    if (!otherPath.hasNext()) {
      return 1;
    }
    int result = comparePathElement(thisPath.next(), otherPath.next());
    if (result != 0) {
      return result;
    }
  }

  return otherPath.hasNext() ? -1 : 0;
}
 
Example #3
Source File: DatastoreV1.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Returns true if a Cloud Datastore key is complete. A key is complete if its last element has
 * either an id or a name.
 */
static boolean isValidKey(Key key) {
  List<PathElement> elementList = key.getPathList();
  if (elementList.isEmpty()) {
    return false;
  }
  PathElement lastElement = elementList.get(elementList.size() - 1);
  return (lastElement.getId() != 0 || !lastElement.getName().isEmpty());
}
 
Example #4
Source File: V1TestUtil.java    From beam with Apache License 2.0 5 votes vote down vote up
static boolean isValidKey(Key key) {
  List<PathElement> elementList = key.getPathList();
  if (elementList.isEmpty()) {
    return false;
  }
  PathElement lastElement = elementList.get(elementList.size() - 1);
  return (lastElement.getId() != 0 || !lastElement.getName().isEmpty());
}
 
Example #5
Source File: DatastoreConvertersTest.java    From DataflowTemplates with Apache License 2.0 4 votes vote down vote up
@Before
public void testData() {
  entities = new ArrayList<>();
  entities.add(Entity.newBuilder()
      .setKey(Key.newBuilder()
          .setPartitionId(PartitionId.newBuilder()
              .setProjectId("my-project")
              .setNamespaceId("some-namespace"))
          .addPath(PathElement.newBuilder()
              .setId(1234)
              .setKind("monkey")))
      .putProperties("someString", Value.newBuilder()
          .setStringValue("someValue")
          .build())
      .build());
  entities.add(Entity.newBuilder()
      .setKey(Key.newBuilder()
          .setPartitionId(PartitionId.newBuilder()
              .setProjectId("my-project")
              .setNamespaceId("some-namespace"))
          .addPath(PathElement.newBuilder()
              .setName("SomeName")
              .setKind("monkey")))
      .putProperties("someString", Value.newBuilder()
          .setIntegerValue(100L)
          .build())
      .putProperties("someSubRecord", Value.newBuilder()
          .setEntityValue(Entity.newBuilder()
              .putProperties("someSubFloat", Value.newBuilder()
                  .setDoubleValue(0.234)
                  .build()))
          .build())
      .putProperties("someArray", Value.newBuilder()
          .setArrayValue(ArrayValue.newBuilder()
              .addValues(Value.newBuilder()
                  .setIntegerValue(1234L))
              .addValues(Value.newBuilder()
                  .setIntegerValue(1234L))
              .addValues(Value.newBuilder()
                  .setArrayValue(ArrayValue.newBuilder()
                      .addValues(Value.newBuilder()
                          .setStringValue("Some nested string in a nested array"))))
              .build())
          .build())
      .build());

  entitiesJson = new LinkedList<>();
  entitiesJson.add(
      "{\"key\":{\"partitionId\":{\"projectId\":\"my-project\","
          + "\"namespaceId\":\"some-namespace\"},\"path\":"
          + "[{\"kind\":\"monkey\",\"id\":\"1234\"}]},"
          + "\"properties\":{\"someString\":{\"stringValue\":\"someValue\"}}}");

}
 
Example #6
Source File: DatastoreHelper.java    From google-cloud-datastore with Apache License 2.0 4 votes vote down vote up
/**
 * Make a key from the specified path of kind/id-or-name pairs
 * and/or Keys.
 *
 * <p>The id-or-name values must be either String, Long, Integer or Short.
 *
 * <p>The last id-or-name value may be omitted, in which case an entity without
 * an id is created (for use with automatic id allocation).
 *
 * <p>The PartitionIds of all Keys in the path must be equal. The returned
 * Key.Builder will use this PartitionId.
 */
public static Key.Builder makeKey(Object... elements) {
  Key.Builder key = Key.newBuilder();
  PartitionId partitionId = null;
  for (int pathIndex = 0; pathIndex < elements.length; pathIndex += 2) {
    PathElement.Builder pathElement = PathElement.newBuilder();
    Object element =  elements[pathIndex];
    if (element instanceof Key) {
      Key subKey = (Key) element;
      if (partitionId == null) {
        partitionId = subKey.getPartitionId();
      } else if (!partitionId.equals(subKey.getPartitionId())) {
        throw new IllegalArgumentException("Partition IDs did not match, found: "
            + partitionId + " and " + subKey.getPartitionId());
      }
      key.addAllPath(((Key) element).getPathList());
      // We increment by 2, but since we got a Key argument we're only consuming 1 element in this
      // iteration of the loop. Decrement the index so that when we jump by 2 we end up in the
      // right spot.
      pathIndex--;
    } else {
      String kind;
      try {
        kind = (String) element;
      } catch (ClassCastException e) {
        throw new IllegalArgumentException("Expected string or Key, got: " + element.getClass());
      }
      pathElement.setKind(kind);
      if (pathIndex + 1 < elements.length) {
        Object value = elements[pathIndex + 1];
        if (value instanceof String) {
          pathElement.setName((String) value);
        } else if (value instanceof Long) {
          pathElement.setId((Long) value);
        } else if (value instanceof Integer) {
          pathElement.setId((Integer) value);
        } else if (value instanceof Short) {
          pathElement.setId((Short) value);
        } else {
          throw new IllegalArgumentException(
              "Expected string or integer, got: " + value.getClass());
        }
      }
      key.addPath(pathElement);
    }
  }
  if (partitionId != null && !partitionId.equals(PartitionId.getDefaultInstance())) {
    key.setPartitionId(partitionId);
  }
  return key;
}