本文整理了Java中com.google.cloud.datastore.Datastore.allocateId()
方法的一些代码示例,展示了Datastore.allocateId()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Datastore.allocateId()
方法的具体详情如下:
包路径:com.google.cloud.datastore.Datastore
类名称:Datastore
方法名:allocateId
[英]Allocate a unique id for the given key. The returned key will have the same information (projectId, kind, namespace and ancestors) as the given key and will have a newly assigned id.
Example of allocating an id.
KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
[中]为给定密钥分配唯一的id。返回的密钥将具有与给定密钥相同的信息(projectId、种类、名称空间和祖先),并且将具有新分配的id。
分配id的示例。
KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
代码示例来源:origin: googleapis/google-cloud-java
static Key allocateId(Datastore service, IncompleteKey key) {
return service.allocateId(new IncompleteKey[] {key}).get(0);
}
代码示例来源:origin: googleapis/google-cloud-java
/** 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;
}
代码示例来源:origin: googleapis/google-cloud-java
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);
}
代码示例来源:origin: googleapis/google-cloud-java
if (!incompleteKeys.isEmpty()) {
IncompleteKey[] toAllocate = Iterables.toArray(incompleteKeys, IncompleteKey.class);
allocated = getDatastore().allocateId(toAllocate).iterator();
} else {
allocated = Collections.emptyIterator();
代码示例来源:origin: googleapis/google-cloud-java
if (!incompleteKeys.isEmpty()) {
IncompleteKey[] toAllocate = Iterables.toArray(incompleteKeys, IncompleteKey.class);
allocated = getDatastore().allocateId(toAllocate).iterator();
} else {
allocated = Collections.emptyIterator();
代码示例来源:origin: googleapis/google-cloud-java
/** 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;
}
代码示例来源:origin: googleapis/google-cloud-java
@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);
}
代码示例来源:origin: googleapis/google-cloud-java
/** 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;
}
代码示例来源:origin: googleapis/google-cloud-java
/** 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;
}
代码示例来源:origin: googleapis/google-cloud-java
@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));
}
代码示例来源:origin: googleapis/google-cloud-java
@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");
}
}
代码示例来源:origin: googleapis/google-cloud-java
/** 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;
}
代码示例来源:origin: googleapis/google-cloud-java
/** 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;
}
}
代码示例来源:origin: objectify/objectify
/** 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);
}
代码示例来源:origin: googleapis/google-cloud-java
@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");
}
}
代码示例来源:origin: googleapis/google-cloud-java
@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);
}
代码示例来源:origin: spotify/styx
@Override
public List<Key> allocateId(IncompleteKey... keys) {
return delegate.allocateId(keys);
}
代码示例来源:origin: GoogleCloudPlatform/java-docs-samples
/**
* 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]
代码示例来源:origin: spotify/styx
/**
* @see Datastore#allocateId(IncompleteKey)
* @throws IOException if the underlying client throws {@link DatastoreException}
*/
Key allocateId(IncompleteKey newKey) throws IOException {
return call(() -> datastore.allocateId(newKey));
}
}
代码示例来源:origin: stackoverflow.com
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);
}
内容来源于网络,如有侵权,请联系作者删除!