Java Code Examples for com.google.cloud.datastore.Cursor
The following examples show how to use
com.google.cloud.datastore.Cursor. These examples are extracted from open source projects.
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 Project: spring-cloud-gcp Source File: GqlDatastoreQuery.java License: Apache License 2.0 | 6 votes |
private GqlQuery<? extends BaseEntity> bindArgsToGqlQuery() { Builder builder = GqlQuery.newGqlQueryBuilder(this.finalGql); builder.setAllowLiteral(true); if (this.tagsOrdered.size() != this.params.size()) { throw new DatastoreDataException("Annotated GQL Query Method " + GqlDatastoreQuery.this.queryMethod.getName() + " has " + this.tagsOrdered.size() + " tags but a different number of parameter values: " + this.params.size()); } for (int i = 0; i < this.tagsOrdered.size(); i++) { Object val = this.params.get(i); Object boundVal; if (val instanceof Cursor) { boundVal = val; } else if (ValueUtil.isCollectionLike(val.getClass())) { boundVal = convertCollectionParamToCompatibleArray((List) ValueUtil.toListIfArray(val)); } else { boundVal = GqlDatastoreQuery.this.datastoreOperations.getDatastoreEntityConverter().getConversions() .convertOnWriteSingle(convertEntitiesToKeys(val)).get(); } DatastoreNativeTypes.bindValueToGqlBuilder(builder, this.tagsOrdered.get(i), boundVal); } return builder.build(); }
Example 2
Source Project: spring-cloud-gcp Source File: PartTreeDatastoreQueryTests.java License: Apache License 2.0 | 6 votes |
@Test public void pageableQueryNextPage() throws NoSuchMethodException { queryWithMockResult("findByActionAndSymbolAndPriceLessThanAndPriceGreater" + "ThanEqualAndIdIsNull", null, getClass().getMethod("tradeMethod", String.class, String.class, double.class, double.class, Pageable.class)); this.partTreeDatastoreQuery = createQuery(true, false, null); PageRequest pageRequest = PageRequest.of(1, 2, Sort.Direction.DESC, "id"); Cursor cursor = Cursor.copyFrom("abc".getBytes()); Object[] params = new Object[] { "BUY", "abcd", 8.88, 3.33, DatastorePageable.from(pageRequest, cursor, 99L) }; preparePageResults(2, 2, cursor, Arrays.asList(3, 4), Arrays.asList(1, 2, 3, 4)); when(this.queryMethod.getCollectionReturnType()).thenReturn(List.class); Page result = (Page) this.partTreeDatastoreQuery.execute(params); assertThat(result.getTotalElements()).isEqualTo(99L); assertThat(result.getTotalPages()).isEqualTo(50); assertThat(result.getNumberOfElements()).isEqualTo(2); verify(this.datastoreTemplate, times(1)) .queryKeysOrEntities(any(), any()); }
Example 3
Source Project: spring-cloud-gcp Source File: PartTreeDatastoreQueryTests.java License: 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 4
Source Project: getting-started-java Source File: DatastoreDao.java License: Apache License 2.0 | 6 votes |
@Override public Result<Book> listBooks(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 .setLimit(10) // Only show 10 at a time .setStartCursor(startCursor) // Where we left off .setOrderBy(OrderBy.asc(Book.TITLE)) // Use default Index "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 5
Source Project: getting-started-java Source File: DatastoreDao.java License: 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 6
Source Project: getting-started-java Source File: DatastoreDao.java License: Apache License 2.0 | 6 votes |
@Override public Result<Book> listBooks(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("Book2") // We only care about Books .setLimit(10) // Only show 10 at a time .setStartCursor(startCursor) // Where we left off .setOrderBy(OrderBy.asc(Book.TITLE)) // Use default Index "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 7
Source Project: getting-started-java Source File: DatastoreDao.java License: Apache License 2.0 | 6 votes |
@Override public Result<Book> listBooks(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("Book3") // We only care about Books .setLimit(10) // Only show 10 at a time .setStartCursor(startCursor) // Where we left off .setOrderBy(OrderBy.asc(Book.TITLE)) // Use default Index "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 8
Source Project: getting-started-java Source File: DatastoreDao.java License: Apache License 2.0 | 6 votes |
@Override public Result<Book> listBooks(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 .setLimit(10) // Only show 10 at a time .setStartCursor(startCursor) // Where we left off .setOrderBy(OrderBy.asc(Book.TITLE)) // Use default Index "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 9
Source Project: getting-started-java Source File: DatastoreDao.java License: 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 10
Source Project: java-docs-samples Source File: ConceptsTest.java License: Apache License 2.0 | 6 votes |
private Cursor cursorPaging(int pageSize, Cursor pageCursor) { // [START datastore_cursor_paging] EntityQuery.Builder queryBuilder = Query.newEntityQueryBuilder().setKind("Task") .setLimit(pageSize); if (pageCursor != null) { queryBuilder.setStartCursor(pageCursor); } QueryResults<Entity> tasks = datastore.run(queryBuilder.build()); while (tasks.hasNext()) { Entity task = tasks.next(); // do something with the task } Cursor nextPageCursor = tasks.getCursorAfter(); // [END datastore_cursor_paging] return nextPageCursor; }
Example 11
Source Project: spring-cloud-gcp Source File: GqlDatastoreQuery.java License: Apache License 2.0 | 5 votes |
private Object buildPageOrSlice(Object[] parameters, ParsedQueryWithTagsAndValues parsedQueryWithTagsAndValues, DatastoreResultsIterable found) { Pageable pageableParam = new ParametersParameterAccessor(getQueryMethod().getParameters(), parameters).getPageable(); List resultsList = found == null ? Collections.emptyList() : (List) StreamSupport.stream(found.spliterator(), false).collect(Collectors.toList()); Cursor cursor = found != null ? found.getCursor() : null; Slice result = isPageQuery() ? buildPage(pageableParam, parsedQueryWithTagsAndValues, cursor, resultsList) : buildSlice(pageableParam, parsedQueryWithTagsAndValues, cursor, resultsList); return processRawObjectForProjection(result); }
Example 12
Source Project: spring-cloud-gcp Source File: GqlDatastoreQuery.java License: Apache License 2.0 | 5 votes |
private Slice buildSlice(Pageable pageableParam, ParsedQueryWithTagsAndValues parsedQueryWithTagsAndValues, Cursor cursor, List resultsList) { GqlQuery nextQuery = parsedQueryWithTagsAndValues.bindArgsToGqlQuery(cursor, 1); DatastoreResultsIterable<?> next = this.datastoreOperations.queryKeysOrEntities(nextQuery, this.entityType); Pageable pageable = DatastorePageable.from(pageableParam, cursor, null); return new SliceImpl(resultsList, pageable, next.iterator().hasNext()); }
Example 13
Source Project: spring-cloud-gcp Source File: GqlDatastoreQuery.java License: Apache License 2.0 | 5 votes |
private Page buildPage(Pageable pageableParam, ParsedQueryWithTagsAndValues parsedQueryWithTagsAndValues, Cursor cursor, List resultsList) { Long count = pageableParam instanceof DatastorePageable ? ((DatastorePageable) pageableParam).getTotalCount() : null; if (count == null) { GqlQuery nextQuery = parsedQueryWithTagsAndValues.bindArgsToGqlQueryNoLimit(); DatastoreResultsIterable<?> next = this.datastoreOperations.queryKeysOrEntities(nextQuery, this.entityType); count = StreamSupport.stream(next.spliterator(), false).count(); } Pageable pageable = DatastorePageable.from(pageableParam, cursor, count); return new PageImpl(resultsList, pageable, count); }
Example 14
Source Project: spring-cloud-gcp Source File: SimpleDatastoreRepositoryTests.java License: Apache License 2.0 | 5 votes |
@Test public void findAllPageableCursor() { Cursor cursor = Cursor.copyFrom("abc".getBytes()); Pageable pageable = DatastorePageable.from(PageRequest.of(1, 5, Sort.Direction.DESC, "property1", "property2"), cursor, 10L); this.simpleDatastoreRepository.findAll(pageable); verify(this.datastoreTemplate, times(1)).findAll(eq(Object.class), eq(new DatastoreQueryOptions.Builder().setLimit(5).setOffset(5).setSort(Sort.by( new Sort.Order(Sort.Direction.DESC, "property1"), new Sort.Order(Sort.Direction.DESC, "property2"))).setCursor(cursor).build())); verify(this.datastoreTemplate, times(0)).count(any()); }
Example 15
Source Project: spring-cloud-gcp Source File: SimpleDatastoreRepositoryTests.java License: Apache License 2.0 | 5 votes |
@Test public void findAllByExamplePageCursor() { Example<Object> example = Example.of(new Object()); Sort sort = Sort.by("id"); Cursor cursor = Cursor.copyFrom("abc".getBytes()); doAnswer((invocationOnMock) -> new DatastoreResultsIterable(Arrays.asList(1, 2), cursor)) .when(this.datastoreTemplate).queryByExample(same(example), eq(new DatastoreQueryOptions.Builder().setLimit(2).setOffset(0).setSort(sort) .build())); doAnswer((invocationOnMock) -> new DatastoreResultsIterable(Arrays.asList(3, 4), null)) .when(this.datastoreTemplate).queryByExample(same(example), eq(new DatastoreQueryOptions.Builder().setLimit(2).setOffset(2).setSort(sort).setCursor(cursor) .build())); doAnswer((invocationOnMock) -> new DatastoreResultsIterable(Arrays.asList(1, 2, 3, 4, 5), null)) .when(this.datastoreTemplate).keyQueryByExample(same(example), isNull()); Page<Object> result = this.simpleDatastoreRepository.findAll(example, PageRequest.of(0, 2, sort)); assertThat(result).containsExactly(1, 2); assertThat(result.getTotalElements()).isEqualTo(5); Page<Object> resultNext = this.simpleDatastoreRepository.findAll(example, result.getPageable().next()); assertThat(resultNext).containsExactly(3, 4); assertThat(resultNext.getTotalElements()).isEqualTo(5); verify(this.datastoreTemplate, times(1)).queryByExample(same(example), eq(new DatastoreQueryOptions.Builder().setLimit(2).setOffset(0).setSort(sort) .build())); verify(this.datastoreTemplate, times(1)).queryByExample(same(example), eq(new DatastoreQueryOptions.Builder().setLimit(2).setOffset(2).setSort(sort).setCursor(cursor) .build())); verify(this.datastoreTemplate, times(1)).keyQueryByExample(same(example), isNull()); }
Example 16
Source Project: catatumbo Source File: DefaultDatastoreMetadata.java License: Apache License 2.0 | 5 votes |
@Override public QueryResponse<String> getNamespaces(DatastoreCursor fromCursor, int limit) { try { String query = "SELECT __key__ FROM " + ENTITY_NAMESPACES + " ORDER BY __key__"; if (limit > 0) { query += " LIMIT @Limit"; } query += " OFFSET @Offset"; GqlQuery.Builder<Key> gqlQueryBuilder = Query.newGqlQueryBuilder(ResultType.KEY, query); if (limit > 0) { gqlQueryBuilder.setBinding("Limit", limit); } gqlQueryBuilder.setBinding("Offset", Cursor.fromUrlSafe(fromCursor.getEncoded())); GqlQuery<Key> gqlQuery = gqlQueryBuilder.build(); Datastore datastore = entityManager.getDatastore(); QueryResults<Key> results = datastore.run(gqlQuery); DefaultQueryResponse<String> response = new DefaultQueryResponse<>(); List<String> namespaces = new ArrayList<>(Math.max(limit, 50)); response.setStartCursor(new DefaultDatastoreCursor(results.getCursorAfter().toUrlSafe())); while (results.hasNext()) { Key key = results.next(); String name = key.getName(); namespaces.add(name == null ? "" : name); } response.setResults(namespaces); response.setEndCursor(new DefaultDatastoreCursor(results.getCursorAfter().toUrlSafe())); return response; } catch (DatastoreException exp) { throw new EntityManagerException(exp); } }
Example 17
Source Project: java-docs-samples Source File: ConceptsTest.java License: Apache License 2.0 | 5 votes |
@Test public void testCursorPaging() { setUpQueryTests(); datastore.put(testEntity); Cursor nextPageCursor = cursorPaging(1, null); assertNotNull(nextPageCursor); nextPageCursor = cursorPaging(1, nextPageCursor); assertNotNull(nextPageCursor); }
Example 18
Source Project: spring-cloud-gcp Source File: PartTreeDatastoreQuery.java License: Apache License 2.0 | 4 votes |
private StructuredQuery applyQueryBody(Object[] parameters, Builder builder, boolean total, boolean singularResult, Cursor cursor) { ParameterAccessor paramAccessor = new ParametersParameterAccessor(getQueryMethod().getParameters(), parameters); if (this.tree.hasPredicate()) { applySelectWithFilter(parameters, builder); } Pageable pageable = paramAccessor.getPageable(); Integer limit = null; Integer offset = null; if (singularResult || this.tree.isExistsProjection()) { limit = 1; } else if (this.tree.isLimiting()) { limit = this.tree.getMaxResults(); } if (!singularResult && !total && pageable.isPaged()) { limit = pageable.getPageSize(); } Sort sort = this.tree.getSort(); if (getQueryMethod().getParameters().hasPageableParameter()) { sort = sort.and(pageable.getSort()); } if (getQueryMethod().getParameters().hasSortParameter()) { sort = sort.and(paramAccessor.getSort()); } if (pageable.isPaged() && !total) { offset = (int) pageable.getOffset(); } Cursor cursorToApply = null; if (cursor != null) { cursorToApply = cursor; } else if (pageable instanceof DatastorePageable) { cursorToApply = ((DatastorePageable) pageable).toCursor(); } DatastoreTemplate.applyQueryOptions( builder, new DatastoreQueryOptions.Builder().setLimit(limit).setOffset(offset).setSort(sort) .setCursor(cursorToApply).build(), this.datastorePersistentEntity); return builder.build(); }
Example 19
Source Project: spring-cloud-gcp Source File: PartTreeDatastoreQuery.java License: Apache License 2.0 | 4 votes |
ExecutionResult(Object result, Cursor cursor) { this.payload = result; this.cursor = cursor; }
Example 20
Source Project: spring-cloud-gcp Source File: PartTreeDatastoreQuery.java License: Apache License 2.0 | 4 votes |
public Cursor getCursor() { return this.cursor; }
Example 21
Source Project: spring-cloud-gcp Source File: DatastorePageable.java License: Apache License 2.0 | 4 votes |
DatastorePageable(Pageable pageable, Cursor cursor, Long totalCount) { this(pageable, cursor.toUrlSafe(), totalCount); }
Example 22
Source Project: spring-cloud-gcp Source File: DatastorePageable.java License: Apache License 2.0 | 4 votes |
public static Pageable from(Pageable pageable, Cursor cursor, Long totalCount) { return from(pageable, cursor == null ? null : cursor.toUrlSafe(), totalCount); }
Example 23
Source Project: spring-cloud-gcp Source File: DatastorePageable.java License: Apache License 2.0 | 4 votes |
public Cursor toCursor() { return this.urlSafeCursor == null ? null : Cursor.fromUrlSafe(this.urlSafeCursor); }
Example 24
Source Project: spring-cloud-gcp Source File: GqlDatastoreQuery.java License: Apache License 2.0 | 4 votes |
private GqlQuery<? extends BaseEntity> bindArgsToGqlQuery(Cursor newCursor, int newLimit) { this.params.set(this.cursorPosition, newCursor); this.params.set(this.limitPosition, newLimit); return bindArgsToGqlQuery(); }
Example 25
Source Project: spring-cloud-gcp Source File: SimpleDatastoreRepository.java License: Apache License 2.0 | 4 votes |
private static Cursor getCursor(Pageable pageable) { return pageable instanceof DatastorePageable ? ((DatastorePageable) pageable).toCursor() : null; }
Example 26
Source Project: spring-cloud-gcp Source File: DatastoreResultsCollection.java License: Apache License 2.0 | 4 votes |
DatastoreResultsCollection(Collection<T> collection, Cursor cursor) { this.collection = collection; this.cursor = cursor; }
Example 27
Source Project: spring-cloud-gcp Source File: DatastoreResultsCollection.java License: Apache License 2.0 | 4 votes |
public Cursor getCursor() { return this.cursor; }
Example 28
Source Project: spring-cloud-gcp Source File: DatastoreQueryOptions.java License: Apache License 2.0 | 4 votes |
private DatastoreQueryOptions(Integer limit, Integer offset, Sort sort, Cursor cursor) { this.limit = limit; this.offset = offset; this.sort = sort; this.cursor = cursor; }
Example 29
Source Project: spring-cloud-gcp Source File: DatastoreQueryOptions.java License: Apache License 2.0 | 4 votes |
public Cursor getCursor() { return this.cursor; }
Example 30
Source Project: spring-cloud-gcp Source File: DatastoreQueryOptions.java License: Apache License 2.0 | 4 votes |
public Builder setCursor(Cursor cursor) { this.cursor = cursor; return this; }