Search in sources :

Example 1 with KeyFactory

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

the class ITDatastoreTest method testAllocateIdArray.

@Test
public void testAllocateIdArray() {
    KeyFactory keyFactory = DATASTORE.newKeyFactory().setKind(KIND1);
    IncompleteKey incompleteKey1 = keyFactory.newKey();
    IncompleteKey incompleteKey2 = keyFactory.setKind(KIND2).addAncestors(PathElement.of(KIND1, 10)).newKey();
    List<Key> result = DATASTORE.allocateId(incompleteKey1, incompleteKey2, incompleteKey1);
    assertEquals(3, result.size());
    assertEquals(Key.newBuilder(incompleteKey1, result.get(0).getId()).build(), result.get(0));
    assertEquals(Key.newBuilder(incompleteKey1, result.get(2).getId()).build(), result.get(2));
    assertEquals(Key.newBuilder(incompleteKey2, result.get(1).getId()).build(), result.get(1));
}
Also used : KeyFactory(com.google.cloud.datastore.KeyFactory) IncompleteKey(com.google.cloud.datastore.IncompleteKey) Key(com.google.cloud.datastore.Key) IncompleteKey(com.google.cloud.datastore.IncompleteKey) Test(org.junit.Test)

Example 2 with KeyFactory

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

the class ITDatastoreTest method testAllocateId.

@Test
public void testAllocateId() {
    KeyFactory keyFactory = DATASTORE.newKeyFactory().setKind(KIND1);
    IncompleteKey pk1 = keyFactory.newKey();
    Key key1 = DATASTORE.allocateId(pk1);
    assertEquals(key1.getProjectId(), pk1.getProjectId());
    assertEquals(key1.getNamespace(), pk1.getNamespace());
    assertEquals(key1.getAncestors(), pk1.getAncestors());
    assertEquals(key1.getKind(), pk1.getKind());
    assertTrue(key1.hasId());
    assertFalse(key1.hasName());
    assertEquals(Key.newBuilder(pk1, key1.getId()).build(), key1);
    Key key2 = DATASTORE.allocateId(pk1);
    assertNotEquals(key1, key2);
    assertEquals(Key.newBuilder(pk1, key2.getId()).build(), key2);
}
Also used : KeyFactory(com.google.cloud.datastore.KeyFactory) IncompleteKey(com.google.cloud.datastore.IncompleteKey) Key(com.google.cloud.datastore.Key) IncompleteKey(com.google.cloud.datastore.IncompleteKey) Test(org.junit.Test)

Example 3 with KeyFactory

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

the class TransactionSnippets method isActive.

/**
   * Example of verifying if a transaction is active.
   */
// [TARGET isActive()]
public Key isActive() {
    Datastore datastore = transaction.getDatastore();
    // [START isActive]
    // create an entity
    KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
    Key key = datastore.allocateId(keyFactory.newKey());
    Entity entity = Entity.newBuilder(key).set("description", "active()").build();
    // calling transaction.active() now would return true
    try {
        // add the entity and commit
        transaction.put(entity);
        transaction.commit();
    } finally {
        // then transaction.active() will be false
        if (transaction.isActive()) {
            // otherwise it's true and we need to rollback
            transaction.rollback();
        }
    }
    // [END isActive]
    return key;
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) Datastore(com.google.cloud.datastore.Datastore) KeyFactory(com.google.cloud.datastore.KeyFactory) Key(com.google.cloud.datastore.Key) IncompleteKey(com.google.cloud.datastore.IncompleteKey)

Example 4 with KeyFactory

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

the class TransactionSnippets method run.

/**
   * Example of running a query to find all entities with an ancestor.
   */
// [TARGET run(Query)]
// [VARIABLE "my_parent_key_name"]
public List<Entity> run(String parentKeyName) {
    Datastore datastore = transaction.getDatastore();
    // [START run]
    KeyFactory keyFactory = datastore.newKeyFactory().setKind("ParentKind");
    Key parentKey = keyFactory.newKey(parentKeyName);
    // Build a query
    Query<Entity> query = Query.newEntityQueryBuilder().setKind("MyKind").setFilter(PropertyFilter.hasAncestor(parentKey)).build();
    QueryResults<Entity> results = transaction.run(query);
    List<Entity> entities = Lists.newArrayList();
    while (results.hasNext()) {
        Entity result = results.next();
        // do something with result
        entities.add(result);
    }
    transaction.commit();
    // [END run]
    return entities;
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) Datastore(com.google.cloud.datastore.Datastore) KeyFactory(com.google.cloud.datastore.KeyFactory) Key(com.google.cloud.datastore.Key) IncompleteKey(com.google.cloud.datastore.IncompleteKey)

Example 5 with KeyFactory

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

the class TransactionSnippets method active.

/**
   * Example of verifying if a transaction is active.
   */
// [TARGET active()]
public Key active() {
    Datastore datastore = transaction.getDatastore();
    // [START active]
    // create an entity
    KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
    Key key = datastore.allocateId(keyFactory.newKey());
    Entity entity = Entity.newBuilder(key).set("description", "active()").build();
    // calling transaction.active() now would return true
    try {
        // add the entity and commit
        transaction.put(entity);
        transaction.commit();
    } finally {
        // then transaction.isActive() will be false
        if (transaction.isActive()) {
            // otherwise it's true and we need to rollback
            transaction.rollback();
        }
    }
    // [END active]
    return key;
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) Datastore(com.google.cloud.datastore.Datastore) KeyFactory(com.google.cloud.datastore.KeyFactory) Key(com.google.cloud.datastore.Key) IncompleteKey(com.google.cloud.datastore.IncompleteKey)

Aggregations

KeyFactory (com.google.cloud.datastore.KeyFactory)18 Key (com.google.cloud.datastore.Key)17 IncompleteKey (com.google.cloud.datastore.IncompleteKey)14 Entity (com.google.cloud.datastore.Entity)12 Datastore (com.google.cloud.datastore.Datastore)11 FullEntity (com.google.cloud.datastore.FullEntity)7 Test (org.junit.Test)3 DatastoreException (com.google.cloud.datastore.DatastoreException)1 DatastoreOptions (com.google.cloud.datastore.DatastoreOptions)1 Transaction (com.google.cloud.datastore.Transaction)1