2626import java .nio .ByteBuffer ;
2727import java .util .ArrayList ;
2828import java .util .List ;
29+
2930import org .apache .hadoop .conf .Configuration ;
3031import org .apache .hadoop .hbase .ArrayBackedTag ;
3132import org .apache .hadoop .hbase .ByteBufferKeyValue ;
@@ -74,14 +75,20 @@ public void testValueCompressionEnabled() throws Exception {
7475 @ Test
7576 public void testValueCompression () throws Exception {
7677 final byte [] row_1 = Bytes .toBytes ("row_1" );
77- final byte [] value_1 = new byte [512 ];
78+ final byte [] value_1 = new byte [WALCellCodec . VALUE_COMPRESS_THRESHOLD ];
7879 Bytes .zero (value_1 );
7980 final byte [] row_2 = Bytes .toBytes ("row_2" );
8081 final byte [] value_2 = new byte [Bytes .SIZEOF_LONG ];
8182 Bytes .random (value_2 );
8283 final byte [] row_3 = Bytes .toBytes ("row_3" );
83- final byte [] value_3 = new byte [1024 ];
84+ final byte [] value_3 = new byte [WALCellCodec . VALUE_COMPRESS_THRESHOLD ];
8485 Bytes .random (value_3 );
86+ final byte [] row_4 = Bytes .toBytes ("row_4" );
87+ final byte [] value_4 = new byte [WALCellCodec .VALUE_COMPRESS_THRESHOLD * 4 ];
88+ fillBytes (value_4 , Bytes .toBytes ("DEADBEEF" ));
89+ final byte [] row_5 = Bytes .toBytes ("row_5" );
90+ final byte [] value_5 = new byte [WALCellCodec .VALUE_COMPRESS_THRESHOLD * 2 ];
91+ fillBytes (value_5 , Bytes .toBytes ("CAFEBABE" ));
8592
8693 Configuration conf = new Configuration (false );
8794 WALCellCodec codec = new WALCellCodec (conf , new CompressionContext (LRUDictionary .class ,
@@ -91,6 +98,8 @@ public void testValueCompression() throws Exception {
9198 encoder .write (createKV (row_1 , value_1 , 0 ));
9299 encoder .write (createKV (row_2 , value_2 , 0 ));
93100 encoder .write (createKV (row_3 , value_3 , 0 ));
101+ encoder .write (createKV (row_4 , value_4 , 0 ));
102+ encoder .write (createKV (row_5 , value_5 , 0 ));
94103 encoder .flush ();
95104
96105 try (InputStream is = new ByteArrayInputStream (bos .toByteArray ())) {
@@ -113,55 +122,57 @@ public void testValueCompression() throws Exception {
113122 kv .getRowArray (), kv .getRowOffset (), kv .getRowLength ()));
114123 assertTrue (Bytes .equals (value_3 , 0 , value_3 .length ,
115124 kv .getValueArray (), kv .getValueOffset (), kv .getValueLength ()));
125+ decoder .advance ();
126+ kv = (KeyValue ) decoder .current ();
127+ assertTrue (Bytes .equals (row_4 , 0 , row_4 .length ,
128+ kv .getRowArray (), kv .getRowOffset (), kv .getRowLength ()));
129+ assertTrue (Bytes .equals (value_4 , 0 , value_4 .length ,
130+ kv .getValueArray (), kv .getValueOffset (), kv .getValueLength ()));
131+ decoder .advance ();
132+ kv = (KeyValue ) decoder .current ();
133+ assertTrue (Bytes .equals (row_5 , 0 , row_5 .length ,
134+ kv .getRowArray (), kv .getRowOffset (), kv .getRowLength ()));
135+ assertTrue (Bytes .equals (value_5 , 0 , value_5 .length ,
136+ kv .getValueArray (), kv .getValueOffset (), kv .getValueLength ()));
137+ }
138+ }
139+
140+ static void fillBytes (byte [] buffer , byte [] fill ) {
141+ int offset = 0 ;
142+ int remaining = buffer .length ;
143+ while (remaining > 0 ) {
144+ int len = remaining < fill .length ? remaining : fill .length ;
145+ System .arraycopy (fill , 0 , buffer , offset , len );
146+ offset += len ;
147+ remaining -= len ;
116148 }
117149 }
118150
119151 @ Test
120152 public void testValueCompressionCompatibility () throws Exception {
121- final byte [] row_1 = Bytes .toBytes ("row_1" );
122- final byte [] value_1 = new byte [512 ];
123- Bytes .zero (value_1 );
124- final byte [] row_2 = Bytes .toBytes ("row_2" );
125- final byte [] value_2 = new byte [Bytes .SIZEOF_LONG ];
126- Bytes .random (value_2 );
127- final byte [] row_3 = Bytes .toBytes ("row_3" );
128- final byte [] value_3 = new byte [1024 ];
129- Bytes .random (value_3 );
130-
153+ final byte [] key = Bytes .toBytes ("myRow" );
154+ final byte [] value = Bytes .toBytes ("myValue" );
131155 Configuration conf = new Configuration (false );
132156 WALCellCodec outCodec = new WALCellCodec (conf , new CompressionContext (LRUDictionary .class ,
133157 false , false , false ));
134158 ByteArrayOutputStream bos = new ByteArrayOutputStream ();
135159 Encoder encoder = outCodec .getEncoder (bos );
136- encoder . write ( createKV ( row_1 , value_1 , 0 ));
137- encoder .write (createKV ( row_2 , value_2 , 0 ));
138- encoder . write ( createKV ( row_3 , value_3 , 0 ));
160+ for ( int i = 0 ; i < 10 ; i ++) {
161+ encoder .write (createOffheapKV ( key , value , 0 ));
162+ }
139163 encoder .flush ();
140-
141- byte [] encodedCells = bos .toByteArray ();
142-
143164 WALCellCodec inCodec = new WALCellCodec (conf , new CompressionContext (LRUDictionary .class ,
144165 false , false , true ));
145- try (InputStream is = new ByteArrayInputStream (encodedCells )) {
166+ try (InputStream is = new ByteArrayInputStream (bos . toByteArray () )) {
146167 Decoder decoder = inCodec .getDecoder (is );
147- decoder .advance ();
148- KeyValue kv = (KeyValue ) decoder .current ();
149- assertTrue (Bytes .equals (row_1 , 0 , row_1 .length ,
150- kv .getRowArray (), kv .getRowOffset (), kv .getRowLength ()));
151- assertTrue (Bytes .equals (value_1 , 0 , value_1 .length ,
152- kv .getValueArray (), kv .getValueOffset (), kv .getValueLength ()));
153- decoder .advance ();
154- kv = (KeyValue ) decoder .current ();
155- assertTrue (Bytes .equals (row_2 , 0 , row_2 .length ,
156- kv .getRowArray (), kv .getRowOffset (), kv .getRowLength ()));
157- assertTrue (Bytes .equals (value_2 , 0 , value_2 .length ,
158- kv .getValueArray (), kv .getValueOffset (), kv .getValueLength ()));
159- decoder .advance ();
160- kv = (KeyValue ) decoder .current ();
161- assertTrue (Bytes .equals (row_3 , 0 , row_3 .length ,
162- kv .getRowArray (), kv .getRowOffset (), kv .getRowLength ()));
163- assertTrue (Bytes .equals (value_3 , 0 , value_3 .length ,
164- kv .getValueArray (), kv .getValueOffset (), kv .getValueLength ()));
168+ for (int i = 0 ; i < 10 ; i ++) {
169+ decoder .advance ();
170+ KeyValue kv = (KeyValue ) decoder .current ();
171+ assertTrue (Bytes .equals (key , 0 , key .length ,
172+ kv .getRowArray (), kv .getRowOffset (), kv .getRowLength ()));
173+ assertTrue (Bytes .equals (value , 0 , value .length ,
174+ kv .getValueArray (), kv .getValueOffset (), kv .getValueLength ()));
175+ }
165176 }
166177 }
167178
0 commit comments