@Override public void update(Entity... entities) { if (entities.length > 0) { List<com.google.datastore.v1.Mutation> mutationsPb = new ArrayList<>(); Map<Key, Entity> dedupEntities = new LinkedHashMap<>(); for (Entity entity : entities) { dedupEntities.put(entity.getKey(), entity); } for (Entity entity : dedupEntities.values()) { mutationsPb.add( com.google.datastore.v1.Mutation.newBuilder().setUpdate(entity.toPb()).build()); } commitMutation(mutationsPb); } }
com.google.datastore.v1.Mutation.newBuilder().setUpsert(entity.toPb()).build());
@Test public void testUpdate() throws Exception { List<com.google.datastore.v1.Mutation> pbs = new LinkedList<>(); pbs.add(com.google.datastore.v1.Mutation.newBuilder().setUpdate(ENTITY1.toPb()).build()); pbs.add(com.google.datastore.v1.Mutation.newBuilder().setUpdate(ENTITY2.toPb()).build()); pbs.add(com.google.datastore.v1.Mutation.newBuilder().setUpdate(ENTITY3.toPb()).build()); batchWriter.update(ENTITY1, ENTITY2); batchWriter.update(ENTITY3); assertEquals(pbs, batchWriter.toMutationPbList()); }
@Test public void testPut() throws Exception { List<com.google.datastore.v1.Mutation> pbs = new LinkedList<>(); pbs.add(com.google.datastore.v1.Mutation.newBuilder().setUpsert(ENTITY1.toPb()).build()); pbs.add(com.google.datastore.v1.Mutation.newBuilder().setUpsert(ENTITY2.toPb()).build()); pbs.add(com.google.datastore.v1.Mutation.newBuilder().setUpsert(ENTITY3.toPb()).build()); List<Entity> putEntities = batchWriter.put(ENTITY1, ENTITY2); Entity putEntity = batchWriter.put(ENTITY3); assertEquals(ENTITY1, putEntities.get(0)); assertEquals(ENTITY2, putEntities.get(1)); assertEquals(ENTITY3, putEntity); assertEquals(pbs, batchWriter.toMutationPbList()); }
@Test public void testAddAfterDelete() throws Exception { List<com.google.datastore.v1.Mutation> pbs = new LinkedList<>(); pbs.add(com.google.datastore.v1.Mutation.newBuilder().setUpsert(ENTITY1.toPb()).build()); batchWriter.delete(KEY1); batchWriter.add(ENTITY1); assertEquals(pbs, batchWriter.toMutationPbList()); }
@Test public void testUpdateAfterAdd() throws Exception { Entity entity = Entity.newBuilder(ENTITY1).set("foo", "bar").build(); List<com.google.datastore.v1.Mutation> pbs = new LinkedList<>(); pbs.add(com.google.datastore.v1.Mutation.newBuilder().setUpsert(entity.toPb()).build()); batchWriter.add(ENTITY1); batchWriter.update(entity); assertEquals(pbs, batchWriter.toMutationPbList()); }
@Test public void testPutIncompleteKey() throws Exception { List<com.google.datastore.v1.Mutation> pbs = new LinkedList<>(); pbs.add(com.google.datastore.v1.Mutation.newBuilder().setUpsert(ENTITY1.toPb()).build()); pbs.add( com.google.datastore.v1.Mutation.newBuilder() .setUpsert(Entity.newBuilder(KEY2, INCOMPLETE_ENTITY_1).build().toPb()) .build()); pbs.add( com.google.datastore.v1.Mutation.newBuilder() .setUpsert(Entity.newBuilder(KEY3, INCOMPLETE_ENTITY_2).build().toPb()) .build()); Entity putEntity = batchWriter.put(ENTITY1); List<Entity> putEntities = batchWriter.put(INCOMPLETE_ENTITY_1, INCOMPLETE_ENTITY_2); assertEquals(ENTITY1, putEntity); assertEquals(Entity.newBuilder(KEY2, INCOMPLETE_ENTITY_1).build(), putEntities.get(0)); assertEquals(Entity.newBuilder(KEY3, INCOMPLETE_ENTITY_2).build(), putEntities.get(1)); assertEquals(pbs, batchWriter.toMutationPbList()); }
@Test public void testPutAfterAdd() throws Exception { Entity entity = Entity.newBuilder(ENTITY1).set("foo", "bar").build(); List<com.google.datastore.v1.Mutation> pbs = new LinkedList<>(); pbs.add(com.google.datastore.v1.Mutation.newBuilder().setUpsert(entity.toPb()).build()); batchWriter.add(ENTITY1); batchWriter.put(entity); assertEquals(pbs, batchWriter.toMutationPbList()); }
@Test public void testUpdateAfterUpdate() throws Exception { Entity entity = Entity.newBuilder(ENTITY1).set("foo", "bar").build(); List<com.google.datastore.v1.Mutation> pbs = new LinkedList<>(); pbs.add(com.google.datastore.v1.Mutation.newBuilder().setUpdate(entity.toPb()).build()); batchWriter.update(ENTITY1); batchWriter.update(entity); assertEquals(pbs, batchWriter.toMutationPbList()); }
lookupResponses.add( LookupResponse.newBuilder() .addFound(EntityResult.newBuilder().setEntity(ENTITY1.toPb())) .addFound(EntityResult.newBuilder().setEntity(entity4.toPb())) .addDeferred(keysPb.get(1)) .addDeferred(keysPb.get(2)) lookupResponses.add( LookupResponse.newBuilder() .addFound(EntityResult.newBuilder().setEntity(ENTITY2.toPb())) .addFound(EntityResult.newBuilder().setEntity(ENTITY3.toPb())) .addDeferred(keysPb.get(4)) .build()); lookupResponses.add( LookupResponse.newBuilder() .addFound(EntityResult.newBuilder().setEntity(entity5.toPb())) .build()); for (int i = 0; i < lookupRequests.size(); i++) {
@Test public void testUpdateAfterPut() throws Exception { Entity entity = Entity.newBuilder(ENTITY1).set("foo", "bar").build(); List<com.google.datastore.v1.Mutation> pbs = new LinkedList<>(); pbs.add(com.google.datastore.v1.Mutation.newBuilder().setUpsert(entity.toPb()).build()); batchWriter.put(ENTITY1); batchWriter.update(entity); assertEquals(pbs, batchWriter.toMutationPbList()); }
@Test public void testAdd() throws Exception { Entity entity2 = Entity.newBuilder(ENTITY2).setKey(Key.newBuilder(KEY1).setName("name2").build()).build(); List<com.google.datastore.v1.Mutation> pbs = new LinkedList<>(); pbs.add(com.google.datastore.v1.Mutation.newBuilder().setInsert(ENTITY1.toPb()).build()); pbs.add( com.google.datastore.v1.Mutation.newBuilder() .setInsert(Entity.newBuilder(KEY2, INCOMPLETE_ENTITY_1).build().toPb()) .build()); pbs.add( com.google.datastore.v1.Mutation.newBuilder() .setInsert(Entity.newBuilder(KEY3, INCOMPLETE_ENTITY_2).build().toPb()) .build()); pbs.add(com.google.datastore.v1.Mutation.newBuilder().setInsert(entity2.toPb()).build()); List<Entity> entities = batchWriter.add(ENTITY1, INCOMPLETE_ENTITY_1, INCOMPLETE_ENTITY_2, entity2); assertEquals(pbs, batchWriter.toMutationPbList()); assertEquals(ENTITY1, entities.get(0)); assertEquals(Entity.newBuilder(KEY2, INCOMPLETE_ENTITY_1).build(), entities.get(1)); assertEquals(Entity.newBuilder(KEY3, INCOMPLETE_ENTITY_2).build(), entities.get(2)); assertEquals(entity2, entities.get(3)); }
@Test public void testPutAfterUpdate() throws Exception { Entity entity = Entity.newBuilder(ENTITY1).set("foo", "bar").build(); List<com.google.datastore.v1.Mutation> pbs = new LinkedList<>(); pbs.add(com.google.datastore.v1.Mutation.newBuilder().setUpsert(entity.toPb()).build()); batchWriter.update(ENTITY1); Entity putEntity = batchWriter.put(entity); assertEquals(entity, putEntity); assertEquals(pbs, batchWriter.toMutationPbList()); }
@Test public void testPutAfterDelete() throws Exception { Entity entity = Entity.newBuilder(ENTITY1).set("foo", "bar").build(); List<com.google.datastore.v1.Mutation> pbs = new LinkedList<>(); pbs.add(com.google.datastore.v1.Mutation.newBuilder().setUpsert(entity.toPb()).build()); batchWriter.delete(KEY1); Entity putEntity = batchWriter.put(entity); assertEquals(entity, putEntity); assertEquals(pbs, batchWriter.toMutationPbList()); }
@Test public void testPutWithDeferredAllocation() 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() .setInsert(INCOMPLETE_ENTITY_2.toPb()) .build()); pbs.add(com.google.datastore.v1.Mutation.newBuilder().setUpsert(ENTITY1.toPb()).build()); batchWriter.put(ENTITY1); batchWriter.putWithDeferredIdAllocation(INCOMPLETE_ENTITY_1, INCOMPLETE_ENTITY_2); assertEquals(pbs, batchWriter.toMutationPbList()); }
@Test public void testPutAfterPut() throws Exception { Entity entity = Entity.newBuilder(ENTITY1).set("foo", "bar").build(); List<com.google.datastore.v1.Mutation> pbs = new LinkedList<>(); pbs.add(com.google.datastore.v1.Mutation.newBuilder().setUpsert(entity.toPb()).build()); Entity putEntity1 = batchWriter.put(ENTITY1); Entity putEntity2 = batchWriter.put(entity); assertEquals(ENTITY1, putEntity1); assertEquals(entity, putEntity2); 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); }
@Test public void testAddWithDeferredAllocation() 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() .setInsert(INCOMPLETE_ENTITY_2.toPb()) .build()); pbs.add(com.google.datastore.v1.Mutation.newBuilder().setInsert(ENTITY1.toPb()).build()); batchWriter.addWithDeferredIdAllocation(ENTITY1, INCOMPLETE_ENTITY_1); batchWriter.addWithDeferredIdAllocation(INCOMPLETE_ENTITY_2); assertEquals(pbs, batchWriter.toMutationPbList()); }
@Override public void update(Entity... entities) { if (entities.length > 0) { List<com.google.datastore.v1.Mutation> mutationsPb = new ArrayList<>(); Map<Key, Entity> dedupEntities = new LinkedHashMap<>(); for (Entity entity : entities) { dedupEntities.put(entity.getKey(), entity); } for (Entity entity : dedupEntities.values()) { mutationsPb.add( com.google.datastore.v1.Mutation.newBuilder().setUpdate(entity.toPb()).build()); } commitMutation(mutationsPb); } }
com.google.datastore.v1.Mutation.newBuilder().setUpsert(entity.toPb()).build());