Skip to content

Commit 24e15c0

Browse files
authored
HBASE-24194 : Refactor anonymous inner classes of BufferedEncodedSeeker to named inner classes (#1522)
Signed-off-by: Peter Somogyi <[email protected]> Signed-off-by: binlijin <[email protected]>
1 parent 1f66806 commit 24e15c0

File tree

4 files changed

+325
-293
lines changed

4 files changed

+325
-293
lines changed

hbase-common/src/main/java/org/apache/hadoop/hbase/io/encoding/CopyKeyDataBlockEncoder.java

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -81,35 +81,7 @@ public String toString() {
8181

8282
@Override
8383
public EncodedSeeker createSeeker(final HFileBlockDecodingContext decodingCtx) {
84-
return new BufferedEncodedSeeker<SeekerState>(decodingCtx) {
85-
@Override
86-
protected void decodeNext() {
87-
current.keyLength = currentBuffer.getInt();
88-
current.valueLength = currentBuffer.getInt();
89-
current.ensureSpaceForKey();
90-
currentBuffer.get(current.keyBuffer, 0, current.keyLength);
91-
current.valueOffset = currentBuffer.position();
92-
currentBuffer.skip(current.valueLength);
93-
if (includesTags()) {
94-
// Read short as unsigned, high byte first
95-
current.tagsLength = ((currentBuffer.get() & 0xff) << 8) ^ (currentBuffer.get() & 0xff);
96-
currentBuffer.skip(current.tagsLength);
97-
}
98-
if (includesMvcc()) {
99-
current.memstoreTS = ByteBufferUtils.readVLong(currentBuffer);
100-
} else {
101-
current.memstoreTS = 0;
102-
}
103-
current.nextKvOffset = currentBuffer.position();
104-
}
105-
106-
@Override
107-
protected void decodeFirst() {
108-
currentBuffer.skip(Bytes.SIZEOF_INT);
109-
current.lastCommonPrefix = 0;
110-
decodeNext();
111-
}
112-
};
84+
return new SeekerStateBufferedEncodedSeeker(decodingCtx);
11385
}
11486

11587
@Override
@@ -123,4 +95,41 @@ protected ByteBuffer internalDecodeKeyValues(DataInputStream source, int allocat
12395

12496
return buffer;
12597
}
98+
99+
private static class SeekerStateBufferedEncodedSeeker
100+
extends BufferedEncodedSeeker<SeekerState> {
101+
102+
private SeekerStateBufferedEncodedSeeker(HFileBlockDecodingContext decodingCtx) {
103+
super(decodingCtx);
104+
}
105+
106+
@Override
107+
protected void decodeNext() {
108+
current.keyLength = currentBuffer.getInt();
109+
current.valueLength = currentBuffer.getInt();
110+
current.ensureSpaceForKey();
111+
currentBuffer.get(current.keyBuffer, 0, current.keyLength);
112+
current.valueOffset = currentBuffer.position();
113+
currentBuffer.skip(current.valueLength);
114+
if (includesTags()) {
115+
// Read short as unsigned, high byte first
116+
current.tagsLength = ((currentBuffer.get() & 0xff) << 8) ^ (currentBuffer.get() & 0xff);
117+
currentBuffer.skip(current.tagsLength);
118+
}
119+
if (includesMvcc()) {
120+
current.memstoreTS = ByteBufferUtils.readVLong(currentBuffer);
121+
} else {
122+
current.memstoreTS = 0;
123+
}
124+
current.nextKvOffset = currentBuffer.position();
125+
}
126+
127+
@Override
128+
protected void decodeFirst() {
129+
currentBuffer.skip(Bytes.SIZEOF_INT);
130+
current.lastCommonPrefix = 0;
131+
decodeNext();
132+
}
133+
}
134+
126135
}

hbase-common/src/main/java/org/apache/hadoop/hbase/io/encoding/DiffKeyDeltaEncoder.java

Lines changed: 130 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -381,129 +381,7 @@ protected void copyFromNext(SeekerState that) {
381381

382382
@Override
383383
public EncodedSeeker createSeeker(HFileBlockDecodingContext decodingCtx) {
384-
return new BufferedEncodedSeeker<DiffSeekerState>(decodingCtx) {
385-
private byte[] familyNameWithSize;
386-
private static final int TIMESTAMP_WITH_TYPE_LENGTH =
387-
Bytes.SIZEOF_LONG + Bytes.SIZEOF_BYTE;
388-
389-
private void decode(boolean isFirst) {
390-
byte flag = currentBuffer.get();
391-
byte type = 0;
392-
if ((flag & FLAG_SAME_KEY_LENGTH) == 0) {
393-
if (!isFirst) {
394-
type = current.keyBuffer[current.keyLength - Bytes.SIZEOF_BYTE];
395-
}
396-
current.keyLength = ByteBuff.readCompressedInt(currentBuffer);
397-
}
398-
if ((flag & FLAG_SAME_VALUE_LENGTH) == 0) {
399-
current.valueLength = ByteBuff.readCompressedInt(currentBuffer);
400-
}
401-
current.lastCommonPrefix = ByteBuff.readCompressedInt(currentBuffer);
402-
403-
current.ensureSpaceForKey();
404-
405-
if (current.lastCommonPrefix < Bytes.SIZEOF_SHORT) {
406-
// length of row is different, copy everything except family
407-
408-
// copy the row size
409-
currentBuffer.get(current.keyBuffer, current.lastCommonPrefix,
410-
Bytes.SIZEOF_SHORT - current.lastCommonPrefix);
411-
current.rowLengthWithSize = Bytes.toShort(current.keyBuffer, 0) +
412-
Bytes.SIZEOF_SHORT;
413-
414-
// copy the rest of row
415-
currentBuffer.get(current.keyBuffer, Bytes.SIZEOF_SHORT,
416-
current.rowLengthWithSize - Bytes.SIZEOF_SHORT);
417-
418-
// copy the column family
419-
System.arraycopy(familyNameWithSize, 0, current.keyBuffer,
420-
current.rowLengthWithSize, familyNameWithSize.length);
421-
422-
// copy the qualifier
423-
currentBuffer.get(current.keyBuffer,
424-
current.rowLengthWithSize + familyNameWithSize.length,
425-
current.keyLength - current.rowLengthWithSize -
426-
familyNameWithSize.length - TIMESTAMP_WITH_TYPE_LENGTH);
427-
} else if (current.lastCommonPrefix < current.rowLengthWithSize) {
428-
// we have to copy part of row and qualifier,
429-
// but column family is in right place
430-
431-
// before column family (rest of row)
432-
currentBuffer.get(current.keyBuffer, current.lastCommonPrefix,
433-
current.rowLengthWithSize - current.lastCommonPrefix);
434-
435-
// after column family (qualifier)
436-
currentBuffer.get(current.keyBuffer,
437-
current.rowLengthWithSize + familyNameWithSize.length,
438-
current.keyLength - current.rowLengthWithSize -
439-
familyNameWithSize.length - TIMESTAMP_WITH_TYPE_LENGTH);
440-
} else {
441-
// copy just the ending
442-
currentBuffer.get(current.keyBuffer, current.lastCommonPrefix,
443-
current.keyLength - TIMESTAMP_WITH_TYPE_LENGTH -
444-
current.lastCommonPrefix);
445-
}
446-
447-
// timestamp
448-
int pos = current.keyLength - TIMESTAMP_WITH_TYPE_LENGTH;
449-
int timestampFitInBytes = 1 +
450-
((flag & MASK_TIMESTAMP_LENGTH) >>> SHIFT_TIMESTAMP_LENGTH);
451-
long timestampOrDiff = ByteBuff.readLong(currentBuffer, timestampFitInBytes);
452-
if ((flag & FLAG_TIMESTAMP_SIGN) != 0) {
453-
timestampOrDiff = -timestampOrDiff;
454-
}
455-
if ((flag & FLAG_TIMESTAMP_IS_DIFF) == 0) { // it is timestamp
456-
current.timestamp = timestampOrDiff;
457-
} else { // it is diff
458-
current.timestamp = current.timestamp - timestampOrDiff;
459-
}
460-
Bytes.putLong(current.keyBuffer, pos, current.timestamp);
461-
pos += Bytes.SIZEOF_LONG;
462-
463-
// type
464-
if ((flag & FLAG_SAME_TYPE) == 0) {
465-
currentBuffer.get(current.keyBuffer, pos, Bytes.SIZEOF_BYTE);
466-
} else if ((flag & FLAG_SAME_KEY_LENGTH) == 0) {
467-
current.keyBuffer[pos] = type;
468-
}
469-
470-
current.valueOffset = currentBuffer.position();
471-
currentBuffer.skip(current.valueLength);
472-
473-
if (includesTags()) {
474-
decodeTags();
475-
}
476-
if (includesMvcc()) {
477-
current.memstoreTS = ByteBufferUtils.readVLong(currentBuffer);
478-
} else {
479-
current.memstoreTS = 0;
480-
}
481-
current.nextKvOffset = currentBuffer.position();
482-
}
483-
484-
@Override
485-
protected void decodeFirst() {
486-
currentBuffer.skip(Bytes.SIZEOF_INT);
487-
488-
// read column family
489-
byte familyNameLength = currentBuffer.get();
490-
familyNameWithSize = new byte[familyNameLength + Bytes.SIZEOF_BYTE];
491-
familyNameWithSize[0] = familyNameLength;
492-
currentBuffer.get(familyNameWithSize, Bytes.SIZEOF_BYTE,
493-
familyNameLength);
494-
decode(true);
495-
}
496-
497-
@Override
498-
protected void decodeNext() {
499-
decode(false);
500-
}
501-
502-
@Override
503-
protected DiffSeekerState createSeekerState() {
504-
return new DiffSeekerState(this.tmpPair, this.includesTags());
505-
}
506-
};
384+
return new DiffSeekerStateBufferedEncodedSeeker(decodingCtx);
507385
}
508386

509387
@Override
@@ -525,4 +403,133 @@ protected ByteBuffer internalDecodeKeyValues(DataInputStream source, int allocat
525403

526404
return buffer;
527405
}
406+
407+
private static class DiffSeekerStateBufferedEncodedSeeker
408+
extends BufferedEncodedSeeker<DiffSeekerState> {
409+
private byte[] familyNameWithSize;
410+
private static final int TIMESTAMP_WITH_TYPE_LENGTH =
411+
Bytes.SIZEOF_LONG + Bytes.SIZEOF_BYTE;
412+
413+
private DiffSeekerStateBufferedEncodedSeeker(HFileBlockDecodingContext decodingCtx) {
414+
super(decodingCtx);
415+
}
416+
417+
private void decode(boolean isFirst) {
418+
byte flag = currentBuffer.get();
419+
byte type = 0;
420+
if ((flag & FLAG_SAME_KEY_LENGTH) == 0) {
421+
if (!isFirst) {
422+
type = current.keyBuffer[current.keyLength - Bytes.SIZEOF_BYTE];
423+
}
424+
current.keyLength = ByteBuff.readCompressedInt(currentBuffer);
425+
}
426+
if ((flag & FLAG_SAME_VALUE_LENGTH) == 0) {
427+
current.valueLength = ByteBuff.readCompressedInt(currentBuffer);
428+
}
429+
current.lastCommonPrefix = ByteBuff.readCompressedInt(currentBuffer);
430+
431+
current.ensureSpaceForKey();
432+
433+
if (current.lastCommonPrefix < Bytes.SIZEOF_SHORT) {
434+
// length of row is different, copy everything except family
435+
436+
// copy the row size
437+
currentBuffer.get(current.keyBuffer, current.lastCommonPrefix,
438+
Bytes.SIZEOF_SHORT - current.lastCommonPrefix);
439+
current.rowLengthWithSize = Bytes.toShort(current.keyBuffer, 0) +
440+
Bytes.SIZEOF_SHORT;
441+
442+
// copy the rest of row
443+
currentBuffer.get(current.keyBuffer, Bytes.SIZEOF_SHORT,
444+
current.rowLengthWithSize - Bytes.SIZEOF_SHORT);
445+
446+
// copy the column family
447+
System.arraycopy(familyNameWithSize, 0, current.keyBuffer,
448+
current.rowLengthWithSize, familyNameWithSize.length);
449+
450+
// copy the qualifier
451+
currentBuffer.get(current.keyBuffer,
452+
current.rowLengthWithSize + familyNameWithSize.length,
453+
current.keyLength - current.rowLengthWithSize -
454+
familyNameWithSize.length - TIMESTAMP_WITH_TYPE_LENGTH);
455+
} else if (current.lastCommonPrefix < current.rowLengthWithSize) {
456+
// we have to copy part of row and qualifier,
457+
// but column family is in right place
458+
459+
// before column family (rest of row)
460+
currentBuffer.get(current.keyBuffer, current.lastCommonPrefix,
461+
current.rowLengthWithSize - current.lastCommonPrefix);
462+
463+
// after column family (qualifier)
464+
currentBuffer.get(current.keyBuffer,
465+
current.rowLengthWithSize + familyNameWithSize.length,
466+
current.keyLength - current.rowLengthWithSize -
467+
familyNameWithSize.length - TIMESTAMP_WITH_TYPE_LENGTH);
468+
} else {
469+
// copy just the ending
470+
currentBuffer.get(current.keyBuffer, current.lastCommonPrefix,
471+
current.keyLength - TIMESTAMP_WITH_TYPE_LENGTH -
472+
current.lastCommonPrefix);
473+
}
474+
475+
// timestamp
476+
int pos = current.keyLength - TIMESTAMP_WITH_TYPE_LENGTH;
477+
int timestampFitInBytes = 1 +
478+
((flag & MASK_TIMESTAMP_LENGTH) >>> SHIFT_TIMESTAMP_LENGTH);
479+
long timestampOrDiff = ByteBuff.readLong(currentBuffer, timestampFitInBytes);
480+
if ((flag & FLAG_TIMESTAMP_SIGN) != 0) {
481+
timestampOrDiff = -timestampOrDiff;
482+
}
483+
if ((flag & FLAG_TIMESTAMP_IS_DIFF) == 0) { // it is timestamp
484+
current.timestamp = timestampOrDiff;
485+
} else { // it is diff
486+
current.timestamp = current.timestamp - timestampOrDiff;
487+
}
488+
Bytes.putLong(current.keyBuffer, pos, current.timestamp);
489+
pos += Bytes.SIZEOF_LONG;
490+
491+
// type
492+
if ((flag & FLAG_SAME_TYPE) == 0) {
493+
currentBuffer.get(current.keyBuffer, pos, Bytes.SIZEOF_BYTE);
494+
} else if ((flag & FLAG_SAME_KEY_LENGTH) == 0) {
495+
current.keyBuffer[pos] = type;
496+
}
497+
498+
current.valueOffset = currentBuffer.position();
499+
currentBuffer.skip(current.valueLength);
500+
501+
if (includesTags()) {
502+
decodeTags();
503+
}
504+
if (includesMvcc()) {
505+
current.memstoreTS = ByteBufferUtils.readVLong(currentBuffer);
506+
} else {
507+
current.memstoreTS = 0;
508+
}
509+
current.nextKvOffset = currentBuffer.position();
510+
}
511+
512+
@Override
513+
protected void decodeFirst() {
514+
currentBuffer.skip(Bytes.SIZEOF_INT);
515+
516+
// read column family
517+
byte familyNameLength = currentBuffer.get();
518+
familyNameWithSize = new byte[familyNameLength + Bytes.SIZEOF_BYTE];
519+
familyNameWithSize[0] = familyNameLength;
520+
currentBuffer.get(familyNameWithSize, Bytes.SIZEOF_BYTE,
521+
familyNameLength);
522+
decode(true);
523+
}
524+
525+
@Override
526+
protected void decodeNext() {
527+
decode(false);
528+
}
529+
530+
@Override
531+
protected DiffSeekerState createSeekerState() {
532+
return new DiffSeekerState(this.tmpPair, this.includesTags());
533+
}
534+
}
528535
}

0 commit comments

Comments
 (0)