response = datastore.lookup(lookupRequest.build());
@Test public void testRuntimeException() throws Exception { LookupRequest requestPb = LookupRequest.newBuilder().addKeys(KEY1.toPb()).build(); String exceptionMessage = "Artificial runtime exception"; EasyMock.expect(rpcMock.lookup(requestPb)).andThrow(new RuntimeException(exceptionMessage)); EasyMock.replay(rpcFactoryMock, rpcMock); Datastore datastore = rpcMockOptions.getService(); thrown.expect(DatastoreException.class); thrown.expectMessage(exceptionMessage); datastore.get(KEY1); EasyMock.verify(rpcFactoryMock, rpcMock); } }
private void loadResults() { com.google.datastore.v1.LookupResponse responsePb = lookup(requestPb.build()); iter = responsePb.getFoundList().iterator(); requestPb.clearKeys(); if (responsePb.getDeferredCount() > 0) { requestPb.addAllKeys(responsePb.getDeferredList()); } }
LookupResponse lresp = datastore.lookup(lreq.build());
// Create an RPC request to begin a new transaction. BeginTransactionRequest.Builder treq = BeginTransactionRequest.newBuilder(); // Execute the RPC synchronously. BeginTransactionResponse tres = datastore.beginTransaction(treq.build()); // Get the transaction handle from the response. ByteString tx = tres.getTransaction(); // Create an RPC request to get entities by key. LookupRequest.Builder lreq = LookupRequest.newBuilder(); // Set the entity key with only one `path_element`: no parent. Key.Builder key = Key.newBuilder().addPathElement( Key.PathElement.newBuilder() .setKind("Trivia") .setName("hgtg")); // Add one key to the lookup request. lreq.addKey(key); // Set the transaction, so we get a consistent snapshot of the // entity at the time the transaction started. lreq.getReadOptionsBuilder().setTransaction(tx); // Execute the RPC and get the response. LookupResponse lresp = datastore.lookup(lreq.build()); // Create an RPC request to commit the transaction. CommitRequest.Builder creq = CommitRequest.newBuilder(); // Set the transaction to commit. creq.setTransaction(tx); Entity entity; if (lresp.getFoundCount() > 0) { entity = lresp.getFound(0).getEntity(); }
@Override public ListenableFuture<QueryResult> executeAsync(final List<KeyQuery> statements, final ListenableFuture<TransactionResult> txn) { final ListenableFuture<Response> httpResponse = Futures.transformAsync(txn, result -> { final List<com.google.datastore.v1.Key> keys = statements .stream().map(s -> s.getKey().getPb(config.getNamespace())).collect(Collectors.toList()); final LookupRequest.Builder request = LookupRequest.newBuilder().addAllKeys(keys); final ByteString transaction = result.getTransaction(); if (transaction != null) { request.setReadOptions(ReadOptions.newBuilder().setTransaction(transaction)); } final ProtoHttpContent payload = new ProtoHttpContent(request.build()); return ListenableFutureAdapter.asGuavaFuture(prepareRequest("lookup", payload).execute()); }, MoreExecutors.directExecutor()); return Futures.transformAsync(httpResponse, response -> { if (!isSuccessful(response.getStatusCode())) { throw new DatastoreException(response.getStatusCode(), response.getResponseBody()); } final LookupResponse query = LookupResponse.parseFrom(streamResponse(response)); return Futures.immediateFuture(QueryResult.build(query)); }, MoreExecutors.directExecutor()); }
private void loadResults() { com.google.datastore.v1.LookupResponse responsePb = lookup(requestPb.build()); iter = responsePb.getFoundList().iterator(); requestPb.clearKeys(); if (responsePb.getDeferredCount() > 0) { requestPb.addAllKeys(responsePb.getDeferredList()); } }
keysPb.add(KEY5.toPb()); List<LookupRequest> lookupRequests = new ArrayList<>(); lookupRequests.add(LookupRequest.newBuilder().addAllKeys(keysPb).build()); lookupRequests.add( LookupRequest.newBuilder() .addKeys(keysPb.get(2)) .addKeys(keysPb.get(4)) .build()); lookupRequests.add(LookupRequest.newBuilder().addKeys(keysPb.get(4)).build()); Entity entity4 = Entity.newBuilder(KEY4).set("value", StringValue.of("value")).build(); Entity entity5 = Entity.newBuilder(KEY5).set("value", "value").build();
@Test public void testLookupEventualConsistency() { ReadOptions readOption = ReadOptions.newBuilder().setReadConsistencyValue(ReadConsistency.EVENTUAL_VALUE).build(); com.google.datastore.v1.Key key = com.google.datastore.v1.Key.newBuilder() .setPartitionId(PartitionId.newBuilder().setProjectId(PROJECT_ID).build()) .addPath( com.google.datastore.v1.Key.PathElement.newBuilder() .setKind("kind1") .setName("name") .build()) .build(); LookupRequest lookupRequest = LookupRequest.newBuilder().setReadOptions(readOption).addKeys(key).build(); EasyMock.expect(rpcMock.lookup(lookupRequest)) .andReturn(LookupResponse.newBuilder().build()) .times(3); EasyMock.replay(rpcFactoryMock, rpcMock); Datastore datastore = rpcMockOptions.getService(); datastore.get(KEY1, ReadOption.eventualConsistency()); datastore.get(ImmutableList.of(KEY1), ReadOption.eventualConsistency()); datastore.fetch(ImmutableList.of(KEY1), ReadOption.eventualConsistency()); EasyMock.verify(rpcFactoryMock, rpcMock); }
@Test public void testRetryableException() throws Exception { LookupRequest requestPb = LookupRequest.newBuilder().addKeys(KEY1.toPb()).build(); LookupResponse responsePb = LookupResponse.newBuilder() .addFound(EntityResult.newBuilder().setEntity(ENTITY1.toPb())) .build(); EasyMock.expect(rpcMock.lookup(requestPb)) .andThrow(new DatastoreException(14, "UNAVAILABLE", "UNAVAILABLE", null)) .andReturn(responsePb); EasyMock.replay(rpcFactoryMock, rpcMock); Datastore datastore = rpcMockOptions.getService(); Entity entity = datastore.get(KEY1); assertEquals(ENTITY1, entity); EasyMock.verify(rpcFactoryMock, rpcMock); }
@Test public void testNonRetryableException() throws Exception { LookupRequest requestPb = LookupRequest.newBuilder().addKeys(KEY1.toPb()).build(); EasyMock.expect(rpcMock.lookup(requestPb)) .andThrow( new DatastoreException(DatastoreException.UNKNOWN_CODE, "denied", "PERMISSION_DENIED")) .times(1); EasyMock.replay(rpcFactoryMock, rpcMock); Datastore datastore = rpcMockOptions.getService(); thrown.expect(DatastoreException.class); thrown.expectMessage("denied"); datastore.get(KEY1); EasyMock.verify(rpcFactoryMock, rpcMock); }