You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@usergrid.apache.org by to...@apache.org on 2014/08/29 19:34:00 UTC

[02/20] Implemented New rolling shard algorithm. This should allow eventual shard consistency between all clients without the need for an external locking allocation system

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/72684dfc/stack/corepersistence/graph/src/main/java/org/apache/usergrid/persistence/graph/serialization/util/EdgeHasher.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/graph/src/main/java/org/apache/usergrid/persistence/graph/serialization/util/EdgeHasher.java b/stack/corepersistence/graph/src/main/java/org/apache/usergrid/persistence/graph/serialization/util/EdgeHasher.java
deleted file mode 100644
index 14a67fa..0000000
--- a/stack/corepersistence/graph/src/main/java/org/apache/usergrid/persistence/graph/serialization/util/EdgeHasher.java
+++ /dev/null
@@ -1,86 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *    http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.apache.usergrid.persistence.graph.serialization.util;
-
-
-import java.nio.ByteBuffer;
-import java.nio.charset.Charset;
-
-import org.apache.cassandra.utils.MurmurHash;
-
-import org.apache.usergrid.persistence.model.entity.Id;
-
-
-/**
- *
- *
- */
-public class EdgeHasher {
-
-    private static final String UTF_8 = "UTF-8";
-    private static final Charset CHARSET = Charset.forName( UTF_8 );
-
-
-    /**
-     * Create a hash based on the edge type and the type of the id that will be inserted into the column
-     *
-     *
-     * @param edgeType The name of the edge type
-     * @param idForColumn The id of the value that will be in the column
-     *
-     * @return A hash that represents a consistent one way hash of the fields
-     */
-    public static long[] createEdgeHash( final String edgeType, final Id idForColumn ) {
-
-        return createEdgeHash( edgeType, idForColumn.getType() );
-    }
-
-
-    /**
-     * Create the edge hash from the edge type and id type
-     * @param edgeTypes
-     * @return
-     */
-    public static long[] createEdgeHash(final String... edgeTypes){
-       final StringBuilder hashString =  new StringBuilder();
-
-        for(String edge: edgeTypes){
-            hashString.append(edge);
-        }
-
-        return createEdgeHash( hashString.toString() );
-    }
-
-
-    /**
-     * Create a ash based on the edge type and the type of the id that will be inserted into the column
-     *
-     * @return A hash that represents a consistent one way hash of the fields
-     */
-    public static long[] createEdgeHash( final String edgeType ) {
-
-
-        ByteBuffer key = ByteBuffer.wrap( edgeType.getBytes( CHARSET ) );
-
-        return  MurmurHash.hash3_x64_128( key, key.position(), key.remaining(), 0 );
-
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/72684dfc/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
deleted file mode 100644
index b191957..0000000
--- a/stack/corepersistence/graph/src/main/java/org/apache/usergrid/persistence/graph/serialization/util/EdgeUtils.java
+++ /dev/null
@@ -1,132 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *    http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.apache.usergrid.persistence.graph.serialization.util;
-
-
-import org.apache.usergrid.persistence.core.util.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;
-import org.apache.usergrid.persistence.graph.SearchIdType;
-
-import com.google.common.base.Preconditions;
-
-
-/**
- *
- *
- */
-public class EdgeUtils {
-
-    /**
-     * Validate an edge input
-     *
-     * @param e The edge to validate
-     */
-    public static void validateEdge( Edge e ) {
-        Preconditions.checkNotNull( e, "edge is required" );
-        ValidationUtils.verifyIdentity( e.getSourceNode() );
-        ValidationUtils.verifyIdentity( e.getTargetNode() );
-        ValidationUtils.verifyString( e.getType(), "type" );
-        validateTimestamp( e.getTimestamp(), "timestamp" );
-
-    }
-
-
-    /**
-     * Validate the timestamp is set
-     * @param value
-     * @param fieldName
-     */
-    public static void validateTimestamp(final long value, final String fieldName){
-        Preconditions.checkArgument( value > -1, fieldName );
-    }
-
-    /**
-     * Validate the search edge
-     */
-    public static void validateSearchEdgeType( final SearchEdgeType search ) {
-        Preconditions.checkNotNull( search, "search is required" );
-
-        ValidationUtils.verifyIdentity( search.getNode() );
-    }
-
-
-    /**
-     * Validate the search edge
-     */
-    public static void validateSearchEdgeIdType( final SearchIdType search ) {
-        Preconditions.checkNotNull( search, "search is required" );
-
-        validateSearchEdgeType( search );
-
-        ValidationUtils.verifyString( search.getEdgeType(), "edgeType" );
-    }
-
-    /**
-     * Validate the search edge
-     */
-    public static void validateSearchByEdgeType( final SearchByEdgeType search ) {
-        Preconditions.checkNotNull( search, "search is required" );
-
-        ValidationUtils.verifyIdentity( search.getNode() );
-        ValidationUtils.verifyString( search.getType(), "type" );
-        validateTimestamp( search.getMaxTimestamp(), "maxTimestamp" );
-
-        //only validate if the value is present
-        if(search.last().isPresent()){
-            validateEdge( search.last().get() );
-        }
-
-    }
-
-    /**
-        * 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" );
-           validateTimestamp( search.getMaxTimestamp(), "maxTimestamp" );
-
-           //only validate if the value is present
-           if(search.last().isPresent()){
-               validateEdge( search.last().get() );
-           }
-
-       }
-
-
-    /**
-     * Validate the search
-     * @param search
-     */
-    public static void validateSearchByIdType(final SearchByIdType search){
-        validateSearchByEdgeType( search );
-
-        ValidationUtils.verifyString(search.getIdType(), "id type");
-
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/72684dfc/stack/corepersistence/graph/src/main/java/org/apache/usergrid/persistence/graph/serialization/util/GraphValidation.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/graph/src/main/java/org/apache/usergrid/persistence/graph/serialization/util/GraphValidation.java b/stack/corepersistence/graph/src/main/java/org/apache/usergrid/persistence/graph/serialization/util/GraphValidation.java
new file mode 100644
index 0000000..15a57b9
--- /dev/null
+++ b/stack/corepersistence/graph/src/main/java/org/apache/usergrid/persistence/graph/serialization/util/GraphValidation.java
@@ -0,0 +1,189 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.usergrid.persistence.graph.serialization.util;
+
+
+import org.apache.usergrid.persistence.core.util.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;
+import org.apache.usergrid.persistence.graph.SearchIdType;
+import org.apache.usergrid.persistence.graph.serialization.impl.shard.DirectedEdgeMeta;
+import org.apache.usergrid.persistence.graph.serialization.impl.shard.Shard;
+import org.apache.usergrid.persistence.graph.serialization.impl.shard.ShardEntryGroup;
+
+import com.google.common.base.Preconditions;
+
+
+/**
+ *
+ *
+ */
+public class GraphValidation {
+
+    /**
+     * Validate an edge input
+     *
+     * @param e The edge to validate
+     */
+    public static void validateEdge( Edge e ) {
+        Preconditions.checkNotNull( e, "edge is required" );
+        ValidationUtils.verifyIdentity( e.getSourceNode() );
+        ValidationUtils.verifyIdentity( e.getTargetNode() );
+        ValidationUtils.verifyString( e.getType(), "type" );
+        validateTimestamp( e.getTimestamp(), "timestamp" );
+
+    }
+
+
+    /**
+     * Validate the timestamp is set
+     * @param value
+     * @param fieldName
+     */
+    public static void validateTimestamp(final long value, final String fieldName){
+        Preconditions.checkArgument( value > -1, fieldName );
+    }
+
+    /**
+     * Validate the search edge
+     */
+    public static void validateSearchEdgeType( final SearchEdgeType search ) {
+        Preconditions.checkNotNull( search, "search is required" );
+
+        ValidationUtils.verifyIdentity( search.getNode() );
+    }
+
+
+    /**
+     * Validate the search edge
+     */
+    public static void validateSearchEdgeIdType( final SearchIdType search ) {
+        Preconditions.checkNotNull( search, "search is required" );
+
+        validateSearchEdgeType( search );
+
+        ValidationUtils.verifyString( search.getEdgeType(), "edgeType" );
+    }
+
+    /**
+     * Validate the search edge
+     */
+    public static void validateSearchByEdgeType( final SearchByEdgeType search ) {
+        Preconditions.checkNotNull( search, "search is required" );
+
+        ValidationUtils.verifyIdentity( search.getNode() );
+        ValidationUtils.verifyString( search.getType(), "type" );
+        validateTimestamp( search.getMaxTimestamp(), "maxTimestamp" );
+
+        //only validate if the value is present
+        if(search.last().isPresent()){
+            validateEdge( search.last().get() );
+        }
+
+    }
+
+    /**
+        * 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" );
+           validateTimestamp( search.getMaxTimestamp(), "maxTimestamp" );
+
+           //only validate if the value is present
+           if(search.last().isPresent()){
+               validateEdge( search.last().get() );
+           }
+
+       }
+
+
+    /**
+     * Validate the search
+     * @param search
+     */
+    public static void validateSearchByIdType(final SearchByIdType search){
+        validateSearchByEdgeType( search );
+
+        ValidationUtils.verifyString(search.getIdType(), "id type");
+
+    }
+
+
+    /**
+     * Validate the directed edge meta data
+     * @param directedEdgeMeta
+     */
+    public static void validateDirectedEdgeMeta(final DirectedEdgeMeta directedEdgeMeta){
+
+        Preconditions.checkNotNull( directedEdgeMeta, "directedEdgeMeta must not be null" );
+
+        final DirectedEdgeMeta.NodeMeta[] nodes = directedEdgeMeta.getNodes();
+
+        Preconditions.checkArgument( nodes.length > 0, "At least one node must be present" );
+
+        for( DirectedEdgeMeta.NodeMeta node : nodes){
+            ValidationUtils.verifyIdentity( node.getId());
+            Preconditions.checkNotNull( node.getNodeType(), "NodeType must not be null" );
+        }
+
+        final String[] types = directedEdgeMeta.getTypes();
+
+        Preconditions.checkArgument( types.length > 0, "At least one type must be present" );
+
+        for(String type: types){
+            Preconditions.checkNotNull( type, "You cannot have a null type" );
+        }
+
+
+    }
+
+
+    /**
+     * Validate the directed edge meta data
+     * @param shardEntryGroup
+     */
+    public static void validateShardEntryGroup(final ShardEntryGroup shardEntryGroup){
+
+        Preconditions.checkNotNull( shardEntryGroup, "shardEntryGroup must not be null" );
+
+        Preconditions.checkArgument( shardEntryGroup.entrySize() > 0, "shardEntryGroups must contain at least 1 shard");
+
+    }
+
+
+    /**
+     * Validate our shard
+     * @param shard
+     */
+    public static void valiateShard(final Shard shard){
+        Preconditions.checkNotNull( shard, "shard must not be null" );
+        Preconditions.checkArgument( shard.getShardIndex() > -1, "shardid must be greater than -1" );
+        Preconditions.checkArgument( shard.getCreatedTime() > -1, "createdTime must be greater than -1" );
+
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/72684dfc/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/ComittedGraphManagerIT.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/ComittedGraphManagerIT.java b/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/ComittedGraphManagerIT.java
deleted file mode 100644
index 596480d..0000000
--- a/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/ComittedGraphManagerIT.java
+++ /dev/null
@@ -1,138 +0,0 @@
-/*
- *
- *  * Licensed to the Apache Software Foundation (ASF) under one
- *  * or more contributor license agreements.  See the NOTICE file
- *  * distributed with this work for additional information
- *  * regarding copyright ownership.  The ASF licenses this file
- *  * to you under the Apache License, Version 2.0 (the
- *  * "License"); you may not use this file except in compliance
- *  * with the License.  You may obtain a copy of the License at
- *  *
- *  *    http://www.apache.org/licenses/LICENSE-2.0
- *  *
- *  * Unless required by applicable law or agreed to in writing,
- *  * software distributed under the License is distributed on an
- *  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- *  * KIND, either express or implied.  See the License for the
- *  * specific language governing permissions and limitations
- *  * under the License.
- *
- */
-package org.apache.usergrid.persistence.graph;
-
-
-import java.util.concurrent.TimeUnit;
-
-import org.jukito.JukitoRunner;
-import org.jukito.UseModules;
-import org.junit.runner.RunWith;
-
-import org.apache.usergrid.persistence.core.cassandra.ITRunner;
-import org.apache.usergrid.persistence.graph.guice.TestGraphModule;
-import org.apache.usergrid.persistence.model.entity.Id;
-
-import rx.Observable;
-
-
-/**
- * Integration test that performs all calls immediately after writes without blocking.  Tests that our
- * view is immediately consistent to our users, even if we have yet to perform background processing
- */
-@RunWith(ITRunner.class)
-@UseModules({ TestGraphModule.class })
-public class ComittedGraphManagerIT extends GraphManagerIT {
-
-
-    @Override
-    protected GraphManager getHelper(GraphManager gm) {
-        return new ComittedGraphTestHelper( gm );
-    }
-
-
-    /**
-     * Doesn't wait for the async process to happen before returning.  Simply executes and immediately returns.
-     */
-    public static class ComittedGraphTestHelper implements GraphManager {
-
-        private final GraphManager graphManager;
-
-
-        public ComittedGraphTestHelper( final GraphManager graphManager ) {
-            this.graphManager = graphManager;
-        }
-
-
-        @Override
-        public Observable<Edge> writeEdge( final Edge edge ) {
-            return graphManager.writeEdge( edge );
-        }
-
-
-        @Override
-        public Observable<Edge> deleteEdge( final Edge edge ) {
-            return graphManager.deleteEdge( edge );
-        }
-
-
-        @Override
-        public Observable<Id> deleteNode( final Id node, final long timestamp) {
-            return graphManager.deleteNode( node, timestamp );
-        }
-
-
-        @Override
-        public Observable<Edge> loadEdgeVersions( final SearchByEdge edge ) {
-            return graphManager.loadEdgeVersions( edge );
-        }
-
-
-        @Override
-        public Observable<Edge> loadEdgesFromSource( final SearchByEdgeType search ) {
-            return graphManager.loadEdgesFromSource( search );
-        }
-
-
-        @Override
-        public Observable<Edge> loadEdgesToTarget( final SearchByEdgeType search ) {
-            return graphManager.loadEdgesToTarget( search );
-        }
-
-
-        @Override
-        public Observable<Edge> loadEdgesFromSourceByType( final SearchByIdType search ) {
-            return  graphManager.loadEdgesFromSourceByType(search);
-        }
-
-
-        @Override
-        public Observable<Edge> loadEdgesToTargetByType( final SearchByIdType search ) {
-            return graphManager.loadEdgesToTargetByType( search );
-        }
-
-
-        @Override
-        public Observable<String> getEdgeTypesFromSource( final SearchEdgeType search ) {
-            return graphManager.getEdgeTypesFromSource( search );
-        }
-
-
-        @Override
-        public Observable<String> getIdTypesFromSource( final SearchIdType search ) {
-            return graphManager.getIdTypesFromSource( search );
-        }
-
-
-        @Override
-        public Observable<String> getEdgeTypesToTarget( final SearchEdgeType search ) {
-            return graphManager.getEdgeTypesToTarget( search );
-        }
-
-
-        @Override
-        public Observable<String> getIdTypesToTarget( final SearchIdType search ) {
-            return graphManager.getIdTypesToTarget( search );
-        }
-
-
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/72684dfc/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/CommittedGraphManagerIT.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/CommittedGraphManagerIT.java b/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/CommittedGraphManagerIT.java
new file mode 100644
index 0000000..feeb1b6
--- /dev/null
+++ b/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/CommittedGraphManagerIT.java
@@ -0,0 +1,135 @@
+/*
+ *
+ *  * Licensed to the Apache Software Foundation (ASF) under one
+ *  * or more contributor license agreements.  See the NOTICE file
+ *  * distributed with this work for additional information
+ *  * regarding copyright ownership.  The ASF licenses this file
+ *  * to you under the Apache License, Version 2.0 (the
+ *  * "License"); you may not use this file except in compliance
+ *  * with the License.  You may obtain a copy of the License at
+ *  *
+ *  *    http://www.apache.org/licenses/LICENSE-2.0
+ *  *
+ *  * Unless required by applicable law or agreed to in writing,
+ *  * software distributed under the License is distributed on an
+ *  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  * KIND, either express or implied.  See the License for the
+ *  * specific language governing permissions and limitations
+ *  * under the License.
+ *
+ */
+package org.apache.usergrid.persistence.graph;
+
+
+import org.jukito.UseModules;
+import org.junit.runner.RunWith;
+
+import org.apache.usergrid.persistence.core.cassandra.ITRunner;
+import org.apache.usergrid.persistence.graph.guice.TestGraphModule;
+import org.apache.usergrid.persistence.model.entity.Id;
+
+import rx.Observable;
+
+
+/**
+ * Integration test that performs all calls immediately after writes without blocking.  Tests that our
+ * view is immediately consistent to our users, even if we have yet to perform background processing
+ */
+@RunWith(ITRunner.class)
+@UseModules({ TestGraphModule.class })
+public class CommittedGraphManagerIT extends GraphManagerIT {
+
+
+    @Override
+    protected GraphManager getHelper(GraphManager gm) {
+        return new ComittedGraphTestHelper( gm );
+    }
+
+
+    /**
+     * Doesn't wait for the async process to happen before returning.  Simply executes and immediately returns.
+     */
+    public static class ComittedGraphTestHelper implements GraphManager {
+
+        private final GraphManager graphManager;
+
+
+        public ComittedGraphTestHelper( final GraphManager graphManager ) {
+            this.graphManager = graphManager;
+        }
+
+
+        @Override
+        public Observable<Edge> writeEdge( final Edge edge ) {
+            return graphManager.writeEdge( edge );
+        }
+
+
+        @Override
+        public Observable<Edge> deleteEdge( final Edge edge ) {
+            return graphManager.deleteEdge( edge );
+        }
+
+
+        @Override
+        public Observable<Id> deleteNode( final Id node, final long timestamp) {
+            return graphManager.deleteNode( node, timestamp );
+        }
+
+
+        @Override
+        public Observable<Edge> loadEdgeVersions( final SearchByEdge edge ) {
+            return graphManager.loadEdgeVersions( edge );
+        }
+
+
+        @Override
+        public Observable<Edge> loadEdgesFromSource( final SearchByEdgeType search ) {
+            return graphManager.loadEdgesFromSource( search );
+        }
+
+
+        @Override
+        public Observable<Edge> loadEdgesToTarget( final SearchByEdgeType search ) {
+            return graphManager.loadEdgesToTarget( search );
+        }
+
+
+        @Override
+        public Observable<Edge> loadEdgesFromSourceByType( final SearchByIdType search ) {
+            return  graphManager.loadEdgesFromSourceByType(search);
+        }
+
+
+        @Override
+        public Observable<Edge> loadEdgesToTargetByType( final SearchByIdType search ) {
+            return graphManager.loadEdgesToTargetByType( search );
+        }
+
+
+        @Override
+        public Observable<String> getEdgeTypesFromSource( final SearchEdgeType search ) {
+            return graphManager.getEdgeTypesFromSource( search );
+        }
+
+
+        @Override
+        public Observable<String> getIdTypesFromSource( final SearchIdType search ) {
+            return graphManager.getIdTypesFromSource( search );
+        }
+
+
+        @Override
+        public Observable<String> getEdgeTypesToTarget( final SearchEdgeType search ) {
+            return graphManager.getEdgeTypesToTarget( search );
+        }
+
+
+        @Override
+        public Observable<String> getIdTypesToTarget( final SearchIdType search ) {
+            return graphManager.getIdTypesToTarget( search );
+        }
+
+
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/72684dfc/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/GraphManagerIT.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/GraphManagerIT.java b/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/GraphManagerIT.java
index 9fa4cbf..61bc7e7 100644
--- a/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/GraphManagerIT.java
+++ b/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/GraphManagerIT.java
@@ -34,7 +34,6 @@ import org.apache.usergrid.persistence.graph.impl.SimpleSearchEdgeType;
 import org.apache.usergrid.persistence.graph.impl.SimpleSearchIdType;
 import org.apache.usergrid.persistence.model.entity.Id;
 import org.apache.usergrid.persistence.model.entity.SimpleId;
-import org.apache.usergrid.persistence.model.util.UUIDGenerator;
 
 import com.google.inject.Inject;
 
@@ -49,8 +48,6 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
 
 
 public abstract class GraphManagerIT {

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/72684dfc/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/GraphManagerLoadTest.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/GraphManagerLoadTest.java b/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/GraphManagerLoadTest.java
index 8378067..d0a0525 100644
--- a/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/GraphManagerLoadTest.java
+++ b/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/GraphManagerLoadTest.java
@@ -40,14 +40,13 @@ import org.slf4j.LoggerFactory;
 
 import org.apache.commons.lang3.time.StopWatch;
 
+import org.apache.usergrid.persistence.collection.guice.MigrationManagerRule;
 import org.apache.usergrid.persistence.core.cassandra.ITRunner;
 import org.apache.usergrid.persistence.core.scope.ApplicationScope;
-import org.apache.usergrid.persistence.collection.guice.MigrationManagerRule;
 import org.apache.usergrid.persistence.core.scope.ApplicationScopeImpl;
 import org.apache.usergrid.persistence.graph.guice.TestGraphModule;
 import org.apache.usergrid.persistence.graph.impl.SimpleSearchByEdgeType;
 import org.apache.usergrid.persistence.model.entity.Id;
-import org.apache.usergrid.persistence.model.util.UUIDGenerator;
 
 import com.google.inject.Inject;
 

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/72684dfc/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/GraphManagerShardingIT.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/GraphManagerShardingIT.java b/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/GraphManagerShardingIT.java
index 01e07b1..e6a6d27 100644
--- a/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/GraphManagerShardingIT.java
+++ b/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/GraphManagerShardingIT.java
@@ -34,9 +34,10 @@ import org.apache.usergrid.persistence.core.cassandra.ITRunner;
 import org.apache.usergrid.persistence.core.scope.ApplicationScope;
 import org.apache.usergrid.persistence.core.scope.ApplicationScopeImpl;
 import org.apache.usergrid.persistence.graph.guice.TestGraphModule;
+import org.apache.usergrid.persistence.graph.serialization.impl.shard.DirectedEdgeMeta;
 import org.apache.usergrid.persistence.graph.serialization.impl.shard.NodeShardApproximation;
+import org.apache.usergrid.persistence.graph.serialization.impl.shard.Shard;
 import org.apache.usergrid.persistence.model.entity.Id;
-import org.apache.usergrid.persistence.model.util.UUIDGenerator;
 
 import com.google.inject.Inject;
 
@@ -44,8 +45,6 @@ import static org.apache.usergrid.persistence.graph.test.util.EdgeTestUtils.crea
 import static org.apache.usergrid.persistence.graph.test.util.EdgeTestUtils.createId;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
 
 
 @RunWith( ITRunner.class )
@@ -101,7 +100,7 @@ public class GraphManagerShardingIT {
         //each edge causes 4 counts
         final long writeCount = flushCount/4;
 
-        assertTrue( "Shard size must be >= flush Count", maxShardSize >= flushCount );
+        assertTrue( "Shard size must be >= beginFlush Count", maxShardSize >= flushCount );
 
         Id targetId = null;
 
@@ -115,14 +114,22 @@ public class GraphManagerShardingIT {
         }
 
 
-        long shardCount = nodeShardApproximation.getCount( scope, sourceId, 0l, edgeType );
+
+        final DirectedEdgeMeta sourceEdgeMeta = DirectedEdgeMeta.fromSourceNodeTargetType( sourceId, edgeType,
+                targetId.getType() );
+        final Shard shard = new Shard(0, 0, true);
+
+
+        long shardCount = nodeShardApproximation.getCount( scope, shard, sourceEdgeMeta );
 
         assertEquals("Shard count for source node should be the same as write count", writeCount, shardCount);
 
 
         //now verify it's correct for the target
+        final DirectedEdgeMeta targetEdgeMeta = DirectedEdgeMeta.fromTargetNodeSourceType(targetId,  edgeType, sourceId.getType() );
+
 
-        shardCount = nodeShardApproximation.getCount( scope, targetId, 0l, edgeType );
+        shardCount = nodeShardApproximation.getCount( scope, shard, targetEdgeMeta );
 
         assertEquals(1, shardCount);
 
@@ -144,14 +151,10 @@ public class GraphManagerShardingIT {
         final long maxShardSize = graphFig.getShardSize();
 
 
-
-
-        final long startTime = System.currentTimeMillis();
-
-        //each edge causes 4 counts
+         //each edge causes 4 counts
         final long writeCount = flushCount/4;
 
-        assertTrue( "Shard size must be >= flush Count", maxShardSize >= flushCount );
+        assertTrue( "Shard size must be >= beginFlush Count", maxShardSize >= flushCount );
 
         Id sourceId = null;
 
@@ -165,14 +168,27 @@ public class GraphManagerShardingIT {
         }
 
 
-        long shardCount = nodeShardApproximation.getCount( scope, targetId, 0l, edgeType );
+        //this is from target->source, since the target id doesn't change
+        final DirectedEdgeMeta targetMeta = DirectedEdgeMeta.fromTargetNode( targetId, edgeType );
+        final Shard shard = new Shard(0l, 0l, true);
+
+        long targetWithType = nodeShardApproximation.getCount( scope, shard, targetMeta );
+
+        assertEquals("Shard count for target node should be the same as write count", writeCount, targetWithType);
 
-        assertEquals("Shard count for source node should be the same as write count", writeCount, shardCount);
+
+        final DirectedEdgeMeta targetNodeSource = DirectedEdgeMeta.fromTargetNodeSourceType( targetId, edgeType, "source" );
+
+        long shardCount = nodeShardApproximation.getCount( scope, shard, targetNodeSource );
+
+        assertEquals("Shard count for target node should be the same as write count", writeCount, shardCount);
 
 
         //now verify it's correct for the target
 
-        shardCount = nodeShardApproximation.getCount( scope, sourceId, 0l, edgeType );
+        final DirectedEdgeMeta sourceMeta = DirectedEdgeMeta.fromSourceNode( sourceId, edgeType );
+
+        shardCount = nodeShardApproximation.getCount( scope, shard, sourceMeta );
 
         assertEquals(1, shardCount);
 

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/72684dfc/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/GraphManagerStressTest.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/GraphManagerStressTest.java b/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/GraphManagerStressTest.java
index 101a31b..cf6dda8 100644
--- a/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/GraphManagerStressTest.java
+++ b/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/GraphManagerStressTest.java
@@ -21,7 +21,6 @@ package org.apache.usergrid.persistence.graph;
 
 import java.util.HashSet;
 import java.util.Set;
-import java.util.UUID;
 import java.util.concurrent.CountDownLatch;
 
 import org.jukito.UseModules;
@@ -35,9 +34,9 @@ import org.slf4j.LoggerFactory;
 
 import org.apache.commons.lang3.time.StopWatch;
 
+import org.apache.usergrid.persistence.collection.guice.MigrationManagerRule;
 import org.apache.usergrid.persistence.core.cassandra.ITRunner;
 import org.apache.usergrid.persistence.core.scope.ApplicationScope;
-import org.apache.usergrid.persistence.collection.guice.MigrationManagerRule;
 import org.apache.usergrid.persistence.graph.guice.TestGraphModule;
 import org.apache.usergrid.persistence.graph.impl.SimpleSearchByEdgeType;
 import org.apache.usergrid.persistence.model.entity.Id;

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/72684dfc/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/SimpleTest.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/SimpleTest.java b/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/SimpleTest.java
index d22c762..87ddc46 100644
--- a/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/SimpleTest.java
+++ b/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/SimpleTest.java
@@ -1,22 +1,26 @@
 
 package org.apache.usergrid.persistence.graph;
 
-import com.google.inject.Inject;
+
+import org.jukito.UseModules;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
 import org.apache.usergrid.persistence.collection.guice.MigrationManagerRule;
 import org.apache.usergrid.persistence.core.cassandra.ITRunner;
 import org.apache.usergrid.persistence.core.scope.ApplicationScope;
 import org.apache.usergrid.persistence.graph.guice.TestGraphModule;
 import org.apache.usergrid.persistence.graph.impl.SimpleSearchEdgeType;
-import static org.apache.usergrid.persistence.graph.test.util.EdgeTestUtils.createEdge;
 import org.apache.usergrid.persistence.model.entity.Id;
 import org.apache.usergrid.persistence.model.entity.SimpleId;
 import org.apache.usergrid.persistence.model.util.UUIDGenerator;
-import org.jukito.UseModules;
+
+import com.google.inject.Inject;
+
+import static org.apache.usergrid.persistence.graph.test.util.EdgeTestUtils.createEdge;
 import static org.junit.Assert.assertEquals;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.runner.RunWith;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/72684dfc/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/EdgeMetadataSerializationTest.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/EdgeMetadataSerializationTest.java b/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/EdgeMetadataSerializationTest.java
index 530da50..517ba03 100644
--- a/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/EdgeMetadataSerializationTest.java
+++ b/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/EdgeMetadataSerializationTest.java
@@ -29,9 +29,9 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
+import org.apache.usergrid.persistence.collection.guice.MigrationManagerRule;
 import org.apache.usergrid.persistence.core.cassandra.ITRunner;
 import org.apache.usergrid.persistence.core.scope.ApplicationScope;
-import org.apache.usergrid.persistence.collection.guice.MigrationManagerRule;
 import org.apache.usergrid.persistence.graph.Edge;
 import org.apache.usergrid.persistence.graph.guice.TestGraphModule;
 import org.apache.usergrid.persistence.model.entity.Id;

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/72684dfc/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/EdgeSerializationChopTest.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/EdgeSerializationChopTest.java b/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/EdgeSerializationChopTest.java
index d50214a..6da3506 100644
--- a/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/EdgeSerializationChopTest.java
+++ b/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/EdgeSerializationChopTest.java
@@ -27,11 +27,10 @@ import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.runner.RunWith;
-//import org.safehaus.chop.api.IterationChop;
 
+import org.apache.usergrid.persistence.collection.guice.MigrationManagerRule;
 import org.apache.usergrid.persistence.core.cassandra.ITRunner;
 import org.apache.usergrid.persistence.core.scope.ApplicationScope;
-import org.apache.usergrid.persistence.collection.guice.MigrationManagerRule;
 import org.apache.usergrid.persistence.graph.MarkedEdge;
 import org.apache.usergrid.persistence.graph.guice.StorageEdgeSerialization;
 import org.apache.usergrid.persistence.graph.guice.TestGraphModule;
@@ -48,6 +47,8 @@ import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
+//import org.safehaus.chop.api.IterationChop;
+
 
 /**
  * Test for use with Judo CHOP to stress test

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/72684dfc/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/NodeSerializationTest.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/NodeSerializationTest.java b/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/NodeSerializationTest.java
index a731bab..935808c 100644
--- a/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/NodeSerializationTest.java
+++ b/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/NodeSerializationTest.java
@@ -21,7 +21,6 @@ package org.apache.usergrid.persistence.graph.serialization;
 
 import java.util.Arrays;
 import java.util.Map;
-import java.util.UUID;
 
 import org.jukito.UseModules;
 import org.junit.Before;
@@ -29,9 +28,9 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
+import org.apache.usergrid.persistence.collection.guice.MigrationManagerRule;
 import org.apache.usergrid.persistence.core.cassandra.ITRunner;
 import org.apache.usergrid.persistence.core.scope.ApplicationScope;
-import org.apache.usergrid.persistence.collection.guice.MigrationManagerRule;
 import org.apache.usergrid.persistence.graph.guice.TestGraphModule;
 import org.apache.usergrid.persistence.model.entity.Id;
 import org.apache.usergrid.persistence.model.util.UUIDGenerator;

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/72684dfc/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/PermanentSerializationTest.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/PermanentSerializationTest.java b/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/PermanentSerializationTest.java
index f914fff..2ff571b 100644
--- a/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/PermanentSerializationTest.java
+++ b/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/PermanentSerializationTest.java
@@ -20,7 +20,6 @@
 package org.apache.usergrid.persistence.graph.serialization;
 
 
-import org.jukito.JukitoRunner;
 import org.jukito.UseModules;
 import org.junit.runner.RunWith;
 

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/72684dfc/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/TestCount.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/TestCount.java b/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/TestCount.java
index 0b947c4..a3bc3b9 100644
--- a/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/TestCount.java
+++ b/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/TestCount.java
@@ -20,7 +20,6 @@ package org.apache.usergrid.persistence.graph.serialization;
 
 
 import java.util.ArrayList;
-import java.util.Iterator;
 import java.util.List;
 
 import org.junit.Test;

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/72684dfc/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/impl/shard/EdgeShardSerializationTest.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/impl/shard/EdgeShardSerializationTest.java b/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/impl/shard/EdgeShardSerializationTest.java
index 479e1bf..da57b0a 100644
--- a/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/impl/shard/EdgeShardSerializationTest.java
+++ b/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/impl/shard/EdgeShardSerializationTest.java
@@ -29,10 +29,10 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
+import org.apache.usergrid.persistence.collection.guice.MigrationManagerRule;
+import org.apache.usergrid.persistence.core.cassandra.CassandraRule;
 import org.apache.usergrid.persistence.core.cassandra.ITRunner;
 import org.apache.usergrid.persistence.core.scope.ApplicationScope;
-import org.apache.usergrid.persistence.core.cassandra.CassandraRule;
-import org.apache.usergrid.persistence.collection.guice.MigrationManagerRule;
 import org.apache.usergrid.persistence.graph.guice.TestGraphModule;
 import org.apache.usergrid.persistence.model.entity.Id;
 import org.apache.usergrid.persistence.model.util.UUIDGenerator;
@@ -49,8 +49,8 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 
-@RunWith( ITRunner.class )
-@UseModules( { TestGraphModule.class } )
+@RunWith(ITRunner.class)
+@UseModules({ TestGraphModule.class })
 public class EdgeShardSerializationTest {
 
     @ClassRule
@@ -86,39 +86,54 @@ public class EdgeShardSerializationTest {
 
         final Id now = createId( "test" );
 
-        final long slice1 = 1000l;
+        final long timestamp = 10000l;
+
+        final Shard shard1 = new Shard( 1000l, timestamp, false );
 
-        final long slice2 = slice1 * 2;
+        final Shard shard2 = new Shard( shard1.getShardIndex() * 2, timestamp, true );
 
-        final long slice3 = slice2 * 2;
+        final Shard shard3 = new Shard( shard2.getShardIndex() * 2, timestamp, false );
 
-        String[] types = { "edgeType", "subType" };
+        final DirectedEdgeMeta sourceEdgeMeta = DirectedEdgeMeta.fromSourceNodeTargetType(now,  "edgeType", "subType"  );
 
-        MutationBatch batch = edgeShardSerialization.writeEdgeMeta( scope, now, slice1, types );
+        MutationBatch batch = edgeShardSerialization.writeShardMeta( scope, shard1, sourceEdgeMeta  );
 
-        batch.mergeShallow( edgeShardSerialization.writeEdgeMeta( scope, now, slice2, types ) );
+        batch.mergeShallow( edgeShardSerialization.writeShardMeta( scope, shard2, sourceEdgeMeta ) );
 
-        batch.mergeShallow( edgeShardSerialization.writeEdgeMeta( scope, now, slice3, types ) );
+        batch.mergeShallow( edgeShardSerialization.writeShardMeta( scope, shard3, sourceEdgeMeta ) );
 
         batch.execute();
 
 
-        Iterator<Long> results = edgeShardSerialization.getEdgeMetaData( scope, now, Optional.<Long>absent(), types );
+        Iterator<Shard> results =
+                edgeShardSerialization.getShardMetaData( scope, Optional.<Shard>absent(), sourceEdgeMeta );
+
 
-        assertEquals( slice3, results.next().longValue() );
+        assertEquals( shard3, results.next() );
 
-        assertEquals( slice2, results.next().longValue() );
+        assertEquals( shard2, results.next() );
 
-        assertEquals( slice1, results.next().longValue() );
+        assertEquals( shard1, results.next() );
+
+
+        assertFalse( results.hasNext() );
+
+        final DirectedEdgeMeta targetEdgeMeta = DirectedEdgeMeta.fromTargetNodeSourceType( now,  "edgeType", "subType"  );
+
+        //test we get nothing with the other node type
+        results =
+                edgeShardSerialization.getShardMetaData( scope, Optional.<Shard>absent(), targetEdgeMeta );
 
         assertFalse( results.hasNext() );
 
+
         //test paging and size
-        results = edgeShardSerialization.getEdgeMetaData( scope, now, Optional.of( slice2 ), types );
+        results = edgeShardSerialization.getShardMetaData( scope, Optional.of( shard2 ), sourceEdgeMeta );
 
-        assertEquals( slice2, results.next().longValue() );
+        assertEquals( shard2, results.next() );
 
-        assertEquals( slice1, results.next().longValue() );
+
+        assertEquals( shard1, results.next() );
 
 
         assertFalse( results.hasNext() );
@@ -130,50 +145,71 @@ public class EdgeShardSerializationTest {
 
         final Id now = createId( "test" );
 
-        final long slice1 = 1000l;
 
-        final long slice2 = slice1 * 2;
+        final long timestamp = 10000l;
+
+        final Shard shard1 = new Shard( 1000l, timestamp, false );
+
+        final Shard shard2 = new Shard( shard1.getShardIndex() * 2, timestamp, true );
 
-        final long slice3 = slice2 * 2;
+        final Shard shard3 = new Shard( shard2.getShardIndex() * 2, timestamp, false );
 
-        String[] types = { "edgeType", "subType" };
 
-        MutationBatch batch = edgeShardSerialization.writeEdgeMeta( scope, now, slice1, types );
+        final DirectedEdgeMeta sourceEdgeMeta = DirectedEdgeMeta.fromSourceNodeTargetType(now,  "edgeType", "subType"  );
 
-        batch.mergeShallow( edgeShardSerialization.writeEdgeMeta( scope, now, slice2, types ) );
 
-        batch.mergeShallow( edgeShardSerialization.writeEdgeMeta( scope, now, slice3, types ) );
+        MutationBatch batch =
+                edgeShardSerialization.writeShardMeta( scope, shard1, sourceEdgeMeta );
+
+        batch.mergeShallow(
+                edgeShardSerialization.writeShardMeta( scope, shard2, sourceEdgeMeta ) );
+
+        batch.mergeShallow(
+                edgeShardSerialization.writeShardMeta( scope, shard3, sourceEdgeMeta ) );
 
         batch.execute();
 
 
-        Iterator<Long> results = edgeShardSerialization.getEdgeMetaData( scope, now, Optional.<Long>absent(), types );
+        Iterator<Shard> results =
+                edgeShardSerialization.getShardMetaData( scope, Optional.<Shard>absent(), sourceEdgeMeta );
 
-        assertEquals( slice3, results.next().longValue() );
+        assertEquals( shard3, results.next() );
 
-        assertEquals( slice2, results.next().longValue() );
+        assertEquals( shard2, results.next() );
 
-        assertEquals( slice1, results.next().longValue() );
+        assertEquals( shard1, results.next() );
 
         assertFalse( results.hasNext() );
 
+        //test nothing with other type
+
+        final DirectedEdgeMeta targetEdgeMeta = DirectedEdgeMeta.fromTargetNodeSourceType( now,  "edgeType", "subType"  );
+
+        results =
+                edgeShardSerialization.getShardMetaData( scope, Optional.<Shard>absent(), targetEdgeMeta );
+
+        assertFalse( results.hasNext() );
+
+
         //test paging and size
-        edgeShardSerialization.removeEdgeMeta( scope, now, slice1, types ).execute();
+        edgeShardSerialization.removeShardMeta( scope, shard1, sourceEdgeMeta ).execute();
 
-        results = edgeShardSerialization.getEdgeMetaData( scope, now,Optional.<Long>absent(), types );
+        results =
+                edgeShardSerialization.getShardMetaData( scope, Optional.<Shard>absent(), sourceEdgeMeta );
 
-        assertEquals( slice3, results.next().longValue() );
+        assertEquals( shard3, results.next() );
 
-        assertEquals( slice2, results.next().longValue() );
+        assertEquals( shard2, results.next() );
 
         assertFalse( results.hasNext() );
 
 
-        edgeShardSerialization.removeEdgeMeta( scope, now, slice2, types ).execute();
+        edgeShardSerialization.removeShardMeta( scope, shard2, sourceEdgeMeta ).execute();
 
-        edgeShardSerialization.removeEdgeMeta( scope, now, slice3, types ).execute();
+        edgeShardSerialization.removeShardMeta( scope, shard3, sourceEdgeMeta ).execute();
 
-        results = edgeShardSerialization.getEdgeMetaData( scope, now, Optional.<Long>absent(), types );
+        results =
+                edgeShardSerialization.getShardMetaData( scope, Optional.<Shard>absent(), sourceEdgeMeta );
 
 
         assertFalse( results.hasNext() );

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/72684dfc/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/impl/shard/NodeShardAllocationTest.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/impl/shard/NodeShardAllocationTest.java b/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/impl/shard/NodeShardAllocationTest.java
index 5c846f1..e4c7627 100644
--- a/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/impl/shard/NodeShardAllocationTest.java
+++ b/stack/corepersistence/graph/src/test/java/org/apache/usergrid/persistence/graph/serialization/impl/shard/NodeShardAllocationTest.java
@@ -20,9 +20,9 @@ package org.apache.usergrid.persistence.graph.serialization.impl.shard;
 
 
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Iterator;
-import java.util.List;
 
 import org.junit.Before;
 import org.junit.Test;
@@ -31,22 +31,24 @@ import org.mockito.ArgumentCaptor;
 import org.apache.usergrid.persistence.core.consistency.TimeService;
 import org.apache.usergrid.persistence.core.scope.ApplicationScope;
 import org.apache.usergrid.persistence.graph.GraphFig;
-import org.apache.usergrid.persistence.graph.serialization.impl.shard.count.NodeShardCounterSerialization;
-import org.apache.usergrid.persistence.graph.serialization.impl.shard.count.ShardKey;
+import org.apache.usergrid.persistence.graph.MarkedEdge;
+import org.apache.usergrid.persistence.graph.SearchByIdType;
+import org.apache.usergrid.persistence.graph.exception.GraphRuntimeException;
+import org.apache.usergrid.persistence.graph.impl.SimpleMarkedEdge;
 import org.apache.usergrid.persistence.graph.serialization.impl.shard.impl.NodeShardAllocationImpl;
 import org.apache.usergrid.persistence.model.entity.Id;
 import org.apache.usergrid.persistence.model.util.UUIDGenerator;
 
 import com.google.common.base.Optional;
-import com.netflix.astyanax.Keyspace;
 import com.netflix.astyanax.MutationBatch;
+import com.netflix.astyanax.connectionpool.exceptions.ConnectionException;
 
 import static junit.framework.TestCase.assertTrue;
 import static org.apache.usergrid.persistence.graph.test.util.EdgeTestUtils.createId;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.fail;
 import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.eq;
 import static org.mockito.Matchers.same;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
@@ -76,68 +78,63 @@ public class NodeShardAllocationTest {
 
         when( graphFig.getShardCacheSize() ).thenReturn( 10000l );
         when( graphFig.getShardSize() ).thenReturn( 20000l );
-        when( graphFig.getShardCacheTimeout()).thenReturn( 30000l );
+
+        final long timeout = 30000;
+        when( graphFig.getShardCacheTimeout() ).thenReturn( timeout );
+        when( graphFig.getShardMinDelta() ).thenReturn( timeout * 2 );
     }
 
 
     @Test
-    public void noShards() {
+    public void minTime() {
         final EdgeShardSerialization edgeShardSerialization = mock( EdgeShardSerialization.class );
 
-        final NodeShardApproximation nodeShardCounterSerialization =
-                mock( NodeShardApproximation.class );
+        final EdgeColumnFamilies edgeColumnFamilies = mock( EdgeColumnFamilies.class );
 
+        final ShardedEdgeSerialization shardedEdgeSerialization = mock( ShardedEdgeSerialization.class );
 
-        final TimeService timeService = mock( TimeService.class );
+        final NodeShardApproximation nodeShardCounterSerialization = mock( NodeShardApproximation.class );
 
-        final Keyspace keyspace = mock( Keyspace.class );
 
-        final MutationBatch batch = mock( MutationBatch.class );
+        final TimeService timeService = mock( TimeService.class );
 
-        when( keyspace.prepareMutationBatch() ).thenReturn( batch );
 
         NodeShardAllocation approximation =
-                new NodeShardAllocationImpl( edgeShardSerialization, nodeShardCounterSerialization, timeService,
-                        graphFig );
+                new NodeShardAllocationImpl( edgeShardSerialization, edgeColumnFamilies, shardedEdgeSerialization,
+                        nodeShardCounterSerialization, timeService, graphFig );
 
-        final Id nodeId = createId( "test" );
-        final String type = "type";
-        final String subType = "subType";
 
-        /**
-         * Mock up returning an empty iterator, our audit shouldn't create a new shard
-         */
-        when( edgeShardSerialization
-                .getEdgeMetaData( same( scope ), same( nodeId ), any( Optional.class ),  same( type ),
-                        same( subType ) ) ).thenReturn( Collections.<Long>emptyList().iterator() );
+        final long timeservicetime = System.currentTimeMillis();
 
-        final boolean result = approximation.auditMaxShard( scope, nodeId, type, subType );
+        when( timeService.getCurrentTime() ).thenReturn( timeservicetime );
 
-        assertFalse( "No shard allocated", result );
+        final long expected = timeservicetime - 2 * graphFig.getShardCacheTimeout();
+
+        final long returned = approximation.getMinTime();
+
+        assertEquals( "Correct time was returned", expected, returned );
     }
 
 
+
     @Test
-    public void existingFutureShard() {
+    public void existingFutureShardSameTime() {
         final EdgeShardSerialization edgeShardSerialization = mock( EdgeShardSerialization.class );
 
-        final NodeShardApproximation nodeShardCounterSerialization =
-                mock( NodeShardApproximation.class );
-
+        final EdgeColumnFamilies edgeColumnFamilies = mock( EdgeColumnFamilies.class );
 
-        final TimeService timeService = mock( TimeService.class );
+        final ShardedEdgeSerialization shardedEdgeSerialization = mock( ShardedEdgeSerialization.class );
 
-        final Keyspace keyspace = mock( Keyspace.class );
+        final NodeShardApproximation nodeShardCounterSerialization = mock( NodeShardApproximation.class );
 
 
-        final MutationBatch batch = mock( MutationBatch.class );
+        final TimeService timeService = mock( TimeService.class );
 
-        when( keyspace.prepareMutationBatch() ).thenReturn( batch );
 
 
         NodeShardAllocation approximation =
-                new NodeShardAllocationImpl( edgeShardSerialization, nodeShardCounterSerialization, timeService,
-                        graphFig );
+                new NodeShardAllocationImpl( edgeShardSerialization, edgeColumnFamilies, shardedEdgeSerialization,
+                        nodeShardCounterSerialization, timeService, graphFig );
 
         final Id nodeId = createId( "test" );
         final String type = "type";
@@ -148,16 +145,16 @@ public class NodeShardAllocationTest {
 
         when( timeService.getCurrentTime() ).thenReturn( timeservicetime );
 
-        final long futureShard =  timeservicetime + graphFig.getShardCacheTimeout() * 2 ;
+        final Shard firstShard = new Shard(0l, 0l, true);
+        final Shard futureShard = new Shard( 10000l, timeservicetime, false );
 
-        /**
-         * Mock up returning a min shard, and a future shard
-         */
-        when( edgeShardSerialization
-                .getEdgeMetaData( same( scope ), same( nodeId ), any( Optional.class ),  same( type ),
-                        same( subType ) ) ).thenReturn( Arrays.asList( futureShard ).iterator() );
+        final ShardEntryGroup shardEntryGroup = new ShardEntryGroup( 1000l );
+        shardEntryGroup.addShard( futureShard );
+        shardEntryGroup.addShard( firstShard );
+
+        final DirectedEdgeMeta targetEdgeMeta = DirectedEdgeMeta.fromSourceNodeTargetType( nodeId, type, subType );
 
-        final boolean result = approximation.auditMaxShard( scope, nodeId, type, subType );
+        final boolean result = approximation.auditShard( scope, shardEntryGroup, targetEdgeMeta );
 
         assertFalse( "No shard allocated", result );
     }
@@ -167,22 +164,18 @@ public class NodeShardAllocationTest {
     public void lowCountFutureShard() {
         final EdgeShardSerialization edgeShardSerialization = mock( EdgeShardSerialization.class );
 
-        final NodeShardApproximation nodeShardApproximation =
-                mock( NodeShardApproximation.class );
-
-
-        final TimeService timeService = mock( TimeService.class );
+        final EdgeColumnFamilies edgeColumnFamilies = mock( EdgeColumnFamilies.class );
 
-        final Keyspace keyspace = mock( Keyspace.class );
+        final ShardedEdgeSerialization shardedEdgeSerialization = mock( ShardedEdgeSerialization.class );
 
-        final MutationBatch batch = mock( MutationBatch.class );
+        final NodeShardApproximation nodeShardApproximation = mock( NodeShardApproximation.class );
 
-        when( keyspace.prepareMutationBatch() ).thenReturn( batch );
 
+        final TimeService timeService = mock( TimeService.class );
 
         NodeShardAllocation approximation =
-                new NodeShardAllocationImpl( edgeShardSerialization, nodeShardApproximation, timeService,
-                        graphFig );
+                new NodeShardAllocationImpl( edgeShardSerialization, edgeColumnFamilies, shardedEdgeSerialization,
+                        nodeShardApproximation, timeService, graphFig );
 
         final Id nodeId = createId( "test" );
         final String type = "type";
@@ -193,23 +186,21 @@ public class NodeShardAllocationTest {
 
         when( timeService.getCurrentTime() ).thenReturn( timeservicetime );
 
+        final Shard futureShard = new Shard( 10000l, timeservicetime, true );
 
-        /**
-         * Mock up returning a min shard, and a future shard
-         */
-        when( edgeShardSerialization
-                .getEdgeMetaData( same( scope ), same( nodeId ), any( Optional.class ),  same( type ),
-                        same( subType ) ) ).thenReturn( Arrays.asList( 0l ).iterator() );
+        final ShardEntryGroup shardEntryGroup = new ShardEntryGroup( 1000l );
+        shardEntryGroup.addShard( futureShard );
+
+        final DirectedEdgeMeta targetEdgeMeta = DirectedEdgeMeta.fromSourceNodeTargetType( nodeId, type, subType );
 
 
         //return a shard size < our max by 1
 
         final long count = graphFig.getShardSize() - 1;
 
-        when( nodeShardApproximation.getCount(scope, nodeId, 0l, type, subType ))
-                                           .thenReturn( count );
+        when( nodeShardApproximation.getCount( scope, futureShard, targetEdgeMeta ) ).thenReturn( count );
 
-        final boolean result = approximation.auditMaxShard( scope, nodeId, type, subType );
+        final boolean result = approximation.auditShard( scope, shardEntryGroup, targetEdgeMeta );
 
         assertFalse( "Shard allocated", result );
     }
@@ -219,22 +210,18 @@ public class NodeShardAllocationTest {
     public void equalCountFutureShard() {
         final EdgeShardSerialization edgeShardSerialization = mock( EdgeShardSerialization.class );
 
-        final NodeShardApproximation nodeShardApproximation =
-                mock( NodeShardApproximation.class );
+        final EdgeColumnFamilies edgeColumnFamilies = mock( EdgeColumnFamilies.class );
 
+        final ShardedEdgeSerialization shardedEdgeSerialization = mock( ShardedEdgeSerialization.class );
 
-        final TimeService timeService = mock( TimeService.class );
-
-        final Keyspace keyspace = mock( Keyspace.class );
+        final NodeShardApproximation nodeShardApproximation = mock( NodeShardApproximation.class );
 
-        final MutationBatch batch = mock(MutationBatch.class);
-
-        when(keyspace.prepareMutationBatch()).thenReturn( batch );
 
+        final TimeService timeService = mock( TimeService.class );
 
         NodeShardAllocation approximation =
-                new NodeShardAllocationImpl( edgeShardSerialization, nodeShardApproximation, timeService,
-                        graphFig );
+                new NodeShardAllocationImpl( edgeShardSerialization, edgeColumnFamilies, shardedEdgeSerialization,
+                        nodeShardApproximation, timeService, graphFig );
 
         final Id nodeId = createId( "test" );
         final String type = "type";
@@ -245,69 +232,75 @@ public class NodeShardAllocationTest {
 
         when( timeService.getCurrentTime() ).thenReturn( timeservicetime );
 
+        final Shard futureShard = new Shard( 0l, 0l, true );
 
-        /**
-         * Mock up returning a min shard
-         */
-        when( edgeShardSerialization
-                .getEdgeMetaData( same( scope ), same( nodeId ), any( Optional.class ),  same( type ),
-                        same( subType ) ) ).thenReturn( Arrays.asList( 0l ).iterator() );
+        final ShardEntryGroup shardEntryGroup = new ShardEntryGroup( 1000l );
+        shardEntryGroup.addShard( futureShard );
 
+        final DirectedEdgeMeta targetEdgeMeta = DirectedEdgeMeta.fromSourceNodeTargetType( nodeId, type, subType );
 
         final long shardCount = graphFig.getShardSize();
 
         //return a shard size equal to our max
-        when( nodeShardApproximation
-                .getCount(   scope , nodeId, 0l,type , subType  ))
-                .thenReturn( shardCount );
+        when( nodeShardApproximation.getCount( scope, futureShard, targetEdgeMeta ) ).thenReturn( shardCount );
 
-        ArgumentCaptor<Long> newUUIDValue = ArgumentCaptor.forClass( Long.class );
+        ArgumentCaptor<Shard> shardValue = ArgumentCaptor.forClass( Shard.class );
 
 
         //mock up our mutation
-        when( edgeShardSerialization
-                .writeEdgeMeta( same( scope ), same( nodeId ), newUUIDValue.capture(), same( type ), same( subType ) ) )
+        when( edgeShardSerialization.writeShardMeta( same( scope ), shardValue.capture(), same( targetEdgeMeta ) ) )
                 .thenReturn( mock( MutationBatch.class ) );
 
 
-        final boolean result = approximation.auditMaxShard( scope, nodeId, type, subType );
+        final SimpleMarkedEdge returnedEdge =
+                new SimpleMarkedEdge( nodeId, type, createId( "subType" ), 10005l, false );
+
+        final Iterator<MarkedEdge> edgeIterator = Collections.singleton( ( MarkedEdge ) returnedEdge ).iterator();
+
+        //mock up returning the value
+        when( shardedEdgeSerialization
+                .getEdgesFromSourceByTargetType( same( edgeColumnFamilies ), same( scope ), any( SearchByIdType.class ),
+                        any( Iterator.class ) ) ).thenReturn( edgeIterator );
+
+
+        final boolean result = approximation.auditShard( scope, shardEntryGroup, targetEdgeMeta );
 
         assertTrue( "Shard allocated", result );
 
         //check our new allocated UUID
 
-        final long expectedTime = timeservicetime + 2 * graphFig.getShardCacheTimeout();
 
-        final long savedTimestamp = newUUIDValue.getValue();
+        final long savedTimestamp = shardValue.getValue().getCreatedTime();
 
 
+        assertEquals( "Expected time service time", timeservicetime, savedTimestamp );
 
-        assertEquals( "Expected at 2x timeout generated", expectedTime, savedTimestamp );
-    }
 
+        //now check our max value was set
 
+        final long savedShardPivot = shardValue.getValue().getShardIndex();
+
+        assertEquals( "Expected max value to be the same", returnedEdge.getTimestamp(), savedShardPivot );
+    }
 
 
     @Test
     public void futureCountShardCleanup() {
         final EdgeShardSerialization edgeShardSerialization = mock( EdgeShardSerialization.class );
 
-        final NodeShardApproximation nodeShardApproximation =
-                mock( NodeShardApproximation.class );
+        final EdgeColumnFamilies edgeColumnFamilies = mock( EdgeColumnFamilies.class );
 
+        final ShardedEdgeSerialization shardedEdgeSerialization = mock( ShardedEdgeSerialization.class );
 
-        final TimeService timeService = mock( TimeService.class );
-
-        final Keyspace keyspace = mock( Keyspace.class );
+        final NodeShardApproximation nodeShardApproximation = mock( NodeShardApproximation.class );
 
-        final MutationBatch batch = mock(MutationBatch.class);
 
-        when(keyspace.prepareMutationBatch()).thenReturn( batch );
+        final TimeService timeService = mock( TimeService.class );
 
 
         NodeShardAllocation approximation =
-                new NodeShardAllocationImpl( edgeShardSerialization, nodeShardApproximation, timeService,
-                        graphFig );
+                new NodeShardAllocationImpl( edgeShardSerialization, edgeColumnFamilies, shardedEdgeSerialization,
+                        nodeShardApproximation, timeService, graphFig );
 
         final Id nodeId = createId( "test" );
         final String type = "type";
@@ -315,120 +308,263 @@ public class NodeShardAllocationTest {
 
 
         /**
-         * Use the time service to generate UUIDS
+         * Use the time service to generate timestamps
          */
-        final long timeservicetime = System.currentTimeMillis();
+        final long timeservicetime = 10000;
 
 
         when( timeService.getCurrentTime() ).thenReturn( timeservicetime );
 
-        assertTrue("Shard cache mocked", graphFig.getShardCacheTimeout() > 0);
+        assertTrue( "Shard cache mocked", graphFig.getShardCacheTimeout() > 0 );
 
 
         /**
          * Simulates clock drift when 2 nodes create future shards near one another
          */
-        final long futureTime = timeService.getCurrentTime()  + 2 * graphFig.getShardCacheTimeout();
+        final long minDelta = graphFig.getShardMinDelta();
 
 
+        final Shard minShard = new Shard( 0l, 0l, true );
+
+        //a shard that isn't our minimum, but exists after compaction
+        final Shard compactedShard = new Shard( 5000, 1000, true );
+
         /**
-         * Simulate slow node
+         * Simulate different node time allocation
          */
-        final long futureShard1 = futureTime - 1;
 
-        final long futureShard2 = futureTime + 10000;
+        final long minTime = 10000;
+        //our second shard is the "oldest", and hence should be returned in the iterator.  Future shard 1 and 3
+        // should be removed
+
+        //this should get dropped, It's allocated after future shard2 even though the time is less
+        final Shard futureShard1 = new Shard( 10000, minTime + minDelta, false );
 
-        final long futureShard3 = futureShard2 + 10000;
+        //should get kept.
+        final Shard futureShard2 = new Shard( 10005, minTime, false );
 
+        //should be removed
+        final Shard futureShard3 = new Shard( 10010, minTime + minDelta / 2, false );
 
-        final int pageSize = 100;
+        final DirectedEdgeMeta directedEdgeMeta = DirectedEdgeMeta.fromTargetNodeSourceType( nodeId, type, subType );
 
         /**
          * Mock up returning a min shard
          */
         when( edgeShardSerialization
-                .getEdgeMetaData( same( scope ), same( nodeId ), any( Optional.class ), same( type ),
-                        same( subType ) ) ).thenReturn( Arrays.asList(futureShard3, futureShard2, futureShard1, 0l).iterator() );
-
-
-
-        ArgumentCaptor<Long> newLongValue = ArgumentCaptor.forClass( Long.class );
+                .getShardMetaData( same( scope ), any( Optional.class ), same( directedEdgeMeta ) ) ).thenReturn(
+                Arrays.asList( futureShard3, futureShard2, futureShard1, compactedShard, minShard ).iterator() );
 
 
+        ArgumentCaptor<Shard> newLongValue = ArgumentCaptor.forClass( Shard.class );
 
 
         //mock up our mutation
         when( edgeShardSerialization
-                .removeEdgeMeta( same( scope ), same( nodeId ), newLongValue.capture(), same( type ), same( subType ) ) )
+                .removeShardMeta( same( scope ), newLongValue.capture(), same( directedEdgeMeta ) ) )
                 .thenReturn( mock( MutationBatch.class ) );
 
 
-        final Iterator<Long>
-                result = approximation.getShards( scope, nodeId, Optional.<Long>absent(), type, subType );
+        final Iterator<ShardEntryGroup> result =
+                approximation.getShards( scope, Optional.<Shard>absent(), directedEdgeMeta );
 
 
         assertTrue( "Shards present", result.hasNext() );
 
-        assertEquals("Only single next shard returned", futureShard1,  result.next().longValue());
 
-        assertTrue("Shards present", result.hasNext());
+        ShardEntryGroup shardEntryGroup = result.next();
 
-        assertEquals("Previous shard present", 0l, result.next().longValue());
+        assertEquals( "Future shard returned", futureShard1, shardEntryGroup.getCompactionTarget() );
 
-        assertFalse("No shards left", result.hasNext());
 
-        /**
-         * Now we need to verify that both our mutations have been added
-         */
+        //now verify all 4 are in this group.  This is because the first shard (0,0) (n-1_ may be the only shard other
+        //nodes see while we're rolling our state.  This means it should be read and merged from as well
 
-        List<Long> values = newLongValue.getAllValues();
+        Collection<Shard> writeShards = shardEntryGroup.getWriteShards( minTime + minDelta );
 
-        assertEquals("2 values removed", 2,  values.size());
+        assertEquals( "Shard size as expected", 4, writeShards.size() );
 
-        assertEquals("Deleted Max Future", futureShard3, values.get( 0 ).longValue());
-        assertEquals("Deleted Next Future", futureShard2, values.get( 1 ).longValue());
+        assertTrue( writeShards.contains( futureShard1 ) );
+        assertTrue( writeShards.contains( futureShard2 ) );
+        assertTrue( writeShards.contains( futureShard3 ) );
+        assertTrue( writeShards.contains( compactedShard ) );
+
+
+        Collection<Shard> readShards = shardEntryGroup.getReadShards();
+
+        assertEquals( "Shard size as expected", 4, readShards.size() );
+
+        assertTrue( readShards.contains( futureShard1 ) );
+        assertTrue( readShards.contains( futureShard2 ) );
+        assertTrue( readShards.contains( futureShard3 ) );
+        assertTrue( readShards.contains( compactedShard ) );
+
+
+        assertTrue( "Shards present", result.hasNext() );
+
+        shardEntryGroup = result.next();
 
-    }
 
+        writeShards = shardEntryGroup.getWriteShards( minTime + minDelta );
 
 
+        assertTrue( "Previous shard present", writeShards.contains( minShard ) );
+
+
+        writeShards = shardEntryGroup.getReadShards();
+
+
+        assertTrue( "Previous shard present", writeShards.contains( minShard ) );
+
+
+        assertFalse( "No shards left", result.hasNext() );
+    }
+
 
     @Test
-    public void noShardsReturns() {
+    public void noShardsReturns() throws ConnectionException {
         final EdgeShardSerialization edgeShardSerialization = mock( EdgeShardSerialization.class );
 
-        final NodeShardApproximation nodeShardApproximation =
-                mock( NodeShardApproximation.class );
+        final EdgeColumnFamilies edgeColumnFamilies = mock( EdgeColumnFamilies.class );
+
+        final ShardedEdgeSerialization shardedEdgeSerialization = mock( ShardedEdgeSerialization.class );
+
+        final NodeShardApproximation nodeShardApproximation = mock( NodeShardApproximation.class );
 
 
         final TimeService timeService = mock( TimeService.class );
 
-        final Keyspace keyspace = mock( Keyspace.class );
+        when( timeService.getCurrentTime() ).thenReturn( 10000l );
 
         final MutationBatch batch = mock( MutationBatch.class );
 
-        when( keyspace.prepareMutationBatch() ).thenReturn( batch );
-
         NodeShardAllocation approximation =
-                new NodeShardAllocationImpl( edgeShardSerialization, nodeShardApproximation, timeService,
-                        graphFig );
+                new NodeShardAllocationImpl( edgeShardSerialization, edgeColumnFamilies, shardedEdgeSerialization,
+                        nodeShardApproximation, timeService, graphFig );
 
         final Id nodeId = createId( "test" );
         final String type = "type";
         final String subType = "subType";
 
+        final DirectedEdgeMeta directedEdgeMeta = DirectedEdgeMeta.fromTargetNodeSourceType( nodeId, type, subType );
+
+
         /**
          * Mock up returning an empty iterator, our audit shouldn't create a new shard
          */
         when( edgeShardSerialization
-                .getEdgeMetaData( same( scope ), same( nodeId ), any( Optional.class ),  same( type ),
-                        same( subType ) ) ).thenReturn( Collections.<Long>emptyList().iterator() );
+                .getShardMetaData( same( scope ), any( Optional.class ), same( directedEdgeMeta ) ) )
+                .thenReturn( Collections.<Shard>emptyList().iterator() );
+
+
+        ArgumentCaptor<Shard> shardArgumentCaptor = ArgumentCaptor.forClass( Shard.class );
+
+        when( edgeShardSerialization
+                .writeShardMeta( same( scope ), shardArgumentCaptor.capture(), same( directedEdgeMeta ) ) )
+                .thenReturn( batch );
+
 
-        final Iterator<Long> result = approximation.getShards( scope, nodeId, Optional.<Long>absent(), type, subType );
+        final Iterator<ShardEntryGroup> result =
+                approximation.getShards( scope, Optional.<Shard>absent(), directedEdgeMeta );
 
-        assertEquals("0 shard allocated", 0l, result.next().longValue());
 
-        assertFalse( "No shard allocated", result.hasNext() );
+        ShardEntryGroup shardEntryGroup = result.next();
+
+        final Shard rootShard = new Shard( 0, 0, true );
+
+        assertEquals( "Shard size expected", 1, shardEntryGroup.entrySize() );
+
+
+        //ensure we persisted the new shard.
+        assertEquals( "Root shard was persisted", rootShard, shardArgumentCaptor.getValue() );
+
+
+        //now verify all 4 are in this group.  This is because the first shard (0,0) (n-1_ may be the only shard other
+        //nodes see while we're rolling our state.  This means it should be read and merged from as well
+
+        Collection<Shard> writeShards = shardEntryGroup.getWriteShards( timeService.getCurrentTime() );
+
+        Collection<Shard> readShards = shardEntryGroup.getReadShards();
+
+
+        assertTrue( "root shard allocated", writeShards.contains( rootShard ) );
+
+        assertTrue( "root shard allocated", readShards.contains( rootShard ) );
+
+
+        assertFalse( "No other shard group allocated", result.hasNext() );
     }
 
+
+    @Test
+    public void invalidConfiguration() {
+
+        final GraphFig graphFig = mock( GraphFig.class );
+
+        final EdgeShardSerialization edgeShardSerialization = mock( EdgeShardSerialization.class );
+
+        final EdgeColumnFamilies edgeColumnFamilies = mock( EdgeColumnFamilies.class );
+
+        final ShardedEdgeSerialization shardedEdgeSerialization = mock( ShardedEdgeSerialization.class );
+
+        final NodeShardApproximation nodeShardApproximation = mock( NodeShardApproximation.class );
+
+
+        /**
+         * Return 100000 milliseconds
+         */
+        final TimeService timeService = mock( TimeService.class );
+
+        final long time = 100000l;
+
+        when( timeService.getCurrentTime() ).thenReturn( time );
+
+
+        final long cacheTimeout = 30000l;
+
+        when( graphFig.getShardCacheTimeout() ).thenReturn( 30000l );
+
+
+        final long tooSmallDelta = ( long ) ( ( cacheTimeout * 2 ) * .99 );
+
+        when( graphFig.getShardMinDelta() ).thenReturn( tooSmallDelta );
+
+        NodeShardAllocation approximation =
+                new NodeShardAllocationImpl( edgeShardSerialization, edgeColumnFamilies, shardedEdgeSerialization,
+                        nodeShardApproximation, timeService, graphFig );
+
+
+        /**
+         * Should throw an exception
+         */
+        try {
+            approximation.getMinTime();
+            fail( "Should have thrown a GraphRuntimeException" );
+        }
+        catch ( GraphRuntimeException gre ) {
+            //swallow
+        }
+
+        //now test something that passes.
+
+        final long minDelta = cacheTimeout * 2;
+
+        when( graphFig.getShardMinDelta() ).thenReturn( minDelta );
+
+        long returned = approximation.getMinTime();
+
+        long expectedReturned = time - minDelta;
+
+        assertEquals( expectedReturned, returned );
+
+        final long delta = cacheTimeout * 4;
+
+        when( graphFig.getShardMinDelta() ).thenReturn( delta );
+
+        returned = approximation.getMinTime();
+
+        expectedReturned = time - delta;
+
+        assertEquals( expectedReturned, returned );
+    }
 }