001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.util; 019 020import java.util.ArrayList; 021import java.util.List; 022import org.apache.hadoop.hbase.ArrayBackedTag; 023import org.apache.hadoop.hbase.HBaseClassTestRule; 024import org.apache.hadoop.hbase.KeyValue; 025import org.apache.hadoop.hbase.Tag; 026import org.apache.hadoop.hbase.testclassification.MiscTests; 027import org.apache.hadoop.hbase.testclassification.SmallTests; 028import org.junit.Assert; 029import org.junit.ClassRule; 030import org.junit.Test; 031import org.junit.experimental.categories.Category; 032 033@Category({ MiscTests.class, SmallTests.class }) 034public class TestByteRangeWithKVSerialization { 035 036 @ClassRule 037 public static final HBaseClassTestRule CLASS_RULE = 038 HBaseClassTestRule.forClass(TestByteRangeWithKVSerialization.class); 039 040 static void writeCell(PositionedByteRange pbr, KeyValue kv) throws Exception { 041 pbr.putInt(kv.getKeyLength()); 042 pbr.putInt(kv.getValueLength()); 043 pbr.put(kv.getBuffer(), kv.getKeyOffset(), kv.getKeyLength()); 044 pbr.put(kv.getBuffer(), kv.getValueOffset(), kv.getValueLength()); 045 int tagsLen = kv.getTagsLength(); 046 pbr.put((byte) (tagsLen >> 8 & 0xff)); 047 pbr.put((byte) (tagsLen & 0xff)); 048 pbr.put(kv.getTagsArray(), kv.getTagsOffset(), tagsLen); 049 pbr.putVLong(kv.getSequenceId()); 050 } 051 052 static KeyValue readCell(PositionedByteRange pbr) throws Exception { 053 int kvStartPos = pbr.getPosition(); 054 int keyLen = pbr.getInt(); 055 int valLen = pbr.getInt(); 056 pbr.setPosition(pbr.getPosition() + keyLen + valLen); // Skip the key and value section 057 int tagsLen = ((pbr.get() & 0xff) << 8) ^ (pbr.get() & 0xff); 058 pbr.setPosition(pbr.getPosition() + tagsLen); // Skip the tags section 059 long mvcc = pbr.getVLong(); 060 KeyValue kv = new KeyValue(pbr.getBytes(), kvStartPos, 061 (int) KeyValue.getKeyValueDataStructureSize(keyLen, valLen, tagsLen)); 062 kv.setSequenceId(mvcc); 063 return kv; 064 } 065 066 @Test 067 public void testWritingAndReadingCells() throws Exception { 068 final byte[] FAMILY = Bytes.toBytes("f1"); 069 final byte[] QUALIFIER = Bytes.toBytes("q1"); 070 final byte[] VALUE = Bytes.toBytes("v"); 071 int kvCount = 1000000; 072 List<KeyValue> kvs = new ArrayList<>(kvCount); 073 int totalSize = 0; 074 Tag[] tags = new Tag[] { new ArrayBackedTag((byte) 1, "tag1") }; 075 for (int i = 0; i < kvCount; i++) { 076 KeyValue kv = new KeyValue(Bytes.toBytes(i), FAMILY, QUALIFIER, i, VALUE, tags); 077 kv.setSequenceId(i); 078 kvs.add(kv); 079 totalSize += kv.getLength() + Bytes.SIZEOF_LONG; 080 } 081 PositionedByteRange pbr = new SimplePositionedMutableByteRange(totalSize); 082 for (KeyValue kv : kvs) { 083 writeCell(pbr, kv); 084 } 085 086 PositionedByteRange pbr1 = 087 new SimplePositionedMutableByteRange(pbr.getBytes(), 0, pbr.getPosition()); 088 for (int i = 0; i < kvCount; i++) { 089 KeyValue kv = readCell(pbr1); 090 KeyValue kv1 = kvs.get(i); 091 Assert.assertTrue(kv.equals(kv1)); 092 Assert.assertTrue(Bytes.equals(kv.getValueArray(), kv.getValueOffset(), kv.getValueLength(), 093 kv1.getValueArray(), kv1.getValueOffset(), kv1.getValueLength())); 094 Assert.assertTrue(Bytes.equals(kv.getTagsArray(), kv.getTagsOffset(), kv.getTagsLength(), 095 kv1.getTagsArray(), kv1.getTagsOffset(), kv1.getTagsLength())); 096 Assert.assertEquals(kv1.getSequenceId(), kv.getSequenceId()); 097 } 098 } 099}