Index: hbase-common/src/main/java/org/apache/hadoop/hbase/util/ByteRange.java =================================================================== --- hbase-common/src/main/java/org/apache/hadoop/hbase/util/ByteRange.java (revision 1585685) +++ hbase-common/src/main/java/org/apache/hadoop/hbase/util/ByteRange.java (working copy) @@ -243,4 +243,12 @@ * @return new {@code ByteRange} object referencing this range's byte[]. */ public ByteRange shallowCopySubRange(int innerOffset, int copyLength); + + /** + * Returns a copy of the ByteRange but the position is reset to initial + * position + * + * @return + */ + public ByteRange asReadOnlyByteRange(); } \ No newline at end of file Index: hbase-common/src/main/java/org/apache/hadoop/hbase/util/PositionedByteRange.java =================================================================== --- hbase-common/src/main/java/org/apache/hadoop/hbase/util/PositionedByteRange.java (revision 1585685) +++ hbase-common/src/main/java/org/apache/hadoop/hbase/util/PositionedByteRange.java (working copy) @@ -115,6 +115,29 @@ */ 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(); + + /** + * Helpful in clearing the bytes allocated off heap + * + * @throws Exception + */ + public void clean() throws Exception; + // override parent interface declarations to return this interface. @Override Index: hbase-common/src/main/java/org/apache/hadoop/hbase/util/ReadOnlyByteRangeException.java =================================================================== --- hbase-common/src/main/java/org/apache/hadoop/hbase/util/ReadOnlyByteRangeException.java (revision 0) +++ hbase-common/src/main/java/org/apache/hadoop/hbase/util/ReadOnlyByteRangeException.java (working copy) @@ -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() { + + } + +} Index: hbase-common/src/main/java/org/apache/hadoop/hbase/util/SimpleByteRange.java =================================================================== --- hbase-common/src/main/java/org/apache/hadoop/hbase/util/SimpleByteRange.java (revision 1585685) +++ hbase-common/src/main/java/org/apache/hadoop/hbase/util/SimpleByteRange.java (working copy) @@ -53,7 +53,7 @@ * 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; + protected int hash = UNSET_HASH_VALUE; /** * Create a new {@code ByteRange} lacking a backing array and with an @@ -300,7 +300,7 @@ return hash; } - private boolean isHashCached() { + protected boolean isHashCached() { return hash != UNSET_HASH_VALUE; } @@ -322,4 +322,11 @@ public String toString() { return Bytes.toStringBinary(bytes, offset, length); } + + @Override + public ByteRange asReadOnlyByteRange() { + SimplePositionedImmutableByteRange clone = new SimplePositionedImmutableByteRange(this.bytes, + this.offset, this.length); + return clone; + } } Index: hbase-common/src/main/java/org/apache/hadoop/hbase/util/SimpleImmutableByteRange.java =================================================================== --- hbase-common/src/main/java/org/apache/hadoop/hbase/util/SimpleImmutableByteRange.java (revision 0) +++ hbase-common/src/main/java/org/apache/hadoop/hbase/util/SimpleImmutableByteRange.java (working copy) @@ -0,0 +1,121 @@ +/* + * 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; + +/** + * A read only version of the {@link SimpleByteRange}. + */ +public class SimpleImmutableByteRange extends SimpleByteRange { + public SimpleImmutableByteRange() { + } + + public SimpleImmutableByteRange(int capacity) { + this(new byte[capacity]); + } + + /** + * Create a new {@code ByteRange} over the provided {@code bytes}. + * @param bytes The array to wrap. + */ + public SimpleImmutableByteRange(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 SimpleImmutableByteRange(byte[] bytes, int offset, int length) { + set(bytes, offset, length); + } + + @Override + public ByteRange set(byte[] bytes) { + if (super.bytes != null) { + throw new ReadOnlyByteRangeException(); + } + return super.set(bytes); + } + + @Override + public ByteRange set(byte[] bytes, int offset, int length) { + if (super.bytes != null) { + throw new ReadOnlyByteRangeException(); + } + return super.set(bytes, offset, length); + } + + @Override + public ByteRange set(int capacity) { + if (super.bytes != null) { + throw new ReadOnlyByteRangeException(); + } + return super.set(capacity); + } + + @Override + public ByteRange put(int index, byte val) { + throw new ReadOnlyByteRangeException(); + } + + @Override + public ByteRange put(int index, byte[] val) { + throw new ReadOnlyByteRangeException(); + } + + @Override + public ByteRange put(int index, byte[] val, int offset, int length) { + throw new ReadOnlyByteRangeException(); + } + + @Override + public ByteRange unset() { + throw new ReadOnlyByteRangeException(); + } + + @Override + public ByteRange deepCopy() { + SimpleImmutableByteRange clone = new SimpleImmutableByteRange(deepCopyToNewArray()); + if (isHashCached()) { + clone.hash = hash; + } + return clone; + } + + @Override + public ByteRange shallowCopy() { + SimpleImmutableByteRange clone = new SimpleImmutableByteRange(bytes, offset, length); + if (isHashCached()) { + clone.hash = hash; + } + return clone; + } + + @Override + public ByteRange shallowCopySubRange(int innerOffset, int copyLength) { + SimpleImmutableByteRange clone = new SimpleImmutableByteRange(bytes, offset + innerOffset, + copyLength); + if (isHashCached()) { + clone.hash = hash; + } + return clone; + } +} Index: hbase-common/src/main/java/org/apache/hadoop/hbase/util/SimplePositionedByteRange.java =================================================================== --- hbase-common/src/main/java/org/apache/hadoop/hbase/util/SimplePositionedByteRange.java (revision 1585685) +++ hbase-common/src/main/java/org/apache/hadoop/hbase/util/SimplePositionedByteRange.java (working copy) @@ -47,8 +47,10 @@ * {@link #equals(Object)} or {@link #hashCode()} comparisons. *

