usergrid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sfeld...@apache.org
Subject [2/2] incubator-usergrid git commit: change cursor to offset
Date Tue, 21 Apr 2015 15:54:00 GMT
change cursor to offset


Project: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/commit/f130b7ce
Tree: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/tree/f130b7ce
Diff: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/diff/f130b7ce

Branch: refs/heads/USERGRID-578
Commit: f130b7ceaf1cfcaa9737ba4ddebfa42ba04581b1
Parents: a2d64a7
Author: Shawn Feldman <sfeldman@apache.org>
Authored: Tue Apr 21 09:50:52 2015 -0600
Committer: Shawn Feldman <sfeldman@apache.org>
Committed: Tue Apr 21 09:50:52 2015 -0600

----------------------------------------------------------------------
 .../index/ApplicationEntityIndex.java           |  11 +-
 .../persistence/index/CandidateResults.java     |  30 ++---
 .../impl/EsApplicationEntityIndexImpl.java      | 133 +------------------
 .../persistence/index/impl/EntityIndexTest.java |  15 +--
 4 files changed, 25 insertions(+), 164 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/f130b7ce/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/ApplicationEntityIndex.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/ApplicationEntityIndex.java
b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/ApplicationEntityIndex.java
index b06cefe..e9374c4 100644
--- a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/ApplicationEntityIndex.java
+++ b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/ApplicationEntityIndex.java
@@ -45,16 +45,7 @@ public interface ApplicationEntityIndex {
                              final int limit );
 
     CandidateResults search( final SearchEdge searchEdge, final SearchTypes searchTypes,
final String query,
-                             final int limit, final int from );
-
-    /**
-     * Get next page of results from a previous cursor.  Note that limit used here should
be the same limit as the initial
-     * Cursor.  Failure to do so can result in strange cursor behavior on the response.
-     *
-     * @param cursor The cursor from the original search
-     * @return The next page of candidate results
-     */
-    CandidateResults getNextPage( final String cursor);
+                             final int limit, final int offset );
 
     /**
      * delete all application records

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/f130b7ce/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/CandidateResults.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/CandidateResults.java
b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/CandidateResults.java
index 5dc5502..716c7d8 100644
--- a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/CandidateResults.java
+++ b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/CandidateResults.java
@@ -27,13 +27,9 @@ package org.apache.usergrid.persistence.index;
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
+import com.google.common.base.Optional;
 
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
-import org.apache.usergrid.persistence.model.util.UUIDGenerator;
-
-import static org.apache.usergrid.persistence.index.utils.StringUtils.sanitizeUUID;
 
 
 /**
@@ -42,9 +38,7 @@ import static org.apache.usergrid.persistence.index.utils.StringUtils.sanitizeUU
  */
 public class CandidateResults implements Iterable<CandidateResult> {
 
-    private static final Logger log = LoggerFactory.getLogger( CandidateResults.class );
-
-    private String cursor = null;
+    private Optional<Integer> offset = null;
 
 
     private final List<CandidateResult> candidates;
@@ -56,27 +50,27 @@ public class CandidateResults implements Iterable<CandidateResult>
{
     ) {
         this.candidates = candidates;
         this.getFieldMappings = getFieldMappings;
+        offset = Optional.absent();
     }
 
 
-    public String initializeCursor(){
-        cursor = sanitizeUUID( UUIDGenerator.newTimeUUID() );
-        return cursor;
+    public void initializeCursor(int offset){
+        this.offset = Optional.of(offset);
     }
 
 
-    public boolean hasCursor() {
-        return cursor != null;
+    public boolean hasOffset() {
+        return offset.isPresent();
     }
 
 
-    public String getCursor() {
-        return cursor;
+    public Optional<Integer> getOffset() {
+        return offset;
     }
 
 
-    public void setCursor(String cursor) {
-        this.cursor = cursor;
+    public void setOffset(int offset) {
+        this.offset = Optional.of(offset);
     }
 
 
@@ -102,7 +96,7 @@ public class CandidateResults implements Iterable<CandidateResult>
{
      * @return
      */
     public CandidateResult get(int index){
-        return candidates.get( index );
+        return candidates.get(index);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/f130b7ce/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/impl/EsApplicationEntityIndexImpl.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/impl/EsApplicationEntityIndexImpl.java
b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/impl/EsApplicationEntityIndexImpl.java
index 3a28549..672f090 100644
--- a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/impl/EsApplicationEntityIndexImpl.java
+++ b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/impl/EsApplicationEntityIndexImpl.java
@@ -153,7 +153,7 @@ public class EsApplicationEntityIndexImpl implements ApplicationEntityIndex
{
     }
 
     public CandidateResults search( final SearchEdge searchEdge, final SearchTypes searchTypes,
final String query,
-                                    final int limit, final int from ) {
+                                    final int limit, final int offset ) {
 
         IndexValidationUtils.validateSearchEdge( searchEdge );
         Preconditions.checkNotNull( searchTypes, "searchTypes cannot be null" );
@@ -165,7 +165,7 @@ public class EsApplicationEntityIndexImpl implements ApplicationEntityIndex
{
 
         final ParsedQuery parsedQuery = ParsedQueryBuilder.build( query );
 
-        final SearchRequestBuilder srb = searchRequest.getBuilder( searchEdge, searchTypes,
parsedQuery, limit, from );
+        final SearchRequestBuilder srb = searchRequest.getBuilder( searchEdge, searchTypes,
parsedQuery, limit, offset );
 
         if ( logger.isDebugEnabled() ) {
             logger.debug( "Searching index (read alias): {}\n  nodeId: {}, edgeType: {},
 \n type: {}\n   query: {} ",
@@ -186,34 +186,7 @@ public class EsApplicationEntityIndexImpl implements ApplicationEntityIndex
{
         }
         failureMonitor.success();
 
-        return parseResults( searchResponse, searchEdge, searchTypes, parsedQuery, limit,
from );
-    }
-
-
-    public CandidateResults getNextPage( final String cursor ) {
-        Preconditions.checkNotNull(cursor, "cursor is a required argument");
-
-        String userCursorString = cursor;
-
-        //sanitiztion, we  probably shouldn't do this here, but in the caller
-        if ( userCursorString.startsWith( "\"" ) ) {
-            userCursorString = userCursorString.substring( 1 );
-        }
-        if ( userCursorString.endsWith( "\"" ) ) {
-            userCursorString = userCursorString.substring( 0, userCursorString.length() -
1 );
-        }
-
-        //now get the cursor from the map  and validate
-        final String queryStateString = mapManager.getString( userCursorString );
-
-
-        if(queryStateString == null){
-            throw new QueryException( String.format("Could not find a cursor for the value
'%s' ", userCursorString ));
-        }
-
-        //parse the query state
-        final QueryState queryState = QueryState.fromSerialized( queryStateString );
-        return search(queryState.searchEdge,queryState.searchTypes,queryState.ql,queryState.limit,queryState.from);
+        return parseResults( searchResponse, parsedQuery, limit, offset );
     }
 
 
@@ -271,8 +244,7 @@ public class EsApplicationEntityIndexImpl implements ApplicationEntityIndex
{
     /**
      * Parse the results and return the canddiate results
      */
-    private CandidateResults parseResults( final SearchResponse searchResponse, final SearchEdge
searchEdge,
-                                           final SearchTypes searchTypes, final ParsedQuery
query,
+    private CandidateResults parseResults( final SearchResponse searchResponse, final ParsedQuery
query,
                                            final int limit, final int from ) {
 
         final SearchHits searchHits = searchResponse.getHits();
@@ -295,106 +267,11 @@ public class EsApplicationEntityIndexImpl implements ApplicationEntityIndex
{
         // >= seems odd.  However if we get an overflow, we need to account for it.
         if (  length >= limit ) {
 
-            final String cursor = candidateResults.initializeCursor();
-
-            //now set this into our map module
-            final int minutes = indexFig.getQueryCursorTimeout();
-            //just truncate it, we'll never hit a long value anyway
-
-            final int storageSeconds = ( int ) TimeUnit.MINUTES.toSeconds( minutes );
-
-            final QueryState state = new QueryState( query.getOriginalQuery(), searchEdge,
searchTypes, limit, from );
-
-            final String queryStateSerialized = state.serialize();
-
-            mapManager.putString(cursor, queryStateSerialized, storageSeconds);
+            candidateResults.initializeCursor(from);
 
         }
 
         return candidateResults;
     }
 
-
-    /**
-     * Class to encapsulate our serialized state
-     */
-    private static final class QueryState implements Serializable{
-
-
-        private static final JsonFactory SMILE_FACTORY = new JsonFactory();
-
-        private static final ObjectMapper MAPPER = new ObjectMapper( SMILE_FACTORY );
-
-        /**
-         * Our reserved character for constructing our storage string
-         */
-        private static final String STORAGE_DELIM = "_ugdelim_";
-
-        private final String ql;
-
-        private final SearchEdge searchEdge;
-        private final SearchTypes searchTypes;
-        private final int limit;
-        private final int from;
-
-
-        private QueryState( final String ql,final SearchEdge searchEdge, final  SearchTypes
searchTypes, final int limit, final int from ) {
-            this.ql = ql;
-            this.searchEdge = searchEdge;
-            this.searchTypes = searchTypes;
-            this.limit = limit;
-            this.from = from;
-        }
-
-
-        /**
-         * Factory to create an instance of our state from the serialized string
-         */
-        public static QueryState fromSerialized( final String input ) {
-
-            final String[] parts = input.split(STORAGE_DELIM);
-
-            Preconditions.checkArgument(parts != null && parts.length == 7,
-                "there must be 5 parts to the serialized query state");
-            try {
-
-                String edgeName = parts[1];
-                Id edgeId = MAPPER.readValue(parts[2],SimpleId.class);
-                SearchEdge.NodeType nodeType = SearchEdge.NodeType.valueOf(parts[3]);
-
-                String[] searchTypes = MAPPER.readValue(parts[4],String[].class);
-                SearchTypes types = searchTypes.length == 0 ? SearchTypes.allTypes() : SearchTypes.fromTypes(searchTypes);
-                return new QueryState(parts[0], new SearchEdgeImpl(edgeId,edgeName,nodeType),types
, Integer.parseInt(parts[5]), Integer.parseInt(parts[6]));
-            } catch (Exception jpe) {
-                throw new RuntimeException(jpe);
-            }
-        }
-
-
-        public String serialize() {
-            try {
-
-                StringBuilder storageString = new StringBuilder();
-
-                storageString.append(ql).append(STORAGE_DELIM);
-
-                String edge =searchEdge.getEdgeName();
-                storageString.append(edge).append(STORAGE_DELIM);
-                String nodeid = MAPPER.writeValueAsString(searchEdge.getNodeId());
-                storageString.append(nodeid).append(STORAGE_DELIM);
-                String nodeType = searchEdge.getNodeType().name();
-                storageString.append(nodeType).append(STORAGE_DELIM);
-                String types = MAPPER.writeValueAsString(searchTypes.getTypes());
-                storageString.append(types).append(STORAGE_DELIM);
-
-                storageString.append(limit).append(STORAGE_DELIM);
-
-                storageString.append(from+limit);
-
-                return storageString.toString();
-            }catch (Exception jpe){
-                throw new RuntimeException(jpe);
-            }
-        }
-    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/f130b7ce/stack/corepersistence/queryindex/src/test/java/org/apache/usergrid/persistence/index/impl/EntityIndexTest.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/queryindex/src/test/java/org/apache/usergrid/persistence/index/impl/EntityIndexTest.java
b/stack/corepersistence/queryindex/src/test/java/org/apache/usergrid/persistence/index/impl/EntityIndexTest.java
index cd61818..f0a1630 100644
--- a/stack/corepersistence/queryindex/src/test/java/org/apache/usergrid/persistence/index/impl/EntityIndexTest.java
+++ b/stack/corepersistence/queryindex/src/test/java/org/apache/usergrid/persistence/index/impl/EntityIndexTest.java
@@ -28,6 +28,7 @@ import java.util.UUID;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.atomic.AtomicLong;
 
+import com.google.common.base.Optional;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -699,23 +700,21 @@ public class EntityIndexTest extends BaseIT {
         final int expectedPages = size / limit;
 
 
-        String cursor = null;
+        Optional<Integer> offset = Optional.absent();
         UUID lastId = null;
         final String query = "select * where mytype='"+myType+"' order by ordinal asc";
         int i ;
         for ( i=0; i < expectedPages; i++ ) {
-            //**
-
-            final CandidateResults results = cursor == null
-                ? entityIndex.search( indexEdge, SearchTypes.allTypes(), query, limit, i*
limit )
-                : entityIndex.getNextPage(cursor);
+            final CandidateResults results = !offset.isPresent()
+                ? entityIndex.search( indexEdge, SearchTypes.allTypes(), query, limit )
+                : entityIndex.search(indexEdge, SearchTypes.allTypes(), query, limit, i *
limit);
 
             assertEquals(limit, results.size());
 
             int ordinal = 0;//i == 0 ? 0 : 1;
             assertNotEquals("Scroll matches last item from previous page",lastId, results.get(ordinal).getId().getUuid());
             lastId = results.get(limit -1).getId().getUuid();
-            cursor = results.getCursor();
+            offset = results.getOffset();
             assertEquals("Failed on page "+i ,results.get( ordinal ).getId(), entityIds.get(
i*limit ) );
         }
 
@@ -723,7 +722,7 @@ public class EntityIndexTest extends BaseIT {
         final CandidateResults results = entityIndex.search(indexEdge, SearchTypes.allTypes(),
query, limit, i * limit);
 
         assertEquals( 0, results.size() );
-        assertNull( results.getCursor() );
+        assertFalse(results.hasOffset());
     }
 
 


Mime
View raw message