Skip to content

Commit 2303882

Browse files
committed
Add Transaction snippets to Transaction javadoc
1 parent b3971bf commit 2303882

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

google-cloud-datastore/src/main/java/com/google/cloud/datastore/Transaction.java

+64-3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ interface Response {
6363
* to fail if entity was changed by others after it was seen by this transaction) but any
6464
* write changes in this transaction will not be reflected by the returned entity.
6565
*
66+
* <p>Example of getting an entity for a given key.
67+
* <pre> {@code
68+
* String keyName = "my_key_name";
69+
* Key key = datastore.newKeyFactory().kind("MyKind").newKey(keyName);
70+
* Entity entity = transaction.get(key);
71+
* transaction.commit();
72+
* // Do something with the entity
73+
* }</pre>
74+
*
6675
* @throws DatastoreException upon failure or if no longer active
6776
*/
6877
@Override
@@ -74,6 +83,23 @@ interface Response {
7483
* to fail if any of the entities was changed by others after they were seen by this transaction)
7584
* but any write changes in this transaction will not be reflected by the returned entities.
7685
*
86+
* <p>Example of getting entities for several keys.
87+
* <pre> {@code
88+
* String firstKeyName = "my_first_key_name";
89+
* String secondKeyName = "my_second_key_name";
90+
* KeyFactory keyFactory = datastore.newKeyFactory().kind("MyKind");
91+
* Key firstKey = keyFactory.newKey(firstKeyName);
92+
* Key secondKey = keyFactory.newKey(secondKeyName);
93+
* Iterator<Entity> entitiesIterator = transaction.get(firstKey, secondKey);
94+
* List<Entity> entities = Lists.newArrayList();
95+
* while (entitiesIterator.hasNext()) {
96+
* Entity entity = entitiesIterator.next();
97+
* // do something with the entity
98+
* entities.add(entity);
99+
* }
100+
* transaction.commit();
101+
* }</pre>
102+
*
77103
* @throws DatastoreException upon failure or if no longer active
78104
*/
79105
@Override
@@ -85,6 +111,20 @@ interface Response {
85111
* to fail if any of the entities was changed by others after they were seen by this transaction)
86112
* but any write changes in this transaction will not be reflected by the returned entities.
87113
*
114+
* <p>Example of fetching a list of entities for several keys.
115+
* <pre> {@code
116+
* String firstKeyName = "my_first_key_name";
117+
* String secondKeyName = "my_second_key_name";
118+
* KeyFactory keyFactory = datastore.newKeyFactory().kind("MyKind");
119+
* Key firstKey = keyFactory.newKey(firstKeyName);
120+
* Key secondKey = keyFactory.newKey(secondKeyName);
121+
* List<Entity> entities = transaction.fetch(firstKey, secondKey);
122+
* for (Entity entity : entities) {
123+
* // do something with the entity
124+
* }
125+
* transaction.commit();
126+
* }</pre>
127+
*
88128
* @throws DatastoreException upon failure or if no longer active
89129
*/
90130
@Override
@@ -97,6 +137,26 @@ interface Response {
97137
* query was performed) but any write changes in this transaction will not be reflected by
98138
* the result.
99139
*
140+
* <p>Example of running a query to find all entities with an ancestor.
141+
* <pre> {@code
142+
* String parentKeyName = "my_parent_key_name";
143+
* KeyFactory keyFactory = datastore.newKeyFactory().kind("ParentKind");
144+
* Key parentKey = keyFactory.newKey(parentKeyName);
145+
* // Build a query
146+
* Query<Entity> query = Query.entityQueryBuilder()
147+
* .kind("MyKind")
148+
* .filter(PropertyFilter.hasAncestor(parentKey))
149+
* .build();
150+
* QueryResults<Entity> results = transaction.run(query);
151+
* List<Entity> entities = Lists.newArrayList();
152+
* while (results.hasNext()) {
153+
* Entity result = results.next();
154+
* // do something with result
155+
* entities.add(result);
156+
* }
157+
* transaction.commit();
158+
* }</pre>
159+
*
100160
* @throws DatastoreException upon failure or if no longer active
101161
*/
102162
@Override
@@ -108,9 +168,10 @@ interface Response {
108168
* <p>Example of committing a transaction.
109169
* <pre> {@code
110170
* // create an entity
111-
* KeyFactory keyFactory = datastore.newKeyFactory().kind("someKind");
171+
* KeyFactory keyFactory = datastore.newKeyFactory().kind("MyKind");
112172
* Key key = datastore.allocateId(keyFactory.newKey());
113173
* Entity entity = Entity.builder(key).set("description", "commit()").build();
174+
*
114175
* // add the entity and commit
115176
* try {
116177
* transaction.put(entity);
@@ -130,7 +191,7 @@ interface Response {
130191
* <p>Example of rolling back a transaction.
131192
* <pre> {@code
132193
* // create an entity
133-
* KeyFactory keyFactory = datastore.newKeyFactory().kind("someKind");
194+
* KeyFactory keyFactory = datastore.newKeyFactory().kind("MyKind");
134195
* Key key = datastore.allocateId(keyFactory.newKey());
135196
* Entity entity = Entity.builder(key).set("description", "rollback()").build();
136197
*
@@ -150,7 +211,7 @@ interface Response {
150211
* <p>Example of verifying if a transaction is active.
151212
* <pre> {@code
152213
* // create an entity
153-
* KeyFactory keyFactory = datastore.newKeyFactory().kind("someKind");
214+
* KeyFactory keyFactory = datastore.newKeyFactory().kind("MyKind");
154215
* Key key = datastore.allocateId(keyFactory.newKey());
155216
* Entity entity = Entity.builder(key).set("description", "active()").build();
156217
* // calling transaction.active() now would return true

google-cloud-examples/src/test/java/com/google/cloud/examples/datastore/snippets/ITTransactionSnippets.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class ITTransactionSnippets {
4141

4242
private static Datastore datastore;
4343

44-
private List<Key> registeredKeys = new ArrayList<>();
44+
private final List<Key> registeredKeys = new ArrayList<>();
4545

4646
private String registerKey(Key key) {
4747
registeredKeys.add(key);

0 commit comments

Comments
 (0)