1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase;
21
22 import static org.apache.hadoop.hbase.util.Bytes.len;
23
24 import java.io.DataInput;
25 import java.io.DataOutput;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.OutputStream;
29 import java.nio.ByteBuffer;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.Comparator;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39 import org.apache.hadoop.hbase.classification.InterfaceAudience;
40 import org.apache.hadoop.hbase.io.HeapSize;
41 import org.apache.hadoop.hbase.io.util.StreamUtils;
42 import org.apache.hadoop.hbase.util.Bytes;
43 import org.apache.hadoop.hbase.util.ClassSize;
44 import org.apache.hadoop.io.RawComparator;
45
46 import com.google.common.annotations.VisibleForTesting;
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83 @InterfaceAudience.Private
84 public class KeyValue implements Cell, HeapSize, Cloneable, SettableSequenceId, SettableTimestamp {
85 private static final ArrayList<Tag> EMPTY_ARRAY_LIST = new ArrayList<Tag>();
86
87 private static final Log LOG = LogFactory.getLog(KeyValue.class);
88
89
90
91
92 public static final char COLUMN_FAMILY_DELIMITER = ':';
93
94 public static final byte[] COLUMN_FAMILY_DELIM_ARRAY =
95 new byte[]{COLUMN_FAMILY_DELIMITER};
96
97
98
99
100
101 public static final KVComparator COMPARATOR = new KVComparator();
102
103
104
105
106 public static final KVComparator META_COMPARATOR = new MetaComparator();
107
108
109
110
111 public static final KVComparator RAW_COMPARATOR = new RawBytesComparator();
112
113
114 public static final int KEY_LENGTH_SIZE = Bytes.SIZEOF_INT;
115
116
117 public static final int TYPE_SIZE = Bytes.SIZEOF_BYTE;
118
119
120 public static final int ROW_LENGTH_SIZE = Bytes.SIZEOF_SHORT;
121
122
123 public static final int FAMILY_LENGTH_SIZE = Bytes.SIZEOF_BYTE;
124
125
126 public static final int TIMESTAMP_SIZE = Bytes.SIZEOF_LONG;
127
128
129 public static final int TIMESTAMP_TYPE_SIZE = TIMESTAMP_SIZE + TYPE_SIZE;
130
131
132 public static final int KEY_INFRASTRUCTURE_SIZE = ROW_LENGTH_SIZE
133 + FAMILY_LENGTH_SIZE + TIMESTAMP_TYPE_SIZE;
134
135
136
137 public static final int ROW_OFFSET =
138 Bytes.SIZEOF_INT
139 Bytes.SIZEOF_INT
140
141
142 public static final int KEYVALUE_INFRASTRUCTURE_SIZE = ROW_OFFSET;
143
144
145 public static final int TAGS_LENGTH_SIZE = Bytes.SIZEOF_SHORT;
146
147 public static final int KEYVALUE_WITH_TAGS_INFRASTRUCTURE_SIZE = ROW_OFFSET + TAGS_LENGTH_SIZE;
148
149 private static final int MAX_TAGS_LENGTH = (2 * Short.MAX_VALUE) + 1;
150
151
152
153
154
155
156
157
158
159
160
161
162 public static long getKeyValueDataStructureSize(int rlength,
163 int flength, int qlength, int vlength) {
164 return KeyValue.KEYVALUE_INFRASTRUCTURE_SIZE
165 + getKeyDataStructureSize(rlength, flength, qlength) + vlength;
166 }
167
168
169
170
171
172
173
174
175
176
177
178
179
180 public static long getKeyValueDataStructureSize(int rlength, int flength, int qlength,
181 int vlength, int tagsLength) {
182 if (tagsLength == 0) {
183 return getKeyValueDataStructureSize(rlength, flength, qlength, vlength);
184 }
185 return KeyValue.KEYVALUE_WITH_TAGS_INFRASTRUCTURE_SIZE
186 + getKeyDataStructureSize(rlength, flength, qlength) + vlength + tagsLength;
187 }
188
189
190
191
192
193
194
195
196
197
198
199 public static long getKeyValueDataStructureSize(int klength, int vlength, int tagsLength) {
200 if (tagsLength == 0) {
201 return KeyValue.KEYVALUE_INFRASTRUCTURE_SIZE + klength + vlength;
202 }
203 return KeyValue.KEYVALUE_WITH_TAGS_INFRASTRUCTURE_SIZE + klength + vlength + tagsLength;
204 }
205
206
207
208
209
210
211
212
213
214
215
216 public static long getKeyDataStructureSize(int rlength, int flength, int qlength) {
217 return KeyValue.KEY_INFRASTRUCTURE_SIZE + rlength + flength + qlength;
218 }
219
220
221
222
223
224
225 public static enum Type {
226 Minimum((byte)0),
227 Put((byte)4),
228
229 Delete((byte)8),
230 DeleteFamilyVersion((byte)10),
231 DeleteColumn((byte)12),
232 DeleteFamily((byte)14),
233
234
235 Maximum((byte)255);
236
237 private final byte code;
238
239 Type(final byte c) {
240 this.code = c;
241 }
242
243 public byte getCode() {
244 return this.code;
245 }
246
247
248
249
250
251
252
253 public static Type codeToType(final byte b) {
254 for (Type t : Type.values()) {
255 if (t.getCode() == b) {
256 return t;
257 }
258 }
259 throw new RuntimeException("Unknown code " + b);
260 }
261 }
262
263
264
265
266
267
268 public static final KeyValue LOWESTKEY =
269 new KeyValue(HConstants.EMPTY_BYTE_ARRAY, HConstants.LATEST_TIMESTAMP);
270
271
272
273 protected byte [] bytes = null;
274 protected int offset = 0;
275 protected int length = 0;
276
277
278
279
280
281
282 public static boolean isDelete(byte t) {
283 return Type.Delete.getCode() <= t && t <= Type.DeleteFamily.getCode();
284 }
285
286
287
288
289 @Override
290 public long getMvccVersion() {
291 return this.getSequenceId();
292 }
293
294
295
296
297 @Override
298 public long getSequenceId() {
299 return seqId;
300 }
301
302 @Override
303 public void setSequenceId(long seqId) {
304 this.seqId = seqId;
305 }
306
307
308 private long seqId = 0;
309
310
311
312
313
314 public KeyValue() {}
315
316
317
318
319
320
321 public KeyValue(final byte [] bytes) {
322 this(bytes, 0);
323 }
324
325
326
327
328
329
330
331
332 public KeyValue(final byte [] bytes, final int offset) {
333 this(bytes, offset, getLength(bytes, offset));
334 }
335
336
337
338
339
340
341
342
343 public KeyValue(final byte [] bytes, final int offset, final int length) {
344 this.bytes = bytes;
345 this.offset = offset;
346 this.length = length;
347 }
348
349
350
351
352
353
354
355
356
357
358 public KeyValue(final byte[] bytes, final int offset, final int length, long ts) {
359 this(bytes, offset, length, null, 0, 0, null, 0, 0, ts, Type.Maximum, null, 0, 0, null);
360 }
361
362
363
364
365
366
367
368
369
370 public KeyValue(final byte [] row, final long timestamp) {
371 this(row, null, null, timestamp, Type.Maximum, null);
372 }
373
374
375
376
377
378
379 public KeyValue(final byte [] row, final long timestamp, Type type) {
380 this(row, null, null, timestamp, type, null);
381 }
382
383
384
385
386
387
388
389
390 public KeyValue(final byte [] row, final byte [] family,
391 final byte [] qualifier) {
392 this(row, family, qualifier, HConstants.LATEST_TIMESTAMP, Type.Maximum);
393 }
394
395
396
397
398
399
400
401
402 public KeyValue(final byte [] row, final byte [] family,
403 final byte [] qualifier, final byte [] value) {
404 this(row, family, qualifier, HConstants.LATEST_TIMESTAMP, Type.Put, value);
405 }
406
407
408
409
410
411
412
413
414
415
416 public KeyValue(final byte[] row, final byte[] family,
417 final byte[] qualifier, final long timestamp, Type type) {
418 this(row, family, qualifier, timestamp, type, null);
419 }
420
421
422
423
424
425
426
427
428
429
430 public KeyValue(final byte[] row, final byte[] family,
431 final byte[] qualifier, final long timestamp, final byte[] value) {
432 this(row, family, qualifier, timestamp, Type.Put, value);
433 }
434
435
436
437
438
439
440
441
442
443
444
445 public KeyValue(final byte[] row, final byte[] family,
446 final byte[] qualifier, final long timestamp, final byte[] value,
447 final Tag[] tags) {
448 this(row, family, qualifier, timestamp, value, tags != null ? Arrays.asList(tags) : null);
449 }
450
451
452
453
454
455
456
457
458
459
460
461 public KeyValue(final byte[] row, final byte[] family,
462 final byte[] qualifier, final long timestamp, final byte[] value,
463 final List<Tag> tags) {
464 this(row, 0, row==null ? 0 : row.length,
465 family, 0, family==null ? 0 : family.length,
466 qualifier, 0, qualifier==null ? 0 : qualifier.length,
467 timestamp, Type.Put,
468 value, 0, value==null ? 0 : value.length, tags);
469 }
470
471
472
473
474
475
476
477
478
479
480
481 public KeyValue(final byte[] row, final byte[] family,
482 final byte[] qualifier, final long timestamp, Type type,
483 final byte[] value) {
484 this(row, 0, len(row), family, 0, len(family), qualifier, 0, len(qualifier),
485 timestamp, type, value, 0, len(value));
486 }
487
488
489
490
491
492
493
494
495
496
497
498
499
500 public KeyValue(final byte[] row, final byte[] family,
501 final byte[] qualifier, final long timestamp, Type type,
502 final byte[] value, final List<Tag> tags) {
503 this(row, family, qualifier, 0, qualifier==null ? 0 : qualifier.length,
504 timestamp, type, value, 0, value==null ? 0 : value.length, tags);
505 }
506
507
508
509
510
511
512
513
514
515
516
517 public KeyValue(final byte[] row, final byte[] family,
518 final byte[] qualifier, final long timestamp, Type type,
519 final byte[] value, final byte[] tags) {
520 this(row, family, qualifier, 0, qualifier==null ? 0 : qualifier.length,
521 timestamp, type, value, 0, value==null ? 0 : value.length, tags);
522 }
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538 public KeyValue(byte [] row, byte [] family,
539 byte [] qualifier, int qoffset, int qlength, long timestamp, Type type,
540 byte [] value, int voffset, int vlength, List<Tag> tags) {
541 this(row, 0, row==null ? 0 : row.length,
542 family, 0, family==null ? 0 : family.length,
543 qualifier, qoffset, qlength, timestamp, type,
544 value, voffset, vlength, tags);
545 }
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560 public KeyValue(byte [] row, byte [] family,
561 byte [] qualifier, int qoffset, int qlength, long timestamp, Type type,
562 byte [] value, int voffset, int vlength, byte[] tags) {
563 this(row, 0, row==null ? 0 : row.length,
564 family, 0, family==null ? 0 : family.length,
565 qualifier, qoffset, qlength, timestamp, type,
566 value, voffset, vlength, tags, 0, tags==null ? 0 : tags.length);
567 }
568
569
570
571
572
573
574
575
576 public KeyValue(final byte [] row, final int roffset, final int rlength,
577 final byte [] family, final int foffset, final int flength,
578 final byte [] qualifier, final int qoffset, final int qlength,
579 final long timestamp, final Type type,
580 final byte [] value, final int voffset, final int vlength) {
581 this(row, roffset, rlength, family, foffset, flength, qualifier, qoffset,
582 qlength, timestamp, type, value, voffset, vlength, null);
583 }
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611 public KeyValue(byte [] buffer, final int boffset,
612 final byte [] row, final int roffset, final int rlength,
613 final byte [] family, final int foffset, final int flength,
614 final byte [] qualifier, final int qoffset, final int qlength,
615 final long timestamp, final Type type,
616 final byte [] value, final int voffset, final int vlength,
617 final Tag[] tags) {
618 this.bytes = buffer;
619 this.length = writeByteArray(buffer, boffset,
620 row, roffset, rlength,
621 family, foffset, flength, qualifier, qoffset, qlength,
622 timestamp, type, value, voffset, vlength, tags);
623 this.offset = boffset;
624 }
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647 public KeyValue(final byte [] row, final int roffset, final int rlength,
648 final byte [] family, final int foffset, final int flength,
649 final byte [] qualifier, final int qoffset, final int qlength,
650 final long timestamp, final Type type,
651 final byte [] value, final int voffset, final int vlength,
652 final List<Tag> tags) {
653 this.bytes = createByteArray(row, roffset, rlength,
654 family, foffset, flength, qualifier, qoffset, qlength,
655 timestamp, type, value, voffset, vlength, tags);
656 this.length = bytes.length;
657 this.offset = 0;
658 }
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677 public KeyValue(final byte [] row, final int roffset, final int rlength,
678 final byte [] family, final int foffset, final int flength,
679 final byte [] qualifier, final int qoffset, final int qlength,
680 final long timestamp, final Type type,
681 final byte [] value, final int voffset, final int vlength,
682 final byte[] tags, final int tagsOffset, final int tagsLength) {
683 this.bytes = createByteArray(row, roffset, rlength,
684 family, foffset, flength, qualifier, qoffset, qlength,
685 timestamp, type, value, voffset, vlength, tags, tagsOffset, tagsLength);
686 this.length = bytes.length;
687 this.offset = 0;
688 }
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703 public KeyValue(final int rlength,
704 final int flength,
705 final int qlength,
706 final long timestamp, final Type type,
707 final int vlength) {
708 this(rlength, flength, qlength, timestamp, type, vlength, 0);
709 }
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725 public KeyValue(final int rlength,
726 final int flength,
727 final int qlength,
728 final long timestamp, final Type type,
729 final int vlength, final int tagsLength) {
730 this.bytes = createEmptyByteArray(rlength, flength, qlength, timestamp, type, vlength,
731 tagsLength);
732 this.length = bytes.length;
733 this.offset = 0;
734 }
735
736
737 public KeyValue(byte[] row, int roffset, int rlength,
738 byte[] family, int foffset, int flength,
739 ByteBuffer qualifier, long ts, Type type, ByteBuffer value, List<Tag> tags) {
740 this.bytes = createByteArray(row, roffset, rlength, family, foffset, flength,
741 qualifier, 0, qualifier == null ? 0 : qualifier.remaining(), ts, type,
742 value, 0, value == null ? 0 : value.remaining(), tags);
743 this.length = bytes.length;
744 this.offset = 0;
745 }
746
747 public KeyValue(Cell c) {
748 this(c.getRowArray(), c.getRowOffset(), (int)c.getRowLength(),
749 c.getFamilyArray(), c.getFamilyOffset(), (int)c.getFamilyLength(),
750 c.getQualifierArray(), c.getQualifierOffset(), (int) c.getQualifierLength(),
751 c.getTimestamp(), Type.codeToType(c.getTypeByte()), c.getValueArray(), c.getValueOffset(),
752 c.getValueLength(), c.getTagsArray(), c.getTagsOffset(), c.getTagsLength());
753 this.seqId = c.getSequenceId();
754 }
755
756
757
758
759
760
761
762
763
764
765 @Deprecated
766 public static KeyValue createFirstOnRow(final byte [] row) {
767 return KeyValueUtil.createFirstOnRow(row, HConstants.LATEST_TIMESTAMP);
768 }
769
770
771
772
773
774
775
776
777
778
779
780
781 @Deprecated
782 public static KeyValue createFirstOnRow(final byte [] row, final byte [] family,
783 final byte [] qualifier) {
784 return KeyValueUtil.createFirstOnRow(row, family, qualifier);
785 }
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805 @Deprecated
806 public static KeyValue createFirstOnRow(final byte [] row,
807 final int roffset, final int rlength, final byte [] family,
808 final int foffset, final int flength, final byte [] qualifier,
809 final int qoffset, final int qlength) {
810 return new KeyValue(row, roffset, rlength, family,
811 foffset, flength, qualifier, qoffset, qlength,
812 HConstants.LATEST_TIMESTAMP, Type.Maximum, null, 0, 0);
813 }
814
815
816
817
818
819
820
821
822
823
824
825
826 private static byte[] createEmptyByteArray(final int rlength, int flength,
827 int qlength, final long timestamp, final Type type, int vlength, int tagsLength) {
828 if (rlength > Short.MAX_VALUE) {
829 throw new IllegalArgumentException("Row > " + Short.MAX_VALUE);
830 }
831 if (flength > Byte.MAX_VALUE) {
832 throw new IllegalArgumentException("Family > " + Byte.MAX_VALUE);
833 }
834
835 if (qlength > Integer.MAX_VALUE - rlength - flength) {
836 throw new IllegalArgumentException("Qualifier > " + Integer.MAX_VALUE);
837 }
838 checkForTagsLength(tagsLength);
839
840 long longkeylength = getKeyDataStructureSize(rlength, flength, qlength);
841 if (longkeylength > Integer.MAX_VALUE) {
842 throw new IllegalArgumentException("keylength " + longkeylength + " > " +
843 Integer.MAX_VALUE);
844 }
845 int keylength = (int)longkeylength;
846
847 if (vlength > HConstants.MAXIMUM_VALUE_LENGTH) {
848 throw new IllegalArgumentException("Valuer > " +
849 HConstants.MAXIMUM_VALUE_LENGTH);
850 }
851
852
853 byte[] bytes= new byte[(int) getKeyValueDataStructureSize(rlength, flength, qlength, vlength,
854 tagsLength)];
855
856 int pos = 0;
857 pos = Bytes.putInt(bytes, pos, keylength);
858 pos = Bytes.putInt(bytes, pos, vlength);
859 pos = Bytes.putShort(bytes, pos, (short)(rlength & 0x0000ffff));
860 pos += rlength;
861 pos = Bytes.putByte(bytes, pos, (byte)(flength & 0x0000ff));
862 pos += flength + qlength;
863 pos = Bytes.putLong(bytes, pos, timestamp);
864 pos = Bytes.putByte(bytes, pos, type.getCode());
865 pos += vlength;
866 if (tagsLength > 0) {
867 pos = Bytes.putAsShort(bytes, pos, tagsLength);
868 }
869 return bytes;
870 }
871
872
873
874
875
876
877
878
879
880
881
882
883
884 private static void checkParameters(final byte [] row, final int rlength,
885 final byte [] family, int flength, int qlength, int vlength)
886 throws IllegalArgumentException {
887 if (rlength > Short.MAX_VALUE) {
888 throw new IllegalArgumentException("Row > " + Short.MAX_VALUE);
889 }
890 if (row == null) {
891 throw new IllegalArgumentException("Row is null");
892 }
893
894 flength = family == null ? 0 : flength;
895 if (flength > Byte.MAX_VALUE) {
896 throw new IllegalArgumentException("Family > " + Byte.MAX_VALUE);
897 }
898
899 if (qlength > Integer.MAX_VALUE - rlength - flength) {
900 throw new IllegalArgumentException("Qualifier > " + Integer.MAX_VALUE);
901 }
902
903 long longKeyLength = getKeyDataStructureSize(rlength, flength, qlength);
904 if (longKeyLength > Integer.MAX_VALUE) {
905 throw new IllegalArgumentException("keylength " + longKeyLength + " > " +
906 Integer.MAX_VALUE);
907 }
908
909 if (vlength > HConstants.MAXIMUM_VALUE_LENGTH) {
910 throw new IllegalArgumentException("Value length " + vlength + " > " +
911 HConstants.MAXIMUM_VALUE_LENGTH);
912 }
913 }
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940 public static int writeByteArray(byte [] buffer, final int boffset,
941 final byte [] row, final int roffset, final int rlength,
942 final byte [] family, final int foffset, int flength,
943 final byte [] qualifier, final int qoffset, int qlength,
944 final long timestamp, final Type type,
945 final byte [] value, final int voffset, int vlength, Tag[] tags) {
946
947 checkParameters(row, rlength, family, flength, qlength, vlength);
948
949
950 int tagsLength = 0;
951 if (tags != null && tags.length > 0) {
952 for (Tag t: tags) {
953 tagsLength += t.getLength();
954 }
955 }
956 checkForTagsLength(tagsLength);
957 int keyLength = (int) getKeyDataStructureSize(rlength, flength, qlength);
958 int keyValueLength = (int) getKeyValueDataStructureSize(rlength, flength, qlength, vlength,
959 tagsLength);
960 if (keyValueLength > buffer.length - boffset) {
961 throw new IllegalArgumentException("Buffer size " + (buffer.length - boffset) + " < " +
962 keyValueLength);
963 }
964
965
966 int pos = boffset;
967 pos = Bytes.putInt(buffer, pos, keyLength);
968 pos = Bytes.putInt(buffer, pos, vlength);
969 pos = Bytes.putShort(buffer, pos, (short)(rlength & 0x0000ffff));
970 pos = Bytes.putBytes(buffer, pos, row, roffset, rlength);
971 pos = Bytes.putByte(buffer, pos, (byte) (flength & 0x0000ff));
972 if (flength != 0) {
973 pos = Bytes.putBytes(buffer, pos, family, foffset, flength);
974 }
975 if (qlength != 0) {
976 pos = Bytes.putBytes(buffer, pos, qualifier, qoffset, qlength);
977 }
978 pos = Bytes.putLong(buffer, pos, timestamp);
979 pos = Bytes.putByte(buffer, pos, type.getCode());
980 if (value != null && value.length > 0) {
981 pos = Bytes.putBytes(buffer, pos, value, voffset, vlength);
982 }
983
984 if (tagsLength > 0) {
985 pos = Bytes.putAsShort(buffer, pos, tagsLength);
986 for (Tag t : tags) {
987 pos = Bytes.putBytes(buffer, pos, t.getBuffer(), t.getOffset(), t.getLength());
988 }
989 }
990 return keyValueLength;
991 }
992
993 private static void checkForTagsLength(int tagsLength) {
994 if (tagsLength > MAX_TAGS_LENGTH) {
995 throw new IllegalArgumentException("tagslength "+ tagsLength + " > " + MAX_TAGS_LENGTH);
996 }
997 }
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017 private static byte [] createByteArray(final byte [] row, final int roffset,
1018 final int rlength, final byte [] family, final int foffset, int flength,
1019 final byte [] qualifier, final int qoffset, int qlength,
1020 final long timestamp, final Type type,
1021 final byte [] value, final int voffset,
1022 int vlength, byte[] tags, int tagsOffset, int tagsLength) {
1023
1024 checkParameters(row, rlength, family, flength, qlength, vlength);
1025 checkForTagsLength(tagsLength);
1026
1027 int keyLength = (int) getKeyDataStructureSize(rlength, flength, qlength);
1028 byte[] bytes = new byte[(int) getKeyValueDataStructureSize(rlength, flength, qlength, vlength,
1029 tagsLength)];
1030
1031 int pos = 0;
1032 pos = Bytes.putInt(bytes, pos, keyLength);
1033 pos = Bytes.putInt(bytes, pos, vlength);
1034 pos = Bytes.putShort(bytes, pos, (short)(rlength & 0x0000ffff));
1035 pos = Bytes.putBytes(bytes, pos, row, roffset, rlength);
1036 pos = Bytes.putByte(bytes, pos, (byte)(flength & 0x0000ff));
1037 if(flength != 0) {
1038 pos = Bytes.putBytes(bytes, pos, family, foffset, flength);
1039 }
1040 if(qlength != 0) {
1041 pos = Bytes.putBytes(bytes, pos, qualifier, qoffset, qlength);
1042 }
1043 pos = Bytes.putLong(bytes, pos, timestamp);
1044 pos = Bytes.putByte(bytes, pos, type.getCode());
1045 if (value != null && value.length > 0) {
1046 pos = Bytes.putBytes(bytes, pos, value, voffset, vlength);
1047 }
1048
1049 if (tagsLength > 0) {
1050 pos = Bytes.putAsShort(bytes, pos, tagsLength);
1051 pos = Bytes.putBytes(bytes, pos, tags, tagsOffset, tagsLength);
1052 }
1053 return bytes;
1054 }
1055
1056
1057
1058
1059
1060 private static byte [] createByteArray(final byte [] row, final int roffset,
1061 final int rlength, final byte [] family, final int foffset, int flength,
1062 final Object qualifier, final int qoffset, int qlength,
1063 final long timestamp, final Type type,
1064 final Object value, final int voffset, int vlength, List<Tag> tags) {
1065
1066 checkParameters(row, rlength, family, flength, qlength, vlength);
1067
1068
1069 int tagsLength = 0;
1070 if (tags != null && !tags.isEmpty()) {
1071 for (Tag t : tags) {
1072 tagsLength += t.getLength();
1073 }
1074 }
1075 checkForTagsLength(tagsLength);
1076
1077 int keyLength = (int) getKeyDataStructureSize(rlength, flength, qlength);
1078 byte[] bytes = new byte[(int) getKeyValueDataStructureSize(rlength, flength, qlength, vlength,
1079 tagsLength)];
1080
1081
1082 int pos = 0;
1083 pos = Bytes.putInt(bytes, pos, keyLength);
1084
1085 pos = Bytes.putInt(bytes, pos, vlength);
1086 pos = Bytes.putShort(bytes, pos, (short)(rlength & 0x0000ffff));
1087 pos = Bytes.putBytes(bytes, pos, row, roffset, rlength);
1088 pos = Bytes.putByte(bytes, pos, (byte)(flength & 0x0000ff));
1089 if(flength != 0) {
1090 pos = Bytes.putBytes(bytes, pos, family, foffset, flength);
1091 }
1092 if (qlength > 0) {
1093 if (qualifier instanceof ByteBuffer) {
1094 pos = Bytes.putByteBuffer(bytes, pos, (ByteBuffer) qualifier);
1095 } else {
1096 pos = Bytes.putBytes(bytes, pos, (byte[]) qualifier, qoffset, qlength);
1097 }
1098 }
1099 pos = Bytes.putLong(bytes, pos, timestamp);
1100 pos = Bytes.putByte(bytes, pos, type.getCode());
1101 if (vlength > 0) {
1102 if (value instanceof ByteBuffer) {
1103 pos = Bytes.putByteBuffer(bytes, pos, (ByteBuffer) value);
1104 } else {
1105 pos = Bytes.putBytes(bytes, pos, (byte[]) value, voffset, vlength);
1106 }
1107 }
1108
1109 if (tagsLength > 0) {
1110 pos = Bytes.putAsShort(bytes, pos, tagsLength);
1111 for (Tag t : tags) {
1112 pos = Bytes.putBytes(bytes, pos, t.getBuffer(), t.getOffset(), t.getLength());
1113 }
1114 }
1115 return bytes;
1116 }
1117
1118
1119
1120
1121 @Override
1122 public boolean equals(Object other) {
1123 if (!(other instanceof Cell)) {
1124 return false;
1125 }
1126 return CellComparator.equals(this, (Cell)other);
1127 }
1128
1129
1130
1131
1132 @Override
1133 public int hashCode() {
1134 return CellComparator.hashCodeIgnoreMvcc(this);
1135 }
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148 @Override
1149 public KeyValue clone() throws CloneNotSupportedException {
1150 super.clone();
1151 byte [] b = new byte[this.length];
1152 System.arraycopy(this.bytes, this.offset, b, 0, this.length);
1153 KeyValue ret = new KeyValue(b, 0, b.length);
1154
1155
1156
1157 ret.setSequenceId(seqId);
1158 return ret;
1159 }
1160
1161
1162
1163
1164
1165
1166 public KeyValue shallowCopy() {
1167 KeyValue shallowCopy = new KeyValue(this.bytes, this.offset, this.length);
1168 shallowCopy.setSequenceId(this.seqId);
1169 return shallowCopy;
1170 }
1171
1172
1173
1174
1175
1176
1177
1178 @Override
1179 public String toString() {
1180 if (this.bytes == null || this.bytes.length == 0) {
1181 return "empty";
1182 }
1183 return keyToString(this.bytes, this.offset + ROW_OFFSET, getKeyLength()) + "/vlen="
1184 + getValueLength() + "/seqid=" + seqId;
1185 }
1186
1187
1188
1189
1190
1191 public static String keyToString(final byte [] k) {
1192 if (k == null) {
1193 return "";
1194 }
1195 return keyToString(k, 0, k.length);
1196 }
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206 public Map<String, Object> toStringMap() {
1207 Map<String, Object> stringMap = new HashMap<String, Object>();
1208 stringMap.put("row", Bytes.toStringBinary(getRow()));
1209 stringMap.put("family", Bytes.toStringBinary(getFamily()));
1210 stringMap.put("qualifier", Bytes.toStringBinary(getQualifier()));
1211 stringMap.put("timestamp", getTimestamp());
1212 stringMap.put("vlen", getValueLength());
1213 List<Tag> tags = getTags();
1214 if (tags != null) {
1215 List<String> tagsString = new ArrayList<String>();
1216 for (Tag t : tags) {
1217 tagsString.add((t.getType()) + ":" +Bytes.toStringBinary(t.getValue()));
1218 }
1219 stringMap.put("tag", tagsString);
1220 }
1221 return stringMap;
1222 }
1223
1224
1225
1226
1227
1228
1229
1230
1231 public static String keyToString(final byte [] b, final int o, final int l) {
1232 if (b == null) return "";
1233 int rowlength = Bytes.toShort(b, o);
1234 String row = Bytes.toStringBinary(b, o + Bytes.SIZEOF_SHORT, rowlength);
1235 int columnoffset = o + Bytes.SIZEOF_SHORT + 1 + rowlength;
1236 int familylength = b[columnoffset - 1];
1237 int columnlength = l - ((columnoffset - o) + TIMESTAMP_TYPE_SIZE);
1238 String family = familylength == 0? "":
1239 Bytes.toStringBinary(b, columnoffset, familylength);
1240 String qualifier = columnlength == 0? "":
1241 Bytes.toStringBinary(b, columnoffset + familylength,
1242 columnlength - familylength);
1243 long timestamp = Bytes.toLong(b, o + (l - TIMESTAMP_TYPE_SIZE));
1244 String timestampStr = humanReadableTimestamp(timestamp);
1245 byte type = b[o + l - 1];
1246 return row + "/" + family +
1247 (family != null && family.length() > 0? ":" :"") +
1248 qualifier + "/" + timestampStr + "/" + Type.codeToType(type);
1249 }
1250
1251 public static String humanReadableTimestamp(final long timestamp) {
1252 if (timestamp == HConstants.LATEST_TIMESTAMP) {
1253 return "LATEST_TIMESTAMP";
1254 }
1255 if (timestamp == HConstants.OLDEST_TIMESTAMP) {
1256 return "OLDEST_TIMESTAMP";
1257 }
1258 return String.valueOf(timestamp);
1259 }
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271 @Deprecated
1272 public byte [] getBuffer() {
1273 return this.bytes;
1274 }
1275
1276
1277
1278
1279 public int getOffset() {
1280 return this.offset;
1281 }
1282
1283
1284
1285
1286 public int getLength() {
1287 return length;
1288 }
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303 private static int getLength(byte [] bytes, int offset) {
1304 int klength = ROW_OFFSET + Bytes.toInt(bytes, offset);
1305 int vlength = Bytes.toInt(bytes, offset + Bytes.SIZEOF_INT);
1306 return klength + vlength;
1307 }
1308
1309
1310
1311
1312 public int getKeyOffset() {
1313 return this.offset + ROW_OFFSET;
1314 }
1315
1316 public String getKeyString() {
1317 return Bytes.toStringBinary(getBuffer(), getKeyOffset(), getKeyLength());
1318 }
1319
1320
1321
1322
1323 public int getKeyLength() {
1324 return Bytes.toInt(this.bytes, this.offset);
1325 }
1326
1327
1328
1329
1330 @Override
1331 public byte[] getValueArray() {
1332 return bytes;
1333 }
1334
1335
1336
1337
1338 @Override
1339 public int getValueOffset() {
1340 int voffset = getKeyOffset() + getKeyLength();
1341 return voffset;
1342 }
1343
1344
1345
1346
1347 @Override
1348 public int getValueLength() {
1349 int vlength = Bytes.toInt(this.bytes, this.offset + Bytes.SIZEOF_INT);
1350 return vlength;
1351 }
1352
1353
1354
1355
1356 @Override
1357 public byte[] getRowArray() {
1358 return bytes;
1359 }
1360
1361
1362
1363
1364 @Override
1365 public int getRowOffset() {
1366 return getKeyOffset() + Bytes.SIZEOF_SHORT;
1367 }
1368
1369
1370
1371
1372 @Override
1373 public short getRowLength() {
1374 return Bytes.toShort(this.bytes, getKeyOffset());
1375 }
1376
1377
1378
1379
1380 @Override
1381 public byte[] getFamilyArray() {
1382 return bytes;
1383 }
1384
1385
1386
1387
1388 @Override
1389 public int getFamilyOffset() {
1390 return getFamilyOffset(getRowLength());
1391 }
1392
1393
1394
1395
1396 private int getFamilyOffset(int rlength) {
1397 return this.offset + ROW_OFFSET + Bytes.SIZEOF_SHORT + rlength + Bytes.SIZEOF_BYTE;
1398 }
1399
1400
1401
1402
1403 @Override
1404 public byte getFamilyLength() {
1405 return getFamilyLength(getFamilyOffset());
1406 }
1407
1408
1409
1410
1411 public byte getFamilyLength(int foffset) {
1412 return this.bytes[foffset-1];
1413 }
1414
1415
1416
1417
1418 @Override
1419 public byte[] getQualifierArray() {
1420 return bytes;
1421 }
1422
1423
1424
1425
1426 @Override
1427 public int getQualifierOffset() {
1428 return getQualifierOffset(getFamilyOffset());
1429 }
1430
1431
1432
1433
1434 private int getQualifierOffset(int foffset) {
1435 return foffset + getFamilyLength(foffset);
1436 }
1437
1438
1439
1440
1441 @Override
1442 public int getQualifierLength() {
1443 return getQualifierLength(getRowLength(),getFamilyLength());
1444 }
1445
1446
1447
1448
1449 private int getQualifierLength(int rlength, int flength) {
1450 return getKeyLength() - (int) getKeyDataStructureSize(rlength, flength, 0);
1451 }
1452
1453
1454
1455
1456 public int getTimestampOffset() {
1457 return getTimestampOffset(getKeyLength());
1458 }
1459
1460
1461
1462
1463
1464 private int getTimestampOffset(final int keylength) {
1465 return getKeyOffset() + keylength - TIMESTAMP_TYPE_SIZE;
1466 }
1467
1468
1469
1470
1471 public boolean isLatestTimestamp() {
1472 return Bytes.equals(getBuffer(), getTimestampOffset(), Bytes.SIZEOF_LONG,
1473 HConstants.LATEST_TIMESTAMP_BYTES, 0, Bytes.SIZEOF_LONG);
1474 }
1475
1476
1477
1478
1479
1480
1481 public boolean updateLatestStamp(final byte [] now) {
1482 if (this.isLatestTimestamp()) {
1483 int tsOffset = getTimestampOffset();
1484 System.arraycopy(now, 0, this.bytes, tsOffset, Bytes.SIZEOF_LONG);
1485
1486 return true;
1487 }
1488 return false;
1489 }
1490
1491 @Override
1492 public void setTimestamp(long ts) {
1493 Bytes.putBytes(this.bytes, this.getTimestampOffset(), Bytes.toBytes(ts), 0, Bytes.SIZEOF_LONG);
1494 }
1495
1496 @Override
1497 public void setTimestamp(byte[] ts, int tsOffset) {
1498 Bytes.putBytes(this.bytes, this.getTimestampOffset(), ts, tsOffset, Bytes.SIZEOF_LONG);
1499 }
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514 public byte [] getKey() {
1515 int keylength = getKeyLength();
1516 byte [] key = new byte[keylength];
1517 System.arraycopy(getBuffer(), getKeyOffset(), key, 0, keylength);
1518 return key;
1519 }
1520
1521
1522
1523
1524
1525
1526
1527
1528 @Override
1529 @Deprecated
1530 public byte [] getValue() {
1531 return CellUtil.cloneValue(this);
1532 }
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542 @Override
1543 @Deprecated
1544 public byte [] getRow() {
1545 return CellUtil.cloneRow(this);
1546 }
1547
1548
1549
1550
1551
1552 @Override
1553 public long getTimestamp() {
1554 return getTimestamp(getKeyLength());
1555 }
1556
1557
1558
1559
1560
1561 long getTimestamp(final int keylength) {
1562 int tsOffset = getTimestampOffset(keylength);
1563 return Bytes.toLong(this.bytes, tsOffset);
1564 }
1565
1566
1567
1568
1569 @Deprecated
1570 public byte getType() {
1571 return getTypeByte();
1572 }
1573
1574
1575
1576
1577 @Override
1578 public byte getTypeByte() {
1579 return this.bytes[this.offset + getKeyLength() - 1 + ROW_OFFSET];
1580 }
1581
1582
1583
1584
1585
1586
1587 @Deprecated
1588 public boolean isDelete() {
1589 return KeyValue.isDelete(getType());
1590 }
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600 @Override
1601 @Deprecated
1602 public byte [] getFamily() {
1603 return CellUtil.cloneFamily(this);
1604 }
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615 @Override
1616 @Deprecated
1617 public byte [] getQualifier() {
1618 return CellUtil.cloneQualifier(this);
1619 }
1620
1621
1622
1623
1624 @Override
1625 public int getTagsOffset() {
1626 int tagsLen = getTagsLength();
1627 if (tagsLen == 0) {
1628 return this.offset + this.length;
1629 }
1630 return this.offset + this.length - tagsLen;
1631 }
1632
1633
1634
1635
1636 @Override
1637 public int getTagsLength() {
1638 int tagsLen = this.length - (getKeyLength() + getValueLength() + KEYVALUE_INFRASTRUCTURE_SIZE);
1639 if (tagsLen > 0) {
1640
1641
1642 tagsLen -= TAGS_LENGTH_SIZE;
1643 }
1644 return tagsLen;
1645 }
1646
1647
1648
1649
1650
1651 public List<Tag> getTags() {
1652 int tagsLength = getTagsLength();
1653 if (tagsLength == 0) {
1654 return EMPTY_ARRAY_LIST;
1655 }
1656 return Tag.asList(getTagsArray(), getTagsOffset(), tagsLength);
1657 }
1658
1659
1660
1661
1662 @Override
1663 public byte[] getTagsArray() {
1664 return bytes;
1665 }
1666
1667
1668
1669
1670
1671
1672
1673
1674 public KeyValue createKeyOnly(boolean lenAsVal) {
1675
1676
1677 int dataLen = lenAsVal? Bytes.SIZEOF_INT : 0;
1678 byte [] newBuffer = new byte[getKeyLength() + ROW_OFFSET + dataLen];
1679 System.arraycopy(this.bytes, this.offset, newBuffer, 0,
1680 Math.min(newBuffer.length,this.length));
1681 Bytes.putInt(newBuffer, Bytes.SIZEOF_INT, dataLen);
1682 if (lenAsVal) {
1683 Bytes.putInt(newBuffer, newBuffer.length - dataLen, this.getValueLength());
1684 }
1685 return new KeyValue(newBuffer);
1686 }
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701 public static byte [][] parseColumn(byte [] c) {
1702 final int index = getDelimiter(c, 0, c.length, COLUMN_FAMILY_DELIMITER);
1703 if (index == -1) {
1704
1705 return new byte [][] { c };
1706 } else if(index == c.length - 1) {
1707
1708 byte [] family = new byte[c.length-1];
1709 System.arraycopy(c, 0, family, 0, family.length);
1710 return new byte [][] { family, HConstants.EMPTY_BYTE_ARRAY};
1711 }
1712
1713 final byte [][] result = new byte [2][];
1714 result[0] = new byte [index];
1715 System.arraycopy(c, 0, result[0], 0, index);
1716 final int len = c.length - (index + 1);
1717 result[1] = new byte[len];
1718 System.arraycopy(c, index + 1
1719 return result;
1720 }
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730 public static byte [] makeColumn(byte [] family, byte [] qualifier) {
1731 return Bytes.add(family, COLUMN_FAMILY_DELIM_ARRAY, qualifier);
1732 }
1733
1734
1735
1736
1737
1738
1739
1740 public static int getDelimiter(final byte [] b, int offset, final int length,
1741 final int delimiter) {
1742 if (b == null) {
1743 throw new IllegalArgumentException("Passed buffer is null");
1744 }
1745 int result = -1;
1746 for (int i = offset; i < length + offset; i++) {
1747 if (b[i] == delimiter) {
1748 result = i;
1749 break;
1750 }
1751 }
1752 return result;
1753 }
1754
1755
1756
1757
1758
1759
1760
1761 public static int getDelimiterInReverse(final byte [] b, final int offset,
1762 final int length, final int delimiter) {
1763 if (b == null) {
1764 throw new IllegalArgumentException("Passed buffer is null");
1765 }
1766 int result = -1;
1767 for (int i = (offset + length) - 1; i >= offset; i--) {
1768 if (b[i] == delimiter) {
1769 result = i;
1770 break;
1771 }
1772 }
1773 return result;
1774 }
1775
1776
1777
1778
1779
1780 public static class MetaComparator extends KVComparator {
1781
1782
1783
1784
1785 @Override
1786 public int compare(final Cell left, final Cell right) {
1787 int c = compareRowKey(left, right);
1788 if (c != 0) {
1789 return c;
1790 }
1791 return CellComparator.compareWithoutRow(left, right);
1792 }
1793
1794 @Override
1795 public int compareOnlyKeyPortion(Cell left, Cell right) {
1796 return compare(left, right);
1797 }
1798
1799 @Override
1800 public int compareRows(byte [] left, int loffset, int llength,
1801 byte [] right, int roffset, int rlength) {
1802 int leftDelimiter = getDelimiter(left, loffset, llength,
1803 HConstants.DELIMITER);
1804 int rightDelimiter = getDelimiter(right, roffset, rlength,
1805 HConstants.DELIMITER);
1806
1807 int lpart = (leftDelimiter < 0 ? llength :leftDelimiter - loffset);
1808 int rpart = (rightDelimiter < 0 ? rlength :rightDelimiter - roffset);
1809 int result = Bytes.compareTo(left, loffset, lpart, right, roffset, rpart);
1810 if (result != 0) {
1811 return result;
1812 } else {
1813 if (leftDelimiter < 0 && rightDelimiter >= 0) {
1814 return -1;
1815 } else if (rightDelimiter < 0 && leftDelimiter >= 0) {
1816 return 1;
1817 } else if (leftDelimiter < 0 && rightDelimiter < 0) {
1818 return 0;
1819 }
1820 }
1821
1822
1823 leftDelimiter++;
1824 rightDelimiter++;
1825 int leftFarDelimiter = getDelimiterInReverse(left, leftDelimiter,
1826 llength - (leftDelimiter - loffset), HConstants.DELIMITER);
1827 int rightFarDelimiter = getDelimiterInReverse(right,
1828 rightDelimiter, rlength - (rightDelimiter - roffset),
1829 HConstants.DELIMITER);
1830
1831 lpart = (leftFarDelimiter < 0 ? llength + loffset: leftFarDelimiter) - leftDelimiter;
1832 rpart = (rightFarDelimiter < 0 ? rlength + roffset: rightFarDelimiter)- rightDelimiter;
1833 result = super.compareRows(left, leftDelimiter, lpart, right, rightDelimiter, rpart);
1834 if (result != 0) {
1835 return result;
1836 } else {
1837 if (leftDelimiter < 0 && rightDelimiter >= 0) {
1838 return -1;
1839 } else if (rightDelimiter < 0 && leftDelimiter >= 0) {
1840 return 1;
1841 } else if (leftDelimiter < 0 && rightDelimiter < 0) {
1842 return 0;
1843 }
1844 }
1845
1846 leftFarDelimiter++;
1847 rightFarDelimiter++;
1848 result = Bytes.compareTo(left, leftFarDelimiter, llength - (leftFarDelimiter - loffset),
1849 right, rightFarDelimiter, rlength - (rightFarDelimiter - roffset));
1850 return result;
1851 }
1852
1853
1854
1855
1856 @Override
1857 public byte[] getShortMidpointKey(final byte[] leftKey, final byte[] rightKey) {
1858 return Arrays.copyOf(rightKey, rightKey.length);
1859 }
1860
1861
1862
1863
1864
1865
1866
1867 @Override
1868 public String getLegacyKeyComparatorName() {
1869 return "org.apache.hadoop.hbase.KeyValue$MetaKeyComparator";
1870 }
1871
1872 @Override
1873 protected Object clone() throws CloneNotSupportedException {
1874 return new MetaComparator();
1875 }
1876
1877
1878
1879
1880 @Override
1881 protected int compareRowKey(final Cell l, final Cell r) {
1882 byte[] left = l.getRowArray();
1883 int loffset = l.getRowOffset();
1884 int llength = l.getRowLength();
1885 byte[] right = r.getRowArray();
1886 int roffset = r.getRowOffset();
1887 int rlength = r.getRowLength();
1888 return compareRows(left, loffset, llength, right, roffset, rlength);
1889 }
1890 }
1891
1892
1893
1894
1895
1896
1897 public static class KVComparator implements RawComparator<Cell>, SamePrefixComparator<byte[]> {
1898
1899
1900
1901
1902
1903
1904
1905 public String getLegacyKeyComparatorName() {
1906 return "org.apache.hadoop.hbase.KeyValue$KeyComparator";
1907 }
1908
1909 @Override
1910 public int compare(byte[] l, int loff, int llen, byte[] r, int roff, int rlen) {
1911 return compareFlatKey(l,loff,llen, r,roff,rlen);
1912 }
1913
1914
1915
1916
1917
1918
1919
1920
1921 protected int compareRowKey(final Cell left, final Cell right) {
1922 return CellComparator.compareRows(left, right);
1923 }
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936 public int compareFlatKey(byte[] left, int loffset, int llength,
1937 byte[] right, int roffset, int rlength) {
1938
1939 short lrowlength = Bytes.toShort(left, loffset);
1940 short rrowlength = Bytes.toShort(right, roffset);
1941 int compare = compareRows(left, loffset + Bytes.SIZEOF_SHORT,
1942 lrowlength, right, roffset + Bytes.SIZEOF_SHORT, rrowlength);
1943 if (compare != 0) {
1944 return compare;
1945 }
1946
1947
1948
1949
1950 return compareWithoutRow(0, left, loffset, llength, right, roffset,
1951 rlength, rrowlength);
1952 }
1953
1954 public int compareFlatKey(byte[] left, byte[] right) {
1955 return compareFlatKey(left, 0, left.length, right, 0, right.length);
1956 }
1957
1958
1959 public int compareKey(Cell cell,
1960 byte[] row, int roff, int rlen,
1961 byte[] fam, int foff, int flen,
1962 byte[] col, int coff, int clen,
1963 long ts, byte type) {
1964
1965 int compare = compareRows(
1966 cell.getRowArray(), cell.getRowOffset(), cell.getRowLength(),
1967 row, roff, rlen);
1968 if (compare != 0) {
1969 return compare;
1970 }
1971
1972
1973
1974
1975
1976 if (cell.getFamilyLength() + cell.getQualifierLength() == 0
1977 && cell.getTypeByte() == Type.Minimum.getCode()) {
1978
1979 return 1;
1980 }
1981 if (flen+clen == 0 && type == Type.Minimum.getCode()) {
1982 return -1;
1983 }
1984
1985 compare = compareFamilies(
1986 cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength(),
1987 fam, foff, flen);
1988 if (compare != 0) {
1989 return compare;
1990 }
1991 compare = compareColumns(
1992 cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength(),
1993 col, coff, clen);
1994 if (compare != 0) {
1995 return compare;
1996 }
1997
1998 compare = compareTimestamps(cell.getTimestamp(), ts);
1999 if (compare != 0) {
2000 return compare;
2001 }
2002
2003
2004
2005
2006
2007 return (0xff & type) - (0xff & cell.getTypeByte());
2008 }
2009
2010 public int compareOnlyKeyPortion(Cell left, Cell right) {
2011 return CellComparator.compare(left, right, true);
2012 }
2013
2014
2015
2016
2017
2018 @Override
2019 public int compare(final Cell left, final Cell right) {
2020 int compare = CellComparator.compare(left, right, false);
2021 return compare;
2022 }
2023
2024 public int compareTimestamps(final Cell left, final Cell right) {
2025 return CellComparator.compareTimestamps(left, right);
2026 }
2027
2028
2029
2030
2031
2032
2033 public int compareRows(final Cell left, final Cell right) {
2034 return compareRows(left.getRowArray(),left.getRowOffset(), left.getRowLength(),
2035 right.getRowArray(), right.getRowOffset(), right.getRowLength());
2036 }
2037
2038 public int compareRows(Cell left, byte[] right, int roffset, int rlength) {
2039 return compareRows(left.getRowArray(), left.getRowOffset(), left.getRowLength(), right,
2040 roffset, rlength);
2041 }
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053 public int compareRows(byte [] left, int loffset, int llength,
2054 byte [] right, int roffset, int rlength) {
2055 return Bytes.compareTo(left, loffset, llength, right, roffset, rlength);
2056 }
2057
2058 int compareColumns(final Cell left, final short lrowlength, final Cell right,
2059 final short rrowlength) {
2060 return CellComparator.compareColumns(left, right);
2061 }
2062
2063 protected int compareColumns(
2064 byte [] left, int loffset, int llength, final int lfamilylength,
2065 byte [] right, int roffset, int rlength, final int rfamilylength) {
2066
2067 int diff = Bytes.compareTo(left, loffset, lfamilylength,
2068 right, roffset, rfamilylength);
2069 if (diff != 0) {
2070 return diff;
2071 }
2072
2073 return Bytes.compareTo(left, loffset + lfamilylength,
2074 llength - lfamilylength,
2075 right, roffset + rfamilylength, rlength - rfamilylength);
2076 }
2077
2078 static int compareTimestamps(final long ltimestamp, final long rtimestamp) {
2079
2080
2081
2082
2083 if (ltimestamp < rtimestamp) {
2084 return 1;
2085 } else if (ltimestamp > rtimestamp) {
2086 return -1;
2087 }
2088 return 0;
2089 }
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102 @Override
2103 public int compareIgnoringPrefix(int commonPrefix, byte[] left,
2104 int loffset, int llength, byte[] right, int roffset, int rlength) {
2105
2106 short lrowlength = Bytes.toShort(left, loffset);
2107 short rrowlength;
2108
2109 int comparisonResult = 0;
2110 if (commonPrefix < ROW_LENGTH_SIZE) {
2111
2112 rrowlength = Bytes.toShort(right, roffset);
2113 comparisonResult = compareRows(left, loffset + ROW_LENGTH_SIZE,
2114 lrowlength, right, roffset + ROW_LENGTH_SIZE, rrowlength);
2115 } else {
2116 rrowlength = lrowlength;
2117 if (commonPrefix < ROW_LENGTH_SIZE + rrowlength) {
2118
2119
2120 int common = commonPrefix - ROW_LENGTH_SIZE;
2121 comparisonResult = compareRows(
2122 left, loffset + common + ROW_LENGTH_SIZE, lrowlength - common,
2123 right, roffset + common + ROW_LENGTH_SIZE, rrowlength - common);
2124 }
2125 }
2126 if (comparisonResult != 0) {
2127 return comparisonResult;
2128 }
2129
2130 assert lrowlength == rrowlength;
2131 return compareWithoutRow(commonPrefix, left, loffset, llength, right,
2132 roffset, rlength, lrowlength);
2133 }
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145 private int compareWithoutRow(int commonPrefix, byte[] left, int loffset,
2146 int llength, byte[] right, int roffset, int rlength, short rowlength) {
2147
2148
2149
2150
2151
2152 int commonLength = ROW_LENGTH_SIZE + FAMILY_LENGTH_SIZE + rowlength;
2153
2154
2155 int commonLengthWithTSAndType = TIMESTAMP_TYPE_SIZE + commonLength;
2156
2157 int lcolumnlength = llength - commonLengthWithTSAndType;
2158 int rcolumnlength = rlength - commonLengthWithTSAndType;
2159
2160 byte ltype = left[loffset + (llength - 1)];
2161 byte rtype = right[roffset + (rlength - 1)];
2162
2163
2164
2165
2166
2167
2168 if (lcolumnlength == 0 && ltype == Type.Minimum.getCode()) {
2169
2170 return 1;
2171 }
2172 if (rcolumnlength == 0 && rtype == Type.Minimum.getCode()) {
2173 return -1;
2174 }
2175
2176 int lfamilyoffset = commonLength + loffset;
2177 int rfamilyoffset = commonLength + roffset;
2178
2179
2180 int lfamilylength = left[lfamilyoffset - 1];
2181 int rfamilylength = right[rfamilyoffset - 1];
2182
2183
2184 boolean sameFamilySize = (lfamilylength == rfamilylength);
2185 int common = 0;
2186 if (commonPrefix > 0) {
2187 common = Math.max(0, commonPrefix - commonLength);
2188 if (!sameFamilySize) {
2189
2190
2191 common = Math.min(common, Math.min(lfamilylength, rfamilylength));
2192 } else {
2193 common = Math.min(common, Math.min(lcolumnlength, rcolumnlength));
2194 }
2195 }
2196 if (!sameFamilySize) {
2197
2198 return Bytes.compareTo(left, lfamilyoffset + common, lfamilylength
2199 - common, right, rfamilyoffset + common, rfamilylength - common);
2200 }
2201
2202 final int comparison = Bytes.compareTo(left, lfamilyoffset + common,
2203 lcolumnlength - common, right, rfamilyoffset + common,
2204 rcolumnlength - common);
2205 if (comparison != 0) {
2206 return comparison;
2207 }
2208
2209
2210
2211 long ltimestamp = Bytes.toLong(left,
2212 loffset + (llength - TIMESTAMP_TYPE_SIZE));
2213 long rtimestamp = Bytes.toLong(right,
2214 roffset + (rlength - TIMESTAMP_TYPE_SIZE));
2215 int compare = compareTimestamps(ltimestamp, rtimestamp);
2216 if (compare != 0) {
2217 return compare;
2218 }
2219
2220
2221
2222
2223
2224 return (0xff & rtype) - (0xff & ltype);
2225 }
2226
2227 protected int compareFamilies(final byte[] left, final int loffset, final int lfamilylength,
2228 final byte[] right, final int roffset, final int rfamilylength) {
2229 int diff = Bytes.compareTo(left, loffset, lfamilylength, right, roffset, rfamilylength);
2230 return diff;
2231 }
2232
2233 protected int compareColumns(final byte[] left, final int loffset, final int lquallength,
2234 final byte[] right, final int roffset, final int rquallength) {
2235 int diff = Bytes.compareTo(left, loffset, lquallength, right, roffset, rquallength);
2236 return diff;
2237 }
2238
2239
2240
2241
2242
2243
2244 public boolean matchingRowColumn(final Cell left,
2245 final Cell right) {
2246 short lrowlength = left.getRowLength();
2247 short rrowlength = right.getRowLength();
2248
2249
2250 if ((left.getRowLength() + left.getFamilyLength() + left.getQualifierLength()) != (right
2251 .getRowLength() + right.getFamilyLength() + right.getQualifierLength())) {
2252 return false;
2253 }
2254
2255 if (!matchingRows(left, lrowlength, right, rrowlength)) {
2256 return false;
2257 }
2258
2259 int lfoffset = left.getFamilyOffset();
2260 int rfoffset = right.getFamilyOffset();
2261 int lclength = left.getQualifierLength();
2262 int rclength = right.getQualifierLength();
2263 int lfamilylength = left.getFamilyLength();
2264 int rfamilylength = right.getFamilyLength();
2265 int diff = compareFamilies(left.getFamilyArray(), lfoffset, lfamilylength,
2266 right.getFamilyArray(), rfoffset, rfamilylength);
2267 if (diff != 0) {
2268 return false;
2269 } else {
2270 diff = compareColumns(left.getQualifierArray(), left.getQualifierOffset(), lclength,
2271 right.getQualifierArray(), right.getQualifierOffset(), rclength);
2272 return diff == 0;
2273 }
2274 }
2275
2276
2277
2278
2279
2280
2281
2282 public boolean matchingRows(final Cell left, final Cell right) {
2283 short lrowlength = left.getRowLength();
2284 short rrowlength = right.getRowLength();
2285 return matchingRows(left, lrowlength, right, rrowlength);
2286 }
2287
2288
2289
2290
2291
2292
2293
2294
2295 private boolean matchingRows(final Cell left, final short lrowlength,
2296 final Cell right, final short rrowlength) {
2297 return lrowlength == rrowlength &&
2298 matchingRows(left.getRowArray(), left.getRowOffset(), lrowlength,
2299 right.getRowArray(), right.getRowOffset(), rrowlength);
2300 }
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312 public boolean matchingRows(final byte [] left, final int loffset, final int llength,
2313 final byte [] right, final int roffset, final int rlength) {
2314 return Bytes.equals(left, loffset, llength, right, roffset, rlength);
2315 }
2316
2317 public byte[] calcIndexKey(byte[] lastKeyOfPreviousBlock, byte[] firstKeyInBlock) {
2318 byte[] fakeKey = getShortMidpointKey(lastKeyOfPreviousBlock, firstKeyInBlock);
2319 if (compareFlatKey(fakeKey, firstKeyInBlock) > 0) {
2320 LOG.error("Unexpected getShortMidpointKey result, fakeKey:"
2321 + Bytes.toStringBinary(fakeKey) + ", firstKeyInBlock:"
2322 + Bytes.toStringBinary(firstKeyInBlock));
2323 return firstKeyInBlock;
2324 }
2325 if (lastKeyOfPreviousBlock != null && compareFlatKey(lastKeyOfPreviousBlock, fakeKey) >= 0) {
2326 LOG.error("Unexpected getShortMidpointKey result, lastKeyOfPreviousBlock:" +
2327 Bytes.toStringBinary(lastKeyOfPreviousBlock) + ", fakeKey:" +
2328 Bytes.toStringBinary(fakeKey));
2329 return firstKeyInBlock;
2330 }
2331 return fakeKey;
2332 }
2333
2334
2335
2336
2337
2338
2339
2340
2341 @Deprecated
2342 public byte[] getShortMidpointKey(final byte[] leftKey, final byte[] rightKey) {
2343 if (rightKey == null) {
2344 throw new IllegalArgumentException("rightKey can not be null");
2345 }
2346 if (leftKey == null) {
2347 return Arrays.copyOf(rightKey, rightKey.length);
2348 }
2349 if (compareFlatKey(leftKey, rightKey) >= 0) {
2350 throw new IllegalArgumentException("Unexpected input, leftKey:" + Bytes.toString(leftKey)
2351 + ", rightKey:" + Bytes.toString(rightKey));
2352 }
2353
2354 short leftRowLength = Bytes.toShort(leftKey, 0);
2355 short rightRowLength = Bytes.toShort(rightKey, 0);
2356 int leftCommonLength = ROW_LENGTH_SIZE + FAMILY_LENGTH_SIZE + leftRowLength;
2357 int rightCommonLength = ROW_LENGTH_SIZE + FAMILY_LENGTH_SIZE + rightRowLength;
2358 int leftCommonLengthWithTSAndType = TIMESTAMP_TYPE_SIZE + leftCommonLength;
2359 int rightCommonLengthWithTSAndType = TIMESTAMP_TYPE_SIZE + rightCommonLength;
2360 int leftColumnLength = leftKey.length - leftCommonLengthWithTSAndType;
2361 int rightColumnLength = rightKey.length - rightCommonLengthWithTSAndType;
2362
2363 if (leftRowLength == rightRowLength && compareRows(leftKey, ROW_LENGTH_SIZE, leftRowLength,
2364 rightKey, ROW_LENGTH_SIZE, rightRowLength) == 0) {
2365
2366 int comparison = Bytes.compareTo(leftKey, leftCommonLength, leftColumnLength, rightKey,
2367 rightCommonLength, rightColumnLength);
2368
2369 if (comparison == 0) {
2370 return Arrays.copyOf(rightKey, rightKey.length);
2371 }
2372
2373 byte[] newKey = Arrays.copyOf(rightKey, rightKey.length);
2374 Bytes.putLong(newKey, rightKey.length - TIMESTAMP_TYPE_SIZE, HConstants.LATEST_TIMESTAMP);
2375 Bytes.putByte(newKey, rightKey.length - TYPE_SIZE, Type.Maximum.getCode());
2376 return newKey;
2377 }
2378
2379 short minLength = leftRowLength < rightRowLength ? leftRowLength : rightRowLength;
2380 short diffIdx = 0;
2381 while (diffIdx < minLength
2382 && leftKey[ROW_LENGTH_SIZE + diffIdx] == rightKey[ROW_LENGTH_SIZE + diffIdx]) {
2383 diffIdx++;
2384 }
2385 byte[] newRowKey = null;
2386 if (diffIdx >= minLength) {
2387
2388 newRowKey = new byte[diffIdx + 1];
2389 System.arraycopy(rightKey, ROW_LENGTH_SIZE, newRowKey, 0, diffIdx + 1);
2390 } else {
2391 int diffByte = leftKey[ROW_LENGTH_SIZE + diffIdx];
2392 if ((0xff & diffByte) < 0xff && (diffByte + 1) <
2393 (rightKey[ROW_LENGTH_SIZE + diffIdx] & 0xff)) {
2394 newRowKey = new byte[diffIdx + 1];
2395 System.arraycopy(leftKey, ROW_LENGTH_SIZE, newRowKey, 0, diffIdx);
2396 newRowKey[diffIdx] = (byte) (diffByte + 1);
2397 } else {
2398 newRowKey = new byte[diffIdx + 1];
2399 System.arraycopy(rightKey, ROW_LENGTH_SIZE, newRowKey, 0, diffIdx + 1);
2400 }
2401 }
2402 return new KeyValue(newRowKey, null, null, HConstants.LATEST_TIMESTAMP,
2403 Type.Maximum).getKey();
2404 }
2405
2406 @Override
2407 protected Object clone() throws CloneNotSupportedException {
2408 super.clone();
2409 return new KVComparator();
2410 }
2411
2412 }
2413
2414
2415
2416
2417
2418
2419 public static KeyValue createKeyValueFromKey(final byte [] b) {
2420 return createKeyValueFromKey(b, 0, b.length);
2421 }
2422
2423
2424
2425
2426
2427
2428 public static KeyValue createKeyValueFromKey(final ByteBuffer bb) {
2429 return createKeyValueFromKey(bb.array(), bb.arrayOffset(), bb.limit());
2430 }
2431
2432
2433
2434
2435
2436
2437
2438
2439 public static KeyValue createKeyValueFromKey(final byte [] b, final int o,
2440 final int l) {
2441 byte [] newb = new byte[l + ROW_OFFSET];
2442 System.arraycopy(b, o, newb, ROW_OFFSET, l);
2443 Bytes.putInt(newb, 0, l);
2444 Bytes.putInt(newb, Bytes.SIZEOF_INT, 0);
2445 return new KeyValue(newb);
2446 }
2447
2448
2449
2450
2451
2452
2453
2454
2455 public static KeyValue create(final DataInput in) throws IOException {
2456 return create(in.readInt(), in);
2457 }
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467 public static KeyValue create(int length, final DataInput in) throws IOException {
2468
2469 if (length <= 0) {
2470 if (length == 0) return null;
2471 throw new IOException("Failed read " + length + " bytes, stream corrupt?");
2472 }
2473
2474
2475 byte [] bytes = new byte[length];
2476 in.readFully(bytes);
2477 return new KeyValue(bytes, 0, length);
2478 }
2479
2480
2481
2482
2483
2484
2485
2486 public static KeyValue cloneAndAddTags(Cell c, List<Tag> newTags) {
2487 List<Tag> existingTags = null;
2488 if(c.getTagsLength() > 0) {
2489 existingTags = Tag.asList(c.getTagsArray(), c.getTagsOffset(), c.getTagsLength());
2490 existingTags.addAll(newTags);
2491 } else {
2492 existingTags = newTags;
2493 }
2494 return new KeyValue(c.getRowArray(), c.getRowOffset(), (int)c.getRowLength(),
2495 c.getFamilyArray(), c.getFamilyOffset(), (int)c.getFamilyLength(),
2496 c.getQualifierArray(), c.getQualifierOffset(), (int) c.getQualifierLength(),
2497 c.getTimestamp(), Type.codeToType(c.getTypeByte()), c.getValueArray(), c.getValueOffset(),
2498 c.getValueLength(), existingTags);
2499 }
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509 @Deprecated
2510 public static KeyValue iscreate(final InputStream in) throws IOException {
2511 return KeyValueUtil.iscreate(in, true);
2512 }
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522 public static long write(final KeyValue kv, final DataOutput out) throws IOException {
2523
2524
2525 int length = kv.getLength();
2526 out.writeInt(length);
2527 out.write(kv.getBuffer(), kv.getOffset(), length);
2528 return length + Bytes.SIZEOF_INT;
2529 }
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543 @Deprecated
2544 public static long oswrite(final KeyValue kv, final OutputStream out)
2545 throws IOException {
2546 int length = kv.getLength();
2547
2548 out.write(Bytes.toBytes(length));
2549 out.write(kv.getBuffer(), kv.getOffset(), length);
2550 return length + Bytes.SIZEOF_INT;
2551 }
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566 public static long oswrite(final KeyValue kv, final OutputStream out, final boolean withTags)
2567 throws IOException {
2568
2569
2570 int length = kv.getLength();
2571 if (!withTags) {
2572 length = kv.getKeyLength() + kv.getValueLength() + KEYVALUE_INFRASTRUCTURE_SIZE;
2573 }
2574
2575 StreamUtils.writeInt(out, length);
2576 out.write(kv.getBuffer(), kv.getOffset(), length);
2577 return length + Bytes.SIZEOF_INT;
2578 }
2579
2580
2581
2582
2583 public static class RowOnlyComparator implements Comparator<KeyValue> {
2584 final KVComparator comparator;
2585
2586 public RowOnlyComparator(final KVComparator c) {
2587 this.comparator = c;
2588 }
2589
2590 @Override
2591 public int compare(KeyValue left, KeyValue right) {
2592 return comparator.compareRows(left, right);
2593 }
2594 }
2595
2596
2597
2598
2599
2600
2601
2602 public interface SamePrefixComparator<T> {
2603
2604
2605
2606
2607 int compareIgnoringPrefix(int commonPrefix, byte[] left, int loffset, int llength,
2608 byte[] right, int roffset, int rlength
2609 );
2610 }
2611
2612
2613
2614
2615 public static class RawBytesComparator extends KVComparator {
2616
2617
2618
2619
2620
2621
2622 @Override
2623 public String getLegacyKeyComparatorName() {
2624 return "org.apache.hadoop.hbase.util.Bytes$ByteArrayComparator";
2625 }
2626
2627
2628
2629
2630 @Override
2631 @Deprecated
2632 public int compareFlatKey(byte[] left, int loffset, int llength, byte[] right,
2633 int roffset, int rlength) {
2634 return Bytes.BYTES_RAWCOMPARATOR.compare(left, loffset, llength, right, roffset, rlength);
2635 }
2636
2637 @Override
2638 public int compare(Cell left, Cell right) {
2639 return compareOnlyKeyPortion(left, right);
2640 }
2641
2642 @Override
2643 @VisibleForTesting
2644 public int compareOnlyKeyPortion(Cell left, Cell right) {
2645 int c = Bytes.BYTES_RAWCOMPARATOR.compare(left.getRowArray(), left.getRowOffset(),
2646 left.getRowLength(), right.getRowArray(), right.getRowOffset(), right.getRowLength());
2647 if (c != 0) {
2648 return c;
2649 }
2650 c = Bytes.BYTES_RAWCOMPARATOR.compare(left.getFamilyArray(), left.getFamilyOffset(),
2651 left.getFamilyLength(), right.getFamilyArray(), right.getFamilyOffset(),
2652 right.getFamilyLength());
2653 if (c != 0) {
2654 return c;
2655 }
2656 c = Bytes.BYTES_RAWCOMPARATOR.compare(left.getQualifierArray(), left.getQualifierOffset(),
2657 left.getQualifierLength(), right.getQualifierArray(), right.getQualifierOffset(),
2658 right.getQualifierLength());
2659 if (c != 0) {
2660 return c;
2661 }
2662 c = compareTimestamps(left.getTimestamp(), right.getTimestamp());
2663 if (c != 0) {
2664 return c;
2665 }
2666 return (0xff & left.getTypeByte()) - (0xff & right.getTypeByte());
2667 }
2668
2669 @Override
2670 public byte[] calcIndexKey(byte[] lastKeyOfPreviousBlock, byte[] firstKeyInBlock) {
2671 return firstKeyInBlock;
2672 }
2673
2674 }
2675
2676
2677
2678
2679
2680
2681
2682 @Override
2683 public long heapSize() {
2684 int sum = 0;
2685 sum += ClassSize.OBJECT;
2686 sum += ClassSize.REFERENCE;
2687 sum += ClassSize.align(ClassSize.ARRAY);
2688 sum += ClassSize.align(length);
2689 sum += 2 * Bytes.SIZEOF_INT;
2690 sum += Bytes.SIZEOF_LONG;
2691 return ClassSize.align(sum);
2692 }
2693
2694
2695
2696
2697
2698
2699
2700
2701 @Deprecated
2702 public long heapSizeWithoutTags() {
2703 int sum = 0;
2704 sum += ClassSize.OBJECT;
2705 sum += ClassSize.REFERENCE;
2706 sum += ClassSize.align(ClassSize.ARRAY);
2707 sum += KeyValue.KEYVALUE_INFRASTRUCTURE_SIZE;
2708 sum += getKeyLength();
2709 sum += getValueLength();
2710 sum += 2 * Bytes.SIZEOF_INT;
2711 sum += Bytes.SIZEOF_LONG;
2712 return ClassSize.align(sum);
2713 }
2714
2715
2716
2717
2718
2719
2720
2721 public static class KeyOnlyKeyValue extends KeyValue {
2722 public KeyOnlyKeyValue() {
2723
2724 }
2725 public KeyOnlyKeyValue(byte[] b) {
2726 this(b, 0, b.length);
2727 }
2728
2729 public KeyOnlyKeyValue(byte[] b, int offset, int length) {
2730 this.bytes = b;
2731 this.length = length;
2732 this.offset = offset;
2733 }
2734
2735 @Override
2736 public int getKeyOffset() {
2737 return this.offset;
2738 }
2739
2740
2741
2742
2743
2744
2745
2746
2747 public void setKey(byte[] key, int offset, int length) {
2748 this.bytes = key;
2749 this.offset = offset;
2750 this.length = length;
2751 }
2752
2753 @Override
2754 public byte[] getKey() {
2755 int keylength = getKeyLength();
2756 byte[] key = new byte[keylength];
2757 System.arraycopy(this.bytes, getKeyOffset(), key, 0, keylength);
2758 return key;
2759 }
2760
2761 @Override
2762 public byte[] getRowArray() {
2763 return bytes;
2764 }
2765
2766 @Override
2767 public int getRowOffset() {
2768 return getKeyOffset() + Bytes.SIZEOF_SHORT;
2769 }
2770
2771 @Override
2772 public byte[] getFamilyArray() {
2773 return bytes;
2774 }
2775
2776 @Override
2777 public byte getFamilyLength() {
2778 return this.bytes[getFamilyOffset() - 1];
2779 }
2780
2781 @Override
2782 public int getFamilyOffset() {
2783 return this.offset + Bytes.SIZEOF_SHORT + getRowLength() + Bytes.SIZEOF_BYTE;
2784 }
2785
2786 @Override
2787 public byte[] getQualifierArray() {
2788 return bytes;
2789 }
2790
2791 @Override
2792 public int getQualifierLength() {
2793 return getQualifierLength(getRowLength(), getFamilyLength());
2794 }
2795
2796 @Override
2797 public int getQualifierOffset() {
2798 return getFamilyOffset() + getFamilyLength();
2799 }
2800
2801 @Override
2802 public int getKeyLength() {
2803 return length;
2804 }
2805
2806 @Override
2807 public short getRowLength() {
2808 return Bytes.toShort(this.bytes, getKeyOffset());
2809 }
2810
2811 @Override
2812 public byte getTypeByte() {
2813 return this.bytes[this.offset + getKeyLength() - 1];
2814 }
2815
2816 private int getQualifierLength(int rlength, int flength) {
2817 return getKeyLength() - (int) getKeyDataStructureSize(rlength, flength, 0);
2818 }
2819
2820 @Override
2821 public long getTimestamp() {
2822 int tsOffset = getTimestampOffset();
2823 return Bytes.toLong(this.bytes, tsOffset);
2824 }
2825
2826 @Override
2827 public int getTimestampOffset() {
2828 return getKeyOffset() + getKeyLength() - TIMESTAMP_TYPE_SIZE;
2829 }
2830
2831 @Override
2832 public byte[] getTagsArray() {
2833 return HConstants.EMPTY_BYTE_ARRAY;
2834 }
2835
2836 @Override
2837 public int getTagsOffset() {
2838 return 0;
2839 }
2840
2841 @Override
2842 public byte[] getValueArray() {
2843 throw new IllegalArgumentException("KeyOnlyKeyValue does not work with values.");
2844 }
2845
2846 @Override
2847 public int getValueOffset() {
2848 throw new IllegalArgumentException("KeyOnlyKeyValue does not work with values.");
2849 }
2850
2851 @Override
2852 public int getValueLength() {
2853 throw new IllegalArgumentException("KeyOnlyKeyValue does not work with values.");
2854 }
2855
2856 @Override
2857 public int getTagsLength() {
2858 return 0;
2859 }
2860
2861 @Override
2862 public String toString() {
2863 if (this.bytes == null || this.bytes.length == 0) {
2864 return "empty";
2865 }
2866 return keyToString(this.bytes, this.offset, getKeyLength()) + "/vlen=0/mvcc=0";
2867 }
2868
2869 @Override
2870 public int hashCode() {
2871 return super.hashCode();
2872 }
2873
2874 @Override
2875 public boolean equals(Object other) {
2876 return super.equals(other);
2877 }
2878 }
2879 }