Java Code Examples for com.google.appengine.api.datastore.DatastoreService#delete()

The following examples show how to use com.google.appengine.api.datastore.DatastoreService#delete() . 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: MyEndpoint.java    From endpoints-codelab-android with GNU General Public License v3.0 6 votes vote down vote up
@ApiMethod(name = "clearTasks")
public void clearTasks() {
    DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
    Transaction txn = datastoreService.beginTransaction();
    try {
        Key taskBeanParentKey = KeyFactory.createKey("TaskBeanParent", "todo.txt");
        Query query = new Query(taskBeanParentKey);
        List<Entity> results = datastoreService.prepare(query).asList(FetchOptions.Builder.withDefaults());
        for (Entity result : results) {
            datastoreService.delete(result.getKey());
        }
        txn.commit();
    } finally {
        if (txn.isActive()) {
            txn.rollback();
        }
    }
}
 
Example 2
Source File: PersistingTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void putStoresEntity() throws Exception {
    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
    Entity client = new Entity("Client");
    client.setProperty("username", "alesj");
    client.setProperty("password", "password");
    final Key key = ds.put(client);
    try {
        Query query = new Query("Client");
        query.setFilter(new Query.FilterPredicate("username", Query.FilterOperator.EQUAL, "alesj"));
        PreparedQuery pq = ds.prepare(query);
        Entity result = pq.asSingleEntity();
        Assert.assertNotNull(result);
        Assert.assertEquals(key, result.getKey());
        Assert.assertEquals("alesj", result.getProperty("username"));
        Assert.assertEquals("password", result.getProperty("password"));
    } finally {
        ds.delete(key);
    }
}
 
Example 3
Source File: TestBase.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
protected static void deleteTempDataInTx(DatastoreService ds, Entity entity, Class<? extends TempData> type) {
    Transaction txn = ds.beginTransaction(TransactionOptions.Builder.withXG(true));
    try {
        TempData data = type.newInstance();
        data.fromProperties(entity.getProperties());
        data.preDelete(ds);
        ds.delete(txn, entity.getKey());
        data.postDelete(ds);
        txn.commit();
    } catch (Exception e) {
        throw new IllegalStateException(e);
    } finally {
        if (txn.isActive()) {
            txn.rollback();
        }
    }
}
 
Example 4
Source File: DemoEntityManagerNoSql.java    From solutions-photo-sharing-demo-java with Apache License 2.0 6 votes vote down vote up
@Override
public T deleteEntity(T demoEntity) {
  Utils.assertTrue(demoEntity != null, "entity cannot be null");
  DemoEntityNoSql entityNoSql = downCastEntity(demoEntity);
  DatastoreService ds = getDatastoreService();
  Transaction txn = ds.beginTransaction();
  try {
    if (checkEntityForDelete(ds, entityNoSql)) {
      ds.delete(entityNoSql.getEntity().getKey());
      txn.commit();
      logger.info("entity deleted.");
      return demoEntity;
    }
  } catch (Exception e) {
    logger.severe("Failed to delete entity from datastore:" + e.getMessage());
  } finally {
    if (txn.isActive()) {
      txn.rollback();
    }
  }
  return null;
}
 
Example 5
Source File: GuestbookTestUtilities.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void cleanDatastore(DatastoreService ds, String book) {
  Query query =
      new Query("Greeting")
          .setAncestor(new KeyFactory.Builder("Guestbook", book).getKey())
          .setKeysOnly();
  PreparedQuery pq = ds.prepare(query);
  List<Entity> entities = pq.asList(FetchOptions.Builder.withDefaults());
  ArrayList<Key> keys = new ArrayList<>(entities.size());

  for (Entity e : entities) {
    keys.add(e.getKey());
  }
  ds.delete(keys);
}
 
Example 6
Source File: RemoteApiSharedTests.java    From appengine-java-vm-runtime with Apache License 2.0 5 votes vote down vote up
@Override
public void run(
    DatastoreService ds, Supplier<Key> keySupplier, Supplier<Entity> entitySupplier) {
  Key key = keySupplier.get();
  Entity entity = new Entity(key);
  entity.setProperty("prop1", 75L);
  ds.put(entity);

  assertGetEquals(ds, key, entity);
  ds.delete(key);
  assertEntityNotFoundException(ds, key);
}
 
Example 7
Source File: NotificationCleanupServlet.java    From io2014-codelabs with Apache License 2.0 5 votes vote down vote up
private void doCleanup() {
  log.log(Level.INFO, "Starting a job to clean up processed notification records");

  DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

  Calendar cutoffTime = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
  cutoffTime.add(Calendar.HOUR, -HOURS_TO_KEEP_RECORDS_OF_PROCESSED_NOTIFICATIONS);

  Query query = new Query(Worker.PROCESSED_NOTIFICATION_TASKS_ENTITY_KIND)
    .setFilter(new FilterPredicate("processedAt", FilterOperator.LESS_THAN, cutoffTime.getTime()))
    .setKeysOnly();

  PreparedQuery preparedQuery = datastore.prepare(query);

  // Delete in batches
  List<Entity> entitiesToBeDeleted = null;
  do {
    entitiesToBeDeleted = preparedQuery.asList(FetchOptions.Builder.withLimit(5));

    List<Key> keys = new ArrayList<Key>();

    for (Entity entity : entitiesToBeDeleted) {
      keys.add(entity.getKey());
    }

    datastore.delete(keys);
  } while (entitiesToBeDeleted.size() > 0);

  log.log(Level.INFO, "Finished a job to clean up processed notification records");
}
 
