incubator-blur-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From amccu...@apache.org
Subject [12/13] git commit: More updates on sorting.
Date Tue, 28 Jan 2014 18:57:44 GMT
More updates on sorting.


Project: http://git-wip-us.apache.org/repos/asf/incubator-blur/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-blur/commit/803765a0
Tree: http://git-wip-us.apache.org/repos/asf/incubator-blur/tree/803765a0
Diff: http://git-wip-us.apache.org/repos/asf/incubator-blur/diff/803765a0

Branch: refs/heads/apache-blur-0.2
Commit: 803765a02928e8f4d72ed6528b0954fbf13de646
Parents: 2f8179c
Author: Aaron McCurry <amccurry@gmail.com>
Authored: Tue Jan 28 13:43:41 2014 -0500
Committer: Aaron McCurry <amccurry@gmail.com>
Committed: Tue Jan 28 13:43:41 2014 -0500

----------------------------------------------------------------------
 .../BlurResultPeekableIteratorComparator.java   | 22 +++++---
 .../java/org/apache/blur/utils/BlurUtil.java    |  7 +++
 .../results/MultipleBlurResultIterableTest.java |  2 +-
 .../apache/blur/thrift/SortFieldComparator.java | 59 ++++++++++++++++++++
 4 files changed, 80 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/803765a0/blur-core/src/main/java/org/apache/blur/manager/results/BlurResultPeekableIteratorComparator.java
----------------------------------------------------------------------
diff --git a/blur-core/src/main/java/org/apache/blur/manager/results/BlurResultPeekableIteratorComparator.java
b/blur-core/src/main/java/org/apache/blur/manager/results/BlurResultPeekableIteratorComparator.java
index 474697f..78c7c45 100644
--- a/blur-core/src/main/java/org/apache/blur/manager/results/BlurResultPeekableIteratorComparator.java
+++ b/blur-core/src/main/java/org/apache/blur/manager/results/BlurResultPeekableIteratorComparator.java
@@ -22,6 +22,7 @@ import java.util.List;
 import org.apache.blur.thrift.generated.BlurException;
 import org.apache.blur.thrift.generated.BlurResult;
 import org.apache.blur.thrift.generated.SortField;
+import org.apache.blur.utils.BlurUtil;
 
 public class BlurResultPeekableIteratorComparator implements Comparator<PeekableIterator<BlurResult,
BlurException>> {
 
@@ -45,26 +46,29 @@ public class BlurResultPeekableIteratorComparator implements Comparator<Peekable
       int size1 = sortFields1.size();
       int size2 = sortFields2.size();
       if (size1 != size2) {
-        throw new RuntimeException("Result mismatch, sizes of sortfields must match [" +
result1 + "] ["
-            + result2 + "]");
+        throw new RuntimeException("Result mismatch, sizes of sortfields must match [" +
result1 + "] [" + result2
+            + "]");
       }
       for (int i = 0; i < size1; i++) {
         SortField sortField1 = sortFields1.get(i);
         SortField sortField2 = sortFields2.get(i);
-        int compare = sortField1.compareTo(sortField2);
+        int compare = BlurUtil.SORT_FIELD_COMPARATOR.compare(sortField1, sortField2);
         if (compare != 0) {
           return compare;
         }
       }
-    } else if (sortFields1 == null || sortFields2 == null) {
+    }
+
+    if (sortFields1 == null && sortFields2 == null) {
+      int compare = Double.compare(result2.score, result1.score);
+      if (compare == 0) {
+        return result2.locationId.compareTo(result1.locationId);
+      }
+      return compare;
+    } else {
       throw new RuntimeException("Result mismatch, one of the 2 results have null sortfields
[" + result1 + "] ["
           + result2 + "]");
     }
-    int compare = Double.compare(result2.score, result1.score);
-    if (compare == 0) {
-      return result2.locationId.compareTo(result1.locationId);
-    }
-    return compare;
   }
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/803765a0/blur-core/src/main/java/org/apache/blur/utils/BlurUtil.java
----------------------------------------------------------------------
diff --git a/blur-core/src/main/java/org/apache/blur/utils/BlurUtil.java b/blur-core/src/main/java/org/apache/blur/utils/BlurUtil.java
index 7f1baaf..01f6684 100644
--- a/blur-core/src/main/java/org/apache/blur/utils/BlurUtil.java
+++ b/blur-core/src/main/java/org/apache/blur/utils/BlurUtil.java
@@ -48,6 +48,10 @@ import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
+import java.util.SortedMap;
+import java.util.SortedSet;
+import java.util.TreeMap;
+import java.util.TreeSet;
 import java.util.UUID;
 import java.util.concurrent.Callable;
 import java.util.concurrent.ConcurrentHashMap;
