com.google.cloud.datastore.Entity Java Examples

The following examples show how to use com.google.cloud.datastore.Entity. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DeletedBlobIndex.java    From nexus-blobstore-google-cloud with Eclipse Public License 1.0 7 votes vote down vote up
/**
 * Removes all deleted blobs tracked in this index.
 *
 */
void removeData() {
  log.warn("removing all entries in the index of soft-deleted blobs...");
  Query<Entity> query = Query.newEntityQueryBuilder()
      .setNamespace(namespace)
      .setKind(DELETED_BLOBS)
      .build();
  QueryResults<Entity> results = gcsDatastore.run(query);
  List<Key> keys = StreamSupport.stream(
      Spliterators.spliteratorUnknownSize(results, Spliterator.ORDERED),
      false).map(entity -> entity.getKey()).collect(Collectors.toList());

  // datastore has a hard limit of 500 keys in a single delete
  List<List<Key>> partitions = Lists.partition(keys, 500);
  partitions.stream().forEach(partition -> gcsDatastore.delete(partition.toArray(new Key[partition.size()])) );

  log.warn("deleted {} blobIds from the soft-deleted blob index", keys.size());
}
 
Example #2
Source File: DefaultDatastoreWriter.java    From catatumbo with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
  }
}
 
Example #3
Source File: DatastoreStore.java    From tomcat-runtime with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new session usable by Tomcat, from a serialized session in a Datastore Entity.
 * @param sessionKey The key associated with the session metadata and attributes.
 * @return A new session containing the metadata and attributes stored in the entity.
 * @throws ClassNotFoundException Thrown if a class serialized in the entity is not available in
 *                                this context.
 * @throws IOException Thrown when an error occur during the deserialization.
 */
private DatastoreSession deserializeSession(Key sessionKey)
    throws ClassNotFoundException, IOException {
  TraceContext loadingSessionContext = startSpan("Fetching the session from Datastore");
  Iterator<Entity> entities = datastore.run(Query.newEntityQueryBuilder()
      .setKind(sessionKind)
      .setFilter(PropertyFilter.hasAncestor(sessionKey))
      .build());
  endSpan(loadingSessionContext);

  DatastoreSession session = null;
  if (entities.hasNext()) {
    session = (DatastoreSession) manager.createEmptySession();
    TraceContext deserializationContext = startSpan("Deserialization of the session");
    session.restoreFromEntities(sessionKey, Lists.newArrayList(entities));
    endSpan(deserializationContext);
  }
  return session;
}
 
Example #4
Source File: GoogleJobStore.java    From data-transfer-project with Apache License 2.0 6 votes vote down vote up
@Override
public <T extends DataModel> void update(UUID jobId, String key, T model) {
  Transaction transaction = datastore.newTransaction();
  Key entityKey = getDataKey(jobId, key);

  try {
    Entity previousEntity = transaction.get(entityKey);
    if (previousEntity == null) {
      throw new IOException("Could not find record for data key: " + entityKey.getName());
    }

    String serialized = objectMapper.writeValueAsString(model);
    Entity entity =
        Entity.newBuilder(entityKey)
            .set(CREATED_FIELD, Timestamp.now())
            .set(model.getClass().getName(), serialized)
            .build();

    transaction.put(entity);
    transaction.commit();
  } catch (IOException t) {
    transaction.rollback();
    throw new RuntimeException("Failed atomic update of key: " + key, t);
  }
}
 
Example #5
Source File: GoogleJobStore.java    From data-transfer-project with Apache License 2.0 6 votes vote down vote up
@Override
public void addErrorsToJob(UUID jobId, Collection<ErrorDetail> errors) throws IOException {
  if (errors == null || errors.isEmpty()) {
    return;
  }
  List<Entity> entities = new ArrayList<>();
  for (ErrorDetail errorDetail : errors) {
    Key key = getErrorKey(jobId, errorDetail.id());
    entities.add(
        GoogleCloudUtils.createEntityBuilder(
                key,
                ImmutableMap.of(
                    JSON_DATA_FIELD,
                    // TODO: encrypt this data
                    objectMapper.writeValueAsString(errorDetail)))
            .build());
  }
  datastore.add(entities.toArray(new Entity[entities.size()]));
}
 
