public class ObjectifyImpl extends Object implements Objectify, Closeable
Implementation of the Objectify interface. This is also suitable for subclassing; you can return your own subclass by overriding ObjectifyFactory.begin().
Note we *always* use the AsyncDatastoreService methods that use transactions to avoid the confusion of implicit transactions.
Modifier and Type | Field and Description |
---|---|
protected ObjectifyFactory |
factory
The factory that produced us
|
protected ObjectifyOptions |
options |
protected com.googlecode.objectify.impl.Transactor |
transactor |
Constructor and Description |
---|
ObjectifyImpl(ObjectifyFactory fact) |
ObjectifyImpl(ObjectifyFactory factory,
ObjectifyOptions options,
TransactorSupplier supplier) |
Modifier and Type | Method and Description |
---|---|
protected AsyncDatastoreReaderWriter |
asyncDatastore() |
Objectify |
cache(boolean value)
Provides a new Objectify instance which uses (or doesn't use) a 2nd-level memcache.
|
void |
clear()
Clears the session; all subsequent requests (or Ref>.get() calls) will go to the datastore/memcache
to repopulate the session.
|
void |
close()
Ends this transactional scope.
|
protected WriteEngine |
createWriteEngine()
Use this once for one operation and then throw it away
|
Objectify |
deadline(Double value)
Provides a new Objectify instance with a limit, in seconds, for datastore calls.
|
Deferred |
defer()
Start a deferred command chain, which lets you make multiple save or delete calls on a single
entity without incurring multiple datastore operations.
|
Deleter |
delete()
Start a delete command chain.
|
void |
execute(TxnType txnType,
Runnable work)
Exactly the same behavior as the Work version, but doesn't return anything.
|
<R> R |
execute(TxnType txnType,
Work<R> work)
Executes the work with the transactional behavior defined by the parameter txnType.
|
ObjectifyFactory |
factory()
Obtain the ObjectifyFactory from which this Objectify instance was created.
|
void |
flush()
Synchronously flushes any deferred operations to the datastore.
|
protected Session |
getSession() |
AsyncTransaction |
getTransaction()
This used to have meaning in the old GAE SDK but no longer does.
|
boolean |
isLoaded(Key<?> key) |
Loader |
load()
Start a load command chain.
|
protected com.google.cloud.datastore.Value<?> |
makeFilterable(Object value)
Translates the value of a filter clause into something the datastore understands.
|
protected ObjectifyImpl |
makeNew(ObjectifyOptions opts,
TransactorSupplier supplier)
Can be overriden if you want to subclass the ObjectifyImpl
|
Objectify |
mandatoryTransactions(boolean value)
Provides a new Objectify instance which throws an exception whenever save() or delete() is
called from outside a transaction context.
|
Saver |
save()
Start a save command chain.
|
void |
transact(Runnable work)
Exactly the same behavior as the Work version, but doesn't return anything.
|
<R> R |
transact(Work<R> work)
Executes work in a transaction.
|
Objectify |
transactionless()
Deprecated.
|
void |
transactionless(Runnable work)
Exactly the same behavior as the Work version, but doesn't return anything.
|
<R> R |
transactionless(Work<R> work)
Executes work outside of a transaction.
|
void |
transactNew(int limitTries,
Runnable work)
Exactly the same behavior as the Work version, but doesn't return anything.
|
<R> R |
transactNew(int limitTries,
Work<R> work)
Executes the work in a new transaction, repeating up to limitTries times when a ConcurrentModificationException
is thrown.
|
void |
transactNew(Runnable work)
Exactly the same behavior as the Work version, but doesn't return anything.
|
<R> R |
transactNew(Work<R> work)
Executes work in a new transaction.
|
protected final ObjectifyFactory factory
protected final ObjectifyOptions options
protected final com.googlecode.objectify.impl.Transactor transactor
public ObjectifyImpl(ObjectifyFactory fact)
public ObjectifyImpl(ObjectifyFactory factory, ObjectifyOptions options, TransactorSupplier supplier)
public ObjectifyFactory factory()
Objectify
public Loader load()
Objectify
Start a load command chain. This is where you begin for any request that fetches data from the datastore: gets and queries.
A quick example:
Map<Key<Thing>, Thing> things = ofy().load().type(Thing.class).parent(par).ids(123L, 456L);
All command objects are immutable; this method returns a new object rather than modifying the current command object.
public Saver save()
Objectify
Start a save command chain. Allows you to save (or re-save) entity objects. Note that all command chain objects are immutable.
Saves do NOT cascade; if you wish to save an object graph, you must save each individual entity.
A quick example:
ofy().save().entities(e1, e2, e3).now();
All command objects are immutable; this method returns a new object rather than modifying the current command object.
public Deleter delete()
Objectify
Start a delete command chain. Lets you delete entities or keys.
Deletes do NOT cascade; if you wish to delete an object graph, you must delete each individual entity.
A quick example:
ofy().delete().entities(e1, e2, e3).now();
All command objects are immutable; this method returns a new object rather than modifying the current command object.
public Deferred defer()
Objectify
Start a deferred command chain, which lets you make multiple save or delete calls on a single entity without incurring multiple datastore operations. Deferred operations are executed at the end of a unit-of-work (transaction, or http request if not in a transaction).
Deferred operations are reflected in the session cache immediately. However query operations may not reflect these changes. For example, newly indexed entities may not show up, even with an otherwise strongly consistent ancestor query. This should not be surprising since the actual save operation has not occurred yet.
In the case of deferred save() and delete() operations on the same entity, the last one wins.
public Objectify deadline(Double value)
Objectify
Provides a new Objectify instance with a limit, in seconds, for datastore calls. If datastore calls take longer than this amount, a timeout exception will be thrown.
The new instance will inherit all other characteristics (transaction, cache policy, session cache contents, etc) from this instance.
All command objects are immutable; this method returns a new object rather than modifying the current command object.
public Objectify cache(boolean value)
Objectify
Provides a new Objectify instance which uses (or doesn't use) a 2nd-level memcache. If true, Objectify will obey the @Cache annotation on entity classes, saving entity data to the GAE memcache service. Fetches from the datastore for @Cache entities will look in the memcache service first. This cache is shared across all versions of your application across the entire GAE cluster.
Objectify instances are cache(true) by default.
All command objects are immutable; this method returns a new object rather than modifying the current command object.
public Objectify mandatoryTransactions(boolean value)
Objectify
Provides a new Objectify instance which throws an exception whenever save() or delete() is called from outside a transaction context. This is a reasonable sanity check for most business workloads; you may wish to enable it globally by overriding ObjectifyFactory.begin() to twiddle this flag on the returned object.
Objectify instances are mandatoryTransactions(false) by default.
All command objects are immutable; this method returns a new object rather than modifying the current command object.
mandatoryTransactions
in interface Objectify
protected ObjectifyImpl makeNew(ObjectifyOptions opts, TransactorSupplier supplier)
@Deprecated public Objectify transactionless()
transactionless
in interface Objectify
public AsyncTransaction getTransaction()
Objectify
This used to have meaning in the old GAE SDK but no longer does. Right now this is pretty much only useful as a null test to see if you are currently in a transaction. This method will probably be removed.
getTransaction
in interface Objectify
public <R> R execute(TxnType txnType, Work<R> work)
Objectify
Executes the work with the transactional behavior defined by the parameter txnType. This is very similar to EJB semantics. The work can inherit a transaction, create a new transaction, prevent transactions, etc.
This method principally exists to facilitate implementation of AOP interceptors that provide EJB-like behavior.
Usually you will call transact()
or transactNew()
when writing code.
Note that ConcurrentModificationExceptions will cause the transaction to repeat as many times as necessary to finish the job. Work MUST idempotent.
Within Work.run()
, obtain the correct Objectify
instance by calling ObjectifyService.ofy()
public void execute(TxnType txnType, Runnable work)
Objectify
Exactly the same behavior as the Work version, but doesn't return anything. Convenient for Java8 so you don't have to return something from the lambda.
public <R> R transactionless(Work<R> work)
Objectify
Executes work outside of a transaction. This is a way to "escape" from a transaction and perform datastore operations that would otherwise not be allowed (or perhaps to load data without hitting entity group limits). If there is not already a transaction running, the work is executed normally. If there is not already a transaction context, a new transaction will be started.
For example, to return an entity fetched outside of a transaction:
Thing th = ofy().transactionless(() -> ofy().load().key(thingKey).now())
transactionless
in interface Objectify
work
- defines the work to be done outside of a transactionpublic void transactionless(Runnable work)
Objectify
Exactly the same behavior as the Work version, but doesn't return anything. Convenient for Java8 so you don't have to return something from the lambda.
transactionless
in interface Objectify
public <R> R transact(Work<R> work)
Objectify
Executes work in a transaction. If there is already a transaction context, that context will be inherited. If there is not already a transaction context, a new transaction will be started.
Within Work.run()
, obtain the correct transactional Objectify
instance by calling
ObjectifyService.ofy()
ConcurrentModificationExceptions will cause the transaction to repeat as many times as necessary to finish the job. Work MUST idempotent.
transact
in interface Objectify
work
- defines the work to be done in a transaction. If this method started a new transaction, it
will be committed when work is complete. If transactional context was inherited, no commit is issued
until the full transaction completes normally.public void transact(Runnable work)
Objectify
Exactly the same behavior as the Work version, but doesn't return anything. Convenient for Java8 so you don't have to return something from the lambda.
public <R> R transactNew(Work<R> work)
Objectify
Executes work in a new transaction. Note that this is equivalent to transactNew(Integer.MAX_VALUE, work);
ConcurrentModificationExceptions will cause the transaction to repeat as many times as necessary to finish the job. Work MUST idempotent.
Within Work.run()
, obtain the new transactional Objectify
instance by calling ObjectifyService.ofy()
transactNew
in interface Objectify
work
- defines the work to be done in a transaction. After the method exits, the transaction will commit.public void transactNew(Runnable work)
Objectify
Exactly the same behavior as the Work version, but doesn't return anything. Convenient for Java8 so you don't have to return something from the lambda.
transactNew
in interface Objectify
public <R> R transactNew(int limitTries, Work<R> work)
Objectify
Executes the work in a new transaction, repeating up to limitTries times when a ConcurrentModificationException is thrown. This requires your Work to be idempotent; otherwise limit tries to 1.
Within Work.run()
, obtain the new transactional Objectify
instance by calling ObjectifyService.ofy()
transactNew
in interface Objectify
limitTries
- is the max # of tries. Must be > 0. A value of 1 means "try only once".work
- defines the work to be done in a transaction. After the method exits, the transaction will commit.public void transactNew(int limitTries, Runnable work)
Objectify
Exactly the same behavior as the Work version, but doesn't return anything. Convenient for Java8 so you don't have to return something from the lambda.
transactNew
in interface Objectify
public void clear()
Objectify
Clears the session; all subsequent requests (or Ref>.get() calls) will go to the datastore/memcache to repopulate the session. This should rarely, if ever be necessary. Note that if you iterate query results you should only perform this action on chunk boundaries, otherwise performance will suffer. This is a "use only if you really know what you are doing" feature.
protected AsyncDatastoreReaderWriter asyncDatastore()
protected WriteEngine createWriteEngine()
protected com.google.cloud.datastore.Value<?> makeFilterable(Object value)
Translates the value of a filter clause into something the datastore understands. Key> goes to native Key, entities go to native Key, java.sql.Date goes to java.util.Date, etc. It uses the same translation system that is used for standard entity fields, but does no checking to see if the value is appropriate for the field.
Unrecognized types are returned as-is.
A future version of this method might check for type validity.
protected Session getSession()
public boolean isLoaded(Key<?> key)
public void flush()
Objectify
public void close()
close
in interface Closeable
close
in interface AutoCloseable
Copyright © 2018. All rights reserved.