Class VersionedEntity

  • All Implemented Interfaces:

    public abstract class VersionedEntity
    extends java.lang.Object
    A Datastore Entity's serialized state with timestamp. The intended use case is a multi-stage pipeline where an Entity's Java form is not needed in most stages.

    For a new or updated Entity, its serialized bytes are stored along with its Datastore Key. For a deleted entity, only its Datastore Key is stored, and the entityProtoBytes() field is left unset.

    Storing raw bytes is motivated by two factors. First, since I/O is frequent and the Java objects are rarely needed in our target use case, storing raw bytes is the most efficient approach. More importantly, due to our data model and our customization of ObjectifyService, it is challenging to implement a serializer for Objectify entities that preserves the value of all properties. Without such serializers, Objectify entities cannot be used in a pipeline.

    Objectify entities do not implement Serializable, serialization of such objects is as follows:

    • Convert an Objectify entity to a Datastore Entity: auditedOfy().save().toEntity(..)
    • Entity is serializable, but the more efficient approach is to convert an Entity to a ProtocolBuffer (OnestoreEntity.EntityProto) and then to raw bytes.

    When the first conversion above is applied to an Objectify entity, a property value in the output may differ from the input in two situations:

    Working around the side effects caused by our customization is difficult. Any solution would likely rely on Objectify's stack of context. However, many Objectify invocations in our code base are hardcoded to call the customized version of ObjectifyService, rendering Objectify's stack useless.

    For now, this inability to use Objectify entities in pipelines is mostly a testing problem: we can not perform BEAM pipeline assertions on Objectify entities. InitSqlTestUtils.assertContainsExactlyElementsIn is an example of a workaround.

    Note that java.util.Optional is not serializable, therefore cannot be used as property type in this class.

    See Also:
    Serialized Form
    • Constructor Detail

      • VersionedEntity

        public VersionedEntity()
    • Method Detail

      • commitTimeMills

        public abstract long commitTimeMills()
      • key

        public abstract key()
        The Key of the Entity.
      • getEntity

        public java.util.Optional<> getEntity()
      • isDelete

        public boolean isDelete()
      • from

        public static VersionedEntity from​(long commitTimeMillis,
                                           byte[] entityProtoBytes)