HBASE-26921 Rewrite the counting cells part in TestMultiVersions (#4316)
[hbase.git] / hbase-common / src / main / java / org / apache / hadoop / hbase / util / SimplePositionedMutableByteRange.java
blobe1e2d2714598ea65041ca795d883a617a037c2bb
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * The portion of this file denoted by 'Copied from com.google.protobuf.CodedOutputStream'
19 * is from Protocol Buffers v2.5.0 under the following license
21 * Copyright 2008 Google Inc. All rights reserved.
22 * http://code.google.com/p/protobuf/
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions are
26 * met:
28 * * Redistributions of source code must retain the above copyright
29 * notice, this list of conditions and the following disclaimer.
30 * * Redistributions in binary form must reproduce the above
31 * copyright notice, this list of conditions and the following disclaimer
32 * in the documentation and/or other materials provided with the
33 * distribution.
34 * * Neither the name of Google Inc. nor the names of its
35 * contributors may be used to endorse or promote products derived from
36 * this software without specific prior written permission.
38 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
39 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
40 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
41 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
42 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
45 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
46 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
47 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
48 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51 package org.apache.hadoop.hbase.util;
53 import org.apache.yetus.audience.InterfaceAudience;
55 /**
56 * Extends the basic {@link AbstractPositionedByteRange} implementation with
57 * position support and it is a mutable version. {@code position} is considered transient,
58 * not fundamental to the definition of the range, and does not participate in
59 * {@link #compareTo(ByteRange)}, {@link #hashCode()}, or
60 * {@link #equals(Object)}. {@code Position} is retained by copy operations.
62 @InterfaceAudience.Public
63 @edu.umd.cs.findbugs.annotations.SuppressWarnings("EQ_DOESNT_OVERRIDE_EQUALS")
64 public class SimplePositionedMutableByteRange extends AbstractPositionedByteRange {
65 /**
66 * Create a new {@code PositionedByteRange} lacking a backing array and with
67 * an undefined viewport.
69 public SimplePositionedMutableByteRange() {
70 super();
73 /**
74 * Create a new {@code PositionedByteRange} over a new backing array of size
75 * {@code capacity}. The range's offset and length are 0 and {@code capacity},
76 * respectively.
78 * @param capacity
79 * the size of the backing array.
81 public SimplePositionedMutableByteRange(int capacity) {
82 this(new byte[capacity]);
85 /**
86 * Create a new {@code PositionedByteRange} over the provided {@code bytes}.
88 * @param bytes
89 * The array to wrap.
91 public SimplePositionedMutableByteRange(byte[] bytes) {
92 set(bytes);
95 /**
96 * Create a new {@code PositionedByteRange} over the provided {@code bytes}.
98 * @param bytes
99 * The array to wrap.
100 * @param offset
101 * The offset into {@code bytes} considered the beginning of this
102 * range.
103 * @param length
104 * The length of this range.
106 public SimplePositionedMutableByteRange(byte[] bytes, int offset, int length) {
107 set(bytes, offset, length);
110 @Override
111 public PositionedByteRange unset() {
112 this.position = 0;
113 clearHashCache();
114 bytes = null;
115 offset = 0;
116 length = 0;
117 return this;
120 @Override
121 public PositionedByteRange set(int capacity) {
122 this.position = 0;
123 super.set(capacity);
124 this.limit = capacity;
125 return this;
128 @Override
129 public PositionedByteRange set(byte[] bytes) {
130 this.position = 0;
131 super.set(bytes);
132 this.limit = bytes.length;
133 return this;
136 @Override
137 public PositionedByteRange set(byte[] bytes, int offset, int length) {
138 this.position = 0;
139 super.set(bytes, offset, length);
140 limit = length;
141 return this;
145 * Update the beginning of this range. {@code offset + length} may not be
146 * greater than {@code bytes.length}. Resets {@code position} to 0.
148 * @param offset
149 * the new start of this range.
150 * @return this.
152 @Override
153 public PositionedByteRange setOffset(int offset) {
154 this.position = 0;
155 super.setOffset(offset);
156 return this;
160 * Update the length of this range. {@code offset + length} should not be
161 * greater than {@code bytes.length}. If {@code position} is greater than the
162 * new {@code length}, sets {@code position} to {@code length}.
164 * @param length
165 * The new length of this range.
166 * @return this.
168 @Override
169 public PositionedByteRange setLength(int length) {
170 this.position = Math.min(position, length);
171 super.setLength(length);
172 return this;
175 @Override
176 public PositionedByteRange put(byte val) {
177 put(position++, val);
178 return this;
181 @Override
182 public PositionedByteRange put(byte[] val) {
183 if (0 == val.length)
184 return this;
185 return this.put(val, 0, val.length);
188 @Override
189 public PositionedByteRange put(byte[] val, int offset, int length) {
190 if (0 == length)
191 return this;
192 put(position, val, offset, length);
193 this.position += length;
194 return this;
197 @Override
198 public PositionedByteRange get(int index, byte[] dst) {
199 super.get(index, dst);
200 return this;
203 @Override
204 public PositionedByteRange get(int index, byte[] dst, int offset, int length) {
205 super.get(index, dst, offset, length);
206 return this;
209 @Override
210 public PositionedByteRange put(int index, byte val) {
211 bytes[offset + index] = val;
212 return this;
215 @Override
216 public PositionedByteRange put(int index, byte[] val) {
217 if (0 == val.length)
218 return this;
219 return put(index, val, 0, val.length);
222 @Override
223 public PositionedByteRange put(int index, byte[] val, int offset, int length) {
224 if (0 == length)
225 return this;
226 System.arraycopy(val, offset, this.bytes, this.offset + index, length);
227 return this;
230 @Override
231 public PositionedByteRange deepCopy() {
232 SimplePositionedMutableByteRange clone = new SimplePositionedMutableByteRange(
233 deepCopyToNewArray());
234 clone.position = this.position;
235 return clone;
238 @Override
239 public PositionedByteRange shallowCopy() {
240 SimplePositionedMutableByteRange clone = new SimplePositionedMutableByteRange(bytes, offset,
241 length);
242 clone.position = this.position;
243 return clone;
246 @Override
247 public PositionedByteRange shallowCopySubRange(int innerOffset, int copyLength) {
248 SimplePositionedMutableByteRange clone = new SimplePositionedMutableByteRange(bytes, offset
249 + innerOffset, copyLength);
250 clone.position = this.position;
251 return clone;
254 @Override
255 public PositionedByteRange putShort(short val) {
256 putShort(position, val);
257 position += Bytes.SIZEOF_SHORT;
258 return this;
261 @Override
262 public PositionedByteRange putInt(int val) {
263 putInt(position, val);
264 position += Bytes.SIZEOF_INT;
265 return this;
268 @Override
269 public PositionedByteRange putLong(long val) {
270 putLong(position, val);
271 position += Bytes.SIZEOF_LONG;
272 return this;
275 @Override
276 public int putVLong(long val) {
277 int len = putVLong(position, val);
278 position += len;
279 return len;
282 @Override
283 public PositionedByteRange putShort(int index, short val) {
284 // This writing is same as BB's putShort. When byte[] is wrapped in a BB and
285 // call putShort(),
286 // one can get the same result.
287 bytes[offset + index + 1] = (byte) val;
288 val >>= 8;
289 bytes[offset + index] = (byte) val;
290 clearHashCache();
291 return this;
294 @Override
295 public PositionedByteRange putInt(int index, int val) {
296 // This writing is same as BB's putInt. When byte[] is wrapped in a BB and
297 // call getInt(), one
298 // can get the same result.
299 for (int i = Bytes.SIZEOF_INT - 1; i > 0; i--) {
300 bytes[offset + index + i] = (byte) val;
301 val >>>= 8;
303 bytes[offset + index] = (byte) val;
304 clearHashCache();
305 return this;
308 @Override
309 public PositionedByteRange putLong(int index, long val) {
310 // This writing is same as BB's putLong. When byte[] is wrapped in a BB and
311 // call putLong(), one
312 // can get the same result.
313 for (int i = Bytes.SIZEOF_LONG - 1; i > 0; i--) {
314 bytes[offset + index + i] = (byte) val;
315 val >>>= 8;
317 bytes[offset + index] = (byte) val;
318 clearHashCache();
319 return this;
322 // Copied from com.google.protobuf.CodedOutputStream v2.5.0 writeRawVarint64
323 @Override
324 public int putVLong(int index, long val) {
325 int rPos = 0;
326 while (true) {
327 if ((val & ~0x7F) == 0) {
328 bytes[offset + index + rPos] = (byte) val;
329 break;
330 } else {
331 bytes[offset + index + rPos] = (byte) ((val & 0x7F) | 0x80);
332 val >>>= 7;
334 rPos++;
336 clearHashCache();
337 return rPos + 1;
339 // end copied from protobuf