Search in sources :

Example 21 with Key

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;
}
Also used : ByteString(com.google.protobuf.ByteString) PartitionId(com.google.datastore.v1.PartitionId) PathElement(com.google.datastore.v1.Key.PathElement) Key(com.google.datastore.v1.Key) PrivateKey(java.security.PrivateKey)

Example 22 with 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;
}
Also used : Query(com.google.datastore.v1.Query) ArrayList(java.util.ArrayList) Key(com.google.datastore.v1.Key)

Example 23 with Key

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));
}
Also used : State(org.apache.beam.sdk.PipelineResult.State) PipelineResult(org.apache.beam.sdk.PipelineResult) BeamSqlEnv(org.apache.beam.sdk.extensions.sql.impl.BeamSqlEnv) ByteString(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.avatica.util.ByteString) Row(org.apache.beam.sdk.values.Row) EntityToRow(org.apache.beam.sdk.io.gcp.datastore.EntityToRow) Key(com.google.datastore.v1.Key) DatastoreHelper.makeKey(com.google.datastore.v1.client.DatastoreHelper.makeKey) Test(org.junit.Test)

Example 24 with Key

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);
}
Also used : DeleteKeyFn(org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.DeleteKeyFn) DeleteKey(org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.DeleteKey) Key(com.google.datastore.v1.Key) DatastoreHelper.makeKey(com.google.datastore.v1.client.DatastoreHelper.makeKey) DatastoreV1.isValidKey(org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.isValidKey) Test(org.junit.Test)

Example 25 with 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);
}
Also used : Entity(com.google.datastore.v1.Entity) DeleteEntity(org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.DeleteEntity) UpsertFn(org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.UpsertFn) DeleteKey(org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.DeleteKey) Key(com.google.datastore.v1.Key) DatastoreHelper.makeKey(com.google.datastore.v1.client.DatastoreHelper.makeKey) DatastoreV1.isValidKey(org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.isValidKey) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)27 Key (com.google.datastore.v1.Key)17 AbstractMessage (com.google.protobuf.AbstractMessage)12 DatastoreHelper.makeKey (com.google.datastore.v1.client.DatastoreHelper.makeKey)11 Key (com.google.recaptchaenterprise.v1.Key)10 RecaptchaEnterpriseServiceClient (com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient)8 Entity (com.google.datastore.v1.Entity)7 DeleteKey (org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.DeleteKey)7 DatastoreV1.isValidKey (org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.isValidKey)7 Mutation (com.google.datastore.v1.Mutation)5 ArrayList (java.util.ArrayList)5 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)4 GetKeyRequest (com.google.recaptchaenterprise.v1.GetKeyRequest)4 Key (com.google.recaptchaenterprise.v1beta1.Key)4 StatusRuntimeException (io.grpc.StatusRuntimeException)4 HashMap (java.util.HashMap)4 Query (com.google.datastore.v1.Query)3 DeleteKeyRequest (com.google.recaptchaenterprise.v1.DeleteKeyRequest)3 GetMetricsRequest (com.google.recaptchaenterprise.v1.GetMetricsRequest)3 Metrics (com.google.recaptchaenterprise.v1.Metrics)3