From 1f8cbf58d7e50bf98a422f10f5facfba8e4de703 Mon Sep 17 00:00:00 2001 From: Nick Dimiduk Date: Tue, 23 Jul 2013 15:48:37 -0700 Subject: [PATCH 1/3] HBASE-8693 Extensible data types API This patch introduces an extensible data types API for HBase. It is inspired by the following systems: - PostgreSQL. Postgres has a user-extensible data type API, which has been used to great effect by it's user community (ie, PostGIS). The desire is for HBase to expose an equally extensible data type API. One aspect of the Postgres data type is the ability to provide equivalence functions for index operations. This appears to be of critical performance utility for its execution engine. - Orderly. Orderly handles the issue of compound rowkeys by providing convenience classes for handling these kinds of data types. This influence is reflected in the Struct and Union family of classes. - Phoenix. The PDataType enum used in Phoenix provides type hints, similar Postgres's equivalence functions. These appear to be used during query execution for numerical type promotion. This patch introduces an interface, DataType, along with a number of data type implementations. Implementations based on Bytes are referred to as "Legacy" types, implementations over OrderedBytes are "Ordered" types. Also included are Struct and Union types, demonstrating simple implementations of compound types. Helper classes around the Struct implementation are also provided. This patch does not address the type compatibility concerns expressed by Phoenix's PDataType API (ie, isComparableTo, isCoercibleTo); these will be addressed in HBASE-8863. --- .../org/apache/hadoop/hbase/types/DataType.java | 107 +++++ .../hadoop/hbase/types/FixedLengthWrapper.java | 103 +++++ .../org/apache/hadoop/hbase/types/LegacyBytes.java | 99 +++++ .../hadoop/hbase/types/LegacyBytesFixedLength.java | 69 ++++ .../hadoop/hbase/types/LegacyBytesTerminated.java | 88 +++++ .../apache/hadoop/hbase/types/LegacyDouble.java | 92 +++++ .../org/apache/hadoop/hbase/types/LegacyFloat.java | 92 +++++ .../apache/hadoop/hbase/types/LegacyInteger.java | 92 +++++ .../org/apache/hadoop/hbase/types/LegacyLong.java | 92 +++++ .../apache/hadoop/hbase/types/LegacyString.java | 101 +++++ .../hbase/types/LegacyStringFixedLength.java | 71 ++++ .../hadoop/hbase/types/LegacyStringTerminated.java | 90 +++++ .../org/apache/hadoop/hbase/types/OrderedBlob.java | 68 ++++ .../apache/hadoop/hbase/types/OrderedBlobVar.java | 64 +++ .../hadoop/hbase/types/OrderedBytesBase.java | 57 +++ .../apache/hadoop/hbase/types/OrderedFloat32.java | 73 ++++ .../apache/hadoop/hbase/types/OrderedFloat64.java | 73 ++++ .../apache/hadoop/hbase/types/OrderedInt32.java | 73 ++++ .../apache/hadoop/hbase/types/OrderedInt64.java | 73 ++++ .../apache/hadoop/hbase/types/OrderedNumeric.java | 103 +++++ .../apache/hadoop/hbase/types/OrderedString.java | 56 +++ .../java/org/apache/hadoop/hbase/types/Struct.java | 167 ++++++++ .../apache/hadoop/hbase/types/StructBuilder.java | 54 +++ .../apache/hadoop/hbase/types/StructIterator.java | 86 ++++ .../hadoop/hbase/types/TerminatedWrapper.java | 152 ++++++++ .../java/org/apache/hadoop/hbase/types/Union2.java | 87 +++++ .../java/org/apache/hadoop/hbase/types/Union3.java | 72 ++++ .../java/org/apache/hadoop/hbase/types/Union4.java | 71 ++++ .../java/org/apache/hadoop/hbase/util/Numeric.java | 6 +- .../org/apache/hadoop/hbase/util/OrderedBytes.java | 16 +- .../hadoop/hbase/types/TestFixedLengthWrapper.java | 88 +++++ .../apache/hadoop/hbase/types/TestOrderedBlob.java | 48 +++ .../hadoop/hbase/types/TestOrderedBlobVar.java | 49 +++ .../hadoop/hbase/types/TestOrderedString.java | 45 +++ .../org/apache/hadoop/hbase/types/TestStruct.java | 434 +++++++++++++++++++++ .../hadoop/hbase/types/TestTerminatedWrapper.java | 102 +++++ .../org/apache/hadoop/hbase/types/TestUnion2.java | 144 +++++++ .../apache/hadoop/hbase/util/TestOrderedBytes.java | 24 +- 38 files changed, 3362 insertions(+), 19 deletions(-) create mode 100644 hbase-common/src/main/java/org/apache/hadoop/hbase/types/DataType.java create mode 100644 hbase-common/src/main/java/org/apache/hadoop/hbase/types/FixedLengthWrapper.java create mode 100644 hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyBytes.java create mode 100644 hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyBytesFixedLength.java create mode 100644 hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyBytesTerminated.java create mode 100644 hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyDouble.java create mode 100644 hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyFloat.java create mode 100644 hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyInteger.java create mode 100644 hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyLong.java create mode 100644 hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyString.java create mode 100644 hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyStringFixedLength.java create mode 100644 hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyStringTerminated.java create mode 100644 hbase-common/src/main/java/org/apache/hadoop/hbase/types/OrderedBlob.java create mode 100644 hbase-common/src/main/java/org/apache/hadoop/hbase/types/OrderedBlobVar.java create mode 100644 hbase-common/src/main/java/org/apache/hadoop/hbase/types/OrderedBytesBase.java create mode 100644 hbase-common/src/main/java/org/apache/hadoop/hbase/types/OrderedFloat32.java create mode 100644 hbase-common/src/main/java/org/apache/hadoop/hbase/types/OrderedFloat64.java create mode 100644 hbase-common/src/main/java/org/apache/hadoop/hbase/types/OrderedInt32.java create mode 100644 hbase-common/src/main/java/org/apache/hadoop/hbase/types/OrderedInt64.java create mode 100644 hbase-common/src/main/java/org/apache/hadoop/hbase/types/OrderedNumeric.java create mode 100644 hbase-common/src/main/java/org/apache/hadoop/hbase/types/OrderedString.java create mode 100644 hbase-common/src/main/java/org/apache/hadoop/hbase/types/Struct.java create mode 100644 hbase-common/src/main/java/org/apache/hadoop/hbase/types/StructBuilder.java create mode 100644 hbase-common/src/main/java/org/apache/hadoop/hbase/types/StructIterator.java create mode 100644 hbase-common/src/main/java/org/apache/hadoop/hbase/types/TerminatedWrapper.java create mode 100644 hbase-common/src/main/java/org/apache/hadoop/hbase/types/Union2.java create mode 100644 hbase-common/src/main/java/org/apache/hadoop/hbase/types/Union3.java create mode 100644 hbase-common/src/main/java/org/apache/hadoop/hbase/types/Union4.java create mode 100644 hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestFixedLengthWrapper.java create mode 100644 hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestOrderedBlob.java create mode 100644 hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestOrderedBlobVar.java create mode 100644 hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestOrderedString.java create mode 100644 hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestStruct.java create mode 100644 hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestTerminatedWrapper.java create mode 100644 hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestUnion2.java diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/types/DataType.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/DataType.java new file mode 100644 index 0000000..f027c20 --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/DataType.java @@ -0,0 +1,107 @@ +/** + * 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.hadoop.hbase.types; + +import java.nio.ByteBuffer; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.hbase.util.Order; + +/** + * HDataType is the base class for all HBase data types. Data + * type implementations are designed to be serialized to and deserialized from + * {@link ByteBuffer}s. Serialized representations can retain the natural sort + * ordering of the source object, when a suitable encoding is supported by the + * underlying implementation. This is a desirable feature for use in rowkeys + * and column qualifiers. + *

+ * Data type instances are designed to be stateless, thread-safe, and reused. + * Implementations should provide static final instances + * corresponding to each variation on configurable parameters. For instance, + * order-preserving types should provide static ASCENDING and DESCENDING + * instances. It is also encouraged for implementations operating on Java + * primitive types to provide primitive implementations of the + * read and write methods. This advice is a + * performance consideration to clients reading and writing values in tight + * loops. + *

