Search in sources :

Example 1 with FullEntity

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

the class ITDatastoreTest method testGetArrayNoDeferredResults.

@Test
public void testGetArrayNoDeferredResults() {
    DATASTORE.put(ENTITY3);
    Iterator<Entity> result = DATASTORE.fetch(KEY1, Key.newBuilder(KEY1).setName("bla").build(), KEY2, KEY3).iterator();
    assertEquals(ENTITY1, result.next());
    assertNull(result.next());
    assertEquals(ENTITY2, result.next());
    Entity entity3 = result.next();
    assertEquals(ENTITY3, entity3);
    assertTrue(entity3.isNull("null"));
    assertFalse(entity3.getBoolean("bool"));
    assertEquals(LIST_VALUE2.get(), entity3.getList("list"));
    FullEntity<IncompleteKey> partial1 = entity3.getEntity("partial1");
    FullEntity<IncompleteKey> partial2 = entity3.getEntity("partial2");
    assertEquals(PARTIAL_ENTITY2, partial1);
    assertEquals(ENTITY2, partial2);
    assertEquals(ValueType.BOOLEAN, entity3.getValue("bool").getType());
    assertEquals(LAT_LNG_VALUE, entity3.getValue("latLng"));
    assertEquals(EMPTY_LIST_VALUE, entity3.getValue("emptyList"));
    assertEquals(8, entity3.getNames().size());
    assertFalse(entity3.contains("bla"));
    try {
        entity3.getString("str");
        fail("Expecting a failure");
    } catch (DatastoreException expected) {
    // expected - no such property
    }
    assertFalse(result.hasNext());
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) ProjectionEntity(com.google.cloud.datastore.ProjectionEntity) DatastoreException(com.google.cloud.datastore.DatastoreException) IncompleteKey(com.google.cloud.datastore.IncompleteKey) Test(org.junit.Test)

Example 2 with FullEntity

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

the class TransactionSnippets method multiplePutEntitiesDeferredId.

/**
   * Example of putting multiple entities with deferred id allocation.
   */
// [TARGET putWithDeferredIdAllocation(FullEntity...)]
public List<Key> multiplePutEntitiesDeferredId() {
    Datastore datastore = transaction.getDatastore();
    // [START multiplePutEntitiesDeferredId]
    IncompleteKey key1 = datastore.newKeyFactory().setKind("MyKind").newKey();
    FullEntity.Builder entityBuilder1 = FullEntity.newBuilder(key1);
    entityBuilder1.set("propertyName", "value1");
    FullEntity entity1 = entityBuilder1.build();
    IncompleteKey key2 = datastore.newKeyFactory().setKind("MyKind").newKey();
    FullEntity.Builder entityBuilder2 = FullEntity.newBuilder(key2);
    entityBuilder2.set("propertyName", "value2");
    FullEntity entity2 = entityBuilder2.build();
    transaction.putWithDeferredIdAllocation(entity1, entity2);
    Response response = transaction.commit();
    // [END multiplePutEntitiesDeferredId]
    return response.getGeneratedKeys();
}
Also used : Response(com.google.cloud.datastore.Transaction.Response) Datastore(com.google.cloud.datastore.Datastore) FullEntity(com.google.cloud.datastore.FullEntity) IncompleteKey(com.google.cloud.datastore.IncompleteKey)

Example 3 with FullEntity

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

the class TransactionSnippets method addSingleEntity.

/**
   * 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]
}
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 4 with FullEntity

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

the class TransactionSnippets method multipleAddEntitiesDeferredId.

/**
   * Example of adding multiple entities with deferred id allocation.
   */
// [TARGET addWithDeferredIdAllocation(FullEntity...)]
public List<Key> multipleAddEntitiesDeferredId() {
    Datastore datastore = transaction.getDatastore();
    // [START multipleAddEntitiesDeferredId]
    IncompleteKey key1 = datastore.newKeyFactory().setKind("MyKind").newKey();
    FullEntity.Builder entityBuilder1 = FullEntity.newBuilder(key1);
    entityBuilder1.set("propertyName", "value1");
    FullEntity entity1 = entityBuilder1.build();
    IncompleteKey key2 = datastore.newKeyFactory().setKind("MyKind").newKey();
    FullEntity.Builder entityBuilder2 = FullEntity.newBuilder(key2);
    entityBuilder2.set("propertyName", "value2");
    FullEntity entity2 = entityBuilder2.build();
    transaction.addWithDeferredIdAllocation(entity1, entity2);
    Response response = transaction.commit();
    // [END multipleAddEntitiesDeferredId]
    return response.getGeneratedKeys();
}
Also used : Response(com.google.cloud.datastore.Transaction.Response) Datastore(com.google.cloud.datastore.Datastore) FullEntity(com.google.cloud.datastore.FullEntity) IncompleteKey(com.google.cloud.datastore.IncompleteKey)

Example 5 with FullEntity

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

the class TransactionSnippets method putSingleEntity.

/**
   * 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]
}
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)

Aggregations

FullEntity (com.google.cloud.datastore.FullEntity)8 IncompleteKey (com.google.cloud.datastore.IncompleteKey)8 Datastore (com.google.cloud.datastore.Datastore)6 Entity (com.google.cloud.datastore.Entity)6 Key (com.google.cloud.datastore.Key)4 ProjectionEntity (com.google.cloud.datastore.ProjectionEntity)2 Response (com.google.cloud.datastore.Transaction.Response)2 Test (org.junit.Test)2 BooleanValue (com.google.cloud.datastore.BooleanValue)1 DatastoreException (com.google.cloud.datastore.DatastoreException)1 LatLngValue (com.google.cloud.datastore.LatLngValue)1 ListValue (com.google.cloud.datastore.ListValue)1 StringValue (com.google.cloud.datastore.StringValue)1 TimestampValue (com.google.cloud.datastore.TimestampValue)1