Repository: incubator-usergrid Updated Branches: refs/heads/two-dot-o-dev 1674dc07c -> 5594d7451
refactor cursor Project: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/commit/5594d745 Tree: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/tree/5594d745 Diff: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/diff/5594d745 Branch: refs/heads/two-dot-o-dev Commit: 5594d745193dc6d693b8587b10b076b3d265dd79 Parents: 1674dc0 Author: Shawn Feldman <sfeld...@apache.org> Authored: Tue Apr 21 15:51:01 2015 -0600 Committer: Shawn Feldman <sfeld...@apache.org> Committed: Tue Apr 21 15:51:01 2015 -0600 ---------------------------------------------------------------------- .../results/ElasticSearchQueryExecutor.java | 15 ++--- .../apache/usergrid/persistence/Results.java | 65 +++++++++++++------- 2 files changed, 51 insertions(+), 29 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/5594d745/stack/core/src/main/java/org/apache/usergrid/corepersistence/results/ElasticSearchQueryExecutor.java ---------------------------------------------------------------------- diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/results/ElasticSearchQueryExecutor.java b/stack/core/src/main/java/org/apache/usergrid/corepersistence/results/ElasticSearchQueryExecutor.java index 895b457..4a1e43a 100644 --- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/results/ElasticSearchQueryExecutor.java +++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/results/ElasticSearchQueryExecutor.java @@ -157,12 +157,7 @@ public class ElasticSearchQueryExecutor implements QueryExecutor { CandidateResults results = cursor.isPresent() ? entityIndex.search( indexScope, types, queryToExecute, query.getLimit() , cursor.get()) : entityIndex.search( indexScope, types, queryToExecute, query.getLimit()); - //set offset into query - if(results.getOffset().isPresent()) { - query.setOffset(results.getOffset().get()); - }else{ - query.clearOffset(); - } + return results; } @@ -187,7 +182,13 @@ public class ElasticSearchQueryExecutor implements QueryExecutor { //signal for post processing resultsLoader.postProcess(); - results.setCursor( query.getOffsetCursor() ); + //set offset into query + if(crs.getOffset().isPresent()) { + query.setOffset(crs.getOffset().get()); + }else{ + query.clearOffset(); + } + results.setCursorFromOffset( query.getOffset() ); //ugly and tight coupling, but we don't have a choice until we finish some refactoring results.setQueryExecutor( this ); http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/5594d745/stack/core/src/main/java/org/apache/usergrid/persistence/Results.java ---------------------------------------------------------------------- diff --git a/stack/core/src/main/java/org/apache/usergrid/persistence/Results.java b/stack/core/src/main/java/org/apache/usergrid/persistence/Results.java index e9e7a40..e572070 100644 --- a/stack/core/src/main/java/org/apache/usergrid/persistence/Results.java +++ b/stack/core/src/main/java/org/apache/usergrid/persistence/Results.java @@ -17,19 +17,17 @@ package org.apache.usergrid.persistence; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.ListIterator; -import java.util.Map; +import java.nio.ByteBuffer; +import java.util.*; +import java.util.Base64; import java.util.Map.Entry; -import java.util.Set; -import java.util.UUID; import javax.xml.bind.annotation.XmlRootElement; +import com.google.common.base.*; +import com.google.common.base.Optional; +import com.netflix.astyanax.serializers.IntegerSerializer; +import org.apache.commons.codec.binary.*; import org.apache.usergrid.corepersistence.results.QueryExecutor; import org.apache.usergrid.persistence.Query.Level; import org.apache.usergrid.utils.MapUtils; @@ -46,6 +44,7 @@ import static org.apache.usergrid.utils.ConversionUtils.bytes; @XmlRootElement public class Results implements Iterable<Entity> { + private static final IntegerSerializer INTEGER_SERIALIZER = IntegerSerializer.get(); Level level = Level.IDS; @@ -173,7 +172,7 @@ public class Results implements Iterable<Entity> { refs.add( ref( type, u ) ); } Results r = new Results(); - r.setRefs( refs ); + r.setRefs(refs); return r; } @@ -198,14 +197,14 @@ public class Results implements Iterable<Entity> { public static Results fromEntities( List<? extends Entity> l ) { Results r = new Results(); - r.setEntities( l ); + r.setEntities(l); return r; } public static Results fromEntity( Entity e ) { Results r = new Results(); - r.setEntity( e ); + r.setEntity(e); return r; } @@ -215,14 +214,14 @@ public class Results implements Iterable<Entity> { return fromEntity( ( Entity ) ref ); } Results r = new Results(); - r.setRef( ref ); + r.setRef(ref); return r; } public static Results fromData( Object obj ) { Results r = new Results(); - r.setData( obj ); + r.setData(obj); return r; } @@ -231,14 +230,14 @@ public class Results implements Iterable<Entity> { Results r = new Results(); List<AggregateCounterSet> l = new ArrayList<AggregateCounterSet>(); l.add( counters ); - r.setCounters( l ); + r.setCounters(l); return r; } public static Results fromCounters( List<AggregateCounterSet> counters ) { Results r = new Results(); - r.setCounters( counters ); + r.setCounters(counters); return r; } @@ -246,7 +245,7 @@ public class Results implements Iterable<Entity> { @SuppressWarnings("unchecked") public static Results fromConnections( List<? extends ConnectionRef> connections ) { Results r = new Results(); - r.setConnections( ( List<ConnectionRef> ) connections, true ); + r.setConnections((List<ConnectionRef>) connections, true); return r; } @@ -254,7 +253,7 @@ public class Results implements Iterable<Entity> { @SuppressWarnings("unchecked") public static Results fromConnections( List<? extends ConnectionRef> connections, boolean forward ) { Results r = new Results(); - r.setConnections( ( List<ConnectionRef> ) connections, forward ); + r.setConnections((List<ConnectionRef>) connections, forward); return r; } @@ -427,7 +426,7 @@ public class Results implements Iterable<Entity> { } if ( ref != null ) { refs = new ArrayList<EntityRef>(); - refs.add( ref ); + refs.add(ref); return refs; } return new ArrayList<EntityRef>(); @@ -630,7 +629,7 @@ public class Results implements Iterable<Entity> { if ( types != null ) { return types; } - getEntityRefsByType( "entity" ); + getEntityRefsByType("entity"); if ( entitiesByType != null ) { types = entitiesByType.keySet(); } @@ -895,7 +894,7 @@ public class Results implements Iterable<Entity> { public Results findForProperty( String propertyName, Object propertyValue ) { - return findForProperty( propertyName, propertyValue, 1 ); + return findForProperty(propertyName, propertyValue, 1); } @@ -916,7 +915,7 @@ public class Results implements Iterable<Entity> { } } } - return Results.fromEntities( found ); + return Results.fromEntities(found); } @@ -1170,6 +1169,27 @@ public class Results implements Iterable<Entity> { return cursor; } + public Optional<Integer> getOffsetFromCursor() { + Optional<Integer> offset = Optional.absent(); + if(cursor != null && cursor.length() > 0){ + byte[] bytes = org.apache.commons.codec.binary.Base64.decodeBase64(cursor); + ByteBuffer buffer = ByteBuffer.wrap(bytes); + Integer number = INTEGER_SERIALIZER.fromByteBuffer(buffer); + offset = Optional.of(number); + } + return offset; + } + + public void setCursorFromOffset(Optional<Integer> offset) { + + if(offset.isPresent()){ + ByteBuffer buffer = INTEGER_SERIALIZER.toByteBuffer(offset.get()); + cursor = org.apache.commons.codec.binary.Base64.encodeBase64String(buffer.array()); + }else{ + cursor = null; + } + } + public boolean hasCursor() { return cursor != null && cursor.length() > 0; @@ -1177,6 +1197,7 @@ public class Results implements Iterable<Entity> { public void setCursor( String cursor ) { + this.cursor = cursor; }