use of com.google.appengine.api.datastore.DatastoreService in project appengine-java-standard by GoogleCloudPlatform.
the class MainServlet method performCount.
/**
* Counts the "car" entities in the datastore.
*
* @param w response writer
*/
private static void performCount(PrintWriter w) {
DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
Query query = new Query(CAR_KIND);
int count = datastoreService.prepare(query).countEntities(FetchOptions.Builder.withDefaults());
emitf(w, "Found %d entities", count);
}
use of com.google.appengine.api.datastore.DatastoreService in project appengine-java-standard by GoogleCloudPlatform.
the class MainServlet method performAddEntities.
/**
* Adds the specified number of entities to the datastore.
*
* @param count the number of entities to persist
* @param w response writer
*/
private void performAddEntities(int count, PrintWriter w) {
DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
for (int i = 0; i < count; ++i) {
Entity car = new Entity(CAR_KIND);
car.setProperty(COLOR_PROPERTY, COLORS[random.nextInt(COLORS.length)]);
car.setProperty(BRAND_PROPERTY, BRANDS[random.nextInt(BRANDS.length)]);
datastoreService.put(car);
}
emitf(w, "Added %d entities", count);
}
use of com.google.appengine.api.datastore.DatastoreService in project appengine-java-standard by GoogleCloudPlatform.
the class MainServlet method performQueries.
/**
* Performs the specified number of datastore queries.
*
* @param count the number of queries to perform
* @param w response writer
*/
private void performQueries(int count, PrintWriter w) {
DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
for (int i = 0; i < count; ++i) {
Query query = new Query(CAR_KIND);
Filter filter = null;
if (random.nextBoolean()) {
filter = new Query.FilterPredicate(COLOR_PROPERTY, Query.FilterOperator.EQUAL, COLORS[random.nextInt(COLORS.length)]);
} else {
filter = new Query.FilterPredicate(BRAND_PROPERTY, Query.FilterOperator.EQUAL, BRANDS[random.nextInt(BRANDS.length)]);
}
query.setFilter(filter);
List<Entity> results = datastoreService.prepare(query).asList(FetchOptions.Builder.withLimit(20));
emitf(w, "Retrieved %d entities", results.size());
}
}
use of com.google.appengine.api.datastore.DatastoreService in project appengine-java-standard by GoogleCloudPlatform.
the class LocalImagesServiceTest method testGetUrlBaseOnInvalidImage.
@Test
public void testGetUrlBaseOnInvalidImage() throws Exception {
try (OutputStream out = service.getBlobStorage().storeBlob(new BlobKey("not-an-image"))) {
out.write("This is not an image but a blob".getBytes(UTF_8));
}
ImagesGetUrlBaseRequest getUrlBaseRequest = ImagesGetUrlBaseRequest.newBuilder().setBlobKey("not-an-image").build();
ApiProxy.ApplicationException ex = assertThrows(ApiProxy.ApplicationException.class, () -> service.getUrlBase(status, getUrlBaseRequest));
assertThat(ex.getApplicationError()).isEqualTo(ErrorCode.NOT_IMAGE.getNumber());
// Should be no blob key.
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
assertThrows(EntityNotFoundException.class, () -> datastore.get(KeyFactory.createKey(ImagesReservedKinds.BLOB_SERVING_URL_KIND, "blob-key")));
}
use of com.google.appengine.api.datastore.DatastoreService in project appengine-java-standard by GoogleCloudPlatform.
the class TransactionCleanupFilterTest method testAbandonedTransactions_firstRollBackFails.
@Test
public void testAbandonedTransactions_firstRollBackFails() throws ServletException, IOException {
DatastoreService datastoreMock = mock(DatastoreService.class);
Transaction txn1 = mock(Transaction.class);
when(txn1.getId()).thenReturn("txn1");
// the first txn throws an exception when we rollback
doThrow(new RuntimeException()).when(txn1).rollback();
Transaction txn2 = mock(Transaction.class);
when(txn2.getId()).thenReturn("txn2");
when(datastoreMock.getActiveTransactions()).thenReturn(Lists.newArrayList(txn1, txn2));
MyTransactionCleanupFilter filter = new MyTransactionCleanupFilter(datastoreMock);
filter.init(null);
FilterChain chain = mock(FilterChain.class);
filter.doFilter(null, null, chain);
verify(chain, atLeastOnce()).doFilter(null, null);
verify(txn1, times(1)).rollback();
verify(txn2, atLeastOnce()).rollback();
}
Aggregations