@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 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); } }
@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 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 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 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()); }
@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 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 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 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 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 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 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 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 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 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 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 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()); }
@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 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()); }