/** Returns the key in an encoded form that can be used as part of a URL. */ public String toUrlSafe() { try { return URLEncoder.encode(TextFormat.printToString(toPb()), UTF_8.name()); } catch (UnsupportedEncodingException e) { throw new IllegalStateException("Unexpected encoding exception", e); } }
@Override public void delete(Key... keys) { if (keys.length > 0) { List<com.google.datastore.v1.Mutation> mutationsPb = new ArrayList<>(); Set<Key> dedupKeys = new LinkedHashSet<>(Arrays.asList(keys)); for (Key key : dedupKeys) { mutationsPb.add( com.google.datastore.v1.Mutation.newBuilder().setDelete(key.toPb()).build()); } commitMutation(mutationsPb); } }
@Override protected void setValue(KeyValue from, com.google.datastore.v1.Value.Builder to) { to.setKeyValue(from.get().toPb()); } };
Iterator<Entity> get(com.google.datastore.v1.ReadOptions readOptionsPb, final Key... keys) { if (keys.length == 0) { return Collections.emptyIterator(); } com.google.datastore.v1.LookupRequest.Builder requestPb = com.google.datastore.v1.LookupRequest.newBuilder(); if (readOptionsPb != null) { requestPb.setReadOptions(readOptionsPb); } for (Key k : Sets.newLinkedHashSet(Arrays.asList(keys))) { requestPb.addKeys(k.toPb()); } return new ResultsIterator(requestPb); }
protected List<com.google.datastore.v1.Mutation> toMutationPbList() { List<com.google.datastore.v1.Mutation> mutationsPb = new ArrayList<>(); for (FullEntity<IncompleteKey> entity : toAddAutoId()) { mutationsPb.add( com.google.datastore.v1.Mutation.newBuilder().setInsert(entity.toPb()).build()); } for (FullEntity<Key> entity : toAdd().values()) { mutationsPb.add( com.google.datastore.v1.Mutation.newBuilder().setInsert(entity.toPb()).build()); } for (FullEntity<Key> entity : toUpdate().values()) { mutationsPb.add( com.google.datastore.v1.Mutation.newBuilder().setUpdate(entity.toPb()).build()); } for (FullEntity<Key> entity : toPut().values()) { mutationsPb.add( com.google.datastore.v1.Mutation.newBuilder().setUpsert(entity.toPb()).build()); } for (Key key : toDelete()) { mutationsPb.add(com.google.datastore.v1.Mutation.newBuilder().setDelete(key.toPb()).build()); } return mutationsPb; }
@Test public void testDelete() throws Exception { List<com.google.datastore.v1.Mutation> pbs = new LinkedList<>(); pbs.add(com.google.datastore.v1.Mutation.newBuilder().setDelete(KEY1.toPb()).build()); pbs.add(com.google.datastore.v1.Mutation.newBuilder().setDelete(KEY2.toPb()).build()); pbs.add(com.google.datastore.v1.Mutation.newBuilder().setDelete(KEY3.toPb()).build()); batchWriter.delete(KEY1, KEY2); batchWriter.delete(KEY3); assertEquals(pbs, batchWriter.toMutationPbList()); }
@Test public void testDeleteAfterPut() throws Exception { List<com.google.datastore.v1.Mutation> pbs = new LinkedList<>(); pbs.add(com.google.datastore.v1.Mutation.newBuilder().setDelete(KEY1.toPb()).build()); batchWriter.put(ENTITY1); batchWriter.delete(KEY1); assertEquals(pbs, batchWriter.toMutationPbList()); }
@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); } }
@Test public void testDeleteAfterUpdate() throws Exception { List<com.google.datastore.v1.Mutation> pbs = new LinkedList<>(); pbs.add(com.google.datastore.v1.Mutation.newBuilder().setDelete(KEY1.toPb()).build()); batchWriter.update(ENTITY1); batchWriter.delete(KEY1); assertEquals(pbs, batchWriter.toMutationPbList()); }
@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); }
private Datastore createDatastoreForDeferredLookup() throws DatastoreException { List<com.google.datastore.v1.Key> keysPb = new ArrayList<>(); keysPb.add(KEY1.toPb()); keysPb.add(KEY2.toPb()); keysPb.add(KEY3.toPb()); keysPb.add(KEY4.toPb()); keysPb.add(KEY5.toPb()); List<LookupRequest> lookupRequests = new ArrayList<>(); lookupRequests.add(LookupRequest.newBuilder().addAllKeys(keysPb).build());
@Test public void testDeleteAfterAdd() throws Exception { List<com.google.datastore.v1.Mutation> pbs = new LinkedList<>(); pbs.add( com.google.datastore.v1.Mutation.newBuilder() .setInsert(INCOMPLETE_ENTITY_1.toPb()) .build()); pbs.add(com.google.datastore.v1.Mutation.newBuilder().setDelete(KEY1.toPb()).build()); batchWriter.add(ENTITY1); batchWriter.addWithDeferredIdAllocation(INCOMPLETE_ENTITY_1); batchWriter.delete(KEY1); assertEquals(pbs, batchWriter.toMutationPbList()); }
@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); }
/** Returns the key in an encoded form that can be used as part of a URL. */ public String toUrlSafe() { try { return URLEncoder.encode(TextFormat.printToString(toPb()), UTF_8.name()); } catch (UnsupportedEncodingException e) { throw new IllegalStateException("Unexpected encoding exception", e); } }
@Override protected void setValue(KeyValue from, com.google.datastore.v1.Value.Builder to) { to.setKeyValue(from.get().toPb()); } };
@Override public void delete(Key... keys) { if (keys.length > 0) { List<com.google.datastore.v1.Mutation> mutationsPb = new ArrayList<>(); Set<Key> dedupKeys = new LinkedHashSet<>(Arrays.asList(keys)); for (Key key : dedupKeys) { mutationsPb.add( com.google.datastore.v1.Mutation.newBuilder().setDelete(key.toPb()).build()); } commitMutation(mutationsPb); } }
Iterator<Entity> get(com.google.datastore.v1.ReadOptions readOptionsPb, final Key... keys) { if (keys.length == 0) { return Collections.emptyIterator(); } com.google.datastore.v1.LookupRequest.Builder requestPb = com.google.datastore.v1.LookupRequest.newBuilder(); if (readOptionsPb != null) { requestPb.setReadOptions(readOptionsPb); } for (Key k : Sets.newLinkedHashSet(Arrays.asList(keys))) { requestPb.addKeys(k.toPb()); } return new ResultsIterator(requestPb); }
protected List<com.google.datastore.v1.Mutation> toMutationPbList() { List<com.google.datastore.v1.Mutation> mutationsPb = new ArrayList<>(); for (FullEntity<IncompleteKey> entity : toAddAutoId()) { mutationsPb.add( com.google.datastore.v1.Mutation.newBuilder().setInsert(entity.toPb()).build()); } for (FullEntity<Key> entity : toAdd().values()) { mutationsPb.add( com.google.datastore.v1.Mutation.newBuilder().setInsert(entity.toPb()).build()); } for (FullEntity<Key> entity : toUpdate().values()) { mutationsPb.add( com.google.datastore.v1.Mutation.newBuilder().setUpdate(entity.toPb()).build()); } for (FullEntity<Key> entity : toPut().values()) { mutationsPb.add( com.google.datastore.v1.Mutation.newBuilder().setUpsert(entity.toPb()).build()); } for (Key key : toDelete()) { mutationsPb.add(com.google.datastore.v1.Mutation.newBuilder().setDelete(key.toPb()).build()); } return mutationsPb; }