Class DatastoreHelper


  • public final class DatastoreHelper
    extends Object
    Helper methods for Datastore.
    • Field Detail

      • SCATTER_PROPERTY_NAME

        public static final String SCATTER_PROPERTY_NAME
        The property used in the Datastore to give us a random distribution. *
        See Also:
        Constant Field Values
      • KEY_PROPERTY_NAME

        public static final String KEY_PROPERTY_NAME
        The property used in the Datastore to get the key of the entity. *
        See Also:
        Constant Field Values
      • PROJECT_ID_ENV_VAR

        public static final String PROJECT_ID_ENV_VAR
        Name of the environment variable used to set the project ID.
        See Also:
        Constant Field Values
      • LOCAL_HOST_ENV_VAR

        public static final String LOCAL_HOST_ENV_VAR
        Name of the environment variable used to set the local host.
        See Also:
        Constant Field Values
      • SERVICE_ACCOUNT_ENV_VAR

        public static final String SERVICE_ACCOUNT_ENV_VAR
        Name of the environment variable used to set the service account.
        See Also:
        Constant Field Values
      • PRIVATE_KEY_FILE_ENV_VAR

        public static final String PRIVATE_KEY_FILE_ENV_VAR
        Name of the environment variable used to set the private key file.
        See Also:
        Constant Field Values
    • Method Detail

      • getServiceAccountCredential

        public static com.google.api.client.auth.oauth2.Credential getServiceAccountCredential​(String serviceAccountId,
                                                                                               String privateKeyFile)
                                                                                        throws GeneralSecurityException,
                                                                                               IOException
        Constructs credentials for the given account and key.
        Parameters:
        serviceAccountId - service account ID (typically an e-mail address).
        privateKeyFile - the file name from which to get the private key.
        Returns:
        valid credentials or null
        Throws:
        GeneralSecurityException
        IOException
      • getServiceAccountCredential

        public static com.google.api.client.auth.oauth2.Credential getServiceAccountCredential​(String serviceAccountId,
                                                                                               String privateKeyFile,
                                                                                               Collection<String> serviceAccountScopes)
                                                                                        throws GeneralSecurityException,
                                                                                               IOException
        Constructs credentials for the given account and key file.
        Parameters:
        serviceAccountId - service account ID (typically an e-mail address).
        privateKeyFile - the file name from which to get the private key.
        serviceAccountScopes - Collection of OAuth scopes to use with the the service account flow or null if not.
        Returns:
        valid credentials or null
        Throws:
        GeneralSecurityException
        IOException
      • getServiceAccountCredential

        public static com.google.api.client.auth.oauth2.Credential getServiceAccountCredential​(String serviceAccountId,
                                                                                               PrivateKey privateKey,
                                                                                               Collection<String> serviceAccountScopes)
                                                                                        throws GeneralSecurityException,
                                                                                               IOException
        Constructs credentials for the given account and key.
        Parameters:
        serviceAccountId - service account ID (typically an e-mail address).
        privateKey - the private key for the given account.
        serviceAccountScopes - Collection of OAuth scopes to use with the the service account flow or null if not.
        Returns:
        valid credentials or null
        Throws:
        GeneralSecurityException
        IOException
      • getOptionsFromEnv

        public static DatastoreOptions.Builder getOptionsFromEnv()
                                                          throws GeneralSecurityException,
                                                                 IOException
        Constructs a Datastore from environment variables and/or the Compute Engine metadata server.

        The project ID is determined from, in order of preference:

        • DATASTORE_PROJECT_ID environment variable
        • Compute Engine

        Credentials are taken from, in order of preference:

        1. No credentials (if the DATASTORE_EMULATOR_HOST environment variable is set)
        2. Service Account specified by the DATASTORE_SERVICE_ACCOUNT and DATASTORE_PRIVATE_KEY_FILE environment variables
        3. Google Application Default as described here.
        Throws:
        GeneralSecurityException
        IOException
      • getProjectIdFromComputeEngine

        @Nullable
        public static String getProjectIdFromComputeEngine()
        Gets the project ID from the Compute Engine metadata server. Returns null if the project ID cannot be determined (because, for instance, the code is not running on Compute Engine).
      • getKeyComparator

        public static Comparator<Key> getKeyComparator()
      • makeAncestorFilter

        public static Filter.Builder makeAncestorFilter​(Key ancestor)
        Makes an ancestor filter.
      • makeAndFilter

        public static Filter.Builder makeAndFilter​(Filter... subfilters)
        Make a composite filter from the given sub-filters using AND to combine filters.
      • makeAndFilter

        public static Filter.Builder makeAndFilter​(Iterable<Filter> subfilters)
        Make a composite filter from the given sub-filters using AND to combine filters.
      • makePropertyReference

        public static PropertyReference.Builder makePropertyReference​(String propertyName)
        Make a property reference for use in a query.
      • makeValue

        public static Value.Builder makeValue​(Iterable<Value> values)
        Make an array value containing the specified values.
      • makeValue

        public static Value.Builder makeValue​(Value value1,
                                              Value value2,
                                              Value... rest)
        Make a list value containing the specified values.
      • makeValue

        public static Value.Builder makeValue​(Key key)
        Make a key value.
      • makeValue

        public static Value.Builder makeValue​(long key)
        Make an integer value.
      • makeValue

        public static Value.Builder makeValue​(double value)
        Make a floating point value.
      • makeValue

        public static Value.Builder makeValue​(boolean value)
        Make a boolean value.
      • makeValue

        public static Value.Builder makeValue​(com.google.protobuf.ByteString blob)
        Make a ByteString value.
      • makeValue

        public static Value.Builder makeValue​(Date date)
        Make a timestamp value given a date.
      • makeValue

        public static Value.Builder makeValue​(com.google.type.LatLng value)
        Makes a GeoPoint value.
      • makeValue

        public static Value.Builder makeValue​(com.google.type.LatLng.Builder value)
        Makes a GeoPoint value.
      • makeKey

        public static Key.Builder makeKey​(Object... elements)
        Make a key from the specified path of kind/id-or-name pairs and/or Keys.

        The id-or-name values must be either String, Long, Integer or Short.

        The last id-or-name value may be omitted, in which case an entity without an id is created (for use with automatic id allocation).

        The PartitionIds of all Keys in the path must be equal. The returned Key.Builder will use this PartitionId.

      • getDouble

        public static double getDouble​(Value value)
        Returns:
        the double contained in value
        Throws:
        IllegalArgumentException - if the value does not contain a double.
      • getKey

        public static Key getKey​(Value value)
        Returns:
        the key contained in value
        Throws:
        IllegalArgumentException - if the value does not contain a key.
      • getByteString

        public static com.google.protobuf.ByteString getByteString​(Value value)
        Returns:
        the blob contained in value
        Throws:
        IllegalArgumentException - if the value does not contain a blob.
      • getEntity

        public static Entity getEntity​(Value value)
        Returns:
        the entity contained in value
        Throws:
        IllegalArgumentException - if the value does not contain an entity.
      • getString

        public static String getString​(Value value)
        Returns:
        the string contained in value
        Throws:
        IllegalArgumentException - if the value does not contain a string.
      • getBoolean

        public static boolean getBoolean​(Value value)
        Returns:
        the boolean contained in value
        Throws:
        IllegalArgumentException - if the value does not contain a boolean.
      • getLong

        public static long getLong​(Value value)
        Returns:
        the long contained in value
        Throws:
        IllegalArgumentException - if the value does not contain a long.
      • getTimestamp

        public static long getTimestamp​(Value value)
        Returns:
        the timestamp in microseconds contained in value
        Throws:
        IllegalArgumentException - if the value does not contain a timestamp.
      • toDate

        public static Date toDate​(Value value)
        Convert a timestamp value into a Date clipping off the microseconds.
        Parameters:
        value - a timestamp value to convert
        Returns:
        the resulting Date
        Throws:
        IllegalArgumentException - if the value does not contain a timestamp.
      • makeInsert

        public static Mutation.Builder makeInsert​(Entity entity)
        Parameters:
        entity - the entity to insert
        Returns:
        a mutation that will insert an entity
      • makeUpdate

        public static Mutation.Builder makeUpdate​(Entity entity)
        Parameters:
        entity - the entity to update
        Returns:
        a mutation that will update an entity
      • makeUpsert

        public static Mutation.Builder makeUpsert​(Entity entity)
        Parameters:
        entity - the entity to upsert
        Returns:
        a mutation that will upsert an entity
      • makeDelete

        public static Mutation.Builder makeDelete​(Key key)
        Parameters:
        key - the key of the entity to delete
        Returns:
        a mutation that will delete an entity