com.google.cloud.datastore.DatastoreException Java Examples

The following examples show how to use com.google.cloud.datastore.DatastoreException. 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: TransactionSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** 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;
}
 
Example #2
Source File: GoogleJobStore.java    From data-transfer-project with Apache License 2.0 6 votes vote down vote up
/**
 * Inserts a new {@link PortabilityJob} keyed by {@code jobId} in Datastore.
 *
 * <p>To update an existing {@link PortabilityJob} instead, use {@link JobStore#update}.
 *
 * @throws IOException if a job already exists for {@code jobId}, or if there was a different
 *     problem inserting the job.
 */
@Override
public void createJob(UUID jobId, PortabilityJob job) throws IOException {
  Preconditions.checkNotNull(jobId);
  Transaction transaction = datastore.newTransaction();
  Entity shouldNotExist = transaction.get(getJobKey(jobId));
  if (shouldNotExist != null) {
    transaction.rollback();
    throw new IOException(
        "Record already exists for jobID: " + jobId + ". Record: " + shouldNotExist);
  }
  Entity entity = createNewEntity(jobId, job.toMap());
  try {
    transaction.put(entity);
  } catch (DatastoreException e) {
    transaction.rollback();
    throw new IOException(
        "Could not create initial record for jobID: " + jobId + ". Record: " + entity, e);
  }
  transaction.commit();
}
 
Example #3
Source File: DatastoreSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of adding multiple entities. */
// [TARGET add(FullEntity...)]
// [VARIABLE "my_key_name1"]
// [VARIABLE "my_key_name2"]
public void batchAddEntities(String keyName1, String keyName2) {
  // [START batchAddEntities]
  Key key1 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName1);
  Entity.Builder entityBuilder1 = Entity.newBuilder(key1);
  entityBuilder1.set("propertyName", "value1");
  Entity entity1 = entityBuilder1.build();

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

  try {
    datastore.add(entity1, entity2);
  } catch (DatastoreException ex) {
    if ("ALREADY_EXISTS".equals(ex.getReason())) {
      // at least one of entity1.getKey() and entity2.getKey() already exists
    }
  }
  // [END batchAddEntities]
}
 
Example #4
Source File: DatastoreSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of adding a single entity. */
// [TARGET add(FullEntity)]
// [VARIABLE "my_key_name"]
public void addSingleEntity(String keyName) {
  // [START addSingleEntity]
  Key key = datastore.newKeyFactory().setKind("MyKind").newKey(keyName);
  Entity.Builder entityBuilder = Entity.newBuilder(key);
  entityBuilder.set("propertyName", "value");
  Entity entity = entityBuilder.build();
  try {
    datastore.add(entity);
  } catch (DatastoreException ex) {
    if ("ALREADY_EXISTS".equals(ex.getReason())) {
      // entity.getKey() already exists
    }
  }
  // [END addSingleEntity]
}
 
Example #5
Source File: DatastoreTransactionManager.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@Override
protected void doCommit(DefaultTransactionStatus defaultTransactionStatus)
		throws TransactionException {
	Tx tx = (Tx) defaultTransactionStatus.getTransaction();
	try {
		if (tx.transaction.isActive()) {
			tx.transaction.commit();
		}
		else {
			this.logger.debug(
					"Transaction was not committed because it is no longer active.");
		}
	}
	catch (DatastoreException ex) {
		throw new TransactionSystemException(
				"Cloud Datastore transaction failed to commit.", ex);
	}
}
 
Example #6
Source File: DatastoreTransactionManager.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@Override
protected void doRollback(DefaultTransactionStatus defaultTransactionStatus)
		throws TransactionException {
	Tx tx = (Tx) defaultTransactionStatus.getTransaction();
	try {
		if (tx.transaction.isActive()) {
			tx.transaction.rollback();
		}
		else {
			this.logger.debug(
					"Transaction was not rolled back because it is no longer active.");
		}
	}
	catch (DatastoreException ex) {
		throw new TransactionSystemException(
				"Cloud Datastore transaction failed to rollback.", ex);
	}
}
 
