geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dschnei...@apache.org
Subject [1/2] geode git commit: implemented string prefix partitioned resolver and added unit test
Date Tue, 06 Jun 2017 00:09:39 GMT
Repository: geode
Updated Branches:
  refs/heads/feature/GEODE-3027 [created] 6f2b73823


implemented string prefix partitioned resolver and added unit test


Project: http://git-wip-us.apache.org/repos/asf/geode/repo
Commit: http://git-wip-us.apache.org/repos/asf/geode/commit/6e338dc9
Tree: http://git-wip-us.apache.org/repos/asf/geode/tree/6e338dc9
Diff: http://git-wip-us.apache.org/repos/asf/geode/diff/6e338dc9

Branch: refs/heads/feature/GEODE-3027
Commit: 6e338dc9b6921748c85cfe076b0e6a89f0eb13df
Parents: 40d36ba
Author: Darrel Schneider <dschneider@pivotal.io>
Authored: Mon Jun 5 17:07:46 2017 -0700
Committer: Darrel Schneider <dschneider@pivotal.io>
Committed: Mon Jun 5 17:07:46 2017 -0700

----------------------------------------------------------------------
 .../cache/StringPrefixPartitionResolver.java    | 82 +++++++++++++++++++
 .../StringPrefixPartitionResolverJUnitTest.java | 85 ++++++++++++++++++++
 2 files changed, 167 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/geode/blob/6e338dc9/geode-core/src/main/java/org/apache/geode/cache/StringPrefixPartitionResolver.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/org/apache/geode/cache/StringPrefixPartitionResolver.java
b/geode-core/src/main/java/org/apache/geode/cache/StringPrefixPartitionResolver.java
new file mode 100644
index 0000000..3093788
--- /dev/null
+++ b/geode-core/src/main/java/org/apache/geode/cache/StringPrefixPartitionResolver.java
@@ -0,0 +1,82 @@
+/*
+ * 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.geode.cache;
+
+/**
+ * This partition resolver requires every key of the partitioned region
+ * to be an instance of String and to contain at least one ":" delimiter.
+ * The prefix, the substring of the key that precedes the first delimiter,
+ * is returned by getRoutingObject.
+ * 
+ * @since Geode 1.2.0
+ */
+public class StringPrefixPartitionResolver implements PartitionResolver<String, Object>
{
+
+  /**
+   * The default delimiter is ":".
+   * Currently this class only uses the default delimiter
+   * but in a future release configuring the delimiter may
+   * be supported.
+   */
+  public static final String DEFAULT_DELIMITER = ":";
+  /**
+   * Creates a prefix resolver with the default delimiter.
+   */
+  public StringPrefixPartitionResolver() {
+  }
+
+  /**
+   * Returns the prefix of the String key that precedes the first ":" in the key.
+   * @throws ClassCastException if the key is not an instance of String
+   * @throws IllegalArgumentException if the key does not contain at least one ":".
+   */
+  @Override
+  public Object getRoutingObject(EntryOperation<String, Object> opDetails) {
+    String key = opDetails.getKey();
+    String delimiter = getDelimiter();
+    int idx = key.indexOf(delimiter);
+    if (idx == -1) {
+      throw new IllegalArgumentException("The key \"" + key + "\" does not contains the \""
+ delimiter + "\" delimiter.");
+    }
+    return key.substring(0, idx);
+  }
+
+  @Override
+  public java.lang.String getName() {
+    return getClass().getName() + getDelimiter();
+  }
+
+  private java.lang.String getDelimiter() {
+    return DEFAULT_DELIMITER;
+  }
+
+  @Override
+  public boolean equals(Object o) {
+    if (this == o) {
+      return true;
+    }
+    if (!(o instanceof StringPrefixPartitionResolver)) {
+      return false;
+    }
+    StringPrefixPartitionResolver other = (StringPrefixPartitionResolver) o;
+    return other.getName().equals(getName());
+  }
+  
+  @Override
+  public void close() {
+  }
+}

