usergrid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From snoopd...@apache.org
Subject [3/3] git commit: Finished tests. Need to review code to remove duplicate inner search code
Date Tue, 04 Feb 2014 12:20:41 GMT
Finished tests.  Need to review code to remove duplicate inner search code

USERGRID-55


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

Branch: refs/heads/two-dot-o
Commit: 804e2df0e99fb9649cb5f43ae7287199e91ec311
Parents: 45e04ac
Author: Todd Nine <tnine@apigee.com>
Authored: Tue Feb 4 04:32:37 2014 -0700
Committer: Todd Nine <tnine@apigee.com>
Committed: Tue Feb 4 04:32:37 2014 -0700

----------------------------------------------------------------------
 .../impl/EdgeSerializationImpl.java             | 169 ++++++++++++++++---
 .../graph/serialization/util/EdgeUtils.java     |  20 +++
 .../serialization/EdgeSerializationTest.java    |   9 +-
 3 files changed, 173 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/804e2df0/stack/corepersistence/graph/src/main/java/org/apache/usergrid/persistence/graph/serialization/impl/EdgeSerializationImpl.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/graph/src/main/java/org/apache/usergrid/persistence/graph/serialization/impl/EdgeSerializationImpl.java
b/stack/corepersistence/graph/src/main/java/org/apache/usergrid/persistence/graph/serialization/impl/EdgeSerializationImpl.java
index 522d4c1..beb730a 100644
--- a/stack/corepersistence/graph/src/main/java/org/apache/usergrid/persistence/graph/serialization/impl/EdgeSerializationImpl.java
+++ b/stack/corepersistence/graph/src/main/java/org/apache/usergrid/persistence/graph/serialization/impl/EdgeSerializationImpl.java
@@ -52,11 +52,13 @@ import org.apache.usergrid.persistence.graph.serialization.util.EdgeHasher;
 import org.apache.usergrid.persistence.graph.serialization.util.EdgeUtils;
 import org.apache.usergrid.persistence.model.entity.Id;
 
+import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.inject.Singleton;
 import com.netflix.astyanax.Keyspace;
 import com.netflix.astyanax.MutationBatch;
 import com.netflix.astyanax.connectionpool.exceptions.ConnectionException;
+import com.netflix.astyanax.model.AbstractComposite;
 import com.netflix.astyanax.model.Column;
 import com.netflix.astyanax.model.CompositeBuilder;
 import com.netflix.astyanax.model.CompositeParser;
