static Key allocateId(Datastore service, IncompleteKey key) { return service.allocateId(new IncompleteKey[] {key}).get(0); }
/** Example of allocating an id. */ // [TARGET allocateId(IncompleteKey)] public Key allocateIdSingle() { // [START allocateIdSingle] KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind"); IncompleteKey incompleteKey = keyFactory.newKey(); // let cloud datastore automatically assign an id Key key = datastore.allocateId(incompleteKey); // [END allocateIdSingle] return key; }
protected DatastoreBatchWriter() { super("test"); datastore = EasyMock.createMock(Datastore.class); IncompleteKey[] expected = {INCOMPLETE_KEY, INCOMPLETE_KEY}; List<Key> result = ImmutableList.of(KEY2, KEY3); expect(datastore.allocateId(expected)).andReturn(result).times(0, 1); replay(datastore); }
if (!incompleteKeys.isEmpty()) { IncompleteKey[] toAllocate = Iterables.toArray(incompleteKeys, IncompleteKey.class); allocated = getDatastore().allocateId(toAllocate).iterator(); } else { allocated = Collections.emptyIterator();
if (!incompleteKeys.isEmpty()) { IncompleteKey[] toAllocate = Iterables.toArray(incompleteKeys, IncompleteKey.class); allocated = getDatastore().allocateId(toAllocate).iterator(); } else { allocated = Collections.emptyIterator();
/** Example of allocating multiple ids in a single batch. */ // [TARGET allocateId(IncompleteKey...)] public List<Key> batchAllocateId() { // [START batchAllocateId] KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind"); IncompleteKey incompleteKey1 = keyFactory.newKey(); IncompleteKey incompleteKey2 = keyFactory.newKey(); // let cloud datastore automatically assign the ids List<Key> keys = datastore.allocateId(incompleteKey1, incompleteKey2); // [END batchAllocateId] return keys; }
@Test public void testAllocateId() throws Exception { Datastore datastore = createStrictMock(Datastore.class); IncompleteKey pKey1 = IncompleteKey.newBuilder("ds", "k").build(); Key key1 = Key.newBuilder(pKey1, 1).build(); expect(datastore.allocateId(new IncompleteKey[] {pKey1})) .andReturn(Collections.singletonList(key1)); replay(datastore); assertEquals(key1, DatastoreHelper.allocateId(datastore, pKey1)); verify(datastore); }
/** 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(); // calling transaction.commit() now would fail // [END rollback] return key; }
/** 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 } // [END commit] return key; }
@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)); }
@Test public void testAllocateIdArray() { KeyFactory keyFactory = datastore.newKeyFactory().setKind(KIND1); IncompleteKey incompleteKey1 = keyFactory.newKey(); IncompleteKey incompleteKey2 = keyFactory.setKind(KIND2).addAncestor(PathElement.of(KIND1, 10)).newKey(); Key key3 = keyFactory.newKey("name"); List<Key> result1 = datastore.allocateId(incompleteKey1, incompleteKey2, incompleteKey1); assertEquals(3, result1.size()); assertEquals(Key.newBuilder(incompleteKey1, result1.get(0).getId()).build(), result1.get(0)); assertEquals(Key.newBuilder(incompleteKey1, result1.get(2).getId()).build(), result1.get(2)); assertEquals(Key.newBuilder(incompleteKey2, result1.get(1).getId()).build(), result1.get(1)); try { datastore.allocateId(incompleteKey1, incompleteKey2, key3); fail("expecting a failure"); } catch (IllegalArgumentException expected) { assertEquals(expected.getMessage(), "keys must be IncompleteKey instances"); } }
/** 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 { // if committing succeeded // then transaction.isActive() will be false if (transaction.isActive()) { // otherwise it's true and we need to rollback transaction.rollback(); } } // [END active] return key; }
/** 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 { // if committing succeeded // then transaction.active() will be false if (transaction.isActive()) { // otherwise it's true and we need to rollback transaction.rollback(); } } // [END isActive] return key; } }
/** Allocate num copies of the incompleteKey */ private <T> KeyRange<T> allocate(final IncompleteKey incompleteKey, final int num) { final IncompleteKey[] allocations = new IncompleteKey[num]; Arrays.fill(allocations, incompleteKey); final List<Key<T>> typedKeys = datastore().allocateId(allocations).stream() .map(Key::<T>create) .collect(Collectors.toList()); return new KeyRange<>(typedKeys); }
@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); try { datastore.allocateId(key1); fail("Expecting a failure"); } catch (IllegalArgumentException expected) { assertEquals(expected.getMessage(), "keys must be IncompleteKey instances"); } }
@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); }
@Override public List<Key> allocateId(IncompleteKey... keys) { return delegate.allocateId(keys); }
/** * Adds a task entity to the Datastore. * * @param description The task description * @return The {@link Key} of the entity * @throws DatastoreException if the ID allocation or put fails */ Key addTask(String description) { Key key = datastore.allocateId(keyFactory.newKey()); Entity task = Entity.newBuilder(key) .set("description", StringValue.newBuilder(description).setExcludeFromIndexes(true).build()) .set("created", Timestamp.now()) .set("done", false) .build(); datastore.put(task); return key; } // [END datastore_add_entity]
/** * @see Datastore#allocateId(IncompleteKey) * @throws IOException if the underlying client throws {@link DatastoreException} */ Key allocateId(IncompleteKey newKey) throws IOException { return call(() -> datastore.allocateId(newKey)); } }
public void processElement(ProcessContext c) { final Datastore datastore = DatastoreOptions.defaultInstance().service(); final KeyFactory keyFactory = datastore.newKeyFactory().kind("Task"); Key key = datastore.allocateId(keyFactory.newKey()); Entity task = Entity.builder(key) .set("description", StringValue.builder(":D").excludeFromIndexes(true).build()) .set("created", DateTime.now()) .set("done", false) .build(); datastore.put(task); }