+ */ +@InterfaceAudience.Public +@InterfaceStability.Evolving +public interface DataType { + + /** + * Indicates whether this instance writes encoded byte[]'s + * which preserve the natural sort order of the unencoded value. + * @return true when natural order is preserved, + * false otherwise. + */ + public boolean isOrderPreserving(); + + /** + * Retrieve the sort {@link Order} honored by this data type, or + * null when natural ordering is not preserved. + */ + public Order getOrder(); + + /** + * Indicates whether this instance supports encoding null + * values. This depends on the implementation details of the encoding + * format. All HDataTypes that support null should + * treat null as comparing less than any non-NULL + * value for default sort ordering purposes. + * @return true when null is supported, + * false otherwise. + */ + public boolean isNullable(); + + /** + * Indicates whether this instance is able to skip over it's encoded value + * in a ByteBuffer. HDataTypes that are not + * skippable can only be used as the right-most field of a {@link Struct}. + * @return + */ + public boolean isSkippable(); + + /** + * Inform consumers how long the encoded byte[] will be. + */ + public int encodedLength(T val); + + /** + * Inform consumers over what type this HDataType operates. + * @return + */ + public Class encodedClass(); + + /** + * Skip the position of buff over the encoded value. + */ + public void skip(ByteBuffer buff); + + /** + * Read an instance of T from the buffer buff. + */ + public T decode(ByteBuffer buff); + + /** + * Write instance val into buffer buff. + */ + public void encode(ByteBuffer buff, T val); +} diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/types/FixedLengthWrapper.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/FixedLengthWrapper.java new file mode 100644 index 0000000..510c09d --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/FixedLengthWrapper.java @@ -0,0 +1,103 @@ +/** + * 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.hadoop.hbase.types; + +import java.nio.ByteBuffer; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.hbase.util.Order; + +/** + * Wraps an existing HDataType implementation as a fixed-length + * version of itself. This has the useful side-effect of turning an existing + * HDataType which is not skippable into a + * skippable variant. + */ +@InterfaceAudience.Public +@InterfaceStability.Evolving +public class FixedLengthWrapper implements DataType { + + protected final DataType wrapped; + protected final int length; + + /** + * Create a fixed-length version of the wrapped. + */ + public FixedLengthWrapper(DataType wrapped, int length) { + this.wrapped = wrapped; + this.length = length; + } + + @Override + public boolean isOrderPreserving() { return wrapped.isOrderPreserving(); } + + @Override + public Order getOrder() { return wrapped.getOrder(); } + + @Override + public boolean isNullable() { return wrapped.isNullable(); } + + @Override + public boolean isSkippable() { return true; } + + @Override + public int encodedLength(T val) { return length; } + + @Override + public Class encodedClass() { return wrapped.encodedClass(); } + + @Override + public void skip(ByteBuffer buff) { buff.position(buff.position() + length); } + + /** + * Read an instance of T from the buffer buff. + * @throws IllegalArgumentException when buff lacks sufficient + * remaining capacity. In the event this exception is thrown, + * buff#position() is restored to the original value. + */ + @Override + public T decode(ByteBuffer buff) { + if (buff.remaining() < length) + throw new IllegalArgumentException("Not enough buffer remaining."); + ByteBuffer b = buff.duplicate(); + b.limit(b.position() + length); + T ret = wrapped.decode(b); + buff.position(buff.position() + length); + return ret; + } + + /** + * Write instance val into buffer buff. Any space + * remaining after val is written is zero-padded up to + * length. + * @throws IllegalArgumentException when buff lacks sufficient + * remaining capacity. In the event this exception is thrown, + * buff#position() is restored the original value. + */ + @Override + public void encode(ByteBuffer buff, T val) { + if (buff.remaining() < length) + throw new IllegalArgumentException("Not enough buffer remaining."); + ByteBuffer b = buff.duplicate(); + b.limit(b.position() + length); + wrapped.encode(b, val); + while (b.position() != b.limit()) b.put((byte) 0x00); + buff.position(buff.position() + length); + } +} diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyBytes.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyBytes.java new file mode 100644 index 0000000..ce1ccd2 --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyBytes.java @@ -0,0 +1,99 @@ +/** + * 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.hadoop.hbase.types; + +import java.nio.ByteBuffer; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.hadoop.hbase.util.Order; + +/** + * An HDataType for interacting with variable-length values + * encoded using {@link Bytes#putBytes(byte[], int, byte[], int, int)}. + * Intended to make it easier to transition away from direct use of + * {@link Bytes}. + * @see Bytes#putBytes(byte[], int, byte[], int, int) + * @see LegacyBytesTerminated + * @see OrderedBlob + */ +@InterfaceAudience.Public +@InterfaceStability.Evolving +public class LegacyBytes implements DataType { + + protected final Order order; + + public LegacyBytes() { order = Order.ASCENDING; } + public LegacyBytes(Order order) { this.order = order; } + + @Override + public boolean isOrderPreserving() { return true; } + + @Override + public Order getOrder() { return order; } + + @Override + public boolean isNullable() { return false; } + + @Override + public boolean isSkippable() { return false; } + + @Override + public void skip(ByteBuffer buff) { buff.position(buff.limit()); } + + @Override + public int encodedLength(byte[] val) { return val.length; } + + @Override + public Class encodedClass() { return byte[].class; } + + /** + * Skip buff ahead length bytes. + */ + public void skip(ByteBuffer buff, int length) { + buff.position(buff.position() + length); + } + + @Override + public byte[] decode(ByteBuffer buff) { + return decode(buff, buff.limit() - buff.position()); + } + + @Override + public void encode(ByteBuffer buff, byte[] val) { + buff.put(val); + } + + /** + * Read a byte[] from the buffer buff. + */ + public byte[] decode(ByteBuffer buff, int length) { + byte[] val = new byte[length]; + buff.get(val); + return val; + } + + /** + * Write val into buff, respecting + * offset and length. + */ + public void encode(ByteBuffer buff, byte[] val, int offset, int length) { + buff.put(val, offset, length); + } +} diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyBytesFixedLength.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyBytesFixedLength.java new file mode 100644 index 0000000..4918f0d --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyBytesFixedLength.java @@ -0,0 +1,69 @@ +/** + * 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.hadoop.hbase.types; + +import java.nio.ByteBuffer; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.hadoop.hbase.util.Order; + +/** + * An HDataType that encodes fixed-length values encoded using + * {@link Bytes#putBytes(byte[], int, byte[], int, int)}. Intended to make it + * easier to transition away from direct use of {@link Bytes}. + * @see Bytes#putBytes(byte[], int, byte[], int, int) + * @see LegacyBytes + * @see OrderedBlob + */ +@InterfaceAudience.Public +@InterfaceStability.Evolving +public class LegacyBytesFixedLength extends FixedLengthWrapper { + + /** + * Create a LegacyBytesFixedLength using the specified + * order and length. + */ + public LegacyBytesFixedLength(Order order, int length) { + super(new LegacyBytes(order), length); + } + + /** + * Create a LegacyBytesFixedLength of the specified + * length>. + */ + public LegacyBytesFixedLength(int length) { + super(new LegacyBytes(), length); + } + + /** + * Read a byte[] from the buffer buff. + */ + public byte[] decode(ByteBuffer buff, int length) { + return ((LegacyBytes) wrapped).decode(buff, length); + } + + /** + * Write val into buff, respecting + * offset and length. + */ + public void encode(ByteBuffer buff, byte[] val, int offset, int length) { + ((LegacyBytes) wrapped).encode(buff, val, offset, length); + } +} diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyBytesTerminated.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyBytesTerminated.java new file mode 100644 index 0000000..849416c --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyBytesTerminated.java @@ -0,0 +1,88 @@ +/** + * 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.hadoop.hbase.types; + +import java.nio.ByteBuffer; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.hadoop.hbase.util.Order; + +/** + * An HDataType that encodes variable-length values encoded using + * {@link Bytes#putBytes(byte[], int, byte[], int, int)}. Includes a + * termination marker following the raw byte[] value. Intended to + * make it easier to transition away from direct use of {@link Bytes}. + * @see Bytes#putBytes(byte[], int, byte[], int, int) + * @see LegacyBytes + * @see OrderedBlob + */ +@InterfaceAudience.Public +@InterfaceStability.Evolving +public class LegacyBytesTerminated extends TerminatedWrapper { + + /** + * Create a LegacyBytesTerminated using the specified terminator and + * order. + * @throws IllegalArgumentException if term is null or empty. + */ + public LegacyBytesTerminated(Order order, byte[] term) { + super(new LegacyBytes(order), term); + } + + /** + * Create a LegacyBytesTerminated using the specified terminator and + * order. + * @throws IllegalArgumentException if term is null or empty. + */ + public LegacyBytesTerminated(Order order, String term) { + super(new LegacyBytes(order), term); + } + + /** + * Create a LegacyBytesTerminated using the specified terminator. + * @throws IllegalArgumentException if term is null or empty. + */ + public LegacyBytesTerminated(byte[] term) { + super(new LegacyBytes(), term); + } + + /** + * Create a LegacyBytesTerminated using the specified terminator. + * @throws IllegalArgumentException if term is null or empty. + */ + public LegacyBytesTerminated(String term) { + super(new LegacyBytes(), term); + } + + /** + * Read a byte[] from the buffer buff. + */ + public byte[] decode(ByteBuffer buff, int length) { + return ((LegacyBytes) wrapped).decode(buff, length); + } + + /** + * Write val into buff, respecting + * offset and length. + */ + public void encode(ByteBuffer buff, byte[] val, int offset, int length) { + ((LegacyBytes) wrapped).encode(buff, val, offset, length); + } +} diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyDouble.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyDouble.java new file mode 100644 index 0000000..7246fa4 --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyDouble.java @@ -0,0 +1,92 @@ +/** + * 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.hadoop.hbase.types; + +import java.nio.ByteBuffer; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.hadoop.hbase.util.Order; + +/** + * An HDataType for interacting with values encoded using + * {@link Bytes#putDouble(byte[], int, double)}. Intended to make it easier to + * transition away from direct use of {@link Bytes}. + * @see Bytes#putDouble(byte[], int, double) + * @see Bytes#toDouble(byte[]) + * @see OrderedFloat64 + * @see OrderedNumeric + */ +@InterfaceAudience.Public +@InterfaceStability.Evolving +public class LegacyDouble implements DataType { + + @Override + public boolean isOrderPreserving() { return false; } + + @Override + public Order getOrder() { return null; } + + @Override + public boolean isNullable() { return false; } + + @Override + public boolean isSkippable() { return true; } + + @Override + public int encodedLength(Double val) { return Bytes.SIZEOF_DOUBLE; } + + @Override + public Class encodedClass() { return Double.class; } + + @Override + public void skip(ByteBuffer buff) { + buff.position(buff.position() + Bytes.SIZEOF_DOUBLE); + } + + @Override + public Double decode(ByteBuffer buff) { + double val = Bytes.toDouble(buff.array(), buff.position()); + skip(buff); + return val; + } + + @Override + public void encode(ByteBuffer buff, Double val) { + Bytes.putDouble(buff.array(), buff.position(), val); + skip(buff); + } + + /** + * Read a double value from the buffer buff. + */ + public double decodeDouble(ByteBuffer buff) { + double val = Bytes.toDouble(buff.array(), buff.position()); + skip(buff); + return val; + } + + /** + * Write instance val into buffer buff. + */ + public void encodeDouble(ByteBuffer buff, double val) { + Bytes.putDouble(buff.array(), buff.position(), val); + skip(buff); + } +} diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyFloat.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyFloat.java new file mode 100644 index 0000000..c9ef97c --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyFloat.java @@ -0,0 +1,92 @@ +/** + * 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.hadoop.hbase.types; + +import java.nio.ByteBuffer; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.hadoop.hbase.util.Order; + +/** + * An HDataType for interacting with values encoded using + * {@link Bytes#putFloat(byte[], int, float)}. Intended to make it easier to + * transition away from direct use of {@link Bytes}. + * @see Bytes#putFloat(byte[], int, float) + * @see Bytes#toFloat(byte[]) + * @see OrderedFloat32 + * @see OrderedNumeric + */ +@InterfaceAudience.Public +@InterfaceStability.Evolving +public class LegacyFloat implements DataType { + + @Override + public boolean isOrderPreserving() { return false; } + + @Override + public Order getOrder() { return null; } + + @Override + public boolean isNullable() { return false; } + + @Override + public boolean isSkippable() { return true; } + + @Override + public int encodedLength(Float val) { return Bytes.SIZEOF_FLOAT; } + + @Override + public Class encodedClass() { return Float.class; } + + @Override + public void skip(ByteBuffer buff) { + buff.position(buff.position() + Bytes.SIZEOF_FLOAT); + } + + @Override + public Float decode(ByteBuffer buff) { + float val = Bytes.toFloat(buff.array(), buff.position()); + skip(buff); + return val; + } + + @Override + public void encode(ByteBuffer buff, Float val) { + Bytes.putFloat(buff.array(), buff.position(), val); + skip(buff); + } + + /** + * Read a float value from the buffer buff. + */ + public float decodeFloat(ByteBuffer buff) { + float val = Bytes.toFloat(buff.array(), buff.position()); + skip(buff); + return val; + } + + /** + * Write instance val into buffer buff. + */ + public void encodeFloat(ByteBuffer buff, float val) { + Bytes.putFloat(buff.array(), buff.position(), val); + skip(buff); + } +} diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyInteger.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyInteger.java new file mode 100644 index 0000000..5cf0c99 --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyInteger.java @@ -0,0 +1,92 @@ +/** + * 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.hadoop.hbase.types; + +import java.nio.ByteBuffer; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.hadoop.hbase.util.Order; + +/** + * An HDataType for interacting with values encoded using + * {@link Bytes#putInt(byte[], int, int)}. Intended to make it easier to + * transition away from direct use of {@link Bytes}. + * @see Bytes#putInt(byte[], int, int) + * @see Bytes#toInt(byte[]) + * @see OrderedInt32 + * @see OrderedNumeric + */ +@InterfaceAudience.Public +@InterfaceStability.Evolving +public class LegacyInteger implements DataType { + + @Override + public boolean isOrderPreserving() { return false; } + + @Override + public Order getOrder() { return null; } + + @Override + public boolean isNullable() { return false; } + + @Override + public boolean isSkippable() { return true; } + + @Override + public int encodedLength(Integer val) { return Bytes.SIZEOF_INT; } + + @Override + public Class encodedClass() { return Integer.class; } + + @Override + public void skip(ByteBuffer buff) { + buff.position(buff.position() + Bytes.SIZEOF_INT); + } + + @Override + public Integer decode(ByteBuffer buff) { + int val = Bytes.toInt(buff.array(), buff.position()); + skip(buff); + return val; + } + + @Override + public void encode(ByteBuffer buff, Integer val) { + Bytes.putInt(buff.array(), buff.position(), val); + skip(buff); + } + + /** + * Read an int value from the buffer buff. + */ + public int decodeInt(ByteBuffer buff) { + int val = Bytes.toInt(buff.array(), buff.position()); + skip(buff); + return val; + } + + /** + * Write instance val into buffer buff. + */ + public void encodeInt(ByteBuffer buff, int val) { + Bytes.putInt(buff.array(), buff.position(), val); + skip(buff); + } +} diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyLong.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyLong.java new file mode 100644 index 0000000..51c18e5 --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyLong.java @@ -0,0 +1,92 @@ +/** + * 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.hadoop.hbase.types; + +import java.nio.ByteBuffer; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.hadoop.hbase.util.Order; + +/** + * An HDataType for interacting with values encoded using + * {@link Bytes#putLong(byte[], int, long)}. Intended to make it easier to + * transition away from direct use of {@link Bytes}. + * @see Bytes#putLong(byte[], int, long) + * @see Bytes#toLong(byte[]) + * @see OrderedInt64 + * @see OrderedNumeric + */ +@InterfaceAudience.Public +@InterfaceStability.Evolving +public class LegacyLong implements DataType { + + @Override + public boolean isOrderPreserving() { return false; } + + @Override + public Order getOrder() { return null; } + + @Override + public boolean isNullable() { return false; } + + @Override + public boolean isSkippable() { return true; } + + @Override + public int encodedLength(Long val) { return Bytes.SIZEOF_LONG; } + + @Override + public Class encodedClass() { return Long.class; } + + @Override + public void skip(ByteBuffer buff) { + buff.position(buff.position() + Bytes.SIZEOF_LONG); + } + + @Override + public Long decode(ByteBuffer buff) { + long val = Bytes.toLong(buff.array(), buff.position()); + skip(buff); + return val; + } + + @Override + public void encode(ByteBuffer buff, Long val) { + Bytes.putLong(buff.array(), buff.position(), val); + skip(buff); + } + + /** + * Read a long value from the buffer buff. + */ + public long decodeLong(ByteBuffer buff) { + long val = Bytes.toLong(buff.array(), buff.position()); + skip(buff); + return val; + } + + /** + * Write instance val into buffer buff. + */ + public void encodeLong(ByteBuffer buff, long val) { + Bytes.putLong(buff.array(), buff.position(), val); + skip(buff); + } +} diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyString.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyString.java new file mode 100644 index 0000000..0a6de15 --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyString.java @@ -0,0 +1,101 @@ +/** + * 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.hadoop.hbase.types; + +import java.nio.ByteBuffer; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.hadoop.hbase.util.Order; + +/** + * An HDataType for interacting with values encoded using + * {@link Bytes#toBytes(String)}. Intended to make it easier to transition + * away from direct use of {@link Bytes}. + * @see Bytes#toBytes(String) + * @see Bytes#toString(byte[]) + * @see LegacyStringTerminated + * @see OrderedString + */ +@InterfaceAudience.Public +@InterfaceStability.Evolving +public class LegacyString implements DataType { + + protected final Order order; + + public LegacyString() { order = Order.ASCENDING; } + public LegacyString(Order order) { this.order = order; } + + @Override + public boolean isOrderPreserving() { return true; } + + @Override + public Order getOrder() { return order; } + + @Override + public boolean isNullable() { return false; } + + @Override + public boolean isSkippable() { return false; } + + @Override + public void skip(ByteBuffer buff) { buff.position(buff.limit()); } + + @Override + public int encodedLength(String val) { return Bytes.toBytes(val).length; } + + @Override + public Class encodedClass() { return String.class; } + + /** + * Skip buff ahead length bytes. + */ + public void skip(ByteBuffer buff, int length) { + buff.position(buff.position() + length); + } + + @Override + public String decode(ByteBuffer buff) { + return decode(buff, buff.limit() - buff.position()); + } + + @Override + public void encode(ByteBuffer buff, String val) { + buff.put(Bytes.toBytes(val)); + } + + /** + * Read a String from the buffer buff. + */ + public String decode(ByteBuffer buff, int length) { + String val = Bytes.toString(buff.array(), buff.position(), length); + skip(buff, length); + return val; + } + + /** + * Write a String into buff at position + * offset. + * @return incremented offset. + */ + public int encode(byte[] buff, int offset, String val) { + byte[] s = Bytes.toBytes(val); + return Bytes.putBytes(buff, offset, s, 0, s.length); + } +} diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyStringFixedLength.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyStringFixedLength.java new file mode 100644 index 0000000..2096bcf --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyStringFixedLength.java @@ -0,0 +1,71 @@ +/** + * 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.hadoop.hbase.types; + +import java.nio.ByteBuffer; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.hadoop.hbase.util.Order; + +/** + * An HDataType that encodes fixed-length values encoded using + * {@link Bytes#toBytes(String)}. Intended to make it easier to transition + * away from direct use of {@link Bytes}. + * @see Bytes#toBytes(String) + * @see Bytes#toString(byte[], int, int) + * @see LegacyString + * @see OrderedString + */ +@InterfaceAudience.Public +@InterfaceStability.Evolving +public class LegacyStringFixedLength extends FixedLengthWrapper { + + /** + * Create a LegacyStringFixedLength using the specified + * order and length. + */ + public LegacyStringFixedLength(Order order, int length) { + super(new LegacyString(order), length); + } + + /** + * Create a LegacyStringFixedLength of the specified + * length>. + */ + public LegacyStringFixedLength(int length) { + super(new LegacyString(), length); + } + + /** + * Read a String from the buffer buff. + */ + public String decode(ByteBuffer buff, int length) { + return ((LegacyString) wrapped).decode(buff, length); + } + + /** + * Write a String into buff at position + * offset. + * @return incremented offset. + */ + public int encode(byte[] buff, int offset, String val) { + return ((LegacyString) wrapped).encode(buff, offset, val); + } +} diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyStringTerminated.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyStringTerminated.java new file mode 100644 index 0000000..9baa67e --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/LegacyStringTerminated.java @@ -0,0 +1,90 @@ +/** + * 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.hadoop.hbase.types; + +import java.nio.ByteBuffer; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.hadoop.hbase.util.Order; + +/** + * An HDataType that encodes variable-length values encoded using + * {@link Bytes#toBytes(String)}. Includes a termination marker following the + * raw byte[] value. Intended to make it easier to transition + * away from direct use of {@link Bytes}. + * @see Bytes#toBytes(String) + * @see Bytes#toString(byte[], int, int) + * @see LegacyString + * @see OrderedString + */ +@InterfaceAudience.Public +@InterfaceStability.Evolving +public class LegacyStringTerminated extends TerminatedWrapper { + + /** + * Create a LegacyStringTerminated using the specified terminator and + * order. + * @throws IllegalArgumentException if term is null or empty. + */ + public LegacyStringTerminated(Order order, byte[] term) { + super(new LegacyString(order), term); + } + + /** + * Create a LegacyStringTerminated using the specified terminator and + * order. + * @throws IllegalArgumentException if term is null or empty. + */ + public LegacyStringTerminated(Order order, String term) { + super(new LegacyString(order), term); + } + + /** + * Create a LegacyStringTerminated using the specified terminator. + * @throws IllegalArgumentException if term is null or empty. + */ + public LegacyStringTerminated(byte[] term) { + super(new LegacyString(), term); + } + + /** + * Create a LegacyStringTerminated using the specified terminator. + * @throws IllegalArgumentException if term is null or empty. + */ + public LegacyStringTerminated(String term) { + super(new LegacyString(), term); + } + + /** + * Read a String from the buffer buff. + */ + public String decode(ByteBuffer buff, int length) { + return ((LegacyString) wrapped).decode(buff, length); + } + + /** + * Write a String into buff at position + * offset. + * @return incremented offset. + */ + public int encode(byte[] buff, int offset, String val) { + return ((LegacyString) wrapped).encode(buff, offset, val); + } +} diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/types/OrderedBlob.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/OrderedBlob.java new file mode 100644 index 0000000..eb1181c --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/OrderedBlob.java @@ -0,0 +1,68 @@ +/** + * 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.hadoop.hbase.types; + +import java.nio.ByteBuffer; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.hbase.util.Order; +import org.apache.hadoop.hbase.util.OrderedBytes; + +/** + * A byte[] of variable-length. + */ +@InterfaceAudience.Public +@InterfaceStability.Evolving +public class OrderedBlob extends OrderedBytesBase { + + public static final OrderedBlob ASCENDING = new OrderedBlob(Order.ASCENDING); + public static final OrderedBlob DESCENDING = new OrderedBlob(Order.DESCENDING); + + protected OrderedBlob(Order order) { super(order); } + + @Override + public boolean isSkippable() { return false; } + + @Override + public int encodedLength(byte[] val) { + return null == val ? + (Order.ASCENDING == order ? 1 : 2) : + (Order.ASCENDING == order ? val.length + 1 : val.length + 2); + } + + @Override + public Class encodedClass() { return byte[].class; } + + @Override + public byte[] decode(ByteBuffer buff) { + return OrderedBytes.decodeBlobCopy(buff); + } + + @Override + public void encode(ByteBuffer buff, byte[] val) { + OrderedBytes.encodeBlobCopy(buff, val, order); + } + + /** + * Write a subset of val to buff. + */ + public void encode(ByteBuffer buff, byte[] val, int length, int offset) { + OrderedBytes.encodeBlobCopy(buff, val, offset, length, order); + } +} diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/types/OrderedBlobVar.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/OrderedBlobVar.java new file mode 100644 index 0000000..abcebf2 --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/OrderedBlobVar.java @@ -0,0 +1,64 @@ +/** + * 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.hadoop.hbase.types; + +import java.nio.ByteBuffer; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.hbase.util.Order; +import org.apache.hadoop.hbase.util.OrderedBytes; + +/** + * An alternative to {@link OrderedBlob} for use by {@link Struct} fields that + * do not terminate the fields list. + */ +@InterfaceAudience.Private +@InterfaceStability.Evolving +public class OrderedBlobVar extends OrderedBytesBase { + + public static final OrderedBlobVar ASCENDING = new OrderedBlobVar(Order.ASCENDING); + public static final OrderedBlobVar DESCENDING = new OrderedBlobVar(Order.DESCENDING); + + protected OrderedBlobVar(Order order) { super(order); } + + @Override + public int encodedLength(byte[] val) { + return null == val ? 1 : OrderedBytes.blobVarEncodedLength(val.length); + } + + @Override + public Class encodedClass() { return byte[].class; } + + @Override + public byte[] decode(ByteBuffer buff) { + return OrderedBytes.decodeBlobVar(buff); + } + + @Override + public void encode(ByteBuffer buff, byte[] val) { + OrderedBytes.encodeBlobVar(buff, val, order); + } + + /** + * Write a subset of val to buff. + */ + public void encode(ByteBuffer buff, byte[] val, int length, int offset) { + OrderedBytes.encodeBlobVar(buff, val, offset, length, order); + } +} diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/types/OrderedBytesBase.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/OrderedBytesBase.java new file mode 100644 index 0000000..1c97ead --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/OrderedBytesBase.java @@ -0,0 +1,57 @@ +/** + * 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.hadoop.hbase.types; + +import java.nio.ByteBuffer; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.hbase.util.Order; +import org.apache.hadoop.hbase.util.OrderedBytes; + +/** + * Base class for data types backed by the {@link OrderedBytes} encoding + * implementations. + */ +@InterfaceAudience.Public +@InterfaceStability.Evolving +public abstract class OrderedBytesBase implements DataType { + + protected final Order order; + + protected OrderedBytesBase(Order order) { this.order = order; } + + @Override + public boolean isOrderPreserving() { return true; } + + @Override + public Order getOrder() { return order; } + + // almost all OrderedBytes implementations are nullable. + @Override + public boolean isNullable() { return true; } + + // almost all OrderedBytes implementations are skippable. + @Override + public boolean isSkippable() { return true; } + + @Override + public void skip(ByteBuffer buff) { + OrderedBytes.skip(buff); + } +} diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/types/OrderedFloat32.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/OrderedFloat32.java new file mode 100644 index 0000000..c80e7ad --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/OrderedFloat32.java @@ -0,0 +1,73 @@ +/** + * 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.hadoop.hbase.types; + +import java.nio.ByteBuffer; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.hbase.util.Order; +import org.apache.hadoop.hbase.util.OrderedBytes; + + +/** + * A float of 32-bits using a fixed-length encoding. + */ +@InterfaceAudience.Public +@InterfaceStability.Evolving +public class OrderedFloat32 extends OrderedBytesBase { + + public static final OrderedFloat32 ASCENDING = new OrderedFloat32(Order.ASCENDING); + public static final OrderedFloat32 DESCENDING = new OrderedFloat32(Order.DESCENDING); + + protected OrderedFloat32(Order order) { super(order); } + + @Override + public boolean isNullable() { return false; } + + @Override + public int encodedLength(Float val) { return 5; } + + @Override + public Class encodedClass() { return Float.class; } + + @Override + public Float decode(ByteBuffer buff) { + return OrderedBytes.decodeFloat32(buff); + } + + @Override + public void encode(ByteBuffer buff, Float val) { + if (null == val) throw new IllegalArgumentException("Null values not supported."); + OrderedBytes.encodeFloat32(buff, val, order); + } + + /** + * Read a float value from the buffer buff. + */ + public float decodeFloat(ByteBuffer buff) { + return OrderedBytes.decodeFloat32(buff); + } + + /** + * Write instance val into buffer buff. + */ + public void encodeFloat(ByteBuffer buff, float val) { + OrderedBytes.encodeFloat32(buff, val, order); + } +} diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/types/OrderedFloat64.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/OrderedFloat64.java new file mode 100644 index 0000000..610ebf1 --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/OrderedFloat64.java @@ -0,0 +1,73 @@ +/** + * 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.hadoop.hbase.types; + +import java.nio.ByteBuffer; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.hbase.util.Order; +import org.apache.hadoop.hbase.util.OrderedBytes; + + +/** + * A double of 64-bits using a fixed-length encoding. + */ +@InterfaceAudience.Public +@InterfaceStability.Evolving +public class OrderedFloat64 extends OrderedBytesBase { + + public static final OrderedFloat64 ASCENDING = new OrderedFloat64(Order.ASCENDING); + public static final OrderedFloat64 DESCENDING = new OrderedFloat64(Order.DESCENDING); + + protected OrderedFloat64(Order order) { super(order); } + + @Override + public boolean isNullable() { return false; } + + @Override + public int encodedLength(Double val) { return 9; } + + @Override + public Class encodedClass() { return Double.class; } + + @Override + public Double decode(ByteBuffer buff) { + return OrderedBytes.decodeFloat64(buff); + } + + @Override + public void encode(ByteBuffer buff, Double val) { + if (null == val) throw new IllegalArgumentException("Null values not supported."); + OrderedBytes.encodeFloat64(buff, val, order); + } + + /** + * Read a double value from the buffer buff. + */ + public double decodeDouble(ByteBuffer buff) { + return OrderedBytes.decodeFloat64(buff); + } + + /** + * Write instance val into buffer buff. + */ + public void encodeDouble(ByteBuffer buff, double val) { + OrderedBytes.encodeFloat64(buff, val, order); + } +} diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/types/OrderedInt32.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/OrderedInt32.java new file mode 100644 index 0000000..6bdbb1c --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/OrderedInt32.java @@ -0,0 +1,73 @@ +/** + * 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.hadoop.hbase.types; + +import java.nio.ByteBuffer; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.hbase.util.Order; +import org.apache.hadoop.hbase.util.OrderedBytes; + + +/** + * An int of 32-bits using a fixed-length encoding. + */ +@InterfaceAudience.Public +@InterfaceStability.Evolving +public class OrderedInt32 extends OrderedBytesBase { + + public static final OrderedInt32 ASCENDING = new OrderedInt32(Order.ASCENDING); + public static final OrderedInt32 DESCENDING = new OrderedInt32(Order.DESCENDING); + + protected OrderedInt32(Order order) { super(order); } + + @Override + public boolean isNullable() { return false; } + + @Override + public int encodedLength(Integer val) { return 5; } + + @Override + public Class encodedClass() { return Integer.class; } + + @Override + public Integer decode(ByteBuffer buff) { + return OrderedBytes.decodeInt32(buff); + } + + @Override + public void encode(ByteBuffer buff, Integer val) { + if (null == val) throw new IllegalArgumentException("Null values not supported."); + OrderedBytes.encodeInt32(buff, val, order); + } + + /** + * Read an int value from the buffer buff. + */ + public int decodeInt(ByteBuffer buff) { + return OrderedBytes.decodeInt32(buff); + } + + /** + * Write instance val into buffer buff. + */ + public void encodeInt(ByteBuffer buff, int val) { + OrderedBytes.encodeInt32(buff, val, order); + } +} diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/types/OrderedInt64.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/OrderedInt64.java new file mode 100644 index 0000000..f95eb6f --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/OrderedInt64.java @@ -0,0 +1,73 @@ +/** + * 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.hadoop.hbase.types; + +import java.nio.ByteBuffer; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.hbase.util.Order; +import org.apache.hadoop.hbase.util.OrderedBytes; + + +/** + * A long of 64-bits using a fixed-length encoding. + */ +@InterfaceAudience.Public +@InterfaceStability.Evolving +public class OrderedInt64 extends OrderedBytesBase { + + public static final OrderedInt64 ASCENDING = new OrderedInt64(Order.ASCENDING); + public static final OrderedInt64 DESCENDING = new OrderedInt64(Order.DESCENDING); + + protected OrderedInt64(Order order) { super(order); } + + @Override + public boolean isNullable() { return false; } + + @Override + public int encodedLength(Long val) { return 9; } + + @Override + public Class encodedClass() { return Long.class; } + + @Override + public Long decode(ByteBuffer buff) { + return OrderedBytes.decodeInt64(buff); + } + + @Override + public void encode(ByteBuffer buff, Long val) { + if (null == val) throw new IllegalArgumentException("Null values not supported."); + OrderedBytes.encodeInt64(buff, val, order); + } + + /** + * Read a long value from the buffer buff. + */ + public long decodeLong(ByteBuffer buff) { + return OrderedBytes.decodeInt64(buff); + } + + /** + * Write instance val into buffer buff. + */ + public void encodeLong(ByteBuffer buff, long val) { + OrderedBytes.encodeInt64(buff, val, order); + } +} diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/types/OrderedNumeric.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/OrderedNumeric.java new file mode 100644 index 0000000..693bed1 --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/OrderedNumeric.java @@ -0,0 +1,103 @@ +/** + * 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.hadoop.hbase.types; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.nio.ByteBuffer; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.hbase.util.Numeric; +import org.apache.hadoop.hbase.util.Order; +import org.apache.hadoop.hbase.util.OrderedBytes; + +/** + * An {@link Number} of arbitrary precision and variable-length encoding. The + * resulting length of encoded values is determined by the numerical (base + * 100) precision, not absolute value. Use this data type anywhere you would + * expect to use a DECIMAL type, a {@link BigDecimal}, a + * {@link BigInteger}, or any time you've parsed floating precision values + * from text. + */ +@InterfaceAudience.Public +@InterfaceStability.Evolving +public class OrderedNumeric extends OrderedBytesBase { + + public static final OrderedNumeric ASCENDING = new OrderedNumeric(Order.ASCENDING); + public static final OrderedNumeric DESCENDING = new OrderedNumeric(Order.DESCENDING); + + protected OrderedNumeric(Order order) { super(order); } + + @Override + public int encodedLength(Numeric val) { + // TODO: this could be done better. + ByteBuffer buff = ByteBuffer.allocate(100); + encode(buff, val); + return buff.position(); + } + + @Override + public Class encodedClass() { return Numeric.class; } + + @Override + public Numeric decode(ByteBuffer buff) { + return OrderedBytes.decodeNumeric(buff); + } + + @Override + public void encode(ByteBuffer b, Numeric val) { + if (null == val) { + OrderedBytes.encodeNull(b, order); + } else if (val.isInteger()) { + OrderedBytes.encodeNumeric(b, val.longValue(), order); + } else if (val.isReal()) { + OrderedBytes.encodeNumeric(b, val.doubleValue(), order); + } else { + OrderedBytes.encodeNumeric(b, val.exactValue(), order); + } + } + + /** + * Read a long value from the buffer buff. + */ + public long decodeLong(ByteBuffer buff) { + return OrderedBytes.decodeNumeric(buff).longValue(); + } + + /** + * Write instance val into buffer buff. + */ + public void encodeLong(ByteBuffer buff, long val) { + OrderedBytes.encodeNumeric(buff, val, order); + } + + /** + * Read a double value from the buffer buff. + */ + public double decodeDouble(ByteBuffer buff) { + return OrderedBytes.decodeNumeric(buff).doubleValue(); + } + + /** + * Write instance val into buffer buff. + */ + public void encodeDouble(ByteBuffer buff, double val) { + OrderedBytes.encodeNumeric(buff, val, order); + } +} diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/types/OrderedString.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/OrderedString.java new file mode 100644 index 0000000..efd1389 --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/OrderedString.java @@ -0,0 +1,56 @@ +/** + * 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.hadoop.hbase.types; + +import java.nio.ByteBuffer; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.hbase.util.Order; +import org.apache.hadoop.hbase.util.OrderedBytes; + +/** + * A String of variable-length. + */ +@InterfaceAudience.Public +@InterfaceStability.Evolving +public class OrderedString extends OrderedBytesBase { + + public static final OrderedString ASCENDING = new OrderedString(Order.ASCENDING); + public static final OrderedString DESCENDING = new OrderedString(Order.DESCENDING); + + protected OrderedString(Order order) { super(order); } + + @Override + public int encodedLength(String val) { + return null == val ? 1 : val.getBytes(OrderedBytes.UTF8).length + 2; + } + + @Override + public Class encodedClass() { return String.class; } + + @Override + public String decode(ByteBuffer buff) { + return OrderedBytes.decodeString(buff); + } + + @Override + public void encode(ByteBuffer buff, String val) { + OrderedBytes.encodeString(buff, val, order); + } +} diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/types/Struct.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/Struct.java new file mode 100644 index 0000000..d68dcfe --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/Struct.java @@ -0,0 +1,167 @@ +/** + * 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.hadoop.hbase.types; + +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Iterator; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.hbase.util.Order; + +/** + *

+ * Struct is a simple {@link DataType} for implementing the + * "compound rowkey" schema design strategy. + *

+ *

Encoding

+ *

+ * Struct member values are encoded onto the target + * ByteBuffer in the order in which they are declared. A + * Struct may be used as a member of another Struct. + * Structs are not nullable but their component + * fields may be. + *

+ *

Sort Order

+ *

+ * Struct instances sort according to the composite order of + * their fields, that is, left-to-right and depth-first. This can also be + * thought of as lexicographic comparison of concatenated members. + *

+ *

+ * {@link StructIterator} is provided as a convenience for consuming the + * sequence of values. Users may find it more appropriate to provide their own + * custom {@link DataType} for encoding application objects rather than using + * this Object[] implementation. Examples are provided in test. + *

+ * @see StructIterator + * @see DataType#isNullable() + */ +@InterfaceAudience.Public +@InterfaceStability.Evolving +public class Struct implements DataType { + + @SuppressWarnings("rawtypes") + protected final DataType[] fields; + protected final boolean isOrderPreserving; + protected final boolean isSkippable; + + /** + * Create a new Struct instance defined as the sequence of + * HDataTypes in memberTypes. + *

+ * A Struct is orderPreserving when all of its + * fields are orderPreserving. A Struct is + * skippable when all of its fields are skippable. + *

+ */ + @SuppressWarnings("rawtypes") + public Struct(DataType[] memberTypes) { + this.fields = memberTypes; + // a Struct is not orderPreserving when any of its fields are not. + boolean preservesOrder = true; + // a Struct is not skippable when any of its fields are not. + boolean skippable = true; + for (int i = 0; i < this.fields.length; i++) { + DataType dt = this.fields[i]; + if (!dt.isOrderPreserving()) preservesOrder = false; + if (i < this.fields.length - 2 && !dt.isSkippable()) + throw new IllegalArgumentException("Non-right-most struct fields must be skippable."); + if (!dt.isSkippable()) skippable = false; + } + this.isOrderPreserving = preservesOrder; + this.isSkippable = skippable; + } + + @Override + public boolean isOrderPreserving() { return isOrderPreserving; } + + @Override + public Order getOrder() { return null; } + + @Override + public boolean isNullable() { return false; } + + @Override + public boolean isSkippable() { return isSkippable; } + + @SuppressWarnings("unchecked") + @Override + public int encodedLength(Object[] val) { + assert fields.length == val.length; + int sum = 0; + for (int i = 0; i < fields.length; i++) + sum += fields[i].encodedLength(val[i]); + return sum; + } + + @Override + public Class encodedClass() { return Object[].class; } + + /** + * Retrieve an {@link Iterator} over the values encoded in b. + * The byte[] backing b is not modified by use of + * the Iterator, however the state of b is. To + * create multiple Iterators over the same backing + * ByteBuffer, construct the Iterators over + * duplicates ( {@link ByteBuffer#duplicate()} ) of b. + */ + public StructIterator iterator(ByteBuffer buff) { + return new StructIterator(buff, fields); + } + + @Override + public void skip(ByteBuffer buff) { + StructIterator it = iterator(buff); + while (it.hasNext()) + it.skip(); + } + + @Override + public Object[] decode(ByteBuffer buff) { + ArrayList values = new ArrayList(fields.length); + Iterator it = iterator(buff); + while (it.hasNext()) + values.add(it.next()); + assert values.size() == fields.length; + return values.toArray(); + } + + /** + * Read the field at position. buff is left + * unmodified. + */ + public Object read(ByteBuffer buff, int position) { + assert position >= 0; + ByteBuffer b = buff.duplicate(); + StructIterator it = iterator(b); + for (; position > 0; position--) + it.skip(); + return it.next(); + } + + @SuppressWarnings("unchecked") + @Override + public void encode(ByteBuffer buff, Object[] val) { + assert fields.length == val.length; + for (int i = 0; i < fields.length; i++) { + fields[i].encode(buff, val[i]); + } + } +} diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/types/StructBuilder.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/StructBuilder.java new file mode 100644 index 0000000..c17b190 --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/StructBuilder.java @@ -0,0 +1,54 @@ +/** + * 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.hadoop.hbase.types; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; + +/** + * A helper for building {@link Struct} instances. + */ +@InterfaceAudience.Public +@InterfaceStability.Evolving +public class StructBuilder { + + protected final List> fields = new ArrayList>(); + + /** + * Create an empty StructBuilder. + */ + public StructBuilder() {} + + /** + * Append field to the sequence of accumulated fields. + */ + public StructBuilder add(DataType field) { fields.add(field); return this; } + + /** + * Retrieve the {@link Struct} represented by this. + */ + public Struct toStruct() { return new Struct(fields.toArray(new DataType[fields.size()])); } + + /** + * Reset the sequence of accumulated fields. + */ + public StructBuilder reset() { fields.clear(); return this; } +} diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/types/StructIterator.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/StructIterator.java new file mode 100644 index 0000000..f33a989 --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/StructIterator.java @@ -0,0 +1,86 @@ +/** + * 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.hadoop.hbase.types; + +import java.nio.ByteBuffer; +import java.util.Iterator; +import java.util.NoSuchElementException; + +/** + * An {@link Iterator} over encoded Struct members. + *

+ * This iterates over each serialized Struct field from the + * specified HDataTypes[] definition. It allows you to read + * the field or skip over its serialized bytes using {@link #next()} and + * {@link #skip()}, respectively. This is in contrast to the + * Struct method which allow you to + * {@link Struct#decode(ByteBuffer)} or {@link Struct#skip(ByteBuffer)} over the + * entire Struct at once. + *

+ *

+ * This iterator may also be used to read bytes from any Struct + * for which the specified HDataType[] is a prefix. For + * example, if the specified Struct definition has a + * {@link OrderedNumeric} and a {@link OrderedString} field, you may parse the serialized + * output of a Struct whose fields are {@link OrderedNumeric}, + * {@link OrderedString}, and {@link Binary}. The iterator would return a number + * followed by a String. The trailing byte[] would + * be ignored. + *

+ */ +public class StructIterator implements Iterator { + + protected final ByteBuffer buff; + @SuppressWarnings("rawtypes") + protected final DataType[] types; + protected int idx = 0; + + /** + * Construct StructIterator over the values encoded in + * buff using the specified types definition. + * @param buff The buffer from which to read encoded values. + * @param types The sequence of types to use as the schema for this + * Struct. + */ + public StructIterator(final ByteBuffer buff, @SuppressWarnings("rawtypes") DataType[] types) { + this.buff = buff; + this.types = types; + } + + @Override + public boolean hasNext() { + return idx < types.length && buff.position() != buff.limit(); + } + + @Override + public void remove() { throw new UnsupportedOperationException(); } + + @Override + public Object next() { + if (!hasNext()) throw new NoSuchElementException(); + return types[idx++].decode(buff); + } + + /** + * Bypass the next encoded value. + */ + public void skip() { + if (!hasNext()) throw new NoSuchElementException(); + types[idx++].skip(buff); + } +} diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/types/TerminatedWrapper.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/TerminatedWrapper.java new file mode 100644 index 0000000..70daa9d --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/TerminatedWrapper.java @@ -0,0 +1,152 @@ +/** + * 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.hadoop.hbase.types; + +import java.nio.ByteBuffer; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.hadoop.hbase.util.Order; + +/** + * Wraps an existing HDataType implementation as a terminated + * version of itself. This has the useful side-effect of turning an existing + * HDataType which is not skippable into a + * skippable variant. + */ +@InterfaceAudience.Public +@InterfaceStability.Evolving +public class TerminatedWrapper implements DataType { + + protected final DataType wrapped; + protected final byte[] term; + + /** + * Create a terminated version of the wrapped. + * @throws IllegalArgumentException when term is null or empty. + */ + public TerminatedWrapper(DataType wrapped, byte[] term) { + if (null == term || term.length == 0) + throw new IllegalArgumentException("terminator must be non-null and non-empty."); + this.wrapped = wrapped; + wrapped.getOrder().apply(term); + this.term = term; + } + + /** + * Create a terminated version of the wrapped. + * term is converted to a byte[] using + * {@link Bytes#toBytes(String)}. + * @throws IllegalArgumentException when term is null or empty. + */ + public TerminatedWrapper(DataType wrapped, String term) { + this(wrapped, Bytes.toBytes(term)); + } + + @Override + public boolean isOrderPreserving() { return wrapped.isOrderPreserving(); } + + @Override + public Order getOrder() { return wrapped.getOrder(); } + + @Override + public boolean isNullable() { return wrapped.isNullable(); } + + @Override + public boolean isSkippable() { return true; } + + @Override + public int encodedLength(T val) { + return wrapped.encodedLength(val) + term.length; + } + + @Override + public Class encodedClass() { return wrapped.encodedClass(); } + + /** + * Return the position at which term begins within + * buff, or -1 if term is not found. + */ + protected int terminatorPosition(ByteBuffer buff) { + int i, limit; + SKIP: for (i = buff.position(), limit = buff.limit(); i < limit; i++) { + if (buff.array()[i] != term[0]) continue; + int j; + for (j = 1; j < term.length && i + j < buff.limit(); j++) { + if (buff.array()[i + j] != term[j]) continue SKIP; + } + if (j == term.length) return i; // success + } + return -1; + } + + /** + * Skip the position of buff over the encoded value. + * @throws IllegalArgumentException when the terminator sequence is not + * found. In the event this exception is thrown, + * buff#position() is restored to the original value. + */ + @Override + public void skip(ByteBuffer buff) { + if (wrapped.isSkippable()) { + wrapped.skip(buff); + buff.position(buff.position() + term.length); + } else { + int skipTo = terminatorPosition(buff); + if (-1 == skipTo) { + throw new IllegalArgumentException("Terminator sequence not found."); + } + buff.position(skipTo + term.length); + } + } + + @Override + public T decode(ByteBuffer buff) { + T ret; + if (wrapped.isSkippable()) { + ret = wrapped.decode(buff); + } else { + ByteBuffer b = buff.duplicate(); + b.limit(terminatorPosition(buff)); + ret = wrapped.decode(b); + buff.position(b.position()); + } + buff.position(buff.position() + term.length); + return ret; + } + + /** + * Write instance val into buffer buff. + * @throws IllegalArgumentException when the encoded representation of + * val contains the term sequence. In + * the event this exception is thrown, + * buff#position() is restored the original value. + */ + @Override + public void encode(ByteBuffer buff, T val) { + int start = buff.position(); + wrapped.encode(buff, val); + ByteBuffer b = ByteBuffer.wrap(buff.array(), start, buff.position() - start); + if (-1 != terminatorPosition(b)) { + buff.position(start); + throw new IllegalArgumentException("Encoded value contains terminator sequence."); + } + buff.put(term); + } +} diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/types/Union2.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/Union2.java new file mode 100644 index 0000000..8276137 --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/Union2.java @@ -0,0 +1,87 @@ +/** + * 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.hadoop.hbase.types; + +import java.nio.ByteBuffer; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.hbase.util.Order; + +/** + * The Union family of {@link DataType}s encode one of a fixed + * set of Objects. They provide convenience methods which handle + * type casting on your behalf. + */ +@SuppressWarnings("unchecked") +@InterfaceAudience.Public +@InterfaceStability.Evolving +public abstract class Union2 implements DataType { + + protected final DataType typeA; + protected final DataType typeB; + + /** + * Create an instance of Union2 over the set of specified + * types. + */ + public Union2(DataType typeA, DataType typeB) { + this.typeA = typeA; + this.typeB = typeB; + } + + @Override + public boolean isOrderPreserving() { + return typeA.isOrderPreserving() && typeB.isOrderPreserving(); + } + + @Override + public Order getOrder() { return null; } + + @Override + public boolean isNullable() { + return typeA.isNullable() && typeB.isNullable(); + } + + @Override + public boolean isSkippable() { + return typeA.isSkippable() && typeB.isSkippable(); + } + + @Override + public Class encodedClass() { + throw new UnsupportedOperationException( + "Union types do not expose a definitive encoded class."); + } + + /** + * Read an instance of the first type parameter from buffer + * buff. + */ + public A decodeA(ByteBuffer buff) { + return (A) decode(buff); + } + + /** + * Read an instance of the second type parameter from buffer + * buff. + */ + public B decodeB(ByteBuffer buff) { + return (B) decode(buff); + } +} diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/types/Union3.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/Union3.java new file mode 100644 index 0000000..6bdf6e2 --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/Union3.java @@ -0,0 +1,72 @@ +/** + * 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.hadoop.hbase.types; + +import java.nio.ByteBuffer; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.hbase.util.Order; + +/** + * The Union family of {@link DataType}s encode one of a fixed + * collection of Objects. They provide convenience methods which handle type + * casting on your behalf. + * @see Union2 + */ +@SuppressWarnings("unchecked") +@InterfaceAudience.Public +@InterfaceStability.Evolving +public abstract class Union3 extends Union2 { + + protected final DataType typeC; + + /** + * Create an instance of Union3 over the set of specified + * types. + */ + public Union3(DataType typeA, DataType typeB, DataType typeC) { + super(typeA, typeB); + this.typeC = typeC; + } + + @Override + public boolean isOrderPreserving() { + return super.isOrderPreserving() && typeC.isOrderPreserving(); + } + + @Override + public Order getOrder() { return null; } + + @Override + public boolean isNullable() { + return super.isNullable() && typeC.isNullable(); + } + + @Override + public boolean isSkippable() { + return super.isSkippable() && typeC.isSkippable(); + } + + /** + * Read an instance of the third type parameter from buffer b. + */ + public C decodeC(ByteBuffer buff) { + return (C) decode(buff); + } +} diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/types/Union4.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/Union4.java new file mode 100644 index 0000000..02b87fe --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/types/Union4.java @@ -0,0 +1,71 @@ +/** + * 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.hadoop.hbase.types; + +import java.nio.ByteBuffer; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.hbase.util.Order; + +/** + * The Union family of {@link DataType}s encode one of a fixed + * collection of Objects. They provide convenience methods which handle type + * casting on your behalf. + */ +@SuppressWarnings("unchecked") +@InterfaceAudience.Public +@InterfaceStability.Evolving +public abstract class Union4 extends Union3 { + + protected final DataType typeD; + + /** + * Create an instance of Union4 over the set of specified + * types. + */ + public Union4(DataType typeA, DataType typeB, DataType typeC, DataType typeD) { + super(typeA, typeB, typeC); + this.typeD = typeD; + } + + @Override + public boolean isOrderPreserving() { + return super.isOrderPreserving() && typeD.isOrderPreserving(); + } + + @Override + public Order getOrder() { return null; } + + @Override + public boolean isNullable() { + return super.isNullable() && typeD.isNullable(); + } + + @Override + public boolean isSkippable() { + return super.isSkippable() && typeD.isSkippable(); + } + + /** + * Read an instance of the fourth type parameter from buffer b. + */ + public D decodeD(ByteBuffer b) { + return (D) decode(b); + } +} diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/Numeric.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/Numeric.java index 9d1a4f8..4d31fe2 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/Numeric.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/Numeric.java @@ -53,7 +53,7 @@ public class Numeric extends Number { private transient int hashCode = 0; /** - * Create an HNumeric instance over a double. + * Create an Numeric instance over a double. */ public Numeric(double val) { isR = true; @@ -64,7 +64,7 @@ public class Numeric extends Number { } /** - * Create an HNumeric instance over a long. + * Create an Numeric instance over a long. */ public Numeric(long val) { isZ = true; @@ -75,7 +75,7 @@ public class Numeric extends Number { } /** - * Create an HNumeric instance over a BigDecimal . + * Create an Numeric instance over a BigDecimal . */ public Numeric(BigDecimal val) { if (null == val) throw new NullPointerException(); diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/OrderedBytes.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/OrderedBytes.java index 2d24636..9f548ef 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/OrderedBytes.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/OrderedBytes.java @@ -840,6 +840,14 @@ public class OrderedBytes { /** * Encode a BLOB value using a modified varint encoding scheme. + * @see #encodeBlobVar(ByteBuffer, byte[], int, int, Order) + */ + public static void encodeBlobVar(ByteBuffer buff, byte[] val, Order ord) { + encodeBlobVar(buff, val, 0, null != val ? val.length : 0, ord); + } + + /** + * Encode a BLOB value using a modified varint encoding scheme. *

* This format encodes a byte[] value such that no limitations on the input * value are imposed. The first byte encodes the encoding scheme that @@ -851,20 +859,20 @@ public class OrderedBytes { * on each encoded byte carry the value payload. *

*/ - public static void encodeBlobVar(ByteBuffer buff, byte[] val, Order ord) { + public static void encodeBlobVar(ByteBuffer buff, byte[] val, int offset, int len, Order ord) { if (null == val) { encodeNull(buff, ord); return; } // Empty value is null-terminated. All other values are encoded as 7-bits per byte. - assert buff.remaining() >= blobVarEncodedLength(val.length) : "buffer overflow expected."; + assert buff.remaining() >= blobVarEncodedLength(len) : "buffer overflow expected."; int start = buff.position(); buff.put(BLOB_VAR); - if (0 == val.length) { + if (0 == len) { buff.put(TERM); } else { byte s = 1, t = 0; - for (int i = 0; i < val.length; i++) { + for (int i = offset; i < len; i++) { buff.put((byte) (0x80 | t | ((val[i] & 0xff) >>> s))); if (s < 7) { t = (byte) (val[i] << (7 - s)); diff --git a/hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestFixedLengthWrapper.java b/hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestFixedLengthWrapper.java new file mode 100644 index 0000000..d42d4f6 --- /dev/null +++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestFixedLengthWrapper.java @@ -0,0 +1,88 @@ +/** + * 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.hadoop.hbase.types; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; + +import java.nio.BufferOverflowException; +import java.nio.ByteBuffer; +import java.util.Arrays; + +import org.apache.hadoop.hbase.SmallTests; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.hadoop.hbase.util.Order; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +@Category(SmallTests.class) +public class TestFixedLengthWrapper { + + static final byte[][] VALUES = new byte[][] { + Bytes.toBytes(""), Bytes.toBytes("1"), Bytes.toBytes("22"), Bytes.toBytes("333"), + Bytes.toBytes("4444"), Bytes.toBytes("55555"), Bytes.toBytes("666666"), + Bytes.toBytes("7777777"), Bytes.toBytes("88888888"), Bytes.toBytes("999999999"), + }; + + /** + * all values of limit are >= max length of a member of + * VALUES. + */ + static final int[] limits = { 9, 12, 15 }; + + @Test + public void testReadWrite() { + for (int limit : limits) { + ByteBuffer buff = ByteBuffer.allocate(limit); + for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) { + for (byte[] val : VALUES) { + DataType type = new FixedLengthWrapper(new LegacyBytes(ord), limit); + buff.clear(); + type.encode(buff, val); + assertEquals(buff.limit(), buff.position()); + buff.flip(); + byte[] expected = Arrays.copyOf(val, limit); + byte[] actual = type.decode(buff); + assertEquals(buff.limit(), buff.position()); + assertArrayEquals(expected, actual); + } + } + } + } + + @Test(expected = IllegalArgumentException.class) + public void testInsufficientRemainingRead() { + ByteBuffer buff = ByteBuffer.allocate(0); + DataType type = new FixedLengthWrapper(new LegacyBytes(), 3); + type.decode(buff); + } + + @Test(expected = IllegalArgumentException.class) + public void testInsufficientRemainingWrite() { + ByteBuffer buff = ByteBuffer.allocate(0); + DataType type = new FixedLengthWrapper(new LegacyBytes(), 3); + type.encode(buff, Bytes.toBytes("")); + } + + @Test(expected = BufferOverflowException.class) + public void testOverflowPassthrough() { + ByteBuffer buff = ByteBuffer.allocate(3); + DataType type = new FixedLengthWrapper(new LegacyBytes(), 0); + type.encode(buff, Bytes.toBytes("foo")); + } +} diff --git a/hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestOrderedBlob.java b/hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestOrderedBlob.java new file mode 100644 index 0000000..c462119 --- /dev/null +++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestOrderedBlob.java @@ -0,0 +1,48 @@ +/** + * 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.hadoop.hbase.types; + +import static org.junit.Assert.assertEquals; + +import java.nio.ByteBuffer; + +import org.apache.hadoop.hbase.util.Bytes; +import org.junit.Test; + +public class TestOrderedBlob { + + static final byte[][] VALUES = new byte[][] { + null, Bytes.toBytes(""), Bytes.toBytes("1"), Bytes.toBytes("22"), Bytes.toBytes("333"), + Bytes.toBytes("4444"), Bytes.toBytes("55555"), Bytes.toBytes("666666"), + Bytes.toBytes("7777777"), Bytes.toBytes("88888888"), Bytes.toBytes("999999999"), + }; + + @Test + public void testEncodedLength() { + ByteBuffer buff = ByteBuffer.allocate(20); + for (DataType type : new OrderedBlob[] { OrderedBlob.ASCENDING, OrderedBlob.DESCENDING }) { + for (byte[] val : VALUES) { + buff.clear(); + type.encode(buff, val); + assertEquals( + "encodedLength does not match actual, " + Bytes.toStringBinary(val), + buff.position(), type.encodedLength(val)); + } + } + } +} diff --git a/hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestOrderedBlobVar.java b/hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestOrderedBlobVar.java new file mode 100644 index 0000000..c43db77 --- /dev/null +++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestOrderedBlobVar.java @@ -0,0 +1,49 @@ +/** + * 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.hadoop.hbase.types; + +import static org.junit.Assert.assertEquals; + +import java.nio.ByteBuffer; + +import org.apache.hadoop.hbase.util.Bytes; +import org.junit.Test; + +public class TestOrderedBlobVar { + + static final byte[][] VALUES = new byte[][] { + null, Bytes.toBytes(""), Bytes.toBytes("1"), Bytes.toBytes("22"), Bytes.toBytes("333"), + Bytes.toBytes("4444"), Bytes.toBytes("55555"), Bytes.toBytes("666666"), + Bytes.toBytes("7777777"), Bytes.toBytes("88888888"), Bytes.toBytes("999999999"), + }; + + @Test + public void testEncodedLength() { + ByteBuffer buff = ByteBuffer.allocate(20); + for (DataType type : + new OrderedBlobVar[] { OrderedBlobVar.ASCENDING, OrderedBlobVar.DESCENDING }) { + for (byte[] val : VALUES) { + buff.clear(); + type.encode(buff, val); + assertEquals( + "encodedLength does not match actual, " + Bytes.toStringBinary(val), + buff.position(), type.encodedLength(val)); + } + } + } +} diff --git a/hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestOrderedString.java b/hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestOrderedString.java new file mode 100644 index 0000000..795dfa5 --- /dev/null +++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestOrderedString.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.hadoop.hbase.types; + +import static org.junit.Assert.assertEquals; + +import java.nio.ByteBuffer; + +import org.junit.Test; + +public class TestOrderedString { + + static final String[] VALUES = + new String[] { null, "", "1", "22", "333", "4444", "55555", "666666", + "7777777", "88888888", "999999999" }; + + @Test + public void testEncodedLength() { + ByteBuffer buff = ByteBuffer.allocate(20); + for (DataType type : new OrderedString[] { OrderedString.ASCENDING, OrderedString.DESCENDING }) { + for (String val : VALUES) { + buff.clear(); + type.encode(buff, val); + assertEquals( + "encodedLength does not match actual, " + val, + buff.position(), type.encodedLength(val)); + } + } + } +} diff --git a/hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestStruct.java b/hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestStruct.java new file mode 100644 index 0000000..f40a519 --- /dev/null +++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestStruct.java @@ -0,0 +1,434 @@ +package org.apache.hadoop.hbase.types; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; + +import java.lang.reflect.Constructor; +import java.nio.ByteBuffer; +import java.util.Arrays; +import java.util.Collection; +import java.util.Comparator; + +import org.apache.hadoop.hbase.SmallTests; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.hadoop.hbase.util.Order; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +/** + * This class both tests and demonstrates how to construct compound rowkeys + * from a POJO. The code under test is {@link Struct}. + * {@link SpecializedPojo1Type1} demonstrates how one might create their own + * custom data type extension for an application POJO. + */ +@RunWith(Parameterized.class) +@Category(SmallTests.class) +public class TestStruct { + + private Struct generic; + @SuppressWarnings("rawtypes") + private DataType specialized; + private Object[][] constructorArgs; + + public TestStruct(Struct generic, @SuppressWarnings("rawtypes") DataType specialized, + Object[][] constructorArgs) { + this.generic = generic; + this.specialized = specialized; + this.constructorArgs = constructorArgs; + } + + @Parameters + public static Collection params() { + Object[][] pojo1Type1Args = { + new Object[] { "foo", -5, new org.apache.hadoop.hbase.util.Numeric(10.001) }, + new Object[] { "foo", 100, new org.apache.hadoop.hbase.util.Numeric(-7.0) }, + new Object[] { "foo", 100, new org.apache.hadoop.hbase.util.Numeric(10.001) }, + new Object[] { "bar", -5, new org.apache.hadoop.hbase.util.Numeric(10.001) }, + new Object[] { "bar", 100, new org.apache.hadoop.hbase.util.Numeric(10.001) }, + new Object[] { "baz", -5, new org.apache.hadoop.hbase.util.Numeric(10.001) }, + }; + + Object[][] pojo1Type2Args = { + new Object[] { "foo", new org.apache.hadoop.hbase.util.Numeric(-5), 10.001 }, + new Object[] { "foo", new org.apache.hadoop.hbase.util.Numeric(100), -7.0 }, + new Object[] { "foo", new org.apache.hadoop.hbase.util.Numeric(100), 10.001 }, + new Object[] { "bar", new org.apache.hadoop.hbase.util.Numeric(-5), 10.001 }, + new Object[] { "bar", new org.apache.hadoop.hbase.util.Numeric(100), 10.001 }, + new Object[] { "baz", new org.apache.hadoop.hbase.util.Numeric(-5), 10.001 }, + }; + + Object[][] pojo2Type1Args = { + new Object[] { null, "it".getBytes(), "was", "the".getBytes() }, + new Object[] { "best".getBytes(), null, "of", "times,".getBytes() }, + new Object[] { "it".getBytes(), "was".getBytes(), null, "the".getBytes() }, + new Object[] { "worst".getBytes(), "of".getBytes(), "times,", null }, + new Object[] { null, null, null, null }, + }; + + Object[][] params = new Object[][] { + { SpecializedPojo1Type1.GENERIC, new SpecializedPojo1Type1(), pojo1Type1Args }, + { SpecializedPojo1Type2.GENERIC, new SpecializedPojo1Type2(), pojo1Type2Args }, + { SpecializedPojo2Type1.GENERIC, new SpecializedPojo2Type1(), pojo2Type1Args }, + }; + return Arrays.asList(params); + } + + static final Comparator NULL_SAFE_BYTES_COMPARATOR = + new Comparator() { + @Override + public int compare(byte[] o1, byte[] o2) { + if (o1 == o2) return 0; + if (null == o1) return -1; + if (null == o2) return 1; + return Bytes.compareTo(o1, o2); + } + }; + + /** + * A simple object to serialize. + */ + private static class Pojo1 implements Comparable { + final String stringFieldAsc; + final int intFieldDsc; + final double doubleFieldAsc; + final transient String str; + + public Pojo1(Object... argv) { + stringFieldAsc = (String) argv[0]; + intFieldDsc = + argv[1] instanceof Integer ? + (Integer) argv[1] : + ((org.apache.hadoop.hbase.util.Numeric) argv[1]).intValue(); + doubleFieldAsc = + argv[2] instanceof Double ? + (Double) argv[2] : + ((org.apache.hadoop.hbase.util.Numeric) argv[2]).doubleValue(); + str = new StringBuilder() + .append("{ ") + .append(null == stringFieldAsc ? "" : "\"") + .append(stringFieldAsc) + .append(null == stringFieldAsc ? "" : "\"").append(", ") + .append(intFieldDsc).append(", ") + .append(doubleFieldAsc) + .append(" }") + .toString(); + } + + @Override + public String toString() { + return str; + } + + @Override + public int compareTo(Pojo1 o) { + int cmp = stringFieldAsc.compareTo(o.stringFieldAsc); + if (cmp != 0) return cmp; + cmp = -Integer.valueOf(intFieldDsc).compareTo(Integer.valueOf(o.intFieldDsc)); + if (cmp != 0) return cmp; + return Double.compare(doubleFieldAsc, o.doubleFieldAsc); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (null == o) return false; + if (!(o instanceof Pojo1)) return false; + Pojo1 that = (Pojo1) o; + return 0 == this.compareTo(that); + } + } + + /** + * A simple object to serialize. + */ + private static class Pojo2 implements Comparable { + final byte[] byteField1Asc; + final byte[] byteField2Dsc; + final String stringFieldDsc; + final byte[] byteField3Dsc; + final transient String str; + + public Pojo2(Object... vals) { + byteField1Asc = (byte[]) vals[0]; + byteField2Dsc = (byte[]) vals[1]; + stringFieldDsc = (String) vals[2]; + byteField3Dsc = (byte[]) vals[3]; + str = new StringBuilder() + .append("{ ") + .append(Bytes.toStringBinary(byteField1Asc)).append(", ") + .append(Bytes.toStringBinary(byteField2Dsc)).append(", ") + .append(null == stringFieldDsc ? "" : "\"") + .append(stringFieldDsc) + .append(null == stringFieldDsc ? "" : "\"").append(", ") + .append(Bytes.toStringBinary(byteField3Dsc)) + .append(" }") + .toString(); + } + + @Override + public String toString() { + return str; + } + + @Override + public int compareTo(Pojo2 o) { + int cmp = NULL_SAFE_BYTES_COMPARATOR.compare(byteField1Asc, o.byteField1Asc); + if (cmp != 0) return cmp; + cmp = -NULL_SAFE_BYTES_COMPARATOR.compare(byteField2Dsc, o.byteField2Dsc); + if (cmp != 0) return cmp; + if (stringFieldDsc == o.stringFieldDsc) cmp = 0; + else if (null == stringFieldDsc) cmp = 1; + else if (null == o.stringFieldDsc) cmp = -1; + else cmp = -stringFieldDsc.compareTo(o.stringFieldDsc); + if (cmp != 0) return cmp; + return -NULL_SAFE_BYTES_COMPARATOR.compare(byteField3Dsc, o.byteField3Dsc); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (null == o) return false; + if (!(o instanceof Pojo2)) return false; + Pojo2 that = (Pojo2) o; + return 0 == this.compareTo(that); + } + } + + /** + * A custom data type implementation specialized for {@link Pojo1}. + */ + private static class SpecializedPojo1Type1 implements DataType { + + private static final OrderedString stringField = OrderedString.ASCENDING; + private static final OrderedInt32 intField = OrderedInt32.DESCENDING; + private static final OrderedNumeric doubleField = OrderedNumeric.ASCENDING; + + /** + * The {@link Struct} equivalent of this type. + */ + public static Struct GENERIC = + new StructBuilder().add(stringField) + .add(intField) + .add(doubleField) + .toStruct(); + + @Override + public boolean isOrderPreserving() { return true; } + + @Override + public Order getOrder() { return null; } + + @Override + public boolean isNullable() { return false; } + + @Override + public boolean isSkippable() { return true; } + + @Override + public int encodedLength(Pojo1 val) { + return + stringField.encodedLength(val.stringFieldAsc) + + intField.encodedLength(val.intFieldDsc) + + doubleField.encodedLength(new org.apache.hadoop.hbase.util.Numeric(val.doubleFieldAsc)); + } + + @Override + public Class encodedClass() { return Pojo1.class; } + + @Override + public void skip(ByteBuffer buff) { + stringField.skip(buff); + intField.skip(buff); + doubleField.skip(buff); + } + + @Override + public Pojo1 decode(ByteBuffer buff) { + return new Pojo1(new Object[] { + stringField.decode(buff), + intField.decodeInt(buff), + doubleField.decodeDouble(buff) + }); + } + + @Override + public void encode(ByteBuffer buff, Pojo1 val) { + stringField.encode(buff, val.stringFieldAsc); + intField.encodeInt(buff, val.intFieldDsc); + doubleField.encodeDouble(buff, val.doubleFieldAsc); + } + } + + /** + * A custom data type implementation specialized for {@link Pojo1}. + */ + private static class SpecializedPojo1Type2 implements DataType { + + private static final OrderedString stringField = OrderedString.ASCENDING; + private static final OrderedNumeric intField = OrderedNumeric.DESCENDING; + private static final OrderedFloat64 doubleField = OrderedFloat64.ASCENDING; + + /** + * The {@link Struct} equivalent of this type. + */ + public static Struct GENERIC = + new StructBuilder().add(stringField) + .add(intField) + .add(doubleField) + .toStruct(); + + @Override + public boolean isOrderPreserving() { return true; } + + @Override + public Order getOrder() { return null; } + + @Override + public boolean isNullable() { return false; } + + @Override + public boolean isSkippable() { return true; } + + @Override + public int encodedLength(Pojo1 val) { + return + stringField.encodedLength(val.stringFieldAsc) + + intField.encodedLength(new org.apache.hadoop.hbase.util.Numeric(val.intFieldDsc)) + + doubleField.encodedLength(val.doubleFieldAsc); + } + + @Override + public Class encodedClass() { return Pojo1.class; } + + @Override + public void skip(ByteBuffer buff) { + stringField.skip(buff); + intField.skip(buff); + doubleField.skip(buff); + } + + @Override + public Pojo1 decode(ByteBuffer buff) { + return new Pojo1(new Object[] { + stringField.decode(buff), + intField.decode(buff).intValue(), + doubleField.decodeDouble(buff) + }); + } + + @Override + public void encode(ByteBuffer buff, Pojo1 val) { + stringField.encode(buff, val.stringFieldAsc); + intField.encodeLong(buff, val.intFieldDsc); + doubleField.encodeDouble(buff, val.doubleFieldAsc); + } + } + + /** + * A custom data type implementation specialized for {@link Pojo2}. + */ + private static class SpecializedPojo2Type1 implements DataType { + + private static OrderedBlobVar byteField1 = OrderedBlobVar.ASCENDING; + private static OrderedBlobVar byteField2 = OrderedBlobVar.DESCENDING; + private static OrderedString stringField = OrderedString.DESCENDING; + private static OrderedBlob byteField3 = OrderedBlob.DESCENDING; + + /** + * The {@link Struct} equivalent of this type. + */ + public static Struct GENERIC = + new StructBuilder().add(byteField1) + .add(byteField2) + .add(stringField) + .add(byteField3) + .toStruct(); + + @Override + public boolean isOrderPreserving() { return true; } + + @Override + public Order getOrder() { return null; } + + @Override + public boolean isNullable() { return false; } + + @Override + public boolean isSkippable() { return true; } + + @Override + public int encodedLength(Pojo2 val) { + return + byteField1.encodedLength(val.byteField1Asc) + + byteField2.encodedLength(val.byteField2Dsc) + + stringField.encodedLength(val.stringFieldDsc) + + byteField3.encodedLength(val.byteField3Dsc); + } + + @Override + public Class encodedClass() { return Pojo2.class; } + + @Override + public void skip(ByteBuffer buff) { + byteField1.skip(buff); + byteField2.skip(buff); + stringField.skip(buff); + byteField3.skip(buff); + } + + @Override + public Pojo2 decode(ByteBuffer buff) { + return new Pojo2( + byteField1.decode(buff), + byteField2.decode(buff), + stringField.decode(buff), + byteField3.decode(buff)); + } + + @Override + public void encode(ByteBuffer buff, Pojo2 val) { + byteField1.encode(buff, val.byteField1Asc); + byteField2.encode(buff, val.byteField2Dsc); + stringField.encode(buff, val.stringFieldDsc); + byteField3.encode(buff, val.byteField3Dsc); + } + } + + @Test + @SuppressWarnings("unchecked") + public void testOrderPreservation() throws Exception { + Object[] vals = new Object[constructorArgs.length]; + byte[][] encodedGeneric = new byte[constructorArgs.length][]; + byte[][] encodedSpecialized = new byte[constructorArgs.length][]; + Constructor ctor = specialized.encodedClass().getConstructor(Object[].class); + for (int i = 0; i < vals.length; i++) { + vals[i] = ctor.newInstance(new Object[] { constructorArgs[i] }); + encodedGeneric[i] = new byte[generic.encodedLength(constructorArgs[i])]; + encodedSpecialized[i] = new byte[specialized.encodedLength(vals[i])]; + } + + // populate our arrays + for (int i = 0; i < vals.length; i++) { + generic.encode(ByteBuffer.wrap(encodedGeneric[i]), constructorArgs[i]); + specialized.encode(ByteBuffer.wrap(encodedSpecialized[i]), vals[i]); + assertArrayEquals(encodedGeneric[i], encodedSpecialized[i]); + } + + Arrays.sort(vals); + Arrays.sort(encodedGeneric, NULL_SAFE_BYTES_COMPARATOR); + Arrays.sort(encodedSpecialized, NULL_SAFE_BYTES_COMPARATOR); + + for (int i = 0; i < vals.length; i++) { + assertEquals( + "Struct encoder does not preserve sort order at position " + i, + vals[i], + ctor.newInstance(new Object[] { generic.decode(ByteBuffer.wrap(encodedGeneric[i])) })); + assertEquals( + "Specialized encoder does not preserve sort order at position " + i, + vals[i], specialized.decode(ByteBuffer.wrap(encodedSpecialized[i]))); + } + } +} diff --git a/hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestTerminatedWrapper.java b/hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestTerminatedWrapper.java new file mode 100644 index 0000000..2f68ecf --- /dev/null +++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestTerminatedWrapper.java @@ -0,0 +1,102 @@ +/** + * 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.hadoop.hbase.types; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; + +import java.nio.ByteBuffer; + +import org.apache.hadoop.hbase.SmallTests; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.hadoop.hbase.util.Order; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +@Category(SmallTests.class) +public class TestTerminatedWrapper { + + static final byte[][] VALUES = new byte[][] { + Bytes.toBytes(""), Bytes.toBytes("1"), Bytes.toBytes("22"), Bytes.toBytes("333"), + Bytes.toBytes("4444"), Bytes.toBytes("55555"), Bytes.toBytes("666666"), + Bytes.toBytes("7777777"), Bytes.toBytes("88888888"), Bytes.toBytes("999999999"), + }; + + static final byte[][] TERMINATORS = new byte[][] { new byte[] { -1 }, Bytes.toBytes("foo") }; + + @Test(expected = IllegalArgumentException.class) + public void testEmptyDelimiter() { + new TerminatedWrapper(new LegacyBytes(), ""); + } + + @Test(expected = IllegalArgumentException.class) + public void testNullDelimiter() { + new LegacyBytesTerminated((byte[]) null); + // new TerminatedWrapper(new LegacyBytes(), (byte[]) null); + } + + @Test(expected = IllegalArgumentException.class) + public void testEncodedValueContainsTerm() { + DataType type = new TerminatedWrapper(new LegacyBytes(), "foo"); + ByteBuffer buff = ByteBuffer.allocate(16); + type.encode(buff, Bytes.toBytes("hello foobar!")); + } + + @Test + public void testReadWrite() { + ByteBuffer buff = ByteBuffer.allocate(12); + for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) { + for (byte[] term : TERMINATORS) { + for (byte[] val : VALUES) { + DataType type = new TerminatedWrapper(new LegacyBytes(ord), term); + buff.clear(); + type.encode(buff, val); + buff.flip(); + assertArrayEquals(val, type.decode(buff)); + } + } + } + } + + @Test + public void testSkip() { + ByteBuffer buff = ByteBuffer.allocate(12); + for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) { + for (byte[] term : TERMINATORS) { + for (byte[] val : VALUES) { + DataType type = new TerminatedWrapper(new LegacyBytes(ord), term); + buff.clear(); + type.encode(buff, val); + int expected = buff.position(); + buff.flip(); + type.skip(buff); + assertEquals(expected, buff.position()); + } + } + } + } + + @Test(expected = IllegalArgumentException.class) + public void testInvalidSkip() { + ByteBuffer buff = ByteBuffer.allocate(3); + buff.put(Bytes.toBytes("foo")); + buff.flip(); + DataType type = new TerminatedWrapper(new LegacyBytes(), new byte[] { 0x00 }); + type.skip(buff); + } +} diff --git a/hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestUnion2.java b/hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestUnion2.java new file mode 100644 index 0000000..5a8b41a --- /dev/null +++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestUnion2.java @@ -0,0 +1,144 @@ +/** + * 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.hadoop.hbase.types; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.nio.ByteBuffer; + +import org.apache.hadoop.hbase.util.Order; +import org.apache.hadoop.hbase.util.OrderedBytes; +import org.junit.Test; + +public class TestUnion2 { + + /** + * An example Union + */ + private static class SampleUnion1 extends Union2 { + + private final Order ord; + + public SampleUnion1(Order order) { + super( + Order.ASCENDING == order ? OrderedInt32.ASCENDING : OrderedInt32.DESCENDING, + Order.ASCENDING == order ? OrderedString.ASCENDING : OrderedString.DESCENDING); + ord = order; + } + + @Override + public void skip(ByteBuffer buff) { + // both types A and B support generic OrderedBytes.skip, so this is easy + OrderedBytes.skip(buff); + } + + @Override + public Object decode(ByteBuffer buff) { + byte header = ord.apply(buff.array()[buff.position()]); + switch (header) { + case OrderedBytes.FIXED_INT32: + return typeA.decode(buff); + case OrderedBytes.TEXT: + return typeB.decode(buff); + default: + throw new IllegalArgumentException("Unrecognized encoding format."); + } + } + + @Override + public int encodedLength(Object val) { + Integer i = null; + String s = null; + try { + i = (Integer) val; + } catch (ClassCastException e) {} + try { + s = (String) val; + } catch (ClassCastException e) {} + + if (null != i) return typeA.encodedLength(i); + if (null != s) return typeB.encodedLength(s); + throw new IllegalArgumentException("val is not a valid member of this union."); + } + + @Override + public void encode(ByteBuffer buff, Object val) { + Integer i = null; + String s = null; + try { + i = (Integer) val; + } catch (ClassCastException e) {} + try { + s = (String) val; + } catch (ClassCastException e) {} + + if (null != i) typeA.encode(buff, i); + else if (null != s) typeB.encode(buff, s); + else + throw new IllegalArgumentException("val is not of a supported type."); + } + + } + + @Test + public void testEncodeDecode() { + Integer intVal = Integer.valueOf(10); + String strVal = "hello"; + ByteBuffer buff = ByteBuffer.allocate(10); + + for (SampleUnion1 type : new SampleUnion1[] { + new SampleUnion1(Order.ASCENDING), + new SampleUnion1(Order.DESCENDING) + }) { + buff.clear(); + type.encode(buff, intVal); + buff.flip(); + assertTrue(0 == intVal.compareTo(type.decodeA(buff))); + + buff.clear(); + type.encode(buff, strVal); + buff.flip(); + assertTrue(0 == strVal.compareTo(type.decodeB(buff))); + } + } + + @Test + public void testSkip() { + Integer intVal = Integer.valueOf(10); + String strVal = "hello"; + ByteBuffer buff = ByteBuffer.allocate(10); + + for (SampleUnion1 type : new SampleUnion1[] { + new SampleUnion1(Order.ASCENDING), + new SampleUnion1(Order.DESCENDING) + }) { + buff.clear(); + type.encode(buff, intVal); + buff.flip(); + type.skip(buff); + assertEquals(buff.limit(), buff.position()); + + buff.clear(); + type.encode(buff, strVal); + buff.flip(); + type.skip(buff); + assertEquals(buff.limit(), buff.position()); + } + } +} diff --git a/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestOrderedBytes.java b/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestOrderedBytes.java index 7614c7a..1789cb7 100644 --- a/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestOrderedBytes.java +++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestOrderedBytes.java @@ -595,11 +595,11 @@ public class TestOrderedBytes { } /** - * Test length estimation algorithms for Blob-mid encoding. Does not cover + * Test length estimation algorithms for BlobVar encoding. Does not cover * 0-length input case properly. */ @Test - public void testblobMidLencodedLength() { + public void testBlobVarLencodedLength() { int[][] values = { /* decoded length, encoded length * ceil((n bytes * 8 bits/input byte) / 7 bits/encoded byte) + 1 header @@ -615,10 +615,10 @@ public class TestOrderedBytes { } /** - * Test Blob-mid encoding. + * Test BlobVar encoding. */ @Test - public void testBlobMid() { + public void testBlobVar() { byte[][] vals = { "".getBytes(), "foo".getBytes(), "foobarbazbub".getBytes(), { (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, @@ -675,10 +675,10 @@ public class TestOrderedBytes { } /** - * Test Blob-last encoding. + * Test BlobCopy encoding. */ @Test - public void testBlobLast() { + public void testBlobCopy() { byte[][] vals = { "".getBytes(), "foo".getBytes(), "foobarbazbub".getBytes(), { (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, @@ -742,10 +742,10 @@ public class TestOrderedBytes { } /** - * Assert invalid input byte[] are rejected by Blob-last + * Assert invalid input byte[] are rejected by BlobCopy */ @Test(expected = IllegalArgumentException.class) - public void testBlobLastNoZeroBytes() { + public void testBlobCopyNoZeroBytes() { byte[] val = { 0x01, 0x02, 0x00, 0x03 }; ByteBuffer buf = ByteBuffer.allocate(val.length + 2); OrderedBytes.encodeBlobCopy(buf, val, Order.ASCENDING); @@ -773,8 +773,8 @@ public class TestOrderedBytes { float float32 = 100.0f; double float64 = 100.0d; String text = "hello world."; - byte[] blobMid = Bytes.toBytes("foo"); - byte[] blobLast = Bytes.toBytes("bar"); + byte[] blobVar = Bytes.toBytes("foo"); + byte[] blobCopy = Bytes.toBytes("bar"); ByteBuffer buff = ByteBuffer.allocate(30); for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) { @@ -875,13 +875,13 @@ public class TestOrderedBytes { assertEquals(buff.position(), buff.limit()); buff.clear(); - OrderedBytes.encodeBlobVar(buff, blobMid, ord); + OrderedBytes.encodeBlobVar(buff, blobVar, ord); buff.flip(); OrderedBytes.skip(buff); assertEquals(buff.position(), buff.limit()); buff.clear(); - OrderedBytes.encodeBlobCopy(buff, blobLast, ord); + OrderedBytes.encodeBlobCopy(buff, blobCopy, ord); buff.flip(); OrderedBytes.skip(buff); assertEquals(buff.position(), buff.limit()); -- 1.8.3.2