Example 8
Source File: NotificationCleanupServlet.java    From solutions-mobile-backend-starter-java with Apache License 2.0 5 votes vote down vote up
private void doCleanup() {
  log.log(Level.INFO, "Starting a job to clean up processed notification records");

  DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

  Calendar cutoffTime = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
  cutoffTime.add(Calendar.HOUR, -HOURS_TO_KEEP_RECORDS_OF_PROCESSED_NOTIFICATIONS);

  Query query = new Query(Worker.PROCESSED_NOTIFICATION_TASKS_ENTITY_KIND)
    .setFilter(new FilterPredicate("processedAt", FilterOperator.LESS_THAN, cutoffTime.getTime()))
    .setKeysOnly();

  PreparedQuery preparedQuery = datastore.prepare(query);

  // Delete in batches
  List<Entity> entitiesToBeDeleted = null;
  do {
    entitiesToBeDeleted = preparedQuery.asList(FetchOptions.Builder.withLimit(5));

    List<Key> keys = new ArrayList<Key>();

    for (Entity entity : entitiesToBeDeleted) {
      keys.add(entity.getKey());
    }

    datastore.delete(keys);
  } while (entitiesToBeDeleted.size() > 0);

  log.log(Level.INFO, "Finished a job to clean up processed notification records");
}
 
Example 9
Source File: DatastoreUtil.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
public void purgeTestRunRecords() {
    DatastoreService datastoreService = DatastoreServiceFactory. getDatastoreService();
    FilterPredicate testRunFilter = new FilterPredicate(TEST_RUN_ID, FilterOperator.EQUAL, testRunId);
    Query query = new Query(entityName).setFilter(testRunFilter).setKeysOnly();
    for (Entity readRec : datastoreService.prepare(query).asIterable()) {
        datastoreService.delete(readRec.getKey());
    }
}
 
Example 10
Source File: TxPolicyTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testAutoPolicy() throws Exception {
    DatastoreServiceConfig config = DatastoreServiceConfig.Builder.withImplicitTransactionManagementPolicy(ImplicitTransactionManagementPolicy.AUTO);
    DatastoreService ds = DatastoreServiceFactory.getDatastoreService(config);

    Key k1 = null;
    Transaction tx = ds.beginTransaction();
    try {
        // this one should be part of auto Tx
        k1 = ds.put(new Entity("PutAutoTx"));
    } finally {
        tx.rollback();
    }

    Assert.assertTrue(ds.get(Collections.singleton(k1)).isEmpty());

    k1 = ds.put(new Entity("DeleteAutoTx"));
    try {
        Assert.assertNotNull(ds.get(k1));

        tx = ds.beginTransaction();
        try {
            // this one should be part of auto Tx
            ds.delete(k1);
        } finally {
            tx.rollback();
        }

        Assert.assertNotNull(ds.get(k1));
    } finally {
        ds.delete(k1);
    }
}
 
Example 11
Source File: SimpleXmppTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
private void clearTestData() {
    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
    Query query = new Query("XmppMsg");
    List<Key> msgs = new ArrayList<>();
    for (Entity e : ds.prepare(query).asIterable()) {
        msgs.add(e.getKey());
    }
    ds.delete(msgs);
}
 
Example 12
Source File: NsTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
@WithinNamespace({ "", "Ns1" })
public void testSmoke() throws Exception {
    final DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
    Key key = ds.put(new Entity("NsTest"));
    try {
        Assert.assertNotNull(ds.get(key));
    } finally {
        ds.delete(key);
    }
}
 
Example 13
Source File: NotificationCleanupServlet.java    From solutions-ios-push-notification-sample-backend-java with Apache License 2.0 5 votes vote down vote up
private void doCleanup() {
  log.log(Level.INFO, "Starting a job to clean up processed notification records");
  
  DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

  Calendar cutoffTime = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
  cutoffTime.add(Calendar.HOUR, -HOURS_TO_KEEP_RECORDS_OF_PROCESSED_NOTIFICATIONS);
  
  Query query = new Query(PushNotificationWorker.PROCESSED_NOTIFICATION_TASKS_ENTITY_KIND)
    .setFilter(new FilterPredicate("processedAt", FilterOperator.LESS_THAN, cutoffTime.getTime()))
    .setKeysOnly();
  
  PreparedQuery preparedQuery = datastore.prepare(query);

  // Delete in batches
  List<Entity> entitiesToBeDeleted = null;
  do {
    entitiesToBeDeleted = preparedQuery.asList(FetchOptions.Builder.withLimit(5));
    
    List<Key> keys = new ArrayList<Key>();
    
    for (Entity entity : entitiesToBeDeleted) {
      keys.add(entity.getKey());
    }
  
    datastore.delete(keys);
  } while (entitiesToBeDeleted.size() > 0);

  log.log(Level.INFO, "Finished a job to clean up processed notification records");
}
 
Example 14
Source File: AppEngineCredentialStore.java    From google-oauth-java-client with Apache License 2.0 4 votes vote down vote up
@Override
public void delete(String userId, Credential credential) {
  DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
  Key key = KeyFactory.createKey(KIND, userId);
  datastore.delete(key);
}
 
Example 15
Source File: InvocationData.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
public void preDelete(DatastoreService ds) {
    Key ek = (lastReceivedDocument != null) ? lastReceivedDocument.getKey() : lrd;
    if (ek != null) {
        ds.delete(ek);
    }
}
 
Example 16
Source File: Ping.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
public void preDelete(DatastoreService ds) {
    Key ek = (lastReceivedDocument != null) ? lastReceivedDocument.getKey() : lrd;
    if (ek != null) {
        ds.delete(ek);
    }
}