http://git-wip-us.apache.org/repos/asf/geode/blob/6e338dc9/geode-core/src/test/java/org/apache/geode/cache/StringPrefixPartitionResolverJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/org/apache/geode/cache/StringPrefixPartitionResolverJUnitTest.java
b/geode-core/src/test/java/org/apache/geode/cache/StringPrefixPartitionResolverJUnitTest.java
new file mode 100644
index 0000000..6be1c22
--- /dev/null
+++ b/geode-core/src/test/java/org/apache/geode/cache/StringPrefixPartitionResolverJUnitTest.java
@@ -0,0 +1,85 @@
+/*
+ * 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.geode.cache;
+
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+import org.apache.geode.internal.cache.EntryOperationImpl;
+
+public class StringPrefixPartitionResolverJUnitTest {
+
+  @Test
+  public void testGetName() {
+    assertEquals("org.apache.geode.cache.StringPrefixPartitionResolver:", (new StringPrefixPartitionResolver()).getName());
+  }
+  @Test
+  public void testEquals() {
+    StringPrefixPartitionResolver pr1 = new StringPrefixPartitionResolver();
+    assertEquals(true, pr1.equals(pr1));
+    StringPrefixPartitionResolver pr2 = new StringPrefixPartitionResolver();
+    assertEquals(true, pr1.equals(pr2));
+    assertEquals(false, pr1.equals(new Object()));
+  }
+  
+  @Test
+  public void testNonStringKey() {
+    @SuppressWarnings("unchecked")
+    EntryOperation<String, Object> eo = new EntryOperationImpl(null, null, new Object(),
null, null);
+    StringPrefixPartitionResolver pr = new StringPrefixPartitionResolver();
+    assertThatThrownBy(() -> pr.getRoutingObject(eo)).isInstanceOf(ClassCastException.class);
+  }
+  @Test
+  public void testNoDelimiterKey() {
+    @SuppressWarnings("unchecked")
+    EntryOperation<String, Object> eo = new EntryOperationImpl(null, null, "foobar",
null, null);
+    StringPrefixPartitionResolver pr = new StringPrefixPartitionResolver();
+    assertThatThrownBy(() -> pr.getRoutingObject(eo)).isInstanceOf(IllegalArgumentException.class)
+      .hasMessage("The key \"foobar\" does not contains the \":\" delimiter.");
+  }
+  @Test
+  public void testEmptyPrefix() {
+    @SuppressWarnings("unchecked")
+    EntryOperation<String, Object> eo = new EntryOperationImpl(null, null, ":foobar",
null, null);
+    StringPrefixPartitionResolver pr = new StringPrefixPartitionResolver();
+    assertEquals("", pr.getRoutingObject(eo));
+  }
+  @Test
+  public void testAllPrefix() {
+    @SuppressWarnings("unchecked")
+    EntryOperation<String, Object> eo = new EntryOperationImpl(null, null, "foobar:",
null, null);
+    StringPrefixPartitionResolver pr = new StringPrefixPartitionResolver();
+    assertEquals("foobar", pr.getRoutingObject(eo));
+  }
+  @Test
+  public void testSimpleKey() {
+    @SuppressWarnings("unchecked")
+    EntryOperation<String, Object> eo = new EntryOperationImpl(null, null, "1:2", null,
null);
+    StringPrefixPartitionResolver pr = new StringPrefixPartitionResolver();
+    assertEquals("1", pr.getRoutingObject(eo));
+  }
+  @Test
+  public void testMulitPrefix() {
+    @SuppressWarnings("unchecked")
+    EntryOperation<String, Object> eo = new EntryOperationImpl(null, null, "one:two:three",
null, null);
+    StringPrefixPartitionResolver pr = new StringPrefixPartitionResolver();
+    assertEquals("one", pr.getRoutingObject(eo));
+  }
+
+}


Mime
View raw message