@@ -81,6 +85,7 @@ import org.apache.blur.thirdparty.thrift_0_9_0.TException;
 import org.apache.blur.thirdparty.thrift_0_9_0.protocol.TJSONProtocol;
 import org.apache.blur.thirdparty.thrift_0_9_0.transport.TMemoryBuffer;
 import org.apache.blur.thrift.BException;
+import org.apache.blur.thrift.SortFieldComparator;
 import org.apache.blur.thrift.UserConverter;
 import org.apache.blur.thrift.generated.Blur.Iface;
 import org.apache.blur.thrift.generated.BlurException;
@@ -141,6 +146,8 @@ import com.yammer.metrics.core.MetricName;
 
 public class BlurUtil {
 
+  public final static SortFieldComparator SORT_FIELD_COMPARATOR = new SortFieldComparator();
+  
   private static final Log REQUEST_LOG = LogFactory.getLog("REQUEST_LOG");
   private static final Log RESPONSE_LOG = LogFactory.getLog("RESPONSE_LOG");
 

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/803765a0/blur-core/src/test/java/org/apache/blur/manager/results/MultipleBlurResultIterableTest.java
----------------------------------------------------------------------
diff --git a/blur-core/src/test/java/org/apache/blur/manager/results/MultipleBlurResultIterableTest.java
b/blur-core/src/test/java/org/apache/blur/manager/results/MultipleBlurResultIterableTest.java
index b1aacf4..d0b284b 100644
--- a/blur-core/src/test/java/org/apache/blur/manager/results/MultipleBlurResultIterableTest.java
+++ b/blur-core/src/test/java/org/apache/blur/manager/results/MultipleBlurResultIterableTest.java
@@ -119,7 +119,7 @@ public class MultipleBlurResultIterableTest {
         for (int i = 0; i < sortFields1.size(); i++) {
           SortField sortField1 = sortFields1.get(i);
           SortField sortField2 = sortFields2.get(i);
-          int compare = sortField1.compareTo(sortField2);
+          int compare = BlurUtil.SORT_FIELD_COMPARATOR.compare(sortField1, sortField2);
           if (compare != 0) {
             return compare;
           }

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/803765a0/blur-thrift/src/main/java/org/apache/blur/thrift/SortFieldComparator.java
----------------------------------------------------------------------
diff --git a/blur-thrift/src/main/java/org/apache/blur/thrift/SortFieldComparator.java b/blur-thrift/src/main/java/org/apache/blur/thrift/SortFieldComparator.java
new file mode 100644
index 0000000..1b7db68
--- /dev/null
+++ b/blur-thrift/src/main/java/org/apache/blur/thrift/SortFieldComparator.java
@@ -0,0 +1,59 @@
+/**
+ * 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.blur.thrift;
+
+import java.util.Comparator;
+
+import org.apache.blur.thrift.generated.SortField;
+import org.apache.blur.thrift.generated.SortField._Fields;
+import org.apache.hadoop.io.WritableComparator;
+
+public class SortFieldComparator implements Comparator<SortField> {
+
+  @Override
+  public int compare(SortField o1, SortField o2) {
+    _Fields field = o1.getSetField();
+    int lastComparison = org.apache.blur.thirdparty.thrift_0_9_0.TBaseHelper.compareTo(field,
o2.getSetField());
+    if (lastComparison == 0) {
+      Object obj1 = o1.getFieldValue();
+      Object obj2 = o2.getFieldValue();
+      switch (field) {
+      case NULL_VALUE:
+        // if both are null type they are equal.
+        return 0;
+      case STRING_VALUE:
+        return ((String) obj1).compareTo((String) obj2);
+      case LONG_VALUE: // INT_VALUE
+        return ((Long) obj1).compareTo((Long) obj2);
+      case DOUBLE_VALUE: // LONG_VALUE
+        return ((Double) obj1).compareTo((Double) obj2);
+      case INT_VALUE: // DOUBLE_VALUE
+        return ((Integer) obj1).compareTo((Integer) obj2);
+      case BINARY_VALUE: // BINARY_VALUE
+        return compare((byte[]) obj1, (byte[]) obj2);
+      default:
+        throw new RuntimeException("Unsupported type of [" + field + "]");
+      }
+    }
+    return lastComparison;
+  }
+
+  public int compare(byte[] b1, byte[] b2) {
+    return WritableComparator.compareBytes(b1, 0, b1.length, b2, 0, b2.length);
+  }
+
+}


Mime
View raw message