Class ObjectifyFactory

java.lang.Object
com.googlecode.objectify.ObjectifyFactory
All Implemented Interfaces:
Forge

public class ObjectifyFactory extends Object implements Forge

Factory which allows us to construct implementations of the Objectify interface. You should usually use the ObjectifyService to access Objectify.

ObjectifyFactory is designed to be subclassed; much default behavior can be changed by overriding methods. In particular, see createObjectify(), construct(), getAsyncDatastoreService().

Author:
Jeff Schnitzer invalid input: '<'[email protected]>
  • Field Details

    • MEMCACHE_NAMESPACE

      public static final String MEMCACHE_NAMESPACE
      Default memcache namespace
      See Also:
    • datastore

      protected com.google.cloud.datastore.Datastore datastore
      The raw interface to the datastore from the Cloud SDK
    • memcache

      protected MemcacheService memcache
      The low-level interface to memcache
    • registrar

      protected Registrar registrar
      Encapsulates entity registration info
    • keys

      protected Keys keys
      Some useful tools for working with keys
    • translators

      protected Translators translators
    • memcacheStats

      protected EntityMemcacheStats memcacheStats
    • entityMemcache

      protected EntityMemcache 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 public ObjectifyFactory(net.spy.memcached.MemcachedClient memcache)
      Deprecated.
      call ObjectifyFactory(new SpyMemcacheService(memcache)) instead
      Uses default datastore
    • ObjectifyFactory

      public ObjectifyFactory(MemcacheService memcache)
      Uses default datastore
    • ObjectifyFactory

      @Deprecated public ObjectifyFactory(com.google.cloud.datastore.Datastore datastore, net.spy.memcached.MemcachedClient memcache)
      Deprecated.
      call ObjectifyFactory(datastore, new SpyMemcacheService(memcache)) instead
    • ObjectifyFactory

      public ObjectifyFactory(com.google.cloud.datastore.Datastore datastore, MemcacheService memcache)
  • Method Details

    • datastore

      public com.google.cloud.datastore.Datastore datastore()
    • memcache

      public MemcacheService memcache()
    • asyncDatastore

      public AsyncDatastore asyncDatastore()
      Always the non-caching version
    • asyncDatastore

      public AsyncDatastore asyncDatastore(boolean enableGlobalCache)
      Might produce a caching version if caching is enabled.
    • construct

      public <T> T construct(Class<T> type)

      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.

      Specified by:
      construct in interface Forge
    • constructCollection

      public <T extends Collection<?>> T constructCollection(Class<T> type, int size)

      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

      public <T extends Map<?, ?>> T constructMap(Class<T> type)

      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.

    • begin

      @Deprecated public Objectify begin()
      Deprecated.
      This method is a holdover from the 1.x days and will be removed soon. Clients should use ObjectifyService.ofy() to obtain Objectify instances.
      This is the beginning of any Objectify session. It creates an Objectify instance with the default options, unless you override this method to alter the options. You can also override this method to produce a wholly different Objectify implementation (possibly using ObjectifyWrapper).

      The default options are:

      • Do NOT begin a transaction.
      • DO use a global cache.
      • Apply no deadline to calls.

      Note that when using Objectify you will almost never directly call this method. Instead you should call the static ofy() method on ObjectifyService.

      Returns:
      a new Objectify instance
    • register

      public <T> void register(Class<T> clazz)

      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.

    • getMemcacheStats

      public EntityMemcacheStats getMemcacheStats()
      Get the object that tracks memcache stats.
    • getMetadata

      public <T> EntityMetadata<T> getMetadata(Class<T> clazz) throws IllegalArgumentException
      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

      public <T> EntityMetadata<T> getMetadata(Key<T> 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

      public <T> EntityMetadata<T> getMetadata(String kind)
      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

      public <T> EntityMetadata<T> getMetadataForEntity(T obj) throws IllegalArgumentException
      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
    • allocateId

      public <T> Key<T> allocateId(Class<T> clazz)
      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

      public <T> Key<T> allocateId(Object parentKeyOrEntity, Class<T> clazz)
      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

      public <T> KeyRange<T> allocateIds(Class<T> clazz, int num)

      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 as List<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

      public <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. 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
    • getTranslators

      public Translators 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
    • keys

      public Keys keys()
      Some tools for working with keys. This is an internal Objectify API and subject to change without notice. You probably want the Key.create() methods instead.
    • ofy

      public Objectify ofy()
      The method to call at any time to get the current Objectify, which may change depending on txn context. Normally you should use the static ObjectifyService.ofy() which calls this method.
    • open

      public ObjectifyImpl open()

      Start a scope of work. This is the outermost scope of work, typically created by the ObjectifyFilter or by one of the methods on ObjectifyService. You need one of these to do anything at all.

    • open

      public ObjectifyImpl open(ObjectifyOptions opts, Transactor transactor)
      This is only public because it is used from the impl package; don't use this as a public API
    • close

      public void close(Objectify ofy)
      Pops context off of stack after a transaction completes. For internal housekeeping only.