Example #6
Source File: DatastoreExample.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
@Override
public void run(Transaction tx, Key userKey, Void arg) {
  Entity user = tx.get(userKey);
  if (user == null) {
    System.out.println("Nothing to delete, user does not exist.");
    return;
  }
  Query<Key> query =
      Query.newKeyQueryBuilder()
          .setNamespace(NAMESPACE)
          .setKind(COMMENT_KIND)
          .setFilter(PropertyFilter.hasAncestor(userKey))
          .build();
  QueryResults<Key> comments = tx.run(query);
  int count = 0;
  while (comments.hasNext()) {
    tx.delete(comments.next());
    count++;
  }
  tx.delete(userKey);
  System.out.printf("Deleting user '%s' and %d comment[s].%n", userKey.getName(), count);
}
 
Example #7
Source File: GoogleJobStore.java    From data-transfer-project with Apache License 2.0 6 votes vote down vote up
@Override
public void addCounts(UUID jobId, Map<String, Integer> newCounts) throws IOException {
  if (newCounts == null) {
    return;
  }

  Transaction transaction = datastore.newTransaction();

  for (String dataType : newCounts.keySet()) {
    Key key = getCountsKey(jobId, dataType);
    Entity current = datastore.get(key);
    Integer oldCount = 0;

    if (current != null && current.getNames().contains(COUNTS_FIELD)) {
      // Datastore only allows Long properties, but we only ever write Integers through this
      // interface so the conversion is OK
      oldCount = Math.toIntExact(current.getLong(COUNTS_FIELD));
    }
    transaction.put(
        GoogleCloudUtils.createEntityBuilder(
                key, ImmutableMap.of(COUNTS_FIELD, oldCount + newCounts.get(dataType)))
            .build());
  }
  transaction.commit();
}
 
Example #8
Source File: ShardedCounterMetricsStore.java    From nexus-blobstore-google-cloud with Eclipse Public License 1.0 6 votes vote down vote up
private Entity getShardCounter(final String location) {
  KeyFactory keyFactory = datastore.newKeyFactory().addAncestors(
      NXRM_ROOT,
      PathElement.of(METRICS_STORE, 1L)
  );
  Key key = keyFactory.setNamespace(namespace)
      .setKind(SHARD).newKey(location);
  Entity exists = datastore.get(key);
  if (exists != null) {
    log.trace("counter for {} already present", location);
    return exists;
  }

  log.debug("creating metrics store counter shard for {}", location);
  // otherwise make it
  Entity entity = Entity.newBuilder(key)
      .set(SIZE, LongValue.newBuilder(0L).build())
      .set(COUNT, LongValue.newBuilder(0L).build())
      .build();

  return datastore.put(entity);
}
 
Example #9
Source File: MessageRepositoryImpl.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Override
public List<String> retrieveClaims(int limit) {
  // Get claim saved in Datastore
  Datastore datastore = getDatastoreInstance();
  Query<Entity> query = Query.newEntityQueryBuilder().setKind(claimsKind).setLimit(limit).build();
  QueryResults<Entity> results = datastore.run(query);

  List<String> claims = new ArrayList<>();
  while (results.hasNext()) {
    Entity entity = results.next();
    String claim = entity.getString("claim");
    if (claim != null) {
      claims.add(claim);
    }
  }
  return claims;
}
 
Example #10
Source File: GoogleCloudUtils.java    From data-transfer-project with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an Entity Builder for the given key and properties. Converts the objects to the proper
 * datastore values
 */
