Skip to content
This repository has been archived by the owner on Jan 9, 2020. It is now read-only.

Commit

Permalink
Merge pull request #29 from Karumi/use-page-object-for-pagination
Browse files Browse the repository at this point in the history
Replace offset + limit by Page class
  • Loading branch information
pedrovgs committed Dec 18, 2015
2 parents 4cba043 + c582418 commit 20c6148
Show file tree
Hide file tree
Showing 16 changed files with 165 additions and 114 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,9 @@ PaginatedRosieRepository<Key, Value> repository = /*...*/;
// Get a value by its key just as with a regular repoitory
Value value = repository.getByKey(key);
// Get a page
page = repository.getPage(offset, limit);
page = repository.getPage(Page.withOffsetAndLimit(offset, limit));
// Get a page using only the cache data source
page = repository.getPage(offset, limit, ReadPolicy.CACHE_ONLY);
page = repository.getPage(Page.withOffsetAndLimit(offset, limit), ReadPolicy.CACHE_ONLY);
```


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.karumi.rosie.repository;

import com.karumi.rosie.repository.datasource.paginated.Page;
import java.util.Collection;
import java.util.Collections;

Expand All @@ -27,8 +28,7 @@ public class PaginatedCollection<T> {

private Collection<T> items;
private boolean hasMore;
private int offset;
private int limit;
private Page page;

public PaginatedCollection() {
this(Collections.<T>emptyList());
Expand All @@ -43,13 +43,8 @@ public PaginatedCollection setHasMore(boolean hasMore) {
return this;
}

public PaginatedCollection setOffset(int offset) {
this.offset = offset;
return this;
}

public PaginatedCollection setLimit(int limit) {
this.limit = limit;
public PaginatedCollection setPage(Page page) {
this.page = page;
return this;
}

Expand All @@ -61,11 +56,7 @@ public boolean hasMore() {
return hasMore;
}

public int getOffset() {
return offset;
}

public int getLimit() {
return limit;
public Page getPage() {
return page;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.karumi.rosie.repository;

import com.karumi.rosie.repository.datasource.Identifiable;
import com.karumi.rosie.repository.datasource.paginated.Page;
import com.karumi.rosie.repository.datasource.paginated.PaginatedCacheDataSource;
import com.karumi.rosie.repository.datasource.paginated.PaginatedReadableDataSource;
import com.karumi.rosie.repository.policy.ReadPolicy;
Expand Down Expand Up @@ -47,43 +48,43 @@ protected final <R extends PaginatedCacheDataSource<K, V>> void addPaginatedCach
}

/**
* Returns a page of values bounded by the offset and limit values.
* @param offset Index of the first item to be retrieved
* @param limit Number of elements that will be retrieved
* Returns a page of values bounded by the provided page.
*
* @param page Page to be retrieved
*/
public PaginatedCollection<V> getPage(int offset, int limit) {
return getPage(offset, limit, ReadPolicy.READ_ALL);
public PaginatedCollection<V> getPage(Page page) {
return getPage(page, ReadPolicy.READ_ALL);
}

/**
* Returns a page of values bounded by the offset and limit values.
* @param offset Index of the first item to be retrieved
* @param limit Number of elements that will be retrieved
* Returns a page of values bounded by the provided page.
*
* @param page Page to be retrieved
* @param policy Specifies how the value is going to be retrieved.
*/
public PaginatedCollection<V> getPage(int offset, int limit, ReadPolicy policy) {
public PaginatedCollection<V> getPage(Page page, ReadPolicy policy) {
PaginatedCollection<V> values = null;

if (policy.useCache()) {
values = getPaginatedValuesFromCaches(offset, limit);
values = getPaginatedValuesFromCaches(page);
}

if (values == null && policy.useReadable()) {
values = getPaginatedValuesFromReadables(offset, limit);
values = getPaginatedValuesFromReadables(page);
}

if (values != null) {
populatePaginatedCaches(offset, limit, values);
populatePaginatedCaches(page, values);
}

return values;
}

protected PaginatedCollection<V> getPaginatedValuesFromCaches(int offset, int limit) {
protected PaginatedCollection<V> getPaginatedValuesFromCaches(Page page) {
PaginatedCollection<V> values = null;

for (PaginatedCacheDataSource<K, V> cacheDataSource : paginatedCacheDataSources) {
values = cacheDataSource.getPage(offset, limit);
values = cacheDataSource.getPage(page);

if (values != null) {
if (areValidValues(values, cacheDataSource)) {
Expand All @@ -98,11 +99,11 @@ protected PaginatedCollection<V> getPaginatedValuesFromCaches(int offset, int li
return values;
}

protected PaginatedCollection<V> getPaginatedValuesFromReadables(int offset, int limit) {
protected PaginatedCollection<V> getPaginatedValuesFromReadables(Page page) {
PaginatedCollection<V> values = null;

for (PaginatedReadableDataSource<V> readable : paginatedReadableDataSources) {
values = readable.getPage(offset, limit);
values = readable.getPage(page);

if (values != null) {
break;
Expand All @@ -112,9 +113,9 @@ protected PaginatedCollection<V> getPaginatedValuesFromReadables(int offset, int
return values;
}

protected void populatePaginatedCaches(int offset, int limit, PaginatedCollection<V> values) {
protected void populatePaginatedCaches(Page page, PaginatedCollection<V> values) {
for (PaginatedCacheDataSource<K, V> cacheDataSource : paginatedCacheDataSources) {
cacheDataSource.addOrUpdatePage(offset, limit, values.getItems(), values.hasMore());
cacheDataSource.addOrUpdatePage(page, values.getItems(), values.hasMore());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@ public class EmptyPaginatedCacheDataSource<K, V extends Identifiable<K>>
return false;
}

@Override public PaginatedCollection<V> getPage(int offset, int limit) {
@Override public PaginatedCollection<V> getPage(Page page) {
return null;
}

@Override
public PaginatedCollection<V> addOrUpdatePage(int offset, int limit, Collection<V> values,
boolean hasMore) {
public PaginatedCollection<V> addOrUpdatePage(Page page, Collection<V> values, boolean hasMore) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,28 @@ public InMemoryPaginatedCacheDataSource(TimeProvider timeProvider, long ttlInMil
this.items = new ArrayList<>();
}

@Override public PaginatedCollection<V> getPage(int offset, int limit) {
@Override public PaginatedCollection<V> getPage(Page page) {
List<V> result = new LinkedList<>();

int offset = page.getOffset();
int limit = page.getLimit();

for (int i = offset; i < items.size() && i < offset + limit; i++) {
V value = items.get(i);
result.add(value);
}
PaginatedCollection<V> paginatedCollection = new PaginatedCollection<>(result);
paginatedCollection.setOffset(offset);
paginatedCollection.setLimit(limit);
paginatedCollection.setPage(page);
paginatedCollection.setHasMore(offset + limit < items.size() || this.hasMore);
return paginatedCollection;
}

@Override
public PaginatedCollection<V> addOrUpdatePage(int offset, int limit, Collection<V> values,
boolean hasMore) {
public PaginatedCollection<V> addOrUpdatePage(Page page, Collection<V> values, boolean hasMore) {
this.items.addAll(values);
this.hasMore = hasMore;
PaginatedCollection<V> paginatedCollection = new PaginatedCollection<>(values);
paginatedCollection.setOffset(offset);
paginatedCollection.setLimit(limit);
paginatedCollection.setPage(page);
paginatedCollection.setHasMore(hasMore);
lastItemsUpdate = timeProvider.currentTimeMillis();
return paginatedCollection;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2015 Karumi.
*
* 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 com.karumi.rosie.repository.datasource.paginated;

public final class Page {

private final int offset;
private final int limit;

private Page(int offset, int limit) {
this.offset = offset;
this.limit = limit;
}

public static Page withOffsetAndLimit(int offset, int limit) {
return new Page(offset, limit);
}

public int getOffset() {
return offset;
}

public int getLimit() {
return limit;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
*/
public interface PaginatedReadableDataSource<V> {
/**
* Returns a page of values bounded by the offset and limit values.
* @param offset Index of the first item to be retrieved
* @param limit Number of elements that will be retrieved
* Returns a page of values bounded by the provided page.
*
* @param page page to be retrieved
*/
PaginatedCollection<V> getPage(int offset, int limit);
PaginatedCollection<V> getPage(Page page);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ public interface PaginatedWriteableDataSource<K, V extends Identifiable<K>> {
/**
* Adds or update a page of values into this data source.
*
* @param offset Index of the first item to be retrieved
* @param limit Number of elements that will be retrieved
* @param page Page to be stored
* @param values Collection of values to be stored
* @param hasMore True whether the persisted page has more elements
*/
PaginatedCollection<V> addOrUpdatePage(int offset, int limit, Collection<V> values,
boolean hasMore);
PaginatedCollection<V> addOrUpdatePage(Page page, Collection<V> values, boolean hasMore);

/**
* Deletes all the pages stored in this data source.
Expand Down
Loading

0 comments on commit 20c6148

Please sign in to comment.