Search in sources :

Example 16 with Datastore

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

the class TransactionSnippets method multipleAddEntities.

/**
   * Example of adding multiple entities.
   */
// [TARGET add(FullEntity...)]
// [VARIABLE "my_key_name1"]
// [VARIABLE "my_key_name2"]
public void multipleAddEntities(String keyName1, String keyName2) {
    Datastore datastore = transaction.getDatastore();
    // [START multipleAddEntities]
    Key key1 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName1);
    Entity.Builder entityBuilder1 = Entity.newBuilder(key1);
    entityBuilder1.set("propertyName", "value1");
    Entity entity1 = entityBuilder1.build();
    Key key2 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName2);
    Entity.Builder entityBuilder2 = Entity.newBuilder(key2);
    entityBuilder2.set("propertyName", "value2");
    Entity entity2 = entityBuilder2.build();
    transaction.add(entity1, entity2);
    transaction.commit();
// [END multipleAddEntities]
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) Datastore(com.google.cloud.datastore.Datastore) Key(com.google.cloud.datastore.Key) IncompleteKey(com.google.cloud.datastore.IncompleteKey)

Example 17 with Datastore

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

the class TransactionSnippets method rollback.

/**
   * 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();
    // [END rollback]
    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 18 with Datastore

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

the class TransactionSnippets method multiplePutEntities.

/**
   * Example of putting multiple entities.
   */
// [TARGET put(FullEntity...)]
// [VARIABLE "my_key_name1"]
// [VARIABLE "my_key_name2"]
public void multiplePutEntities(String keyName1, String keyName2) {
    Datastore datastore = transaction.getDatastore();
    // [START multiplePutEntities]
    Key key1 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName1);
    Entity.Builder entityBuilder1 = Entity.newBuilder(key1);
    entityBuilder1.set("propertyName", "value1");
    Entity entity1 = entityBuilder1.build();
    Key key2 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName2);
    Entity.Builder entityBuilder2 = Entity.newBuilder(key2);
    entityBuilder2.set("propertyName", "value2");
    Entity entity2 = entityBuilder2.build();
    transaction.put(entity1, entity2);
    transaction.commit();
// [END multiplePutEntities]
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) Datastore(com.google.cloud.datastore.Datastore) Key(com.google.cloud.datastore.Key) IncompleteKey(com.google.cloud.datastore.IncompleteKey)

Example 19 with Datastore

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

the class TransactionSnippets method commit.

/**
   * Example of committing a transaction.
   */
// [TARGET commit()]
public Key commit() {
    Datastore datastore = transaction.getDatastore();
    // [START commit]
    // create an entity
    KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
    Key key = datastore.allocateId(keyFactory.newKey());
    Entity entity = Entity.newBuilder(key).set("description", "commit()").build();
    // add the entity and commit
    try {
        transaction.put(entity);
        transaction.commit();
    } catch (DatastoreException ex) {
    // handle exception
    }
    return key;
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) Datastore(com.google.cloud.datastore.Datastore) DatastoreException(com.google.cloud.datastore.DatastoreException) KeyFactory(com.google.cloud.datastore.KeyFactory) Key(com.google.cloud.datastore.Key) IncompleteKey(com.google.cloud.datastore.IncompleteKey)

Example 20 with Datastore

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

the class LocalDatastoreHelperTest method testStartStopReset.

@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);
}
Also used : Datastore(com.google.cloud.datastore.Datastore) Key(com.google.cloud.datastore.Key) Test(org.junit.Test)

Aggregations

Datastore (com.google.cloud.datastore.Datastore)21 Key (com.google.cloud.datastore.Key)19 IncompleteKey (com.google.cloud.datastore.IncompleteKey)17 Entity (com.google.cloud.datastore.Entity)16 FullEntity (com.google.cloud.datastore.FullEntity)15 KeyFactory (com.google.cloud.datastore.KeyFactory)11 Response (com.google.cloud.datastore.Transaction.Response)2 DatastoreException (com.google.cloud.datastore.DatastoreException)1 DatastoreOptions (com.google.cloud.datastore.DatastoreOptions)1 Transaction (com.google.cloud.datastore.Transaction)1 Test (org.junit.Test)1