static Entity.Builder createEntityBuilder(Key key, Map<String, Object> data) throws IOException {
  Entity.Builder builder = Entity.newBuilder(key);

  for (Map.Entry<String, Object> entry : data.entrySet()) {
    if (entry.getValue() instanceof String) {
      builder.set(entry.getKey(), (String) entry.getValue()); // StringValue
    } else if (entry.getValue() instanceof Integer) {
      builder.set(entry.getKey(), (Integer) entry.getValue()); // LongValue
    } else if (entry.getValue() instanceof Double) {
      builder.set(entry.getKey(), (Double) entry.getValue()); // DoubleValue
    } else if (entry.getValue() instanceof Boolean) {
      builder.set(entry.getKey(), (Boolean) entry.getValue()); // BooleanValue
    } else if (entry.getValue() instanceof Timestamp) {
      builder.set(entry.getKey(), (Timestamp) entry.getValue()); // TimestampValue
    } else {
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      try (ObjectOutputStream out = new ObjectOutputStream(bos)) {
        out.writeObject(entry.getValue());
      }
      builder.set(entry.getKey(), Blob.copyFrom(bos.toByteArray())); // BlobValue
    }
  }
  return builder;
}
 
Example #11
Source File: DatastoreSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of starting a new batch. */
// [TARGET newBatch()]
// [VARIABLE "my_key_name_1"]
// [VARIABLE "my_key_name_2"]
public Batch newBatch(String keyName1, String keyName2) {
  // [START newBatch]
  Key key1 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName1);
  Key key2 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName2);
  Batch batch = datastore.newBatch();
  Entity entity1 = Entity.newBuilder(key1).set("name", "John").build();
  Entity entity2 = Entity.newBuilder(key2).set("title", "title").build();
  batch.add(entity1);
  batch.add(entity2);
  batch.submit();
  // [END newBatch]
  return batch;
}
 
Example #12
Source File: UserService.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
/**
 * Updates a user in Cloud Datastore.
 */
public User updateUser(String id, String name, String email) {
  failIfInvalid(name, email);
  Key key = keyFactory.newKey(id);
  Entity entity = datastore.get(key);
  if (entity == null) {
    throw new IllegalArgumentException("No user with id '" + id + "' found");
  } else {
    entity = Entity.newBuilder(entity)
        .set("id", id)
        .set("name", name)
        .set("email", email)
        .build();
    datastore.update(entity);
  }
  return new User(id, name, email);
}
 
Example #13
Source File: DatastoreSessionTest.java    From tomcat-runtime with Apache License 2.0 6 votes vote down vote up
@Test
public void testMetadataDeserialization() throws Exception {
  Entity metadata = Entity.newBuilder(sessionKey)
      .set(SessionMetadata.MAX_INACTIVE_INTERVAL, 0)
      .set(SessionMetadata.CREATION_TIME, 1)
      .set(SessionMetadata.LAST_ACCESSED_TIME, 2)
      .set(SessionMetadata.IS_NEW, true)
      .set(SessionMetadata.IS_VALID, true)
      .set(SessionMetadata.THIS_ACCESSED_TIME, 3)
      .build();

  DatastoreSession session = new DatastoreSession(sessionManager);
  session.restoreFromEntities(sessionKey, Collections.singleton(metadata));

  assertEquals(session.getMaxInactiveInterval(), 0);
  assertEquals(session.getCreationTime(), 1);
  assertEquals(session.getLastAccessedTime(), 2);
  assertEquals(session.isNew(), true);
  assertEquals(session.isValid(), true);
  assertEquals(session.getThisAccessedTime(), 3);
}
 
Example #14
Source File: DefaultDatastoreEntityConverterTests.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@Test
public void writeTestSubtypes() {
	DiscrimEntityD entityD = new DiscrimEntityD();
	entityD.stringField = "item D";
	entityD.intField = 10;
	entityD.enumField = TestDatastoreItem.Color.BLACK;

	Entity.Builder builder = getEntityBuilder();
	ENTITY_CONVERTER.write(entityD, builder);

	Entity entity = builder.build();


	assertThat(entity.getString("stringField")).as("validate string field").isEqualTo("item D");
	assertThat(entity.getLong("intField")).as("validate int field").isEqualTo(10L);
	assertThat(entity.getString("enumField")).as("validate enum field").isEqualTo("BLACK");
	assertThat(entity.getList("discrimination_column")).as("validate discrimination field")
			.containsExactly(StringValue.of("D"), StringValue.of("B"), StringValue.of("X"));
}
 