*/ - private int position = 0; + protected int position = 0; + protected int limit = 0; + /** * Create a new {@code PositionedByteRange} lacking a backing array and with * an undefined viewport. @@ -258,4 +260,29 @@ clone.position = this.position; return clone; } + + @Override + public PositionedByteRange asReadOnlyByteRange() { + SimplePositionedImmutableByteRange clone = new SimplePositionedImmutableByteRange(bytes, + offset, length); + clone.setPosition(0); + clone.setLimit(this.limit); + return clone; + } + + @Override + public PositionedByteRange setLimit(int limit) { + this.limit = limit; + return this; + } + + @Override + public int getLimit() { + return this.limit; + } + + @Override + public void clean() throws Exception { + // Do nothing here + } } Index: hbase-common/src/main/java/org/apache/hadoop/hbase/util/SimplePositionedImmutableByteRange.java =================================================================== --- hbase-common/src/main/java/org/apache/hadoop/hbase/util/SimplePositionedImmutableByteRange.java (revision 0) +++ hbase-common/src/main/java/org/apache/hadoop/hbase/util/SimplePositionedImmutableByteRange.java (working copy) @@ -0,0 +1,160 @@ +/* + * 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; + +/** + * + * A read only version of the {@link SimplePositionedByteRange} + * + */ +public class SimplePositionedImmutableByteRange extends SimplePositionedByteRange { + + /** + * Create a new {@code PositionedByteRange} lacking a backing array and with + * an undefined viewport. + */ + public SimplePositionedImmutableByteRange() { + 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 SimplePositionedImmutableByteRange(int capacity) { + super(capacity); + } + + /** + * Create a new {@code PositionedByteRange} over the provided {@code bytes}. + * + * @param bytes + * The array to wrap. + */ + public SimplePositionedImmutableByteRange(byte[] bytes) { + super(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 SimplePositionedImmutableByteRange(byte[] bytes, int offset, int length) { + super(bytes, offset, length); + } + + @Override + public PositionedByteRange set(byte[] bytes) { + if (super.bytes != null) { + throw new ReadOnlyByteRangeException(); + } + return super.set(bytes); + } + + @Override + public PositionedByteRange set(byte[] bytes, int offset, int length) { + if (super.bytes != null) { + throw new ReadOnlyByteRangeException(); + } + return super.set(bytes, offset, length); + } + + @Override + public PositionedByteRange set(int capacity) { + if (super.bytes != null) { + throw new ReadOnlyByteRangeException(); + } + return super.set(capacity); + } + + @Override + public PositionedByteRange put(byte val) { + throw new ReadOnlyByteRangeException(); + } + + @Override + public PositionedByteRange put(byte[] val) { + throw new ReadOnlyByteRangeException(); + } + + @Override + public PositionedByteRange put(byte[] val, int offset, int length) { + throw new ReadOnlyByteRangeException(); + } + + @Override + public PositionedByteRange put(int index, byte val) { + throw new ReadOnlyByteRangeException(); + } + + @Override + public PositionedByteRange put(int index, byte[] val) { + throw new ReadOnlyByteRangeException(); + } + + @Override + public PositionedByteRange put(int index, byte[] val, int offset, int length) { + throw new ReadOnlyByteRangeException(); + } + + @Override + public PositionedByteRange unset() { + throw new ReadOnlyByteRangeException(); + } + + @Override + public PositionedByteRange deepCopy() { + SimplePositionedImmutableByteRange clone = new SimplePositionedImmutableByteRange( + deepCopyToNewArray()); + clone.position = this.position; + return clone; + } + + @Override + public PositionedByteRange shallowCopy() { + SimplePositionedImmutableByteRange clone = new SimplePositionedImmutableByteRange(bytes, + offset, length); + clone.position = this.position; + return clone; + } + + @Override + public PositionedByteRange shallowCopySubRange(int innerOffset, int copyLength) { + SimplePositionedImmutableByteRange clone = new SimplePositionedImmutableByteRange(bytes, offset + + innerOffset, copyLength); + clone.position = this.position; + return clone; + } + + @Override + public PositionedByteRange setLimit(int limit) { + throw new ReadOnlyByteRangeException(); + } + +}