Search in sources :

Example 91 with CommitRequest

use of com.google.firestore.v1beta1.CommitRequest in project java-firestore by googleapis.

the class WriteBatchTest method deleteDocument.

@Test
public void deleteDocument() throws Exception {
    doReturn(commitResponse(2, 0)).when(firestoreMock).sendRequest(commitCapture.capture(), Matchers.<UnaryCallable<CommitRequest, CommitResponse>>any());
    List<Write> writes = new ArrayList<>();
    batch.delete(documentReference);
    writes.add(delete());
    batch.delete(documentReference, Precondition.updatedAt(Timestamp.ofTimeSecondsAndNanos(1, 2)));
    com.google.firestore.v1.Precondition.Builder precondition = com.google.firestore.v1.Precondition.newBuilder();
    precondition.getUpdateTimeBuilder().setSeconds(1).setNanos(2);
    writes.add(delete(precondition.build()));
    assertEquals(2, batch.getMutationsSize());
    List<WriteResult> writeResults = batch.commit().get();
    for (int i = 0; i < writeResults.size(); ++i) {
        assertEquals(Timestamp.ofTimeSecondsAndNanos(i, i), writeResults.get(i).getUpdateTime());
    }
    CommitRequest commitRequest = commitCapture.getValue();
    assertEquals(commit(writes.toArray(new Write[] {})), commitRequest);
}
Also used : CommitRequest(com.google.firestore.v1.CommitRequest) Write(com.google.firestore.v1.Write) ArrayList(java.util.ArrayList) CommitResponse(com.google.firestore.v1.CommitResponse) Test(org.junit.Test)

Example 92 with CommitRequest

use of com.google.firestore.v1beta1.CommitRequest in project java-firestore by googleapis.

the class FirestoreTest method arrayUnionWithPojo.

@Test
public void arrayUnionWithPojo() throws ExecutionException, InterruptedException {
    doReturn(commitResponse(1, 0)).when(firestoreMock).sendRequest(commitCapture.capture(), Matchers.<UnaryCallable<CommitRequest, CommitResponse>>any());
    DocumentReference doc = firestoreMock.document("coll/doc");
    doc.update("array", FieldValue.arrayUnion(SINGLE_FIELD_OBJECT)).get();
    CommitRequest expectedRequest = commit(update(Collections.emptyMap(), new ArrayList<>()), transform("array", arrayUnion(SINGLE_FIELD_VALUE)));
    CommitRequest actualRequest = commitCapture.getValue();
    assertEquals(expectedRequest, actualRequest);
}
Also used : CommitRequest(com.google.firestore.v1.CommitRequest) ArrayList(java.util.ArrayList) CommitResponse(com.google.firestore.v1.CommitResponse) Test(org.junit.Test)

Example 93 with CommitRequest

use of com.google.firestore.v1beta1.CommitRequest in project appengine-java-standard by GoogleCloudPlatform.

the class AsyncCloudDatastoreV1ServiceImplTest method testCommitTriggersGetOnAllFutures.