Example #15
Source File: DatastoreSessionFilter.java    From getting-started-java with Apache License 2.0 6 votes vote down vote up
@Override
public void init(FilterConfig config) throws ServletException {
  // initialize local copy of datastore session variables

  datastore = DatastoreOptions.getDefaultInstance().getService();
  keyFactory = datastore.newKeyFactory().setKind("SessionVariable");
  // Delete all sessions unmodified for over two days
  DateTime dt = DateTime.now(DateTimeZone.UTC);
  Query<Entity> query = Query.newEntityQueryBuilder()
      .setKind("SessionVariable")
      .setFilter(PropertyFilter.le("lastModified", dt.minusDays(2).toString(dtf)))
      .build();
  QueryResults<Entity> resultList = datastore.run(query);
  while (resultList.hasNext()) {
    Entity stateEntity = resultList.next();
    datastore.delete(stateEntity.getKey());
  }
}
 
Example #16
Source File: DatastoreSessionFilter.java    From getting-started-java with Apache License 2.0 6 votes vote down vote up
protected void deleteSessionWithValue(String varName, String varValue) {
  Transaction transaction = datastore.newTransaction();
  try {
    Query<Entity> query = Query.newEntityQueryBuilder()
        .setKind("SessionVariable")
        .setFilter(PropertyFilter.eq(varName, varValue))
        .build();
    QueryResults<Entity> resultList = transaction.run(query);
    while (resultList.hasNext()) {
      Entity stateEntity = resultList.next();
      transaction.delete(stateEntity.getKey());
    }
    transaction.commit();
  } finally {
    if (transaction.isActive()) {
      transaction.rollback();
    }
  }
}
 
Example #17
Source File: DatastoreSessionFilter.java    From getting-started-java with Apache License 2.0 6 votes vote down vote up
@Override
public void init(FilterConfig config) throws ServletException {
  // initialize local copy of datastore session variables

  datastore = DatastoreOptions.getDefaultInstance().getService();
  keyFactory = datastore.newKeyFactory().setKind("SessionVariable");
  // Delete all sessions unmodified for over two days
  DateTime dt = DateTime.now(DateTimeZone.UTC);
  Query<Entity> query = Query.newEntityQueryBuilder()
      .setKind("SessionVariable")
      .setFilter(PropertyFilter.le("lastModified", dt.minusDays(2).toString(dtf)))
      .build();
  QueryResults<Entity> resultList = datastore.run(query);
  while (resultList.hasNext()) {
    Entity stateEntity = resultList.next();
    datastore.delete(stateEntity.getKey());
  }
}
 
Example #18
Source File: DatastoreStorage.java    From styx with Apache License 2.0 6 votes vote down vote up
static RunState entityToRunState(Entity entity, WorkflowInstance instance)
    throws IOException {
  final long counter = entity.getLong(PROPERTY_COUNTER);
  final State state = State.valueOf(entity.getString(PROPERTY_STATE));
  final long timestamp = entity.getLong(PROPERTY_STATE_TIMESTAMP);
  final StateData data = StateData.newBuilder()
      .tries((int) entity.getLong(PROPERTY_STATE_TRIES))
      .consecutiveFailures((int) entity.getLong(PROPERTY_STATE_CONSECUTIVE_FAILURES))
      .retryCost(entity.getDouble(PROPERTY_STATE_RETRY_COST))
      .trigger(DatastoreStorage.<String>readOpt(entity, PROPERTY_STATE_TRIGGER_TYPE).map(type ->
          TriggerUtil.trigger(type, entity.getString(PROPERTY_STATE_TRIGGER_ID))))
      .messages(OBJECT_MAPPER.<List<Message>>readValue(entity.getString(PROPERTY_STATE_MESSAGES),
          new TypeReference<List<Message>>() { }))
      .retryDelayMillis(readOpt(entity, PROPERTY_STATE_RETRY_DELAY_MILLIS))
      .lastExit(DatastoreStorage.<Long>readOpt(entity, PROPERTY_STATE_LAST_EXIT).map(Long::intValue))
      .executionId(readOpt(entity, PROPERTY_STATE_EXECUTION_ID))
      .runnerId(readOpt(entity, PROPERTY_STATE_RUNNER_ID))
      .executionDescription(readOptJson(entity, PROPERTY_STATE_EXECUTION_DESCRIPTION,
          ExecutionDescription.class))
      .resourceIds(readOptJson(entity, PROPERTY_STATE_RESOURCE_IDS,
          new TypeReference<Set<String>>() { }))
      .triggerParameters(readOptJson(entity, PROPERTY_STATE_TRIGGER_PARAMETERS, TriggerParameters.class))
      .build();
  return RunState.create(instance, state, data, Instant.ofEpochMilli(timestamp), counter);
}
 
