20
20
import com .google .common .collect .Maps ;
21
21
22
22
import java .util .ArrayList ;
23
+ import java .util .Arrays ;
24
+ import java .util .Collections ;
23
25
import java .util .Iterator ;
24
26
import java .util .List ;
25
27
import java .util .Map ;
@@ -33,13 +35,16 @@ class DatastoreHelper {
33
35
private DatastoreHelper () {
34
36
}
35
37
36
-
37
38
static Key allocateId (Datastore service , IncompleteKey key ) {
38
39
return service .allocateId (new IncompleteKey []{key }).get (0 );
39
40
}
40
41
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 );
43
48
}
44
49
45
50
static Entity add (DatastoreWriter writer , FullEntity <?> entity ) {
@@ -51,19 +56,30 @@ static KeyFactory newKeyFactory(DatastoreOptions options) {
51
56
}
52
57
53
58
/**
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.
56
61
*/
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 ) {
59
75
Map <Key , Entity > map = Maps .newHashMapWithExpectedSize (keys .length );
60
76
while (entities .hasNext ()) {
61
77
Entity entity = entities .next ();
62
78
map .put (entity .key (), entity );
63
79
}
64
80
List <Entity > list = new ArrayList <>(keys .length );
65
81
for (Key key : keys ) {
66
- // this will include nulls for non-existing keys
82
+ // this will include nulls for nonexistent keys
67
83
list .add (map .get (key ));
68
84
}
69
85
return list ;
0 commit comments