@Test
public void testCommitTriggersGetOnAllFutures() throws Exception {
    // This test will have 2 Put calls and 1 Get call within a transaction.
    Entity putEntity1 = new Entity("Foo1");
    putEntity1.setProperty("aString", "test1");
    Entity putEntity2 = new Entity("Foo2");
    putEntity1.setProperty("aString", "test2");
    Key getKey1 = KeyFactory.createKey("Foo3", "name1");
    Entity expectedGetEntity = new Entity(getKey1);
    expectedGetEntity.setProperty("aString", "test3");
    ByteString remoteTxn = expectBeginTransaction();
    AllocateIdsRequest expectedAllocIdsReq1 = createAllocateIdsRequest(putEntity1);
    AllocateIdsRequest expectedAllocIdsReq2 = createAllocateIdsRequest(putEntity2);
    LookupRequest expectedLookupRequest = createLookupRequest(remoteTxn, getKey1).build();
    // Use TrackingFutures to allow us to assert exactly when Future.get is called.
    TrackingFuture<AllocateIdsResponse> trackingAllocIdRespFuture1 = new TrackingFuture<>(createAllocateIdsResponse(expectedAllocIdsReq1, 11L));
    TrackingFuture<AllocateIdsResponse> trackingAllocIdRespFuture2 = new TrackingFuture<>(createAllocateIdsResponse(expectedAllocIdsReq2, 22L));
    CommitRequest expectedPutRequest = createPutCommitRequest(remoteTxn, copyWithNewId(putEntity2, 22L), copyWithNewId(putEntity1, 11L)).build();
    CommitResponse expectedPutResponse = CommitResponse.newBuilder().addMutationResults(MutationResult.getDefaultInstance()).addMutationResults(MutationResult.getDefaultInstance()).build();
    TrackingFuture<CommitResponse> trackingPutResponseFuture = new TrackingFuture<>(expectedPutResponse);
    TrackingFuture<LookupResponse> trackingLookupResponseFuture = new TrackingFuture<>(createLookupResponse(expectedGetEntity));
    expectAllocateIds(expectedAllocIdsReq1, trackingAllocIdRespFuture1);
    expectAllocateIds(expectedAllocIdsReq2, trackingAllocIdRespFuture2);
    expectLookup(expectedLookupRequest, trackingLookupResponseFuture);
    expectCommitRequest(expectedPutRequest, trackingPutResponseFuture);
    replay();
    AsyncCloudDatastoreV1ServiceImpl ads = newAsyncDatastoreService();
    // Begin a transaction.
    Transaction txn = ads.beginTransaction().get();
    // Issue the async calls.
    Future<Key> putFuture1 = ads.put(putEntity1);
    Future<Key> putFuture2 = ads.put(putEntity2);
    Future<Entity> getFuture = ads.get(getKey1);
    // Nothing should have triggered the Future.get calls yet.
    assertThat(trackingAllocIdRespFuture1.getNumCallsToGet()).isEqualTo(0);
    assertThat(trackingAllocIdRespFuture2.getNumCallsToGet()).isEqualTo(0);
    assertThat(trackingPutResponseFuture.getNumCallsToGet()).isEqualTo(0);
    assertThat(trackingLookupResponseFuture.getNumCallsToGet()).isEqualTo(0);
    // Simulate user code getting one of the Futures.
    Key putResult2 = putFuture2.get();
    assertThat(putResult2.getId()).isEqualTo(22L);
    // That should be the only one of the four underlying Futures that had get() called.
    assertThat(trackingAllocIdRespFuture1.getNumCallsToGet()).isEqualTo(0);
    assertThat(trackingAllocIdRespFuture2.getNumCallsToGet()).isEqualTo(1);
    assertThat(trackingPutResponseFuture.getNumCallsToGet()).isEqualTo(0);
    assertThat(trackingLookupResponseFuture.getNumCallsToGet()).isEqualTo(0);
    // Commit the transaction.  This should trigger the other underlying Futures.
    txn.commit();
    assertThat(trackingAllocIdRespFuture1.getNumCallsToGet()).isEqualTo(1);
    assertThat(trackingAllocIdRespFuture2.getNumCallsToGet()).isEqualTo(1);
    assertThat(trackingPutResponseFuture.getNumCallsToGet()).isEqualTo(1);
    assertThat(trackingLookupResponseFuture.getNumCallsToGet()).isEqualTo(1);
    // User code gets the remaining results after the commit.
    Key putResult1 = putFuture1.get();
    assertThat(putResult1.getId()).isEqualTo(11L);
    Entity getResult = getFuture.get();
    assertThat(getResult).isEqualTo(expectedGetEntity);
    // This triggers an additional call on the getFuture, but no additional calls to the
    // CloudDatastoreV1Proxy.
    assertThat(trackingAllocIdRespFuture1.getNumCallsToGet()).isEqualTo(1);
    assertThat(trackingAllocIdRespFuture2.getNumCallsToGet()).isEqualTo(1);
    assertThat(trackingPutResponseFuture.getNumCallsToGet()).isEqualTo(1);
    assertThat(trackingLookupResponseFuture.getNumCallsToGet()).isEqualTo(2);
    verify();
}
Also used : CommitRequest(com.google.datastore.v1.CommitRequest) ByteString(com.google.protobuf.ByteString) CommitResponse(com.google.datastore.v1.CommitResponse) AllocateIdsRequest(com.google.datastore.v1.AllocateIdsRequest) LookupResponse(com.google.datastore.v1.LookupResponse) LookupRequest(com.google.datastore.v1.LookupRequest) AllocateIdsResponse(com.google.datastore.v1.AllocateIdsResponse) Test(org.junit.Test)