Example #19
Source File: ConceptsTest.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
/**
 * Initializes Datastore and cleans out any residual values.  Also initializes global variables
 * used for testing.
 */
@Before
public void setUp() {
  datastore = HELPER.getOptions().toBuilder().setNamespace("ghijklmnop").build().getService();
  StructuredQuery<Key> query = Query.newKeyQueryBuilder().build();
  QueryResults<Key> result = datastore.run(query);
  datastore.delete(Iterators.toArray(result, Key.class));
  keyFactory = datastore.newKeyFactory().setKind("Task");
  taskKey = keyFactory.newKey("some-arbitrary-key");
  testEntity = Entity.newBuilder(taskKey, TEST_FULL_ENTITY).build();
  Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
  calendar.set(1990, JANUARY, 1);
  startDate = Timestamp.of(calendar.getTime());
  calendar.set(2000, JANUARY, 1);
  endDate = Timestamp.of(calendar.getTime());
  calendar.set(1999, DECEMBER, 31);
  includedDate = Timestamp.of(calendar.getTime());
}
 
Example #20
Source File: DatastoreSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of updating multiple entities. */
// [TARGET update(Entity...)]
// [VARIABLE "my_key_name_1"]
// [VARIABLE "my_key_name_2"]
public void batchUpdateEntities(String keyName1, String keyName2) {
  // [START batchUpdateEntities]
  Key key1 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName1);
  Entity.Builder entityBuilder1 = Entity.newBuilder(key1);
  entityBuilder1.set("propertyName", "updatedValue1");
  Entity entity1 = entityBuilder1.build();

  Key key2 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName2);
  Entity.Builder entityBuilder2 = Entity.newBuilder(key2);
  entityBuilder2.set("propertyName", "updatedValue2");
  Entity entity2 = entityBuilder2.build();

  datastore.update(entity1, entity2);
  // [END batchUpdateEntities]
}
 
Example #21
Source File: DatastoreDao.java    From getting-started-java with Apache License 2.0 6 votes vote down vote up
@Override
public Result<Book> listBooks(String startCursorString) {
  Cursor startCursor = null;
  if (startCursorString != null && !startCursorString.equals("")) {
    startCursor = Cursor.fromUrlSafe(startCursorString);    // Where we left off
  }
  Query<Entity> query = Query.newEntityQueryBuilder()       // Build the Query
      .setKind("Book3")                                     // We only care about Books
      .setLimit(10)                                         // Only show 10 at a time
      .setStartCursor(startCursor)                          // Where we left off
      .setOrderBy(OrderBy.asc(Book.TITLE))                  // Use default Index "title"
      .build();
  QueryResults<Entity> resultList = datastore.run(query);   // Run the query
  List<Book> resultBooks = entitiesToBooks(resultList);     // Retrieve and convert Entities
  Cursor cursor = resultList.getCursorAfter();              // Where to start next time
  if (cursor != null && resultBooks.size() == 10) {         // Are we paging? Save Cursor
    String cursorString = cursor.toUrlSafe();               // Cursors are WebSafe
    return new Result<>(resultBooks, cursorString);
  } else {
    return new Result<>(resultBooks);
  }
}
 
