camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [1/7] camel git commit: Add a data structure to model the way the shard list works.
Date Wed, 16 Dec 2015 13:22:46 GMT
Repository: camel
Updated Branches:
  refs/heads/master 3da84a6ba -> e3b86b977


Add a data structure to model the way the shard list works.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/8f5136af
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/8f5136af
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/8f5136af

Branch: refs/heads/master
Commit: 8f5136af0ceb93db47858b6ba96b3388ea19c0e7
Parents: 78fd81e
Author: Candle <candle@candle.me.uk>
Authored: Fri Dec 11 10:49:56 2015 +0000
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Wed Dec 16 14:19:11 2015 +0100

----------------------------------------------------------------------
 .../component/aws/ddbstream/ShardList.java      |  57 ++++++++++
 .../component/aws/ddbstream/ShardListTest.java  | 104 +++++++++++++++++++
 2 files changed, 161 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/8f5136af/components/camel-aws/src/main/java/org/apache/camel/component/aws/ddbstream/ShardList.java
----------------------------------------------------------------------
diff --git a/components/camel-aws/src/main/java/org/apache/camel/component/aws/ddbstream/ShardList.java
b/components/camel-aws/src/main/java/org/apache/camel/component/aws/ddbstream/ShardList.java
new file mode 100644
index 0000000..fb188f4
--- /dev/null
+++ b/components/camel-aws/src/main/java/org/apache/camel/component/aws/ddbstream/ShardList.java
@@ -0,0 +1,57 @@
+package org.apache.camel.component.aws.ddbstream;
+
+import com.amazonaws.services.dynamodbv2.model.Shard;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+class ShardList {
+
+    private final Map<String, Shard> shards = new HashMap<>();
+
+    void addAll(Collection<Shard> shards) {
+        for (Shard shard : shards) {
+            add(shard);
+        }
+    }
+
+    void add(Shard shard) {
+        shards.put(shard.getShardId(), shard);
+    }
+
+    Shard nextAfter(Shard previous) {
+        for (Shard shard : shards.values()) {
+            if (previous.getShardId().equals(shard.getParentShardId())) {
+                return shard;
+            }
+        }
+        throw new IllegalStateException("Unable to find the next shard for " + previous +
" in " + shards);
+    }
+
+    Shard first() {
+        for (Shard shard : shards.values()) {
+            if (!shards.containsKey(shard.getParentShardId())) {
+                return shard;
+            }
+        }
+        throw new IllegalStateException("Unable to find an unparented shard in " + shards);
+    }
+
+    /**
+     * Removes shards that are older than the provided shard.
+     * Does not remove the provided shard.
+     * @param removeBefore
+     */
+    void removeOlderThan(Shard removeBefore) {
+        String current = removeBefore.getParentShardId();
+
+        while (current != null) {
+            Shard s = shards.remove(current);
+            if (s == null) {
+                current = null;
+            } else {
+                current = s.getParentShardId();
+            }
+        }
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/8f5136af/components/camel-aws/src/test/java/org/apache/camel/component/aws/ddbstream/ShardListTest.java
----------------------------------------------------------------------
diff --git a/components/camel-aws/src/test/java/org/apache/camel/component/aws/ddbstream/ShardListTest.java
b/components/camel-aws/src/test/java/org/apache/camel/component/aws/ddbstream/ShardListTest.java
new file mode 100644
index 0000000..773f4a0
--- /dev/null
+++ b/components/camel-aws/src/test/java/org/apache/camel/component/aws/ddbstream/ShardListTest.java
@@ -0,0 +1,104 @@
+package org.apache.camel.component.aws.ddbstream;
+
+import com.amazonaws.services.dynamodbv2.model.Shard;
+import java.util.ArrayList;
+import java.util.List;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import org.junit.Test;
+
+public class ShardListTest {
+
+    @Test
+    public void nextReturnsShardWithParent() throws Exception {
+        Shard first = new Shard()
+                .withShardId("first_shard")
+                .withParentShardId("other_shard_id");
+        Shard second = new Shard()
+                .withParentShardId("first_shard")
+                .withShardId("second_shard");
+
+        ShardList shards = new ShardList();
+        shards.add(first);
+        shards.add(second);
+
+        assertThat(shards.nextAfter(first), is(second));
+    }
+
+    @Test
+    public void nextWithNullReturnsFirstKnownShard() throws Exception {
+        Shard first = new Shard()
+                .withShardId("first_shard");
+        Shard second = new Shard()
+                .withParentShardId("first_shard")
+                .withShardId("second_shard");
+
+        ShardList shards = new ShardList();
+        shards.add(first);
+        shards.add(second);
+
+        assertThat(shards.nextAfter(first), is(second));
+    }
+
+    @Test
+    public void reAddingEntriesMaintainsOrder() throws Exception {
+        Shard first = new Shard()
+                .withShardId("first_shard");
+        Shard second = new Shard()
+                .withParentShardId("first_shard")
+                .withShardId("second_shard");
+
+        ShardList shards = new ShardList();
+        shards.add(first);
+        shards.add(second);
+
+        assertThat(shards.nextAfter(first), is(second));
+
+        Shard second2 = new Shard()
+                .withParentShardId("first_shard")
+                .withShardId("second_shard");
+        Shard third = new Shard()
+                .withParentShardId("second_shard")
+                .withShardId("third_shard");
+        shards.add(second2);
+        shards.add(third);
+
+        assertThat(shards.nextAfter(first), is(second));
+        assertThat(shards.nextAfter(second), is(third));
+    }
+
+    @Test
+    public void firstShardGetsTheFirstWithoutAParent() throws Exception {
+        ShardList shards = new ShardList();
+        shards.addAll(createShards(null, "a", "b", "c", "d"));
+
+        assertThat(shards.first().getShardId(), is("a"));
+    }
+
+    @Test
+    public void firstShardGetsTheFirstWithAnUnknownParent() throws Exception {
+        ShardList shards = new ShardList();
+        shards.addAll(createShards("a", "b", "c", "d"));
+
+        assertThat(shards.first().getShardId(), is("b"));
+    }
+
+    @Test
+    public void removingShards() throws Exception {
+        ShardList shards = new ShardList();
+        shards.addAll(createShards(null, "a", "b", "c", "d"));
+        Shard removeBefore = new Shard().withShardId("c").withParentShardId("b");
+        shards.removeOlderThan(removeBefore);
+        assertThat(shards.first().getShardId(), is("c"));
+    }
+
+    List<Shard> createShards(String initialParent, String... shardIds) {
+        String previous = initialParent;
+        List<Shard> result = new ArrayList<>();
+        for (String s : shardIds) {
+            result.add(new Shard().withShardId(s).withParentShardId(previous));
+            previous = s;
+        }
+        return result;
+    }
+}
\ No newline at end of file


Mime
View raw message