1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under the BSD-style license found in the
3 // LICENSE file in the root directory of this source tree. An additional grant
4 // of patent rights can be found in the PATENTS file in the same directory.
8 import org
.junit
.ClassRule
;
10 import org
.junit
.Test
;
11 import org
.junit
.rules
.TemporaryFolder
;
12 import org
.rocksdb
.util
.BytewiseComparator
;
15 import java
.io
.IOException
;
17 import java
.util
.TreeMap
;
19 import static org
.assertj
.core
.api
.Assertions
.assertThat
;
21 public class SstFileWriterTest
{
22 private static final String SST_FILE_NAME
= "test.sst";
23 private static final String DB_DIRECTORY_NAME
= "test_db";
26 public static final RocksMemoryResource rocksMemoryResource
= new RocksMemoryResource();
28 @Rule public TemporaryFolder parentFolder
= new TemporaryFolder();
30 private File
newSstFile(final TreeMap
<String
, String
> keyValues
,
31 boolean useJavaBytewiseComparator
)
32 throws IOException
, RocksDBException
{
33 final EnvOptions envOptions
= new EnvOptions();
34 final Options options
= new Options();
35 SstFileWriter sstFileWriter
= null;
36 ComparatorOptions comparatorOptions
= null;
37 BytewiseComparator comparator
= null;
38 if (useJavaBytewiseComparator
) {
39 comparatorOptions
= new ComparatorOptions();
40 comparator
= new BytewiseComparator(comparatorOptions
);
41 options
.setComparator(comparator
);
42 sstFileWriter
= new SstFileWriter(envOptions
, options
, comparator
);
44 sstFileWriter
= new SstFileWriter(envOptions
, options
);
47 final File sstFile
= parentFolder
.newFile(SST_FILE_NAME
);
49 sstFileWriter
.open(sstFile
.getAbsolutePath());
50 for (Map
.Entry
<String
, String
> keyValue
: keyValues
.entrySet()) {
51 Slice keySlice
= new Slice(keyValue
.getKey());
52 Slice valueSlice
= new Slice(keyValue
.getValue());
53 sstFileWriter
.add(keySlice
, valueSlice
);
57 sstFileWriter
.finish();
59 assertThat(sstFileWriter
).isNotNull();
60 sstFileWriter
.close();
63 if (comparatorOptions
!= null) {
64 comparatorOptions
.close();
66 if (comparator
!= null) {
74 public void generateSstFileWithJavaComparator() throws RocksDBException
, IOException
{
75 final TreeMap
<String
, String
> keyValues
= new TreeMap
<>();
76 keyValues
.put("key1", "value1");
77 keyValues
.put("key2", "value2");
78 newSstFile(keyValues
, true);
82 public void generateSstFileWithNativeComparator() throws RocksDBException
, IOException
{
83 final TreeMap
<String
, String
> keyValues
= new TreeMap
<>();
84 keyValues
.put("key1", "value1");
85 keyValues
.put("key2", "value2");
86 newSstFile(keyValues
, false);
90 public void ingestSstFile() throws RocksDBException
, IOException
{
91 final TreeMap
<String
, String
> keyValues
= new TreeMap
<>();
92 keyValues
.put("key1", "value1");
93 keyValues
.put("key2", "value2");
94 final File sstFile
= newSstFile(keyValues
, false);
95 final File dbFolder
= parentFolder
.newFolder(DB_DIRECTORY_NAME
);
96 final Options options
= new Options().setCreateIfMissing(true);
97 final RocksDB db
= RocksDB
.open(options
, dbFolder
.getAbsolutePath());
98 db
.addFileWithFilePath(sstFile
.getAbsolutePath());
100 assertThat(db
.get("key1".getBytes())).isEqualTo("value1".getBytes());
101 assertThat(db
.get("key2".getBytes())).isEqualTo("value2".getBytes());