package com.voodoodyne.gstrap.objectify;

import com.google.common.base.Preconditions;

import static com.googlecode.objectify.ObjectifyService.ofy;

/**
 * Utilities for working with transactions
 */
public class Transactions {

	/** Throw an exception if we aren't in a transaction */
	public static void require() {
		Preconditions.checkState(ofy().getTransaction() != null);
	}
}

@@ -79,16 +79,70 @@
 ditions;
+%0Aimport com.googlecode.objectify.impl.TransactionImpl;
 %0A%0Aimport
@@ -428,11 +428,651 @@
 ull);%0A%09%7D
+%0A%0A%09/**%0A%09 * For any given key, do something once in a given transaction. Useful for ensuring that tasks are only%0A%09 * enqueued once.%0A%09 * @param bagKey an arbitrary unique hashable value%0A%09 * @param runnable some code to execute if this is the first time we've seen the hashable value in the transaction%0A%09 */%0A%09public static void once(final Object bagKey, final Runnable runnable) %7B%0A%09%09final TransactionImpl txn = (TransactionImpl)ofy().getTransaction();%0A%0A%09%09if (txn == null) %7B%0A%09%09%09// Like autocommit mode%0A%09%09%09runnable.run();%0A%09%09%09return;%0A%09%09%7D%0A%0A%09%09final Object before = txn.getBag().put(bagKey, bagKey);%0A%09%09if (before == null) %7B%0A%09%09%09runnable.run();%0A%09%09%7D%0A%09%7D
 %0A%7D%0A

Add Transactions.once()