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));
}
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);
}
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());
}
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();
}
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();
}
Aggregations