Example #7
Source File: DatastoreIntegrationTests.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 10000L)
public void testSliceString() {
	try {
		Slice<String> slice = this.testEntityRepository.getSliceStringBySize(2L, PageRequest.of(0, 3));

		List<String> testEntityProjections = slice.get().collect(Collectors.toList());

		assertThat(testEntityProjections).hasSize(1);
		assertThat(testEntityProjections.get(0)).isEqualTo("blue");
	}
	catch (DatastoreException e) {
		if (e.getMessage().contains("no matching index found")) {
			throw new RuntimeException("The required index is not found. " +
					"The index could be created by running this command from 'resources' directory: " +
					"'gcloud datastore indexes create index.yaml'");
		}
		throw e;
	}
}
 
Example #8
Source File: ConceptsTest.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void testTransactionalRetry() {
  List<Key> keys = setUpTransferTests();
  Key fromKey = keys.get(0);
  Key toKey = keys.get(1);
  // [START datastore_transactional_retry]
  int retries = 5;
  while (true) {
    try {
      transferFunds(fromKey, toKey, 10);
      break;
    } catch (DatastoreException e) {
      if (retries == 0) {
        throw e;
      }
      --retries;
    }
  }
  // Retry handling can also be configured and automatically applied using google-cloud-java.
  // [END datastore_transactional_retry]
  assertSuccessfulTransfer(keys.get(0), keys.get(1));
}
 
Example #9
Source File: DefaultDatastoreMetadata.java    From catatumbo with Apache License 2.0 6 votes vote down vote up
@Override
public List<String> getKinds(boolean excludeSystemKinds) {
  try {
    String query = "SELECT __key__ FROM " + ENTITY_KINDS + " ORDER BY __key__";
    GqlQuery.Builder<Key> gqlQueryBuilder = Query.newGqlQueryBuilder(ResultType.KEY, query);
    gqlQueryBuilder.setNamespace(entityManager.getEffectiveNamespace());
    GqlQuery<Key> gqlQuery = gqlQueryBuilder.build();
    QueryResults<Key> results = entityManager.getDatastore().run(gqlQuery);
    List<String> kinds = new ArrayList<>(50);
    while (results.hasNext()) {
      Key key = results.next();
      String kind = key.getName();
      if (excludeSystemKinds && kind.startsWith("__")) {
        continue;
      }
      kinds.add(key.getName());
    }
    return kinds;
  } catch (DatastoreException exp) {
    throw new EntityManagerException(exp);
  }
}
 
Example #10
Source File: DefaultDatastoreMetadata.java    From catatumbo with Apache License 2.0 6 votes vote down vote up
@Override
public List<DatastoreProperty> getProperties(String kind) {
  try {
    Key nativeKey = entityManager.newNativeKeyFactory().setKind(ENTITY_KINDS).newKey(kind);
    DefaultDatastoreKey key = new DefaultDatastoreKey(nativeKey);
    String query = "SELECT * FROM " + ENTITY_PROPERTIES
        + " WHERE __key__ HAS ANCESTOR @1 ORDER BY __key__";
    EntityQueryRequest request = entityManager.createEntityQueryRequest(query);
    request.addPositionalBinding(key);
    QueryResponse<DatastoreProperty> response = entityManager
        .executeEntityQueryRequest(DatastoreProperty.class, request);
    return response.getResults();
  } catch (DatastoreException exp) {
    throw new EntityManagerException(exp);
  }
}
 
Example #11
Source File: PersistentStateManagerTest.java    From styx with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldFailTriggerWFIfAlreadyActive() throws Exception {
  reset(storage);
  when(storage.getLatestStoredCounter(any())).thenReturn(Optional.empty());
  when(transaction.workflow(INSTANCE.workflowId())).thenReturn(Optional.of(WORKFLOW));
  DatastoreException datastoreException = new DatastoreException(1, "", "");
  TransactionException transactionException = spy(new TransactionException(datastoreException));

  when(transactionException.isAlreadyExists()).thenReturn(true);
  doThrow(transactionException).when(transaction).writeActiveState(any(), any());
  when(storage.runInTransactionWithRetries(any())).thenAnswer(a ->
      a.<TransactionFunction>getArgument(0).apply(transaction));

  try {
    stateManager.trigger(INSTANCE, TRIGGER1, PARAMETERS);
    fail();
  } catch (AlreadyInitializedException ignore) {
  }
}
 
