Java Code Examples for com.google.appengine.api.datastore.Entity#getKey()
The following examples show how to use
com.google.appengine.api.datastore.Entity#getKey() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QueryFilteringByBasicPropertyTypesTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void testInequalityFilterWithNegativeInteger() { String methodName = "testInequalityFilterWithNegativeInteger"; Entity parentEntity = createTestEntityWithUniqueMethodNameKey(TEST_ENTITY_KIND, methodName); Key key = parentEntity.getKey(); Entity minus2 = buildTestEntity(key).withProperty("prop", -2).store(); Entity minus1 = buildTestEntity(key).withProperty("prop", -1).store(); Entity zero = buildTestEntity(key).withProperty("prop", 0).store(); Entity plus1 = buildTestEntity(key).withProperty("prop", 1).store(); Entity plus2 = buildTestEntity(key).withProperty("prop", 2).store(); assertSet(queryReturns(zero, plus1, plus2), whenFilteringBy(GREATER_THAN, -1, key)); assertSet(queryReturns(minus2, minus1), whenFilteringBy(LESS_THAN_OR_EQUAL, -1, key)); clearData(TEST_ENTITY_KIND, key, 0); }
Example 2
Source File: QueryOptimizationsTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void testProjectionQueryOnlyReturnsEntitiesContainingProjectedProperty() throws Exception { String methodName = "testProjectionQueryOnlyReturnsEntitiesContainingProjectedProperty"; Entity parent = createTestEntityWithUniqueMethodNameKey("Kind", methodName); Key key = parent.getKey(); Entity e1 = createEntity("Kind", key) .withProperty("foo", "foo") .store(); Entity e2 = createEntity("Kind", key) .withProperty("bar", "bar") .store(); Query query = new Query("Kind") .setAncestor(key) .addProjection(new PropertyProjection("foo", String.class)); List<Entity> results = service.prepare(query).asList(withDefaults()); assertEquals(Collections.singletonList(e1), results); }
Example 3
Source File: QueryFilteringOnCollectionPropertyTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void queryWithMultipleEqualityFiltersOnMultivaluedPropertyReturnsEntityIfAllFiltersMatch() throws Exception { String testMethodName = "queryWithMultipleInequalityFiltersOnMultivaluedPropertyReturnsNothing"; Entity parentEntity = createTestEntityWithUniqueMethodNameKey(TEST_ENTITY_KIND, testMethodName); Key parentKey = parentEntity.getKey(); Entity entity = storeTestEntityWithSingleProperty(parentKey, Arrays.asList(1, 2)); assertSet( queryReturns(entity), whenFilteringWith(and( new FilterPredicate(SINGLE_PROPERTY_NAME, EQUAL, 1), new FilterPredicate(SINGLE_PROPERTY_NAME, EQUAL, 2)), parentKey) ); }
Example 4
Source File: QuerySortingTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void testSortingByStringProperty() throws Exception { String methodName = "testSortingByStringProperty"; Entity parent = createTestEntityWithUniqueMethodNameKey(QUERY_SORTING_ENTITY, methodName); Key key = parent.getKey(); // NOTE: intentionally storing entities in non-alphabetic order Entity bill = storeTestEntityWithSingleProperty(key, "Bill"); Entity abraham = storeTestEntityWithSingleProperty(key, "Abraham"); Entity carl = storeTestEntityWithSingleProperty(key, "Carl"); assertList(whenSortingByTheSingleProperty(ASCENDING, key), containsResultsInOrder(abraham, bill, carl)); assertList(whenSortingByTheSingleProperty(DESCENDING, key), containsResultsInOrder(carl, bill, abraham)); service.delete(bill.getKey(), abraham.getKey(), carl.getKey()); }
Example 5
Source File: SessionCleanupServlet.java From appengine-java-vm-runtime with Apache License 2.0 | 6 votes |
private void clearAll(HttpServletResponse response) { Query query = new Query(SESSION_ENTITY_TYPE); query.setKeysOnly(); query.addFilter(EXPIRES_PROP, Query.FilterOperator.LESS_THAN, System.currentTimeMillis()); ArrayList<Key> killList = new ArrayList<Key>(); Iterable<Entity> entities = datastore.prepare(query).asIterable( FetchOptions.Builder.withLimit(MAX_SESSION_COUNT)); for (Entity expiredSession : entities) { Key key = expiredSession.getKey(); killList.add(key); } datastore.delete(killList); response.setStatus(HttpServletResponse.SC_OK); try { response.getWriter().println("Cleared " + killList.size() + " expired sessions."); } catch (IOException ex) { // We still did the work, and successfully... just send an empty body. } }
Example 6
Source File: EntitiesTest.java From java-docs-samples with Apache License 2.0 | 6 votes |
@Test public void keyToString_getsPerson() throws Exception { Entity p = new Entity("Person"); p.setProperty("relationship", "Me"); datastore.put(p); Key k = p.getKey(); // [START generating_keys_3] String personKeyStr = KeyFactory.keyToString(k); // Some time later (for example, after using personKeyStr in a link). Key personKey = KeyFactory.stringToKey(personKeyStr); Entity person = datastore.get(personKey); // [END generating_keys_3] assertThat(personKey).isEqualTo(k); assertWithMessage("person.relationship") .that((String) person.getProperty("relationship")) .isEqualTo("Me"); }
Example 7
Source File: MetadataEntityGroupTest.java From java-docs-samples with Apache License 2.0 | 6 votes |
private static void printEntityGroupVersions(DatastoreService ds, PrintWriter writer) { Entity entity1 = new Entity("Simple"); Key key1 = ds.put(entity1); Key entityGroupKey = Entities.createEntityGroupKey(key1); // Print entity1's entity group version writer.println("version " + getEntityGroupVersion(ds, null, key1)); // Write to a different entity group Entity entity2 = new Entity("Simple"); ds.put(entity2); // Will print the same version, as entity1's entity group has not changed writer.println("version " + getEntityGroupVersion(ds, null, key1)); // Change entity1's entity group by adding a new child entity Entity entity3 = new Entity("Simple", entity1.getKey()); ds.put(entity3); // Will print a higher version, as entity1's entity group has changed writer.println("version " + getEntityGroupVersion(ds, null, key1)); }
Example 8
Source File: QueryFilteringByGAEPropertyTypesTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void testEntityKeyInequalityFilter() { Entity parentEntity = createTestEntityWithUniqueMethodNameKey(TEST_ENTITY_KIND, "testFilterByInequalityFilter"); Key parentKey = parentEntity.getKey(); Entity entity1 = new Entity("foo", parentKey); service.put(entity1); Entity entity2 = new Entity("foo", parentKey); service.put(entity2); Query query = new Query("foo") .setAncestor(parentKey) .setFilter(new Query.FilterPredicate(Entity.KEY_RESERVED_PROPERTY, GREATER_THAN, entity1.getKey())); List<Entity> list = service.prepare(query).asList(FetchOptions.Builder.withDefaults()); assertEquals(1, list.size()); assertEquals(entity2.getKey(), list.get(0).getKey()); }
Example 9
Source File: QueriesTest.java From java-docs-samples with Apache License 2.0 | 5 votes |
@Test public void keyFilterExample_kindless_returnsMatchingEntities() throws Exception { // Arrange Entity a = new Entity("Child", "a"); Entity b = new Entity("Child", "b"); Entity c = new Entity("Child", "c"); Entity aa = new Entity("Child", "aa", b.getKey()); Entity bb = new Entity("Child", "bb", b.getKey()); Entity aaa = new Entity("Child", "aaa", bb.getKey()); Entity bbb = new Entity("Child", "bbb", bb.getKey()); Entity adult = new Entity("Adult", "a"); Entity zooAnimal = new Entity("ZooAnimal", "a"); datastore.put(ImmutableList.<Entity>of(a, b, c, aa, bb, aaa, bbb, adult, zooAnimal)); // Act Key lastSeenKey = bb.getKey(); // [START gae_java8_datastore_kindless_query] Filter keyFilter = new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN, lastSeenKey); Query q = new Query().setFilter(keyFilter); // [END gae_java8_datastore_kindless_query] // Assert List<Entity> results = datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults()); assertWithMessage("query results") .that(results) .containsExactly( aaa, // Ancestor path "b/bb/aaa" is greater than "b/bb". bbb, // Ancestor path "b/bb/bbb" is greater than "b/bb". zooAnimal, // Kind "ZooAnimal" is greater than "Child" c); // Key name identifier "c" is greater than b. }
Example 10
Source File: TransactionTest.java From appengine-tck with Apache License 2.0 | 5 votes |
private GroupParentKeys writeMultipleInList(boolean allow) throws Exception { GroupParentKeys keys = new GroupParentKeys(); List<Entity> es = new ArrayList<>(); TransactionOptions tos = TransactionOptions.Builder.withXG(allow); Transaction tx = service.beginTransaction(tos); try { Entity parent = new Entity(kindName); parent.setProperty("check", "parent"); parent.setProperty("stamp", new Date()); es.add(parent); keys.firstParent = parent.getKey(); Entity other = new Entity(otherKind); other.setProperty("check", "other"); other.setProperty("stamp", new Date()); es.add(other); keys.secondParent = other.getKey(); service.put(tx, es); tx.commit(); sync(sleepTime); } catch (Exception e) { tx.rollback(); throw e; } sync(sleepTime); return keys; }
Example 11
Source File: QueryOptimizationsTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testProjectionQueriesHandleEntityModificationProperly() throws Exception { String methodName = "testProjectionAfterRemove"; Entity parent = createTestEntityWithUniqueMethodNameKey("test", methodName); Key key = parent.getKey(); Entity e = createEntity("test", key) .withProperty("prop", Arrays.asList("aaa", "bbb", "ccc")) .store(); Query query = new Query("test") .setAncestor(key) .addProjection(new PropertyProjection("prop", String.class)) .addSort("prop"); assertEquals(3, service.prepare(query).asList(withDefaults()).size()); e = createEntity(e.getKey()) .withProperty("prop", Arrays.asList("aaa", "bbb")) .store(); assertEquals(2, service.prepare(query).asList(withDefaults()).size()); service.delete(e.getKey()); assertEquals(0, service.prepare(query).asList(withDefaults()).size()); }
Example 12
Source File: QueriesTest.java From java-docs-samples with Apache License 2.0 | 5 votes |
@Test public void ancestorQueryExample_returnsMatchingEntities() throws Exception { // [START gae_java8_datastore_ancestor_query] DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); Entity tom = new Entity("Person", "Tom"); Key tomKey = tom.getKey(); datastore.put(tom); Entity weddingPhoto = new Entity("Photo", tomKey); weddingPhoto.setProperty("imageURL", "http://domain.com/some/path/to/wedding_photo.jpg"); Entity babyPhoto = new Entity("Photo", tomKey); babyPhoto.setProperty("imageURL", "http://domain.com/some/path/to/baby_photo.jpg"); Entity dancePhoto = new Entity("Photo", tomKey); dancePhoto.setProperty("imageURL", "http://domain.com/some/path/to/dance_photo.jpg"); Entity campingPhoto = new Entity("Photo"); campingPhoto.setProperty("imageURL", "http://domain.com/some/path/to/camping_photo.jpg"); List<Entity> photoList = Arrays.asList(weddingPhoto, babyPhoto, dancePhoto, campingPhoto); datastore.put(photoList); Query photoQuery = new Query("Photo").setAncestor(tomKey); // This returns weddingPhoto, babyPhoto, and dancePhoto, // but not campingPhoto, because tom is not an ancestor List<Entity> results = datastore.prepare(photoQuery).asList(FetchOptions.Builder.withDefaults()); // [END gae_java8_datastore_ancestor_query] assertWithMessage("query results") .that(results) .containsExactly(weddingPhoto, babyPhoto, dancePhoto); }
Example 13
Source File: QueryOptimizationsTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testProjections() throws Exception { Entity parent = createTestEntityWithUniqueMethodNameKey("Product", "testProjections"); Key key = parent.getKey(); Entity e = createEntity("Product", key) .withProperty("price", 123L) .withProperty("percent", 0.123) .withProperty("x", -0.321) .withProperty("diff", -5L) .withProperty("weight", 10L) .store(); Query query = new Query("Product") .setAncestor(key) .addProjection(new PropertyProjection("price", Long.class)) .addProjection(new PropertyProjection("percent", Double.class)) .addProjection(new PropertyProjection("x", Double.class)) .addProjection(new PropertyProjection("diff", Long.class)); PreparedQuery preparedQuery = service.prepare(query); Entity result = preparedQuery.asSingleEntity(); assertEquals(e.getKey(), result.getKey()); assertEquals(e.getProperty("price"), result.getProperty("price")); assertEquals(e.getProperty("percent"), result.getProperty("percent")); assertEquals(e.getProperty("x"), result.getProperty("x")); assertEquals(e.getProperty("diff"), result.getProperty("diff")); assertNull(result.getProperty("weight")); }
Example 14
Source File: QueryFilteringOnCollectionPropertyTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void queryWithMultipleInequalityFiltersOnMultivaluedPropertyReturnsNothing() throws Exception { String testMethodName = "queryWithMultipleEqualityFiltersOnMultivaluedPropertyReturnsEntityIfAllFiltersMatch"; Entity parentEntity = createTestEntityWithUniqueMethodNameKey(TEST_ENTITY_KIND, testMethodName); Key parentKey = parentEntity.getKey(); storeTestEntityWithSingleProperty(parentKey, Arrays.asList(1, 2)); assertSet( queryReturnsNothing(), whenFilteringWith(and( new FilterPredicate(SINGLE_PROPERTY_NAME, GREATER_THAN, 1), new FilterPredicate(SINGLE_PROPERTY_NAME, LESS_THAN, 2)), parentKey) ); }
Example 15
Source File: QuerySortingTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testSortingByFloatProperty() throws Exception { String methodName = "testSortingByFloatProperty"; Entity parent = createTestEntityWithUniqueMethodNameKey(QUERY_SORTING_ENTITY, methodName); Key key = parent.getKey(); Entity thirty = storeTestEntityWithSingleProperty(key, 30f); Entity two = storeTestEntityWithSingleProperty(key, 2f); Entity hundred = storeTestEntityWithSingleProperty(key, 100f); assertList(whenSortingByTheSingleProperty(ASCENDING, key), containsResultsInOrder(two, thirty, hundred)); assertList(whenSortingByTheSingleProperty(DESCENDING, key), containsResultsInOrder(hundred, thirty, two)); service.delete(thirty.getKey(), two.getKey(), hundred.getKey()); }
Example 16
Source File: QueryTestBase.java From appengine-tck with Apache License 2.0 | 5 votes |
/** * Tests whether given two entities with each having a single property, whose value is either foo or bar; when * querying by EQUAL foo, the query returns foo; and when querying by NOT_EQUAL foo, the query returns bar. * * @param foo property value for first entity * @param bar property value for second entity */ protected void testEqualityQueries(Object foo, Object bar) { Entity parentEntity = createTestEntityWithUniqueMethodNameKey(TEST_ENTITY_KIND, "testEqualityQueries"); Key key = parentEntity.getKey(); Entity fooEntity = storeTestEntityWithSingleProperty(key, foo); Entity barEntity = storeTestEntityWithSingleProperty(key, bar); Entity noPropertyEntity = storeTestEntityWithoutProperties(key); assertSet(queryReturns(fooEntity), whenFilteringBy(EQUAL, foo, key)); assertSet(queryReturns(barEntity), whenFilteringBy(NOT_EQUAL, foo, key)); clearData(TEST_ENTITY_KIND, key, 0); }
Example 17
Source File: QueryOptimizationsTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testEntityOnlyContainsProjectedProperties() throws Exception { String methodName = "testEntityOnlyContainsProjectedProperties"; Entity parent = createTestEntityWithUniqueMethodNameKey("Product", methodName); Key key = parent.getKey(); Entity b = createEntity("Product", key) .withProperty("name", "b") .withProperty("price", 1L) .store(); Entity a = createEntity("Product", key) .withProperty("name", "a") .withProperty("price", 2L) .store(); Query query = new Query("Product") .setAncestor(key) .addProjection(new PropertyProjection("price", Long.class)) .setFilter(new Query.FilterPredicate("name", IN, Arrays.asList("a", "b"))); Entity firstResult = service.prepare(query).asList(FetchOptions.Builder.withDefaults()).get(0); assertEquals(1, firstResult.getProperties().size()); assertEquals("price", firstResult.getProperties().keySet().iterator().next()); query = new Query("Product") .setKeysOnly() .setFilter(new Query.FilterPredicate("name", IN, Arrays.asList("a", "b"))); firstResult = service.prepare(query).asList(FetchOptions.Builder.withDefaults()).get(0); assertEquals(0, firstResult.getProperties().size()); }
Example 18
Source File: AsyncServiceTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testDataDelete() throws Exception { Entity parent = createTestEntityWithUniqueMethodNameKey(ASYNC_ENTITY, "testDataDelete"); Key key = parent.getKey(); Entity newRec = new Entity(ASYNC_ENTITY); newRec.setProperty("count", 0); newRec.setProperty("timestamp", new Date()); Key ekey = service.put(newRec); Future<Void> future = asyncService.delete(ekey); future.get(); assertTaskIsDoneAndNotCancelled(future); assertEquals(0, service.prepare(simpleQuery(key)).countEntities(withDefaults())); }
Example 19
Source File: PipelineModelObject.java From appengine-pipelines with Apache License 2.0 | 4 votes |
private static Key extractKey(Entity entity) { return entity.getKey(); }
Example 20
Source File: QueryOptimizationsTest.java From appengine-tck with Apache License 2.0 | 4 votes |
@Ignore("According to the docs, ordering of query results is undefined when no sort order is specified. They are " + "currently ordered according to the index, but this may change in the future and so it shouldn't be tested by the TCK.") @Test public void testProjectionQueryDefaultSortOrderIsDefinedByIndexDefinition() throws Exception { String methodName = "testProjectionQueryDefaultSortOrderIsDefinedByIndexDefinition"; Entity parent = createTestEntityWithUniqueMethodNameKey("Product", methodName); Key key = parent.getKey(); Entity b1 = createEntity("Product", key) .withProperty("name", "b") .withProperty("price", 1L) .withProperty("group", "x") .store(); Entity c2 = createEntity("Product", key) .withProperty("name", "c") .withProperty("price", 2L) .withProperty("group", "y") .store(); Entity c1 = createEntity("Product", key) .withProperty("name", "c") .withProperty("price", 1L) .withProperty("group", "y") .store(); Entity a1 = createEntity("Product", key) .withProperty("name", "a") .withProperty("price", 1L) .withProperty("group", "z") .store(); Query query = new Query("Product") .setAncestor(key) .addProjection(new PropertyProjection("name", String.class)) .addProjection(new PropertyProjection("price", Long.class)); assertResultsInOrder(query, a1, b1, c1, c2); query = new Query("Product") .setAncestor(key) .addProjection(new PropertyProjection("name", String.class)) .addProjection(new PropertyProjection("group", String.class)) .addProjection(new PropertyProjection("price", Long.class)); assertResultsInOrder(query, c1, c2, b1, a1); }