From 730ba75310d93ed3215b4bd2c67f424a74ca0b0f Mon Sep 17 00:00:00 2001 From: Victor Chudnovsky Date: Fri, 16 Sep 2016 12:03:22 -0700 Subject: [PATCH 1/6] Add snippets and test for transaction.get() --- .../snippets/TransactionSnippets.java | 75 +++++++++++++++++++ .../snippets/ITTransactionSnippets.java | 15 ++++ 2 files changed, 90 insertions(+) diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/datastore/snippets/TransactionSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/datastore/snippets/TransactionSnippets.java index e81911992ac2..f0c95be1cc78 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/datastore/snippets/TransactionSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/datastore/snippets/TransactionSnippets.java @@ -29,6 +29,8 @@ import com.google.cloud.datastore.KeyFactory; import com.google.cloud.datastore.Transaction; +import java.util.Iterator; + /** * This class contains a number of snippets for the {@link Transaction} interface. */ @@ -40,6 +42,79 @@ public TransactionSnippets(Transaction transaction) { this.transaction = transaction; } + /** + * Example of getting an Entity for a given key. + * + * This function cleans up after itself after the snippet part executes. + */ + // [TARGET get(Key)] + public boolean get() { + // [START get] + Datastore datastore = transaction.datastore(); + KeyFactory keyFactory = datastore.newKeyFactory().kind("someKind"); + Key key = datastore.allocateId(keyFactory.newKey()); + Entity entity = Entity.builder(key).set("description", "get()").build(); + datastore.put(entity); + + Entity result = null; + try { + result = transaction.get(key); + } catch (DatastoreException ex) { + // handle exception + } + // [END get] + + // For tests. + boolean consistent = result != null && result.equals(entity); + + // Clean up. + transaction.rollback(); + + return consistent; + } + + /** + * Example of getting multiple transactions. + */ + // [TARGET get(Key...)] + public boolean getMultiple() { + // [START getmultiple] + Datastore datastore = transaction.datastore(); + KeyFactory keyFactory = datastore.newKeyFactory().kind("someKind"); + + Key keyOne = datastore.allocateId(keyFactory.newKey()); + Entity entityOne = Entity.builder(keyOne).set("description", "get() One").build(); + datastore.put(entityOne); + + Key keyTwo = datastore.allocateId(keyFactory.newKey()); + Entity entityTwo = Entity.builder(keyTwo).set("description", "get() Two").build(); + datastore.put(entityTwo); + + Iterator result = null; + try { + result = transaction.get(keyOne, keyTwo); + } catch (DatastoreException ex) { + // handle exception + } + // [END getmultiple] + + // For tests. + boolean consistent = (result != null); + if (consistent) { + Entity entityFirst = result.next(); + Entity entitySecond = result.next(); + consistent = !result.hasNext() && + (!entityFirst.equals(entitySecond)) && + (entityFirst.equals(entityOne) || entityFirst.equals(entityTwo)) && + (entitySecond.equals(entityOne) || entitySecond.equals(entityTwo)); + } + + // Clean up. + transaction.rollback(); + + return consistent; + } + /** * Example of committing a transaction. */ diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/datastore/snippets/ITTransactionSnippets.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/datastore/snippets/ITTransactionSnippets.java index dab72d63857f..9efd058d9aac 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/datastore/snippets/ITTransactionSnippets.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/datastore/snippets/ITTransactionSnippets.java @@ -18,6 +18,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import com.google.cloud.datastore.Datastore; import com.google.cloud.datastore.DatastoreOptions; @@ -37,6 +38,20 @@ public static void beforeClass() { datastore = DatastoreOptions.defaultInstance().service(); } + @Test + public void testGet() { + Transaction transaction = datastore.newTransaction(); + TransactionSnippets transactionSnippets = new TransactionSnippets(transaction); + assertTrue(transactionSnippets.get()); + } + + @Test + public void testGetMultiple() { + Transaction transaction = datastore.newTransaction(); + TransactionSnippets transactionSnippets = new TransactionSnippets(transaction); + assertTrue(transactionSnippets.getMultiple()); + } + @Test public void testCommit() { Transaction transaction = datastore.newTransaction(); From bd9b8b17052743e71993f6af22184164ca6e4a8a Mon Sep 17 00:00:00 2001 From: Victor Chudnovsky Date: Fri, 16 Sep 2016 14:43:10 -0700 Subject: [PATCH 2/6] Add snippets and test for transaction.fetch() --- .../snippets/TransactionSnippets.java | 75 +++++++++++++++++-- .../snippets/ITTransactionSnippets.java | 7 ++ 2 files changed, 76 insertions(+), 6 deletions(-) diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/datastore/snippets/TransactionSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/datastore/snippets/TransactionSnippets.java index f0c95be1cc78..8031be75cddc 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/datastore/snippets/TransactionSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/datastore/snippets/TransactionSnippets.java @@ -30,6 +30,7 @@ import com.google.cloud.datastore.Transaction; import java.util.Iterator; +import java.util.List; /** * This class contains a number of snippets for the {@link Transaction} interface. @@ -52,6 +53,8 @@ public boolean get() { // [START get] Datastore datastore = transaction.datastore(); KeyFactory keyFactory = datastore.newKeyFactory().kind("someKind"); + + // Create an entity. Key key = datastore.allocateId(keyFactory.newKey()); Entity entity = Entity.builder(key).set("description", "get()").build(); datastore.put(entity); @@ -65,29 +68,35 @@ public boolean get() { // [END get] // For tests. + // TODO: Consider putting this block inside the try statement above so users can see the + // expected output without even having to run the snippet. boolean consistent = result != null && result.equals(entity); // Clean up. transaction.rollback(); - return consistent; + return consistent; } - /** - * Example of getting multiple transactions. - */ + /** + * Example of getting Entitys for several keys. + * + * This function cleans up after itself after the snippet part executes. + */ // [TARGET get(Key...)] public boolean getMultiple() { // [START getmultiple] Datastore datastore = transaction.datastore(); KeyFactory keyFactory = datastore.newKeyFactory().kind("someKind"); + // Create an entity. Key keyOne = datastore.allocateId(keyFactory.newKey()); - Entity entityOne = Entity.builder(keyOne).set("description", "get() One").build(); + Entity entityOne = Entity.builder(keyOne).set("description", "One").build(); datastore.put(entityOne); + // Create another entity. Key keyTwo = datastore.allocateId(keyFactory.newKey()); - Entity entityTwo = Entity.builder(keyTwo).set("description", "get() Two").build(); + Entity entityTwo = Entity.builder(keyTwo).set("description", "Two").build(); datastore.put(entityTwo); Iterator result = null; @@ -99,6 +108,8 @@ public boolean getMultiple() { // [END getmultiple] // For tests. + // TODO: Consider putting this block inside the try statement above so users can see the + // expected output without even having to run the snippet. boolean consistent = (result != null); if (consistent) { Entity entityFirst = result.next(); @@ -115,6 +126,56 @@ public boolean getMultiple() { return consistent; } + /** + * Example of fetching a list of Entity for several keys. + * + * This function cleans up after itself after the snippet part executes. + */ + // [TARGET fetch(Key...)] + public boolean fetch() { + // [START fetch] + Datastore datastore = transaction.datastore(); + KeyFactory keyFactory = datastore.newKeyFactory().kind("someKind"); + + // Create an entity. + Key keyOne = datastore.allocateId(keyFactory.newKey()); + Entity entityOne = Entity.builder(keyOne).set("description", "One").build(); + datastore.put(entityOne); + + // Create another entity. + Key keyTwo = datastore.allocateId(keyFactory.newKey()); + Entity entityTwo = Entity.builder(keyTwo).set("description", "Two").build(); + datastore.put(entityTwo); + + // No entity is associated with the following key. + Key keyAbsent = datastore.allocateId(keyFactory.newKey()); + + List result = null; + try { + result = transaction.fetch(keyOne, keyAbsent, keyTwo); + } catch (DatastoreException ex) { + // handle exception + } + // [END fetch] + + // For tests. + // TODO: Consider putting this block inside the try statement above so users can see the + // expected output without even having to run the snippet. + Iterator iterator = result.iterator(); + Entity entityFirst = iterator.next(); + Entity entityAbsent = iterator.next(); + Entity entitySecond = iterator.next(); + boolean consistent = !iterator.hasNext() && + entityFirst.equals(entityOne) && + entityAbsent == null && + entitySecond.equals(entityTwo); + + // Clean up. + transaction.rollback(); + + return consistent; + } + /** * Example of committing a transaction. */ @@ -126,6 +187,7 @@ public Key commit() { KeyFactory keyFactory = datastore.newKeyFactory().kind("someKind"); Key key = datastore.allocateId(keyFactory.newKey()); Entity entity = Entity.builder(key).set("description", "commit()").build(); + // add the entity and commit try { transaction.put(entity); @@ -134,6 +196,7 @@ public Key commit() { // handle exception } // [END commit] + return key; } diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/datastore/snippets/ITTransactionSnippets.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/datastore/snippets/ITTransactionSnippets.java index 9efd058d9aac..3f6a5300c118 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/datastore/snippets/ITTransactionSnippets.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/datastore/snippets/ITTransactionSnippets.java @@ -52,6 +52,13 @@ public void testGetMultiple() { assertTrue(transactionSnippets.getMultiple()); } + @Test + public void fetch() { + Transaction transaction = datastore.newTransaction(); + TransactionSnippets transactionSnippets = new TransactionSnippets(transaction); + assertTrue(transactionSnippets.fetch()); + } + @Test public void testCommit() { Transaction transaction = datastore.newTransaction(); From 1b78e2356cf788696a077270b85c54cd8bc2708a Mon Sep 17 00:00:00 2001 From: Victor Chudnovsky Date: Fri, 16 Sep 2016 17:06:22 -0700 Subject: [PATCH 3/6] Add snippets and test for transaction.run() --- .../snippets/TransactionSnippets.java | 67 +++++++++++++++++++ .../snippets/ITTransactionSnippets.java | 7 ++ 2 files changed, 74 insertions(+) diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/datastore/snippets/TransactionSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/datastore/snippets/TransactionSnippets.java index 8031be75cddc..1b1f145f0995 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/datastore/snippets/TransactionSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/datastore/snippets/TransactionSnippets.java @@ -27,6 +27,10 @@ import com.google.cloud.datastore.Entity; import com.google.cloud.datastore.Key; import com.google.cloud.datastore.KeyFactory; +import com.google.cloud.datastore.PathElement; +import com.google.cloud.datastore.Query; +import com.google.cloud.datastore.QueryResults; +import com.google.cloud.datastore.StructuredQuery.PropertyFilter; import com.google.cloud.datastore.Transaction; import java.util.Iterator; @@ -74,6 +78,7 @@ public boolean get() { // Clean up. transaction.rollback(); + datastore.delete(key); return consistent; } @@ -122,6 +127,8 @@ public boolean getMultiple() { // Clean up. transaction.rollback(); + datastore.delete(keyOne); + datastore.delete(keyTwo); return consistent; } @@ -172,10 +179,70 @@ public boolean fetch() { // Clean up. transaction.rollback(); + datastore.delete(keyOne); + datastore.delete(keyTwo); return consistent; } + /** + * Example of committing a transaction. + */ + // [TARGET run(Query)] + public boolean run() { + Datastore datastore = transaction.datastore(); + + // [START run] + // Create an entity + Key keyOne = datastore.newKeyFactory().kind("First").newKey("One"); + Entity entityOne = Entity.builder(keyOne).set("description", "run one").build(); + datastore.put(entityOne); + + // Create a child. Note that queries inside transactions must have ancestors. + Key keyTwo = datastore + .newKeyFactory() + .kind("Second") + .ancestors(PathElement.of("First", "One")) + .newKey("Two"); + Entity entityTwo = Entity.builder(keyTwo).set("description", "run two").build(); + datastore.put(entityTwo); + + // Build a query + Query query = Query.entityQueryBuilder() + .kind("Second") + .filter(PropertyFilter.hasAncestor(keyOne)) + .build(); + + QueryResults result = null; + Entity first = null; + + // Run the query + try { + // first = transaction.get(keyOne); + result = transaction.run(query); + transaction.commit(); + } catch (DatastoreException ex) { + // handle exception + } + // [END run] + + // For tests. + // TODO: Consider putting this block inside the try statement above so users can see the + // expected output without even having to run the snippet. + // + // If this test starts failing, ensure in the Cloud Console that all entities of kinds "First" + // and "Second" have been deleted. + boolean consistent = result != null && + result.next().equals(entityTwo) && + !result.hasNext(); + + // Clean up. + datastore.delete(keyOne); + datastore.delete(keyTwo); + + return consistent; + } + /** * Example of committing a transaction. */ diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/datastore/snippets/ITTransactionSnippets.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/datastore/snippets/ITTransactionSnippets.java index 3f6a5300c118..75504533a7e3 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/datastore/snippets/ITTransactionSnippets.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/datastore/snippets/ITTransactionSnippets.java @@ -59,6 +59,13 @@ public void fetch() { assertTrue(transactionSnippets.fetch()); } + @Test + public void run() { + Transaction transaction = datastore.newTransaction(); + TransactionSnippets transactionSnippets = new TransactionSnippets(transaction); + assertTrue(transactionSnippets.run()); + } + @Test public void testCommit() { Transaction transaction = datastore.newTransaction(); From 8e9e14220bd496dddf2da06928d3b94096e7db61 Mon Sep 17 00:00:00 2001 From: Victor Chudnovsky Date: Fri, 16 Sep 2016 18:23:51 -0700 Subject: [PATCH 4/6] Comsetic changes. --- .../cloud/examples/datastore/snippets/TransactionSnippets.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/datastore/snippets/TransactionSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/datastore/snippets/TransactionSnippets.java index 1b1f145f0995..1d9942e6495a 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/datastore/snippets/TransactionSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/datastore/snippets/TransactionSnippets.java @@ -115,7 +115,7 @@ public boolean getMultiple() { // For tests. // TODO: Consider putting this block inside the try statement above so users can see the // expected output without even having to run the snippet. - boolean consistent = (result != null); + boolean consistent = result != null; if (consistent) { Entity entityFirst = result.next(); Entity entitySecond = result.next(); @@ -214,7 +214,6 @@ public boolean run() { .build(); QueryResults result = null; - Entity first = null; // Run the query try { From b3971bf5669900102228c4e2368f98636c2a8dd4 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Tue, 20 Sep 2016 11:14:08 +0200 Subject: [PATCH 5/6] Minor consistency fixes to Transaction snippets --- .../snippets/TransactionSnippets.java | 236 +++++------------- .../snippets/ITTransactionSnippets.java | 86 ++++++- 2 files changed, 145 insertions(+), 177 deletions(-) diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/datastore/snippets/TransactionSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/datastore/snippets/TransactionSnippets.java index 1d9942e6495a..eb6b5d4afdd4 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/datastore/snippets/TransactionSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/datastore/snippets/TransactionSnippets.java @@ -22,12 +22,12 @@ package com.google.cloud.examples.datastore.snippets; +import com.google.api.client.util.Lists; import com.google.cloud.datastore.Datastore; import com.google.cloud.datastore.DatastoreException; import com.google.cloud.datastore.Entity; import com.google.cloud.datastore.Key; import com.google.cloud.datastore.KeyFactory; -import com.google.cloud.datastore.PathElement; import com.google.cloud.datastore.Query; import com.google.cloud.datastore.QueryResults; import com.google.cloud.datastore.StructuredQuery.PropertyFilter; @@ -48,198 +48,92 @@ public TransactionSnippets(Transaction transaction) { } /** - * Example of getting an Entity for a given key. - * - * This function cleans up after itself after the snippet part executes. + * Example of getting an entity for a given key. */ // [TARGET get(Key)] - public boolean get() { - // [START get] + // [VARIABLE "my_key_name"] + public Entity get(String keyName) { Datastore datastore = transaction.datastore(); - KeyFactory keyFactory = datastore.newKeyFactory().kind("someKind"); - - // Create an entity. - Key key = datastore.allocateId(keyFactory.newKey()); - Entity entity = Entity.builder(key).set("description", "get()").build(); - datastore.put(entity); - - Entity result = null; - try { - result = transaction.get(key); - } catch (DatastoreException ex) { - // handle exception - } + // [START get] + Key key = datastore.newKeyFactory().kind("MyKind").newKey(keyName); + Entity entity = transaction.get(key); + transaction.commit(); + // Do something with the entity // [END get] - - // For tests. - // TODO: Consider putting this block inside the try statement above so users can see the - // expected output without even having to run the snippet. - boolean consistent = result != null && result.equals(entity); - - // Clean up. - transaction.rollback(); - datastore.delete(key); - - return consistent; + return entity; } /** - * Example of getting Entitys for several keys. - * - * This function cleans up after itself after the snippet part executes. + * Example of getting entities for several keys. */ - // [TARGET get(Key...)] - public boolean getMultiple() { - // [START getmultiple] + // [TARGET get(Key...)] + // [VARIABLE "my_first_key_name"] + // [VARIABLE "my_second_key_name"] + public List getMultiple(String firstKeyName, String secondKeyName) { Datastore datastore = transaction.datastore(); - KeyFactory keyFactory = datastore.newKeyFactory().kind("someKind"); - - // Create an entity. - Key keyOne = datastore.allocateId(keyFactory.newKey()); - Entity entityOne = Entity.builder(keyOne).set("description", "One").build(); - datastore.put(entityOne); - - // Create another entity. - Key keyTwo = datastore.allocateId(keyFactory.newKey()); - Entity entityTwo = Entity.builder(keyTwo).set("description", "Two").build(); - datastore.put(entityTwo); - - Iterator result = null; - try { - result = transaction.get(keyOne, keyTwo); - } catch (DatastoreException ex) { - // handle exception + // TODO change so that it's not necessary to hold the entities in a list for integration testing + // [START getMultiple] + KeyFactory keyFactory = datastore.newKeyFactory().kind("MyKind"); + Key firstKey = keyFactory.newKey(firstKeyName); + Key secondKey = keyFactory.newKey(secondKeyName); + Iterator entitiesIterator = transaction.get(firstKey, secondKey); + List entities = Lists.newArrayList(); + while (entitiesIterator.hasNext()) { + Entity entity = entitiesIterator.next(); + // do something with the entity + entities.add(entity); } - // [END getmultiple] - - // For tests. - // TODO: Consider putting this block inside the try statement above so users can see the - // expected output without even having to run the snippet. - boolean consistent = result != null; - if (consistent) { - Entity entityFirst = result.next(); - Entity entitySecond = result.next(); - consistent = !result.hasNext() && - (!entityFirst.equals(entitySecond)) && - (entityFirst.equals(entityOne) || entityFirst.equals(entityTwo)) && - (entitySecond.equals(entityOne) || entitySecond.equals(entityTwo)); - } - - // Clean up. - transaction.rollback(); - datastore.delete(keyOne); - datastore.delete(keyTwo); - - return consistent; - } + transaction.commit(); + // [END getMultiple] + return entities; + } /** - * Example of fetching a list of Entity for several keys. - * - * This function cleans up after itself after the snippet part executes. + * Example of fetching a list of entities for several keys. */ - // [TARGET fetch(Key...)] - public boolean fetch() { - // [START fetch] + // [TARGET fetch(Key...)] + // [VARIABLE "my_first_key_name"] + // [VARIABLE "my_second_key_name"] + public List fetchEntitiesWithKeys(String firstKeyName, String secondKeyName) { Datastore datastore = transaction.datastore(); - KeyFactory keyFactory = datastore.newKeyFactory().kind("someKind"); - - // Create an entity. - Key keyOne = datastore.allocateId(keyFactory.newKey()); - Entity entityOne = Entity.builder(keyOne).set("description", "One").build(); - datastore.put(entityOne); - - // Create another entity. - Key keyTwo = datastore.allocateId(keyFactory.newKey()); - Entity entityTwo = Entity.builder(keyTwo).set("description", "Two").build(); - datastore.put(entityTwo); - - // No entity is associated with the following key. - Key keyAbsent = datastore.allocateId(keyFactory.newKey()); - - List result = null; - try { - result = transaction.fetch(keyOne, keyAbsent, keyTwo); - } catch (DatastoreException ex) { - // handle exception + // [START fetchEntitiesWithKeys] + KeyFactory keyFactory = datastore.newKeyFactory().kind("MyKind"); + Key firstKey = keyFactory.newKey(firstKeyName); + Key secondKey = keyFactory.newKey(secondKeyName); + List entities = transaction.fetch(firstKey, secondKey); + for (Entity entity : entities) { + // do something with the entity } - // [END fetch] - - // For tests. - // TODO: Consider putting this block inside the try statement above so users can see the - // expected output without even having to run the snippet. - Iterator iterator = result.iterator(); - Entity entityFirst = iterator.next(); - Entity entityAbsent = iterator.next(); - Entity entitySecond = iterator.next(); - boolean consistent = !iterator.hasNext() && - entityFirst.equals(entityOne) && - entityAbsent == null && - entitySecond.equals(entityTwo); - - // Clean up. - transaction.rollback(); - datastore.delete(keyOne); - datastore.delete(keyTwo); - - return consistent; - } + transaction.commit(); + // [END fetchEntitiesWithKeys] + return entities; + } /** - * Example of committing a transaction. + * Example of running a query to find all entities with an ancestor. */ - // [TARGET run(Query)] - public boolean run() { + // [TARGET run(Query)] + // [VARIABLE "my_parent_key_name"] + public List run(String parentKeyName) { Datastore datastore = transaction.datastore(); - // [START run] - // Create an entity - Key keyOne = datastore.newKeyFactory().kind("First").newKey("One"); - Entity entityOne = Entity.builder(keyOne).set("description", "run one").build(); - datastore.put(entityOne); - - // Create a child. Note that queries inside transactions must have ancestors. - Key keyTwo = datastore - .newKeyFactory() - .kind("Second") - .ancestors(PathElement.of("First", "One")) - .newKey("Two"); - Entity entityTwo = Entity.builder(keyTwo).set("description", "run two").build(); - datastore.put(entityTwo); - + KeyFactory keyFactory = datastore.newKeyFactory().kind("ParentKind"); + Key parentKey = keyFactory.newKey(parentKeyName); // Build a query Query query = Query.entityQueryBuilder() - .kind("Second") - .filter(PropertyFilter.hasAncestor(keyOne)) + .kind("MyKind") + .filter(PropertyFilter.hasAncestor(parentKey)) .build(); - - QueryResults result = null; - - // Run the query - try { - // first = transaction.get(keyOne); - result = transaction.run(query); - transaction.commit(); - } catch (DatastoreException ex) { - // handle exception + QueryResults results = transaction.run(query); + List entities = Lists.newArrayList(); + while (results.hasNext()) { + Entity result = results.next(); + // do something with result + entities.add(result); } + transaction.commit(); // [END run] - - // For tests. - // TODO: Consider putting this block inside the try statement above so users can see the - // expected output without even having to run the snippet. - // - // If this test starts failing, ensure in the Cloud Console that all entities of kinds "First" - // and "Second" have been deleted. - boolean consistent = result != null && - result.next().equals(entityTwo) && - !result.hasNext(); - - // Clean up. - datastore.delete(keyOne); - datastore.delete(keyTwo); - - return consistent; + return entities; } /** @@ -250,7 +144,7 @@ public Key commit() { Datastore datastore = transaction.datastore(); // [START commit] // create an entity - KeyFactory keyFactory = datastore.newKeyFactory().kind("someKind"); + KeyFactory keyFactory = datastore.newKeyFactory().kind("MyKind"); Key key = datastore.allocateId(keyFactory.newKey()); Entity entity = Entity.builder(key).set("description", "commit()").build(); @@ -274,7 +168,7 @@ public Key rollback() { Datastore datastore = transaction.datastore(); // [START rollback] // create an entity - KeyFactory keyFactory = datastore.newKeyFactory().kind("someKind"); + KeyFactory keyFactory = datastore.newKeyFactory().kind("MyKind"); Key key = datastore.allocateId(keyFactory.newKey()); Entity entity = Entity.builder(key).set("description", "rollback()").build(); @@ -294,7 +188,7 @@ public Key active() { Datastore datastore = transaction.datastore(); // [START active] // create an entity - KeyFactory keyFactory = datastore.newKeyFactory().kind("someKind"); + KeyFactory keyFactory = datastore.newKeyFactory().kind("MyKind"); Key key = datastore.allocateId(keyFactory.newKey()); Entity entity = Entity.builder(key).set("description", "active()").build(); // calling transaction.active() now would return true diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/datastore/snippets/ITTransactionSnippets.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/datastore/snippets/ITTransactionSnippets.java index 75504533a7e3..ec58ab0cc9c0 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/datastore/snippets/ITTransactionSnippets.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/datastore/snippets/ITTransactionSnippets.java @@ -16,6 +16,7 @@ package com.google.cloud.examples.datastore.snippets; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; @@ -24,46 +25,119 @@ import com.google.cloud.datastore.DatastoreOptions; import com.google.cloud.datastore.Entity; import com.google.cloud.datastore.Key; +import com.google.cloud.datastore.PathElement; import com.google.cloud.datastore.Transaction; +import com.google.common.collect.Sets; +import org.junit.After; import org.junit.BeforeClass; import org.junit.Test; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + public class ITTransactionSnippets { private static Datastore datastore; + private List registeredKeys = new ArrayList<>(); + + private String registerKey(Key key) { + registeredKeys.add(key); + return key.name(); + } + + private String registerKey(String keyName) { + return registerKey(keyName, "MyKind"); + } + + private String registerKey(String keyName, String kind) { + Key key = datastore.newKeyFactory().kind(kind).newKey(keyName); + registeredKeys.add(key); + return key.name(); + } + @BeforeClass public static void beforeClass() { datastore = DatastoreOptions.defaultInstance().service(); } + @After + public void afterTest() { + datastore.delete(registeredKeys.toArray(new Key[registeredKeys.size()])); + } + @Test public void testGet() { + Key key1 = datastore.newKeyFactory().kind("MyKind").newKey("fetch_key_1"); + Entity entity1 = Entity.builder(key1).set("description", "fetch1").build(); + datastore.put(entity1); + registerKey("fetch_key_1"); + Transaction transaction = datastore.newTransaction(); TransactionSnippets transactionSnippets = new TransactionSnippets(transaction); - assertTrue(transactionSnippets.get()); + assertEquals(entity1, transactionSnippets.get("fetch_key_1")); } @Test public void testGetMultiple() { + Key key1 = datastore.newKeyFactory().kind("MyKind").newKey("fetch_key_1"); + Key key2 = datastore.newKeyFactory().kind("MyKind").newKey("fetch_key_2"); + Entity entity1 = Entity.builder(key1).set("description", "fetch1").build(); + Entity entity2 = Entity.builder(key2).set("description", "fetch2").build(); + datastore.put(entity1, entity2); + registerKey("fetch_key_1"); + registerKey("fetch_key_2"); + Transaction transaction = datastore.newTransaction(); TransactionSnippets transactionSnippets = new TransactionSnippets(transaction); - assertTrue(transactionSnippets.getMultiple()); + Set entities = + Sets.newHashSet(transactionSnippets.getMultiple("fetch_key_1", "fetch_key_2")); + assertEquals(2, entities.size()); + assertTrue(entities.contains(entity1)); + assertTrue(entities.contains(entity2)); } @Test - public void fetch() { + public void testFetchEntitiesWithKeys() { + Key key1 = datastore.newKeyFactory().kind("MyKind").newKey("fetch_key_1"); + Key key2 = datastore.newKeyFactory().kind("MyKind").newKey("fetch_key_2"); + Entity entity1 = Entity.builder(key1).set("description", "fetch1").build(); + Entity entity2 = Entity.builder(key2).set("description", "fetch2").build(); + datastore.put(entity1, entity2); + registerKey("fetch_key_1"); + registerKey("fetch_key_2"); + Transaction transaction = datastore.newTransaction(); TransactionSnippets transactionSnippets = new TransactionSnippets(transaction); - assertTrue(transactionSnippets.fetch()); + Set entities = + Sets.newHashSet(transactionSnippets.fetchEntitiesWithKeys("fetch_key_1", "fetch_key_2")); + assertEquals(2, entities.size()); + assertTrue(entities.contains(entity1)); + assertTrue(entities.contains(entity2)); } @Test - public void run() { + public void testRun() { + Key key1 = datastore.newKeyFactory().kind("ParentKind").newKey("run_key_1"); + Entity entity1 = Entity.builder(key1).set("description", "run1").build(); + datastore.put(entity1); + Key key2 = datastore + .newKeyFactory() + .kind("MyKind") + .ancestors(PathElement.of("ParentKind", "run_key_1")) + .newKey("run_key_2"); + registerKey(key1); + registerKey(key2); + Entity entity2 = Entity.builder(key2).set("description", "run2").build(); + datastore.put(entity2); + Transaction transaction = datastore.newTransaction(); TransactionSnippets transactionSnippets = new TransactionSnippets(transaction); - assertTrue(transactionSnippets.run()); + List entities = transactionSnippets.run("run_key_1"); + assertEquals(1, entities.size()); + assertEquals(entity2, entities.get(0)); } @Test From 23038824b23c002e5916a592027f7ab076d86db5 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Tue, 20 Sep 2016 11:16:10 +0200 Subject: [PATCH 6/6] Add Transaction snippets to Transaction javadoc --- .../google/cloud/datastore/Transaction.java | 67 ++++++++++++++++++- .../snippets/ITTransactionSnippets.java | 2 +- 2 files changed, 65 insertions(+), 4 deletions(-) diff --git a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/Transaction.java b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/Transaction.java index 1ac52ec1a3f8..fd629bf0294b 100644 --- a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/Transaction.java +++ b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/Transaction.java @@ -63,6 +63,15 @@ interface Response { * to fail if entity was changed by others after it was seen by this transaction) but any * write changes in this transaction will not be reflected by the returned entity. * + *

