Class ObjectifyFactory
- All Implemented Interfaces:
Forge
ObjectifyFactory encapsulates a connection to a single datastore, and allows the datastore to be queries and manipulated.
For most applications which connect to a single datastore, you should use the
ObjectifyService class to initialize the ObjectifyFactory and make ofy()
calls.
If your application connects to multiple datastores, you can skip the ObjectifyService
and manage multiple ObjectifyFactory instances yourself.
Unlike many software libraries with a hard distinction between public and private APIs, Objectify has three layers. Public methods are robust and only change on major version numbers. However, there is quite a lot of internal behavior exposed, especially if you subclass the ObjectifyFactory. This "middle ground" is available to you, though we can't promise it won't change.
- Author:
- Jeff Schnitzer invalid input: '<'[email protected]>
-
Field Summary
Modifier and TypeFieldDescriptionprotected com.google.cloud.datastore.Datastore
The raw interface to the datastore from the Cloud SDKprotected EntityMemcache
Manages caching of entities; might be null to indicate "no cache"protected Keys
Some useful tools for working with keysprotected MemcacheService
The low-level interface to memcachestatic final String
Default memcache namespaceprotected EntityMemcacheStats
protected Registrar
Encapsulates entity registration infoprotected Translators
-
Constructor Summary
ConstructorDescriptionUses default datastore, no memcacheObjectifyFactory
(com.google.cloud.datastore.Datastore datastore) No memcacheObjectifyFactory
(com.google.cloud.datastore.Datastore datastore, MemcacheService memcache) ObjectifyFactory
(com.google.cloud.datastore.Datastore datastore, net.spy.memcached.MemcachedClient memcache) Deprecated.ObjectifyFactory
(MemcacheService memcache) Uses default datastoreObjectifyFactory
(net.spy.memcached.MemcachedClient memcache) Deprecated.callObjectifyFactory(new SpyMemcacheService(memcache))
instead -
Method Summary
Modifier and TypeMethodDescription<T> Key
<T> allocateId
(Class<T> clazz) Allocates a single id from the allocator for the specified kind.<T> Key
<T> allocateId
(Object parentKeyOrEntity, Class<T> clazz) Allocates a single id from the allocator for the specified kind.<T> KeyRange
<T> allocateIds
(Class<T> clazz, int num) Preallocate multiple unique ids within the namespace of the specified entity class.<T> KeyRange
<T> allocateIds
(Object parentKeyOrEntity, Class<T> clazz, int num) Preallocate a contiguous range of unique ids within the namespace of the specified entity class and the parent key.Always the non-caching versionasyncDatastore
(boolean enableGlobalCache) Might produce a caching version if caching is enabled.begin()
An alternative to run() which is somewhat easier to use with testing (ie, @Before and @After) frameworks.void
This is for internal housekeeping and is not part of the public API<T> T
Construct an instance of the specified type.<T extends Collection<?>>
TconstructCollection
(Class<T> type, int size) Construct a collection of the specified type and the specified size for use on a POJO field.<T extends Map<?,
?>>
TconstructMap
(Class<T> type) Construct a map of the specified type for use on a POJO field.com.google.cloud.datastore.Datastore
Get the object that tracks memcache stats.<T> EntityMetadata
<T> getMetadata
(com.google.cloud.datastore.Key key) <T> EntityMetadata
<T> getMetadata
(Key<T> key) <T> EntityMetadata
<T> getMetadata
(Class<T> clazz) <T> EntityMetadata
<T> getMetadata
(String kind) Gets metadata for the specified kind, returning null if nothing registered.<T> EntityMetadata
<T> getMetadataForEntity
(T obj) Named differently so you don't accidentally use the Object formGets the master list of all registered TranslatorFactory objects.<T> Key
<T> key
(com.google.cloud.datastore.Key raw) Create an Objectify key from the native datastore key<T> Key
<T> Create an Objectify key from a parent, type, and numeric id<T> Key
<T> Create an Objectify key from a parent, type, and string id<T> Key
<T> Create an Objectify key from a type and numeric id<T> Key
<T> Create an Objectify key from a type and string id<T> Key
<T> Create an Objectify key from a namespace, type, and numeric id<T> Key
<T> Create an Objectify key from a namespace, type, and string id<T> Key
<T> key
(T pojo) Create a key from a registered POJO entity.keys()
Some tools for working with keys.memcache()
ofy()
The method to call at any time to get the current Objectify, which may change depending on txn context.open
(ObjectifyOptions opts, Transactor transactor) This is for internal housekeeping and is not part of the public API<T> Ref
<T> Create a Ref from an existing key<T> Ref
<T> ref
(T value) Creates a Ref from a registered pojo entity<T> void
All POJO entity classes which are to be managed by Objectify must be registered first.<R> R
Runs one unit of work, making the root Objectify context available and performing all necessary housekeeping.void
Exactly the same behavior as the method that takes aWork<R>
, but doesn't force you to return something from your lambda.
-
Field Details
-
MEMCACHE_NAMESPACE
Default memcache namespace- See Also:
-
datastore
protected com.google.cloud.datastore.Datastore datastoreThe raw interface to the datastore from the Cloud SDK -
memcache
The low-level interface to memcache -
registrar
Encapsulates entity registration info -
keys
Some useful tools for working with keys -
translators
-
memcacheStats
-
entityMemcache
Manages caching of entities; might be null to indicate "no cache"
-
-
Constructor Details
-
ObjectifyFactory
public ObjectifyFactory()Uses default datastore, no memcache -
ObjectifyFactory
public ObjectifyFactory(com.google.cloud.datastore.Datastore datastore) No memcache -
ObjectifyFactory
Deprecated.callObjectifyFactory(new SpyMemcacheService(memcache))
insteadUses default datastore -
ObjectifyFactory
Uses default datastore -
ObjectifyFactory
@Deprecated public ObjectifyFactory(com.google.cloud.datastore.Datastore datastore, net.spy.memcached.MemcachedClient memcache) Deprecated.callObjectifyFactory(datastore, new SpyMemcacheService(memcache))
instead -
ObjectifyFactory
-
-
Method Details
-
datastore
public com.google.cloud.datastore.Datastore datastore() -
memcache
-
asyncDatastore
Always the non-caching version -
asyncDatastore
Might produce a caching version if caching is enabled. -
construct
Construct an instance of the specified type. Objectify uses this method whenever possible to create instances of entities, condition classes, or other types; by overriding this method you can substitute Guice or other dependency injection mechanisms. By default it constructs with a simple no-args constructor.
-
constructCollection
Construct a collection of the specified type and the specified size for use on a POJO field. You can override this with Guice or whatnot.
The default is to call construct(Class), with one twist - if a Set, SortedSet, or List interface is presented, Objectify will construct a HashSet, TreeSet, or ArrayList (respectively). If you override this method with dependency injection and you use uninitialized fields of these interface types in your entity pojos, you will need to bind these interfaces to concrete types.
-
constructMap
Construct a map of the specified type for use on a POJO field. You can override this with Guice or whatnot.
The default is to call construct(Class), with one twist - if a Map or SortedMap List interface is presented, Objectify will construct a HashMap or TreeMap (respectively). If you override this method with dependency injection and you use uninitialized fields of these interface types in your entity pojos, you will need to bind these interfaces to concrete types.
-
register
All POJO entity classes which are to be managed by Objectify must be registered first. This method must be called in a single-threaded mode sometime around application initialization.
Any extra translators must be added to the Translators *before* entity classes are registered.
Attempts to re-register entity classes are ignored.
-
getTranslators
Gets the master list of all registered TranslatorFactory objects. By adding Translators, Objectify can process additional field types which are not part of the standard GAE SDK. You must add translators *before* registering entity pojo classes.
- Returns:
- the repository of TranslatorFactory objects, to which you can optionally add translators
-
getMemcacheStats
Get the object that tracks memcache stats. -
allocateId
Allocates a single id from the allocator for the specified kind. Safe to use in concert with the automatic generator. This is just a convenience method for allocateIds().- Parameters:
clazz
- must be a registered entity class with a Long or long id field.- Returns:
- a key with an id that is unique to the kind
-
allocateId
Allocates a single id from the allocator for the specified kind. Safe to use in concert with the automatic generator. This is just a convenience method for allocateIds(). Note that the id is only unique within the parent, not across the entire kind.- Parameters:
parentKeyOrEntity
- must be a legitimate parent for the class type. It need not point to an existent entity, but it must be the correct type for clazz.clazz
- must be a registered entity class with a Long or long id field, and a parent key of the correct type.- Returns:
- a key with a new id unique to the kind and parent
-
allocateIds
Preallocate multiple unique ids within the namespace of the specified entity class. These ids can be used in concert with the normal automatic allocation of ids when save()ing entities with null Long id fields.
The
KeyRange<?>
class is deprecated; when using this method, treat the return value asList<Key<T>>
.- Parameters:
clazz
- must be a registered entity class with a Long or long id field.num
- must be >= 1 and small enough we can fit a set of keys in RAM.
-
allocateIds
Preallocate a contiguous range of unique ids within the namespace of the specified entity class and the parent key. These ids can be used in concert with the normal automatic allocation of ids when put()ing entities with null Long id fields.- Parameters:
parentKeyOrEntity
- must be a legitimate parent for the class type. It need not point to an existent entity, but it must be the correct type for clazz.clazz
- must be a registered entity class with a Long or long id field, and a parent key of the correct type.num
- must be >= 1 and invalid input: '<'= 1 billion
-
run
Runs one unit of work, making the root Objectify context available and performing all necessary housekeeping. Either this method or
begin()
must be called beforeofy()
can be called.Does not start a transaction. If you want a transaction, call
ofy().transact()
.- Returns:
- the result of the work.
-
run
Exactly the same behavior as the method that takes a
Work<R>
, but doesn't force you to return something from your lambda. -
begin
An alternative to run() which is somewhat easier to use with testing (ie, @Before and @After) frameworks. You must close the return value at the end of the request in a finally block.
This method is not typically necessary - in a normal request, the ObjectifyFilter takes care of this housekeeping for you. However, in unit tests or remote API calls it can be useful.
-
ofy
The method to call at any time to get the current Objectify, which may change depending on txn context. This is the start point for queries and data manipulation. -
open
This is for internal housekeeping and is not part of the public API -
close
This is for internal housekeeping and is not part of the public API -
getMetadata
- Returns:
- the metadata for a kind of typed object
- Throws:
IllegalArgumentException
- if the kind has not been registered
-
getMetadata
public <T> EntityMetadata<T> getMetadata(com.google.cloud.datastore.Key key) throws IllegalArgumentException - Returns:
- the metadata for a kind of entity based on its key
- Throws:
IllegalArgumentException
- if the kind has not been registered
-
getMetadata
- Returns:
- the metadata for a kind of entity based on its key
- Throws:
IllegalArgumentException
- if the kind has not been registered
-
getMetadata
Gets metadata for the specified kind, returning null if nothing registered. This method is not like the others because it returns null instead of throwing an exception if the kind is not found.- Returns:
- null if the kind is not registered.
-
getMetadataForEntity
Named differently so you don't accidentally use the Object form- Returns:
- the metadata for a kind of typed object.
- Throws:
IllegalArgumentException
- if the kind has not been registered
-
keys
Some tools for working with keys. This is an internal Objectify API and subject to change without notice. You probably want the key() methods instead. -
key
Create an Objectify key from the native datastore key -
key
Create an Objectify key from a type and numeric id -
key
Create an Objectify key from a type and string id -
key
Create an Objectify key from a parent, type, and numeric id -
key
Create an Objectify key from a parent, type, and string id -
key
Create an Objectify key from a namespace, type, and numeric id -
key
Create an Objectify key from a namespace, type, and string id -
key
Create a key from a registered POJO entity. -
ref
Create a Ref from an existing key -
ref
Creates a Ref from a registered pojo entity
-
ObjectifyFactory(datastore, new SpyMemcacheService(memcache))
instead