com.googlecode.objectify.VoidWork Java Examples

The following examples show how to use com.googlecode.objectify.VoidWork. 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: OfyHelper.java    From webauthndemo with Apache License 2.0 6 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent event) {
  ObjectifyService.run(new VoidWork() {
    @Override
    public void vrun() {
      ObjectifyService.register(User.class);
      ObjectifyService.register(Credential.class);
      ObjectifyService.register(AttestationSessionData.class);
      ObjectifyService.register(AuthenticatorAttestationResponse.class);
      ObjectifyService.register(AuthenticatorAssertionResponse.class);
      ObjectifyService.register(RsaKey.class);
      ObjectifyService.register(EccKey.class);
      ObjectifyService.register(FidoU2fAttestationStatement.class);
      ObjectifyService.register(PackedAttestationStatement.class);
      ObjectifyService.register(AndroidSafetyNetAttestationStatement.class);
      ObjectifyService.register(NoneAttestationStatement.class);
    }
  });

}
 
Example #2
Source File: ObjectifyBenchmarkTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testInserts() throws Exception {
    final int N = Integer.parseInt(getTestSystemProperty("benchmark.datastore.size", "6000"));
    log.info(String.format(">>>> N = %s", N));

    final Key<Root> parent = getRootKey();

    // wrap inserts in same Tx -- as expected
    long txStart = System.currentTimeMillis();
    ObjectifyService.ofy().transact(new VoidWork() {
        public void vrun() {
            doInsert(generateData(N, parent));
        }
    });
    long txEnd = System.currentTimeMillis();
    logDuration("Full Tx ", txStart, txEnd);

    // do it w/o Tx
    doInsert(generateData(N, parent));
}
 
Example #3
Source File: ShardedCounterServiceShardIncrementInExistingTXTest.java    From appengine-counter with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #4
Source File: ShardedCounterServiceShardIncrementInExistingTXTest.java    From appengine-counter with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #5
Source File: ShardedCounterServiceShardIncrementInExistingTXTest.java    From appengine-counter with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #6
Source File: ShardedCounterServiceShardIncrementInExistingTXTest.java    From appengine-counter with Apache License 2.0 5 votes vote down vote up
/**
 * 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_CounterNotCached()
{
	final String counterName = UUID.randomUUID().toString();
	singleShardShardedCounterService.increment(counterName, 1L);
	assertThat(this.singleShardShardedCounterService.getCounter(counterName).get().getCount(),
		is(BigInteger.valueOf(1L)));

	this.clearAllCaches();

	// 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);

	this.clearAllCaches();

	assertThat(this.singleShardShardedCounterService.getCounter(counterName).get().getCount(),
		is(BigInteger.valueOf(11L)));
	this.assertCounterShardValue(counterName, 11L);
}
 
Example #7
Source File: ShardedCounterServiceShardIncrementInExistingTXTest.java    From appengine-counter with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #8
Source File: ShardedCounterServiceShardIncrementInExistingTXTest.java    From appengine-counter with Apache License 2.0 5 votes vote down vote up
/**
 * 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 increment_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()
			{
				// Do something else as part of the TX.
				final Key<CounterShardData> counterShardDataKey = CounterShardData.key(
					CounterData.key(UUID.randomUUID().toString()), 0);
				ObjectifyService.ofy().save().entity(counterShardDataKey);

				// The actual test.
				singleShardShardedCounterService.increment(counterName, 10L);

				throw new RuntimeException("Abort the Transaction!");
			}
		});
		fail();
	}
	catch (Exception e)
	{
		// Eat the Exception.
	}

	assertThat(this.singleShardShardedCounterService.getCounter(counterName).isPresent(), is(false));
	this.assertCounterShardValue(counterName, null);
}
 
Example #9
Source File: ShardedCounterServiceShardIncrementInExistingTXTest.java    From appengine-counter with Apache License 2.0 5 votes vote down vote up
/**
 * Test an increment where there is an active transaction, but the counter does not exist (ExistingTx: T;
 * ExistingCounter: T), and then the transaction aborts after the call to increment.
 */
