com.google.cloud.datastore.StructuredQuery.PropertyFilter Java Examples
The following examples show how to use
com.google.cloud.datastore.StructuredQuery.PropertyFilter.
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: TransactionSnippets.java From google-cloud-java with Apache License 2.0 | 6 votes |
/** Example of running a query to find all entities with an ancestor. */ // [TARGET run(Query)] // [VARIABLE "my_parent_key_name"] public List<Entity> run(String parentKeyName) { Datastore datastore = transaction.getDatastore(); // [START run] KeyFactory keyFactory = datastore.newKeyFactory().setKind("ParentKind"); Key parentKey = keyFactory.newKey(parentKeyName); // Build a query Query<Entity> query = Query.newEntityQueryBuilder() .setKind("MyKind") .setFilter(PropertyFilter.hasAncestor(parentKey)) .build(); QueryResults<Entity> results = transaction.run(query); List<Entity> entities = Lists.newArrayList(); while (results.hasNext()) { Entity result = results.next(); // do something with result entities.add(result); } transaction.commit(); // [END run] return entities; }
Example #2
Source File: GoogleCloudIdempotentImportExecutor.java From data-transfer-project with Apache License 2.0 | 6 votes |
private Map<String, ErrorDetail> getErrorDetailsForJob(UUID jobId) { Map<String, ErrorDetail> datastoreKnownErrors = new HashMap<>(); Query<Entity> query = Query.newEntityQueryBuilder() .setKind(IDEMPONTENT_ERRORS_KIND) .setFilter(CompositeFilter.and(PropertyFilter.eq(JOB_ID_FIELD, String.valueOf(jobId)))) .build(); QueryResults<Entity> results = datastore.run(query); while (results.hasNext()) { Entity result = results.next(); try { ErrorDetail error = objectMapper.readerFor(ErrorDetail.class).readValue(result.getString(ERROR_FIELD)); datastoreKnownErrors.put(result.getString(IDEMPOTENT_ID_FIELD), error); } catch (IOException e) { monitor.severe(() -> jobIdPrefix + "Unable to parse ErrorDetail: " + e); throw new IllegalStateException(e); } } return datastoreKnownErrors; }
Example #3
Source File: GoogleJobStore.java From data-transfer-project with Apache License 2.0 | 6 votes |
/** * Finds the ID of the first {@link PortabilityJob} in state {@code jobState} in Datastore, or * null if none found. * * <p>TODO(rtannenbaum): Order by creation time so we can process jobs in a FIFO manner. Trying to * OrderBy.asc("created") currently fails because we don't yet have an index set up. */ @Override public UUID findFirst(JobAuthorization.State jobState) { Query<Key> query = Query.newKeyQueryBuilder() .setKind(JOB_KIND) .setFilter(PropertyFilter.eq(PortabilityJob.AUTHORIZATION_STATE, jobState.name())) // .setOrderBy(OrderBy.asc("created")) .setLimit(1) .build(); QueryResults<Key> results = datastore.run(query); if (!results.hasNext()) { return null; } Key key = results.next(); return UUID.fromString(key.getName()); }
Example #4
Source File: PartTreeDatastoreQueryTests.java From spring-cloud-gcp with Apache License 2.0 | 6 votes |
private void prepareDeleteResults(boolean isCollection) { Cursor cursor = Cursor.copyFrom("abc".getBytes()); List<Integer> datastoreMatchingRecords = Arrays.asList(3, 4, 5); when(this.datastoreTemplate.queryKeysOrEntities(any(), any())).thenAnswer((invocation) -> { StructuredQuery<?> statement = invocation.getArgument(0); StructuredQuery.Builder builder = isCollection ? StructuredQuery.newEntityQueryBuilder() : StructuredQuery.newKeyQueryBuilder(); StructuredQuery<?> expected = builder .setFilter(PropertyFilter.eq("action", "BUY")) .setKind("trades") .build(); assertThat(statement).isEqualTo(expected); return new DatastoreResultsIterable(datastoreMatchingRecords.iterator(), cursor); }); }
Example #5
Source File: PartTreeDatastoreQueryTests.java From spring-cloud-gcp with Apache License 2.0 | 6 votes |
@Test public void nonCollectionReturnType() throws NoSuchMethodException { Trade trade = new Trade(); queryWithMockResult("findByAction", null, getClass().getMethod("findByAction", String.class), true, null); Object[] params = new Object[] { "BUY", }; when(this.datastoreTemplate.queryKeysOrEntities(any(), any())).thenAnswer((invocation) -> { EntityQuery statement = invocation.getArgument(0); EntityQuery expected = StructuredQuery.newEntityQueryBuilder() .setFilter(PropertyFilter.eq("action", "BUY")) .setKind("trades") .setLimit(1).build(); assertThat(statement).isEqualTo(expected); List<Trade> results = Collections.singletonList(trade); return new DatastoreResultsIterable(results.iterator(), null); }); assertThat(this.partTreeDatastoreQuery.execute(params)).isEqualTo(trade); }
Example #6
Source File: DatastoreIntegrationTests.java From spring-cloud-gcp with Apache License 2.0 | 6 votes |
@Test public void testSlicedEntityProjections() { reset(datastoreTemplate); Slice<TestEntityProjection> testEntityProjectionSlice = this.testEntityRepository.findBySize(2L, PageRequest.of(0, 1)); List<TestEntityProjection> testEntityProjections = testEntityProjectionSlice.get().collect(Collectors.toList()); assertThat(testEntityProjections).hasSize(1); assertThat(testEntityProjections.get(0)).isInstanceOf(TestEntityProjection.class); assertThat(testEntityProjections.get(0)).isNotInstanceOf(TestEntity.class); // Verifies that the projection method call works. assertThat(testEntityProjections.get(0).getColor()).isEqualTo("blue"); ProjectionEntityQuery projectionQuery = com.google.cloud.datastore.Query.newProjectionEntityQueryBuilder() .addProjection("color") .setFilter(PropertyFilter.eq("size", 2L)) .setKind("test_entities_ci").setLimit(1).build(); verify(datastoreTemplate).queryKeysOrEntities(eq(projectionQuery), any()); }
Example #7
Source File: GoogleCloudIdempotentImportExecutor.java From data-transfer-project with Apache License 2.0 | 6 votes |
private Map<String, Serializable> getKnownValuesForJob(UUID jobId) { Map<String, Serializable> dataStoreKnownValues = new HashMap<>(); Query<Entity> query = Query.newEntityQueryBuilder() .setKind(IDEMPOTENT_RESULTS_KIND) .setFilter(CompositeFilter.and(PropertyFilter.eq(JOB_ID_FIELD, String.valueOf(jobId)))) .build(); QueryResults<Entity> results = datastore.run(query); while (results.hasNext()) { Entity result = results.next(); dataStoreKnownValues.put( result.getString(IDEMPOTENT_ID_FIELD), result.getString(RESULTS_FIELD)); } return dataStoreKnownValues; }
Example #8
Source File: DatastoreStorage.java From styx with Apache License 2.0 | 6 votes |
private EntityQuery.Builder backfillQueryBuilder(boolean showAll, Filter... filters) { final EntityQuery.Builder queryBuilder = Query.newEntityQueryBuilder().setKind(KIND_BACKFILL); final List<Filter> andedFilters = Lists.newArrayList(filters); if (!showAll) { andedFilters.add(PropertyFilter.eq(PROPERTY_ALL_TRIGGERED, false)); andedFilters.add(PropertyFilter.eq(PROPERTY_HALTED, false)); } if (!andedFilters.isEmpty()) { final Filter head = andedFilters.get(0); final Filter[] tail = andedFilters.stream().skip(1).toArray(Filter[]::new); queryBuilder.setFilter(CompositeFilter.and(head, tail)); } return queryBuilder; }
Example #9
Source File: ShardedCounterTest.java From styx with Apache License 2.0 | 6 votes |
@Test public void shouldIncrementCounter() throws IOException { // init counter assertEquals(0, shardedCounter.getCounter(COUNTER_ID1)); //increment counter by 1 updateCounterInTransaction(COUNTER_ID1, 1L); QueryResults<Entity> results = datastore.run(EntityQuery.newEntityQueryBuilder() .setKind(KIND_COUNTER_SHARD) .setFilter(CompositeFilter.and(PropertyFilter.eq(PROPERTY_COUNTER_ID, COUNTER_ID1), PropertyFilter.eq(PROPERTY_SHARD_VALUE,1))) .build()); // assert there's one and only one shard with the value set to 1 assertEquals(1L, results.next().getLong(PROPERTY_SHARD_VALUE)); assertFalse(results.hasNext()); // assert the correct value is fetched after cache expiry shardedCounter.inMemSnapshot.invalidate(COUNTER_ID1); assertEquals(1L, shardedCounter.getCounter(COUNTER_ID1)); }
Example #10
Source File: DatastoreDao.java From getting-started-java with Apache License 2.0 | 6 votes |
@Override public Result<Book> listBooksByUser(String userId, String startCursorString) { Cursor startCursor = null; if (startCursorString != null && !startCursorString.equals("")) { startCursor = Cursor.fromUrlSafe(startCursorString); // Where we left off } Query<Entity> query = Query.newEntityQueryBuilder() // Build the Query .setKind("Book5") // We only care about Books .setFilter(PropertyFilter.eq(Book.CREATED_BY_ID, userId))// Only for this user .setLimit(10) // Only show 10 at a time .setStartCursor(startCursor) // Where we left off // a custom datastore index is required since you are filtering by one property // but ordering by another .setOrderBy(OrderBy.asc(Book.TITLE)) .build(); QueryResults<Entity> resultList = datastore.run(query); // Run the Query List<Book> resultBooks = entitiesToBooks(resultList); // Retrieve and convert Entities Cursor cursor = resultList.getCursorAfter(); // Where to start next time if (cursor != null && resultBooks.size() == 10) { // Are we paging? Save Cursor String cursorString = cursor.toUrlSafe(); // Cursors are WebSafe return new Result<>(resultBooks, cursorString); } else { return new Result<>(resultBooks); } }
Example #11
Source File: DatastoreSessionFilter.java From getting-started-java with Apache License 2.0 | 6 votes |
protected void deleteSessionWithValue(String varName, String varValue) { Transaction transaction = datastore.newTransaction(); try { Query<Entity> query = Query.newEntityQueryBuilder() .setKind("SessionVariable") .setFilter(PropertyFilter.eq(varName, varValue)) .build(); QueryResults<Entity> resultList = transaction.run(query); while (resultList.hasNext()) { Entity stateEntity = resultList.next(); transaction.delete(stateEntity.getKey()); } transaction.commit(); } finally { if (transaction.isActive()) { transaction.rollback(); } } }
Example #12
Source File: DatastoreSessionFilter.java From getting-started-java with Apache License 2.0 | 6 votes |
@Override public void init(FilterConfig config) throws ServletException { // initialize local copy of datastore session variables datastore = DatastoreOptions.getDefaultInstance().getService(); keyFactory = datastore.newKeyFactory().setKind("SessionVariable"); // Delete all sessions unmodified for over two days DateTime dt = DateTime.now(DateTimeZone.UTC); Query<Entity> query = Query.newEntityQueryBuilder() .setKind("SessionVariable") .setFilter(PropertyFilter.le("lastModified", dt.minusDays(2).toString(dtf))) .build(); QueryResults<Entity> resultList = datastore.run(query); while (resultList.hasNext()) { Entity stateEntity = resultList.next(); datastore.delete(stateEntity.getKey()); } }
Example #13
Source File: DatastoreSessionFilter.java From getting-started-java with Apache License 2.0 | 6 votes |
@Override public void init(FilterConfig config) throws ServletException { // initialize local copy of datastore session variables datastore = DatastoreOptions.getDefaultInstance().getService(); keyFactory = datastore.newKeyFactory().setKind("SessionVariable"); // Delete all sessions unmodified for over two days DateTime dt = DateTime.now(DateTimeZone.UTC); Query<Entity> query = Query.newEntityQueryBuilder() .setKind("SessionVariable") .setFilter(PropertyFilter.le("lastModified", dt.minusDays(2).toString(dtf))) .build(); QueryResults<Entity> resultList = datastore.run(query); while (resultList.hasNext()) { Entity stateEntity = resultList.next(); datastore.delete(stateEntity.getKey()); } }
Example #14
Source File: DatastoreSessionFilter.java From getting-started-java with Apache License 2.0 | 6 votes |
@Override public void init(FilterConfig config) throws ServletException { // initialize local copy of datastore session variables datastore = DatastoreOptions.getDefaultInstance().getService(); keyFactory = datastore.newKeyFactory().setKind("SessionVariable"); // Delete all sessions unmodified for over two days DateTime dt = DateTime.now(DateTimeZone.UTC); Query<Entity> query = Query.newEntityQueryBuilder() .setKind("SessionVariable") .setFilter(PropertyFilter.le("lastModified", dt.minusDays(2).toString(dtf))) .build(); QueryResults<Entity> resultList = datastore.run(query); while (resultList.hasNext()) { Entity stateEntity = resultList.next(); datastore.delete(stateEntity.getKey()); } }
Example #15
Source File: DatastoreSnippets.java From google-cloud-java with Apache License 2.0 | 6 votes |
/** Example of running a query to find all entities with a matching property value. */ // [TARGET run(Query, ReadOption...)] // [VARIABLE "my_kind"] // [VARIABLE "my_property"] // [VARIABLE "my_value"] public List<Entity> runQueryOnProperty(String kind, String property, String value) { // TODO change so that it's not necessary to hold the entities in a list for integration testing // [START runQueryOnProperty] StructuredQuery<Entity> query = Query.newEntityQueryBuilder() .setKind(kind) .setFilter(PropertyFilter.eq(property, value)) .build(); QueryResults<Entity> results = datastore.run(query); List<Entity> entities = Lists.newArrayList(); while (results.hasNext()) { Entity result = results.next(); // do something with result entities.add(result); } // [END runQueryOnProperty] return entities; }
Example #16
Source File: DatastoreExample.java From google-cloud-java with Apache License 2.0 | 6 votes |
@Override public void run(Transaction tx, Key userKey, Void arg) { Entity user = tx.get(userKey); if (user == null) { System.out.println("Nothing to delete, user does not exist."); return; } Query<Key> query = Query.newKeyQueryBuilder() .setNamespace(NAMESPACE) .setKind(COMMENT_KIND) .setFilter(PropertyFilter.hasAncestor(userKey)) .build(); QueryResults<Key> comments = tx.run(query); int count = 0; while (comments.hasNext()) { tx.delete(comments.next()); count++; } tx.delete(userKey); System.out.printf("Deleting user '%s' and %d comment[s].%n", userKey.getName(), count); }
Example #17
Source File: DatastoreStore.java From tomcat-runtime with Apache License 2.0 | 6 votes |
/** * Remove expired sessions from the datastore. */ @Override public void processExpires() { log.debug("Processing expired sessions"); Query<Key> query = Query.newKeyQueryBuilder().setKind(sessionKind) .setFilter(PropertyFilter.le(SessionMetadata.EXPIRATION_TIME, clock.millis())) .build(); QueryResults<Key> keys = datastore.run(query); Stream<Key> toDelete = Streams.stream(keys) .parallel() .flatMap(key -> Streams.stream(datastore.run(Query.newKeyQueryBuilder() .setKind(sessionKind) .setFilter(PropertyFilter.hasAncestor(newKey(key.getName()))) .build()))); datastore.delete(toDelete.toArray(Key[]::new)); }
Example #18
Source File: DatastoreStore.java From tomcat-runtime with Apache License 2.0 | 6 votes |
/** * Create a new session usable by Tomcat, from a serialized session in a Datastore Entity. * @param sessionKey The key associated with the session metadata and attributes. * @return A new session containing the metadata and attributes stored in the entity. * @throws ClassNotFoundException Thrown if a class serialized in the entity is not available in * this context. * @throws IOException Thrown when an error occur during the deserialization. */ private DatastoreSession deserializeSession(Key sessionKey) throws ClassNotFoundException, IOException { TraceContext loadingSessionContext = startSpan("Fetching the session from Datastore"); Iterator<Entity> entities = datastore.run(Query.newEntityQueryBuilder() .setKind(sessionKind) .setFilter(PropertyFilter.hasAncestor(sessionKey)) .build()); endSpan(loadingSessionContext); DatastoreSession session = null; if (entities.hasNext()) { session = (DatastoreSession) manager.createEmptySession(); TraceContext deserializationContext = startSpan("Deserialization of the session"); session.restoreFromEntities(sessionKey, Lists.newArrayList(entities)); endSpan(deserializationContext); } return session; }
Example #19
Source File: ConceptsTest.java From java-docs-samples with Apache License 2.0 | 6 votes |
@Test public void testNamespaceRunQuery() { setUpQueryTests(); // [START datastore_namespace_run_query] KeyFactory keyFactory = datastore.newKeyFactory().setKind("__namespace__"); Key startNamespace = keyFactory.newKey("g"); Key endNamespace = keyFactory.newKey("h"); Query<Key> query = Query.newKeyQueryBuilder() .setKind("__namespace__") .setFilter(CompositeFilter.and( PropertyFilter.gt("__key__", startNamespace), PropertyFilter.lt("__key__", endNamespace))) .build(); List<String> namespaces = new ArrayList<>(); QueryResults<Key> results = datastore.run(query); while (results.hasNext()) { namespaces.add(results.next().getName()); } // [END datastore_namespace_run_query] assertEquals(ImmutableList.of("ghijklmnop"), namespaces); }
Example #20
Source File: DatastoreSessionFilter.java From getting-started-java with Apache License 2.0 | 6 votes |
protected void deleteSessionWithValue(String varName, String varValue) { Transaction transaction = datastore.newTransaction(); try { Query<Entity> query = Query.newEntityQueryBuilder() .setKind("SessionVariable") .setFilter(PropertyFilter.eq(varName, varValue)) .build(); QueryResults<Entity> resultList = transaction.run(query); while (resultList.hasNext()) { Entity stateEntity = resultList.next(); transaction.delete(stateEntity.getKey()); } transaction.commit(); } finally { if (transaction.isActive()) { transaction.rollback(); } } }
Example #21
Source File: DatastoreSessionFilter.java From getting-started-java with Apache License 2.0 | 6 votes |
@Override public void init(FilterConfig config) throws ServletException { // initialize local copy of datastore session variables datastore = DatastoreOptions.getDefaultInstance().getService(); keyFactory = datastore.newKeyFactory().setKind("SessionVariable"); // Delete all sessions unmodified for over two days DateTime dt = DateTime.now(DateTimeZone.UTC); Query<Entity> query = Query.newEntityQueryBuilder() .setKind("SessionVariable") .setFilter(PropertyFilter.le("lastModified", dt.minusDays(2).toString(dtf))) .build(); QueryResults<Entity> resultList = datastore.run(query); while (resultList.hasNext()) { Entity stateEntity = resultList.next(); datastore.delete(stateEntity.getKey()); } }
Example #22
Source File: DatastoreDao.java From getting-started-java with Apache License 2.0 | 6 votes |
@Override public Result<Book> listBooksByUser(String userId, String startCursorString) { Cursor startCursor = null; if (startCursorString != null && !startCursorString.equals("")) { startCursor = Cursor.fromUrlSafe(startCursorString); // Where we left off } Query<Entity> query = Query.newEntityQueryBuilder() // Build the Query .setKind("Book4") // We only care about Books .setFilter(PropertyFilter.eq(Book.CREATED_BY_ID, userId))// Only for this user .setLimit(10) // Only show 10 at a time .setStartCursor(startCursor) // Where we left off // a custom datastore index is required since you are filtering by one property // but ordering by another .setOrderBy(OrderBy.asc(Book.TITLE)) .build(); QueryResults<Entity> resultList = datastore.run(query); // Run the Query List<Book> resultBooks = entitiesToBooks(resultList); // Retrieve and convert Entities Cursor cursor = resultList.getCursorAfter(); // Where to start next time if (cursor != null && resultBooks.size() == 10) { // Are we paging? Save Cursor String cursorString = cursor.toUrlSafe(); // Cursors are WebSafe return new Result<>(resultBooks, cursorString); } else { return new Result<>(resultBooks); } }
Example #23
Source File: DatastoreSessionFilter.java From getting-started-java with Apache License 2.0 | 6 votes |
protected void deleteSessionWithValue(String varName, String varValue) { Transaction transaction = datastore.newTransaction(); try { Query<Entity> query = Query.newEntityQueryBuilder() .setKind("SessionVariable") .setFilter(PropertyFilter.eq(varName, varValue)) .build(); QueryResults<Entity> resultList = transaction.run(query); while (resultList.hasNext()) { Entity stateEntity = resultList.next(); transaction.delete(stateEntity.getKey()); } transaction.commit(); } finally { if (transaction.isActive()) { transaction.rollback(); } } }
Example #24
Source File: DatastoreSessionFilter.java From getting-started-java with Apache License 2.0 | 6 votes |
protected void deleteSessionWithValue(String varName, String varValue) { Transaction transaction = datastore.newTransaction(); try { Query<Entity> query = Query.newEntityQueryBuilder() .setKind("SessionVariable") .setFilter(PropertyFilter.eq(varName, varValue)) .build(); QueryResults<Entity> resultList = transaction.run(query); while (resultList.hasNext()) { Entity stateEntity = resultList.next(); transaction.delete(stateEntity.getKey()); } transaction.commit(); } finally { if (transaction.isActive()) { transaction.rollback(); } } }
Example #25
Source File: ConceptsTest.java From java-docs-samples with Apache License 2.0 | 5 votes |
@Test public void testCompositeFilter() { setUpQueryTests(); // [START datastore_composite_filter] Query<Entity> query = Query.newEntityQueryBuilder() .setKind("Task") .setFilter( CompositeFilter.and(PropertyFilter.eq("done", false), PropertyFilter.eq("priority", 4))) .build(); // [END datastore_composite_filter] assertValidQuery(query); }
Example #26
Source File: ConceptsTest.java From java-docs-samples with Apache License 2.0 | 5 votes |
@Test public void testBasicQuery() { setUpQueryTests(); // [START datastore_basic_query] Query<Entity> query = Query.newEntityQueryBuilder() .setKind("Task") .setFilter(CompositeFilter.and( PropertyFilter.eq("done", false), PropertyFilter.ge("priority", 4))) .setOrderBy(OrderBy.desc("priority")) .build(); // [END datastore_basic_query] assertValidQuery(query); }
Example #27
Source File: ConceptsTest.java From java-docs-samples with Apache License 2.0 | 5 votes |
@Test public void testInequalityInvalid() { // [START datastore_inequality_invalid] Query<Entity> query = Query.newEntityQueryBuilder() .setKind("Task") .setFilter(CompositeFilter.and( PropertyFilter.gt("created", startDate), PropertyFilter.gt("priority", 3))) .build(); // [END datastore_inequality_invalid] assertInvalidQuery(query); }
Example #28
Source File: ConceptsTest.java From java-docs-samples with Apache License 2.0 | 5 votes |
@Test public void testInequalityRange() { setUpQueryTests(); // [START datastore_inequality_range] Query<Entity> query = Query.newEntityQueryBuilder() .setKind("Task") .setFilter(CompositeFilter.and( PropertyFilter.gt("created", startDate), PropertyFilter.lt("created", endDate))) .build(); // [END datastore_inequality_range] assertValidQuery(query); }
Example #29
Source File: ConceptsTest.java From java-docs-samples with Apache License 2.0 | 5 votes |
@Test public void testArrayValueEquality() { setUpQueryTests(); // [START datastore_array_value_equality] Query<Entity> query = Query.newEntityQueryBuilder() .setKind("Task") .setFilter(CompositeFilter.and( PropertyFilter.eq("tag", "fun"), PropertyFilter.eq("tag", "programming"))) .build(); // [END datastore_array_value_equality] assertValidQuery(query); }
Example #30
Source File: ConceptsTest.java From java-docs-samples with Apache License 2.0 | 5 votes |
@Test public void testKindlessQuery() { Key lastSeenKey = keyFactory.newKey("a"); setUpQueryTests(); // [START datastore_kindless_query] Query<Entity> query = Query.newEntityQueryBuilder().setFilter(PropertyFilter.gt("__key__", lastSeenKey)).build(); // [END datastore_kindless_query] assertValidQuery(query); }