22
22
23
23
package com .google .cloud .examples .datastore .snippets ;
24
24
25
+ import com .google .api .client .util .Lists ;
25
26
import com .google .cloud .datastore .Datastore ;
26
27
import com .google .cloud .datastore .DatastoreException ;
27
28
import com .google .cloud .datastore .Entity ;
28
29
import com .google .cloud .datastore .Key ;
29
30
import com .google .cloud .datastore .KeyFactory ;
31
+ import com .google .cloud .datastore .Query ;
32
+ import com .google .cloud .datastore .QueryResults ;
33
+ import com .google .cloud .datastore .StructuredQuery .PropertyFilter ;
30
34
import com .google .cloud .datastore .Transaction ;
31
35
36
+ import java .util .Iterator ;
37
+ import java .util .List ;
38
+
32
39
/**
33
40
* This class contains a number of snippets for the {@link Transaction} interface.
34
41
*/
@@ -40,6 +47,95 @@ public TransactionSnippets(Transaction transaction) {
40
47
this .transaction = transaction ;
41
48
}
42
49
50
+ /**
51
+ * Example of getting an entity for a given key.
52
+ */
53
+ // [TARGET get(Key)]
54
+ // [VARIABLE "my_key_name"]
55
+ public Entity get (String keyName ) {
56
+ Datastore datastore = transaction .datastore ();
57
+ // [START get]
58
+ Key key = datastore .newKeyFactory ().kind ("MyKind" ).newKey (keyName );
59
+ Entity entity = transaction .get (key );
60
+ transaction .commit ();
61
+ // Do something with the entity
62
+ // [END get]
63
+ return entity ;
64
+ }
65
+
66
+ /**
67
+ * Example of getting entities for several keys.
68
+ */
69
+ // [TARGET get(Key...)]
70
+ // [VARIABLE "my_first_key_name"]
71
+ // [VARIABLE "my_second_key_name"]
72
+ public List <Entity > getMultiple (String firstKeyName , String secondKeyName ) {
73
+ Datastore datastore = transaction .datastore ();
74
+ // TODO change so that it's not necessary to hold the entities in a list for integration testing
75
+ // [START getMultiple]
76
+ KeyFactory keyFactory = datastore .newKeyFactory ().kind ("MyKind" );
77
+ Key firstKey = keyFactory .newKey (firstKeyName );
78
+ Key secondKey = keyFactory .newKey (secondKeyName );
79
+ Iterator <Entity > entitiesIterator = transaction .get (firstKey , secondKey );
80
+ List <Entity > entities = Lists .newArrayList ();
81
+ while (entitiesIterator .hasNext ()) {
82
+ Entity entity = entitiesIterator .next ();
83
+ // do something with the entity
84
+ entities .add (entity );
85
+ }
86
+ transaction .commit ();
87
+ // [END getMultiple]
88
+ return entities ;
89
+ }
90
+
91
+ /**
92
+ * Example of fetching a list of entities for several keys.
93
+ */
94
+ // [TARGET fetch(Key...)]
95
+ // [VARIABLE "my_first_key_name"]
96
+ // [VARIABLE "my_second_key_name"]
97
+ public List <Entity > fetchEntitiesWithKeys (String firstKeyName , String secondKeyName ) {
98
+ Datastore datastore = transaction .datastore ();
99
+ // [START fetchEntitiesWithKeys]
100
+ KeyFactory keyFactory = datastore .newKeyFactory ().kind ("MyKind" );
101
+ Key firstKey = keyFactory .newKey (firstKeyName );
102
+ Key secondKey = keyFactory .newKey (secondKeyName );
103
+ List <Entity > entities = transaction .fetch (firstKey , secondKey );
104
+ for (Entity entity : entities ) {
105
+ // do something with the entity
106
+ }
107
+ transaction .commit ();
108
+ // [END fetchEntitiesWithKeys]
109
+ return entities ;
110
+ }
111
+
112
+ /**
113
+ * Example of running a query to find all entities with an ancestor.
114
+ */
115
+ // [TARGET run(Query)]
116
+ // [VARIABLE "my_parent_key_name"]
117
+ public List <Entity > run (String parentKeyName ) {
118
+ Datastore datastore = transaction .datastore ();
119
+ // [START run]
120
+ KeyFactory keyFactory = datastore .newKeyFactory ().kind ("ParentKind" );
121
+ Key parentKey = keyFactory .newKey (parentKeyName );
122
+ // Build a query
123
+ Query <Entity > query = Query .entityQueryBuilder ()
124
+ .kind ("MyKind" )
125
+ .filter (PropertyFilter .hasAncestor (parentKey ))
126
+ .build ();
127
+ QueryResults <Entity > results = transaction .run (query );
128
+ List <Entity > entities = Lists .newArrayList ();
129
+ while (results .hasNext ()) {
130
+ Entity result = results .next ();
131
+ // do something with result
132
+ entities .add (result );
133
+ }
134
+ transaction .commit ();
135
+ // [END run]
136
+ return entities ;
137
+ }
138
+
43
139
/**
44
140
* Example of committing a transaction.
45
141
*/
@@ -48,9 +144,10 @@ public Key commit() {
48
144
Datastore datastore = transaction .datastore ();
49
145
// [START commit]
50
146
// create an entity
51
- KeyFactory keyFactory = datastore .newKeyFactory ().kind ("someKind " );
147
+ KeyFactory keyFactory = datastore .newKeyFactory ().kind ("MyKind " );
52
148
Key key = datastore .allocateId (keyFactory .newKey ());
53
149
Entity entity = Entity .builder (key ).set ("description" , "commit()" ).build ();
150
+
54
151
// add the entity and commit
55
152
try {
56
153
transaction .put (entity );
@@ -59,6 +156,7 @@ public Key commit() {
59
156
// handle exception
60
157
}
61
158
// [END commit]
159
+
62
160
return key ;
63
161
}
64
162
@@ -70,7 +168,7 @@ public Key rollback() {
70
168
Datastore datastore = transaction .datastore ();
71
169
// [START rollback]
72
170
// create an entity
73
- KeyFactory keyFactory = datastore .newKeyFactory ().kind ("someKind " );
171
+ KeyFactory keyFactory = datastore .newKeyFactory ().kind ("MyKind " );
74
172
Key key = datastore .allocateId (keyFactory .newKey ());
75
173
Entity entity = Entity .builder (key ).set ("description" , "rollback()" ).build ();
76
174
@@ -90,7 +188,7 @@ public Key active() {
90
188
Datastore datastore = transaction .datastore ();
91
189
// [START active]
92
190
// create an entity
93
- KeyFactory keyFactory = datastore .newKeyFactory ().kind ("someKind " );
191
+ KeyFactory keyFactory = datastore .newKeyFactory ().kind ("MyKind " );
94
192
Key key = datastore .allocateId (keyFactory .newKey ());
95
193
Entity entity = Entity .builder (key ).set ("description" , "active()" ).build ();
96
194
// calling transaction.active() now would return true
0 commit comments