本文整理汇总了Java中com.google.cloud.datastore.Entity类的典型用法代码示例。如果您正苦于以下问题:Java Entity类的具体用法?Java Entity怎么用?Java Entity使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Entity类属于com.google.cloud.datastore包,在下文中一共展示了Entity类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: complete
import com.google.cloud.datastore.Entity; //导入依赖的package包/类
@Override
protected StructuredQuery.Builder<Entity> complete(StructuredQuery.Filter filter,
Sort sort) {
if (sort == null) {
return Query.newEntityQueryBuilder()
.setFilter(setAncestorFilter(filter));
}
StructuredQuery.OrderBy[] orderBy = StreamSupport
.stream(sort.spliterator(), false)
.map(order -> order.isAscending()
? StructuredQuery.OrderBy.asc(order.getProperty())
: StructuredQuery.OrderBy.desc(order.getProperty()))
.toArray(len -> new StructuredQuery.OrderBy[len]);
if (orderBy.length == 0) {
return Query.newEntityQueryBuilder()
.setFilter(setAncestorFilter(filter));
}
else {
return Query.newEntityQueryBuilder()
.addOrderBy(orderBy[0], orderBy)
.setFilter(setAncestorFilter(filter));
}
}
示例2: testAndCondition
import com.google.cloud.datastore.Entity; //导入依赖的package包/类
@Test
public void testAndCondition() throws Exception {
// Setup
GcloudDatastoreQueryCreator creator = createCreator(PersonRepository.class,
PersonRepository.class.getMethod("findByEmailAddressAndLastName",
String.class, String.class),
"[email protected]", "Doe");
// Exercise
StructuredQuery.Builder<Entity> queryBuilder = creator.createQuery();
// Verify
assertEquals(
Query.newEntityQueryBuilder()
.setFilter(StructuredQuery.CompositeFilter.and(
StructuredQuery.PropertyFilter.eq("emailAddress",
"[email protected]"),
StructuredQuery.PropertyFilter.eq("lastName", "Doe")))
.build(),
queryBuilder.build());
}
示例3: testUnmarshalToObject_Blob
import com.google.cloud.datastore.Entity; //导入依赖的package包/类
@Test
public void testUnmarshalToObject_Blob() {
// Setup
byte[] hello = "hello".getBytes(Charset.forName("UTF-8"));
Key key = Key.newBuilder("project", "kind", 1).build();
Entity entity = Entity.newBuilder(key).set("object", Blob.copyFrom(hello))
.set("bytes", Blob.copyFrom(hello)).set("string", Blob.copyFrom(hello))
.build();
Unmarshaller unmarshaller = new Unmarshaller();
TestBean bean = new TestBean();
// Exercise
unmarshaller.unmarshalToObject(entity, bean);
// Verify
Assert.assertArrayEquals(hello, (byte[]) bean.object);
Assert.assertArrayEquals(hello, bean.bytes);
Assert.assertEquals("hello", bean.string);
}
示例4: testUnmarshalToObject_Boolean
import com.google.cloud.datastore.Entity; //导入依赖的package包/类
@Test
public void testUnmarshalToObject_Boolean() {
// Setup
Key key = Key.newBuilder("project", "kind", 1).build();
Entity entity = Entity.newBuilder(key).set("object", true)
.set("boxedBoolean", true).set("primitiveBoolean", true).build();
Unmarshaller unmarshaller = new Unmarshaller();
TestBean bean = new TestBean();
// Exercise
unmarshaller.unmarshalToObject(entity, bean);
// Verify
Assert.assertEquals(Boolean.TRUE, bean.object);
Assert.assertEquals(Boolean.TRUE, bean.boxedBoolean);
Assert.assertTrue(bean.primitiveBoolean);
}
示例5: testUnmarshalToObject_Map1
import com.google.cloud.datastore.Entity; //导入依赖的package包/类
@Test
public void testUnmarshalToObject_Map1() {
// Setup
Key key = Key.newBuilder("project", "kind", 1).build();
Entity entity = Entity.newBuilder(key)
.set("object", Entity.newBuilder().set("k", "v").build())
.set("map", Entity.newBuilder().set("k", "v").build()).build();
Unmarshaller unmarshaller = new Unmarshaller();
TestBean bean = new TestBean();
// Exercise
unmarshaller.unmarshalToObject(entity, bean);
// Verify
Map<String, Object> expected = new HashMap<>();
expected.put("k", "v");
Assert.assertEquals(expected, bean.object);
Assert.assertEquals(expected, bean.map);
}
示例6: testUnmarshalToObject_Map2
import com.google.cloud.datastore.Entity; //导入依赖的package包/类
@Test
public void testUnmarshalToObject_Map2() {
// Setup
Key key = Key.newBuilder("project", "kind", 1).build();
Entity entity = Entity.newBuilder(key)
.set("object", Entity.newBuilder()
.set("k1", Entity.newBuilder().set("k2", "v2").build()).build())
.set("map", Entity.newBuilder()
.set("k1", Entity.newBuilder().set("k2", "v2").build()).build())
.build();
Unmarshaller unmarshaller = new Unmarshaller();
TestBean bean = new TestBean();
// Exercise
unmarshaller.unmarshalToObject(entity, bean);
// Verify
Map<String, Object> innerMap = new HashMap<>();
innerMap.put("k2", "v2");
Map<String, Object> expected = new HashMap<>();
expected.put("k1", innerMap);
Assert.assertEquals(expected, bean.object);
Assert.assertEquals(expected, bean.map);
}
示例7: marshal
import com.google.cloud.datastore.Entity; //导入依赖的package包/类
/**
* Marshals the given entity and and returns the equivalent Entity needed for the underlying Cloud
* Datastore API.
*
* @return A native entity that is equivalent to the POJO being marshalled. The returned value
* could either be a FullEntity or Entity.
*/
private BaseEntity<?> marshal() {
marshalKey();
if (key instanceof Key) {
entityBuilder = Entity.newBuilder((Key) key);
} else {
entityBuilder = FullEntity.newBuilder(key);
}
marshalFields();
marshalAutoTimestampFields();
if (intent == Intent.UPDATE) {
marshalVersionField();
}
marshalEmbeddedFields();
return entityBuilder.build();
}
示例8: insert
import com.google.cloud.datastore.Entity; //导入依赖的package包/类
/**
* Inserts the given list of entities into the Cloud Datastore.
*
* @param entities
* the entities to insert.
* @return the inserted entities. The inserted entities will not be same as the passed in
* entities. For example, the inserted entities may contain generated ID, key, parent key,
* etc.
* @throws EntityManagerException
* if any error occurs while inserting.
*/
@SuppressWarnings("unchecked")
public <E> List<E> insert(List<E> entities) {
if (entities == null || entities.isEmpty()) {
return new ArrayList<>();
}
try {
entityManager.executeEntityListeners(CallbackType.PRE_INSERT, entities);
FullEntity<?>[] nativeEntities = toNativeFullEntities(entities, entityManager, Intent.INSERT);
Class<?> entityClass = entities.get(0).getClass();
List<Entity> insertedNativeEntities = nativeWriter.add(nativeEntities);
List<E> insertedEntities = (List<E>) toEntities(entityClass, insertedNativeEntities);
entityManager.executeEntityListeners(CallbackType.POST_INSERT, insertedEntities);
return insertedEntities;
} catch (DatastoreException exp) {
throw DatastoreUtils.wrap(exp);
}
}
示例9: update
import com.google.cloud.datastore.Entity; //导入依赖的package包/类
/**
* Updates the given entity in the Cloud Datastore. The passed in Entity must have its ID set for
* the update to work.
*
* @param entity
* the entity to update
* @return the updated entity.
* @throws EntityManagerException
* if any error occurs while updating.
*/
@SuppressWarnings("unchecked")
public <E> E update(E entity) {
try {
entityManager.executeEntityListeners(CallbackType.PRE_UPDATE, entity);
Intent intent = (nativeWriter instanceof Batch) ? Intent.BATCH_UPDATE : Intent.UPDATE;
Entity nativeEntity = (Entity) Marshaller.marshal(entityManager, entity, intent);
nativeWriter.update(nativeEntity);
E updatedEntity = (E) Unmarshaller.unmarshal(nativeEntity, entity.getClass());
entityManager.executeEntityListeners(CallbackType.POST_UPDATE, updatedEntity);
return updatedEntity;
} catch (DatastoreException exp) {
throw DatastoreUtils.wrap(exp);
}
}
示例10: upsert
import com.google.cloud.datastore.Entity; //导入依赖的package包/类
/**
* Updates or inserts the given list of entities in the Cloud Datastore. If the entities do not
* have a valid ID, IDs may be generated.
*
* @param entities
* the entities to update/or insert.
* @return the updated or inserted entities
* @throws EntityManagerException
* if any error occurs while saving.
*/
@SuppressWarnings("unchecked")
public <E> List<E> upsert(List<E> entities) {
if (entities == null || entities.isEmpty()) {
return new ArrayList<>();
}
try {
entityManager.executeEntityListeners(CallbackType.PRE_UPSERT, entities);
FullEntity<?>[] nativeEntities = toNativeFullEntities(entities, entityManager, Intent.UPSERT);
Class<?> entityClass = entities.get(0).getClass();
List<Entity> upsertedNativeEntities = nativeWriter.put(nativeEntities);
List<E> upsertedEntities = (List<E>) toEntities(entityClass, upsertedNativeEntities);
entityManager.executeEntityListeners(CallbackType.POST_UPSERT, upsertedEntities);
return upsertedEntities;
} catch (DatastoreException exp) {
throw DatastoreUtils.wrap(exp);
}
}
示例11: store
import com.google.cloud.datastore.Entity; //导入依赖的package包/类
void store(Workflow workflow) throws IOException {
storeWithRetries(() -> datastore.runInTransaction(transaction -> {
final Key componentKey = componentKeyFactory.newKey(workflow.componentId());
if (transaction.get(componentKey) == null) {
transaction.put(Entity.newBuilder(componentKey).build());
}
final String json = OBJECT_MAPPER.writeValueAsString(workflow);
final Key workflowKey = workflowKey(workflow.id());
final Optional<Entity> workflowOpt = getOpt(transaction, workflowKey);
final Entity workflowEntity = asBuilderOrNew(workflowOpt, workflowKey)
.set(PROPERTY_WORKFLOW_JSON, StringValue.newBuilder(json).setExcludeFromIndexes(true).build())
.build();
return transaction.put(workflowEntity);
}));
}
示例12: updateNextNaturalTrigger
import com.google.cloud.datastore.Entity; //导入依赖的package包/类
public void updateNextNaturalTrigger(WorkflowId workflowId, TriggerInstantSpec triggerSpec) throws IOException {
storeWithRetries(() -> datastore.runInTransaction(transaction -> {
final Key workflowKey = workflowKey(workflowId);
final Optional<Entity> workflowOpt = getOpt(transaction, workflowKey);
if (!workflowOpt.isPresent()) {
throw new ResourceNotFoundException(
String.format("%s:%s doesn't exist.", workflowId.componentId(), workflowId.id()));
}
final Entity.Builder builder = Entity
.newBuilder(workflowOpt.get())
.set(PROPERTY_NEXT_NATURAL_TRIGGER, instantToTimestamp(triggerSpec.instant()))
.set(PROPERTY_NEXT_NATURAL_OFFSET_TRIGGER, instantToTimestamp(triggerSpec.offsetInstant()));
return transaction.put(builder.build());
}));
}
示例13: workflows
import com.google.cloud.datastore.Entity; //导入依赖的package包/类
public Map<WorkflowId, Workflow> workflows() {
final Map<WorkflowId, Workflow> map = Maps.newHashMap();
final EntityQuery query = Query.newEntityQueryBuilder().setKind(KIND_WORKFLOW).build();
final QueryResults<Entity> result = datastore.run(query);
while (result.hasNext()) {
final Entity entity = result.next();
final Workflow workflow;
try {
workflow = OBJECT_MAPPER.readValue(entity.getString(PROPERTY_WORKFLOW_JSON), Workflow.class);
} catch (IOException e) {
LOG.warn("Failed to read workflow {}.", entity.getKey());
continue;
}
map.put(workflow.id(), workflow);
}
return map;
}
示例14: patchState
import com.google.cloud.datastore.Entity; //导入依赖的package包/类
void patchState(WorkflowId workflowId, WorkflowState state) throws IOException {
storeWithRetries(() -> datastore.runInTransaction(transaction -> {
final Key workflowKey = workflowKey(workflowId);
final Optional<Entity> workflowOpt = getOpt(transaction, workflowKey);
if (!workflowOpt.isPresent()) {
throw new ResourceNotFoundException(
String.format("%s:%s doesn't exist.", workflowId.componentId(), workflowId.id()));
}
final Entity.Builder builder = Entity.newBuilder(workflowOpt.get());
state.enabled().ifPresent(x -> builder.set(PROPERTY_WORKFLOW_ENABLED, x));
state.nextNaturalTrigger()
.ifPresent(x -> builder.set(PROPERTY_NEXT_NATURAL_TRIGGER, instantToTimestamp(x)));
state.nextNaturalOffsetTrigger()
.ifPresent(x -> builder.set(PROPERTY_NEXT_NATURAL_OFFSET_TRIGGER, instantToTimestamp(x)));
return transaction.put(builder.build());
}));
}
示例15: entityToBackfill
import com.google.cloud.datastore.Entity; //导入依赖的package包/类
private Backfill entityToBackfill(Entity entity) {
final WorkflowId workflowId = WorkflowId.create(entity.getString(PROPERTY_COMPONENT),
entity.getString(PROPERTY_WORKFLOW));
final BackfillBuilder builder = Backfill.newBuilder()
.id(entity.getKey().getName())
.start(timestampToInstant(entity.getTimestamp(PROPERTY_START)))
.end(timestampToInstant(entity.getTimestamp(PROPERTY_END)))
.workflowId(workflowId)
.concurrency((int) entity.getLong(PROPERTY_CONCURRENCY))
.nextTrigger(timestampToInstant(entity.getTimestamp(PROPERTY_NEXT_TRIGGER)))
.schedule(Schedule.parse(entity.getString(PROPERTY_SCHEDULE)))
.allTriggered(entity.getBoolean(PROPERTY_ALL_TRIGGERED))
.halted(entity.getBoolean(PROPERTY_HALTED));
if (entity.contains(PROPERTY_DESCRIPTION)) {
builder.description(entity.getString(PROPERTY_DESCRIPTION));
}
return builder.build();
}