@Override public Result<Void> delete(Iterable<?> keysOrEntities) { // We have to be careful here, objs could contain raw Keys or Keys or entity objects or both! List<com.google.appengine.api.datastore.Key> keys = new ArrayList<com.google.appengine.api.datastore.Key>(); for (Object obj: keysOrEntities) keys.add(this.factory.getRawKey(obj)); return new ResultAdapter<Void>(this.ads.delete(this.txn, keys)); }
@Override public Future<KeyRange> allocateIds(String kind, long num) { return this.rawAsync.allocateIds(kind, num); }
@Override public Future<Transaction> beginTransaction() { return new TransactionFutureWrapper(this.rawAsync.beginTransaction()); }
final AsyncDatastoreService service = DatastoreServiceFactory.getAsyncDatastoreService(); Transaction tx = waitOnFuture(service.beginTransaction(TransactionOptions.Builder.withXG(true))); Key key, key2; try { key = waitOnFuture(service.put(tx, new Entity("AsyncTx"))); key2 = waitOnFuture(service.put(tx, new Entity("AsyncTx"))); tx.commit(); } catch (Exception e) { tx = waitOnFuture(service.beginTransaction(TransactionOptions.Builder.withXG(true))); try { try { try { Assert.assertNotNull(waitOnFuture(service.get(tx, key))); Assert.assertNotNull(waitOnFuture(service.get(tx, Collections.singleton(key2)))); } finally { service.delete(tx, key2); service.delete(tx, Collections.singleton(key));
@Test public void testCRUD() throws Exception { AsyncDatastoreService service = DatastoreServiceFactory.getAsyncDatastoreService(); Entity e1 = new Entity("ASYNC"); e1.setProperty("foo", "bar"); Future<Key> k1 = service.put(e1); Assert.assertNotNull(k1); Key key = k1.get(); Assert.assertNotNull(key); Future<Entity> fe1 = service.get(key); Assert.assertNotNull(fe1); Assert.assertEquals(e1, fe1.get()); Future<Void> fd1 = service.delete(key); Assert.assertNotNull(fd1); fd1.get(); assertStoreDoesNotContain(key); }
@Test public void testRollbackTx() throws Exception { AsyncDatastoreService service = DatastoreServiceFactory.getAsyncDatastoreService(); Transaction tx = waitOnFuture(service.beginTransaction(TransactionOptions.Builder.withDefaults())); Key key = null; try { Future<Key> fKey = service.put(tx, new Entity("AsyncTx")); key = waitOnFuture(fKey); } finally { waitOnFuture(tx.rollbackAsync()); } if (key != null) { Assert.assertNull(getSingleEntity(service, key)); } } }
/** * Need this for beingTransaction() */ private class TransactionFutureWrapper extends SimpleFutureWrapper<Transaction, Transaction> { TransactionWrapper xact; public TransactionFutureWrapper(Future<Transaction> base) { super(base); } @Override protected Transaction wrap(Transaction t) { if (xact == null) xact = new TransactionWrapper(memcache, t); return xact; } }
@Test public void testMultipleDataGet() throws Exception { Entity parent = createTestEntityWithUniqueMethodNameKey(ASYNC_ENTITY, "testMultipleDataGet"); Key key = parent.getKey(); final int recordCount = 10; List<Entity> lisRec = new ArrayList<Entity>(); for (int i = 0; i < recordCount; i++) { Entity newRec = new Entity(ASYNC_ENTITY, key); newRec.setProperty("count", i); newRec.setProperty("timestamp", new Date()); lisRec.add(newRec); } List<Key> eKeys = service.put(lisRec); Future<Map<Key, Entity>> futureKeyEntity = asyncService.get(eKeys); Map<Key, Entity> es = futureKeyEntity.get(); assertTaskIsDoneAndNotCancelled(futureKeyEntity); assertEquals(recordCount, es.size()); asyncService.delete(eKeys); }
rawKeys.add(obKey.getRaw()); Future<Map<com.google.appengine.api.datastore.Key, Entity>> futureEntities = this.ads.get(this.txn, rawKeys); Future<Map<Key<T>, T>> wrapped = new SimpleFutureWrapper<Map<com.google.appengine.api.datastore.Key, Entity>, Map<Key<T>, T>>(futureEntities) { @Override
AsyncDatastoreService ds = DatastoreServiceFactory.getAsyncDatastoreService(); // create query for your kind Query query = new Query("Kind"); // we don't need anything else than the key query.setKeysOnly(); // set the filter for date before now query.setFilter( new FilterPredicate( "date", FilterOperator.LESS_THAN_OR_EQUAL, new Date())); PreparedQuery pq = ds.prepare(query); // asIterable is more efficient than asList for (Entity en : pq.asIterable()) { // delete each entity ds.delete(en.getKey()); }
@Test public void testMiscOps() throws Exception { AsyncDatastoreService service = DatastoreServiceFactory.getAsyncDatastoreService(); DatastoreAttributes attributes = waitOnFuture(service.getDatastoreAttributes()); Assert.assertNotNull(attributes); Assert.assertNotNull(attributes.getDatastoreType()); Map<Index, Index.IndexState> indexes = waitOnFuture(service.getIndexes()); Assert.assertNotNull(indexes); Transaction tx = waitOnFuture(service.beginTransaction()); try { String txId = tx.getId(); Assert.assertNotNull(txId); Assert.assertEquals(txId, tx.getId()); String appId = tx.getApp(); Assert.assertNotNull(appId); Assert.assertEquals(appId, tx.getApp()); } finally { tx.rollback(); } }
@Override public PreparedQuery prepare(Transaction txn, Query query) { return this.rawAsync.prepare(txn, query); }
@Override public Future<Map<Index, IndexState>> getIndexes() { return this.rawAsync.getIndexes(); } }
@Override public Future<DatastoreAttributes> getDatastoreAttributes() { return this.rawAsync.getDatastoreAttributes(); }
Entity newRec = new Entity(ASYNC_ENTITY); newRec.setProperty("timestamp", new Date()); Transaction trans = asyncService.beginTransaction().get(); Future<Key> firstE = asyncService.put(trans, newRec); trans.rollback(); assertEquals(0, service.prepare(new Query(ASYNC_ENTITY)).countEntities(withDefaults())); lisRec.add(newRec); trans = asyncService.beginTransaction().get(); Future<List<Key>> eKeys = asyncService.put(trans, lisRec); trans.commit(); List<Key> realKey = eKeys.get();
public Future<Key> run(AsyncDatastoreService ads) { Future<Key> k1 = ads.put(e1); Assert.assertNotNull(k1); return k1; } };
protected Entity getSingleEntity(AsyncDatastoreService ds, Key key) { Map<Key, Entity> map = waitOnFuture(ds.get(Collections.singleton(key))); return (map.isEmpty() ? null : map.values().iterator().next()); }
@Override public PreparedQuery prepare(Query query) { this.checkForImplicitTransaction(); return this.rawAsync.prepare(query); }
@Override public Future<Void> delete(final Transaction txn, final Iterable<Key> keys) { // Always trigger, even on failure - the delete might have succeeded even though a timeout // exception was thrown. We will always be safe emptying the key from the cache. Future<Void> future = new TriggerFuture<Void>(this.rawAsync.delete(txn, keys)) { @Override protected void trigger() { if (txn != null) { for (Key key: keys) ((TransactionWrapper)txn).deferEmptyFromCache(key); } else { memcache.empty(keys); } } }; if (txn instanceof TransactionWrapper) ((TransactionWrapper)txn).enlist(future); return future; }
@Test public void testCommitTx() throws Exception { AsyncDatastoreService service = DatastoreServiceFactory.getAsyncDatastoreService(); Transaction tx = waitOnFuture(service.beginTransaction(TransactionOptions.Builder.withDefaults())); Key key; try { Future<Key> fKey = service.put(tx, new Entity("AsyncTx")); key = waitOnFuture(fKey); waitOnFuture(tx.commitAsync()); } catch (Exception e) { waitOnFuture(tx.rollbackAsync()); throw e; } if (key != null) { Assert.assertNotNull(getSingleEntity(service, key)); } }