Search in sources :

Example 1 with Batch

use of com.google.cloud.datastore.Batch in project google-cloud-java by GoogleCloudPlatform.

the class ITDatastoreSnippets method testNewBatch.

@Test
public void testNewBatch() {
    String testKey1 = registerKey("new_batch_key1");
    String testKey2 = registerKey("new_batch_key2");
    Batch batch = datastoreSnippets.newBatch(testKey1, testKey2);
    assertNotNull(batch);
}
Also used : Batch(com.google.cloud.datastore.Batch) Test(org.junit.Test)

Example 2 with Batch

use of com.google.cloud.datastore.Batch in project google-cloud-java by GoogleCloudPlatform.

the class ITDatastoreTest method testNewBatch.

@Test
public void testNewBatch() {
    Batch batch = DATASTORE.newBatch();
    Entity entity1 = Entity.newBuilder(ENTITY1).clear().build();
    Entity entity2 = Entity.newBuilder(ENTITY2).clear().setNull("bla").build();
    Entity entity4 = Entity.newBuilder(KEY4).set("value", StringValue.of("value")).build();
    Entity entity5 = Entity.newBuilder(KEY5).set("value", "value").build();
    List<Entity> entities = batch.add(entity4, PARTIAL_ENTITY2, entity5);
    Entity entity6 = entities.get(1);
    assertSame(entity4, entities.get(0));
    assertEquals(PARTIAL_ENTITY2.getNames(), entity6.getNames());
    assertEquals(PARTIAL_ENTITY2.getKey().getProjectId(), entity6.getKey().getProjectId());
    assertEquals(PARTIAL_ENTITY2.getKey().getNamespace(), entity6.getKey().getNamespace());
    assertEquals(PARTIAL_ENTITY2.getKey().getAncestors(), entity6.getKey().getAncestors());
    assertEquals(PARTIAL_ENTITY2.getKey().getKind(), entity6.getKey().getKind());
    assertEquals(PARTIAL_ENTITY2.getKey(), IncompleteKey.newBuilder(entity6.getKey()).build());
    assertEquals(PARTIAL_ENTITY2.getKey().getAncestors(), entity6.getKey().getAncestors());
    assertNotEquals(PARTIAL_ENTITY2.getKey(), entity6.getKey());
    assertSame(entity5, entities.get(2));
    batch.addWithDeferredIdAllocation(PARTIAL_ENTITY3);
    batch.put(ENTITY3, entity1, entity2);
    Batch.Response response = batch.submit();
    entities = DATASTORE.fetch(KEY1, KEY2, KEY3, entity4.getKey(), entity5.getKey(), entity6.getKey());
    assertEquals(entity1, entities.get(0));
    assertEquals(entity2, entities.get(1));
    assertEquals(ENTITY3, entities.get(2));
    assertEquals(entity4, entities.get(3));
    assertEquals(entity5, entities.get(4));
    assertEquals(entity6, entities.get(5));
    assertEquals(6, entities.size());
    List<Key> generatedKeys = response.getGeneratedKeys();
    assertEquals(1, generatedKeys.size());
    assertEquals(PARTIAL_ENTITY3.getNames(), DATASTORE.get(generatedKeys.get(0)).getNames());
    assertEquals(PARTIAL_ENTITY3.getKey(), IncompleteKey.newBuilder(generatedKeys.get(0)).build());
    try {
        batch.submit();
        fail("Expecting a failure");
    } catch (DatastoreException expected) {
        assertEquals("FAILED_PRECONDITION", expected.getReason());
    }
    batch = DATASTORE.newBatch();
    batch.delete(entity4.getKey(), entity5.getKey(), entity6.getKey());
    batch.update(ENTITY1, ENTITY2, ENTITY3);
    batch.submit();
    entities = DATASTORE.fetch(KEY1, KEY2, KEY3, entity4.getKey(), entity5.getKey(), entity6.getKey());
    assertEquals(ENTITY1, entities.get(0));
    assertEquals(ENTITY2, entities.get(1));
    assertEquals(ENTITY3, entities.get(2));
    assertNull(entities.get(3));
    assertNull(entities.get(4));
    assertNull(entities.get(5));
    assertEquals(6, entities.size());
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) ProjectionEntity(com.google.cloud.datastore.ProjectionEntity) Batch(com.google.cloud.datastore.Batch) DatastoreException(com.google.cloud.datastore.DatastoreException) IncompleteKey(com.google.cloud.datastore.IncompleteKey) Key(com.google.cloud.datastore.Key) Test(org.junit.Test)

Example 3 with Batch

use of com.google.cloud.datastore.Batch in project google-cloud-java by GoogleCloudPlatform.

the class DatastoreSnippets method newBatch.

/**
   * 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;
}
Also used : Entity(com.google.cloud.datastore.Entity) Batch(com.google.cloud.datastore.Batch) Key(com.google.cloud.datastore.Key) IncompleteKey(com.google.cloud.datastore.IncompleteKey)

Aggregations

Batch (com.google.cloud.datastore.Batch)3 Entity (com.google.cloud.datastore.Entity)2 IncompleteKey (com.google.cloud.datastore.IncompleteKey)2 Key (com.google.cloud.datastore.Key)2 Test (org.junit.Test)2 DatastoreException (com.google.cloud.datastore.DatastoreException)1 FullEntity (com.google.cloud.datastore.FullEntity)1 ProjectionEntity (com.google.cloud.datastore.ProjectionEntity)1