@Test public void testGetWithTransaction() throws Exception { Transaction transaction = createStrictMock(Transaction.class); IncompleteKey pKey1 = IncompleteKey.newBuilder("ds", "k").build(); Key key1 = Key.newBuilder(pKey1, 1).build(); Entity entity1 = Entity.newBuilder(key1).build(); Key key2 = Key.newBuilder(pKey1, 2).build(); expect(transaction.get(new Key[] {key1})) .andReturn(Collections.singletonList(entity1).iterator()); expect(transaction.get(new Key[] {key2})).andReturn(Collections.<Entity>emptyIterator()); replay(transaction); assertEquals(entity1, DatastoreHelper.get(transaction, key1)); assertNull(DatastoreHelper.get(transaction, key2)); verify(transaction); }
@Test public void testAdd() throws Exception { Datastore datastore = createStrictMock(Datastore.class); IncompleteKey pKey = IncompleteKey.newBuilder("ds", "k").build(); Key key = Key.newBuilder(pKey, 1).build(); Entity entity = Entity.newBuilder(key).build(); expect(datastore.add(new Entity[] {entity})).andReturn(Collections.singletonList(entity)); replay(datastore); assertEquals(entity, DatastoreHelper.add(datastore, entity)); verify(datastore); }
public static void main(String... args) { Datastore datastore = DatastoreOptions.getDefaultInstance().getService(); KeyFactory keyFactory = datastore.newKeyFactory().setKind("keyKind"); Key key = keyFactory.newKey("keyName"); Entity entity = datastore.get(key); if (entity != null) { System.out.println("Updating access_time for " + entity.getString("name")); entity = Entity.newBuilder(entity).set("access_time", Timestamp.now()).build(); datastore.update(entity); } } }
/** Example of starting a new batch. */ // [TARGET newBatch()] // [VARIABLE "my_key_name_1"] // [VARIABLE "my_key_name_2"] public Batch newBatch(String keyName1, String keyName2) { // [START newBatch] Key key1 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName1); Key key2 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName2); Batch batch = datastore.newBatch(); Entity entity1 = Entity.newBuilder(key1).set("name", "John").build(); Entity entity2 = Entity.newBuilder(key2).set("title", "title").build(); batch.add(entity1); batch.add(entity2); batch.submit(); // [END newBatch] return batch; }
@Test public void testStartStopReset() throws IOException, InterruptedException, TimeoutException { LocalDatastoreHelper helper = LocalDatastoreHelper.create(); helper.start(); Datastore datastore = helper.getOptions().getService(); Key key = datastore.newKeyFactory().setKind("kind").newKey("name"); datastore.put(Entity.newBuilder(key).build()); assertNotNull(datastore.get(key)); helper.reset(); assertNull(datastore.get(key)); helper.stop(Duration.ofMinutes(1)); thrown.expect(DatastoreException.class); datastore.get(key); } }
/** Example of rolling back a transaction. */ // [TARGET rollback()] public Key rollback() { Datastore datastore = transaction.getDatastore(); // [START rollback] // create an entity KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind"); Key key = datastore.allocateId(keyFactory.newKey()); Entity entity = Entity.newBuilder(key).set("description", "rollback()").build(); // add the entity and rollback transaction.put(entity); transaction.rollback(); // calling transaction.commit() now would fail // [END rollback] return key; }
/** Example of adding a single entity. */ // [TARGET add(FullEntity)] // [VARIABLE "my_key_name"] public void addSingleEntity(String keyName) { Datastore datastore = transaction.getDatastore(); // [START addSingleEntity] Key key = datastore.newKeyFactory().setKind("MyKind").newKey(keyName); Entity.Builder entityBuilder = Entity.newBuilder(key); entityBuilder.set("propertyName", "value"); Entity entity = entityBuilder.build(); transaction.add(entity); transaction.commit(); // [END addSingleEntity] }
/** Example of putting a single entity. */ // [TARGET put(FullEntity)] // [VARIABLE "my_key_name"] public void putSingleEntity(String keyName) { Datastore datastore = transaction.getDatastore(); // [START putSingleEntity] Key key = datastore.newKeyFactory().setKind("MyKind").newKey(keyName); Entity.Builder entityBuilder = Entity.newBuilder(key); entityBuilder.set("propertyName", "value"); Entity entity = entityBuilder.build(); transaction.put(entity); transaction.commit(); // [END putSingleEntity] }
static Entity fromPb(com.google.datastore.v1.Entity entityPb) { return new Builder().fill(entityPb).build(); } }
public static void main(String... args) { Datastore datastore = DatastoreOptions.getDefaultInstance().getService(); KeyFactory keyFactory = datastore.newKeyFactory().setKind("keyKind"); Key key = keyFactory.newKey("keyName"); Entity entity = Entity.newBuilder(key) .set("name", "John Doe") .set("age", 30) .set("access_time", Timestamp.now()) .build(); datastore.put(entity); } }
@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 testCopyFromIncompleteEntity() throws Exception { Entity.Builder builder = Entity.newBuilder(KEY2, INCOMPLETE_ENTITY); Entity entity = builder.build(); assertNotEquals(INCOMPLETE_ENTITY, entity); assertEquals(INCOMPLETE_ENTITY.getProperties(), entity.getProperties()); } }
@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 testCopyFrom() throws Exception { Entity.Builder builder = Entity.newBuilder(ENTITY); assertEquals(ENTITY, builder.build()); Entity entity = builder.setKey(KEY2).build(); assertNotEquals(ENTITY, entity); assertEquals(KEY2, entity.getKey()); assertEquals(ENTITY.getProperties(), entity.getProperties()); }
/** Example of putting a single entity. */ // [TARGET put(FullEntity)] // [VARIABLE "my_key_name"] public void putSingleEntity(String keyName) { // [START putSingleEntity] Key key = datastore.newKeyFactory().setKind("MyKind").newKey(keyName); Entity.Builder entityBuilder = Entity.newBuilder(key); entityBuilder.set("propertyName", "value"); Entity entity = entityBuilder.build(); datastore.put(entity); // [END putSingleEntity] }
@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 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()); }