Search in sources :

Example 6 with Key

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

the class ITTransactionSnippets method testRun.

@Test
public void testRun() {
    Key key1 = datastore.newKeyFactory().setKind("ParentKind").newKey("run_key_1");
    Entity entity1 = Entity.newBuilder(key1).set("description", "run1").build();
    datastore.put(entity1);
    Key key2 = datastore.newKeyFactory().setKind("MyKind").addAncestor(PathElement.of("ParentKind", "run_key_1")).newKey("run_key_2");
    registerKey(key1);
    registerKey(key2);
    Entity entity2 = Entity.newBuilder(key2).set("description", "run2").build();
    datastore.put(entity2);
    Transaction transaction = datastore.newTransaction();
    TransactionSnippets transactionSnippets = new TransactionSnippets(transaction);
    List<Entity> entities = transactionSnippets.run("run_key_1");
    assertEquals(1, entities.size());
    assertEquals(entity2, entities.get(0));
}
Also used : Entity(com.google.cloud.datastore.Entity) Transaction(com.google.cloud.datastore.Transaction) Key(com.google.cloud.datastore.Key) Test(org.junit.Test)

Example 7 with Key

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

the class ITTransactionSnippets method registerKey.

private String registerKey(String keyName, String kind) {
    Key key = datastore.newKeyFactory().setKind(kind).newKey(keyName);
    registeredKeys.add(key);
    return key.getName();
}
Also used : Key(com.google.cloud.datastore.Key)

Example 8 with Key

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

the class ITTransactionSnippets method testPutGetMultipleDeferredId.

@Test
public void testPutGetMultipleDeferredId() {
    Transaction transaction = datastore.newTransaction();
    TransactionSnippets transactionSnippets = new TransactionSnippets(transaction);
    List<Key> keys = transactionSnippets.multiplePutEntitiesDeferredId();
    assertEquals(2, keys.size());
    Key key1 = keys.get(0);
    registerKey(key1);
    Entity entity1 = datastore.get(key1);
    assertEquals("value1", entity1.getString("propertyName"));
    Key key2 = keys.get(1);
    registerKey(key2);
    Entity entity2 = datastore.get(key2);
    assertEquals("value2", entity2.getString("propertyName"));
}
Also used : Entity(com.google.cloud.datastore.Entity) Transaction(com.google.cloud.datastore.Transaction) Key(com.google.cloud.datastore.Key) Test(org.junit.Test)

Example 9 with Key

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

the class ITTransactionSnippets method testRollback.

@Test
public void testRollback() {
    Transaction transaction = datastore.newTransaction();
    TransactionSnippets transactionSnippets = new TransactionSnippets(transaction);
    Key key = transactionSnippets.rollback();
    Entity result = datastore.get(key);
    assertNull(result);
}
Also used : Entity(com.google.cloud.datastore.Entity) Transaction(com.google.cloud.datastore.Transaction) Key(com.google.cloud.datastore.Key) Test(org.junit.Test)

Example 10 with Key

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

the class ITTransactionSnippets method testFetchDeleteEntitiesWithKeys.

@Test
public void testFetchDeleteEntitiesWithKeys() {
    Key key1 = datastore.newKeyFactory().setKind("MyKind").newKey("fetch_key_1");
    Key key2 = datastore.newKeyFactory().setKind("MyKind").newKey("fetch_key_2");
    Entity entity1 = Entity.newBuilder(key1).set("description", "fetch1").build();
    Entity entity2 = Entity.newBuilder(key2).set("description", "fetch2").build();
    datastore.put(entity1, entity2);
    registerKey("fetch_key_1");
    registerKey("fetch_key_2");
    Transaction transaction = datastore.newTransaction();
    TransactionSnippets transactionSnippets = new TransactionSnippets(transaction);
    Set<Entity> entities = Sets.newHashSet(transactionSnippets.fetchEntitiesWithKeys("fetch_key_1", "fetch_key_2"));
    assertEquals(2, entities.size());
    assertTrue(entities.contains(entity1));
    assertTrue(entities.contains(entity2));
    transaction = datastore.newTransaction();
    transactionSnippets = new TransactionSnippets(transaction);
    transactionSnippets.multipleDeleteEntities("fetch_key_1", "fetch_key_2");
    transaction = datastore.newTransaction();
    transactionSnippets = new TransactionSnippets(transaction);
    List<Entity> deletedEntities = transactionSnippets.fetchEntitiesWithKeys("fetch_key_1", "fetch_key_2");
    assertNull(deletedEntities.get(0));
    assertNull(deletedEntities.get(1));
}
Also used : Entity(com.google.cloud.datastore.Entity) Transaction(com.google.cloud.datastore.Transaction) Key(com.google.cloud.datastore.Key) Test(org.junit.Test)

Aggregations

Key (com.google.cloud.datastore.Key)49 Entity (com.google.cloud.datastore.Entity)37 IncompleteKey (com.google.cloud.datastore.IncompleteKey)32 Datastore (com.google.cloud.datastore.Datastore)19 KeyFactory (com.google.cloud.datastore.KeyFactory)17 FullEntity (com.google.cloud.datastore.FullEntity)16 Test (org.junit.Test)15 Transaction (com.google.cloud.datastore.Transaction)9 DatastoreException (com.google.cloud.datastore.DatastoreException)4 ProjectionEntity (com.google.cloud.datastore.ProjectionEntity)3 Batch (com.google.cloud.datastore.Batch)2 DatastoreOptions (com.google.cloud.datastore.DatastoreOptions)1 BeforeClass (org.junit.BeforeClass)1