Search in sources :

Example 51 with DatastoreService

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);
}
Also used : Query(com.google.appengine.api.datastore.Query) DatastoreService(com.google.appengine.api.datastore.DatastoreService)

Example 52 with DatastoreService

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);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) DatastoreService(com.google.appengine.api.datastore.DatastoreService)

Example 53 with DatastoreService

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());
    }
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) Filter(com.google.appengine.api.datastore.Query.Filter) DatastoreService(com.google.appengine.api.datastore.DatastoreService)

Example 54 with DatastoreService

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")));
}
Also used : BlobKey(com.google.appengine.api.blobstore.BlobKey) ApiProxy(com.google.apphosting.api.ApiProxy) OutputStream(java.io.OutputStream) DatastoreService(com.google.appengine.api.datastore.DatastoreService) ImagesGetUrlBaseRequest(com.google.appengine.api.images.ImagesServicePb.ImagesGetUrlBaseRequest) Test(org.junit.Test)

Example 55 with DatastoreService

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();
}
Also used : Transaction(com.google.appengine.api.datastore.Transaction) DatastoreService(com.google.appengine.api.datastore.DatastoreService) FilterChain(javax.servlet.FilterChain) Test(org.junit.Test)

Aggregations

DatastoreService (com.google.appengine.api.datastore.DatastoreService)64 Entity (com.google.appengine.api.datastore.Entity)43 Test (org.junit.Test)29 Key (com.google.appengine.api.datastore.Key)25 Query (com.google.appengine.api.datastore.Query)25 Transaction (com.google.appengine.api.datastore.Transaction)15 EntityNotFoundException (com.google.appengine.api.datastore.EntityNotFoundException)6 PreparedQuery (com.google.appengine.api.datastore.PreparedQuery)6 LocalDatastoreService (com.google.appengine.api.datastore.dev.LocalDatastoreService)6 ArrayList (java.util.ArrayList)6 Date (java.util.Date)5 PersistenceManager (javax.jdo.PersistenceManager)5 Filter (com.google.appengine.api.datastore.Query.Filter)4 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)4 FilterPredicate (com.google.appengine.api.datastore.Query.FilterPredicate)3 FilterChain (javax.servlet.FilterChain)3 BlobKey (com.google.appengine.api.blobstore.BlobKey)2 Blob (com.google.appengine.api.datastore.Blob)2 DatastoreServiceConfig (com.google.appengine.api.datastore.DatastoreServiceConfig)2 FetchOptions (com.google.appengine.api.datastore.FetchOptions)2