@Test
public void increment_Abort_TransactionActive_ExistingCounter_CounterCached()
{
	final String counterName = UUID.randomUUID().toString();
	singleShardShardedCounterService.increment(counterName, 1L);
	assertThat(this.singleShardShardedCounterService.getCounter(counterName).get().getCount(), is(BigInteger.ONE));
	this.assertCounterShardValue(counterName, 1L);

	try
	{
		// 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);
				ObjectifyService.ofy().save().entity(counterShardDataKey);

				// The actual test.
				singleShardShardedCounterService.increment(counterName, 10L);

				throw new RuntimeException("Abort the Transaction!");
			}
		});
		fail();
	}
	catch (Exception e)
	{
		// Eat the Exception.
	}

	assertThat(this.singleShardShardedCounterService.getCounter(counterName).get().getCount(),
		is(BigInteger.valueOf(1L)));
	this.assertCounterShardValue(counterName, 1L);
}
 
Example #10
Source File: ShardedCounterServiceShardIncrementInExistingTXTest.java    From appengine-counter with Apache License 2.0 5 votes vote down vote up
/**
 * 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_CounterNotCached()
{
	final String counterName = UUID.randomUUID().toString();
	singleShardShardedCounterService.increment(counterName, 10L);
	assertThat(this.singleShardShardedCounterService.getCounter(counterName).get().getCount(), is(BigInteger.TEN));
	this.assertCounterShardValue(counterName, 10L);

	// clear the cache.
	memcache.clearAll();

	// Perform another increment in a Work, but abort it before it can commit.
	ObjectifyService.ofy().transactNew(1, new VoidWork()
	{
		@Override
		public void vrun()
		{
			// The actual test.
			CounterOperation amountDecrement = singleShardShardedCounterService.decrement(counterName, 1L);
			assertThat(amountDecrement.getAppliedAmount(), is(1L));
		}
	});

	assertThat(this.singleShardShardedCounterService.getCounter(counterName).get().getCount(),
		is(BigInteger.valueOf(9L)));
	this.assertCounterShardValue(counterName, 9L);

	memcache.clearAll();

	assertThat(this.singleShardShardedCounterService.getCounter(counterName).get().getCount(),
		is(BigInteger.valueOf(9L)));
	this.assertCounterShardValue(counterName, 9L);
}
 
Example #11
Source File: ShardedCounterServiceShardIncrementInExistingTXTest.java    From appengine-counter with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #12
Source File: ShardedCounterServiceShardIncrementInExistingTXTest.java    From appengine-counter with Apache License 2.0 5 votes vote down vote up
/**
 * Test an increment where there is an active transaction, but the counter does not exist (ExistingTx: T;
 * ExistingCounter: T), and then the transaction aborts after the call to increment.
 */
@Test
public void decrement_Abort_TransactionActive_ExistingCounter_CounterCached()
{
	final String counterName = UUID.randomUUID().toString();
	singleShardShardedCounterService.increment(counterName, 1L);
	assertThat(this.singleShardShardedCounterService.getCounter(counterName).get().getCount(), is(BigInteger.ONE));
	this.assertCounterShardValue(counterName, 1L);

	try
	{
		// 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);
				ObjectifyService.ofy().save().entity(counterShardDataKey);

				// The actual test.
				CounterOperation decrementAmount = singleShardShardedCounterService.decrement(counterName, 10L);
				assertThat(decrementAmount.getAppliedAmount(), is(1L));
				throw new RuntimeException("Abort the Transaction!");
			}
		});
	}
	catch (Exception e)
	{
		// Eat the Exception.
	}

	assertThat(this.singleShardShardedCounterService.getCounter(counterName).get().getCount(), is(BigInteger.ONE));
	this.assertCounterShardValue(counterName, 1L);
}
 