Example of getting an entity for a given key. + *

 {@code
+   * String keyName = "my_key_name";
+   * Key key = datastore.newKeyFactory().kind("MyKind").newKey(keyName);
+   * Entity entity = transaction.get(key);
+   * transaction.commit();
+   * // Do something with the entity
+   * }
+ * * @throws DatastoreException upon failure or if no longer active */ @Override @@ -74,6 +83,23 @@ interface Response { * to fail if any of the entities was changed by others after they were seen by this transaction) * but any write changes in this transaction will not be reflected by the returned entities. * + *

Example of getting entities for several keys. + *

 {@code
+   * String firstKeyName = "my_first_key_name";
+   * String secondKeyName = "my_second_key_name";
+   * KeyFactory keyFactory = datastore.newKeyFactory().kind("MyKind");
+   * Key firstKey = keyFactory.newKey(firstKeyName);
+   * Key secondKey = keyFactory.newKey(secondKeyName);
+   * Iterator entitiesIterator = transaction.get(firstKey, secondKey);
+   * List entities = Lists.newArrayList();
+   * while (entitiesIterator.hasNext()) {
+   *   Entity entity = entitiesIterator.next();
+   *   // do something with the entity
+   *   entities.add(entity);
+   * }
+   * transaction.commit();
+   * }
+ * * @throws DatastoreException upon failure or if no longer active */ @Override @@ -85,6 +111,20 @@ interface Response { * to fail if any of the entities was changed by others after they were seen by this transaction) * but any write changes in this transaction will not be reflected by the returned entities. * + *

