Search in sources :

Example 1 with IncompleteKey

use of com.google.cloud.datastore.IncompleteKey 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 IncompleteKey

use of com.google.cloud.datastore.IncompleteKey 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 IncompleteKey

use of com.google.cloud.datastore.IncompleteKey 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 4 with IncompleteKey

use of com.google.cloud.datastore.IncompleteKey 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 5 with IncompleteKey

use of com.google.cloud.datastore.IncompleteKey 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)

Aggregations

IncompleteKey (com.google.cloud.datastore.IncompleteKey)8 FullEntity (com.google.cloud.datastore.FullEntity)4 Key (com.google.cloud.datastore.Key)4 KeyFactory (com.google.cloud.datastore.KeyFactory)4 Test (org.junit.Test)4 Datastore (com.google.cloud.datastore.Datastore)2 Entity (com.google.cloud.datastore.Entity)2 ProjectionEntity (com.google.cloud.datastore.ProjectionEntity)2 Response (com.google.cloud.datastore.Transaction.Response)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