Example #13
Source File: ShardedCounterServiceImpl.java    From appengine-counter with Apache License 2.0 4 votes vote down vote up
@Override
public void delete(final String counterName)
{
	Preconditions.checkNotNull(counterName);
	Preconditions.checkArgument(!StringUtils.isBlank(counterName));

	// Delete the main counter in a new TX...
	ObjectifyService.ofy().transactNew(new VoidWork()
	{
		@Override
		public void vrun()
		{
			// Load in a TX so that two threads don't mark the counter as deleted at the same time.
			final Key<CounterData> counterDataKey = CounterData.key(counterName);
			final CounterData counterData = ObjectifyService.ofy().load().key(counterDataKey).now();
			if (counterData == null)
			{
				// We throw an exception here so that callers can be aware that the deletion failed. In the
				// task-queue, however, no exception is thrown since failing to delete something that's not there
				// can be treated the same as succeeding at deleting something that's there.
				throw new NoCounterExistsException(counterName);
			}

			Queue queue;
			if (config.getDeleteCounterShardQueueName() == null)
			{
				queue = QueueFactory.getDefaultQueue();
			}
			else
			{
				queue = QueueFactory.getQueue(config.getDeleteCounterShardQueueName());
			}

			// The TaskQueue will delete the counter once all shards are deleted.
			counterData.setCounterStatus(CounterData.CounterStatus.DELETING);
			// Call this Async so that the rest of the thread can
			// continue. Everything will block till commit is called.
			ObjectifyService.ofy().save().entity(counterData);

			// Transactionally enqueue this task to the path specified in the constructor (if this is null, then the
			// default queue will be used).
			TaskOptions taskOptions = TaskOptions.Builder.withParam(COUNTER_NAME, counterName);
			if (config.getRelativeUrlPathForDeleteTaskQueue() != null)
			{
				taskOptions = taskOptions.url(config.getRelativeUrlPathForDeleteTaskQueue());
			}

			// Kick off a Task to delete the Shards for this CounterData and the CounterData itself, but only if the
			// overall TX commit succeeds
			queue.add(taskOptions);
		}
	});

}
 
Example #14
Source File: ShardedCounterServiceShardIncrementInExistingTXTest.java    From appengine-counter with Apache License 2.0 4 votes vote down vote up
/**
 * Test an increment where there is an active transaction, but the counter does not exist (ExistingTx: T;
 * ExistingCounter: T), and then the transaction aborts after the call to increment.
 */
@Test
public void increment_Abort_TransactionActive_ExistingCounter_CounterNotCached()
{
	final String counterName = UUID.randomUUID().toString();
	singleShardShardedCounterService.increment(counterName, 1L);
	this.clearAllCaches();
	assertThat(this.singleShardShardedCounterService.getCounter(counterName).get().getCount(),
		is(BigInteger.valueOf(1L)));
	this.assertCounterShardValue(counterName, 1L);

	this.clearAllCaches();

	try
	{
		// 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);
				ObjectifyService.ofy().save().entity(counterShardDataKey);

				// The actual test.
				singleShardShardedCounterService.increment(counterName, 10L);

				throw new RuntimeException("Abort the Transaction!");
			}
		});
		fail();
	}
	catch (Exception e)
	{
		// Eat the Exception.
	}

	this.clearAllCaches();

	assertThat(this.singleShardShardedCounterService.getCounter(counterName).get().getCount(),
		is(BigInteger.valueOf(1L)));
	this.assertCounterShardValue(counterName, 1L);

	assertThat(this.singleShardShardedCounterService.getCounter(counterName).get().getCount(),
		is(BigInteger.valueOf(1L)));
	this.assertCounterShardValue(counterName, 1L);
}
 
