use of com.google.recaptchaenterprise.v1.Key in project java-datastore by googleapis.
the class DatastoreHelper method makeKey.
/**
* 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;
}
use of com.google.recaptchaenterprise.v1.Key in project java-datastore by googleapis.
the class QuerySplitterImpl method getSplits.
@Override
public List<Query> getSplits(Query query, PartitionId partition, int numSplits, Datastore datastore) throws DatastoreException, IllegalArgumentException {
List<Query> splits = new ArrayList<Query>(numSplits);
if (numSplits == 1) {
splits.add(query);
return splits;
}
validateQuery(query);
validateSplitSize(numSplits);
List<Key> scatterKeys = getScatterKeys(numSplits, query, partition, datastore);
Key lastKey = null;
for (Key nextKey : getSplitKey(scatterKeys, numSplits)) {
splits.add(createSplit(lastKey, nextKey, query));
lastKey = nextKey;
}
splits.add(createSplit(lastKey, null, query));
return splits;
}
use of com.google.recaptchaenterprise.v1.Key in project beam by apache.
the class DataStoreReadWriteIT method testDataStoreV1SqlWriteRead.
@Test
public void testDataStoreV1SqlWriteRead() {
BeamSqlEnv sqlEnv = BeamSqlEnv.inMemory(new DataStoreV1TableProvider());
String projectId = options.getProject();
String createTableStatement = "CREATE EXTERNAL TABLE TEST( \n" + " `__key__` VARBINARY, \n" + " `content` VARCHAR \n" + ") \n" + "TYPE 'datastoreV1' \n" + "LOCATION '" + projectId + "/" + KIND + "'";
sqlEnv.executeDdl(createTableStatement);
Key ancestor = makeKey(KIND, UUID.randomUUID().toString()).build();
Key itemKey = makeKey(ancestor, KIND, UUID.randomUUID().toString()).build();
String insertStatement = "INSERT INTO TEST VALUES ( \n" + keyToSqlByteString(itemKey) + ", \n" + "'2000' \n" + ")";
BeamSqlRelUtils.toPCollection(writePipeline, sqlEnv.parseQuery(insertStatement));
writePipeline.run().waitUntilFinish();
String selectTableStatement = "SELECT * FROM TEST";
PCollection<Row> output = BeamSqlRelUtils.toPCollection(readPipeline, sqlEnv.parseQuery(selectTableStatement));
assertThat(output.getSchema(), equalTo(SOURCE_SCHEMA));
PipelineResult.State state = readPipeline.run().waitUntilFinish(Duration.standardMinutes(5));
assertThat(state, equalTo(State.DONE));
}
use of com.google.recaptchaenterprise.v1.Key in project beam by apache.
the class DatastoreV1Test method testDeleteIncompleteKeys.
/**
* Test that incomplete keys cannot be deleted.
*/
@Test
public void testDeleteIncompleteKeys() throws Exception {
Key key = makeKey("bird").build();
DeleteKeyFn deleteKeyFn = new DeleteKeyFn();
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Keys to be deleted from the Cloud Datastore must be complete");
deleteKeyFn.apply(key);
}
use of com.google.recaptchaenterprise.v1.Key in project beam by apache.
the class DatastoreV1Test method testAddEntitiesWithIncompleteKeys.
/**
* Test that entities with incomplete keys cannot be updated.
*/
@Test
public void testAddEntitiesWithIncompleteKeys() throws Exception {
Key key = makeKey("bird").build();
Entity entity = Entity.newBuilder().setKey(key).build();
UpsertFn upsertFn = new UpsertFn();
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Entities to be written to the Cloud Datastore must have complete keys");
upsertFn.apply(entity);
}
Aggregations