1 // Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2 //  This source code is licensed under both the GPLv2 (found in the
3 //  COPYING file in the root directory) and Apache 2.0 License
4 //  (found in the LICENSE.Apache file in the root directory).
5 
6 import java.lang.IllegalArgumentException;
7 import java.util.Arrays;
8 import java.util.List;
9 import java.util.Map;
10 import java.util.ArrayList;
11 
12 import org.rocksdb.*;
13 import org.rocksdb.util.SizeUnit;
14 
15 public class RocksDBSample {
16   static {
RocksDB.loadLibrary()17     RocksDB.loadLibrary();
18   }
19 
main(final String[] args)20   public static void main(final String[] args) {
21     if (args.length < 1) {
22       System.out.println("usage: RocksDBSample db_path");
23       System.exit(-1);
24     }
25 
26     final String db_path = args[0];
27     final String db_path_not_found = db_path + "_not_found";
28 
29     System.out.println("RocksDBSample");
30     try (final Options options = new Options();
31          final Filter bloomFilter = new BloomFilter(10);
32          final ReadOptions readOptions = new ReadOptions()
33              .setFillCache(false);
34          final Statistics stats = new Statistics();
35          final RateLimiter rateLimiter = new RateLimiter(10000000,10000, 10)) {
36 
37       try (final RocksDB db = RocksDB.open(options, db_path_not_found)) {
38         assert (false);
39       } catch (final RocksDBException e) {
40         System.out.format("Caught the expected exception -- %s\n", e);
41       }
42 
43       try {
44         options.setCreateIfMissing(true)
45             .setStatistics(stats)
46             .setWriteBufferSize(8 * SizeUnit.KB)
47             .setMaxWriteBufferNumber(3)
48             .setMaxBackgroundCompactions(10)
49             .setCompressionType(CompressionType.SNAPPY_COMPRESSION)
50             .setCompactionStyle(CompactionStyle.UNIVERSAL);
51       } catch (final IllegalArgumentException e) {
52         assert (false);
53       }
54 
55       assert (options.createIfMissing() == true);
56       assert (options.writeBufferSize() == 8 * SizeUnit.KB);
57       assert (options.maxWriteBufferNumber() == 3);
58       assert (options.maxBackgroundCompactions() == 10);
59       assert (options.compressionType() == CompressionType.SNAPPY_COMPRESSION);
60       assert (options.compactionStyle() == CompactionStyle.UNIVERSAL);
61 
62       assert (options.memTableFactoryName().equals("SkipListFactory"));
63       options.setMemTableConfig(
64           new HashSkipListMemTableConfig()
65               .setHeight(4)
66               .setBranchingFactor(4)
67               .setBucketCount(2000000));
68       assert (options.memTableFactoryName().equals("HashSkipListRepFactory"));
69 
70       options.setMemTableConfig(
71           new HashLinkedListMemTableConfig()
72               .setBucketCount(100000));
73       assert (options.memTableFactoryName().equals("HashLinkedListRepFactory"));
74 
75       options.setMemTableConfig(
76           new VectorMemTableConfig().setReservedSize(10000));
77       assert (options.memTableFactoryName().equals("VectorRepFactory"));
78 
79       options.setMemTableConfig(new SkipListMemTableConfig());
80       assert (options.memTableFactoryName().equals("SkipListFactory"));
81 
82       options.setTableFormatConfig(new PlainTableConfig());
83       // Plain-Table requires mmap read
84       options.setAllowMmapReads(true);
85       assert (options.tableFactoryName().equals("PlainTable"));
86 
87       options.setRateLimiter(rateLimiter);
88 
89       final BlockBasedTableConfig table_options = new BlockBasedTableConfig();
90       table_options.setBlockCacheSize(64 * SizeUnit.KB)
91           .setFilter(bloomFilter)
92           .setCacheNumShardBits(6)
93           .setBlockSizeDeviation(5)
94           .setBlockRestartInterval(10)
95           .setCacheIndexAndFilterBlocks(true)
96           .setHashIndexAllowCollision(false)
97           .setBlockCacheCompressedSize(64 * SizeUnit.KB)
98           .setBlockCacheCompressedNumShardBits(10);
99 
100       assert (table_options.blockCacheSize() == 64 * SizeUnit.KB);
101       assert (table_options.cacheNumShardBits() == 6);
102       assert (table_options.blockSizeDeviation() == 5);
103       assert (table_options.blockRestartInterval() == 10);
104       assert (table_options.cacheIndexAndFilterBlocks() == true);
105       assert (table_options.hashIndexAllowCollision() == false);
106       assert (table_options.blockCacheCompressedSize() == 64 * SizeUnit.KB);
107       assert (table_options.blockCacheCompressedNumShardBits() == 10);
108 
109       options.setTableFormatConfig(table_options);
110       assert (options.tableFactoryName().equals("BlockBasedTable"));
111 
112       try (final RocksDB db = RocksDB.open(options, db_path)) {
113         db.put("hello".getBytes(), "world".getBytes());
114 
115         final byte[] value = db.get("hello".getBytes());
116         assert ("world".equals(new String(value)));
117 
118         final String str = db.getProperty("rocksdb.stats");
119         assert (str != null && !str.equals(""));
120       } catch (final RocksDBException e) {
121         System.out.format("[ERROR] caught the unexpected exception -- %s\n", e);
122         assert (false);
123       }
124 
125       try (final RocksDB db = RocksDB.open(options, db_path)) {
126         db.put("hello".getBytes(), "world".getBytes());
127         byte[] value = db.get("hello".getBytes());
128         System.out.format("Get('hello') = %s\n",
129             new String(value));
130 
131         for (int i = 1; i <= 9; ++i) {
132           for (int j = 1; j <= 9; ++j) {
133             db.put(String.format("%dx%d", i, j).getBytes(),
134                 String.format("%d", i * j).getBytes());
135           }
136         }
137 
138         for (int i = 1; i <= 9; ++i) {
139           for (int j = 1; j <= 9; ++j) {
140             System.out.format("%s ", new String(db.get(
141                 String.format("%dx%d", i, j).getBytes())));
142           }
143           System.out.println("");
144         }
145 
146         // write batch test
147         try (final WriteOptions writeOpt = new WriteOptions()) {
148           for (int i = 10; i <= 19; ++i) {
149             try (final WriteBatch batch = new WriteBatch()) {
150               for (int j = 10; j <= 19; ++j) {
151                 batch.put(String.format("%dx%d", i, j).getBytes(),
152                     String.format("%d", i * j).getBytes());
153               }
154               db.write(writeOpt, batch);
155             }
156           }
157         }
158         for (int i = 10; i <= 19; ++i) {
159           for (int j = 10; j <= 19; ++j) {
160             assert (new String(
161                 db.get(String.format("%dx%d", i, j).getBytes())).equals(
162                 String.format("%d", i * j)));
163             System.out.format("%s ", new String(db.get(
164                 String.format("%dx%d", i, j).getBytes())));
165           }
166           System.out.println("");
167         }
168 
169         value = db.get("1x1".getBytes());
170         assert (value != null);
171         value = db.get("world".getBytes());
172         assert (value == null);
173         value = db.get(readOptions, "world".getBytes());
174         assert (value == null);
175 
176         final byte[] testKey = "asdf".getBytes();
177         final byte[] testValue =
178             "asdfghjkl;'?><MNBVCXZQWERTYUIOP{+_)(*&^%$#@".getBytes();
179         db.put(testKey, testValue);
180         byte[] testResult = db.get(testKey);
181         assert (testResult != null);
182         assert (Arrays.equals(testValue, testResult));
183         assert (new String(testValue).equals(new String(testResult)));
184         testResult = db.get(readOptions, testKey);
185         assert (testResult != null);
186         assert (Arrays.equals(testValue, testResult));
187         assert (new String(testValue).equals(new String(testResult)));
188 
189         final byte[] insufficientArray = new byte[10];
190         final byte[] enoughArray = new byte[50];
191         int len;
192         len = db.get(testKey, insufficientArray);
193         assert (len > insufficientArray.length);
194         len = db.get("asdfjkl;".getBytes(), enoughArray);
195         assert (len == RocksDB.NOT_FOUND);
196         len = db.get(testKey, enoughArray);
197         assert (len == testValue.length);
198 
199         len = db.get(readOptions, testKey, insufficientArray);
200         assert (len > insufficientArray.length);
201         len = db.get(readOptions, "asdfjkl;".getBytes(), enoughArray);
202         assert (len == RocksDB.NOT_FOUND);
203         len = db.get(readOptions, testKey, enoughArray);
204         assert (len == testValue.length);
205 
206         db.remove(testKey);
207         len = db.get(testKey, enoughArray);
208         assert (len == RocksDB.NOT_FOUND);
209 
210         // repeat the test with WriteOptions
211         try (final WriteOptions writeOpts = new WriteOptions()) {
212           writeOpts.setSync(true);
213           writeOpts.setDisableWAL(true);
214           db.put(writeOpts, testKey, testValue);
215           len = db.get(testKey, enoughArray);
216           assert (len == testValue.length);
217           assert (new String(testValue).equals(
218               new String(enoughArray, 0, len)));
219         }
220 
221         try {
222           for (final TickerType statsType : TickerType.values()) {
223             if (statsType != TickerType.TICKER_ENUM_MAX) {
224               stats.getTickerCount(statsType);
225             }
226           }
227           System.out.println("getTickerCount() passed.");
228         } catch (final Exception e) {
229           System.out.println("Failed in call to getTickerCount()");
230           assert (false); //Should never reach here.
231         }
232 
233         try {
234           for (final HistogramType histogramType : HistogramType.values()) {
235             if (histogramType != HistogramType.HISTOGRAM_ENUM_MAX) {
236               HistogramData data = stats.getHistogramData(histogramType);
237             }
238           }
239           System.out.println("getHistogramData() passed.");
240         } catch (final Exception e) {
241           System.out.println("Failed in call to getHistogramData()");
242           assert (false); //Should never reach here.
243         }
244 
245         try (final RocksIterator iterator = db.newIterator()) {
246 
247           boolean seekToFirstPassed = false;
248           for (iterator.seekToFirst(); iterator.isValid(); iterator.next()) {
249             iterator.status();
250             assert (iterator.key() != null);
251             assert (iterator.value() != null);
252             seekToFirstPassed = true;
253           }
254           if (seekToFirstPassed) {
255             System.out.println("iterator seekToFirst tests passed.");
256           }
257 
258           boolean seekToLastPassed = false;
259           for (iterator.seekToLast(); iterator.isValid(); iterator.prev()) {
260             iterator.status();
261             assert (iterator.key() != null);
262             assert (iterator.value() != null);
263             seekToLastPassed = true;
264           }
265 
266           if (seekToLastPassed) {
267             System.out.println("iterator seekToLastPassed tests passed.");
268           }
269 
270           iterator.seekToFirst();
271           iterator.seek(iterator.key());
272           assert (iterator.key() != null);
273           assert (iterator.value() != null);
274 
275           System.out.println("iterator seek test passed.");
276 
277         }
278         System.out.println("iterator tests passed.");
279 
280         final List<byte[]> keys = new ArrayList<>();
281         try (final RocksIterator iterator = db.newIterator()) {
282           for (iterator.seekToLast(); iterator.isValid(); iterator.prev()) {
283             keys.add(iterator.key());
284           }
285         }
286 
287         Map<byte[], byte[]> values = db.multiGet(keys);
288         assert (values.size() == keys.size());
289         for (final byte[] value1 : values.values()) {
290           assert (value1 != null);
291         }
292 
293         values = db.multiGet(new ReadOptions(), keys);
294         assert (values.size() == keys.size());
295         for (final byte[] value1 : values.values()) {
296           assert (value1 != null);
297         }
298       } catch (final RocksDBException e) {
299         System.err.println(e);
300       }
301     }
302   }
303 }
304