Example #22
Source File: ITDatastoreSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testRunQuery() {
  String kindToFind = "ClassToFind";
  String kindToMiss = "OtherClass";

  String keyNameToFind = registerKey("my_key_name_to_find", kindToFind);
  String otherKeyNameToFind = registerKey("other_key_name_to_find", kindToFind);
  String keyNameToMiss = registerKey("my_key_name_to_miss", kindToMiss);

  String property = "my_property_name";

  String valueToFind = "my_value_to_find";
  String valueToMiss = "my_value_to_miss";

  addEntity(keyNameToFind, kindToFind, property, valueToFind);
  addEntity(otherKeyNameToFind, kindToFind, property, valueToMiss);
  addEntity(keyNameToMiss, kindToMiss, property, valueToFind);

  List<Entity> queryResults = datastoreSnippets.runQuery(kindToFind);
  assertNotNull(queryResults);
  assertEquals(2, queryResults.size());

  queryResults = datastoreSnippets.runQueryOnProperty(kindToFind, property, valueToFind);
  assertNotNull(queryResults);
  assertEquals(1, queryResults.size());
}
 
Example #23
Source File: DatastoreSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of fetching a list of Entity objects. */
// [TARGET fetch(Iterable, ReadOption...)]
// [VARIABLE "my_first_key_name"]
// [VARIABLE "my_second_key_name"]
public List<Entity> fetchEntitiesWithKeys(String firstKeyName, String secondKeyName) {
  // [START fetchEntitiesWithKeys]
  KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
  Key firstKey = keyFactory.newKey(firstKeyName);
  Key secondKey = keyFactory.newKey(secondKeyName);
  List<Entity> entities = datastore.fetch(Lists.newArrayList(firstKey, secondKey));
  for (Entity entity : entities) {
    // do something with the entity
  }
  // [END fetchEntitiesWithKeys]
  return entities;
}
 
Example #24
Source File: OptionalFieldsTest.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Test
public void testInsert_PhoneList() {
  OptionalFieldsEntity2 entity = OptionalFieldsEntity2.getSample1();
  OptionalFieldsEntity2 entity2 = em.insert(entity);
  OptionalFieldsEntity2 entity3 = em.load(OptionalFieldsEntity2.class, entity2.getId());
  com.google.cloud.datastore.Entity nativeEntity = datastore.get(entity2.getKey().nativeKey());

  List<EntityValue> entityValues = nativeEntity.getList("phoneNumbers");
  assertTrue(entityValues.get(0).get().contains("countryCode"));
  assertTrue(entityValues.get(1).get().contains("countryCode"));
  assertFalse(entityValues.get(2).get().contains("countryCode"));

  assertEquals(entity.getPhoneNumbers(), entity2.getPhoneNumbers());
  assertEquals(entity.getPhoneNumbers(), entity3.getPhoneNumbers());
}
 
Example #25
Source File: DatastoreStorage.java    From styx with Apache License 2.0 5 votes vote down vote up
Optional<Resource> getResource(String id) throws IOException {
  Entity entity = datastore.get(datastore.newKeyFactory().setKind(KIND_COUNTER_LIMIT).newKey(id));
  if (entity == null) {
    return Optional.empty();
  }
  return Optional.of(entityToResource(entity));
}
 
Example #26
Source File: EntityPropertyValueProviderTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void getPropertyValue() {
	byte[] bytes = { 1, 2, 3 };
	Entity entity = Entity.newBuilder(this.datastore.newKeyFactory().setKind("aKind").newKey("1"))
			.set("stringField", "string value")
			.set("boolField", true)
			.set("doubleField", 3.1415D)
			.set("longField", 123L)
			.set("latLngField", LatLng.of(10, 20))
			.set("timestampField", Timestamp.ofTimeSecondsAndNanos(30, 40))
			.set("blobField", Blob.copyFrom(bytes))
			.build();

	EntityPropertyValueProvider provider = new EntityPropertyValueProvider(entity, this.twoStepsConversion);

	assertThat((String) provider.getPropertyValue(this.persistentEntity.getPersistentProperty("stringField")))
			.as("validate string field").isEqualTo("string value");
	assertThat((Boolean) provider.getPropertyValue(this.persistentEntity.getPersistentProperty("boolField")))
			.as("validate boolean field").isTrue();
	assertThat((Double) provider.getPropertyValue(this.persistentEntity.getPersistentProperty("doubleField")))
			.as("validate double field").isEqualTo(3.1415D);
	assertThat((Long) provider.getPropertyValue(this.persistentEntity.getPersistentProperty("longField")))
			.as("validate long field").isEqualTo(123L);
	assertThat((LatLng) provider.getPropertyValue(this.persistentEntity.getPersistentProperty("latLngField")))
			.as("validate latLng field").isEqualTo(LatLng.of(10, 20));
	assertThat((Timestamp) provider.getPropertyValue(this.persistentEntity.getPersistentProperty("timestampField")))
			.as("validate timestamp field")
			.isEqualTo(Timestamp.ofTimeSecondsAndNanos(30, 40));
	assertThat((Blob) provider.getPropertyValue(this.persistentEntity.getPersistentProperty("blobField")))
			.as("validate blob field").isEqualTo(Blob.copyFrom(bytes));
}
 