Example #15
Source File: ShardedCounterServiceShardIncrementInExistingTXTest.java    From appengine-counter with Apache License 2.0 4 votes vote down vote up
@Test
public void incrementAndDecrement()
{
	// Make sure the counter exists
	this.singleShardShardedCounterService.getCounter(TEST_COUNTER1);

	// Increment the counter's 1 shard so it has a count of 1.
	this.singleShardShardedCounterService.increment(TEST_COUNTER1, 1);
	assertThat(this.singleShardShardedCounterService.getCounter(TEST_COUNTER1).get().getCount(),
		is(BigInteger.valueOf(1L)));

	final Key<CounterShardData> counterShardDataKey = CounterShardData.key(CounterData.key(TEST_COUNTER1), 0);
	CounterShardData counterShard = ObjectifyService.ofy().load().key(counterShardDataKey).now();
	assertThat(counterShard, is(not(nullValue())));
	assertThat(counterShard.getCount(), is(1L));

	// Perform another increment in a Work, but abort it before it can commit.
	ObjectifyService.ofy().transactNew(new VoidWork()
	{
		@Override
		public void vrun()
		{
			singleShardShardedCounterService.increment(TEST_COUNTER1, 10L);
		}
	});

	// Both increments should have succeeded
	counterShard = ObjectifyService.ofy().load().key(counterShardDataKey).now();
	assertThat(counterShard, is(not(nullValue())));
	assertThat(counterShard.getCount(), is(11L));
	assertThat(this.singleShardShardedCounterService.getCounter(TEST_COUNTER1).get().getCount(),
		is(BigInteger.valueOf(11L)));

	// Perform another increment in a Work, but abort it before it can commit.
	ObjectifyService.ofy().transactNew(new VoidWork()
	{
		@Override
		public void vrun()
		{
			singleShardShardedCounterService.decrement(TEST_COUNTER1, 10L);
		}
	});

	// Both increments should have succeeded
	counterShard = ObjectifyService.ofy().load().key(counterShardDataKey).now();
	assertThat(counterShard, is(not(nullValue())));
	assertThat(counterShard.getCount(), is(1L));
	assertThat(this.singleShardShardedCounterService.getCounter(TEST_COUNTER1).get().getCount(), is(BigInteger.ONE));
}
 
Example #16
Source File: ShardedCounterServiceShardIncrementInExistingTXTest.java    From appengine-counter with Apache License 2.0 4 votes vote down vote up
/**
 * Test an increment where there is an active transaction, but the counter does not exist (ExistingTx: T;
 * ExistingCounter: T), and then the transaction aborts after the call to increment.
 */
@Test
public void decrement_Abort_TransactionActive_ExistingCounter_CounterNotCached()
{
	final String counterName = UUID.randomUUID().toString();
	singleShardShardedCounterService.increment(counterName, 1L);
	this.clearAllCaches();
	assertThat(this.singleShardShardedCounterService.getCounter(counterName).get().getCount(),
		is(BigInteger.valueOf(1L)));
	this.assertCounterShardValue(counterName, 1L);

	this.clearAllCaches();

	try
	{
		// 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);
				ObjectifyService.ofy().save().entity(counterShardDataKey);

				// The actual test.
				final CounterOperation decrementAmount = singleShardShardedCounterService.decrement(counterName,
					10L);
				assertThat(decrementAmount.getAppliedAmount(), is(1L));
				throw new RuntimeException("Abort the Transaction!");
			}
		});
	}
	catch (Exception e)
	{
		// Eat the Exception.
	}

	this.clearAllCaches();

	assertThat(this.singleShardShardedCounterService.getCounter(counterName).get().getCount(),
		is(BigInteger.valueOf(1L)));
	this.assertCounterShardValue(counterName, 1L);

	assertThat(this.singleShardShardedCounterService.getCounter(counterName).get().getCount(),
		is(BigInteger.valueOf(1L)));
	this.assertCounterShardValue(counterName, 1L);
}