public class ObjectifyWrapper<W extends ObjectifyWrapper<W,F>,F extends ObjectifyFactory> extends Object implements Objectify, Cloneable
Simple wrapper/decorator for an Objectify interface.
Use by subclassing like this: class MyObjectify extends ObjectifyWrapper<MyObjectify, MyFactory>
Be aware that chained settings require the wrapper to be cloned.
Constructor and Description |
---|
ObjectifyWrapper(Objectify ofy)
Wraps the base objectify
|
Modifier and Type | Method and Description |
---|---|
W |
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.
|
protected W |
clone() |
W |
consistency(com.google.appengine.api.datastore.ReadPolicy.Consistency policy)
Provides a new Objectify instance with the specified Consistency.
|
W |
deadline(Double value)
Provides a new Objectify instance with a limit, in seconds, for datastore calls.
|
Deleter |
delete()
Start a delete command chain.
|
<R> R |
execute(TxnType txnType,
Work<R> work)
Executes the work with the transactional behavior defined by the parameter txnType.
|
F |
getFactory()
Obtain the ObjectifyFactory from which this Objectify instance was created.
|
com.google.appengine.api.datastore.Transaction |
getTxn()
Get the underlying transaction object associated with this Objectify instance.
|
boolean |
isLoaded(Key<?> key) |
Loader |
load()
Start a load command chain.
|
Saver |
save()
Start a save command chain.
|
void |
setWrapper(Objectify ofy)
Sets the object instance that should be passed on by the base implementation in subsequent actions; for
example, the Objectify instance that is passed to transact() Work.
|
com.google.appengine.api.datastore.Entity |
toEntity(Object pojo)
Convert a POJO object to a native datastore Entity.
|
<T> T |
toPojo(com.google.appengine.api.datastore.Entity entity)
Convert a native datastore Entity into a typed POJO.
|
<R> R |
transact(Work<R> work)
Executes work in a transaction.
|
W |
transaction()
Creates a new Objectify instance that wraps a transaction.
|
W |
transactionless()
If you are in a transaction, this provides you an objectify instance which is outside of the
current transaction and works with the session prior to the transaction start.
|
<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.
|
<R> R |
transactNew(Work<R> work)
Executes work in a new transaction.
|
public ObjectifyWrapper(Objectify ofy)
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. Note that all command objects are immutable.
A quick example:
Map<Key<Thing>, Thing> things = ofy.load().type(Thing.class).parent(par).ids(123L, 456L);
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();
public Deleter delete()
Objectify
Start a delete command chain. Lets you delete entities or keys. Note that all command chain objects are immutable.
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();
public com.google.appengine.api.datastore.Transaction getTxn()
Objectify
Get the underlying transaction object associated with this Objectify instance. You typically do not need to use this; use transact() instead.
Note that this is *not* the same as DatastoreService.getCurrentTransaction()
,
which uses the Low-Level API's implicit transaction management. Every transactional Objectify
instance is associated with a specific Transaction
object.
public F getFactory()
Objectify
getFactory
in interface Objectify
public W consistency(com.google.appengine.api.datastore.ReadPolicy.Consistency policy)
Objectify
Provides a new Objectify instance with the specified Consistency. Generally speaking, STRONG consistency provides more consistent results more slowly; EVENTUAL consistency produces results quickly but they might be out of date. See the Appengine Docs for more explanation.
The new instance will inherit all other characteristics (transaction, cache policy, session cache contents, etc) from this instance.
consistency
in interface Objectify
policy
- the consistency policy to use. STRONG load()s are more consistent but EVENTUAL load()s
are faster.public W 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.
public W 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.
public W transaction()
Objectify
Creates a new Objectify instance that wraps a transaction. The instance inherits any settings, but the session cache will be empty. Upon successful commit, the contents of the session cache will be loaded back into the main session.
This method exists for low-level manipulation of transactions and does not modify
the transaction stack in ObjectifyService
. It should only be used if you know
exactly what you are doing; for almost all use cases, you should use the transact()
method instead.
transaction
in interface Objectify
public W transactionless()
Objectify
If you are in a transaction, this provides you an objectify instance which is outside of the current transaction and works with the session prior to the transaction start. Inherits any settings (consistency, deadline, etc) from the present Objectify instance.
If you are not in a transaction, this simply returns "this".
This allows code to quickly "escape" a transactional context for the purpose of loading manipulating data without creating or affecting XG transactions.
transactionless
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.
public void setWrapper(Objectify ofy)
Objectify
setWrapper
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 <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 <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
work
- defines the work to be done in a transaction. After the method exits, the transaction will commit.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 com.google.appengine.api.datastore.Entity toEntity(Object pojo)
Objectify
public <T> T toPojo(com.google.appengine.api.datastore.Entity entity)
Objectify
Copyright © 2013. All Rights Reserved.