Skip to content

Apache Lucene open-source search software

SOLR-2592: refactor routers to separate public classes

Yonik Seeleyff17839ed65f
diff --git a/solr/solrj/src/java/org/apache/solr/common/cloud/CompositeIdRouter.java b/solr/solrj/src/java/org/apache/solr/common/cloud/CompositeIdRouter.java
new file mode 100644
index 00000000000..b00aae6c0b4
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/common/cloud/CompositeIdRouter.java
@@ -0,0 +1,136 @@
+package org.apache.solr.common.cloud;
+
+/*
+ * 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.
+ */
+
+import org.apache.solr.common.SolrInputDocument;
+import org.apache.solr.common.params.SolrParams;
+import org.apache.solr.common.util.Hash;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+//
+// user!uniqueid
+// user/4!uniqueid
+//
+public class CompositeIdRouter extends HashBasedRouter {
+ public static final String NAME = "compositeId";
+
+ private int separator = '!';
+
+ // separator used to optionally specify number of bits to allocate toward first part.
+ private int bitsSepartor = '/';
+ private int bits = 16;
+ private int mask1 = 0xffff0000;
+ private int mask2 = 0x0000ffff;
+
+ protected void setBits(int bits) {
+ this.bits = bits;
+ mask1 = -1 << (32-bits);
+ mask2 = -1 >>> bits;
+ }
+
+ protected int getBits(String firstPart, int commaIdx) {
+ int v = 0;
+ for (int idx = commaIdx + 1; idx<firstPart.length(); idx++) {
+ char ch = firstPart.charAt(idx);
+ if (ch < '0' || ch > '9') return -1;
+ v *= 10 + (ch - '0');
+ }
+ return v > 32 ? -1 : v;
+ }
+
+ @Override
+ protected int sliceHash(String id, SolrInputDocument doc, SolrParams params) {
+ int idx = id.indexOf(separator);
+ if (idx < 0) {
+ return Hash.murmurhash3_x86_32(id, 0, id.length(), 0);
+ }
+
+ int m1 = mask1;
+ int m2 = mask2;
+
+ String part1 = id.substring(0,idx);
+ int commaIdx = part1.indexOf(bitsSepartor);
+ if (commaIdx > 0) {
+ int firstBits = getBits(part1, commaIdx);
+ if (firstBits >= 0) {
+ m1 = -1 << (32-firstBits);
+ m2 = -1 >>> firstBits;
+ part1 = part1.substring(0, commaIdx);
+ }
+ }
+
+ String part2 = id.substring(idx+1);
+
+ int hash1 = Hash.murmurhash3_x86_32(part1, 0, part1.length(), 0);
+ int hash2 = Hash.murmurhash3_x86_32(part2, 0, part2.length(), 0);
+ return (hash1 & m1) | (hash2 & m2);
+ }
+
+ @Override
+ public Collection<Slice> getSearchSlices(String shardKey, SolrParams params, DocCollection collection) {
+ if (shardKey == null) {
+ // search across whole collection
+ // TODO: this may need modification in the future when shard splitting could cause an overlap
+ return collection.getSlices();
+ }
+ String id = shardKey;
+
+ int idx = shardKey.indexOf(separator);
+ if (idx < 0) {
+ // shardKey is a simple id, so don't do a range
+ return Collections.singletonList(hashToSlice(Hash.murmurhash3_x86_32(id, 0, id.length(), 0), collection));
+ }
+
+ int m1 = mask1;
+ int m2 = mask2;
+
+ String part1 = id.substring(0,idx);
+ int commaIdx = part1.indexOf(bitsSepartor);
+ if (commaIdx > 0) {
+ int firstBits = getBits(part1, commaIdx);
+ if (firstBits >= 0) {
+ m1 = -1 << (32-firstBits);
+ m2 = -1 >>> firstBits;
+ part1 = part1.substring(0, commaIdx);
+ }
+ }
+
+ // If the upper bits are 0xF0000000, the range we want to cover is
+ // 0xF0000000 0xFfffffff
+
+ int hash1 = Hash.murmurhash3_x86_32(part1, 0, part1.length(), 0);
+ int upperBits = hash1 & m1;
+ int lowerBound = upperBits;
+ int upperBound = upperBits | m2;
+ Range completeRange = new Range(lowerBound, upperBound);
+
+ List<Slice> slices = new ArrayList(1);
+ for (Slice slice : slices) {
+ Range range = slice.getRange();
+ if (range != null && range.overlaps(completeRange)) {
+ slices.add(slice);
+ }
+ }
+
+ return slices;
+ }
+}
diff --git a/solr/solrj/src/java/org/apache/solr/common/cloud/DocRouter.java b/solr/solrj/src/java/org/apache/solr/common/cloud/DocRouter.java
index a47926ab2f2..7bf05d889bb 100644
--- a/solr/solrj/src/java/org/apache/solr/common/cloud/DocRouter.java
+++ b/solr/solrj/src/java/org/apache/solr/common/cloud/DocRouter.java
@@ -163,156 +163,3 @@ public abstract class DocRouter {
}
-abstract class HashBasedRouter extends DocRouter {
-
- @Override
- public Slice getTargetSlice(String id, SolrInputDocument sdoc, SolrParams params, DocCollection collection) {
- if (id == null) id = getId(sdoc, params);
- int hash = sliceHash(id, sdoc, params);
- return hashToSlice(hash, collection);
- }
-
- protected int sliceHash(String id, SolrInputDocument sdoc, SolrParams params) {
- return Hash.murmurhash3_x86_32(id, 0, id.length(), 0);
- }
-
- protected String getId(SolrInputDocument sdoc, SolrParams params) {
- Object idObj = sdoc.getFieldValue("id"); // blech
- String id = idObj != null ? idObj.toString() : "null"; // should only happen on client side
- return id;
- }
-
- protected Slice hashToSlice(int hash, DocCollection collection) {
- for (Slice slice : collection.getSlices()) {
- DocRouter.Range range = slice.getRange();
- if (range != null && range.includes(hash)) return slice;
- }
- throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "No slice servicing hash code " + Integer.toHexString(hash) + " in " + collection);
- }
-
-
- @Override
- public Collection<Slice> getSearchSlices(String shardKey, SolrParams params, DocCollection collection) {
- if (shardKey == null) {
- // search across whole collection
- // TODO: this may need modification in the future when shard splitting could cause an overlap
- return collection.getSlices();
- }
-
- // use the shardKey as an id for plain hashing
- return Collections.singletonList(getTargetSlice(shardKey, null, params, collection));
- }
-}
-
-class PlainIdRouter extends HashBasedRouter {
- public static final String NAME = "plain";
-}
-
-//
-// user!uniqueid
-// user/4!uniqueid
-//
-class CompositeIdRouter extends HashBasedRouter {
- public static final String NAME = "compositeId";
-
- private int separator = '!';
-
- // separator used to optionally specify number of bits to allocate toward first part.
- private int bitsSepartor = '/';
- private int bits = 16;
- private int mask1 = 0xffff0000;
- private int mask2 = 0x0000ffff;
-
- protected void setBits(int bits) {
- this.bits = bits;
- mask1 = -1 << (32-bits);
- mask2 = -1 >>> bits;
- }
-
- protected int getBits(String firstPart, int commaIdx) {
- int v = 0;
- for (int idx = commaIdx + 1; idx<firstPart.length(); idx++) {
- char ch = firstPart.charAt(idx);
- if (ch < '0' || ch > '9') return -1;
- v *= 10 + (ch - '0');
- }
- return v > 32 ? -1 : v;
- }
-
- @Override
- protected int sliceHash(String id, SolrInputDocument doc, SolrParams params) {
- int idx = id.indexOf(separator);
- if (idx < 0) {
- return Hash.murmurhash3_x86_32(id, 0, id.length(), 0);
- }
-
- int m1 = mask1;
- int m2 = mask2;
-
- String part1 = id.substring(0,idx);
- int commaIdx = part1.indexOf(bitsSepartor);
- if (commaIdx > 0) {
- int firstBits = getBits(part1, commaIdx);
- if (firstBits >= 0) {
- m1 = -1 << (32-firstBits);
- m2 = -1 >>> firstBits;
- part1 = part1.substring(0, commaIdx);
- }
- }
-
- String part2 = id.substring(idx+1);
-
- int hash1 = Hash.murmurhash3_x86_32(part1, 0, part1.length(), 0);
- int hash2 = Hash.murmurhash3_x86_32(part2, 0, part2.length(), 0);
- return (hash1 & m1) | (hash2 & m2);
- }
-
- @Override
- public Collection<Slice> getSearchSlices(String shardKey, SolrParams params, DocCollection collection) {
- if (shardKey == null) {
- // search across whole collection
- // TODO: this may need modification in the future when shard splitting could cause an overlap
- return collection.getSlices();
- }
- String id = shardKey;
-
- int idx = shardKey.indexOf(separator);
- if (idx < 0) {
- // shardKey is a simple id, so don't do a range
- return Collections.singletonList(hashToSlice(Hash.murmurhash3_x86_32(id, 0, id.length(), 0), collection));
- }
-
- int m1 = mask1;
- int m2 = mask2;
-
- String part1 = id.substring(0,idx);
- int commaIdx = part1.indexOf(bitsSepartor);
- if (commaIdx > 0) {
- int firstBits = getBits(part1, commaIdx);
- if (firstBits >= 0) {
- m1 = -1 << (32-firstBits);
- m2 = -1 >>> firstBits;
- part1 = part1.substring(0, commaIdx);
- }
- }
-
- // If the upper bits are 0xF0000000, the range we want to cover is
- // 0xF0000000 0xFfffffff
-
- int hash1 = Hash.murmurhash3_x86_32(part1, 0, part1.length(), 0);
- int upperBits = hash1 & m1;
- int lowerBound = upperBits;
- int upperBound = upperBits | m2;
- Range completeRange = new Range(lowerBound, upperBound);
-
- List<Slice> slices = new ArrayList(1);
- for (Slice slice : slices) {
- Range range = slice.getRange();
- if (range != null && range.overlaps(completeRange)) {
- slices.add(slice);
- }
- }
-
- return slices;
- }
-}
diff --git a/solr/solrj/src/java/org/apache/solr/common/cloud/HashBasedRouter.java b/solr/solrj/src/java/org/apache/solr/common/cloud/HashBasedRouter.java
new file mode 100644
index 00000000000..0b1b2cba691
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/common/cloud/HashBasedRouter.java
@@ -0,0 +1,67 @@
+package org.apache.solr.common.cloud;
+
+/*
+ * 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.
+ */
+
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.SolrInputDocument;
+import org.apache.solr.common.params.SolrParams;
+import org.apache.solr.common.util.Hash;
+
+import java.util.Collection;
+import java.util.Collections;
+
+public abstract class HashBasedRouter extends DocRouter {
+
+ @Override
+ public Slice getTargetSlice(String id, SolrInputDocument sdoc, SolrParams params, DocCollection collection) {
+ if (id == null) id = getId(sdoc, params);
+ int hash = sliceHash(id, sdoc, params);
+ return hashToSlice(hash, collection);
+ }
+
+ protected int sliceHash(String id, SolrInputDocument sdoc, SolrParams params) {
+ return Hash.murmurhash3_x86_32(id, 0, id.length(), 0);
+ }
+
+ protected String getId(SolrInputDocument sdoc, SolrParams params) {
+ Object idObj = sdoc.getFieldValue("id"); // blech
+ String id = idObj != null ? idObj.toString() : "null"; // should only happen on client side
+ return id;
+ }
+
+ protected Slice hashToSlice(int hash, DocCollection collection) {
+ for (Slice slice : collection.getSlices()) {
+ Range range = slice.getRange();
+ if (range != null && range.includes(hash)) return slice;
+ }
+ throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "No slice servicing hash code " + Integer.toHexString(hash) + " in " + collection);
+ }
+
+
+ @Override
+ public Collection<Slice> getSearchSlices(String shardKey, SolrParams params, DocCollection collection) {
+ if (shardKey == null) {
+ // search across whole collection
+ // TODO: this may need modification in the future when shard splitting could cause an overlap
+ return collection.getSlices();
+ }
+
+ // use the shardKey as an id for plain hashing
+ return Collections.singletonList(getTargetSlice(shardKey, null, params, collection));
+ }
+}
diff --git a/solr/solrj/src/java/org/apache/solr/common/cloud/PlainIdRouter.java b/solr/solrj/src/java/org/apache/solr/common/cloud/PlainIdRouter.java
new file mode 100644
index 00000000000..bd14089f0da
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/common/cloud/PlainIdRouter.java
@@ -0,0 +1,23 @@
+package org.apache.solr.common.cloud;
+
+
+/*
+ * 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.
+ */
+
+public class PlainIdRouter extends HashBasedRouter {
+ public static final String NAME = "plain";
+}