Skip to content

Commit 229690f

Browse files
committed
---
yaml --- r: 73 b: refs/heads/master c: 01ab2c0 h: refs/heads/master i: 71: 77af580 v: v3
1 parent 20b6f8f commit 229690f

8 files changed

Lines changed: 161 additions & 40 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 2105cb7514508a68a2d9819b721ec8c5bd78029a
2+
refs/heads/master: 01ab2c08244fc60af8995586dba6d2c9b160c884

trunk/src/main/java/com/google/gcloud/datastore/BaseKey.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,13 @@ abstract static class Builder<B extends Builder<B>> {
3232

3333
private static final int MAX_PATH = 100;
3434

35-
public Builder(String dataset, String kind) {
35+
public Builder(String dataset) {
3636
this.dataset = validateDataset(dataset);
3737
ancestors = new LinkedList<>();
38+
}
39+
40+
public Builder(String dataset, String kind) {
41+
this(dataset);
3842
this.kind = validateKind(kind);
3943
}
4044

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package com.google.gcloud.datastore;
2+
3+
import com.google.common.collect.Maps;
4+
5+
import java.util.ArrayList;
6+
import java.util.Iterator;
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
/**
11+
* Adds some functionality to DatastoreService that should
12+
* be provided statically to the interface (Java 8).
13+
*
14+
*/
15+
public class DatastoreHelper implements DatastoreService {
16+
17+
private final DatastoreService delegate;
18+
19+
private DatastoreHelper(DatastoreService delegate) {
20+
this.delegate = delegate;
21+
}
22+
23+
@Override
24+
public Entity get(Key key) {
25+
return delegate.get(key);
26+
}
27+
28+
@Override
29+
public Iterator<Entity> get(Key key, Key... others) {
30+
return delegate.get(key, others);
31+
}
32+
33+
@Override
34+
public <T> QueryResult<T> run(Query<T> query) {
35+
return delegate.run(query);
36+
}
37+
38+
@Override
39+
public DatastoreServiceOptions options() {
40+
return delegate.options();
41+
}
42+
43+
@Override
44+
public Transaction newTransaction(TransactionOption... options) {
45+
return delegate.newTransaction(options);
46+
}
47+
48+
@Override
49+
public BatchWriter newBatchWriter(BatchWriteOption... options) {
50+
return delegate.newBatchWriter(options);
51+
}
52+
53+
@Override
54+
public Key allocateId(PartialKey key) {
55+
return delegate.allocateId(key);
56+
}
57+
58+
@Override
59+
public Iterator<Key> allocateId(PartialKey key, PartialKey... others) {
60+
return delegate.allocateId(key, others);
61+
}
62+
63+
@Override
64+
public void add(Entity... entity) {
65+
delegate.add(entity);
66+
}
67+
68+
@Override
69+
public void update(Entity... entity) {
70+
delegate.update(entity);
71+
}
72+
73+
@Override
74+
public void put(Entity... entity) {
75+
delegate.put(entity);
76+
}
77+
78+
@Override
79+
public void delete(Key... key) {
80+
delegate.delete(key);
81+
}
82+
83+
/**
84+
* Returns a new KeyFactory for this service
85+
*/
86+
public KeyFactory newKeyFactory() {
87+
return new KeyFactory(this);
88+
}
89+
90+
/**
91+
* Returns a list with a value for each given key (ordered by input).
92+
* A {@code null} would be returned for non-existing keys.
93+
*/
94+
public List<Entity> fetch(Key key, Key... others) {
95+
Iterator<Entity> entities = delegate.get(key, others);
96+
Map<Key, Entity> map = Maps.newHashMapWithExpectedSize(1 + others.length);
97+
while (entities.hasNext()) {
98+
Entity entity = entities.next();
99+
map.put(entity.key(), entity);
100+
}
101+
List<Entity> list = new ArrayList<>(1 + others.length);
102+
list.add(map.get(key));
103+
for (Key other : others) {
104+
list.add(map.get(other));
105+
}
106+
return list;
107+
}
108+
109+
public interface RunInTransaction {
110+
void run(DatastoreReaderWriter readerWriter);
111+
}
112+
113+
public void runInTransaction(RunInTransaction runFor, TransactionOption... options) {
114+
Transaction transaction = newTransaction(options);
115+
try {
116+
runFor.run(transaction);
117+
transaction.commit();
118+
} finally {
119+
if (transaction.active()) {
120+
transaction.rollback();
121+
}
122+
}
123+
}
124+
125+
public static DatastoreHelper createFor(DatastoreService datastoreService) {
126+
if (datastoreService instanceof DatastoreHelper) {
127+
return (DatastoreHelper) datastoreService;
128+
}
129+
return new DatastoreHelper(datastoreService);
130+
}
131+
}

trunk/src/main/java/com/google/gcloud/datastore/DatastoreService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ public interface DatastoreService extends DatastoreReaderWriter {
1717
*
1818
* @throws DatastoreServiceExcepiton upon failure
1919
*/
20-
Transaction newTransaction(TransactionOption... transactionOption);
20+
Transaction newTransaction(TransactionOption... options);
2121

2222
/**
2323
* Returns a new Batch writer for processing multiple write operations
2424
* in one request.
2525
*/
26-
BatchWriter newBatchWriter(BatchWriteOption... batchWriteOption);
26+
BatchWriter newBatchWriter(BatchWriteOption... options);
2727

2828
/**
2929
* Allocate a unique id for the given key.

trunk/src/main/java/com/google/gcloud/datastore/KeyFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ public final class KeyFactory extends BaseKey.Builder<KeyFactory> {
1212

1313
private final DatastoreService service;
1414

15-
public KeyFactory(DatastoreService service, String kind) {
16-
super(checkNotNull(service).options().dataset(), kind);
15+
public KeyFactory(DatastoreService service) {
16+
super(checkNotNull(service).options().dataset());
1717
this.service = service;
1818
namespace(service.options().namespace());
1919
}

trunk/src/main/java/com/google/gcloud/datastore/PathElement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public final class PathElement extends Serializable<DatastoreV1.Key.PathElement>
2121
private final transient String name;
2222

2323
private PathElement(String kind, String name, Long id) {
24-
this.kind = checkNotNull(kind);
24+
this.kind = checkNotNull(kind, "kind must not be null");
2525
this.name = name;
2626
this.id = id;
2727
}

trunk/src/main/java/com/google/gcloud/datastore/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* <pre> {@code
66
* DatastoreServiceOptions options = DatastoreServiceOptions.builder().dataset(DATASET).build();
77
* DatastoreService datastore = DatastoreServiceFactory.getDefault(options);
8-
* KeyFactory keyFactory = new KeyFactory(datastore, kind);
8+
* KeyFactory keyFactory = new KeyFactory(datastore).kind(kind);
99
* Key key = keyFactory.newKey(keyName);
1010
* Entity entity = datastore.get(key);
1111
* if (entity == null) {

trunk/src/test/java/com/google/gcloud/datastore/DatastoreServiceTest.java

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import com.google.api.services.datastore.DatastoreV1;
1212
import com.google.api.services.datastore.client.Datastore;
1313
import com.google.api.services.datastore.client.DatastoreException;
14-
import com.google.common.collect.Maps;
1514
import com.google.gcloud.datastore.Query.Type;
1615
import com.google.gcloud.datastore.StructuredQuery.OrderBy;
1716
import com.google.gcloud.datastore.StructuredQuery.Projection;
@@ -21,7 +20,6 @@
2120
import org.junit.Before;
2221
import org.junit.Test;
2322

24-
import java.util.ArrayList;
2523
import java.util.Collections;
2624
import java.util.HashMap;
2725
import java.util.Iterator;
@@ -68,6 +66,7 @@ public class DatastoreServiceTest {
6866

6967
private DatastoreServiceOptions options;
7068
private DatastoreService datastore;
69+
private DatastoreHelper helper;
7170

7271
@Before
7372
public void setUp() {
@@ -83,6 +82,7 @@ public void setUp() {
8382
.host("http://localhost:8080")
8483
.build();
8584
datastore = DatastoreServiceFactory.getDefault(options);
85+
helper = DatastoreHelper.createFor(datastore);
8686
// Prepare data for testing
8787
datastore.delete(KEY1, KEY2, KEY3, KEY4, KEY5);
8888
datastore.add(ENTITY1, ENTITY2);
@@ -105,7 +105,7 @@ public void testNewTransactionCommit() {
105105
transaction.delete(KEY1);
106106
transaction.commit();
107107

108-
List<Entity> list = fetch(KEY1, KEY2, KEY3);
108+
List<Entity> list = helper.fetch(KEY1, KEY2, KEY3);
109109
assertNull(list.get(0));
110110
assertEquals(entity2, list.get(1));
111111
assertEquals(ENTITY3, list.get(2));
@@ -197,7 +197,7 @@ public void testNewTransactionRollback() {
197197

198198
verifyNotUsable(transaction);
199199

200-
List<Entity> list = fetch(KEY1, KEY2, KEY3);
200+
List<Entity> list = helper.fetch(KEY1, KEY2, KEY3);
201201
assertEquals(ENTITY1, list.get(0));
202202
assertEquals(ENTITY2, list.get(1));
203203
assertNull(list.get(2));
@@ -245,7 +245,8 @@ public void testNewBatchWriter() {
245245
batchWriter.add(entity4, entity5);
246246
batchWriter.put(ENTITY3, entity1, entity2);
247247
batchWriter.submit();
248-
Iterator<Entity> entities = fetch(KEY1, KEY2, KEY3, entity4.key(), entity5.key()).iterator();
248+
Iterator<Entity> entities =
249+
helper.fetch(KEY1, KEY2, KEY3, entity4.key(), entity5.key()).iterator();
249250
assertEquals(entity1, entities.next());
250251
assertEquals(entity2, entities.next());
251252
assertEquals(ENTITY3, entities.next());
@@ -265,7 +266,7 @@ public void testNewBatchWriter() {
265266
batchWriter.delete(entity4.key(), entity5.key());
266267
batchWriter.update(ENTITY1, ENTITY2, ENTITY3);
267268
batchWriter.submit();
268-
entities = fetch(KEY1, KEY2, KEY3, entity4.key(), entity5.key()).iterator();
269+
entities = helper.fetch(KEY1, KEY2, KEY3, entity4.key(), entity5.key()).iterator();
269270
assertEquals(ENTITY1, entities.next());
270271
assertEquals(ENTITY2, entities.next());
271272
assertEquals(ENTITY3, entities.next());
@@ -431,7 +432,7 @@ public void testRunStructuredQuery() throws DatastoreException {
431432

432433
@Test
433434
public void testAllocateId() {
434-
KeyFactory keyFactory = new KeyFactory(datastore, KIND1);
435+
KeyFactory keyFactory = helper.newKeyFactory().kind(KIND1);
435436
PartialKey pk1 = keyFactory.newKey();
436437
Key key1 = keyFactory.allocateId();
437438
assertEquals(key1.dataset(), pk1.dataset());
@@ -453,7 +454,7 @@ public void testAllocateId() {
453454

454455
@Test
455456
public void testAllocateIdArray() {
456-
KeyFactory keyFactory = new KeyFactory(datastore, KIND1);
457+
KeyFactory keyFactory = helper.newKeyFactory().kind(KIND1);
457458
PartialKey partialKey1 = keyFactory.newKey();
458459
PartialKey partialKey2 = keyFactory.kind(KIND2).ancestors(PathElement.of(KIND1, 10)).newKey();
459460
Key key3 = keyFactory.newKey("name");
@@ -499,7 +500,7 @@ public void testGet() {
499500
public void testGetArray() {
500501
datastore.put(ENTITY3);
501502
Iterator<Entity> result =
502-
fetch(KEY1, Key.builder(KEY1).name("bla").build(), KEY2, KEY3).iterator();
503+
helper.fetch(KEY1, Key.builder(KEY1).name("bla").build(), KEY2, KEY3).iterator();
503504
assertEquals(ENTITY1, result.next());
504505
assertNull(result.next());
505506
assertEquals(ENTITY2, result.next());
@@ -525,24 +526,9 @@ public void testGetArray() {
525526
// TODO(ozarov): construct a test to verify more results
526527
}
527528

528-
public List<Entity> fetch(Key key, Key... others) {
529-
Iterator<Entity> entities = datastore.get(key, others);
530-
Map<Key, Entity> map = Maps.newHashMapWithExpectedSize(1 + others.length);
531-
while (entities.hasNext()) {
532-
Entity entity = entities.next();
533-
map.put(entity.key(), entity);
534-
}
535-
List<Entity> list = new ArrayList<>(1 + others.length);
536-
list.add(map.get(key));
537-
for (Key other : others) {
538-
list.add(map.get(other));
539-
}
540-
return list;
541-
}
542-
543529
@Test
544530
public void testAdd() {
545-
List<Entity> keys = fetch(ENTITY1.key(), ENTITY3.key());
531+
List<Entity> keys = helper.fetch(ENTITY1.key(), ENTITY3.key());
546532
assertEquals(ENTITY1, keys.get(0));
547533
assertNull(keys.get(1));
548534
assertEquals(2, keys.size());
@@ -559,7 +545,7 @@ public void testAdd() {
559545

560546
@Test
561547
public void testUpdate() {
562-
List<Entity> keys = fetch(ENTITY1.key(), ENTITY3.key());
548+
List<Entity> keys = helper.fetch(ENTITY1.key(), ENTITY3.key());
563549
assertEquals(ENTITY1, keys.get(0));
564550
assertNull(keys.get(1));
565551
assertEquals(2, keys.size());
@@ -580,7 +566,7 @@ public void testUpdate() {
580566

581567
@Test
582568
public void testPut() {
583-
Iterator<Entity> keys = fetch(ENTITY1.key(), ENTITY2.key(), ENTITY3.key()).iterator();
569+
Iterator<Entity> keys = helper.fetch(ENTITY1.key(), ENTITY2.key(), ENTITY3.key()).iterator();
584570
assertEquals(ENTITY1, keys.next());
585571
assertEquals(ENTITY2, keys.next());
586572
assertNull(keys.next());
@@ -589,7 +575,7 @@ public void testPut() {
589575
Entity entity2 = Entity.builder(ENTITY2).clear().set("bla", new NullValue()).build();
590576
assertNotEquals(ENTITY2, entity2);
591577
datastore.put(ENTITY3, ENTITY1, entity2);
592-
keys = fetch(ENTITY1.key(), ENTITY2.key(), ENTITY3.key()).iterator();
578+
keys = helper.fetch(ENTITY1.key(), ENTITY2.key(), ENTITY3.key()).iterator();
593579
assertEquals(ENTITY1, keys.next());
594580
assertEquals(entity2, keys.next());
595581
assertEquals(ENTITY3, keys.next());
@@ -598,13 +584,13 @@ public void testPut() {
598584

599585
@Test
600586
public void testDelete() {
601-
Iterator<Entity> keys = fetch(ENTITY1.key(), ENTITY2.key(), ENTITY3.key()).iterator();
587+
Iterator<Entity> keys = helper.fetch(ENTITY1.key(), ENTITY2.key(), ENTITY3.key()).iterator();
602588
assertEquals(ENTITY1, keys.next());
603589
assertEquals(ENTITY2, keys.next());
604590
assertNull(keys.next());
605591
assertFalse(keys.hasNext());
606592
datastore.delete(ENTITY1.key(), ENTITY2.key(), ENTITY3.key());
607-
keys = fetch(ENTITY1.key(), ENTITY2.key(), ENTITY3.key()).iterator();
593+
keys = helper.fetch(ENTITY1.key(), ENTITY2.key(), ENTITY3.key()).iterator();
608594
assertNull(keys.next());
609595
assertNull(keys.next());
610596
assertNull(keys.next());
@@ -613,10 +599,10 @@ public void testDelete() {
613599

614600
@Test
615601
public void testKeyFactory() {
616-
KeyFactory keyFactory = new KeyFactory(datastore, KIND1);
602+
KeyFactory keyFactory = new KeyFactory(datastore).kind(KIND1);
617603
assertEquals(PARTIAL_KEY1, keyFactory.newKey());
618604
assertEquals(PartialKey.builder(PARTIAL_KEY1).kind(KIND2).build(),
619-
new KeyFactory(datastore, KIND2).newKey());
605+
new KeyFactory(datastore).kind(KIND2).newKey());
620606
assertEquals(KEY1, keyFactory.newKey("name"));
621607
assertEquals(Key.builder(KEY1).id(2).build(), keyFactory.newKey(2));
622608
}

0 commit comments

Comments
 (0)