]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/java/src/test/java/org/rocksdb/SstFileWriterTest.java
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / rocksdb / java / src / test / java / org / rocksdb / SstFileWriterTest.java
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.
5
6 package org.rocksdb;
7
8 import org.junit.ClassRule;
9 import org.junit.Rule;
10 import org.junit.Test;
11 import org.junit.rules.TemporaryFolder;
12 import org.rocksdb.util.BytewiseComparator;
13
14 import java.io.File;
15 import java.io.IOException;
16 import java.util.Map;
17 import java.util.TreeMap;
18
19 import static org.assertj.core.api.Assertions.assertThat;
20
21 public class SstFileWriterTest {
22 private static final String SST_FILE_NAME = "test.sst";
23 private static final String DB_DIRECTORY_NAME = "test_db";
24
25 @ClassRule
26 public static final RocksMemoryResource rocksMemoryResource = new RocksMemoryResource();
27
28 @Rule public TemporaryFolder parentFolder = new TemporaryFolder();
29
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);
43 } else {
44 sstFileWriter = new SstFileWriter(envOptions, options);
45 }
46
47 final File sstFile = parentFolder.newFile(SST_FILE_NAME);
48 try {
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);
54 keySlice.close();
55 valueSlice.close();
56 }
57 sstFileWriter.finish();
58 } finally {
59 assertThat(sstFileWriter).isNotNull();
60 sstFileWriter.close();
61 options.close();
62 envOptions.close();
63 if (comparatorOptions != null) {
64 comparatorOptions.close();
65 }
66 if (comparator != null) {
67 comparator.close();
68 }
69 }
70 return sstFile;
71 }
72
73 @Test
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);
79 }
80
81 @Test
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);
87 }
88
89 @Test
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());
99
100 assertThat(db.get("key1".getBytes())).isEqualTo("value1".getBytes());
101 assertThat(db.get("key2".getBytes())).isEqualTo("value2".getBytes());
102
103 options.close();
104 db.close();
105 }
106 }