Example 94 with CommitRequest

use of com.google.firestore.v1beta1.CommitRequest in project appengine-java-standard by GoogleCloudPlatform.

the class CloudDatastoreV1ServiceImplTest method putAndReturnError.

private void putAndReturnError(Code code) throws Exception {
    Entity golden = new Entity("Foo");
    golden.setProperty("aString", "test");
    golden.setProperty("anInteger", 42);
    golden.setProperty("aFloat", 62.3);
    golden.getKey().simulatePutForTesting(12345L);
    ByteString remoteTxn = maybeExpectBeginTransaction();
    CommitRequest putRequest = createPutCommitRequest(remoteTxn, golden).build();
    expectCommit(putRequest, code);
    replay();
    newDatastoreService().put(golden);
    throw new AssertionError("should have thrown an exception");
}
Also used : CommitRequest(com.google.datastore.v1.CommitRequest) ByteString(com.google.protobuf.ByteString)

Example 95 with CommitRequest

use of com.google.firestore.v1beta1.CommitRequest in project grpc-gcp-java by GoogleCloudPlatform.

the class DataStoreChecksumClient method commitTransaction.

private static void commitTransaction(DatastoreBlockingStub stub, String projectId) {
    int transactionBytes = 2000;
    String payload = new String(new char[transactionBytes]).replace('\0', 'x');
    Key key = Key.newBuilder().addPath(PathElement.newBuilder().setKind("Data").setName("data1").build()).build();
    Entity entity = Entity.newBuilder().setKey(key).putProperties("2kb", Value.newBuilder().setExcludeFromIndexes(true).setStringValue(payload).build()).build();
    Mutation mutation = Mutation.newBuilder().setUpdate(entity).build();
    CommitRequest commitRequest = CommitRequest.newBuilder().setProjectId(projectId).setMode(Mode.NON_TRANSACTIONAL).addMutations(mutation).build();
    CommitResponse commitResponse = stub.commit(commitRequest);
    System.out.println("index updates: " + commitResponse.getIndexUpdates());
}
Also used : CommitRequest(com.google.datastore.v1.CommitRequest) Entity(com.google.datastore.v1.Entity) CommitResponse(com.google.datastore.v1.CommitResponse) Mutation(com.google.datastore.v1.Mutation) Key(com.google.datastore.v1.Key)

Aggregations

Test (org.junit.Test)85 CommitRequest (com.google.spanner.v1.CommitRequest)47 CommitRequest (com.google.firestore.v1.CommitRequest)40 CommitResponse (com.google.firestore.v1.CommitResponse)40 ArrayList (java.util.ArrayList)26 Connection (java.sql.Connection)23 ByteString (com.google.protobuf.ByteString)14 ExecuteSqlRequest (com.google.spanner.v1.ExecuteSqlRequest)12 SQLException (java.sql.SQLException)12 Statement (java.sql.Statement)12 CopyManager (org.postgresql.copy.CopyManager)11 BaseConnection (org.postgresql.core.BaseConnection)11 ExecuteBatchDmlRequest (com.google.spanner.v1.ExecuteBatchDmlRequest)10 ArrayValue (com.google.firestore.v1.ArrayValue)9 MapValue (com.google.firestore.v1.MapValue)9 Value (com.google.firestore.v1.Value)9 Write (com.google.firestore.v1.Write)9 StringReader (java.io.StringReader)9 HashMap (java.util.HashMap)9 AbstractMessage (com.google.protobuf.AbstractMessage)8