com.appenginefan.toolkit.persistence
Class DatastorePersistence

java.lang.Object
  extended by com.appenginefan.toolkit.persistence.DatastorePersistence
All Implemented Interfaces:
Persistence<byte[]>

public class DatastorePersistence
extends Object
implements Persistence<byte[]>

A datastore-based persistence for byte arrays. Wrap other persistences like StringPersistence or ObjectPersistence around this class to persist arbitrary data types in the store.


Constructor Summary
DatastorePersistence(DatastoreService serviceOrNull, String partition)
          Constructor.
DatastorePersistence(String partition)
          Constructor.
 
Method Summary
 byte[] get(String key)
          Gets an entry from the store.
 List<String> keyScan(String start, String end, int max)
          Finds zero or more entries that are within a given range.
 List<String> keyScanReverse(String start, String end, int max)
          Finds zero or more entries that are within a given range, but orders in the opposite direction.
 byte[] mutate(String key, Function<? super byte[],? extends byte[]> mutator)
          Modifies an entry in the store.
 List<Map.Entry<String,byte[]>> scan(String start, String end, int max)
          Finds zero or more entries that are within a given range
 List<Map.Entry<String,byte[]>> scan(String start, String end, int max, Query.SortDirection direction, boolean keysOnly)
           
 List<Map.Entry<String,byte[]>> scanReverse(String start, String end, int max)
          Finds zero or more entries that are within a given range, but orders in the opposite direction.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DatastorePersistence

public DatastorePersistence(DatastoreService serviceOrNull,
                            String partition)
Constructor.

Parameters:
serviceOrNull - a DatastoreService to use. If left null, the constructor fetch its own service
partition - determines what "partition" to store the data in. Different stores must use different partitions, or unspecified behavior will occur.

DatastorePersistence

public DatastorePersistence(String partition)
Constructor.

Parameters:
partition - determines what "partition" to store the data in. Different stores must use different partitions, or unspecified behavior will occur.
Method Detail

get

public byte[] get(String key)
Description copied from interface: Persistence
Gets an entry from the store.

Specified by:
get in interface Persistence<byte[]>
Parameters:
key - the key to look up the data from
Returns:
the data or null, if the store does not contain data

mutate

public byte[] mutate(String key,
                     Function<? super byte[],? extends byte[]> mutator)
Description copied from interface: Persistence
Modifies an entry in the store.

Specified by:
mutate in interface Persistence<byte[]>
Parameters:
key - the key to modify
mutator - a function that applies a change to the data and stores the new result. If the data does not exist in the store yet, null will be passed in. If the data should be deleted from the store, the function will return null.
Returns:
the data that was stored

scan

public List<Map.Entry<String,byte[]>> scan(String start,
                                           String end,
                                           int max,
                                           Query.SortDirection direction,
                                           boolean keysOnly)

scan

public List<Map.Entry<String,byte[]>> scan(String start,
                                           String end,
                                           int max)
Description copied from interface: Persistence
Finds zero or more entries that are within a given range

Specified by:
scan in interface Persistence<byte[]>
Parameters:
start - a lower bound of the range of keys to look in (inclusive)
end - an upper bound of the range of keys to look in (exclusive)
max - a maximum amount of elements to return. The implementation of the store may choose to return less (for example, if a store can only fetch 10 elements per query, then setting a max of 1000 will still only return 10), but never more.
Returns:
a list of up to max key/value pairs, ordered by key

scanReverse

public List<Map.Entry<String,byte[]>> scanReverse(String start,
                                                  String end,
                                                  int max)
Description copied from interface: Persistence
Finds zero or more entries that are within a given range, but orders in the opposite direction. Useful for pagination to jump to the end of a range and get the "last ten" entries

Specified by:
scanReverse in interface Persistence<byte[]>
Parameters:
start - a lower bound of the range of keys to look in (inclusive)
end - an upper bound of the range of keys to look in (exclusive)
max - a maximum amount of elements to return. The implementation of the store may choose to return less (for example, if a store can only fetch 10 elements per query, then setting a max of 1000 will still only return 10), but never more.
Returns:
a list of up to max key/value pairs, ordered by key in descending order

keyScan

public List<String> keyScan(String start,
                            String end,
                            int max)
Description copied from interface: Persistence
Finds zero or more entries that are within a given range. Only returns the keys.

Specified by:
keyScan in interface Persistence<byte[]>
Parameters:
start - a lower bound of the range of keys to look in (inclusive)
end - an upper bound of the range of keys to look in (exclusive)
max - a maximum amount of elements to return. The implementation of the store may choose to return less (for example, if a store can only fetch 10 elements per query, then setting a max of 1000 will still only return 10), but never more.
Returns:
a list of up to max key/value pairs, ordered by key

keyScanReverse

public List<String> keyScanReverse(String start,
                                   String end,
                                   int max)
Description copied from interface: Persistence
Finds zero or more entries that are within a given range, but orders in the opposite direction. Only returns the keys.

Specified by:
keyScanReverse in interface Persistence<byte[]>
Parameters:
start - a lower bound of the range of keys to look in (inclusive)
end - an upper bound of the range of keys to look in (exclusive)
max - a maximum amount of elements to return. The implementation of the store may choose to return less (for example, if a store can only fetch 10 elements per query, then setting a max of 1000 will still only return 10), but never more.
Returns:
a list of up to max key/value pairs, ordered by key in descending order