Skip to content

Commit d0dd3c8

Browse files
add backupmeta decoder
Signed-off-by: marsishandsome <marsishandsome@gmail.com>
1 parent e0c00f6 commit d0dd3c8

11 files changed

+252
-63
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
*
3+
* Copyright 2021 PingCAP, Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.tikv.br;
19+
20+
import org.rocksdb.Options;
21+
import org.rocksdb.ReadOptions;
22+
import org.tikv.kvproto.BackupOuterClass;
23+
24+
public class BackupDecoder {
25+
private final BackupOuterClass.BackupMeta backupMeta;
26+
27+
public BackupDecoder(BackupOuterClass.BackupMeta backupMeta) {
28+
this.backupMeta = backupMeta;
29+
}
30+
31+
private KVDecoder getKVDecoder() {
32+
// Currently only v1 is supported.
33+
// V2 will be added after https://github.com/tikv/tikv/issues/10938.
34+
return RawKVDecoderV1.INSTANCE;
35+
}
36+
37+
public SSTDecoder decodeSST(String sstFilePath) {
38+
return decodeSST(sstFilePath, new Options(), new ReadOptions());
39+
}
40+
41+
public SSTDecoder decodeSST(String sstFilePath, Options options, ReadOptions readOptions) {
42+
return new SSTDecoder(sstFilePath, getKVDecoder(), options, readOptions);
43+
}
44+
45+
public BackupOuterClass.BackupMeta getBackupMeta() {
46+
return backupMeta;
47+
}
48+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
*
3+
* Copyright 2021 PingCAP, Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.tikv.br;
19+
20+
import com.google.protobuf.InvalidProtocolBufferException;
21+
import java.io.File;
22+
import java.io.IOException;
23+
import java.nio.file.Files;
24+
import org.tikv.kvproto.BackupOuterClass;
25+
26+
public class BackupMetaDecoder {
27+
private final BackupOuterClass.BackupMeta backupMeta;
28+
29+
public BackupMetaDecoder(byte[] data) throws InvalidProtocolBufferException {
30+
this.backupMeta = BackupOuterClass.BackupMeta.parseFrom(data);
31+
}
32+
33+
public BackupOuterClass.BackupMeta getBackupMeta() {
34+
return backupMeta;
35+
}
36+
37+
public static BackupMetaDecoder parse(String backupMetaFilePath) throws IOException {
38+
byte[] data = Files.readAllBytes(new File(backupMetaFilePath).toPath());
39+
return new BackupMetaDecoder(data);
40+
}
41+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
*
3+
* Copyright 2021 PingCAP, Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.tikv.br;
19+
20+
import com.google.protobuf.ByteString;
21+
22+
public interface KVDecoder {
23+
ByteString decodeKey(byte[] key);
24+
25+
ByteString decodeValue(byte[] value);
26+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
*
3+
* Copyright 2021 PingCAP, Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.tikv.br;
19+
20+
import com.google.protobuf.ByteString;
21+
import java.util.Arrays;
22+
import org.slf4j.Logger;
23+
import org.slf4j.LoggerFactory;
24+
25+
public class RawKVDecoderV1 implements KVDecoder {
26+
private static final Logger logger = LoggerFactory.getLogger(SSTIterator.class);
27+
28+
public static final RawKVDecoderV1 INSTANCE = new RawKVDecoderV1();
29+
30+
private RawKVDecoderV1() {}
31+
32+
@Override
33+
public ByteString decodeKey(byte[] key) {
34+
if (key == null || key.length == 0) {
35+
logger.warn(
36+
"skip Key-Value pair because key == null || key.length == 0, key = "
37+
+ Arrays.toString(key));
38+
return null;
39+
} else if (key[0] != 'z') {
40+
logger.warn("skip Key-Value pair because key[0] != 'z', key = " + Arrays.toString(key));
41+
return null;
42+
}
43+
return ByteString.copyFrom(key, 1, key.length - 1);
44+
}
45+
46+
@Override
47+
public ByteString decodeValue(byte[] value) {
48+
return ByteString.copyFrom(value);
49+
}
50+
}

src/main/java/org/tikv/sst/SSTDecoder.java renamed to src/main/java/org/tikv/br/SSTDecoder.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
*/
1717

18-
package org.tikv.sst;
18+
package org.tikv.br;
1919

2020
import com.google.protobuf.ByteString;
2121
import java.util.Iterator;
@@ -28,20 +28,24 @@
2828

2929
public class SSTDecoder {
3030
private final String filePath;
31+
private final KVDecoder kvDecoder;
3132
private final Options options;
3233
private final ReadOptions readOptions;
3334

3435
private SstFileReader sstFileReader;
3536
private SstFileReaderIterator iterator;
3637

37-
public SSTDecoder(String filePath) {
38-
this.filePath = filePath;
38+
public SSTDecoder(String sstFilePath, KVDecoder kvDecoder) {
39+
this.filePath = sstFilePath;
40+
this.kvDecoder = kvDecoder;
3941
this.options = new Options();
4042
this.readOptions = new ReadOptions();
4143
}
4244

43-
public SSTDecoder(String filePath, Options options, ReadOptions readOptions) {
45+
public SSTDecoder(
46+
String filePath, KVDecoder kvDecoder, Options options, ReadOptions readOptions) {
4447
this.filePath = filePath;
48+
this.kvDecoder = kvDecoder;
4549
this.options = options;
4650
this.readOptions = readOptions;
4751
}
@@ -54,7 +58,7 @@ public synchronized Iterator<Pair<ByteString, ByteString>> getIterator() throws
5458
sstFileReader = new SstFileReader(new Options());
5559
sstFileReader.open(filePath);
5660
iterator = sstFileReader.newIterator(new ReadOptions());
57-
return new SSTIterator(iterator);
61+
return new SSTIterator(iterator, kvDecoder);
5862
}
5963

6064
public synchronized void close() {

src/main/java/org/tikv/sst/SSTIterator.java renamed to src/main/java/org/tikv/br/SSTIterator.java

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,22 @@
1515
*
1616
*/
1717

18-
package org.tikv.sst;
18+
package org.tikv.br;
1919

2020
import com.google.protobuf.ByteString;
21-
import java.util.Arrays;
2221
import java.util.Iterator;
2322
import org.rocksdb.SstFileReaderIterator;
24-
import org.slf4j.Logger;
25-
import org.slf4j.LoggerFactory;
2623
import org.tikv.common.util.Pair;
2724

2825
public class SSTIterator implements Iterator<Pair<ByteString, ByteString>> {
29-
private static final Logger logger = LoggerFactory.getLogger(SSTIterator.class);
30-
3126
private final SstFileReaderIterator iterator;
27+
private final KVDecoder kvDecoder;
3228

3329
private Pair<ByteString, ByteString> nextPair;
3430

35-
public SSTIterator(SstFileReaderIterator iterator) {
31+
public SSTIterator(SstFileReaderIterator iterator, KVDecoder kvDecoder) {
3632
this.iterator = iterator;
33+
this.kvDecoder = kvDecoder;
3734
this.iterator.seekToFirst();
3835
this.nextPair = processNext();
3936
}
@@ -52,8 +49,8 @@ public Pair<ByteString, ByteString> next() {
5249

5350
private Pair<ByteString, ByteString> processNext() {
5451
if (iterator.isValid()) {
55-
ByteString key = decodeKey(iterator.key());
56-
ByteString value = decodeValue(iterator.value());
52+
ByteString key = kvDecoder.decodeKey(iterator.key());
53+
ByteString value = kvDecoder.decodeValue(iterator.value());
5754
iterator.next();
5855
if (key != null) {
5956
return Pair.create(key, value);
@@ -64,21 +61,4 @@ private Pair<ByteString, ByteString> processNext() {
6461
return null;
6562
}
6663
}
67-
68-
private ByteString decodeKey(byte[] key) {
69-
if (key == null || key.length == 0) {
70-
logger.warn(
71-
"skip Key-Value pair because key == null || key.length == 0, key = "
72-
+ Arrays.toString(key));
73-
return null;
74-
} else if (key[0] != 'z') {
75-
logger.warn("skip Key-Value pair because key[0] != 'z', key = " + Arrays.toString(key));
76-
return null;
77-
}
78-
return ByteString.copyFrom(key, 1, key.length - 1);
79-
}
80-
81-
private ByteString decodeValue(byte[] value) {
82-
return ByteString.copyFrom(value);
83-
}
8464
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.tikv.br;
2+
3+
import com.google.protobuf.ByteString;
4+
import java.io.File;
5+
import java.io.IOException;
6+
import java.util.Iterator;
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
import org.rocksdb.RocksDBException;
10+
import org.tikv.common.util.Pair;
11+
import org.tikv.kvproto.BackupOuterClass;
12+
13+
public class BackupDecoderTest {
14+
15+
private static final int TOTAL_COUNT = 500;
16+
private static final String KEY_PREFIX = "test_";
17+
private static final String VALUE =
18+
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
19+
20+
private static final String backupmetaFilePath = "src/test/resources/sst/backupmeta";
21+
private static final String sst1FilePath =
22+
"src/test/resources/sst/1_2_2_7154800cc311f03afd1532e961b9a878dfbb119b104cf4daad5d0c7c0eacb502_1633919546277_default.sst";
23+
private static final String sst2FilePath =
24+
"src/test/resources/sst/4_8_2_9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08_1633919546278_default.sst";
25+
26+
@Test
27+
public void rawKVSSTDecoderTest() throws RocksDBException, IOException {
28+
BackupMetaDecoder backupMetaDecoder = BackupMetaDecoder.parse(backupmetaFilePath);
29+
BackupOuterClass.BackupMeta backupMeta = backupMetaDecoder.getBackupMeta();
30+
31+
BackupDecoder sstBackup = new BackupDecoder(backupMeta);
32+
33+
decodeSST(sstBackup, sst1FilePath);
34+
decodeSST(sstBackup, sst2FilePath);
35+
}
36+
37+
private void decodeSST(BackupDecoder sstBackup, String sst) throws RocksDBException {
38+
String fileName = new File(sst).getName();
39+
BackupOuterClass.File backupFile =
40+
sstBackup
41+
.getBackupMeta()
42+
.getFilesList()
43+
.stream()
44+
.filter(a -> a.getName().equals(fileName))
45+
.findFirst()
46+
.get();
47+
Assert.assertEquals(TOTAL_COUNT, backupFile.getTotalKvs());
48+
49+
SSTDecoder sstDecoder = sstBackup.decodeSST(sst);
50+
Iterator<Pair<ByteString, ByteString>> iterator = sstDecoder.getIterator();
51+
int count = 0;
52+
while (iterator.hasNext()) {
53+
Pair<ByteString, ByteString> pair = iterator.next();
54+
Assert.assertEquals(VALUE, pair.second.toStringUtf8());
55+
Assert.assertTrue(pair.first.toStringUtf8().startsWith(KEY_PREFIX));
56+
count += 1;
57+
}
58+
sstDecoder.close();
59+
Assert.assertEquals(TOTAL_COUNT, count);
60+
}
61+
}

src/test/java/org/tikv/sst/SSTDecoderTest.java

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/test/resources/rawkv.sst renamed to src/test/resources/sst/1_2_2_7154800cc311f03afd1532e961b9a878dfbb119b104cf4daad5d0c7c0eacb502_1633919546277_default.sst

File renamed without changes.

0 commit comments

Comments
 (0)