Example #12
Source File: DatastoreStorageTest.java    From styx with Apache License 2.0 6 votes vote down vote up
@Test
public void runInTransactionWithRetriesShouldPropagateCommitExceptionIfRollbackFails() throws Exception {

  var transaction = spy(datastore.newTransaction());
  doReturn(transaction).when(datastore).newTransaction();

  when(transactionFunction.apply(any())).thenReturn("");

  var commitException = new DatastoreIOException(new DatastoreException(1, "", ""));
  doThrow(commitException).when(transaction).commit();

  var rollbackException = new DatastoreIOException(new DatastoreException(2, "", ""));
  doThrow(rollbackException).when(transaction).rollback();

  try {
    storage.runInTransactionWithRetries(transactionFunction);
    fail("Expected exception!");
  } catch (DatastoreIOException e) {
    assertThat(e, is(commitException));
  }

  verify(transactionFunction).apply(any());
  verify(transaction).rollback();
  verify(logger).debug("Exception on rollback", rollbackException);
}
 
Example #13
Source File: DatastoreStorageTest.java    From styx with Apache License 2.0 6 votes vote down vote up
@Test
public void runInTransactionWithRetriesShouldCallFunctionAndRollbackOnCommitFailure() throws Exception {
  var commitException = new DatastoreIOException(new DatastoreException(1, "", ""));

  var transaction = spy(datastore.newTransaction());
  doReturn(transaction).when(datastore).newTransaction();

  when(transactionFunction.apply(any())).thenReturn("");
  doThrow(commitException).when(transaction).commit();

  try {
    storage.runInTransactionWithRetries(transactionFunction);
    fail("Expected exception!");
  } catch (DatastoreIOException e) {
    assertThat(e, is(commitException));
  }

  verify(transactionFunction).apply(any());
  verify(transaction).rollback();
}
 
Example #14
Source File: DatastoreStorageTest.java    From styx with Apache License 2.0 6 votes vote down vote up
@Test
public void runInTransactionWithRetriesShouldCallFunctionAndRollbackOnPreCommitConflict() throws Exception {
  var expectedException = new DatastoreIOException(new DatastoreException(1, "", ""));
  when(transactionFunction.apply(any())).thenThrow(expectedException);

  var transaction = spy(datastore.newTransaction());
  when(datastore.newTransaction()).thenReturn(transaction);

  try {
    storage.runInTransactionWithRetries(transactionFunction);
    fail("Expected exception!");
  } catch (DatastoreIOException e) {
    assertThat(e, is(expectedException));
  }

  verify(transactionFunction).apply(any());
  verify(transaction, never()).commit();
  verify(transaction).rollback();
}
 
Example #15
Source File: DefaultDatastoreWriter.java    From catatumbo with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
  }
}
 
Example #16
Source File: DefaultDatastoreWriter.java    From catatumbo with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
  }

}
 
Example #17
Source File: DefaultDatastoreWriter.java    From catatumbo with Apache License 2.0 6 votes vote down vote up
/**
 * Updates the given list of entities in the Cloud Datastore.
 * 
 * @param entities
 *          the entities to update. The passed in entities must have their ID set for the update
 *          to work.
 * @return the updated entities
 * @throws EntityManagerException
 *           if any error occurs while inserting.
 */
@SuppressWarnings("unchecked")
public <E> List<E> update(List<E> entities) {
  if (entities == null || entities.isEmpty()) {
    return new ArrayList<>();
  }
  try {
    Class<E> entityClass = (Class<E>) entities.get(0).getClass();
    entityManager.executeEntityListeners(CallbackType.PRE_UPDATE, entities);
    Intent intent = (nativeWriter instanceof Batch) ? Intent.BATCH_UPDATE : Intent.UPDATE;
    Entity[] nativeEntities = toNativeEntities(entities, entityManager, intent);
    nativeWriter.update(nativeEntities);
    List<E> updatedEntities = toEntities(entityClass, nativeEntities);
    entityManager.executeEntityListeners(CallbackType.POST_UPDATE, updatedEntities);
    return updatedEntities;
  } catch (DatastoreException exp) {
    throw DatastoreUtils.wrap(exp);
  }
}
 
