Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(sample-service-cassandra): adding pagination to sample #224

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
package io.americanexpress.data.book.repository;

import io.americanexpress.data.book.entity.BookEntity;
import org.springframework.data.cassandra.core.query.CassandraPageRequest;
import org.springframework.data.cassandra.repository.AllowFiltering;
import org.springframework.data.cassandra.repository.ReactiveCassandraRepository;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Mono;

Expand All @@ -31,4 +34,7 @@ public interface BookRepository extends ReactiveCassandraRepository<BookEntity,

@AllowFiltering
Mono<BookEntity> findByTitle(String title);

Mono<Slice<BookEntity>> findAllBy(Pageable pageable);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2020 American Express Travel Related Services Company, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package io.americanexpress.service.book.rest.controller;

import io.americanexpress.service.book.rest.config.BookEndpoints;
import io.americanexpress.service.book.rest.model.ReadBookResponse;
import io.americanexpress.service.book.rest.model.ReadBookPaginatedRequest;
import io.americanexpress.service.book.rest.service.ReadPolyBookReactiveService;
import io.americanexpress.synapse.service.rest.controller.reactive.BaseReadPolyReactiveController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* {@code ReadPolyBookReactiveController} is the controller class for retrieving books from the Cassandra Book database.
*/
@RestController
@RequestMapping(BookEndpoints.BOOK_ENDPOINT)
public class ReadPolyBookReactiveController extends BaseReadPolyReactiveController<ReadBookPaginatedRequest, ReadBookResponse, ReadPolyBookReactiveService> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2020 American Express Travel Related Services Company, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package io.americanexpress.service.book.rest.model;

import io.americanexpress.synapse.service.rest.model.BasePaginatedServiceRequest;


