2020import com .google .common .collect .Maps ;
2121
2222import java .util .ArrayList ;
23+ import java .util .Arrays ;
24+ import java .util .Collections ;
2325import java .util .Iterator ;
2426import java .util .List ;
2527import java .util .Map ;
@@ -33,13 +35,16 @@ class DatastoreHelper {
3335 private DatastoreHelper () {
3436 }
3537
36-
3738 static Key allocateId (Datastore service , IncompleteKey key ) {
3839 return service .allocateId (new IncompleteKey []{key }).get (0 );
3940 }
4041
41- static Entity get (DatastoreReader reader , Key key ) {
42- return Iterators .getNext (reader .get (new Key []{key }), null );
42+ static Entity get (Transaction reader , Key key ) {
43+ return Iterators .getNext (reader .get (new Key [] {key }), null );
44+ }
45+
46+ static Entity get (Datastore reader , Key key , ReadOption ... options ) {
47+ return Iterators .getNext (reader .get (Collections .singletonList (key ), options ), null );
4348 }
4449
4550 static Entity add (DatastoreWriter writer , FullEntity <?> entity ) {
@@ -51,19 +56,30 @@ static KeyFactory newKeyFactory(DatastoreOptions options) {
5156 }
5257
5358 /**
54- * Returns a list with a value for each given key (ordered by input).
55- * A {@code null} would be returned for non-existing keys.
59+ * Returns a list with a value for each given key (ordered by input). {@code null} values are
60+ * returned for nonexistent keys.
5661 */
57- static List <Entity > fetch (DatastoreReader reader , Key ... keys ) {
58- Iterator <Entity > entities = reader .get (keys );
62+ static List <Entity > fetch (Transaction reader , Key ... keys ) {
63+ return compileEntities (keys , reader .get (keys ));
64+ }
65+
66+ /**
67+ * Returns a list with a value for each given key (ordered by input). {@code null} values are
68+ * returned for nonexistent keys.
69+ */
70+ static List <Entity > fetch (Datastore reader , Key [] keys , ReadOption ... options ) {
71+ return compileEntities (keys , reader .get (Arrays .asList (keys ), options ));
72+ }
73+
74+ private static List <Entity > compileEntities (Key [] keys , Iterator <Entity > entities ) {
5975 Map <Key , Entity > map = Maps .newHashMapWithExpectedSize (keys .length );
6076 while (entities .hasNext ()) {
6177 Entity entity = entities .next ();
6278 map .put (entity .key (), entity );
6379 }
6480 List <Entity > list = new ArrayList <>(keys .length );
6581 for (Key key : keys ) {
66- // this will include nulls for non-existing keys
82+ // this will include nulls for nonexistent keys
6783 list .add (map .get (key ));
6884 }
6985 return list ;
0 commit comments