Example #18
Source File: DatastoreStorageTest.java    From styx with Apache License 2.0 6 votes vote down vote up
@Test
public void runInTransactionWithRetriesShouldHandleRollbackFailure() throws Exception {
  var transaction = spy(datastore.newTransaction());
  doReturn(transaction).when(datastore).newTransaction();
  var rollbackFailure = new DatastoreIOException(new DatastoreException(1, "fail", "error"));
  doThrow(rollbackFailure).when(transaction).rollback();

  var cause = new RuntimeException("foobar");

  // Run a failing transaction and verify that the rollback failure does not suppress our exception
  try {
    storage.runInTransactionWithRetries(tx -> {
      throw cause;
    });
  } catch (RuntimeException e) {
    assertThat(e, is(cause));
  }

  verify(transaction).rollback();
}
 
Example #19
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 #20
Source File: TransactionalWriter.java    From catatumbo with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected <E> E updateWithOptimisticLockingInternal(E entity, PropertyMetadata versionMetadata) {
  try {
    entityManager.executeEntityListeners(CallbackType.PRE_UPDATE, entity);
    Entity nativeEntity = (Entity) Marshaller.marshal(entityManager, entity, Intent.UPDATE);
    Entity storedNativeEntity = nativeTransaction.get(nativeEntity.getKey());
    if (storedNativeEntity == null) {
      throw new OptimisticLockException(
          String.format("Entity does not exist: %s", nativeEntity.getKey()));
    }
    String versionPropertyName = versionMetadata.getMappedName();
    long version = nativeEntity.getLong(versionPropertyName) - 1;
    long storedVersion = storedNativeEntity.getLong(versionPropertyName);
    if (version != storedVersion) {
      throw new OptimisticLockException(
          String.format("Expecting version %d, but found %d", version, storedVersion));
    }
    nativeTransaction.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);
  }
}
 
Example #21
Source File: PersistentStateManagerTest.java    From styx with Apache License 2.0 5 votes vote down vote up
@Test
public void triggerShouldLogTransactionFailure() throws Exception {
  final TransactionException cause = new TransactionException(
      new DatastoreException(new IOException("netsplit!")));
  reset(storage);
  when(storage.getLatestStoredCounter(INSTANCE)).thenReturn(Optional.empty());
  when(storage.runInTransactionWithRetries(any())).thenThrow(cause);
  try {
    stateManager.trigger(INSTANCE, Trigger.natural(), PARAMETERS);
    fail();
  } catch (Exception ignore) {
  }
  verify(logger).debug("Transaction failure when triggering workflow instance: {}: {}",
      INSTANCE, cause.getMessage(), cause);
}
 
Example #22
Source File: CheckedDatastore.java    From styx with Apache License 2.0 5 votes vote down vote up
/**
 * @see Datastore#newTransaction()
 * @throws DatastoreIOException if the underlying client throws {@link DatastoreException}
 */
CheckedDatastoreTransaction newTransaction() throws DatastoreIOException {
  try {
    return new CheckedDatastoreTransaction(this, datastore.newTransaction());
  } catch (DatastoreException e) {
    throw new DatastoreIOException(e);
  }
}
 
Example #23
Source File: DefaultEntityManager.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Override
public long deleteAll(String kind) {
  if (Utility.isNullOrEmpty(kind)) {
    throw new IllegalArgumentException("kind cannot be null or blank");
  }
  String query = "SELECT __key__ FROM " + kind;
  try {
    GqlQuery<Key> gqlQuery = Query.newGqlQueryBuilder(Query.ResultType.KEY, query)
        .setNamespace(getEffectiveNamespace()).build();
    QueryResults<Key> keys = datastore.run(gqlQuery);
    Key[] nativeKeys = new Key[DEFAULT_DELETE_ALL_BATCH_SIZE];
    long deleteCount = 0;
    int i = 0;
    while (keys.hasNext()) {
      nativeKeys[i++] = keys.next();
      if (i % DEFAULT_DELETE_ALL_BATCH_SIZE == 0) {
        datastore.delete(nativeKeys);
        deleteCount += DEFAULT_DELETE_ALL_BATCH_SIZE;
        i = 0;
      }
    }
    if (i > 0) {
      datastore.delete(Arrays.copyOfRange(nativeKeys, 0, i));
      deleteCount += i;
    }
    return deleteCount;
  } catch (DatastoreException exp) {
    throw new EntityManagerException(exp);
  }
}
 