@@ -90,7 +92,6 @@ public class EdgeSerializationImpl implements EdgeSerialization, Migration
{
     private static final EdgeSerializer EDGE_SERIALIZER = new EdgeSerializer();
 
 
-
     // column families
     /**
      * Edges that are from the source node. The row key is the source node
@@ -173,17 +174,11 @@ public class EdgeSerializationImpl implements EdgeSerialization, Migration
{
     }
 
 
-    @Override
-    public Iterator<Edge> getEdgeFromSource( final OrganizationScope scope, final SearchByEdge
search ) {
-        return null;  //To change body of implemented methods use File | Settings | File
Templates.
-    }
-
 
     private void doWrite( final OrganizationScope scope, final Edge edge, final RowOp op
) {
         ValidationUtils.validateOrganizationScope( scope );
         EdgeUtils.validateEdge( edge );
 
-        final MutationBatch batch = keyspace.prepareMutationBatch();
         final Id sourceNodeId = edge.getSourceNode();
         final Id targetNodeId = edge.getTargetNode();
         final UUID version = edge.getVersion();
@@ -223,6 +218,63 @@ public class EdgeSerializationImpl implements EdgeSerialization, Migration
{
     }
 
 
+
+    @Override
+    public Iterator<Edge> getEdgeFromSource( final OrganizationScope scope, final SearchByEdge
search ) {
+        ValidationUtils.validateOrganizationScope( scope );
+        EdgeUtils.validateSearchByEdge( search );
+
+        final Id sourceId = search.sourceNode();
+        final Id targetId = search.targetNode();
+        final String type = search.getType();
+        final UUID maxVersion = search.getMaxVersion();
+        final Optional<Edge> last = search.last();
+
+        return getEdges( GRAPH_SOURCE_NODE_EDGES, new EdgeSearcher<RowKey>() {
+
+            @Override
+            public void setRange( final RangeBuilder builder ) {
+
+                //set our start range since it was supplied to us
+                if ( search.last().isPresent() ) {
+
+                    final Edge edge = search.last().get();
+                    DirectedEdge sourceEdge = new DirectedEdge( edge.getTargetNode(), edge.getVersion()
);
+
+                    builder.setStart( sourceEdge, EDGE_SERIALIZER );
+                }
+
+
+                //                final DirectedEdge last = new DirectedEdge( targetId, maxVersion
);
+                //                final ByteBuffer colValue = EDGE_SERIALIZER.createSearchEdgeInclusive(
last );
+                //                builder.setEnd( colValue );
+            }
+
+
+            @Override
+            public ScopedRowKey<OrganizationScope, RowKey> getRowKey() {
+                final RowKey sourceRowKey = new RowKey( sourceId, type );
+
+                return ScopedRowKey.fromKey( scope, sourceRowKey );
+            }
+
+
+            @Override
+            public boolean hasPage() {
+                return search.last().isPresent();
+            }
+
+
+            @Override
+            public Edge parseColumn( final Column<DirectedEdge> column ) {
+                final DirectedEdge edge = column.getName();
+
+                return new SimpleEdge( sourceId, type, edge.id, edge.version );
+            }
+        } );
+    }
+
+
     @Override
     public Iterator<Edge> getEdgesFromSource( final OrganizationScope scope, final
SearchByEdgeType edgeType ) {
 
@@ -232,7 +284,7 @@ public class EdgeSerializationImpl implements EdgeSerialization, Migration
{
         final Id sourceId = edgeType.getNode();
         final String type = edgeType.getType();
 
-        return getEdges( scope, GRAPH_SOURCE_NODE_EDGES, new EdgeSearcher<RowKey>()
{
+        return getEdges( GRAPH_SOURCE_NODE_EDGES, new EdgeSearcher<RowKey>() {
 
             @Override
             public void setRange( final RangeBuilder builder ) {
@@ -283,7 +335,7 @@ public class EdgeSerializationImpl implements EdgeSerialization, Migration
{
         final String type = edgeType.getType();
         final String targetType = edgeType.getIdType();
 
-        return getEdges( scope, GRAPH_SOURCE_NODE_TARGET_TYPE, new EdgeSearcher<RowKeyType>()
{
+        return getEdges( GRAPH_SOURCE_NODE_TARGET_TYPE, new EdgeSearcher<RowKeyType>()
{
 
             @Override
             public void setRange( final RangeBuilder builder ) {
@@ -301,7 +353,7 @@ public class EdgeSerializationImpl implements EdgeSerialization, Migration
{
 
             @Override
             public ScopedRowKey<OrganizationScope, RowKeyType> getRowKey() {
-                final RowKeyType sourceRowKey = new RowKeyType( targetId, type, targetType);
+                final RowKeyType sourceRowKey = new RowKeyType( targetId, type, targetType
);
 
                 return ScopedRowKey.fromKey( scope, sourceRowKey );
             }
@@ -332,7 +384,7 @@ public class EdgeSerializationImpl implements EdgeSerialization, Migration
{
         final Id targetId = edgeType.getNode();
         final String type = edgeType.getType();
 
-        return getEdges( scope, GRAPH_TARGET_NODE_EDGES, new EdgeSearcher<RowKey>()
{
+        return getEdges( GRAPH_TARGET_NODE_EDGES, new EdgeSearcher<RowKey>() {
 
             @Override
             public void setRange( final RangeBuilder builder ) {
@@ -366,7 +418,7 @@ public class EdgeSerializationImpl implements EdgeSerialization, Migration
{
             public Edge parseColumn( final Column<DirectedEdge> column ) {
                 final DirectedEdge edge = column.getName();
 
-                return new SimpleEdge( edge.id,  type, targetId, edge.version );
+                return new SimpleEdge( edge.id, type, targetId, edge.version );
             }
         } );
     }
@@ -374,7 +426,57 @@ public class EdgeSerializationImpl implements EdgeSerialization, Migration
{
 
     @Override
     public Iterator<Edge> getEdgeToTarget( final OrganizationScope scope, final SearchByEdge
search ) {
-        return null;  //To change body of implemented methods use File | Settings | File
Templates.
+        ValidationUtils.validateOrganizationScope( scope );
+        EdgeUtils.validateSearchByEdge( search );
+
+        final Id sourceId = search.sourceNode();
+        final Id targetId = search.targetNode();
+        final String type = search.getType();
+        final UUID maxVersion = search.getMaxVersion();
+        final Optional<Edge> last = search.last();
+
+        return getEdges( GRAPH_TARGET_NODE_EDGES, new EdgeSearcher<RowKey>() {
+
+            @Override
+            public void setRange( final RangeBuilder builder ) {
+
+                //set our start range since it was supplied to us
+                if ( last.isPresent() ) {
+
+                    final Edge edge = last.get();
+                    DirectedEdge sourceEdge = new DirectedEdge( edge.getSourceNode(), edge.getVersion()
);
+
+                    builder.setStart( sourceEdge, EDGE_SERIALIZER );
+                }
+
+
+                final DirectedEdge last = new DirectedEdge( sourceId, maxVersion );
+                final ByteBuffer colValue = EDGE_SERIALIZER.createSearchEdgeInclusive( last
);
+                builder.setEnd( colValue );
+            }
+
+
+            @Override
+            public ScopedRowKey<OrganizationScope, RowKey> getRowKey() {
+                final RowKey sourceRowKey = new RowKey( targetId, type );
+
+                return ScopedRowKey.fromKey( scope, sourceRowKey );
+            }
+
+
+            @Override
+            public boolean hasPage() {
+                return last.isPresent();
+            }
+
+
+            @Override
+            public Edge parseColumn( final Column<DirectedEdge> column ) {
+                final DirectedEdge edge = column.getName();
+
+                return new SimpleEdge( edge.id, type, targetId, edge.version );
+            }
+        } );
     }
 
 
@@ -388,7 +490,7 @@ public class EdgeSerializationImpl implements EdgeSerialization, Migration
{
         final String sourceType = edgeType.getIdType();
         final String type = edgeType.getType();
 
-        return getEdges( scope, GRAPH_TARGET_NODE_SOURCE_TYPE, new EdgeSearcher<RowKeyType>()
{
+        return getEdges( GRAPH_TARGET_NODE_SOURCE_TYPE, new EdgeSearcher<RowKeyType>()
{
 
             @Override
             public void setRange( final RangeBuilder builder ) {
@@ -436,8 +538,7 @@ public class EdgeSerializationImpl implements EdgeSerialization, Migration
{
      *
      * @return An iterator of all Edge instance that match the criteria
      */
-    private <R> Iterator<Edge> getEdges( final OrganizationScope scope,
-                                         MultiTennantColumnFamily<OrganizationScope, R,
DirectedEdge> cf,
+    private <R> Iterator<Edge> getEdges( MultiTennantColumnFamily<OrganizationScope,
R, DirectedEdge> cf,
                                          final EdgeSearcher<R> searcher ) {
 
 
@@ -514,14 +615,9 @@ public class EdgeSerializationImpl implements EdgeSerialization, Migration
{
 
         @Override
         public ByteBuffer toByteBuffer( final DirectedEdge edge ) {
-            DynamicComposite composite = new DynamicComposite();
-
-            ID_COL_SERIALIZER.toComposite( composite, edge.id );
-
-            //add our edge
-            composite.addComponent( edge.version, UUID_SERIALIZER );
+            final DynamicComposite colValue = createComposite( edge, AbstractComposite.ComponentEquality.EQUAL
);
 
-            return composite.serialize();
+            return colValue.serialize();
         }
 
 
@@ -541,6 +637,33 @@ public class EdgeSerializationImpl implements EdgeSerialization, Migration
{
 
             return new DirectedEdge( id, version );
         }
+
+
+        /**
+         * Creates a column value that will include the directed Edge as it's last element.
 I.E all edges returned from
+         * the scan will have the node id and a version <= the specified directed edge
version
+         */
+        public ByteBuffer createSearchEdgeInclusive( DirectedEdge edge ) {
+            final DynamicComposite colValue =
+                    createComposite( edge, AbstractComposite.ComponentEquality.GREATER_THAN_EQUAL
);
+
+            return colValue.serialize();
+        }
+
+
+        /**
+         * Create the dynamic composite for this directed edge
+         */
+        private DynamicComposite createComposite( DirectedEdge edge, AbstractComposite.ComponentEquality
equality ) {
+            DynamicComposite composite = new DynamicComposite();
+
+            ID_COL_SERIALIZER.toComposite( composite, edge.id );
+
+            //add our edge
+            composite.addComponent( edge.version, UUID_SERIALIZER, equality );
+
+            return composite;
+        }
     }
 
 

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/804e2df0/stack/corepersistence/graph/src/main/java/org/apache/usergrid/persistence/graph/serialization/util/EdgeUtils.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/graph/src/main/java/org/apache/usergrid/persistence/graph/serialization/util/EdgeUtils.java
b/stack/corepersistence/graph/src/main/java/org/apache/usergrid/persistence/graph/serialization/util/EdgeUtils.java
index 2dc7ba7..291787e 100644
--- a/stack/corepersistence/graph/src/main/java/org/apache/usergrid/persistence/graph/serialization/util/EdgeUtils.java
+++ b/stack/corepersistence/graph/src/main/java/org/apache/usergrid/persistence/graph/serialization/util/EdgeUtils.java
@@ -22,6 +22,7 @@ package org.apache.usergrid.persistence.graph.serialization.util;
 
 import org.apache.usergrid.persistence.collection.mvcc.entity.ValidationUtils;
 import org.apache.usergrid.persistence.graph.Edge;
+import org.apache.usergrid.persistence.graph.SearchByEdge;
 import org.apache.usergrid.persistence.graph.SearchByEdgeType;
 import org.apache.usergrid.persistence.graph.SearchByIdType;
 import org.apache.usergrid.persistence.graph.SearchEdgeType;
@@ -79,6 +80,7 @@ public class EdgeUtils {
 
         ValidationUtils.verifyIdentity( search.getNode() );
         ValidationUtils.verifyString( search.getType(), "type" );
+        ValidationUtils.verifyTimeUuid( search.getMaxVersion(), "maxVersion" );
 
         //only validate if the value is present
         if(search.last().isPresent()){
@@ -87,6 +89,24 @@ public class EdgeUtils {
 
     }
 
+    /**
+        * Validate the search edge
+        */
+       public static void validateSearchByEdge( final SearchByEdge search ) {
+           Preconditions.checkNotNull( search, "search is required" );
+
+           ValidationUtils.verifyIdentity( search.sourceNode() );
+           ValidationUtils.verifyIdentity( search.targetNode() );
+           ValidationUtils.verifyString( search.getType(), "type" );
+           ValidationUtils.verifyTimeUuid( search.getMaxVersion(), "maxVersion" );
+
+           //only validate if the value is present
+           if(search.last().isPresent()){
+               validateEdge( search.last().get() );
+           }
+
+       }
+
 
     /**
      * Validate the search

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/804e2df0/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/EdgeSerializationTest.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/EdgeSerializationTest.java
b/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/EdgeSerializationTest.java
index 8cee1e0..6548ab0 100644
--- a/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/EdgeSerializationTest.java
+++ b/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/EdgeSerializationTest.java
@@ -169,17 +169,22 @@ public class EdgeSerializationTest {
      */
     @Test
     public void directEdgeGets() throws ConnectionException {
-        final Edge edgev1 = createEdge( "source", "edge", "target" );
+        final Edge edgev1 = createEdge( "source", "edge1", "target" );
 
         final Id sourceId = edgev1.getSourceNode();
         final Id targetId = edgev1.getTargetNode();
 
 
-        final Edge edgev2 = createEdge( sourceId, "edge", targetId );
+        final Edge edgev2 = createEdge( sourceId, "edge1", targetId );
 
         assertTrue( "Edge version 1 has lower time uuid", edgev1.getVersion().compareTo(
edgev2.getVersion() ) < 0 );
 
+        //create edge type 2 to ensure we don't get it in results
+        final Edge edgeType2V1 = createEdge( sourceId, "edge2", targetId );
+
         serialization.writeEdge( scope, edgev1 ).execute();
+        serialization.writeEdge( scope, edgev2 ).execute();
+        serialization.writeEdge( scope, edgeType2V1 ).execute();
 
         final UUID now = UUIDGenerator.newTimeUUID();
 


Mime
View raw message