Java Code Examples for com.google.datastore.v1.Key#Builder

The following examples show how to use com.google.datastore.v1.Key#Builder . 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: V1TestUtil.java    From beam with Apache License 2.0 6 votes vote down vote up
/**
 * Build an entity for the given ancestorKey, kind, namespace and value.
 *
 * @param largePropertySize if greater than 0, add an unindexed property of the given size.
 */
static Entity makeEntity(
    Long value, Key ancestorKey, String kind, @Nullable String namespace, int largePropertySize) {
  Entity.Builder entityBuilder = Entity.newBuilder();
  Key.Builder keyBuilder = makeKey(ancestorKey, kind, UUID.randomUUID().toString());
  // NOTE: Namespace is not inherited between keys created with DatastoreHelper.makeKey, so
  // we must set the namespace on keyBuilder. TODO: Once partitionId inheritance is added,
  // we can simplify this code.
  if (namespace != null) {
    keyBuilder.getPartitionIdBuilder().setNamespaceId(namespace);
  }

  entityBuilder.setKey(keyBuilder.build());
  entityBuilder.putProperties("value", makeValue(value).build());
  if (largePropertySize > 0) {
    entityBuilder.putProperties(
        "unindexed_value",
        makeValue(new String(new char[largePropertySize]).replace("\0", "A"))
            .setExcludeFromIndexes(true)
            .build());
  }
  return entityBuilder.build();
}
 
Example 2
Source File: V1TestUtil.java    From beam with Apache License 2.0 5 votes vote down vote up
/** A helper function to create the ancestor key for all created and queried entities. */
static Key makeAncestorKey(@Nullable String namespace, String kind, String ancestor) {
  Key.Builder keyBuilder = makeKey(kind, ancestor);
  if (namespace != null) {
    keyBuilder.getPartitionIdBuilder().setNamespaceId(namespace);
  }
  return keyBuilder.build();
}
 
Example 3
Source File: DatastoreHelper.java    From google-cloud-datastore with Apache License 2.0 4 votes vote down vote up
/**
 * Make a key value.
 */
public static Value.Builder makeValue(Key.Builder key) {
  return makeValue(key.build());
}
 
Example 4
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;
}