New repositories approach #16
Changes from 6 commits
053bd40
1c7fadf
b174d4d
536bfa6
b532157
e180a40
bbc336e
fa1c71d
1170ca1
39e337b
cabd480
ed761b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) { | ||
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)); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still missing the if (values != null) {
if (areValidValues(values, cacheDataSource)) {
break;
} else {
cacheDataSource.deleteAll();
values = null;
}
} What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
There was a problem hiding this comment.
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 aUSE_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.There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 ;)