Example of fetching a list of entities for several keys. + *

 {@code
+   * String firstKeyName = "my_first_key_name";
+   * String secondKeyName = "my_second_key_name";
+   * KeyFactory keyFactory = datastore.newKeyFactory().kind("MyKind");
+   * Key firstKey = keyFactory.newKey(firstKeyName);
+   * Key secondKey = keyFactory.newKey(secondKeyName);
+   * List entities = transaction.fetch(firstKey, secondKey);
+   * for (Entity entity : entities) {
+   *   // do something with the entity
+   * }
+   * transaction.commit();
+   * }
+ * * @throws DatastoreException upon failure or if no longer active */ @Override @@ -97,6 +137,26 @@ interface Response { * query was performed) but any write changes in this transaction will not be reflected by * the result. * + *

Example of running a query to find all entities with an ancestor. + *

 {@code
+   * String parentKeyName = "my_parent_key_name";
+   * KeyFactory keyFactory = datastore.newKeyFactory().kind("ParentKind");
+   * Key parentKey = keyFactory.newKey(parentKeyName);
+   * // Build a query
+   * Query query = Query.entityQueryBuilder()
+   *     .kind("MyKind")
+   *     .filter(PropertyFilter.hasAncestor(parentKey))
+   *     .build();
+   * QueryResults results = transaction.run(query);
+   * List entities = Lists.newArrayList();
+   * while (results.hasNext()) {
+   *   Entity result = results.next();
+   *   // do something with result
+   *   entities.add(result);
+   * }
+   * transaction.commit();
+   * }
+ * * @throws DatastoreException upon failure or if no longer active */ @Override @@ -108,9 +168,10 @@ interface Response { *

Example of committing a transaction. *

 {@code
    * // create an entity
-   * KeyFactory keyFactory = datastore.newKeyFactory().kind("someKind");
+   * KeyFactory keyFactory = datastore.newKeyFactory().kind("MyKind");
    * Key key = datastore.allocateId(keyFactory.newKey());
    * Entity entity = Entity.builder(key).set("description", "commit()").build();
+   * 
    * // add the entity and commit
    * try {
    *   transaction.put(entity);
@@ -130,7 +191,7 @@ interface Response {
    * 

Example of rolling back a transaction. *

 {@code
    * // create an entity
-   * KeyFactory keyFactory = datastore.newKeyFactory().kind("someKind");
+   * KeyFactory keyFactory = datastore.newKeyFactory().kind("MyKind");
    * Key key = datastore.allocateId(keyFactory.newKey());
    * Entity entity = Entity.builder(key).set("description", "rollback()").build();
    * 
@@ -150,7 +211,7 @@ interface Response {
    * 

Example of verifying if a transaction is active. *

 {@code
    * // create an entity
-   * KeyFactory keyFactory = datastore.newKeyFactory().kind("someKind");
+   * KeyFactory keyFactory = datastore.newKeyFactory().kind("MyKind");
    * Key key = datastore.allocateId(keyFactory.newKey());
    * Entity entity = Entity.builder(key).set("description", "active()").build();
    * // calling transaction.active() now would return true
diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/datastore/snippets/ITTransactionSnippets.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/datastore/snippets/ITTransactionSnippets.java
index ec58ab0cc9c0..07492ffa2d27 100644
--- a/google-cloud-examples/src/test/java/com/google/cloud/examples/datastore/snippets/ITTransactionSnippets.java
+++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/datastore/snippets/ITTransactionSnippets.java
@@ -41,7 +41,7 @@ public class ITTransactionSnippets {
 
   private static Datastore datastore;
 
-  private List registeredKeys = new ArrayList<>();
+  private final List registeredKeys = new ArrayList<>();
 
   private String registerKey(Key key) {
     registeredKeys.add(key);