/** * Delete the segment specified. * @param name The name of the segment */ protected void deleteSegment(final String name) { ofy().transactNew(4, new Work<Void>() { @Override public Void run() { deleteSegment(ofy(), name); return null; } }); } /**
/** * Delete this directory. * @throws IOException If an error occurs */ public void delete() throws IOException { ofy().transactNew(3, new Work<Void>() { @Override public Void run() { Objectify objectify = ofy(); for(String name : listAll()) deleteSegment(objectify, name); objectify.delete().key(indexKey); objectify.delete().entities(((GaeLockFactory)lockFactory).getLocks(GaeDirectory.this)); return null; } }); } /**
ObjectifyService.ofy().transactNew(new VoidWork()
@Override public void close() throws LockReleaseFailedException { try { ofy().transactNew(3, new Work<Void>() { @Override public Void run() { final GaeLock gaeLock = ofy().load().key(Key.create(indexKey, GaeLock.class, lockName)).now(); if (gaeLock != null && gaeLock.locked) { log.debug("Unlocking Lock '{}'.", lockName); gaeLock.locked = false; ofy().save().entity(gaeLock).now(); } else { log.warn("Trying to release a non locked Lock '{}'.", lockName); } return null; } }); } catch (RuntimeException e) { log.error("Error closing lock:{} error:{}", lockName, e.getMessage(), e); throw new LockReleaseFailedException("Error closing lock:" + lockName, e); } finally { closed = true; } } /*
final Integer numCounterShards = ObjectifyService.ofy().transactNew(new Work<Integer>() ObjectifyService.ofy().transactNew(new VoidWork() ObjectifyService.ofy().transactNew(new VoidWork()
boolean obtainedLock; try { obtainedLock = ofy().transactNew(new Work<Boolean>() { @Override public Boolean run() {
public static <T> T getOrCreate(final Key<T> key, final ObjectifyBuilder<T> builder) { final Objectify objectify = ofy(); T t = objectify.load().key(key).now(); if(t == null) { t = objectify.transactNew(4, new Work<T>() { @Override public T run() { T t = ofy().load().key(key).now(); if(t == null) { t = builder.newInstance(key); ofy().save().entity(t).now(); } return t; } }); } return t; }
/** * Test an increment where there is an active parent transaction, but the counter does not exist (ExistingTx: T; * ExistingCounter: F) */ @Test public void increment_TransactionActive_NoExistingCounter_CounterNotCached() { final String counterName = UUID.randomUUID().toString(); // Perform another increment in a Work, but abort it before it can commit. ObjectifyService.ofy().transactNew(new VoidWork() { @Override public void vrun() { // Do something else as part of the TX. final Key<CounterShardData> counterShardDataKey = CounterShardData.key( CounterData.key(UUID.randomUUID().toString()), 0); final CounterShardData counterShardData = new CounterShardData(counterShardDataKey); ObjectifyService.ofy().save().entity(counterShardData); // The actual test. singleShardShardedCounterService.increment(counterName, 10L); } }); assertThat(this.singleShardShardedCounterService.getCounter(counterName).get().getCount(), is(BigInteger.valueOf(10L))); this.assertCounterShardValue(counterName, 10L); }
/** * Test an increment where there is an active transaction, but the counter does not exist (ExistingTx: T; * ExistingCounter: F), and then the transaction aborts after the call to increment. */ @Test public void decrement_Abort_TransactionActive_NoExistingCounter_CounterNotCached() { final String counterName = UUID.randomUUID().toString(); try { // Perform another increment in a Work, but abort it before it can commit. ObjectifyService.ofy().transactNew(new VoidWork() { @Override public void vrun() { // The actual test. CounterOperation decrementAmount = singleShardShardedCounterService.decrement(counterName, 10L); assertThat(decrementAmount.getAppliedAmount(), is(10L)); throw new RuntimeException("Abort the Transaction!"); } }); } catch (Exception e) { // Eat the Exception. } assertThat(this.singleShardShardedCounterService.getCounter(counterName).isPresent(), is(false)); this.assertCounterShardValue(counterName, null); }
/** * Test an increment where there is an active parent transaction, but the counter does not exist (ExistingTx: T; * ExistingCounter: F) */ @Test public void decrement_TransactionActive_NoExistingCounter_CounterNotCached() { final String counterName = UUID.randomUUID().toString(); // Perform another increment in a Work, but abort it before it can commit. ObjectifyService.ofy().transactNew(new VoidWork() { @Override public void vrun() { // Do something else as part of the TX. final Key<CounterShardData> counterShardDataKey = CounterShardData.key( CounterData.key(UUID.randomUUID().toString()), 0); final CounterShardData counterShardData = new CounterShardData(counterShardDataKey); ObjectifyService.ofy().save().entity(counterShardData); // The actual test. singleShardShardedCounterService.increment(counterName, 10L); } }); assertThat(this.singleShardShardedCounterService.getCounter(counterName).get().getCount(), is(BigInteger.TEN)); this.assertCounterShardValue(counterName, 10L); }
ObjectifyService.ofy().transactNew(new VoidWork()
/** * Test an increment where there is an active parent transaction, but the counter does not exist (ExistingTx: T; * ExistingCounter: T) */ @Test public void increment_TransactionActive_ExistingCounter_CounterCached() { final String counterName = UUID.randomUUID().toString(); singleShardShardedCounterService.increment(counterName, 1L); assertThat(this.singleShardShardedCounterService.getCounter(counterName).get().getCount(), is(BigInteger.valueOf(1L))); // Perform another increment in a Work, but abort it before it can commit. ObjectifyService.ofy().transactNew(new VoidWork() { @Override public void vrun() { // Do something else as part of the TX. final Key<CounterShardData> counterShardDataKey = CounterShardData.key( CounterData.key(UUID.randomUUID().toString()), 0); final CounterShardData counterShardData = new CounterShardData(counterShardDataKey); ObjectifyService.ofy().save().entity(counterShardData); // The actual test. singleShardShardedCounterService.increment(counterName, 10L); } }); assertThat(this.singleShardShardedCounterService.getCounter(counterName).get().getCount(), is(BigInteger.valueOf(11L))); this.assertCounterShardValue(counterName, 11L); }
/** * Test an increment where there is an active parent transaction, but the counter does not exist (ExistingTx: T; * ExistingCounter: T) */ @Test public void decrement_TransactionActive_ExistingCounter_CounterCached() { final String counterName = UUID.randomUUID().toString(); singleShardShardedCounterService.increment(counterName, 10L); assertThat(this.singleShardShardedCounterService.getCounter(counterName).get().getCount(), is(BigInteger.TEN)); this.assertCounterShardValue(counterName, 10L); // Perform another increment in a Work, but abort it before it can commit. ObjectifyService.ofy().transactNew(new VoidWork() { @Override public void vrun() { // The actual test. singleShardShardedCounterService.decrement(counterName, 1L); } }); assertThat(this.singleShardShardedCounterService.getCounter(counterName).get().getCount(), is(BigInteger.valueOf(9L))); this.assertCounterShardValue(counterName, 9L); }
ObjectifyService.ofy().transactNew(new VoidWork()
ObjectifyService.ofy().transactNew(1, new VoidWork()
ObjectifyService.ofy().transactNew(new VoidWork()
ObjectifyService.ofy().transactNew(new VoidWork() ObjectifyService.ofy().transactNew(new VoidWork()
ObjectifyService.ofy().transactNew(new VoidWork()
ObjectifyService.ofy().transactNew(new VoidWork()
ObjectifyService.ofy().transactNew(new VoidWork()