Example #24
Source File: DatastoreUtils.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
/**
 * Wraps the given DatastoreException into an {@link EntityManagerException} or a subclass of
 * {@link EntityManagerException}.
 * 
 * @param exp
 *          the DatastoreException
 * @return An {@link EntityManagerException} or a subclass of {@link EntityManagerException}.
 */
static EntityManagerException wrap(DatastoreException exp) {
  switch (exp.getCode()) {
    case ERROR_CODE_ENTITY_NOT_FOUND:
      return new EntityNotFoundException(exp);
    case ERROR_CODE_ENTITY_ALREADY_EXISTS:
      return new EntityAlreadyExistsException(exp);
    default:
      return new EntityManagerException(exp);

  }
}
 
Example #25
Source File: DefaultDatastoreReader.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
/**
 * Executes the given {@link KeyQueryRequest} and returns the response.
 * 
 * @param request
 *          the key query request
 * @return the query response
 */
public QueryResponse<DatastoreKey> executeKeyQueryRequest(KeyQueryRequest request) {
  try {
    GqlQuery.Builder<Key> queryBuilder = Query.newGqlQueryBuilder(ResultType.KEY,
        request.getQuery());
    queryBuilder.setNamespace(entityManager.getEffectiveNamespace());
    queryBuilder.setAllowLiteral(request.isAllowLiterals());
    QueryUtils.applyNamedBindings(queryBuilder, request.getNamedBindings());
    QueryUtils.applyPositionalBindings(queryBuilder, request.getPositionalBindings());
    GqlQuery<Key> gqlQuery = queryBuilder.build();
    QueryResults<Key> results = nativeReader.run(gqlQuery);
    List<DatastoreKey> entities = new ArrayList<>();
    DefaultQueryResponse<DatastoreKey> response = new DefaultQueryResponse<>();
    response.setStartCursor(new DefaultDatastoreCursor(results.getCursorAfter().toUrlSafe()));
    while (results.hasNext()) {
      Key result = results.next();
      DatastoreKey datastoreKey = new DefaultDatastoreKey(result);
      entities.add(datastoreKey);
    }
    response.setResults(entities);
    response.setEndCursor(new DefaultDatastoreCursor(results.getCursorAfter().toUrlSafe()));
    response.setQueryResponseMetadata(
        new DefaultQueryResponseMetadata(
            QueryResponseMetadata.QueryState.forMoreResultsType(results.getMoreResults())));
    return response;
  } catch (DatastoreException exp) {
    throw new EntityManagerException(exp);
  }
}
 
Example #26
Source File: DefaultDatastoreReader.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
/**
 * Executes the given {@link ProjectionQueryRequest} and returns the response.
 * 
 * @param expectedResultType
 *          the expected type of results.
 * @param request
 *          the projection query request
 * @return the query response
 */
