diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValueUtil.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValueUtil.java index 4026b1b43d8..4c1f345fac3 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValueUtil.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValueUtil.java @@ -27,7 +27,7 @@ import org.apache.hadoop.hbase.KeyValue.Type; import org.apache.hadoop.hbase.util.ByteBufferUtils; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.IterableUtils; -import org.apache.hadoop.hbase.util.SimpleByteRange; +import org.apache.hadoop.hbase.util.SimpleMutableByteRange; import org.apache.hadoop.io.WritableUtils; import com.google.common.base.Function; @@ -197,8 +197,8 @@ public class KeyValueUtil { * Increment the row bytes and clear the other fields */ public static KeyValue createFirstKeyInIncrementedRow(final Cell in){ - byte[] thisRow = new SimpleByteRange(in.getRowArray(), in.getRowOffset(), in.getRowLength()) - .deepCopyToNewArray(); + byte[] thisRow = new SimpleMutableByteRange(in.getRowArray(), in.getRowOffset(), + in.getRowLength()).deepCopyToNewArray(); byte[] nextRow = Bytes.unsignedCopyAndIncrement(thisRow); return createFirstOnRow(nextRow); } 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 index 024f9ff366e..6f94a51690b 100644 --- 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 @@ -21,7 +21,7 @@ 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.PositionedByteRange; -import org.apache.hadoop.hbase.util.SimplePositionedByteRange; +import org.apache.hadoop.hbase.util.SimplePositionedMutableByteRange; /** * Wraps an existing {@link DataType} implementation as a fixed-length @@ -83,7 +83,7 @@ public class FixedLengthWrapper implements DataType { + src.getPosition() + " max length: " + length); } // create a copy range limited to length bytes. boo. - PositionedByteRange b = new SimplePositionedByteRange(length); + PositionedByteRange b = new SimplePositionedMutableByteRange(length); src.get(b.getBytes()); return base.decode(b); } 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 index 42636f11ffc..d2c837ce846 100644 --- 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 @@ -25,7 +25,7 @@ import org.apache.hadoop.classification.InterfaceStability; import org.apache.hadoop.hbase.util.Order; import org.apache.hadoop.hbase.util.OrderedBytes; import org.apache.hadoop.hbase.util.PositionedByteRange; -import org.apache.hadoop.hbase.util.SimplePositionedByteRange; +import org.apache.hadoop.hbase.util.SimplePositionedMutableByteRange; /** * An {@link Number} of arbitrary precision and variable-length encoding. The @@ -47,7 +47,7 @@ public class OrderedNumeric extends OrderedBytesBase { @Override public int encodedLength(Number val) { // TODO: this could be done better. - PositionedByteRange buff = new SimplePositionedByteRange(100); + PositionedByteRange buff = new SimplePositionedMutableByteRange(100); return encode(buff, val); } 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 index 17bd3fa7453..85d11fb24af 100644 --- 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 @@ -22,7 +22,7 @@ import org.apache.hadoop.classification.InterfaceStability; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.Order; import org.apache.hadoop.hbase.util.PositionedByteRange; -import org.apache.hadoop.hbase.util.SimplePositionedByteRange; +import org.apache.hadoop.hbase.util.SimplePositionedMutableByteRange; /** * Wraps an existing {@code DataType} implementation as a terminated @@ -134,7 +134,7 @@ public class TerminatedWrapper implements DataType { byte[] b = new byte[term - src.getPosition()]; src.get(b); // TODO: should we assert that b.position == b.length? - T ret = wrapped.decode(new SimplePositionedByteRange(b)); + T ret = wrapped.decode(new SimplePositionedMutableByteRange(b)); src.get(this.term); return ret; } diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/AbstractByteRange.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/AbstractByteRange.java new file mode 100644 index 00000000000..c5aebab599a --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/AbstractByteRange.java @@ -0,0 +1,293 @@ +/* + * 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.util; + +/** + * An abstract implementation of the ByteRange API + */ +public abstract class AbstractByteRange implements ByteRange { + + public static final int UNSET_HASH_VALUE = -1; + + // Note to maintainers: Do not make these final, as the intention is to + // reuse objects of this class + + /** + * The array containing the bytes in this range. It will be >= length. + */ + protected byte[] bytes; + + /** + * The index of the first byte in this range. {@code ByteRange.get(0)} will + * return bytes[offset]. + */ + protected int offset; + + /** + * The number of bytes in the range. Offset + length must be <= bytes.length + */ + protected int length; + + /** + * Variable for lazy-caching the hashCode of this range. Useful for frequently + * used ranges, long-lived ranges, or long ranges. + */ + protected int hash = UNSET_HASH_VALUE; + + // + // methods for managing the backing array and range viewport + // + @Override + public byte[] getBytes() { + return bytes; + } + + @Override + public abstract ByteRange unset(); + + @Override + public ByteRange set(int capacity) { + return set(new byte[capacity]); + } + + @Override + public ByteRange set(byte[] bytes) { + if (null == bytes) + return unset(); + clearHashCache(); + this.bytes = bytes; + this.offset = 0; + this.length = bytes.length; + return this; + } + + @Override + public ByteRange set(byte[] bytes, int offset, int length) { + if (null == bytes) + return unset(); + clearHashCache(); + this.bytes = bytes; + this.offset = offset; + this.length = length; + return this; + } + + @Override + public int getOffset() { + return offset; + } + + @Override + public ByteRange setOffset(int offset) { + clearHashCache(); + this.offset = offset; + return this; + } + + @Override + public int getLength() { + return length; + } + + @Override + public ByteRange setLength(int length) { + clearHashCache(); + this.length = length; + return this; + } + + @Override + public boolean isEmpty() { + return isEmpty(this); + } + + /** + * @return true when {@code range} is of zero length, false otherwise. + */ + public static boolean isEmpty(ByteRange range) { + return range == null || range.getLength() == 0; + } + + // + // methods for retrieving data + // + + @Override + public byte get(int index) { + return bytes[offset + index]; + } + + @Override + public ByteRange get(int index, byte[] dst) { + if (0 == dst.length) + return this; + return get(index, dst, 0, dst.length); + } + + @Override + public ByteRange get(int index, byte[] dst, int offset, int length) { + if (0 == length) + return this; + System.arraycopy(this.bytes, this.offset + index, dst, offset, length); + return this; + } + + @Override + public short getShort(int index) { + int offset = this.offset + index; + short n = 0; + n ^= bytes[offset] & 0xFF; + n <<= 8; + n ^= bytes[offset + 1] & 0xFF; + return n; + } + + @Override + public int getInt(int index) { + int offset = this.offset + index; + int n = 0; + for (int i = offset; i < (offset + Bytes.SIZEOF_INT); i++) { + n <<= 8; + n ^= bytes[i] & 0xFF; + } + return n; + } + + @Override + public long getLong(int index) { + int offset = this.offset + index; + long l = 0; + for (int i = offset; i < offset + Bytes.SIZEOF_LONG; i++) { + l <<= 8; + l ^= bytes[i] & 0xFF; + } + return l; + } + + // Copied from com.google.protobuf.CodedInputStream + @Override + public long getVLong(int index) { + int shift = 0; + long result = 0; + while (shift < 64) { + final byte b = get(index++); + result |= (long) (b & 0x7F) << shift; + if ((b & 0x80) == 0) { + break; + } + shift += 7; + } + return result; + } + + public static int getVLongSize(long val) { + int rPos = 0; + while ((val & ~0x7F) != 0) { + val >>>= 7; + rPos++; + } + return rPos + 1; + } + + @Override + public abstract ByteRange put(int index, byte val); + + @Override + public abstract ByteRange put(int index, byte[] val); + + @Override + public abstract ByteRange put(int index, byte[] val, int offset, int length); + + @Override + public abstract ByteRange putInt(int index, int val); + + @Override + public abstract ByteRange putLong(int index, long val); + + @Override + public abstract ByteRange putShort(int index, short val); + + @Override + public abstract int putVLong(int index, long val); + + // + // methods for duplicating the current instance + // + + @Override + public byte[] deepCopyToNewArray() { + byte[] result = new byte[length]; + System.arraycopy(bytes, offset, result, 0, length); + return result; + } + + @Override + public void deepCopyTo(byte[] destination, int destinationOffset) { + System.arraycopy(bytes, offset, destination, destinationOffset, length); + } + + @Override + public void deepCopySubRangeTo(int innerOffset, int copyLength, byte[] destination, + int destinationOffset) { + System.arraycopy(bytes, offset + innerOffset, destination, destinationOffset, copyLength); + } + + // + // methods used for comparison + // + + @Override + public int hashCode() { + if (isHashCached()) {// hash is already calculated and cached + return hash; + } + if (this.isEmpty()) {// return 0 for empty ByteRange + hash = 0; + return hash; + } + int off = offset; + hash = 0; + for (int i = 0; i < length; i++) { + hash = 31 * hash + bytes[off++]; + } + return hash; + } + + protected boolean isHashCached() { + return hash != UNSET_HASH_VALUE; + } + + protected void clearHashCache() { + hash = UNSET_HASH_VALUE; + } + + /** + * Bitwise comparison of each byte in the array. Unsigned comparison, not + * paying attention to java's signed bytes. + */ + @Override + public int compareTo(ByteRange other) { + return Bytes.compareTo(bytes, offset, length, other.getBytes(), other.getOffset(), + other.getLength()); + } + + @Override + public String toString() { + return Bytes.toStringBinary(bytes, offset, length); + } +} diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/AbstractPositionedByteRange.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/AbstractPositionedByteRange.java new file mode 100644 index 00000000000..26bcf9711cb --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/AbstractPositionedByteRange.java @@ -0,0 +1,274 @@ +/* + * 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.util; + +import java.nio.ByteBuffer; + +import com.google.common.annotations.VisibleForTesting; + +/** + * Extends the basic {@link SimpleByteRange} implementation with position + * support. {@code position} is considered transient, not fundamental to the + * definition of the range, and does not participate in + * {@link #compareTo(ByteRange)}, {@link #hashCode()}, or + * {@link #equals(Object)}. {@code Position} is retained by copy operations. + */ +public abstract class AbstractPositionedByteRange extends AbstractByteRange implements + PositionedByteRange { + /** + * The current index into the range. Like {@link ByteBuffer} position, it + * points to the next value that will be read/written in the array. It + * provides the appearance of being 0-indexed, even though its value is + * calculated according to offset. + *

+ * Position is considered transient and does not participate in + * {@link #equals(Object)} or {@link #hashCode()} comparisons. + *

+ */ + protected int position = 0; + + protected int limit = 0; + + @Override + public abstract PositionedByteRange unset(); + + @Override + public PositionedByteRange set(int capacity) { + this.position = 0; + super.set(capacity); + this.limit = capacity; + return this; + } + + @Override + public PositionedByteRange set(byte[] bytes) { + this.position = 0; + super.set(bytes); + this.limit = bytes.length; + return this; + } + + @Override + public PositionedByteRange set(byte[] bytes, int offset, int length) { + this.position = 0; + super.set(bytes, offset, length); + limit = length; + return this; + } + + /** + * Update the beginning of this range. {@code offset + length} may not be + * greater than {@code bytes.length}. Resets {@code position} to 0. + * + * @param offset + * the new start of this range. + * @return this. + */ + @Override + public PositionedByteRange setOffset(int offset) { + this.position = 0; + super.setOffset(offset); + return this; + } + + /** + * Update the length of this range. {@code offset + length} should not be + * greater than {@code bytes.length}. If {@code position} is greater than the + * new {@code length}, sets {@code position} to {@code length}. + * + * @param length + * The new length of this range. + * @return this. + */ + @Override + public PositionedByteRange setLength(int length) { + this.position = Math.min(position, length); + super.setLength(length); + return this; + } + + @Override + public int getPosition() { + return position; + } + + @Override + public PositionedByteRange setPosition(int position) { + this.position = position; + return this; + } + + @Override + public int getRemaining() { + return length - position; + } + + @Override + public byte peek() { + return bytes[offset + position]; + } + + @Override + public byte get() { + return get(position++); + } + + @Override + public PositionedByteRange get(byte[] dst) { + if (0 == dst.length) + return this; + return this.get(dst, 0, dst.length); // be clear we're calling self, not + // super + } + + @Override + public PositionedByteRange get(byte[] dst, int offset, int length) { + if (0 == length) + return this; + super.get(this.position, dst, offset, length); + this.position += length; + return this; + } + + @Override + public abstract PositionedByteRange put(byte val); + + @Override + public abstract PositionedByteRange put(byte[] val); + + @Override + public abstract PositionedByteRange put(byte[] val, int offset, int length); + + @Override + public abstract PositionedByteRange putInt(int index, int val); + + @Override + public abstract PositionedByteRange putLong(int index, long val); + + @Override + public abstract PositionedByteRange putShort(int index, short val); + + @Override + public abstract PositionedByteRange putInt(int val); + + @Override + public abstract PositionedByteRange putLong(long val); + + @Override + public abstract PositionedByteRange putShort(short val); + + @Override + public abstract int putVLong(int index, long val); + + @Override + public abstract int putVLong(long val); + /** + * Similar to {@link ByteBuffer#flip()}. Sets length to position, position to + * offset. + */ + @VisibleForTesting + PositionedByteRange flip() { + clearHashCache(); + length = position; + position = offset; + return this; + } + + /** + * Similar to {@link ByteBuffer#clear()}. Sets position to 0, length to + * capacity. + */ + @VisibleForTesting + PositionedByteRange clear() { + clearHashCache(); + position = 0; + length = bytes.length - offset; + return this; + } + + // java boilerplate + + @Override + public PositionedByteRange get(int index, byte[] dst) { + super.get(index, dst); + return this; + } + + @Override + public PositionedByteRange get(int index, byte[] dst, int offset, int length) { + super.get(index, dst, offset, length); + return this; + } + + @Override + public short getShort() { + short s = getShort(position); + position += Bytes.SIZEOF_SHORT; + return s; + } + + @Override + public int getInt() { + int i = getInt(position); + position += Bytes.SIZEOF_INT; + return i; + } + + @Override + public long getLong() { + long l = getLong(position); + position += Bytes.SIZEOF_LONG; + return l; + } + + @Override + public long getVLong() { + long p = getVLong(position); + position += getVLongSize(p); + return p; + } + + @Override + public abstract PositionedByteRange put(int index, byte val); + + @Override + public abstract PositionedByteRange put(int index, byte[] val); + + @Override + public abstract PositionedByteRange put(int index, byte[] val, int offset, int length); + + @Override + public abstract PositionedByteRange deepCopy(); + + @Override + public abstract PositionedByteRange shallowCopy(); + + @Override + public abstract PositionedByteRange shallowCopySubRange(int innerOffset, int copyLength); + + @Override + public PositionedByteRange setLimit(int limit) { + this.limit = limit; + return this; + } + + @Override + public int getLimit() { + return this.limit; + } +} diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/ByteRange.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/ByteRange.java index 90cfa09c93e..6323a4018ca 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/ByteRange.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/ByteRange.java @@ -303,4 +303,5 @@ public interface ByteRange extends Comparable { * @return new {@code ByteRange} object referencing this range's byte[]. */ public ByteRange shallowCopySubRange(int innerOffset, int copyLength); + } \ No newline at end of file diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/ByteRangeUtils.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/ByteRangeUtils.java index d0c38734941..a5714715e19 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/ByteRangeUtils.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/ByteRangeUtils.java @@ -64,7 +64,7 @@ public class ByteRangeUtils { } ArrayList ranges = Lists.newArrayListWithCapacity(arrays.size()); for (byte[] array : arrays) { - ranges.add(new SimpleByteRange(array)); + ranges.add(new SimpleMutableByteRange(array)); } return ranges; } 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 d8ee65eaa7d..bb858c498c9 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 @@ -1104,7 +1104,8 @@ public class OrderedBytes { ; end++; // increment end to 1-past last byte // create ret buffer using length of encoded data + 1 (header byte) - PositionedByteRange ret = new SimplePositionedByteRange(blobVarDecodedLength(end - start + 1)); + PositionedByteRange ret = new SimplePositionedMutableByteRange(blobVarDecodedLength(end - start + + 1)); int s = 6; byte t = (byte) ((ord.apply(a[offset + start]) << 1) & 0xff); for (int i = start + 1; i < end; i++) { @@ -1730,7 +1731,7 @@ public class OrderedBytes { */ public static int length(PositionedByteRange buff) { PositionedByteRange b = - new SimplePositionedByteRange(buff.getBytes(), buff.getOffset(), buff.getLength()); + new SimplePositionedMutableByteRange(buff.getBytes(), buff.getOffset(), buff.getLength()); b.setPosition(buff.getPosition()); int cnt = 0; for (; isEncodedValue(b); skip(buff), cnt++) diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/PositionedByteRange.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/PositionedByteRange.java index 5aebf31a8bd..0d3115433d2 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/PositionedByteRange.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/PositionedByteRange.java @@ -164,6 +164,22 @@ public interface PositionedByteRange extends ByteRange { */ public PositionedByteRange put(byte[] val, int offset, int length); + /** + * Limits the byte range upto a specified value. Limit cannot be greater than + * capacity + * + * @param limit + * @return PositionedByteRange + */ + public PositionedByteRange setLimit(int limit); + + /** + * Return the current limit + * + * @return limit + */ + public int getLimit(); + // override parent interface declarations to return this interface. @Override diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/ReadOnlyByteRangeException.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/ReadOnlyByteRangeException.java new file mode 100644 index 00000000000..6f508e29a93 --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/ReadOnlyByteRangeException.java @@ -0,0 +1,28 @@ +/* + * 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.util; + +/** + * Exception thrown when a read only byte range is modified + */ +public class ReadOnlyByteRangeException extends UnsupportedOperationException { + public ReadOnlyByteRangeException() { + + } + +} diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/SimpleByteRange.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/SimpleByteRange.java index 32058549796..db3ca0fdae6 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/SimpleByteRange.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/SimpleByteRange.java @@ -15,60 +15,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.apache.hadoop.hbase.util; -import org.apache.hadoop.classification.InterfaceAudience; -import org.apache.hadoop.classification.InterfaceStability; - /** - * A basic {@link ByteRange} implementation. + * A read only version of the {@link ByteRange}. */ -@InterfaceAudience.Public -@InterfaceStability.Evolving -public class SimpleByteRange implements ByteRange { - - private static final int UNSET_HASH_VALUE = -1; - - // Note to maintainers: Do not make these final, as the intention is to - // reuse objects of this class - - /** - * The array containing the bytes in this range. It will be >= length. - */ - protected byte[] bytes; - - /** - * The index of the first byte in this range. {@code ByteRange.get(0)} will - * return bytes[offset]. - */ - protected int offset; - - /** - * The number of bytes in the range. Offset + length must be <= bytes.length - */ - protected int length; - - /** - * Variable for lazy-caching the hashCode of this range. Useful for - * frequently used ranges, long-lived ranges, or long ranges. - */ - private int hash = UNSET_HASH_VALUE; - - /** - * Create a new {@code ByteRange} lacking a backing array and with an - * undefined viewport. - */ +public class SimpleByteRange extends AbstractByteRange { public SimpleByteRange() { - unset(); } - - /** - * Create a new {@code ByteRange} over a new backing array of size - * {@code capacity}. The range's offset and length are 0 and {@code capacity}, - * respectively. - * @param capacity the size of the backing array. - */ + public SimpleByteRange(int capacity) { this(new byte[capacity]); } @@ -96,267 +51,57 @@ public class SimpleByteRange implements ByteRange { // methods for managing the backing array and range viewport // - @Override - public byte[] getBytes() { - return bytes; - } - @Override public ByteRange unset() { - clearHashCache(); - this.bytes = null; - this.offset = 0; - this.length = 0; - return this; + throw new ReadOnlyByteRangeException(); } @Override public ByteRange set(int capacity) { - return set(new byte[capacity]); + if (super.bytes != null) { + throw new ReadOnlyByteRangeException(); + } + return super.set(capacity); } @Override public ByteRange set(byte[] bytes) { - if (null == bytes) return unset(); - clearHashCache(); - this.bytes = bytes; - this.offset = 0; - this.length = bytes.length; - return this; + if (super.bytes != null) { + throw new ReadOnlyByteRangeException(); + } + return super.set(bytes); } @Override public ByteRange set(byte[] bytes, int offset, int length) { - if (null == bytes) return unset(); - clearHashCache(); - this.bytes = bytes; - this.offset = offset; - this.length = length; - return this; - } - - @Override - public int getOffset() { - return offset; - } - - @Override - public ByteRange setOffset(int offset) { - clearHashCache(); - this.offset = offset; - return this; - } - - @Override - public int getLength() { - return length; - } - - @Override - public ByteRange setLength(int length) { - clearHashCache(); - this.length = length; - return this; - } - - @Override - public boolean isEmpty() { - return isEmpty(this); - } - - /** - * @return true when {@code range} is of zero length, false otherwise. - */ - public static boolean isEmpty(ByteRange range) { - return range == null || range.getLength() == 0; + if (super.bytes != null) { + throw new ReadOnlyByteRangeException(); + } + return super.set(bytes, offset, length); } // // methods for retrieving data // - - @Override - public byte get(int index) { - return bytes[offset + index]; - } - - @Override - public short getShort(int index) { - int offset = this.offset + index; - short n = 0; - n ^= bytes[offset] & 0xFF; - n <<= 8; - n ^= bytes[offset + 1] & 0xFF; - return n; - } - - @Override - public int getInt(int index) { - int offset = this.offset + index; - int n = 0; - for (int i = offset; i < (offset + Bytes.SIZEOF_INT); i++) { - n <<= 8; - n ^= bytes[i] & 0xFF; - } - return n; - } - - @Override - public long getLong(int index) { - int offset = this.offset + index; - long l = 0; - for (int i = offset; i < offset + Bytes.SIZEOF_LONG; i++) { - l <<= 8; - l ^= bytes[i] & 0xFF; - } - return l; - } - - // Copied from com.google.protobuf.CodedInputStream - @Override - public long getVLong(int index) { - int shift = 0; - long result = 0; - while (shift < 64) { - final byte b = get(index++); - result |= (long) (b & 0x7F) << shift; - if ((b & 0x80) == 0) { - break; - } - shift += 7; - } - return result; - } - - public static int getVLongSize(long val) { - int rPos = 0; - while ((val & ~0x7F) != 0) { - val >>>= 7; - rPos++; - } - return rPos + 1; - } - - @Override - public ByteRange get(int index, byte[] dst) { - if (0 == dst.length) return this; - return get(index, dst, 0, dst.length); - } - - @Override - public ByteRange get(int index, byte[] dst, int offset, int length) { - if (0 == length) return this; - System.arraycopy(this.bytes, this.offset + index, dst, offset, length); - return this; - } - @Override public ByteRange put(int index, byte val) { - bytes[offset + index] = val; - clearHashCache(); - return this; - } - - @Override - public ByteRange putShort(int index, short val) { - // This writing is same as BB's putShort. When byte[] is wrapped in a BB and call putShort(), - // one can get the same result. - bytes[offset + index + 1] = (byte) val; - val >>= 8; - bytes[offset + index] = (byte) val; - clearHashCache(); - return this; - } - - @Override - public ByteRange putInt(int index, int val) { - // This writing is same as BB's putInt. When byte[] is wrapped in a BB and call getInt(), one - // can get the same result. - for (int i = Bytes.SIZEOF_INT - 1; i > 0; i--) { - bytes[offset + index + i] = (byte) val; - val >>>= 8; - } - bytes[offset + index] = (byte) val; - clearHashCache(); - return this; - } - - @Override - public ByteRange putLong(int index, long val) { - // This writing is same as BB's putLong. When byte[] is wrapped in a BB and call putLong(), one - // can get the same result. - for (int i = Bytes.SIZEOF_LONG - 1; i > 0; i--) { - bytes[offset + index + i] = (byte) val; - val >>>= 8; - } - bytes[offset + index] = (byte) val; - clearHashCache(); - return this; - } - - // Copied from com.google.protobuf.CodedOutputStream - @Override - public int putVLong(int index, long val) { - int rPos = 0; - while (true) { - if ((val & ~0x7F) == 0) { - bytes[offset + index + rPos] = (byte) val; - break; - } else { - bytes[offset + index + rPos] = (byte) ((val & 0x7F) | 0x80); - val >>>= 7; - } - rPos++; - } - clearHashCache(); - return rPos + 1; + throw new ReadOnlyByteRangeException(); } @Override public ByteRange put(int index, byte[] val) { - if (0 == val.length) return this; - return put(index, val, 0, val.length); + throw new ReadOnlyByteRangeException(); } @Override public ByteRange put(int index, byte[] val, int offset, int length) { - if (0 == length) return this; - System.arraycopy(val, offset, this.bytes, this.offset + index, length); - clearHashCache(); - return this; + throw new ReadOnlyByteRangeException(); } // // methods for duplicating the current instance // - @Override - public byte[] deepCopyToNewArray() { - byte[] result = new byte[length]; - System.arraycopy(bytes, offset, result, 0, length); - return result; - } - - @Override - public ByteRange deepCopy() { - SimpleByteRange clone = new SimpleByteRange(deepCopyToNewArray()); - if (isHashCached()) { - clone.hash = hash; - } - return clone; - } - - @Override - public void deepCopyTo(byte[] destination, int destinationOffset) { - System.arraycopy(bytes, offset, destination, destinationOffset, length); - } - - @Override - public void deepCopySubRangeTo(int innerOffset, int copyLength, byte[] destination, - int destinationOffset) { - System.arraycopy(bytes, offset + innerOffset, destination, destinationOffset, copyLength); - } - @Override public ByteRange shallowCopy() { SimpleByteRange clone = new SimpleByteRange(bytes, offset, length); @@ -365,20 +110,17 @@ public class SimpleByteRange implements ByteRange { } return clone; } - + @Override public ByteRange shallowCopySubRange(int innerOffset, int copyLength) { - SimpleByteRange clone = new SimpleByteRange(bytes, offset + innerOffset, copyLength); + SimpleByteRange clone = new SimpleByteRange(bytes, offset + innerOffset, + copyLength); if (isHashCached()) { clone.hash = hash; } return clone; } - - // - // methods used for comparison - // - + @Override public boolean equals(Object thatObject) { if (thatObject == null){ @@ -398,42 +140,31 @@ public class SimpleByteRange implements ByteRange { } @Override - public int hashCode() { - if (isHashCached()) {// hash is already calculated and cached - return hash; + public ByteRange deepCopy() { + SimpleByteRange clone = new SimpleByteRange(deepCopyToNewArray()); + if (isHashCached()) { + clone.hash = hash; } - if (this.isEmpty()) {// return 0 for empty ByteRange - hash = 0; - return hash; - } - int off = offset; - hash = 0; - for (int i = 0; i < length; i++) { - hash = 31 * hash + bytes[off++]; - } - return hash; - } - - private boolean isHashCached() { - return hash != UNSET_HASH_VALUE; - } - - protected void clearHashCache() { - hash = UNSET_HASH_VALUE; - } - - /** - * Bitwise comparison of each byte in the array. Unsigned comparison, not - * paying attention to java's signed bytes. - */ - @Override - public int compareTo(ByteRange other) { - return Bytes.compareTo(bytes, offset, length, other.getBytes(), other.getOffset(), - other.getLength()); + return clone; } @Override - public String toString() { - return Bytes.toStringBinary(bytes, offset, length); + public ByteRange putInt(int index, int val) { + throw new ReadOnlyByteRangeException(); + } + + @Override + public ByteRange putLong(int index, long val) { + throw new ReadOnlyByteRangeException(); + } + + @Override + public ByteRange putShort(int index, short val) { + throw new ReadOnlyByteRangeException(); + } + + @Override + public int putVLong(int index, long val) { + throw new ReadOnlyByteRangeException(); } } diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/SimpleMutableByteRange.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/SimpleMutableByteRange.java new file mode 100644 index 00000000000..65f01479cad --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/SimpleMutableByteRange.java @@ -0,0 +1,212 @@ +/* + * 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.util; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; + +/** + * A basic mutable {@link ByteRange} implementation. + */ +@InterfaceAudience.Public +@InterfaceStability.Evolving +public class SimpleMutableByteRange extends AbstractByteRange { + + /** + * Create a new {@code ByteRange} lacking a backing array and with an + * undefined viewport. + */ + public SimpleMutableByteRange() { + unset(); + } + + /** + * Create a new {@code ByteRange} over a new backing array of size + * {@code capacity}. The range's offset and length are 0 and {@code capacity}, + * respectively. + * + * @param capacity + * the size of the backing array. + */ + public SimpleMutableByteRange(int capacity) { + this(new byte[capacity]); + } + + /** + * Create a new {@code ByteRange} over the provided {@code bytes}. + * + * @param bytes + * The array to wrap. + */ + public SimpleMutableByteRange(byte[] bytes) { + set(bytes); + } + + /** + * Create a new {@code ByteRange} over the provided {@code bytes}. + * + * @param bytes + * The array to wrap. + * @param offset + * The offset into {@code bytes} considered the beginning of this + * range. + * @param length + * The length of this range. + */ + public SimpleMutableByteRange(byte[] bytes, int offset, int length) { + set(bytes, offset, length); + } + + @Override + public ByteRange unset() { + clearHashCache(); + bytes = null; + offset = 0; + length = 0; + return this; + } + + @Override + public ByteRange put(int index, byte val) { + bytes[offset + index] = val; + clearHashCache(); + return this; + } + + @Override + public ByteRange put(int index, byte[] val) { + if (0 == val.length) + return this; + return put(index, val, 0, val.length); + } + + @Override + public ByteRange put(int index, byte[] val, int offset, int length) { + if (0 == length) + return this; + System.arraycopy(val, offset, this.bytes, this.offset + index, length); + clearHashCache(); + return this; + } + + @Override + public ByteRange putShort(int index, short val) { + // This writing is same as BB's putShort. When byte[] is wrapped in a BB and + // call putShort(), + // one can get the same result. + bytes[offset + index + 1] = (byte) val; + val >>= 8; + bytes[offset + index] = (byte) val; + clearHashCache(); + return this; + } + + @Override + public ByteRange putInt(int index, int val) { + // This writing is same as BB's putInt. When byte[] is wrapped in a BB and + // call getInt(), one + // can get the same result. + for (int i = Bytes.SIZEOF_INT - 1; i > 0; i--) { + bytes[offset + index + i] = (byte) val; + val >>>= 8; + } + bytes[offset + index] = (byte) val; + clearHashCache(); + return this; + } + + @Override + public ByteRange putLong(int index, long val) { + // This writing is same as BB's putLong. When byte[] is wrapped in a BB and + // call putLong(), one + // can get the same result. + for (int i = Bytes.SIZEOF_LONG - 1; i > 0; i--) { + bytes[offset + index + i] = (byte) val; + val >>>= 8; + } + bytes[offset + index] = (byte) val; + clearHashCache(); + return this; + } + + // Copied from com.google.protobuf.CodedOutputStream + @Override + public int putVLong(int index, long val) { + int rPos = 0; + while (true) { + if ((val & ~0x7F) == 0) { + bytes[offset + index + rPos] = (byte) val; + break; + } else { + bytes[offset + index + rPos] = (byte) ((val & 0x7F) | 0x80); + val >>>= 7; + } + rPos++; + } + clearHashCache(); + return rPos + 1; + } + + @Override + public ByteRange deepCopy() { + SimpleMutableByteRange clone = new SimpleMutableByteRange(deepCopyToNewArray()); + if (isHashCached()) { + clone.hash = hash; + } + return clone; + } + + @Override + public ByteRange shallowCopy() { + SimpleMutableByteRange clone = new SimpleMutableByteRange(bytes, offset, length); + if (isHashCached()) { + clone.hash = hash; + } + return clone; + } + + @Override + public ByteRange shallowCopySubRange(int innerOffset, int copyLength) { + SimpleMutableByteRange clone = new SimpleMutableByteRange(bytes, offset + innerOffset, + copyLength); + if (isHashCached()) { + clone.hash = hash; + } + return clone; + } + + @Override + public boolean equals(Object thatObject) { + if (thatObject == null) { + return false; + } + if (this == thatObject) { + return true; + } + if (hashCode() != thatObject.hashCode()) { + return false; + } + if (!(thatObject instanceof SimpleMutableByteRange)) { + return false; + } + SimpleMutableByteRange that = (SimpleMutableByteRange) thatObject; + return Bytes.equals(bytes, offset, length, that.bytes, that.offset, that.length); + } + +} diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/SimplePositionedByteRange.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/SimplePositionedByteRange.java index c436b918761..2f7e5c3f5fc 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/SimplePositionedByteRange.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/SimplePositionedByteRange.java @@ -18,37 +18,20 @@ package org.apache.hadoop.hbase.util; -import java.io.IOException; -import java.nio.ByteBuffer; - import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceStability; -import com.google.common.annotations.VisibleForTesting; - /** - * Extends the basic {@link SimpleByteRange} implementation with position - * support. {@code position} is considered transient, not fundamental to the - * definition of the range, and does not participate in - * {@link #compareTo(ByteRange)}, {@link #hashCode()}, or + * Extends the basic {@link SimpleMutableByteRange} implementation with position + * support and it is a readonly version. {@code position} is considered + * transient, not fundamental to the definition of the range, and does not + * participate in {@link #compareTo(ByteRange)}, {@link #hashCode()}, or * {@link #equals(Object)}. {@code Position} is retained by copy operations. */ @InterfaceAudience.Public @InterfaceStability.Evolving @edu.umd.cs.findbugs.annotations.SuppressWarnings("EQ_DOESNT_OVERRIDE_EQUALS") -public class SimplePositionedByteRange extends SimpleByteRange implements PositionedByteRange { - - /** - * The current index into the range. Like {@link ByteBuffer} position, it - * points to the next value that will be read/written in the array. It - * provides the appearance of being 0-indexed, even though its value is - * calculated according to offset. - *

- * Position is considered transient and does not participate in - * {@link #equals(Object)} or {@link #hashCode()} comparisons. - *

- */ - private int position = 0; +public class SimplePositionedByteRange extends AbstractPositionedByteRange { /** * Create a new {@code PositionedByteRange} lacking a backing array and with @@ -65,7 +48,7 @@ public class SimplePositionedByteRange extends SimpleByteRange implements Positi * @param capacity the size of the backing array. */ public SimplePositionedByteRange(int capacity) { - super(capacity); + this(new byte[capacity]); } /** @@ -73,7 +56,7 @@ public class SimplePositionedByteRange extends SimpleByteRange implements Positi * @param bytes The array to wrap. */ public SimplePositionedByteRange(byte[] bytes) { - super(bytes); + set(bytes); } /** @@ -84,194 +67,68 @@ public class SimplePositionedByteRange extends SimpleByteRange implements Positi * @param length The length of this range. */ public SimplePositionedByteRange(byte[] bytes, int offset, int length) { - super(bytes, offset, length); - } - - @Override - public PositionedByteRange unset() { - this.position = 0; - super.unset(); - return this; + set(bytes, offset, length); } @Override public PositionedByteRange set(int capacity) { - this.position = 0; - super.set(capacity); - return this; + if (super.bytes != null) { + throw new ReadOnlyByteRangeException(); + } + return super.set(capacity); } @Override public PositionedByteRange set(byte[] bytes) { - this.position = 0; - super.set(bytes); - return this; + if (super.bytes != null) { + throw new ReadOnlyByteRangeException(); + } + return super.set(bytes); } @Override public PositionedByteRange set(byte[] bytes, int offset, int length) { - this.position = 0; - super.set(bytes, offset, length); - return this; - } - - /** - * Update the beginning of this range. {@code offset + length} may not be greater than - * {@code bytes.length}. Resets {@code position} to 0. - * @param offset the new start of this range. - * @return this. - */ - @Override - public PositionedByteRange setOffset(int offset) { - this.position = 0; - super.setOffset(offset); - return this; - } - - /** - * Update the length of this range. {@code offset + length} should not be - * greater than {@code bytes.length}. If {@code position} is greater than - * the new {@code length}, sets {@code position} to {@code length}. - * @param length The new length of this range. - * @return this. - */ - @Override - public PositionedByteRange setLength(int length) { - this.position = Math.min(position, length); - super.setLength(length); - return this; - } - - @Override - public int getPosition() { return position; } - - @Override - public PositionedByteRange setPosition(int position) { this.position = position; return this; } - - @Override - public int getRemaining() { return length - position; } - - @Override - public byte peek() { return bytes[offset + position]; } - - @Override - public byte get() { return get(position++); } - - @Override - public short getShort() { - short s = getShort(position); - position += Bytes.SIZEOF_SHORT; - return s; - } - - @Override - public int getInt() { - int i = getInt(position); - position += Bytes.SIZEOF_INT; - return i; - } - - @Override - public long getLong() { - long l = getLong(position); - position += Bytes.SIZEOF_LONG; - return l; - } - - @Override - public long getVLong() { - long p = getVLong(position); - position += getVLongSize(p); - return p; - } - - @Override - public PositionedByteRange get(byte[] dst) { - if (0 == dst.length) return this; - return this.get(dst, 0, dst.length); // be clear we're calling self, not super - } - - @Override - public PositionedByteRange get(byte[] dst, int offset, int length) { - if (0 == length) return this; - super.get(this.position, dst, offset, length); - this.position += length; - return this; + if (super.bytes != null) { + throw new ReadOnlyByteRangeException(); + } + return super.set(bytes, offset, length); } @Override public PositionedByteRange put(byte val) { - put(position++, val); - return this; + throw new ReadOnlyByteRangeException(); } @Override public PositionedByteRange putShort(short val) { - putShort(position, val); - position += Bytes.SIZEOF_SHORT; - return this; + throw new ReadOnlyByteRangeException(); } @Override public PositionedByteRange putInt(int val) { - putInt(position, val); - position += Bytes.SIZEOF_INT; - return this; + throw new ReadOnlyByteRangeException(); } @Override public PositionedByteRange putLong(long val) { - putLong(position, val); - position += Bytes.SIZEOF_LONG; - return this; + throw new ReadOnlyByteRangeException(); } @Override public int putVLong(long val) { - int len = putVLong(position, val); - position += len; - return len; + throw new ReadOnlyByteRangeException(); } @Override public PositionedByteRange put(byte[] val) { - if (0 == val.length) return this; - return this.put(val, 0, val.length); + throw new ReadOnlyByteRangeException(); } @Override public PositionedByteRange put(byte[] val, int offset, int length) { - if (0 == length) return this; - super.put(position, val, offset, length); - this.position += length; - return this; + throw new ReadOnlyByteRangeException(); } - /** - * Similar to {@link ByteBuffer#flip()}. Sets length to position, position - * to offset. - */ - @VisibleForTesting - PositionedByteRange flip() { - clearHashCache(); - length = position; - position = offset; - return this; - } - - /** - * Similar to {@link ByteBuffer#clear()}. Sets position to 0, length to - * capacity. - */ - @VisibleForTesting - PositionedByteRange clear() { - clearHashCache(); - position = 0; - length = bytes.length - offset; - return this; - } - - // java boilerplate @Override public PositionedByteRange get(int index, byte[] dst) { super.get(index, dst); return this; } @@ -283,33 +140,38 @@ public class SimplePositionedByteRange extends SimpleByteRange implements Positi } @Override - public PositionedByteRange put(int index, byte val) { super.put(index, val); return this; } + public PositionedByteRange put(int index, byte val) { + throw new ReadOnlyByteRangeException(); + } @Override public PositionedByteRange putShort(int index, short val) { - super.putShort(index, val); - return this; + throw new ReadOnlyByteRangeException(); } @Override public PositionedByteRange putInt(int index, int val) { - super.putInt(index, val); - return this; + throw new ReadOnlyByteRangeException(); + } + + @Override + public int putVLong(int index, long val) { + throw new ReadOnlyByteRangeException(); } @Override public PositionedByteRange putLong(int index, long val) { - super.putLong(index, val); - return this; + throw new ReadOnlyByteRangeException(); } @Override - public PositionedByteRange put(int index, byte[] val) { super.put(index, val); return this; } + public PositionedByteRange put(int index, byte[] val) { + throw new ReadOnlyByteRangeException(); + } @Override public PositionedByteRange put(int index, byte[] val, int offset, int length) { - super.put(index, val, offset, length); - return this; + throw new ReadOnlyByteRangeException(); } @Override @@ -333,4 +195,15 @@ public class SimplePositionedByteRange extends SimpleByteRange implements Positi clone.position = this.position; return clone; } + + @Override + public PositionedByteRange setLimit(int limit) { + throw new ReadOnlyByteRangeException(); + } + + @Override + public PositionedByteRange unset() { + throw new ReadOnlyByteRangeException(); + } + } diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/SimplePositionedMutableByteRange.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/SimplePositionedMutableByteRange.java new file mode 100644 index 00000000000..cb4ae86f089 --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/SimplePositionedMutableByteRange.java @@ -0,0 +1,310 @@ +/* + * 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.util; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; + +/** + * Extends the basic {@link AbstractPositionedByteRange} implementation with + * position support and it is a mutable version. {@code position} is considered transient, + * not fundamental to the definition of the range, and does not participate in + * {@link #compareTo(ByteRange)}, {@link #hashCode()}, or + * {@link #equals(Object)}. {@code Position} is retained by copy operations. + */ +@InterfaceAudience.Public +@InterfaceStability.Evolving +@edu.umd.cs.findbugs.annotations.SuppressWarnings("EQ_DOESNT_OVERRIDE_EQUALS") +public class SimplePositionedMutableByteRange extends AbstractPositionedByteRange { + /** + * Create a new {@code PositionedByteRange} lacking a backing array and with + * an undefined viewport. + */ + public SimplePositionedMutableByteRange() { + super(); + } + + /** + * Create a new {@code PositionedByteRange} over a new backing array of size + * {@code capacity}. The range's offset and length are 0 and {@code capacity}, + * respectively. + * + * @param capacity + * the size of the backing array. + */ + public SimplePositionedMutableByteRange(int capacity) { + this(new byte[capacity]); + } + + /** + * Create a new {@code PositionedByteRange} over the provided {@code bytes}. + * + * @param bytes + * The array to wrap. + */ + public SimplePositionedMutableByteRange(byte[] bytes) { + set(bytes); + } + + /** + * Create a new {@code PositionedByteRange} over the provided {@code bytes}. + * + * @param bytes + * The array to wrap. + * @param offset + * The offset into {@code bytes} considered the beginning of this + * range. + * @param length + * The length of this range. + */ + public SimplePositionedMutableByteRange(byte[] bytes, int offset, int length) { + set(bytes, offset, length); + } + + @Override + public PositionedByteRange unset() { + this.position = 0; + clearHashCache(); + bytes = null; + offset = 0; + length = 0; + return this; + } + + @Override + public PositionedByteRange set(int capacity) { + this.position = 0; + super.set(capacity); + this.limit = capacity; + return this; + } + + @Override + public PositionedByteRange set(byte[] bytes) { + this.position = 0; + super.set(bytes); + this.limit = bytes.length; + return this; + } + + @Override + public PositionedByteRange set(byte[] bytes, int offset, int length) { + this.position = 0; + super.set(bytes, offset, length); + limit = length; + return this; + } + + /** + * Update the beginning of this range. {@code offset + length} may not be + * greater than {@code bytes.length}. Resets {@code position} to 0. + * + * @param offset + * the new start of this range. + * @return this. + */ + @Override + public PositionedByteRange setOffset(int offset) { + this.position = 0; + super.setOffset(offset); + return this; + } + + /** + * Update the length of this range. {@code offset + length} should not be + * greater than {@code bytes.length}. If {@code position} is greater than the + * new {@code length}, sets {@code position} to {@code length}. + * + * @param length + * The new length of this range. + * @return this. + */ + @Override + public PositionedByteRange setLength(int length) { + this.position = Math.min(position, length); + super.setLength(length); + return this; + } + + @Override + public PositionedByteRange put(byte val) { + put(position++, val); + return this; + } + + @Override + public PositionedByteRange put(byte[] val) { + if (0 == val.length) + return this; + return this.put(val, 0, val.length); + } + + @Override + public PositionedByteRange put(byte[] val, int offset, int length) { + if (0 == length) + return this; + put(position, val, offset, length); + this.position += length; + return this; + } + + @Override + public PositionedByteRange get(int index, byte[] dst) { + super.get(index, dst); + return this; + } + + @Override + public PositionedByteRange get(int index, byte[] dst, int offset, int length) { + super.get(index, dst, offset, length); + return this; + } + + @Override + public PositionedByteRange put(int index, byte val) { + bytes[offset + index] = val; + return this; + } + + @Override + public PositionedByteRange put(int index, byte[] val) { + if (0 == val.length) + return this; + return put(index, val, 0, val.length); + } + + @Override + public PositionedByteRange put(int index, byte[] val, int offset, int length) { + if (0 == length) + return this; + System.arraycopy(val, offset, this.bytes, this.offset + index, length); + return this; + } + + @Override + public PositionedByteRange deepCopy() { + SimplePositionedMutableByteRange clone = new SimplePositionedMutableByteRange( + deepCopyToNewArray()); + clone.position = this.position; + return clone; + } + + @Override + public PositionedByteRange shallowCopy() { + SimplePositionedMutableByteRange clone = new SimplePositionedMutableByteRange(bytes, offset, + length); + clone.position = this.position; + return clone; + } + + @Override + public PositionedByteRange shallowCopySubRange(int innerOffset, int copyLength) { + SimplePositionedMutableByteRange clone = new SimplePositionedMutableByteRange(bytes, offset + + innerOffset, copyLength); + clone.position = this.position; + return clone; + } + + @Override + public PositionedByteRange putShort(short val) { + putShort(position, val); + position += Bytes.SIZEOF_SHORT; + return this; + } + + @Override + public PositionedByteRange putInt(int val) { + putInt(position, val); + position += Bytes.SIZEOF_INT; + return this; + } + + @Override + public PositionedByteRange putLong(long val) { + putLong(position, val); + position += Bytes.SIZEOF_LONG; + return this; + } + + @Override + public int putVLong(long val) { + int len = putVLong(position, val); + position += len; + return len; + } + + @Override + public PositionedByteRange putShort(int index, short val) { + // This writing is same as BB's putShort. When byte[] is wrapped in a BB and + // call putShort(), + // one can get the same result. + bytes[offset + index + 1] = (byte) val; + val >>= 8; + bytes[offset + index] = (byte) val; + clearHashCache(); + return this; + } + + @Override + public PositionedByteRange putInt(int index, int val) { + // This writing is same as BB's putInt. When byte[] is wrapped in a BB and + // call getInt(), one + // can get the same result. + for (int i = Bytes.SIZEOF_INT - 1; i > 0; i--) { + bytes[offset + index + i] = (byte) val; + val >>>= 8; + } + bytes[offset + index] = (byte) val; + clearHashCache(); + return this; + } + + @Override + public PositionedByteRange putLong(int index, long val) { + // This writing is same as BB's putLong. When byte[] is wrapped in a BB and + // call putLong(), one + // can get the same result. + for (int i = Bytes.SIZEOF_LONG - 1; i > 0; i--) { + bytes[offset + index + i] = (byte) val; + val >>>= 8; + } + bytes[offset + index] = (byte) val; + clearHashCache(); + return this; + } + + // Copied from com.google.protobuf.CodedOutputStream + @Override + public int putVLong(int index, long val) { + int rPos = 0; + while (true) { + if ((val & ~0x7F) == 0) { + bytes[offset + index + rPos] = (byte) val; + break; + } else { + bytes[offset + index + rPos] = (byte) ((val & 0x7F) | 0x80); + val >>>= 7; + } + rPos++; + } + clearHashCache(); + return rPos + 1; + } + +} 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 index ddc7354a285..d9227c3ef8b 100644 --- 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 @@ -24,7 +24,7 @@ import org.apache.hadoop.hbase.SmallTests; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.Order; import org.apache.hadoop.hbase.util.PositionedByteRange; -import org.apache.hadoop.hbase.util.SimplePositionedByteRange; +import org.apache.hadoop.hbase.util.SimplePositionedMutableByteRange; import org.junit.Test; import org.junit.experimental.categories.Category; @@ -46,7 +46,7 @@ public class TestFixedLengthWrapper { @Test public void testReadWrite() { for (int limit : limits) { - PositionedByteRange buff = new SimplePositionedByteRange(limit); + PositionedByteRange buff = new SimplePositionedMutableByteRange(limit); for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) { for (byte[] val : VALUES) { buff.setPosition(0); @@ -65,21 +65,21 @@ public class TestFixedLengthWrapper { @Test(expected = IllegalArgumentException.class) public void testInsufficientRemainingRead() { - PositionedByteRange buff = new SimplePositionedByteRange(0); + PositionedByteRange buff = new SimplePositionedMutableByteRange(0); DataType type = new FixedLengthWrapper(new RawBytes(), 3); type.decode(buff); } @Test(expected = IllegalArgumentException.class) public void testInsufficientRemainingWrite() { - PositionedByteRange buff = new SimplePositionedByteRange(0); + PositionedByteRange buff = new SimplePositionedMutableByteRange(0); DataType type = new FixedLengthWrapper(new RawBytes(), 3); type.encode(buff, Bytes.toBytes("")); } @Test(expected = IllegalArgumentException.class) public void testOverflowPassthrough() { - PositionedByteRange buff = new SimplePositionedByteRange(3); + PositionedByteRange buff = new SimplePositionedMutableByteRange(3); DataType type = new FixedLengthWrapper(new RawBytes(), 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 index 412bb2d99f2..d24f4d4c39f 100644 --- 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 @@ -22,7 +22,7 @@ import static org.junit.Assert.assertEquals; import org.apache.hadoop.hbase.SmallTests; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.PositionedByteRange; -import org.apache.hadoop.hbase.util.SimplePositionedByteRange; +import org.apache.hadoop.hbase.util.SimplePositionedMutableByteRange; import org.junit.Test; import org.junit.experimental.categories.Category; @@ -37,7 +37,7 @@ public class TestOrderedBlob { @Test public void testEncodedLength() { - PositionedByteRange buff = new SimplePositionedByteRange(20); + PositionedByteRange buff = new SimplePositionedMutableByteRange(20); for (DataType type : new OrderedBlob[] { OrderedBlob.ASCENDING, OrderedBlob.DESCENDING }) { for (byte[] val : VALUES) { buff.setPosition(0); 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 index 18c6a66728d..4e88552b184 100644 --- 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 @@ -22,7 +22,7 @@ import static org.junit.Assert.assertEquals; import org.apache.hadoop.hbase.SmallTests; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.PositionedByteRange; -import org.apache.hadoop.hbase.util.SimplePositionedByteRange; +import org.apache.hadoop.hbase.util.SimplePositionedMutableByteRange; import org.junit.Test; import org.junit.experimental.categories.Category; @@ -37,7 +37,7 @@ public class TestOrderedBlobVar { @Test public void testEncodedLength() { - PositionedByteRange buff = new SimplePositionedByteRange(20); + PositionedByteRange buff = new SimplePositionedMutableByteRange(20); for (DataType type : new OrderedBlobVar[] { OrderedBlobVar.ASCENDING, OrderedBlobVar.DESCENDING }) { for (byte[] val : VALUES) { 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 index 68c6f55e9b6..8306f0000f2 100644 --- 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 @@ -21,7 +21,7 @@ import static org.junit.Assert.assertEquals; import org.apache.hadoop.hbase.SmallTests; import org.apache.hadoop.hbase.util.PositionedByteRange; -import org.apache.hadoop.hbase.util.SimplePositionedByteRange; +import org.apache.hadoop.hbase.util.SimplePositionedMutableByteRange; import org.junit.Test; import org.junit.experimental.categories.Category; @@ -34,7 +34,7 @@ public class TestOrderedString { @Test public void testEncodedLength() { - PositionedByteRange buff = new SimplePositionedByteRange(20); + PositionedByteRange buff = new SimplePositionedMutableByteRange(20); for (DataType type : new OrderedString[] { OrderedString.ASCENDING, OrderedString.DESCENDING }) { for (String val : VALUES) { buff.setPosition(0); diff --git a/hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestRawString.java b/hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestRawString.java index dc00f2cc5f3..b022229d274 100644 --- a/hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestRawString.java +++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestRawString.java @@ -24,7 +24,7 @@ import org.apache.hadoop.hbase.SmallTests; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.Order; import org.apache.hadoop.hbase.util.PositionedByteRange; -import org.apache.hadoop.hbase.util.SimplePositionedByteRange; +import org.apache.hadoop.hbase.util.SimplePositionedMutableByteRange; import org.junit.Test; import org.junit.experimental.categories.Category; @@ -41,7 +41,7 @@ public class TestRawString { RawString type = Order.ASCENDING == ord ? RawString.ASCENDING : RawString.DESCENDING; for (String val : VALUES) { - PositionedByteRange buff = new SimplePositionedByteRange(Bytes.toBytes(val).length); + PositionedByteRange buff = new SimplePositionedMutableByteRange(Bytes.toBytes(val).length); assertEquals(buff.getLength(), type.encode(buff, val)); byte[] expected = Bytes.toBytes(val); ord.apply(expected); 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 index f679035bd71..2b79eeba812 100644 --- 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 @@ -29,7 +29,7 @@ import org.apache.hadoop.hbase.SmallTests; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.Order; import org.apache.hadoop.hbase.util.PositionedByteRange; -import org.apache.hadoop.hbase.util.SimplePositionedByteRange; +import org.apache.hadoop.hbase.util.SimplePositionedMutableByteRange; import org.junit.Test; import org.junit.experimental.categories.Category; import org.junit.runner.RunWith; @@ -350,8 +350,10 @@ public class TestStruct { 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 SimplePositionedByteRange(generic.encodedLength(constructorArgs[i])); - encodedSpecialized[i] = new SimplePositionedByteRange(specialized.encodedLength(vals[i])); + encodedGeneric[i] = new SimplePositionedMutableByteRange( + generic.encodedLength(constructorArgs[i])); + encodedSpecialized[i] = new SimplePositionedMutableByteRange( + specialized.encodedLength(vals[i])); } // populate our arrays diff --git a/hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestStructNullExtension.java b/hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestStructNullExtension.java index 7fa70233205..8e457937ed8 100644 --- a/hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestStructNullExtension.java +++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/types/TestStructNullExtension.java @@ -26,7 +26,7 @@ import java.util.Arrays; import org.apache.hadoop.hbase.SmallTests; import org.apache.hadoop.hbase.util.PositionedByteRange; -import org.apache.hadoop.hbase.util.SimplePositionedByteRange; +import org.apache.hadoop.hbase.util.SimplePositionedMutableByteRange; import org.junit.Test; import org.junit.experimental.categories.Category; @@ -41,7 +41,7 @@ public class TestStructNullExtension { Struct s = new StructBuilder() .add(new RawStringTerminated("|")) // not nullable .toStruct(); - PositionedByteRange buf = new SimplePositionedByteRange(4); + PositionedByteRange buf = new SimplePositionedMutableByteRange(4); s.encode(buf, new Object[1]); } @@ -61,7 +61,7 @@ public class TestStructNullExtension { .add(OrderedNumeric.ASCENDING) .toStruct(); - PositionedByteRange buf1 = new SimplePositionedByteRange(7); + PositionedByteRange buf1 = new SimplePositionedMutableByteRange(7); Object[] val1 = new Object[] { BigDecimal.ONE, "foo" }; // => 2 bytes + 5 bytes assertEquals("Encoding shorter value wrote a surprising number of bytes.", buf1.getLength(), shorter.encode(buf1, val1)); @@ -93,7 +93,7 @@ public class TestStructNullExtension { Arrays.copyOf(val1, 4), longer.decode(buf1)); // test omission of trailing members - PositionedByteRange buf2 = new SimplePositionedByteRange(7); + PositionedByteRange buf2 = new SimplePositionedMutableByteRange(7); buf1.setPosition(0); assertEquals( "Encoding a short value with extended struct should have same result as using short struct.", 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 index 6cccb03e417..6bb331add20 100644 --- 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 @@ -24,7 +24,7 @@ import org.apache.hadoop.hbase.SmallTests; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.Order; import org.apache.hadoop.hbase.util.PositionedByteRange; -import org.apache.hadoop.hbase.util.SimplePositionedByteRange; +import org.apache.hadoop.hbase.util.SimplePositionedMutableByteRange; import org.junit.Test; import org.junit.experimental.categories.Category; @@ -58,13 +58,13 @@ public class TestTerminatedWrapper { @Test(expected = IllegalArgumentException.class) public void testEncodedValueContainsTerm() { DataType type = new TerminatedWrapper(new RawBytes(), "foo"); - PositionedByteRange buff = new SimplePositionedByteRange(16); + PositionedByteRange buff = new SimplePositionedMutableByteRange(16); type.encode(buff, Bytes.toBytes("hello foobar!")); } @Test public void testReadWriteSkippable() { - PositionedByteRange buff = new SimplePositionedByteRange(14); + PositionedByteRange buff = new SimplePositionedMutableByteRange(14); for (OrderedString t : new OrderedString[] { OrderedString.ASCENDING, OrderedString.DESCENDING }) { @@ -83,7 +83,7 @@ public class TestTerminatedWrapper { @Test public void testReadWriteNonSkippable() { - PositionedByteRange buff = new SimplePositionedByteRange(12); + PositionedByteRange buff = new SimplePositionedMutableByteRange(12); for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) { for (byte[] term : TERMINATORS) { for (byte[] val : VALUES_BYTES) { @@ -100,7 +100,7 @@ public class TestTerminatedWrapper { @Test public void testSkipSkippable() { - PositionedByteRange buff = new SimplePositionedByteRange(14); + PositionedByteRange buff = new SimplePositionedMutableByteRange(14); for (OrderedString t : new OrderedString[] { OrderedString.ASCENDING, OrderedString.DESCENDING }) { @@ -120,7 +120,7 @@ public class TestTerminatedWrapper { @Test public void testSkipNonSkippable() { - PositionedByteRange buff = new SimplePositionedByteRange(12); + PositionedByteRange buff = new SimplePositionedMutableByteRange(12); for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) { for (byte[] term : TERMINATORS) { for (byte[] val : VALUES_BYTES) { @@ -137,7 +137,7 @@ public class TestTerminatedWrapper { @Test(expected = IllegalArgumentException.class) public void testInvalidSkip() { - PositionedByteRange buff = new SimplePositionedByteRange(Bytes.toBytes("foo")); + PositionedByteRange buff = new SimplePositionedMutableByteRange(Bytes.toBytes("foo")); DataType type = new TerminatedWrapper(new RawBytes(), 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 index 6396193db6d..ff1699cc2d4 100644 --- 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 @@ -23,7 +23,7 @@ import static org.junit.Assert.assertTrue; import org.apache.hadoop.hbase.SmallTests; import org.apache.hadoop.hbase.util.Order; import org.apache.hadoop.hbase.util.PositionedByteRange; -import org.apache.hadoop.hbase.util.SimplePositionedByteRange; +import org.apache.hadoop.hbase.util.SimplePositionedMutableByteRange; import org.junit.Test; import org.junit.experimental.categories.Category; @@ -109,7 +109,7 @@ public class TestUnion2 { public void testEncodeDecode() { Integer intVal = Integer.valueOf(10); String strVal = "hello"; - PositionedByteRange buff = new SimplePositionedByteRange(10); + PositionedByteRange buff = new SimplePositionedMutableByteRange(10); SampleUnion1 type = new SampleUnion1(); type.encode(buff, intVal); @@ -125,7 +125,7 @@ public class TestUnion2 { public void testSkip() { Integer intVal = Integer.valueOf(10); String strVal = "hello"; - PositionedByteRange buff = new SimplePositionedByteRange(10); + PositionedByteRange buff = new SimplePositionedMutableByteRange(10); SampleUnion1 type = new SampleUnion1(); int len = type.encode(buff, intVal); diff --git a/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestByteRangeWithKVSerialization.java b/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestByteRangeWithKVSerialization.java index 9319fdee0a4..e2af9665626 100644 --- a/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestByteRangeWithKVSerialization.java +++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestByteRangeWithKVSerialization.java @@ -69,12 +69,13 @@ public class TestByteRangeWithKVSerialization { kvs.add(kv); totalSize += kv.getLength() + Bytes.SIZEOF_LONG; } - PositionedByteRange pbr = new SimplePositionedByteRange(totalSize); + PositionedByteRange pbr = new SimplePositionedMutableByteRange(totalSize); for (KeyValue kv : kvs) { writeCell(pbr, kv); } - PositionedByteRange pbr1 = new SimplePositionedByteRange(pbr.getBytes(), 0, pbr.getPosition()); + PositionedByteRange pbr1 = new SimplePositionedMutableByteRange(pbr.getBytes(), 0, + pbr.getPosition()); for (int i = 0; i < kvCount; i++) { KeyValue kv = readCell(pbr1); KeyValue kv1 = kvs.get(i); 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 aef24be3403..c65ba101fd3 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 @@ -125,7 +125,7 @@ public class TestOrderedBytes { for (int i = 0; i < vals.length; i++) { // allocate a buffer 2-bytes larger than necessary and place our range over the center. byte[] a = new byte[lens[i] + 2]; - PositionedByteRange buf = new SimplePositionedByteRange(a, 1, lens[i]); + PositionedByteRange buf = new SimplePositionedMutableByteRange(a, 1, lens[i]); // verify encode assertEquals("Surprising return value.", @@ -162,7 +162,7 @@ public class TestOrderedBytes { for (int i = 0; i < I_VALS.length; i++) { // allocate a buffer 3-bytes larger than necessary to detect over/underflow byte[] a = new byte[I_LENGTHS[i] + 3]; - PositionedByteRange buf1 = new SimplePositionedByteRange(a, 1, I_LENGTHS[i] + 1); + PositionedByteRange buf1 = new SimplePositionedMutableByteRange(a, 1, I_LENGTHS[i] + 1); buf1.setPosition(1); // verify encode @@ -193,7 +193,7 @@ public class TestOrderedBytes { */ for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) { byte[][] encoded = new byte[I_VALS.length][]; - PositionedByteRange pbr = new SimplePositionedByteRange(); + PositionedByteRange pbr = new SimplePositionedMutableByteRange(); for (int i = 0; i < I_VALS.length; i++) { encoded[i] = new byte[I_LENGTHS[i]]; OrderedBytes.encodeNumeric(pbr.set(encoded[i]), I_VALS[i], ord); @@ -229,7 +229,7 @@ public class TestOrderedBytes { for (int i = 0; i < D_VALS.length; i++) { // allocate a buffer 3-bytes larger than necessary to detect over/underflow byte[] a = new byte[D_LENGTHS[i] + 3]; - PositionedByteRange buf1 = new SimplePositionedByteRange(a, 1, D_LENGTHS[i] + 1); + PositionedByteRange buf1 = new SimplePositionedMutableByteRange(a, 1, D_LENGTHS[i] + 1); buf1.setPosition(1); // verify encode @@ -260,7 +260,7 @@ public class TestOrderedBytes { */ for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) { byte[][] encoded = new byte[D_VALS.length][]; - PositionedByteRange pbr = new SimplePositionedByteRange(); + PositionedByteRange pbr = new SimplePositionedMutableByteRange(); for (int i = 0; i < D_VALS.length; i++) { encoded[i] = new byte[D_LENGTHS[i]]; OrderedBytes.encodeNumeric(pbr.set(encoded[i]), D_VALS[i], ord); @@ -296,7 +296,7 @@ public class TestOrderedBytes { for (int i = 0; i < BD_VALS.length; i++) { // allocate a buffer 3-bytes larger than necessary to detect over/underflow byte[] a = new byte[BD_LENGTHS[i] + 3]; - PositionedByteRange buf1 = new SimplePositionedByteRange(a, 1, BD_LENGTHS[i] + 1); + PositionedByteRange buf1 = new SimplePositionedMutableByteRange(a, 1, BD_LENGTHS[i] + 1); buf1.setPosition(1); // verify encode @@ -335,8 +335,8 @@ public class TestOrderedBytes { for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) { for (int i = 0; i < I_VALS.length; i++) { // verify primitives - PositionedByteRange pbri = new SimplePositionedByteRange(I_LENGTHS[i]); - PositionedByteRange pbrr = new SimplePositionedByteRange(I_LENGTHS[i]); + PositionedByteRange pbri = new SimplePositionedMutableByteRange(I_LENGTHS[i]); + PositionedByteRange pbrr = new SimplePositionedMutableByteRange(I_LENGTHS[i]); OrderedBytes.encodeNumeric(pbri, I_VALS[i], ord); OrderedBytes.encodeNumeric(pbrr, I_VALS[i], ord); assertArrayEquals("Integer and real encodings differ.", pbri.getBytes(), pbrr.getBytes()); @@ -347,7 +347,7 @@ public class TestOrderedBytes { // verify BigDecimal for Real encoding BigDecimal bd = BigDecimal.valueOf(I_VALS[i]); - PositionedByteRange pbrbd = new SimplePositionedByteRange(I_LENGTHS[i]); + PositionedByteRange pbrbd = new SimplePositionedMutableByteRange(I_LENGTHS[i]); OrderedBytes.encodeNumeric(pbrbd, bd, ord); assertArrayEquals("Integer and BigDecimal encodings differ.", pbri.getBytes(), pbrbd.getBytes()); @@ -374,7 +374,7 @@ public class TestOrderedBytes { for (int i = 0; i < vals.length; i++) { // allocate a buffer 3-bytes larger than necessary to detect over/underflow byte[] a = new byte[2 + 3]; - PositionedByteRange buf1 = new SimplePositionedByteRange(a, 1, 2 + 1); + PositionedByteRange buf1 = new SimplePositionedMutableByteRange(a, 1, 2 + 1); buf1.setPosition(1); // verify encode @@ -405,7 +405,7 @@ public class TestOrderedBytes { */ for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) { byte[][] encoded = new byte[vals.length][2]; - PositionedByteRange pbr = new SimplePositionedByteRange(); + PositionedByteRange pbr = new SimplePositionedMutableByteRange(); for (int i = 0; i < vals.length; i++) { OrderedBytes.encodeInt8(pbr.set(encoded[i]), vals[i], ord); } @@ -442,7 +442,7 @@ public class TestOrderedBytes { for (int i = 0; i < vals.length; i++) { // allocate a buffer 3-bytes larger than necessary to detect over/underflow byte[] a = new byte[3 + 3]; - PositionedByteRange buf1 = new SimplePositionedByteRange(a, 1, 3 + 1); + PositionedByteRange buf1 = new SimplePositionedMutableByteRange(a, 1, 3 + 1); buf1.setPosition(1); // verify encode @@ -473,7 +473,7 @@ public class TestOrderedBytes { */ for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) { byte[][] encoded = new byte[vals.length][3]; - PositionedByteRange pbr = new SimplePositionedByteRange(); + PositionedByteRange pbr = new SimplePositionedMutableByteRange(); for (int i = 0; i < vals.length; i++) { OrderedBytes.encodeInt16(pbr.set(encoded[i]), vals[i], ord); } @@ -510,7 +510,7 @@ public class TestOrderedBytes { for (int i = 0; i < vals.length; i++) { // allocate a buffer 3-bytes larger than necessary to detect over/underflow byte[] a = new byte[5 + 3]; - PositionedByteRange buf1 = new SimplePositionedByteRange(a, 1, 5 + 1); + PositionedByteRange buf1 = new SimplePositionedMutableByteRange(a, 1, 5 + 1); buf1.setPosition(1); // verify encode @@ -541,7 +541,7 @@ public class TestOrderedBytes { */ for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) { byte[][] encoded = new byte[vals.length][5]; - PositionedByteRange pbr = new SimplePositionedByteRange(); + PositionedByteRange pbr = new SimplePositionedMutableByteRange(); for (int i = 0; i < vals.length; i++) { OrderedBytes.encodeInt32(pbr.set(encoded[i]), vals[i], ord); } @@ -577,7 +577,7 @@ public class TestOrderedBytes { for (int i = 0; i < vals.length; i++) { // allocate a buffer 3-bytes larger than necessary to detect over/underflow byte[] a = new byte[9 + 3]; - PositionedByteRange buf1 = new SimplePositionedByteRange(a, 1, 9 + 1); + PositionedByteRange buf1 = new SimplePositionedMutableByteRange(a, 1, 9 + 1); buf1.setPosition(1); // verify encode @@ -608,7 +608,7 @@ public class TestOrderedBytes { */ for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) { byte[][] encoded = new byte[vals.length][9]; - PositionedByteRange pbr = new SimplePositionedByteRange(); + PositionedByteRange pbr = new SimplePositionedMutableByteRange(); for (int i = 0; i < vals.length; i++) { OrderedBytes.encodeInt64(pbr.set(encoded[i]), vals[i], ord); } @@ -645,7 +645,7 @@ public class TestOrderedBytes { for (int i = 0; i < vals.length; i++) { // allocate a buffer 3-bytes larger than necessary to detect over/underflow byte[] a = new byte[5 + 3]; - PositionedByteRange buf1 = new SimplePositionedByteRange(a, 1, 5 + 1); + PositionedByteRange buf1 = new SimplePositionedMutableByteRange(a, 1, 5 + 1); buf1.setPosition(1); // verify encode @@ -677,7 +677,7 @@ public class TestOrderedBytes { */ for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) { byte[][] encoded = new byte[vals.length][5]; - PositionedByteRange pbr = new SimplePositionedByteRange(); + PositionedByteRange pbr = new SimplePositionedMutableByteRange(); for (int i = 0; i < vals.length; i++) { OrderedBytes.encodeFloat32(pbr.set(encoded[i]), vals[i], ord); } @@ -715,7 +715,7 @@ public class TestOrderedBytes { for (int i = 0; i < vals.length; i++) { // allocate a buffer 3-bytes larger than necessary to detect over/underflow byte[] a = new byte[9 + 3]; - PositionedByteRange buf1 = new SimplePositionedByteRange(a, 1, 9 + 1); + PositionedByteRange buf1 = new SimplePositionedMutableByteRange(a, 1, 9 + 1); buf1.setPosition(1); // verify encode @@ -747,7 +747,7 @@ public class TestOrderedBytes { */ for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) { byte[][] encoded = new byte[vals.length][9]; - PositionedByteRange pbr = new SimplePositionedByteRange(); + PositionedByteRange pbr = new SimplePositionedMutableByteRange(); for (int i = 0; i < vals.length; i++) { OrderedBytes.encodeFloat64(pbr.set(encoded[i]), vals[i], ord); } @@ -785,7 +785,8 @@ public class TestOrderedBytes { for (int i = 0; i < vals.length; i++) { // allocate a buffer 3-bytes larger than necessary to detect over/underflow byte[] a = new byte[expectedLengths[i] + 3]; - PositionedByteRange buf1 = new SimplePositionedByteRange(a, 1, expectedLengths[i] + 1); + PositionedByteRange buf1 = new SimplePositionedMutableByteRange(a, 1, + expectedLengths[i] + 1); buf1.setPosition(1); // verify encode @@ -815,7 +816,7 @@ public class TestOrderedBytes { */ for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) { byte[][] encoded = new byte[vals.length][]; - PositionedByteRange pbr = new SimplePositionedByteRange(); + PositionedByteRange pbr = new SimplePositionedMutableByteRange(); for (int i = 0; i < vals.length; i++) { encoded[i] = new byte[expectedLengths[i]]; OrderedBytes.encodeString(pbr.set(encoded[i]), vals[i], ord); @@ -840,7 +841,7 @@ public class TestOrderedBytes { @Test(expected = IllegalArgumentException.class) public void testStringNoNullChars() { - PositionedByteRange buff = new SimplePositionedByteRange(3); + PositionedByteRange buff = new SimplePositionedMutableByteRange(3); OrderedBytes.encodeString(buff, "\u0000", Order.ASCENDING); } @@ -898,7 +899,7 @@ public class TestOrderedBytes { // allocate a buffer 3-bytes larger than necessary to detect over/underflow int expectedLen = OrderedBytes.blobVarEncodedLength(val.length); byte[] a = new byte[expectedLen + 3]; - PositionedByteRange buf1 = new SimplePositionedByteRange(a, 1, expectedLen + 1); + PositionedByteRange buf1 = new SimplePositionedMutableByteRange(a, 1, expectedLen + 1); buf1.setPosition(1); // verify encode @@ -928,7 +929,7 @@ public class TestOrderedBytes { */ for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) { byte[][] encoded = new byte[vals.length][]; - PositionedByteRange pbr = new SimplePositionedByteRange(); + PositionedByteRange pbr = new SimplePositionedMutableByteRange(); for (int i = 0; i < vals.length; i++) { encoded[i] = new byte[OrderedBytes.blobVarEncodedLength(vals[i].length)]; OrderedBytes.encodeBlobVar(pbr.set(encoded[i]), vals[i], ord); @@ -973,7 +974,7 @@ public class TestOrderedBytes { // allocate a buffer 3-bytes larger than necessary to detect over/underflow int expectedLen = val.length + (Order.ASCENDING == ord ? 1 : 2); byte[] a = new byte[expectedLen + 3]; - PositionedByteRange buf1 = new SimplePositionedByteRange(a, 1, expectedLen + 1); + PositionedByteRange buf1 = new SimplePositionedMutableByteRange(a, 1, expectedLen + 1); buf1.setPosition(1); // verify encode @@ -1003,7 +1004,7 @@ public class TestOrderedBytes { */ for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) { byte[][] encoded = new byte[vals.length][]; - PositionedByteRange pbr = new SimplePositionedByteRange(); + PositionedByteRange pbr = new SimplePositionedMutableByteRange(); for (int i = 0; i < vals.length; i++) { encoded[i] = new byte[vals[i].length + (Order.ASCENDING == ord ? 1 : 2)]; OrderedBytes.encodeBlobCopy(pbr.set(encoded[i]), vals[i], ord); @@ -1031,7 +1032,7 @@ public class TestOrderedBytes { for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) { byte[] a = new byte[3 + (Order.ASCENDING == ord ? 1 : 2) + 2]; PositionedByteRange buf = - new SimplePositionedByteRange(a, 1, 3 + (Order.ASCENDING == ord ? 1 : 2)); + new SimplePositionedMutableByteRange(a, 1, 3 + (Order.ASCENDING == ord ? 1 : 2)); OrderedBytes.encodeBlobCopy(buf, "foobarbaz".getBytes(), 3, 3, ord); buf.setPosition(0); assertArrayEquals("bar".getBytes(), OrderedBytes.decodeBlobCopy(buf)); @@ -1046,7 +1047,7 @@ public class TestOrderedBytes { byte[] val = { 0x01, 0x02, 0x00, 0x03 }; // TODO: implementation detail leaked here. byte[] ascExpected = { 0x38, 0x01, 0x02, 0x00, 0x03 }; - PositionedByteRange buf = new SimplePositionedByteRange(val.length + 1); + PositionedByteRange buf = new SimplePositionedMutableByteRange(val.length + 1); OrderedBytes.encodeBlobCopy(buf, val, Order.ASCENDING); assertArrayEquals(ascExpected, buf.getBytes()); buf.set(val.length + 2); @@ -1081,7 +1082,7 @@ public class TestOrderedBytes { byte[] blobCopy = Bytes.toBytes("bar"); for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) { - PositionedByteRange buff = new SimplePositionedByteRange(30); + PositionedByteRange buff = new SimplePositionedMutableByteRange(30); int o; o = OrderedBytes.encodeNull(buff, ord); buff.setPosition(0); diff --git a/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestSimpleByteRange.java b/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestSimpleMutableByteRange.java similarity index 88% rename from hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestSimpleByteRange.java rename to hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestSimpleMutableByteRange.java index 1bd09694781..43949f8ccff 100644 --- a/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestSimpleByteRange.java +++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestSimpleMutableByteRange.java @@ -23,27 +23,27 @@ import org.junit.Test; import org.junit.experimental.categories.Category; @Category(SmallTests.class) -public class TestSimpleByteRange { +public class TestSimpleMutableByteRange { @Test public void testEmpty(){ - Assert.assertTrue(SimpleByteRange.isEmpty(null)); - ByteRange r = new SimpleByteRange(); - Assert.assertTrue(SimpleByteRange.isEmpty(r)); + Assert.assertTrue(SimpleMutableByteRange.isEmpty(null)); + ByteRange r = new SimpleMutableByteRange(); + Assert.assertTrue(SimpleMutableByteRange.isEmpty(r)); Assert.assertTrue(r.isEmpty()); r.set(new byte[0]); Assert.assertEquals(0, r.getBytes().length); Assert.assertEquals(0, r.getOffset()); Assert.assertEquals(0, r.getLength()); Assert.assertTrue(Bytes.equals(new byte[0], r.deepCopyToNewArray())); - Assert.assertEquals(0, r.compareTo(new SimpleByteRange(new byte[0], 0, 0))); + Assert.assertEquals(0, r.compareTo(new SimpleMutableByteRange(new byte[0], 0, 0))); Assert.assertEquals(0, r.hashCode()); } @Test public void testBasics() { - ByteRange r = new SimpleByteRange(new byte[] { 1, 3, 2 }); - Assert.assertFalse(SimpleByteRange.isEmpty(r)); + ByteRange r = new SimpleMutableByteRange(new byte[] { 1, 3, 2 }); + Assert.assertFalse(SimpleMutableByteRange.isEmpty(r)); Assert.assertNotNull(r.getBytes());//should be empty byte[], but could change this behavior Assert.assertEquals(3, r.getBytes().length); Assert.assertEquals(0, r.getOffset()); @@ -68,10 +68,10 @@ public class TestSimpleByteRange { r.setLength(2);//verify we retained the 2nd byte, but dangerous in real code Assert.assertTrue(Bytes.equals(new byte[]{1, 3}, r.deepCopyToNewArray())); } - + @Test public void testPutandGetPrimitiveTypes() throws Exception { - ByteRange r = new SimpleByteRange(100); + ByteRange r = new SimpleMutableByteRange(100); int offset = 0; int i1 = 18, i2 = 2; short s1 = 0; diff --git a/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestPositionedByteRange.java b/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestSimplePositionedMutableByteRange.java similarity index 93% rename from hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestPositionedByteRange.java rename to hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestSimplePositionedMutableByteRange.java index ab43ef3681b..b097b36e747 100644 --- a/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestPositionedByteRange.java +++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestSimplePositionedMutableByteRange.java @@ -25,10 +25,10 @@ import org.junit.Test; import org.junit.experimental.categories.Category; @Category(SmallTests.class) -public class TestPositionedByteRange { +public class TestSimplePositionedMutableByteRange { @Test public void testPosition() { - PositionedByteRange r = new SimplePositionedByteRange(new byte[5], 1, 3); + PositionedByteRange r = new SimplePositionedMutableByteRange(new byte[5], 1, 3); // exercise single-byte put r.put(Bytes.toBytes("f")[0]) @@ -70,7 +70,7 @@ public class TestPositionedByteRange { @Test public void testPutAndGetPrimitiveTypes() throws Exception { - PositionedByteRange pbr = new SimplePositionedByteRange(100); + PositionedByteRange pbr = new SimplePositionedMutableByteRange(100); int i1 = 18, i2 = 2; short s1 = 0; long l1 = 1234L; @@ -97,7 +97,7 @@ public class TestPositionedByteRange { @Test public void testPutGetAPIsCompareWithBBAPIs() throws Exception { // confirm that the long/int/short writing is same as BBs - PositionedByteRange pbr = new SimplePositionedByteRange(100); + PositionedByteRange pbr = new SimplePositionedMutableByteRange(100); int i1 = -234, i2 = 2; short s1 = 0; long l1 = 1234L; diff --git a/hbase-prefix-tree/src/main/java/org/apache/hadoop/hbase/codec/prefixtree/decode/row/RowNodeReader.java b/hbase-prefix-tree/src/main/java/org/apache/hadoop/hbase/codec/prefixtree/decode/row/RowNodeReader.java index 638f4fa1c34..ab13441cfc7 100644 --- a/hbase-prefix-tree/src/main/java/org/apache/hadoop/hbase/codec/prefixtree/decode/row/RowNodeReader.java +++ b/hbase-prefix-tree/src/main/java/org/apache/hadoop/hbase/codec/prefixtree/decode/row/RowNodeReader.java @@ -21,7 +21,7 @@ package org.apache.hadoop.hbase.codec.prefixtree.decode.row; import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.hbase.codec.prefixtree.PrefixTreeBlockMeta; import org.apache.hadoop.hbase.util.Bytes; -import org.apache.hadoop.hbase.util.SimpleByteRange; +import org.apache.hadoop.hbase.util.SimpleMutableByteRange; import org.apache.hadoop.hbase.util.vint.UFIntTool; import org.apache.hadoop.hbase.util.vint.UVIntTool; @@ -216,7 +216,7 @@ public class RowNodeReader { public byte[] getToken() { // TODO pass in reusable ByteRange - return new SimpleByteRange(block, tokenOffset, tokenLength).deepCopyToNewArray(); + return new SimpleMutableByteRange(block, tokenOffset, tokenLength).deepCopyToNewArray(); } public int getOffset() { diff --git a/hbase-prefix-tree/src/main/java/org/apache/hadoop/hbase/codec/prefixtree/encode/PrefixTreeEncoder.java b/hbase-prefix-tree/src/main/java/org/apache/hadoop/hbase/codec/prefixtree/encode/PrefixTreeEncoder.java index ca3c8041593..290968c8428 100644 --- a/hbase-prefix-tree/src/main/java/org/apache/hadoop/hbase/codec/prefixtree/encode/PrefixTreeEncoder.java +++ b/hbase-prefix-tree/src/main/java/org/apache/hadoop/hbase/codec/prefixtree/encode/PrefixTreeEncoder.java @@ -37,7 +37,7 @@ import org.apache.hadoop.hbase.codec.prefixtree.encode.tokenize.Tokenizer; import org.apache.hadoop.hbase.io.CellOutputStream; import org.apache.hadoop.hbase.util.ArrayUtils; import org.apache.hadoop.hbase.util.ByteRange; -import org.apache.hadoop.hbase.util.SimpleByteRange; +import org.apache.hadoop.hbase.util.SimpleMutableByteRange; import org.apache.hadoop.hbase.util.byterange.ByteRangeSet; import org.apache.hadoop.hbase.util.byterange.impl.ByteRangeHashSet; import org.apache.hadoop.hbase.util.byterange.impl.ByteRangeTreeSet; @@ -154,9 +154,9 @@ public class PrefixTreeEncoder implements CellOutputStream { public PrefixTreeEncoder(OutputStream outputStream, boolean includeMvccVersion) { // used during cell accumulation this.blockMeta = new PrefixTreeBlockMeta(); - this.rowRange = new SimpleByteRange(); - this.familyRange = new SimpleByteRange(); - this.qualifierRange = new SimpleByteRange(); + this.rowRange = new SimpleMutableByteRange(); + this.familyRange = new SimpleMutableByteRange(); + this.qualifierRange = new SimpleMutableByteRange(); this.timestamps = new long[INITIAL_PER_CELL_ARRAY_SIZES]; this.mvccVersions = new long[INITIAL_PER_CELL_ARRAY_SIZES]; this.typeBytes = new byte[INITIAL_PER_CELL_ARRAY_SIZES]; @@ -210,7 +210,7 @@ public class PrefixTreeEncoder implements CellOutputStream { } protected void initializeTagHelpers() { - this.tagsRange = new SimpleByteRange(); + this.tagsRange = new SimpleMutableByteRange(); this.tagsDeduplicator = USE_HASH_COLUMN_SORTER ? new ByteRangeHashSet() : new ByteRangeTreeSet(); this.tagsTokenizer = new Tokenizer(); @@ -534,7 +534,7 @@ public class PrefixTreeEncoder implements CellOutputStream { } public ByteRange getValueByteRange() { - return new SimpleByteRange(values, 0, totalValueBytes); + return new SimpleMutableByteRange(values, 0, totalValueBytes); } } diff --git a/hbase-prefix-tree/src/main/java/org/apache/hadoop/hbase/codec/prefixtree/encode/tokenize/TokenizerNode.java b/hbase-prefix-tree/src/main/java/org/apache/hadoop/hbase/codec/prefixtree/encode/tokenize/TokenizerNode.java index fc78cd860fd..1ef1b922019 100644 --- a/hbase-prefix-tree/src/main/java/org/apache/hadoop/hbase/codec/prefixtree/encode/tokenize/TokenizerNode.java +++ b/hbase-prefix-tree/src/main/java/org/apache/hadoop/hbase/codec/prefixtree/encode/tokenize/TokenizerNode.java @@ -26,7 +26,7 @@ import org.apache.hadoop.hbase.util.ByteRange; import org.apache.hadoop.hbase.util.ByteRangeUtils; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.CollectionUtils; -import org.apache.hadoop.hbase.util.SimpleByteRange; +import org.apache.hadoop.hbase.util.SimpleMutableByteRange; import org.apache.hadoop.hbase.util.Strings; import com.google.common.collect.Lists; @@ -137,7 +137,7 @@ public class TokenizerNode{ public TokenizerNode(Tokenizer builder, TokenizerNode parent, int nodeDepth, int tokenStartOffset, int tokenOffset, int tokenLength) { - this.token = new SimpleByteRange(); + this.token = new SimpleMutableByteRange(); reconstruct(builder, parent, nodeDepth, tokenStartOffset, tokenOffset, tokenLength); this.children = Lists.newArrayList(); } diff --git a/hbase-prefix-tree/src/main/java/org/apache/hadoop/hbase/util/byterange/ByteRangeSet.java b/hbase-prefix-tree/src/main/java/org/apache/hadoop/hbase/util/byterange/ByteRangeSet.java index db6fc5180db..f2de0e20fde 100644 --- a/hbase-prefix-tree/src/main/java/org/apache/hadoop/hbase/util/byterange/ByteRangeSet.java +++ b/hbase-prefix-tree/src/main/java/org/apache/hadoop/hbase/util/byterange/ByteRangeSet.java @@ -26,7 +26,7 @@ import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.hbase.util.ArrayUtils; import org.apache.hadoop.hbase.util.ByteRange; import org.apache.hadoop.hbase.util.Bytes; -import org.apache.hadoop.hbase.util.SimpleByteRange; +import org.apache.hadoop.hbase.util.SimpleMutableByteRange; import com.google.common.collect.Lists; @@ -106,7 +106,7 @@ public abstract class ByteRangeSet { protected int store(ByteRange bytes) { int indexOfNewElement = numUniqueRanges; if (uniqueRanges.size() <= numUniqueRanges) { - uniqueRanges.add(new SimpleByteRange()); + uniqueRanges.add(new SimpleMutableByteRange()); } ByteRange storedRange = uniqueRanges.get(numUniqueRanges); int neededBytes = numBytes + bytes.getLength(); diff --git a/hbase-prefix-tree/src/test/java/org/apache/hadoop/hbase/codec/prefixtree/builder/TestTokenizer.java b/hbase-prefix-tree/src/test/java/org/apache/hadoop/hbase/codec/prefixtree/builder/TestTokenizer.java index 8c53cae48bc..dc32d6fcc54 100644 --- a/hbase-prefix-tree/src/test/java/org/apache/hadoop/hbase/codec/prefixtree/builder/TestTokenizer.java +++ b/hbase-prefix-tree/src/test/java/org/apache/hadoop/hbase/codec/prefixtree/builder/TestTokenizer.java @@ -25,7 +25,7 @@ import org.apache.hadoop.hbase.SmallTests; import org.apache.hadoop.hbase.codec.prefixtree.encode.tokenize.Tokenizer; import org.apache.hadoop.hbase.codec.prefixtree.encode.tokenize.TokenizerNode; import org.apache.hadoop.hbase.codec.prefixtree.encode.tokenize.TokenizerRowSearchResult; -import org.apache.hadoop.hbase.util.SimpleByteRange; +import org.apache.hadoop.hbase.util.SimpleMutableByteRange; import org.apache.hadoop.hbase.util.Bytes; import org.junit.Assert; import org.junit.Test; @@ -51,7 +51,7 @@ public class TestTokenizer { this.inputs = sortedByteArrays.getInputs(); this.builder = new Tokenizer(); for (byte[] array : inputs) { - builder.addSorted(new SimpleByteRange(array)); + builder.addSorted(new SimpleMutableByteRange(array)); } this.roundTripped = builder.getArrays(); } diff --git a/hbase-prefix-tree/src/test/java/org/apache/hadoop/hbase/codec/prefixtree/builder/TestTreeDepth.java b/hbase-prefix-tree/src/test/java/org/apache/hadoop/hbase/codec/prefixtree/builder/TestTreeDepth.java index c5a9c7b275c..542b0e550d8 100644 --- a/hbase-prefix-tree/src/test/java/org/apache/hadoop/hbase/codec/prefixtree/builder/TestTreeDepth.java +++ b/hbase-prefix-tree/src/test/java/org/apache/hadoop/hbase/codec/prefixtree/builder/TestTreeDepth.java @@ -23,7 +23,7 @@ import java.util.List; import org.apache.hadoop.hbase.SmallTests; import org.apache.hadoop.hbase.codec.prefixtree.encode.tokenize.Tokenizer; -import org.apache.hadoop.hbase.util.SimpleByteRange; +import org.apache.hadoop.hbase.util.SimpleMutableByteRange; import org.apache.hadoop.hbase.util.Bytes; import org.junit.Assert; import org.junit.Test; @@ -79,7 +79,7 @@ public class TestTreeDepth { protected void testInternal(List inputs, int expectedTreeDepth) { Tokenizer builder = new Tokenizer(); for (String s : inputs) { - SimpleByteRange b = new SimpleByteRange(Bytes.toBytes(s)); + SimpleMutableByteRange b = new SimpleMutableByteRange(Bytes.toBytes(s)); builder.addSorted(b); } Assert.assertEquals(1, builder.getRoot().getNodeDepth()); diff --git a/hbase-prefix-tree/src/test/java/org/apache/hadoop/hbase/codec/prefixtree/column/data/TestColumnDataRandom.java b/hbase-prefix-tree/src/test/java/org/apache/hadoop/hbase/codec/prefixtree/column/data/TestColumnDataRandom.java index 2c14a78726e..ad08a296fe3 100644 --- a/hbase-prefix-tree/src/test/java/org/apache/hadoop/hbase/codec/prefixtree/column/data/TestColumnDataRandom.java +++ b/hbase-prefix-tree/src/test/java/org/apache/hadoop/hbase/codec/prefixtree/column/data/TestColumnDataRandom.java @@ -23,7 +23,7 @@ import java.util.List; import org.apache.hadoop.hbase.KeyValue; import org.apache.hadoop.hbase.codec.prefixtree.column.TestColumnData; import org.apache.hadoop.hbase.util.ByteRange; -import org.apache.hadoop.hbase.util.SimpleByteRange; +import org.apache.hadoop.hbase.util.SimpleMutableByteRange; import org.apache.hadoop.hbase.util.byterange.ByteRangeSet; import org.apache.hadoop.hbase.util.byterange.impl.ByteRangeTreeSet; import org.apache.hadoop.hbase.util.test.RedundantKVGenerator; @@ -40,7 +40,7 @@ public class TestColumnDataRandom implements TestColumnData { ByteRangeSet sortedColumns = new ByteRangeTreeSet(); List d = generator.generateTestKeyValues(numColumns); for (KeyValue col : d) { - ByteRange colRange = new SimpleByteRange(col.getQualifier()); + ByteRange colRange = new SimpleMutableByteRange(col.getQualifier()); inputs.add(colRange); sortedColumns.add(colRange); } diff --git a/hbase-prefix-tree/src/test/java/org/apache/hadoop/hbase/codec/prefixtree/row/data/TestRowDataExerciseFInts.java b/hbase-prefix-tree/src/test/java/org/apache/hadoop/hbase/codec/prefixtree/row/data/TestRowDataExerciseFInts.java index b03eae122c7..184f5376f4f 100644 --- a/hbase-prefix-tree/src/test/java/org/apache/hadoop/hbase/codec/prefixtree/row/data/TestRowDataExerciseFInts.java +++ b/hbase-prefix-tree/src/test/java/org/apache/hadoop/hbase/codec/prefixtree/row/data/TestRowDataExerciseFInts.java @@ -27,7 +27,7 @@ import org.apache.hadoop.hbase.codec.prefixtree.PrefixTreeTestConstants; import org.apache.hadoop.hbase.codec.prefixtree.row.BaseTestRowData; import org.apache.hadoop.hbase.util.ByteRange; import org.apache.hadoop.hbase.util.Bytes; -import org.apache.hadoop.hbase.util.SimpleByteRange; +import org.apache.hadoop.hbase.util.SimpleMutableByteRange; import org.apache.hadoop.hbase.util.byterange.impl.ByteRangeTreeSet; import org.junit.Assert; @@ -62,7 +62,7 @@ public class TestRowDataExerciseFInts extends BaseTestRowData{ rowStrings.add("com.isabellasBlog/directoryBb/pageHhh"); ByteRangeTreeSet ba = new ByteRangeTreeSet(); for(String row : rowStrings){ - ba.add(new SimpleByteRange(Bytes.toBytes(row))); + ba.add(new SimpleMutableByteRange(Bytes.toBytes(row))); } rows = ba.compile().getSortedRanges(); } diff --git a/hbase-prefix-tree/src/test/java/org/apache/hadoop/hbase/codec/prefixtree/row/data/TestRowDataUrls.java b/hbase-prefix-tree/src/test/java/org/apache/hadoop/hbase/codec/prefixtree/row/data/TestRowDataUrls.java index e534c89db4d..d0801c19f43 100644 --- a/hbase-prefix-tree/src/test/java/org/apache/hadoop/hbase/codec/prefixtree/row/data/TestRowDataUrls.java +++ b/hbase-prefix-tree/src/test/java/org/apache/hadoop/hbase/codec/prefixtree/row/data/TestRowDataUrls.java @@ -26,7 +26,7 @@ import org.apache.hadoop.hbase.codec.prefixtree.PrefixTreeTestConstants; import org.apache.hadoop.hbase.codec.prefixtree.row.BaseTestRowData; import org.apache.hadoop.hbase.util.ByteRange; import org.apache.hadoop.hbase.util.Bytes; -import org.apache.hadoop.hbase.util.SimpleByteRange; +import org.apache.hadoop.hbase.util.SimpleMutableByteRange; import org.apache.hadoop.hbase.util.byterange.impl.ByteRangeTreeSet; import com.google.common.collect.Lists; @@ -60,7 +60,7 @@ public class TestRowDataUrls extends BaseTestRowData{ rowStrings.add("com.isabellasBlog/directoryBb/pageHhh"); ByteRangeTreeSet ba = new ByteRangeTreeSet(); for (String row : rowStrings) { - ba.add(new SimpleByteRange(Bytes.toBytes(row))); + ba.add(new SimpleMutableByteRange(Bytes.toBytes(row))); } rows = ba.compile().getSortedRanges(); } diff --git a/hbase-prefix-tree/src/test/java/org/apache/hadoop/hbase/util/bytes/TestByteRange.java b/hbase-prefix-tree/src/test/java/org/apache/hadoop/hbase/util/bytes/TestByteRange.java index 46a00af0498..11057e00972 100644 --- a/hbase-prefix-tree/src/test/java/org/apache/hadoop/hbase/util/bytes/TestByteRange.java +++ b/hbase-prefix-tree/src/test/java/org/apache/hadoop/hbase/util/bytes/TestByteRange.java @@ -22,7 +22,7 @@ import junit.framework.Assert; import org.apache.hadoop.hbase.SmallTests; import org.apache.hadoop.hbase.util.ByteRange; -import org.apache.hadoop.hbase.util.SimpleByteRange; +import org.apache.hadoop.hbase.util.SimpleMutableByteRange; import org.junit.Test; import org.junit.experimental.categories.Category; @@ -31,7 +31,7 @@ public class TestByteRange { @Test public void testConstructor() { - ByteRange b = new SimpleByteRange(new byte[] { 0, 1, 2 }); + ByteRange b = new SimpleMutableByteRange(new byte[] { 0, 1, 2 }); Assert.assertEquals(3, b.getLength()); } diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HeapMemStoreLAB.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HeapMemStoreLAB.java index f4301f22660..b4836b03415 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HeapMemStoreLAB.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HeapMemStoreLAB.java @@ -27,7 +27,7 @@ import java.util.concurrent.atomic.AtomicReference; import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.util.ByteRange; -import org.apache.hadoop.hbase.util.SimpleByteRange; +import org.apache.hadoop.hbase.util.SimpleMutableByteRange; import com.google.common.base.Preconditions; @@ -117,7 +117,7 @@ public class HeapMemStoreLAB implements MemStoreLAB { if (allocOffset != -1) { // We succeeded - this is the common case - small alloc // from a big buffer - return new SimpleByteRange(c.data, allocOffset, size); + return new SimpleMutableByteRange(c.data, allocOffset, size); } // not enough space! diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/security/access/AccessControlFilter.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/security/access/AccessControlFilter.java index bec10305bb3..f750cc85841 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/security/access/AccessControlFilter.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/security/access/AccessControlFilter.java @@ -29,7 +29,7 @@ import org.apache.hadoop.hbase.filter.FilterBase; import org.apache.hadoop.hbase.security.User; import org.apache.hadoop.hbase.util.ByteRange; import org.apache.hadoop.hbase.util.Bytes; -import org.apache.hadoop.hbase.util.SimpleByteRange; +import org.apache.hadoop.hbase.util.SimpleMutableByteRange; /** * NOTE: for internal use only by AccessController implementation @@ -82,8 +82,8 @@ class AccessControlFilter extends FilterBase { isSystemTable = tableName.isSystemTable(); this.strategy = strategy; this.cfVsMaxVersions = cfVsMaxVersions; - this.prevFam = new SimpleByteRange(); - this.prevQual = new SimpleByteRange(); + this.prevFam = new SimpleMutableByteRange(); + this.prevQual = new SimpleMutableByteRange(); } @Override diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/security/access/AccessController.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/security/access/AccessController.java index 0474884b3a1..c1f90b77318 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/security/access/AccessController.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/security/access/AccessController.java @@ -98,7 +98,7 @@ import org.apache.hadoop.hbase.util.ByteRange; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; import org.apache.hadoop.hbase.util.Pair; -import org.apache.hadoop.hbase.util.SimpleByteRange; +import org.apache.hadoop.hbase.util.SimpleMutableByteRange; import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher; import com.google.common.collect.ArrayListMultimap; @@ -669,13 +669,13 @@ public class AccessController extends BaseRegionObserver Map> familyMap1 = new HashMap>(); for (Entry> entry : familyMap.entrySet()) { if (entry.getValue() instanceof List) { - familyMap1.put(new SimpleByteRange(entry.getKey()), (List) entry.getValue()); + familyMap1.put(new SimpleMutableByteRange(entry.getKey()), (List) entry.getValue()); } } RegionScanner scanner = getRegion(e).getScanner(new Scan(get)); List cells = Lists.newArrayList(); Cell prevCell = null; - ByteRange curFam = new SimpleByteRange(); + ByteRange curFam = new SimpleMutableByteRange(); boolean curColAllVersions = (request == OpType.DELETE); long curColCheckTs = opTs; boolean foundColumn = false; @@ -773,7 +773,6 @@ public class AccessController extends BaseRegionObserver e.setValue(newCells); } } - /* ---- MasterObserver implementation ---- */ public void start(CoprocessorEnvironment env) throws IOException { @@ -1360,7 +1359,7 @@ public class AccessController extends BaseRegionObserver TableName table = getTableName(region); Map cfVsMaxVersions = Maps.newHashMap(); for (HColumnDescriptor hcd : region.getTableDesc().getFamilies()) { - cfVsMaxVersions.put(new SimpleByteRange(hcd.getName()), hcd.getMaxVersions()); + cfVsMaxVersions.put(new SimpleMutableByteRange(hcd.getName()), hcd.getMaxVersions()); } if (!authResult.isAllowed()) { if (!cellFeaturesEnabled || compatibleEarlyTermination) { diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/security/visibility/VisibilityController.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/security/visibility/VisibilityController.java index a152fcc0e4d..c7e0ccb084f 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/security/visibility/VisibilityController.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/security/visibility/VisibilityController.java @@ -110,7 +110,7 @@ import org.apache.hadoop.hbase.security.visibility.expression.Operator; import org.apache.hadoop.hbase.util.ByteRange; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.Pair; -import org.apache.hadoop.hbase.util.SimpleByteRange; +import org.apache.hadoop.hbase.util.SimpleMutableByteRange; import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher; import com.google.common.collect.Lists; @@ -1016,7 +1016,7 @@ public class VisibilityController extends BaseRegionObserver implements MasterOb throws IOException { Map cfVsMaxVersions = new HashMap(); for (HColumnDescriptor hcd : region.getTableDesc().getFamilies()) { - cfVsMaxVersions.put(new SimpleByteRange(hcd.getName()), hcd.getMaxVersions()); + cfVsMaxVersions.put(new SimpleMutableByteRange(hcd.getName()), hcd.getMaxVersions()); } if (authorizations == null) { // No Authorizations present for this scan/Get! diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/security/visibility/VisibilityLabelFilter.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/security/visibility/VisibilityLabelFilter.java index 0ff8d67e856..14d69ede066 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/security/visibility/VisibilityLabelFilter.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/security/visibility/VisibilityLabelFilter.java @@ -31,7 +31,7 @@ import org.apache.hadoop.hbase.io.util.StreamUtils; import org.apache.hadoop.hbase.util.ByteRange; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.Pair; -import org.apache.hadoop.hbase.util.SimpleByteRange; +import org.apache.hadoop.hbase.util.SimpleMutableByteRange; /** * This Filter checks the visibility expression with each KV against visibility labels associated @@ -50,8 +50,8 @@ class VisibilityLabelFilter extends FilterBase { public VisibilityLabelFilter(BitSet authLabels, Map cfVsMaxVersions) { this.authLabels = authLabels; this.cfVsMaxVersions = cfVsMaxVersions; - this.curFamily = new SimpleByteRange(); - this.curQualifier = new SimpleByteRange(); + this.curFamily = new SimpleMutableByteRange(); + this.curQualifier = new SimpleMutableByteRange(); } @Override