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 package org.rocksdb;
7 
8 import java.util.ArrayList;
9 import java.util.List;
10 
11 public class OptionsUtil {
12   /**
13    * A static method to construct the DBOptions and ColumnFamilyDescriptors by
14    * loading the latest RocksDB options file stored in the specified rocksdb
15    * database.
16    *
17    * Note that the all the pointer options (except table_factory, which will
18    * be described in more details below) will be initialized with the default
19    * values.  Developers can further initialize them after this function call.
20    * Below is an example list of pointer options which will be initialized.
21    *
22    * - env
23    * - memtable_factory
24    * - compaction_filter_factory
25    * - prefix_extractor
26    * - comparator
27    * - merge_operator
28    * - compaction_filter
29    *
30    * For table_factory, this function further supports deserializing
31    * BlockBasedTableFactory and its BlockBasedTableOptions except the
32    * pointer options of BlockBasedTableOptions (flush_block_policy_factory,
33    * block_cache, and block_cache_compressed), which will be initialized with
34    * default values.  Developers can further specify these three options by
35    * casting the return value of TableFactoroy::GetOptions() to
36    * BlockBasedTableOptions and making necessary changes.
37    *
38    * @param dbPath the path to the RocksDB.
39    * @param env {@link org.rocksdb.Env} instance.
40    * @param dbOptions {@link org.rocksdb.DBOptions} instance. This will be
41    *     filled and returned.
42    * @param cfDescs A list of {@link org.rocksdb.ColumnFamilyDescriptor}'s be
43    *    returned.
44    *
45    * @throws RocksDBException thrown if error happens in underlying
46    *     native library.
47    */
48 
loadLatestOptions(String dbPath, Env env, DBOptions dbOptions, List<ColumnFamilyDescriptor> cfDescs)49   public static void loadLatestOptions(String dbPath, Env env, DBOptions dbOptions,
50       List<ColumnFamilyDescriptor> cfDescs) throws RocksDBException {
51     loadLatestOptions(dbPath, env, dbOptions, cfDescs, false);
52   }
53 
54   /**
55    * @param dbPath the path to the RocksDB.
56    * @param env {@link org.rocksdb.Env} instance.
57    * @param dbOptions {@link org.rocksdb.DBOptions} instance. This will be
58    *     filled and returned.
59    * @param cfDescs A list of {@link org.rocksdb.ColumnFamilyDescriptor}'s be
60    *     returned.
61    * @param ignoreUnknownOptions this flag can be set to true if you want to
62    *     ignore options that are from a newer version of the db, esentially for
63    *     forward compatibility.
64    *
65    * @throws RocksDBException thrown if error happens in underlying
66    *     native library.
67    */
loadLatestOptions(String dbPath, Env env, DBOptions dbOptions, List<ColumnFamilyDescriptor> cfDescs, boolean ignoreUnknownOptions)68   public static void loadLatestOptions(String dbPath, Env env, DBOptions dbOptions,
69       List<ColumnFamilyDescriptor> cfDescs, boolean ignoreUnknownOptions) throws RocksDBException {
70     loadLatestOptions(
71         dbPath, env.nativeHandle_, dbOptions.nativeHandle_, cfDescs, ignoreUnknownOptions);
72   }
73 
74   /**
75    * Similar to LoadLatestOptions, this function constructs the DBOptions
76    * and ColumnFamilyDescriptors based on the specified RocksDB Options file.
77    * See LoadLatestOptions above.
78    *
79    * @param optionsFileName the RocksDB options file path.
80    * @param env {@link org.rocksdb.Env} instance.
81    * @param dbOptions {@link org.rocksdb.DBOptions} instance. This will be
82    *     filled and returned.
83    * @param cfDescs A list of {@link org.rocksdb.ColumnFamilyDescriptor}'s be
84    *     returned.
85    *
86    * @throws RocksDBException thrown if error happens in underlying
87    *     native library.
88    */
loadOptionsFromFile(String optionsFileName, Env env, DBOptions dbOptions, List<ColumnFamilyDescriptor> cfDescs)89   public static void loadOptionsFromFile(String optionsFileName, Env env, DBOptions dbOptions,
90       List<ColumnFamilyDescriptor> cfDescs) throws RocksDBException {
91     loadOptionsFromFile(optionsFileName, env, dbOptions, cfDescs, false);
92   }
93 
94   /**
95    * @param optionsFileName the RocksDB options file path.
96    * @param env {@link org.rocksdb.Env} instance.
97    * @param dbOptions {@link org.rocksdb.DBOptions} instance. This will be
98    *     filled and returned.
99    * @param cfDescs A list of {@link org.rocksdb.ColumnFamilyDescriptor}'s be
100    *     returned.
101    * @param ignoreUnknownOptions this flag can be set to true if you want to
102    *     ignore options that are from a newer version of the db, esentially for
103    *     forward compatibility.
104    *
105    * @throws RocksDBException thrown if error happens in underlying
106    *     native library.
107    */
loadOptionsFromFile(String optionsFileName, Env env, DBOptions dbOptions, List<ColumnFamilyDescriptor> cfDescs, boolean ignoreUnknownOptions)108   public static void loadOptionsFromFile(String optionsFileName, Env env, DBOptions dbOptions,
109       List<ColumnFamilyDescriptor> cfDescs, boolean ignoreUnknownOptions) throws RocksDBException {
110     loadOptionsFromFile(
111         optionsFileName, env.nativeHandle_, dbOptions.nativeHandle_, cfDescs, ignoreUnknownOptions);
112   }
113 
114   /**
115    * Returns the latest options file name under the specified RocksDB path.
116    *
117    * @param dbPath the path to the RocksDB.
118    * @param env {@link org.rocksdb.Env} instance.
119    * @return the latest options file name under the db path.
120    *
121    * @throws RocksDBException thrown if error happens in underlying
122    *     native library.
123    */
getLatestOptionsFileName(String dbPath, Env env)124   public static String getLatestOptionsFileName(String dbPath, Env env) throws RocksDBException {
125     return getLatestOptionsFileName(dbPath, env.nativeHandle_);
126   }
127 
128   /**
129    * Private constructor.
130    * This class has only static methods and shouldn't be instantiated.
131    */
OptionsUtil()132   private OptionsUtil() {}
133 
134   // native methods
loadLatestOptions(String dbPath, long envHandle, long dbOptionsHandle, List<ColumnFamilyDescriptor> cfDescs, boolean ignoreUnknownOptions)135   private native static void loadLatestOptions(String dbPath, long envHandle, long dbOptionsHandle,
136       List<ColumnFamilyDescriptor> cfDescs, boolean ignoreUnknownOptions) throws RocksDBException;
loadOptionsFromFile(String optionsFileName, long envHandle, long dbOptionsHandle, List<ColumnFamilyDescriptor> cfDescs, boolean ignoreUnknownOptions)137   private native static void loadOptionsFromFile(String optionsFileName, long envHandle,
138       long dbOptionsHandle, List<ColumnFamilyDescriptor> cfDescs, boolean ignoreUnknownOptions)
139       throws RocksDBException;
getLatestOptionsFileName(String dbPath, long envHandle)140   private native static String getLatestOptionsFileName(String dbPath, long envHandle)
141       throws RocksDBException;
142 }
143