You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@accumulo.apache.org by kt...@apache.org on 2016/01/19 20:27:39 UTC

accumulo git commit: ACCUMULO-4111: add float lexicoder

Repository: accumulo
Updated Branches:
  refs/heads/master f7b9fd40a -> 702ea54f6


ACCUMULO-4111: add float lexicoder

Signed-off-by: Keith Turner <kt...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/accumulo/repo
Commit: http://git-wip-us.apache.org/repos/asf/accumulo/commit/702ea54f
Tree: http://git-wip-us.apache.org/repos/asf/accumulo/tree/702ea54f
Diff: http://git-wip-us.apache.org/repos/asf/accumulo/diff/702ea54f

Branch: refs/heads/master
Commit: 702ea54f69c4568bc707d4134fa46d13bd429fd0
Parents: f7b9fd4
Author: Wil Selwood <wi...@sa.catapult.org.uk>
Authored: Thu Jan 14 13:37:43 2016 +0000
Committer: Keith Turner <kt...@apache.org>
Committed: Tue Jan 19 12:18:04 2016 -0500

----------------------------------------------------------------------
 .../core/client/lexicoder/FloatLexicoder.java   | 61 ++++++++++++++++++++
 .../client/lexicoder/FloatLexicoderTest.java    | 45 +++++++++++++++
 2 files changed, 106 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/accumulo/blob/702ea54f/core/src/main/java/org/apache/accumulo/core/client/lexicoder/FloatLexicoder.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/accumulo/core/client/lexicoder/FloatLexicoder.java b/core/src/main/java/org/apache/accumulo/core/client/lexicoder/FloatLexicoder.java
new file mode 100644
index 0000000..50c6205
--- /dev/null
+++ b/core/src/main/java/org/apache/accumulo/core/client/lexicoder/FloatLexicoder.java
@@ -0,0 +1,61 @@
+/*
+ * 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.accumulo.core.client.lexicoder;
+
+import org.apache.accumulo.core.client.lexicoder.impl.AbstractLexicoder;
+import org.apache.accumulo.core.iterators.ValueFormatException;
+
+/**
+ * A lexicoder for preserving the native Java sort order of Float values.
+ *
+ * @since 1.8.0
+ */
+public class FloatLexicoder extends AbstractLexicoder<Float> {
+
+  private UIntegerLexicoder intEncoder = new UIntegerLexicoder();
+
+  @Override
+  public byte[] encode(Float f) {
+    int i = Float.floatToRawIntBits(f);
+    if (i < 0) {
+      i = ~i;
+    } else {
+      i = i ^ 0x80000000;
+    }
+
+    return intEncoder.encode(i);
+  }
+
+  @Override
+  public Float decode(byte[] b) {
+    // This concrete implementation is provided for binary compatibility with 1.6; it can be removed in 2.0. See ACCUMULO-3789.
+    return super.decode(b);
+  }
+
+  @Override
+  protected Float decodeUnchecked(byte[] b, int offset, int len) throws ValueFormatException {
+    int i = intEncoder.decodeUnchecked(b, offset, len);
+    if (i < 0) {
+      i = i ^ 0x80000000;
+    } else {
+      i = ~i;
+    }
+
+    return Float.intBitsToFloat(i);
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/accumulo/blob/702ea54f/core/src/test/java/org/apache/accumulo/core/client/lexicoder/FloatLexicoderTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/accumulo/core/client/lexicoder/FloatLexicoderTest.java b/core/src/test/java/org/apache/accumulo/core/client/lexicoder/FloatLexicoderTest.java
new file mode 100644
index 0000000..7ac683a
--- /dev/null
+++ b/core/src/test/java/org/apache/accumulo/core/client/lexicoder/FloatLexicoderTest.java
@@ -0,0 +1,45 @@
+/*
+ * 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.accumulo.core.client.lexicoder;
+
+import org.apache.accumulo.core.client.lexicoder.impl.AbstractLexicoderTest;
+
+import java.util.Arrays;
+
+/**
+ *
+ */
+public class FloatLexicoderTest extends AbstractLexicoderTest {
+
+  public void testSortOrder() {
+    assertSortOrder(
+        new FloatLexicoder(),
+        Arrays.asList(Float.MIN_VALUE, Float.MAX_VALUE, Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY, 0.0F, 0.01F, 0.001F, 1.0F, -1.0F, -1.1F, -1.01F,
+            Math.nextUp(Float.NEGATIVE_INFINITY), Math.nextAfter(0.0F, Float.NEGATIVE_INFINITY), Math.nextAfter(Float.MAX_VALUE, Float.NEGATIVE_INFINITY)));
+
+  }
+
+  public void testDecode() {
+    assertDecodes(new FloatLexicoder(), Float.MIN_VALUE);
+    assertDecodes(new FloatLexicoder(), Math.nextUp(Float.NEGATIVE_INFINITY));
+    assertDecodes(new FloatLexicoder(), -1.0F);
+    assertDecodes(new FloatLexicoder(), 0.0F);
+    assertDecodes(new FloatLexicoder(), 1.0F);
+    assertDecodes(new FloatLexicoder(), Math.nextAfter(Float.POSITIVE_INFINITY, 0.0F));
+    assertDecodes(new FloatLexicoder(), Float.MAX_VALUE);
+  }
+}