Example #27
Source File: TransactionSnippets.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
/** Example of adding a single entity. */
// [TARGET add(FullEntity)]
// [VARIABLE "my_key_name"]
public void addSingleEntity(String keyName) {
  Datastore datastore = transaction.getDatastore();
  // [START addSingleEntity]
  Key key = datastore.newKeyFactory().setKind("MyKind").newKey(keyName);
  Entity.Builder entityBuilder = Entity.newBuilder(key);
  entityBuilder.set("propertyName", "value");
  Entity entity = entityBuilder.build();
  transaction.add(entity);
  transaction.commit();
  // [END addSingleEntity]
}
 
Example #28
Source File: DatastoreDao.java    From getting-started-java with Apache License 2.0 5 votes vote down vote up
public List<Book> entitiesToBooks(QueryResults<Entity> resultList) {
  List<Book> resultBooks = new ArrayList<>();
  while (resultList.hasNext()) {  // We still have data
    resultBooks.add(entityToBook(resultList.next()));      // Add the Book to the List
  }
  return resultBooks;
}
 
Example #29
Source File: DefaultDatastoreEntityConverterTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void testCollectionFieldsUnsupportedWriteReadException() {
	this.thrown.expect(DatastoreDataException.class);
	this.thrown.expectMessage(
			"Unable to read org.springframework.cloud.gcp.data.datastore.core.convert." +
					"TestItemUnsupportedFields$CollectionOfUnsupportedTypes entity");
	this.thrown.expectMessage("Unable to read property unsupportedElts");
	this.thrown.expectMessage("Unable process elements of a collection");
	this.thrown.expectMessage(
			"No converter found capable of converting from type [java.lang.Integer] " +
			"to type [org.springframework.cloud.gcp.data.datastore.core.convert." +
			"TestItemUnsupportedFields$NewType]");

	TestItemUnsupportedFields.CollectionOfUnsupportedTypes item = getCollectionOfUnsupportedTypesItem();

	DatastoreEntityConverter entityConverter =
			new DefaultDatastoreEntityConverter(new DatastoreMappingContext(),
					new TwoStepsConversions(new DatastoreCustomConversions(Collections.singletonList(
							getNewTypeToIntegerConverter()
					)), null, datastoreMappingContext));

	Entity.Builder builder = getEntityBuilder();
	entityConverter.write(item, builder);
	Entity entity = builder.build();

	entityConverter.read(TestItemUnsupportedFields.CollectionOfUnsupportedTypes.class, entity);
}
 
Example #30
Source File: DefaultDatastoreReader.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
/**
 * Fetches a list of entities for the given native keys.
 * 
 * @param entityClass
 *          the expected result type
 * @param nativeKeys
 *          the native keys of the entities
 * @return the list of entities. If one or more keys do not exist, the corresponding item in the
 *         returned list will be <code>null</code>.
 */
private <E> List<E> fetch(Class<E> entityClass, Key[] nativeKeys) {
  try {
    List<Entity> nativeEntities = nativeReader.fetch(nativeKeys);
    List<E> entities = DatastoreUtils.toEntities(entityClass, nativeEntities);
    entityManager.executeEntityListeners(CallbackType.POST_LOAD, entities);
    return entities;
  } catch (DatastoreException exp) {
    throw new EntityManagerException(exp);
  }
}