2222
2323package com .google .cloud .examples .datastore .snippets ;
2424
25+ import com .google .api .client .util .Lists ;
2526import com .google .cloud .datastore .Datastore ;
2627import com .google .cloud .datastore .DatastoreException ;
2728import com .google .cloud .datastore .Entity ;
2829import com .google .cloud .datastore .Key ;
2930import 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 ;
3034import com .google .cloud .datastore .Transaction ;
3135
36+ import java .util .Iterator ;
37+ import java .util .List ;
38+
3239/**
3340 * This class contains a number of snippets for the {@link Transaction} interface.
3441 */
@@ -40,6 +47,95 @@ public TransactionSnippets(Transaction transaction) {
4047 this .transaction = transaction ;
4148 }
4249
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+
43139 /**
44140 * Example of committing a transaction.
45141 */
@@ -48,9 +144,10 @@ public Key commit() {
48144 Datastore datastore = transaction .datastore ();
49145 // [START commit]
50146 // create an entity
51- KeyFactory keyFactory = datastore .newKeyFactory ().kind ("someKind " );
147+ KeyFactory keyFactory = datastore .newKeyFactory ().kind ("MyKind " );
52148 Key key = datastore .allocateId (keyFactory .newKey ());
53149 Entity entity = Entity .builder (key ).set ("description" , "commit()" ).build ();
150+
54151 // add the entity and commit
55152 try {
56153 transaction .put (entity );
@@ -59,6 +156,7 @@ public Key commit() {
59156 // handle exception
60157 }
61158 // [END commit]
159+
62160 return key ;
63161 }
64162
@@ -70,7 +168,7 @@ public Key rollback() {
70168 Datastore datastore = transaction .datastore ();
71169 // [START rollback]
72170 // create an entity
73- KeyFactory keyFactory = datastore .newKeyFactory ().kind ("someKind " );
171+ KeyFactory keyFactory = datastore .newKeyFactory ().kind ("MyKind " );
74172 Key key = datastore .allocateId (keyFactory .newKey ());
75173 Entity entity = Entity .builder (key ).set ("description" , "rollback()" ).build ();
76174
@@ -90,7 +188,7 @@ public Key active() {
90188 Datastore datastore = transaction .datastore ();
91189 // [START active]
92190 // create an entity
93- KeyFactory keyFactory = datastore .newKeyFactory ().kind ("someKind " );
191+ KeyFactory keyFactory = datastore .newKeyFactory ().kind ("MyKind " );
94192 Key key = datastore .allocateId (keyFactory .newKey ());
95193 Entity entity = Entity .builder (key ).set ("description" , "active()" ).build ();
96194 // calling transaction.active() now would return true
0 commit comments