Search in sources :

Example 16 with Key

use of com.google.recaptchaenterprise.v1.Key in project beam by apache.

the class DatastoreV1Test method testDeleteKeys.

/**
 * Test that valid keys are transformed to delete mutations.
 */
@Test
public void testDeleteKeys() {
    Key key = makeKey("bird", "finch").build();
    DeleteKeyFn deleteKeyFn = new DeleteKeyFn();
    Mutation expectedMutation = makeDelete(key).build();
    assertEquals(expectedMutation, deleteKeyFn.apply(key));
}
Also used : DeleteKeyFn(org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.DeleteKeyFn) Mutation(com.google.datastore.v1.Mutation) 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 17 with Key

use of com.google.recaptchaenterprise.v1.Key in project beam by apache.

the class DatastoreV1Test method testAddEntities.

@Test
public /**
 * Test that entities with valid keys are transformed to upsert mutations.
 */
void testAddEntities() throws Exception {
    Key key = makeKey("bird", "finch").build();
    Entity entity = Entity.newBuilder().setKey(key).build();
    UpsertFn upsertFn = new UpsertFn();
    Mutation expectedMutation = makeUpsert(entity).build();
    assertEquals(expectedMutation, 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) Mutation(com.google.datastore.v1.Mutation) 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 18 with Key

use of com.google.recaptchaenterprise.v1.Key in project beam by apache.

the class V1WriteIT method testDatastoreWriterFnWithDuplicatedEntities.

/**
 * Tests {@link DatastoreV1.DatastoreWriterFn} with duplicated entries. Once a duplicated entry is
 * found the batch gets flushed.
 */
@Test
public void testDatastoreWriterFnWithDuplicatedEntities() throws Exception {
    List<Mutation> mutations = new ArrayList<>(200);
    V1TestOptions options = TestPipeline.testingPipelineOptions().as(V1TestOptions.class);
    Pipeline pipeline = TestPipeline.create(options);
    for (int i = 1; i <= 200; i++) {
        Key key = makeKey("key" + i, i + 1).build();
        mutations.add(makeUpsert(Entity.newBuilder().setKey(key).build()).build());
        if (i % 30 == 0) {
            mutations.add(makeUpsert(Entity.newBuilder().setKey(key).build()).build());
        }
    }
    DatastoreV1.DatastoreWriterFn datastoreWriter = new DatastoreV1.DatastoreWriterFn(TestPipeline.testingPipelineOptions().as(GcpOptions.class).getProject(), null);
    PTransform<PCollection<? extends Mutation>, PCollection<Void>> datastoreWriterTransform = ParDo.of(datastoreWriter);
    /**
     * Following three lines turn the original arrayList into a member of the first PCollection
     */
    List<Mutation> newArrayList = new ArrayList<>(mutations);
    Create.Values<Iterable<Mutation>> mutationIterable = Create.of(Collections.singleton(newArrayList));
    PCollection<Iterable<Mutation>> input = pipeline.apply(mutationIterable);
    /**
     * Flatten divides the PCollection into several elements of the same bundle. By doing this we're
     * forcing the processing of the List of mutation in the same order the mutations were added to
     * the original List.
     */
    input.apply(Flatten.<Mutation>iterables()).apply(datastoreWriterTransform);
    PipelineResult pResult = pipeline.run();
    MetricQueryResults metricResults = pResult.metrics().queryMetrics(MetricsFilter.builder().addNameFilter(MetricNameFilter.named(DatastoreV1.DatastoreWriterFn.class, "batchSize")).build());
    AtomicLong timesCommitted = new AtomicLong();
    metricResults.getDistributions().forEach(distribution -> {
        if (distribution.getName().getName().equals("batchSize")) {
            timesCommitted.set(distribution.getCommitted().getCount());
        }
    });
    assertEquals(7, timesCommitted.get());
}
Also used : ArrayList(java.util.ArrayList) MetricQueryResults(org.apache.beam.sdk.metrics.MetricQueryResults) PipelineResult(org.apache.beam.sdk.PipelineResult) TestPipeline(org.apache.beam.sdk.testing.TestPipeline) Pipeline(org.apache.beam.sdk.Pipeline) PCollection(org.apache.beam.sdk.values.PCollection) AtomicLong(java.util.concurrent.atomic.AtomicLong) Create(org.apache.beam.sdk.transforms.Create) Mutation(com.google.datastore.v1.Mutation) Key(com.google.datastore.v1.Key) DatastoreHelper.makeKey(com.google.datastore.v1.client.DatastoreHelper.makeKey) Test(org.junit.Test)

Example 19 with Key

use of com.google.recaptchaenterprise.v1.Key in project java-datastore by googleapis.

the class QuerySplitterImpl method getScatterKeys.

/**
 * Gets a list of split keys given a desired number of splits.
 *
 * <p>This list will contain multiple split keys for each split. Only a single split key will be
 * chosen as the split point, however providing multiple keys allows for more uniform sharding.
 *
 * @param numSplits the number of desired splits.
 * @param query the user query.
 * @param partition the partition to run the query in.
 * @param datastore the datastore containing the data.
 * @throws DatastoreException if there was an error when executing the datastore query.
 */
private List<Key> getScatterKeys(int numSplits, Query query, PartitionId partition, Datastore datastore) throws DatastoreException {
    Query.Builder scatterPointQuery = createScatterQuery(query, numSplits);
    List<Key> keySplits = new ArrayList<Key>();
    QueryResultBatch batch;
    do {
        RunQueryRequest scatterRequest = RunQueryRequest.newBuilder().setPartitionId(partition).setQuery(scatterPointQuery).build();
        batch = datastore.runQuery(scatterRequest).getBatch();
        for (EntityResult result : batch.getEntityResultsList()) {
            keySplits.add(result.getEntity().getKey());
        }
        scatterPointQuery.setStartCursor(batch.getEndCursor());
        scatterPointQuery.getLimitBuilder().setValue(scatterPointQuery.getLimit().getValue() - batch.getEntityResultsCount());
    } while (batch.getMoreResults() == MoreResultsType.NOT_FINISHED);
    Collections.sort(keySplits, DatastoreHelper.getKeyComparator());
    return keySplits;
}
Also used : QueryResultBatch(com.google.datastore.v1.QueryResultBatch) Query(com.google.datastore.v1.Query) RunQueryRequest(com.google.datastore.v1.RunQueryRequest) ArrayList(java.util.ArrayList) EntityResult(com.google.datastore.v1.EntityResult) Key(com.google.datastore.v1.Key)

Example 20 with Key

use of com.google.recaptchaenterprise.v1.Key in project java-datastore by googleapis.

the class DatastoreHelperTest method testMakeKey_PartitionId.

@Test
public void testMakeKey_PartitionId() {
    PartitionId partitionId = PartitionId.newBuilder().setNamespaceId("namespace-id").build();
    Key parent = PARENT.toBuilder().setPartitionId(partitionId).build();
    assertEquals(Key.newBuilder().setPartitionId(partitionId).addPath(PARENT.getPath(0)).addPath(Key.PathElement.newBuilder().setKind("Child")).build(), makeKey(parent, "Child").build());
}
Also used : PartitionId(com.google.datastore.v1.PartitionId) Key(com.google.datastore.v1.Key) DatastoreHelper.makeKey(com.google.datastore.v1.client.DatastoreHelper.makeKey) 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