/**
* {@code ReadBookPaginatedRequest} is the request object for reading book paginated.
*/
public class ReadBookPaginatedRequest extends BasePaginatedServiceRequest {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright 2020 American Express Travel Related Services Company, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package io.americanexpress.service.book.rest.service;

import io.americanexpress.data.book.entity.BookEntity;
import io.americanexpress.data.book.repository.BookRepository;
import io.americanexpress.service.book.rest.model.ReadBookResponse;
import io.americanexpress.service.book.rest.model.ReadBookPaginatedRequest;
import io.americanexpress.service.book.rest.service.helper.ReadBookResponseCreator;
import io.americanexpress.synapse.service.rest.model.PageInformation;
import io.americanexpress.synapse.service.rest.service.reactive.BaseReadPolyReactiveService;
import org.springframework.data.cassandra.core.query.CassandraPageRequest;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;

/**
* {@code ReadPolyBookReactiveService} is the service class for retrieving books from the Cassandra Book database.
*/
@Service
public class ReadPolyBookReactiveService extends BaseReadPolyReactiveService<ReadBookPaginatedRequest, ReadBookResponse> {

private final BookRepository bookRepository;

private Map<PageInformation, ByteBuffer> pageInformationPagingStateMap = new HashMap<>();


/**
* Instantiates a new Read poly book reactive service.
*
* @param bookRepository the book repository
*/
public ReadPolyBookReactiveService(BookRepository bookRepository) {
this.bookRepository = bookRepository;
}

@Override
protected Flux<ReadBookResponse> executeRead(HttpHeaders headers, ReadBookPaginatedRequest request) {
Flux<ReadBookResponse> readBookResponseFlux;
if(request.getPageInformation() != null) {
Pageable pageable = PageRequest.of(0, request.getPageInformation().getSize());
Pageable cassandraPageable = CassandraPageRequest.of(pageable, getPageState(request.getPageInformation()));
Mono<Slice<BookEntity>> bookEntityList = bookRepository.findAllBy(cassandraPageable);
bookEntityList.map(Slice::nextOrLastPageable).subscribe(nextPageable -> addPageState(request.getPageInformation(), (CassandraPageRequest) nextPageable));
return bookEntityList.flatMapMany(Flux::fromIterable).map(ReadBookResponseCreator::create);
}else {
readBookResponseFlux = bookRepository.findAll()
.map(ReadBookResponseCreator::create)
.switchIfEmpty(Flux.empty());
}
return readBookResponseFlux;
}
private ByteBuffer getPageState(PageInformation pageInformation) {
return pageInformationPagingStateMap.get(pageInformation);
}

private void addPageState(PageInformation pageInformation, CassandraPageRequest nextOrLastPageable) {
System.out.println("AddPageState");
if(nextOrLastPageable.getPagingState() != null ) {
pageInformation.setPage(pageInformation.getPage() + 1);
pageInformationPagingStateMap.put(pageInformation, nextOrLastPageable.getPagingState());
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2020 American Express Travel Related Services Company, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package io.americanexpress.service.book.rest.controller;

import io.americanexpress.service.book.rest.config.BookEndpoints;
import io.americanexpress.service.book.rest.model.ReadBookPaginatedRequest;
import io.americanexpress.service.book.rest.model.ReadBookResponse;
import io.americanexpress.service.book.rest.service.ReadPolyBookService;
import io.americanexpress.synapse.service.rest.controller.BaseReadPolyController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* {@code ReadPolyBookController} is the controller class for reading books in the Cassandra Book database.
*/
@RestController
@RequestMapping(BookEndpoints.BOOK_ENDPOINT)
public class ReadPolyBookController extends BaseReadPolyController<ReadBookPaginatedRequest, ReadBookResponse, ReadPolyBookService> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2020 American Express Travel Related Services Company, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package io.americanexpress.service.book.rest.model;

import io.americanexpress.synapse.service.rest.model.BasePaginatedServiceRequest;


/**
* {@code ReadBookPaginatedRequest} is the request object for reading book paginated.
*/
public class ReadBookPaginatedRequest extends BasePaginatedServiceRequest {

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import java.util.Optional;

/**
* {@code ReadBookService} is the service class for creating a book in the Cassandra Book database.
* {@code ReadBookService} is the service class for reading a book in the Cassandra Book database.
*/
@Service
public class ReadBookService extends BaseReadMonoService<ReadBookRequest, ReadBookResponse> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2020 American Express Travel Related Services Company, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package io.americanexpress.service.book.rest.service;

import io.americanexpress.data.book.entity.BookEntity;
import io.americanexpress.data.book.repository.BookRepository;
import io.americanexpress.service.book.rest.model.ReadBookPaginatedRequest;
import io.americanexpress.service.book.rest.model.ReadBookResponse;
import io.americanexpress.service.book.rest.service.helper.ReadBookResponseCreator;
import io.americanexpress.synapse.service.rest.model.PageInformation;
import io.americanexpress.synapse.service.rest.service.BaseReadPolyService;
import org.springframework.data.cassandra.core.query.CassandraPageRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;

import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* {@code ReadPolyBookService} is the service class for retrieving books from the Cassandra Book database.
*/
@Service
public class ReadPolyBookService extends BaseReadPolyService<ReadBookPaginatedRequest, ReadBookResponse> {

private final BookRepository bookRepository;

private Map<PageInformation, ByteBuffer> pageInformationPagingStateMap = new HashMap<>();

/**
* Instantiates a new Read poly book service.
*
* @param bookRepository the book repository
*/
public ReadPolyBookService(BookRepository bookRepository) {
this.bookRepository = bookRepository;
}

@Override
protected Page<ReadBookResponse> executeRead(HttpHeaders headers, ReadBookPaginatedRequest request) {
List<ReadBookResponse> readBookResponses;
if(request.getPageInformation() != null) {
Pageable pageable = PageRequest.of(0, request.getPageInformation().getSize());
Pageable cassandraPageable = CassandraPageRequest.of(pageable, getPageState(request.getPageInformation()));

Slice<BookEntity> bookEntitySlice = bookRepository.findAll(cassandraPageable);
addPageState(request.getPageInformation(), (CassandraPageRequest) bookEntitySlice.nextOrLastPageable());

readBookResponses = bookEntitySlice.getContent().stream().map(ReadBookResponseCreator::create).toList();
}else {
readBookResponses = bookRepository.findAll().stream().map(ReadBookResponseCreator::create).toList();
}
return new PageImpl<>(readBookResponses);
}

private ByteBuffer getPageState(PageInformation pageInformation) {
return pageInformationPagingStateMap.get(pageInformation);
}

private void addPageState(PageInformation pageInformation, CassandraPageRequest nextOrLastPageable) {
if(nextOrLastPageable.getPagingState() != null ) {
pageInformation.setPage(pageInformation.getPage() + 1);
pageInformationPagingStateMap.put(pageInformation, nextOrLastPageable.getPagingState());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
package io.americanexpress.synapse.service.rest.model;

import java.util.Objects;

/**
* {@code PageInformation} class specifies the parameters for a service request,
* limiting the results to a subset of how many (size) and on which page (page).
Expand Down Expand Up @@ -65,4 +67,17 @@ public int getSize() {
public void setSize(int size) {
this.size = size;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PageInformation that = (PageInformation) o;
return page == that.page && size == that.size;
}

@Override
public int hashCode() {
return Objects.hash(page, size);
}
}