public <E> QueryResponse<E> executeProjectionQueryRequest(Class<E> expectedResultType,
    ProjectionQueryRequest request) {
  try {
    GqlQuery.Builder<ProjectionEntity> queryBuilder = Query
        .newGqlQueryBuilder(ResultType.PROJECTION_ENTITY, request.getQuery());
    queryBuilder.setNamespace(entityManager.getEffectiveNamespace());
    queryBuilder.setAllowLiteral(request.isAllowLiterals());
    QueryUtils.applyNamedBindings(queryBuilder, request.getNamedBindings());
    QueryUtils.applyPositionalBindings(queryBuilder, request.getPositionalBindings());
    GqlQuery<ProjectionEntity> gqlQuery = queryBuilder.build();
    QueryResults<ProjectionEntity> results = nativeReader.run(gqlQuery);
    List<E> entities = new ArrayList<>();
    DefaultQueryResponse<E> response = new DefaultQueryResponse<>();
    response.setStartCursor(new DefaultDatastoreCursor(results.getCursorAfter().toUrlSafe()));
    while (results.hasNext()) {
      ProjectionEntity result = results.next();
      E entity = Unmarshaller.unmarshal(result, expectedResultType);
      entities.add(entity);
    }
    response.setResults(entities);
    response.setEndCursor(new DefaultDatastoreCursor(results.getCursorAfter().toUrlSafe()));
    response.setQueryResponseMetadata(
        new DefaultQueryResponseMetadata(
            QueryResponseMetadata.QueryState.forMoreResultsType(results.getMoreResults())));
    // TODO should we invoke PostLoad callback for projected entities?
    return response;
  } catch (DatastoreException exp) {
    throw new EntityManagerException(exp);
  }
}
 
Example #27
Source File: DefaultDatastoreReader.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
/**
 * Executes the given {@link EntityQueryRequest} and returns the response.
 * 
 * @param expectedResultType
 *          the expected type of results.
 * @param request
 *          the entity query request
 * @return the query response
 */
public <E> QueryResponse<E> executeEntityQueryRequest(Class<E> expectedResultType,
    EntityQueryRequest request) {
  try {
    GqlQuery.Builder<Entity> queryBuilder = Query.newGqlQueryBuilder(ResultType.ENTITY,
        request.getQuery());
    queryBuilder.setNamespace(entityManager.getEffectiveNamespace());
    queryBuilder.setAllowLiteral(request.isAllowLiterals());
    QueryUtils.applyNamedBindings(queryBuilder, request.getNamedBindings());
    QueryUtils.applyPositionalBindings(queryBuilder, request.getPositionalBindings());
    GqlQuery<Entity> gqlQuery = queryBuilder.build();
    QueryResults<Entity> results = nativeReader.run(gqlQuery);
    List<E> entities = new ArrayList<>();
    DefaultQueryResponse<E> response = new DefaultQueryResponse<>();
    response.setStartCursor(new DefaultDatastoreCursor(results.getCursorAfter().toUrlSafe()));
    while (results.hasNext()) {
      Entity result = results.next();
      E entity = Unmarshaller.unmarshal(result, expectedResultType);
      entities.add(entity);
    }
    response.setResults(entities);
    response.setEndCursor(new DefaultDatastoreCursor(results.getCursorAfter().toUrlSafe()));
    response.setQueryResponseMetadata(
        new DefaultQueryResponseMetadata(
            QueryResponseMetadata.QueryState.forMoreResultsType(results.getMoreResults())));
    entityManager.executeEntityListeners(CallbackType.POST_LOAD, entities);
    return response;
  } catch (DatastoreException exp) {
    throw new EntityManagerException(exp);
  }
}
 
Example #28
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);
  }
}
 
Example #29
Source File: DefaultDatastoreReader.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
/**
 * Fetches the entity given the native key.
 * 
 * @param entityClass
 *          the expected result type
 * @param nativeKey
 *          the native key
 * @return the entity with the given key, or <code>null</code>, if no entity exists with the given
 *         key.
 */
private <E> E fetch(Class<E> entityClass, Key nativeKey) {
  try {
    Entity nativeEntity = nativeReader.get(nativeKey);
    E entity = Unmarshaller.unmarshal(nativeEntity, entityClass);
    entityManager.executeEntityListeners(CallbackType.POST_LOAD, entity);
    return entity;
  } catch (DatastoreException exp) {
    throw new EntityManagerException(exp);
  }
}
 
Example #30
Source File: ShardedCounterMetricsStore.java    From nexus-blobstore-google-cloud with Eclipse Public License 1.0 5 votes vote down vote up
void test() throws DatastoreException {
  // throw a sentinel in to confirm we can write
  BlobId sentinel = new BlobId("tmp$/sentinel");
  Key key = datastore.newKeyFactory().setKind(METRICS_STORE).newKey(sentinel.asUniqueString());
  Entity entity = Entity.newBuilder(shardRoot).setKey(key).build();
  datastore.put(entity);
  datastore.delete(key);
}