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

New repositories approach #16

Merged
merged 12 commits into from Dec 1, 2015
Expand Up @@ -31,7 +31,7 @@ public class PaginatedCollection<T> {
private int limit;

public PaginatedCollection() {
this(Collections.EMPTY_LIST);
this(Collections.<T>emptyList());
}

public PaginatedCollection(Collection<T> items) {
Expand Down

This file was deleted.

@@ -0,0 +1,121 @@
/*
* The MIT License (MIT) Copyright (c) 2014 karumi Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to
* do so, subject to the following conditions: The above copyright notice and this permission
* notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE
* IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.karumi.rosie.repository;

import com.karumi.rosie.repository.datasource.Identifiable;
import com.karumi.rosie.repository.datasource.PaginatedCacheDataSource;
import com.karumi.rosie.repository.datasource.PaginatedReadableDataSource;
import com.karumi.rosie.repository.policy.ReadPolicy;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;
import java.util.LinkedList;

/**
* Paginated version of {@link RosieRepository}. It adds methods for retrieving paginated data
*/
public class PaginatedRosieRepository<K, V extends Identifiable<K>> extends RosieRepository<K, V> {

private final Collection<PaginatedReadableDataSource<V>> paginatedReadableDataSources =
new LinkedList<>();
private final Collection<PaginatedCacheDataSource<K, V>> paginatedCacheDataSources =
new LinkedList<>();

public PaginatedCollection<V> getPage(int offset, int limit) throws Exception {
return getPage(offset, limit, ReadPolicy.READ_ALL_AND_POPULATE);
}

public PaginatedCollection<V> getPage(int offset, int limit, EnumSet<ReadPolicy> policies)
throws Exception {
PaginatedCollection<V> values = null;

if (policies.contains(ReadPolicy.USE_CACHE)) {
values = getPaginatedValuesFromCaches(offset, limit);
}

if (values == null && policies.contains(ReadPolicy.USE_READABLE)) {
values = getPaginatedValuesFromReadables(offset, limit);
}

if (values != null && policies.contains(ReadPolicy.POPULATE_CACHE)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can ReadPolicy.POPULATE_CACHE be used without any other policy?. As far as I read we should not create a USE_READABLE_POPULATE_CACHE because if we do that we should also add another policies with the same meaning. Maybe could be enough just checking the policies set contains at least another politcy.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand what you are saying, basically CACHE_POLICY & READ_POLICY are two different things and some combinations doesn't make any sense. We can hide it in another class and let only the values that are correct visible. I'll think about it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can create an issue to implement this in the future if you want ;)

populatePaginatedCaches(offset, limit, values);
}

return values;
}

@SafeVarargs
protected final <R extends PaginatedReadableDataSource<V>> void addPaginatedReadables(
R... readables) {
this.paginatedReadableDataSources.addAll(Arrays.asList(readables));
}

@SafeVarargs
protected final <R extends PaginatedCacheDataSource<K, V>> void addPaginatedCaches(R... caches) {
this.paginatedCacheDataSources.addAll(Arrays.asList(caches));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two methods are related to the repository dependencies, shouldn't we move them to the top of the class?


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

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

if (values == null) {
continue;
}

if (areValidValues(values, cacheDataSource)) {
break;
}

cacheDataSource.deleteAll();
values = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking in the body of this method maybe we could improve it to do it more readable:

if (values == null) {
   continue;
} else if (!areValid(values, cacheDataSource)) {
   cacheDataSource.deleteAll();
   values = null;
}

If I'm not wrong this should be equivalent.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still missing the break call to read only the first data source and stop iterating once we have a valid value, this should be equivalent:

      if (values != null) {
        if (areValidValues(values, cacheDataSource)) {
          break;
        } else {
          cacheDataSource.deleteAll();
          values = null;
        }
      }

What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok :)

}

return values;
}

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

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

if (values != null) {
break;
}
}

return values;
}

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

private boolean areValidValues(PaginatedCollection<V> values,
PaginatedCacheDataSource<K, V> cacheDataSource) {
boolean areValidValues = false;
for (V value : values.getItems()) {
areValidValues |= cacheDataSource.isValid(value);
}
return areValidValues;
}
}