com.google.appengine.api.datastore.PropertyProjection Java Examples
The following examples show how to use
com.google.appengine.api.datastore.PropertyProjection.
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: ProjectionTest.java From java-docs-samples with Apache License 2.0 | 6 votes |
@Test public void projectionQuery_grouping_filtersDuplicates() { putTestData("some duplicate", 0L); putTestData("some duplicate", 0L); putTestData("too big", 1L); // [START grouping] Query q = new Query("TestKind"); q.addProjection(new PropertyProjection("A", String.class)); q.addProjection(new PropertyProjection("B", Long.class)); q.setDistinct(true); q.setFilter(Query.FilterOperator.LESS_THAN.of("B", 1L)); q.addSort("B", Query.SortDirection.DESCENDING); q.addSort("A"); // [END grouping] List<Entity> entities = datastore.prepare(q).asList(FetchOptions.Builder.withLimit(5)); assertThat(entities).hasSize(1); Entity entity = entities.get(0); assertWithMessage("entity.A") .that((String) entity.getProperty("A")) .isEqualTo("some duplicate"); assertWithMessage("entity.B").that((long) entity.getProperty("B")).isEqualTo(0L); }
Example #2
Source File: AppEngineBackEnd.java From appengine-pipelines with Apache License 2.0 | 6 votes |
@Override public Set<String> getRootPipelinesDisplayName() { Query query = new Query(JobRecord.DATA_STORE_KIND); query.addProjection( new PropertyProjection(JobRecord.ROOT_JOB_DISPLAY_NAME, String.class)); query.setDistinct(true); final PreparedQuery preparedQuery = dataStore.prepare(query); return tryFiveTimes(new Operation<Set<String>>("getRootPipelinesDisplayName") { @Override public Set<String> call() { Set<String> pipelines = new LinkedHashSet<>(); for (Entity entity : preparedQuery.asIterable()) { pipelines.add((String) entity.getProperty(JobRecord.ROOT_JOB_DISPLAY_NAME)); } return pipelines; } }); }
Example #3
Source File: RequiredIndexesTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void testProjectionQueryOperatingOnlyOnASinglePropertyDoesNotRequireConfiguredIndex() throws Exception { executeQuery(new Query("Unindexed") .addProjection(new PropertyProjection("someProperty", null))); executeQuery(new Query("Unindexed") .addProjection(new PropertyProjection("someProperty", null)) .setFilter(new Query.FilterPredicate("someProperty", GREATER_THAN, "a"))); executeQuery(new Query("Unindexed") .addProjection(new PropertyProjection("someProperty", null)) .setFilter(new Query.FilterPredicate("someProperty", GREATER_THAN, "a")) .addSort("someProperty")); executeQuery(new Query("Unindexed") .addProjection(new PropertyProjection("someProperty", null)) .setFilter(new Query.FilterPredicate("someProperty", GREATER_THAN, "a")) .addSort("someProperty", Query.SortDirection.DESCENDING)); }
Example #4
Source File: QueryTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void testWithPropertyProjection() { Query query = new Query(kindName, rootKey); query.addProjection(new PropertyProjection("geoptData", GeoPt.class)); Filter filter1 = Query.CompositeFilterOperator.or( Query.FilterOperator.LESS_THAN.of("intList", 5), Query.FilterOperator.GREATER_THAN.of("intList", 90)); Filter filter2 = Query.FilterOperator.EQUAL.of("intList", 52); query.setFilter(Query.CompositeFilterOperator.and(filter1, filter2)); // sql statement String sql = "SELECT geoptData FROM " + kindName; sql += " WHERE ((intList < 5 or intList > 90) AND intList = 52)"; sql += " AND __ancestor__ is " + rootKey; assertEquals(sql.toLowerCase(), query.toString().toLowerCase()); // check query result List<Entity> results = service.prepare(query).asList(fo); Assert.assertTrue(results.size() > 0); assertEquals(new GeoPt((float) (2.12), (float) (2.98)), results.get(0).getProperty("geoptData")); for (Entity e : results) { assertEquals(1, e.getProperties().size()); assertTrue(e.getProperties().containsKey("geoptData")); } }
Example #5
Source File: QueryOptimizationsTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void testOrderOfReturnedResultsIsSameAsOrderOfElementsInInStatementWhenUsingProjections() throws Exception { String methodName = "testOrderOfReturnedResultsIsSameAsOrderOfElementsInInStatementWhenUsingProjections"; 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"))); assertResultsInOrder(query, a, b); query = query.setFilter(new Query.FilterPredicate("name", IN, Arrays.asList("b", "a"))); assertResultsInOrder(query, b, a); }
Example #6
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 #7
Source File: QueryOptimizationsTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void testProjectionQueryReturnsEntitiesContainingProjectedPropertyEvenIfPropertyValueIsNull() throws Exception { String methodName = "testProjectionQueryOnlyReturnsEntitiesContainingProjectedPropertyEvenIfPropertyValueIsNull"; Entity parent = createTestEntityWithUniqueMethodNameKey("Kind", methodName); Key key = parent.getKey(); Entity e1 = createEntity("Kind", key) .withProperty("foo", null) .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 #8
Source File: QueryOptimizationsTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void testProjectionsWithoutType() throws Exception { String methodName = "testProjectionsWithoutType"; Entity parent = createTestEntityWithUniqueMethodNameKey("Product", methodName); Key key = parent.getKey(); Entity e = createEntity("Product", key) .withProperty("long", 123L) .store(); Query query = new Query("Product") .setAncestor(key) .addProjection(new PropertyProjection("long", null)); PreparedQuery preparedQuery = service.prepare(query); Entity result = preparedQuery.asSingleEntity(); assertEquals(e.getKey(), result.getKey()); RawValue rawValue = (RawValue) result.getProperty("long"); assertEquals(Long.valueOf(123L), rawValue.asType(Long.class)); assertEquals(Long.valueOf(123L), rawValue.asStrictType(Long.class)); }
Example #9
Source File: DistinctTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testDistinctFilter2() { Query query = new Query(kindName, rootKey); query.addProjection(new PropertyProjection("stringData", String.class)); query.addProjection(new PropertyProjection("floatData", Float.class)); query.setFilter(new FilterPredicate("stringData", Query.FilterOperator.GREATER_THAN, "string0")); query.addSort("stringData", Query.SortDirection.DESCENDING); query.addSort("floatData", Query.SortDirection.DESCENDING); query.setDistinct(true); assertEquals(2, service.prepare(query).countEntities(fo)); assertTrue(query.getDistinct()); }
Example #10
Source File: DistinctTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testDistinctFilter() { Query query = new Query(kindName, rootKey); query.addProjection(new PropertyProjection("stringData", String.class)); query.addProjection(new PropertyProjection("floatData", Float.class)); query.setFilter(new FilterPredicate("stringData", Query.FilterOperator.NOT_EQUAL, "string1")); query.addSort("stringData", Query.SortDirection.DESCENDING); query.setDistinct(true); assertEquals(5, service.prepare(query).countEntities(fo)); assertTrue(query.getDistinct()); }
Example #11
Source File: DistinctTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testDistinctSort() { Query query = new Query(kindName, rootKey); query.addProjection(new PropertyProjection("stringData", String.class)); query.addProjection(new PropertyProjection("floatData", Float.class)); query.addSort("stringData", Query.SortDirection.DESCENDING); query.setDistinct(true); assertEquals(7, service.prepare(query).countEntities(fo)); assertTrue(query.getDistinct()); query.addSort("floatData", Query.SortDirection.DESCENDING); assertEquals(7, service.prepare(query).countEntities(fo)); }
Example #12
Source File: DatastoreHelperTestBase.java From appengine-tck with Apache License 2.0 | 5 votes |
protected List<Entity> doQuery(String kind, String pName, Class<?> type, boolean indexed) { FetchOptions fo = FetchOptions.Builder.withDefaults(); Query query = new Query(kind, rootKey); if (indexed) { query.addProjection(new PropertyProjection(pName, type)); query.addSort(pName); } return service.prepare(query).asList(fo); }
Example #13
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 #14
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 #15
Source File: QueryOptimizationsTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testProjectionOfCollectionPropertyWithFilterOnCollectionProperty() throws Exception { String methodName = "testProjectionOfCollectionPropertyWithFilterOnCollectionProperty"; Entity parent = createTestEntityWithUniqueMethodNameKey("Product", methodName); Key key = parent.getKey(); Entity e = createEntity("Product", key) .withProperty("name", Arrays.asList("aaa", "bbb")) .withProperty("price", Arrays.asList(10L, 20L)) .store(); Query query = new Query("Product") .setAncestor(key) .addProjection(new PropertyProjection("name", String.class)) .setFilter(new Query.FilterPredicate("price", GREATER_THAN, 0L)) .addSort("price") .addSort("name"); PreparedQuery preparedQuery = service.prepare(query); List<Entity> results = preparedQuery.asList(withDefaults()); assertEquals(4, results.size()); assertEquals(e.getKey(), results.get(0).getKey()); assertEquals(e.getKey(), results.get(1).getKey()); assertEquals(e.getKey(), results.get(2).getKey()); assertEquals(e.getKey(), results.get(3).getKey()); assertEquals("aaa", results.get(0).getProperty("name")); assertEquals("bbb", results.get(1).getProperty("name")); assertEquals("aaa", results.get(2).getProperty("name")); assertEquals("bbb", results.get(3).getProperty("name")); }
Example #16
Source File: QueryOptimizationsTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testProjectionOfCollectionProperties() throws Exception { String methodName = "testProjectionOfCollectionProperties"; String entityKind = "test-proj"; Entity parent = createTestEntityWithUniqueMethodNameKey(entityKind, methodName); Key key = parent.getKey(); Entity e = createEntity(entityKind, key) .withProperty("prop", Arrays.asList("bbb", "ccc", "aaa")) .store(); Query query = new Query(entityKind) .setAncestor(key) .addProjection(new PropertyProjection("prop", String.class)) .addSort("prop"); PreparedQuery preparedQuery = service.prepare(query); List<Entity> results = preparedQuery.asList(withDefaults()); assertEquals(3, results.size()); Entity firstResult = results.get(0); Entity secondResult = results.get(1); Entity thirdResult = results.get(2); assertEquals(e.getKey(), firstResult.getKey()); assertEquals(e.getKey(), secondResult.getKey()); assertEquals(e.getKey(), thirdResult.getKey()); assertEquals("aaa", firstResult.getProperty("prop")); assertEquals("bbb", secondResult.getProperty("prop")); assertEquals("ccc", thirdResult.getProperty("prop")); }
Example #17
Source File: QueryOptimizationsTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testProjectionQueryOnlyReturnsEntitiesContainingAllProjectedProperties() throws Exception { String methodName = "testProjectionQueryOnlyReturnsEntitiesContainingAllProjectedProperties"; Entity parent = createTestEntityWithUniqueMethodNameKey("Kind", methodName); Key key = parent.getKey(); Entity e1 = createEntity("Kind", key) .withProperty("foo", "foo") .withProperty("bar", "bar") .store(); Entity e2 = createEntity("Kind", key) .withProperty("foo", "foo") .store(); Entity e3 = createEntity("Kind", key) .withProperty("bar", "bar") .store(); Entity e4 = createEntity("Kind", key) .withProperty("baz", "baz") .store(); Query query = new Query("Kind") .setAncestor(key) .addProjection(new PropertyProjection("foo", String.class)) .addProjection(new PropertyProjection("bar", String.class)); List<Entity> results = service.prepare(query).asList(withDefaults()); assertEquals(Collections.singletonList(e1), results); }
Example #18
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 #19
Source File: IndexQueryTest.java From appengine-tck with Apache License 2.0 | 5 votes |
private void checkQueryWithLimit(int limit) { FetchOptions fo = FetchOptions.Builder.withLimit(limit); Query query = new Query(kindName, rootKey); query.addProjection(new PropertyProjection("stringData", String.class)); List<Entity> results = service.prepare(query).asList(fo); assertEquals(limit, results.size()); }
Example #20
Source File: IndexQueryTest.java From appengine-tck with Apache License 2.0 | 5 votes |
private void checkQueryType(String property, Class<?> type) { Query query = new Query(kindName, rootKey); query.addProjection(new PropertyProjection(property, type)); String sql = "SELECT " + property + " FROM " + kindName + " WHERE __ancestor__ is " + rootKey; assertEquals(sql.toLowerCase(), query.toString().toLowerCase()); List<Entity> results = service.prepare(query).asList(fetchOption); for (Entity e : results) { assertEquals(1, e.getProperties().size()); assertTrue(e.getProperties().containsKey(property)); } }
Example #21
Source File: IndexQueryTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testQueryFilter() { Query query = new Query(kindName, rootKey); query.addProjection(new PropertyProjection("stringData", String.class)); query.setFilter(new FilterPredicate("intData", FilterOperator.NOT_EQUAL, 50)); query.addSort("intData"); List<Entity> results = service.prepare(query).asList(fetchOption); assertEquals(count - 1, results.size()); for (Entity e : results) { assertTrue(e.getProperty("stringData").toString().contains("5") == false); } }
Example #22
Source File: IndexQueryTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testQueryOrder() { Query query = new Query(kindName, rootKey); query.addProjection(new PropertyProjection("intData", Integer.class)); query.addSort("stringData", Query.SortDirection.DESCENDING); List<Entity> results = service.prepare(query).asList(fetchOption); assertEquals(count, results.size()); int first = new Integer(results.get(0).getProperty("intData").toString()); int last = new Integer(results.get(count - 1).getProperty("intData").toString()); assertTrue(first > last); }
Example #23
Source File: IndexQueryTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testListQuery() { Query query = new Query(kindName, rootKey); query.addProjection(new PropertyProjection("stringList", String.class)); query.addProjection(new PropertyProjection("intList", Integer.class)); List<Entity> results = service.prepare(query).asList(fetchOption); // Distinct stringList data 2 * Distinct intList data 3 * entity's count 10 assertEquals(60, results.size()); Entity e = results.get(0); assertEquals(2, e.getProperties().size()); assertTrue(e.getProperties().containsKey("stringList")); assertTrue(e.getProperties().containsKey("intList")); }
Example #24
Source File: IndexQueryTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testBasicQuery() { Query query = new Query(kindName, rootKey); query.addProjection(new PropertyProjection("stringData", String.class)); query.addProjection(new PropertyProjection("intData", Integer.class)); String sql = "SELECT stringData, intData FROM " + kindName + " WHERE __ancestor__ is " + rootKey; assertEquals(sql.toLowerCase(), query.toString().toLowerCase()); List<Entity> results = service.prepare(query).asList(fetchOption); assertEquals(count, results.size()); for (Entity e : results) { assertEquals(2, e.getProperties().size()); assertTrue(e.getProperties().containsKey("stringData")); assertTrue(e.getProperties().containsKey("intData")); } }
Example #25
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); }
Example #26
Source File: IndexQueryTest.java From appengine-tck with Apache License 2.0 | 4 votes |
@Test public void testCount() { Query query = new Query(kindName, rootKey); query.addProjection(new PropertyProjection("stringData", String.class)); assertEquals(count, service.prepare(query).countEntities(fetchOption)); }
Example #27
Source File: IndexQueryTest.java From appengine-tck with Apache License 2.0 | 4 votes |
@Test(expected = IllegalArgumentException.class) public void testWrongType() { Query query = new Query(kindName, rootKey); query.addProjection(new PropertyProjection("stringData", Integer.class)); service.prepare(query).asIterator(fetchOption).next(); }
Example #28
Source File: ProjectionServlet.java From java-docs-samples with Apache License 2.0 | 4 votes |
private void addGuestbookProjections(Query query) { query.addProjection(new PropertyProjection("content", String.class)); query.addProjection(new PropertyProjection("date", Date.class)); }