Class VKey<T>
- java.lang.Object
-
- google.registry.model.ImmutableObject
-
- google.registry.persistence.VKey<T>
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Cloneable
public class VKey<T> extends ImmutableObject implements java.io.Serializable
VKey is an abstraction that encapsulates the key concept.A VKey instance must contain both the JPA primary key for the referenced entity class and the objectify key for the object.
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class google.registry.model.ImmutableObject
ImmutableObject.DoNotCompare, ImmutableObject.DoNotHydrate, ImmutableObject.EmptySetToNull, ImmutableObject.IgnoredInDiffableMap, ImmutableObject.Insignificant
-
-
Field Summary
-
Fields inherited from class google.registry.model.ImmutableObject
hashCode
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <T> VKey<T>
create(java.lang.Class<T> kind, long id)
Creates a symmetricVKey
in which both sql and ofy keys areid
.static <T> VKey<T>
create(java.lang.Class<T> kind, java.io.Serializable sqlKey, com.googlecode.objectify.Key<T> ofyKey)
Creates aVKey
which only contains both sql and ofy primary key.static <T> VKey<T>
create(java.lang.Class<T> kind, java.lang.String name)
Creates a symmetricVKey
in which both sql and ofy keys arename
.static <T> VKey<T>
create(java.lang.String keyString)
Constructs aVKey
from the string representation of a vkey.static <T> VKey<T>
createOfy(java.lang.Class<? extends T> kind, com.googlecode.objectify.Key<T> ofyKey)
Creates aVKey
which only contains the ofy primary key.static <T> VKey<T>
createSql(java.lang.Class<T> kind, java.io.Serializable sqlKey)
Creates aVKey
which only contains the sql primary key.static <T> VKey<T>
from(com.googlecode.objectify.Key<T> key)
Convenience method to construct a VKey from an objectify Key.static <T> VKey<T>
fromWebsafeKey(java.lang.String ofyKeyRepr)
Construct a VKey from the string representation of an ofy key.java.lang.Class<? extends T>
getKind()
Returns the type of the entity.com.googlecode.objectify.Key<T>
getOfyKey()
Returns the objectify key.java.io.Serializable
getSqlKey()
Returns the SQL primary key.java.util.Optional<com.googlecode.objectify.Key<T>>
maybeGetOfyKey()
Returns the objectify key if it exists.java.util.Optional<java.lang.Object>
maybeGetSqlKey()
Returns the SQL primary key if it exists.VKey<T>
restoreOfy(java.lang.Object... ancestors)
Returns a clone with an ofy key restored fromancestors
.static <T> VKey<T>
restoreOfyFrom(VKey<T> key, java.lang.Object... ancestors)
Returns a clone ofkey
with an ofy key restored fromancestors
.java.lang.String
stringify()
Constructs the string representation of aVKey
.java.lang.String
toString()
Constructs the readable string representation of aVKey
.-
Methods inherited from class google.registry.model.ImmutableObject
clone, cloneEmptyToNull, equals, getSignificantFields, hashCode, toDiffableFieldMap, toHydratedString, toStringHelper
-
-
-
-
Method Detail
-
createSql
public static <T> VKey<T> createSql(java.lang.Class<T> kind, java.io.Serializable sqlKey)
Creates aVKey
which only contains the sql primary key.Deprecated. Create symmetric keys with create() instead.
-
createOfy
public static <T> VKey<T> createOfy(java.lang.Class<? extends T> kind, com.googlecode.objectify.Key<T> ofyKey)
Creates aVKey
which only contains the ofy primary key.
-
create
public static <T> VKey<T> create(java.lang.Class<T> kind, java.io.Serializable sqlKey, com.googlecode.objectify.Key<T> ofyKey)
Creates aVKey
which only contains both sql and ofy primary key.
-
create
public static <T> VKey<T> create(java.lang.Class<T> kind, long id)
Creates a symmetricVKey
in which both sql and ofy keys areid
.IMPORTANT USAGE NOTE: Datastore entities that are not roots of entity groups (i.e. those that do not have a null parent in their Objectify keys) require the full entity group inheritance chain to be specified and thus cannot use this create method. You need to use
create(Class, Serializable, Key)
instead and pass in the full, valid parent field in the Datastore key.
-
create
public static <T> VKey<T> create(java.lang.Class<T> kind, java.lang.String name)
Creates a symmetricVKey
in which both sql and ofy keys arename
.IMPORTANT USAGE NOTE: Datastore entities that are not roots of entity groups (i.e. those that do not have a null parent in their Objectify keys) require the full entity group inheritance chain to be specified and thus cannot use this create method. You need to use
create(Class, Serializable, Key)
instead and pass in the full, valid parent field in the Datastore key.
-
create
public static <T> VKey<T> create(java.lang.String keyString)
Constructs aVKey
from the string representation of a vkey.There are two types of string representations: 1) existing ofy key string handled by fromWebsafeKey() and 2) string encoded via stringify() where @ separates the substrings and each of the substrings contains a look up key, ":", and its corresponding value. The key info is encoded via Base64. The string begins with "kind:" and it must contains at least ofy key or sql key.
Example of a Vkey string by fromWebsafeKey(): "agR0ZXN0chYLEgpEb21haW5CYXNlIgZST0lELTEM"
Example of a vkey string by stringify(): "kind:TestObject@sql:rO0ABXQAA2Zvbw" + "@ofy:agR0ZXN0cjELEg9FbnRpdHlHcm91cFJvb3QiCWNyb3NzLXRsZAwLEgpUZXN0T2JqZWN0IgNmb28M", where sql key and ofy key values are encoded in Base64.
-
restoreOfy
public VKey<T> restoreOfy(java.lang.Object... ancestors)
Returns a clone with an ofy key restored fromancestors
.The arguments should generally consist of pairs of Class and value, where the Class is the kind of the ancestor key and the value is either a String or a Long.
For example, to restore the objectify key for DomainBase("COM-1234")/HistoryEntry(123)/PollEvent(567), one might use:
pollEvent.restoreOfy(DomainBase.class, "COM-1234", HistoryEntry.class, 567)
The final key id or name is obtained from the SQL key. It is assumed that this value must be either a long integer or a
String
and that this proper identifier for the objectify key.As a special case, an objectify Key may be used as the first ancestor instead of a Class, value pair.
-
restoreOfyFrom
public static <T> VKey<T> restoreOfyFrom(@Nullable VKey<T> key, java.lang.Object... ancestors)
Returns a clone ofkey
with an ofy key restored fromancestors
.This is the static form of the method restoreOfy() above. If
key
is null, it returns null.
-
getKind
public java.lang.Class<? extends T> getKind()
Returns the type of the entity.
-
getSqlKey
public java.io.Serializable getSqlKey()
Returns the SQL primary key.
-
maybeGetSqlKey
public java.util.Optional<java.lang.Object> maybeGetSqlKey()
Returns the SQL primary key if it exists.
-
getOfyKey
public com.googlecode.objectify.Key<T> getOfyKey()
Returns the objectify key.
-
maybeGetOfyKey
public java.util.Optional<com.googlecode.objectify.Key<T>> maybeGetOfyKey()
Returns the objectify key if it exists.
-
from
@Nullable public static <T> VKey<T> from(com.googlecode.objectify.Key<T> key)
Convenience method to construct a VKey from an objectify Key.
-
fromWebsafeKey
@Nullable public static <T> VKey<T> fromWebsafeKey(java.lang.String ofyKeyRepr)
Construct a VKey from the string representation of an ofy key.TODO(b/184350590): After migration, we'll want remove the ofy key dependency from this.
-
stringify
public java.lang.String stringify()
Constructs the string representation of aVKey
.The string representation of a vkey contains its kind, and sql key or ofy key, or both. Each of the keys is first serialized into a byte array then encoded via Base64 into a web safe string.
The string representation of a vkey contains key values pairs separated by delimiter "@". Another delimiter ":" is put in between each key and value. The following is the complete format of the string: "kind:class_name@sql:encoded_sqlKey@ofy:encoded_ofyKey", where kind is required. The string representation may contain an encoded ofy key, or an encoded sql key, or both.
-
toString
public java.lang.String toString()
Constructs the readable string representation of aVKey
.This readable string representation of a vkey contains its kind and its sql key or ofy key, or both.
- Overrides:
toString
in classImmutableObject
-
-