@@ -57,7 +57,133 @@ endif::rootpath[]
57
57
58
58
=== 打开一个数据库
59
59
60
+ 使用 `static Status Open(const Options &options, const std::string &name, DB **dbptr)` 可以打开一个数据库, options填入数据库打开选项,name指定数据库名,DB给出一个数据库指针(原先指向nullptr),因为调用之后会把创建的数据库句柄赋值给对应的指针。
60
61
62
+ - Options 用来控制数据库行为的一些参数在调用 `DB::Open` 的时候使用。
63
+
64
+ [source,c++]
65
+ ----
66
+ struct LEVELDB_EXPORT Options {
67
+ // Create an Options object with default values for all fields.
68
+ Options();
69
+
70
+ // -------------------
71
+ // Parameters that affect behavior
72
+
73
+ // Comparator used to define the order of keys in the table.
74
+ // Default: a comparator that uses lexicographic byte-wise ordering
75
+ //
76
+ // REQUIRES: The client must ensure that the comparator supplied
77
+ // here has the same name and orders keys *exactly* the same as the
78
+ // comparator provided to previous open calls on the same DB.
79
+ const Comparator *comparator;
80
+
81
+ // If true, the database will be created if it is missing.
82
+ bool create_if_missing = false;
83
+
84
+ // If true, an error is raised if the database already exists.
85
+ bool error_if_exists = false;
86
+
87
+ // If true, the implementation will do aggressive checking of the
88
+ // data it is processing and will stop early if it detects any
89
+ // errors. This may have unforeseen ramifications: for example, a
90
+ // corruption of one DB entry may cause a large number of entries to
91
+ // become unreadable or for the entire DB to become unopenable.
92
+ bool paranoid_checks = false;
93
+
94
+ // Use the specified object to interact with the environment,
95
+ // e.g. to read/write files, schedule background work, etc.
96
+ // Default: Env::Default()
97
+ Env *env;
98
+
99
+ // Any internal progress/error information generated by the db will
100
+ // be written to info_log if it is non-null, or to a file stored
101
+ // in the same directory as the DB contents if info_log is null.
102
+ Logger *info_log = nullptr;
103
+
104
+ // -------------------
105
+ // Parameters that affect performance
106
+
107
+ // Amount of data to build up in memory (backed by an unsorted log
108
+ // on disk) before converting to a sorted on-disk file.
109
+ //
110
+ // Larger values increase performance, especially during bulk loads.
111
+ // Up to two write buffers may be held in memory at the same time,
112
+ // so you may wish to adjust this parameter to control memory usage.
113
+ // Also, a larger write buffer will result in a longer recovery time
114
+ // the next time the database is opened.
115
+ size_t write_buffer_size = 4 * 1024 * 1024;
116
+
117
+ // Number of open files that can be used by the DB. You may need to
118
+ // increase this if your database has a large working set (budget
119
+ // one open file per 2MB of working set).
120
+ int max_open_files = 1000;
121
+
122
+ // Control over blocks (user data is stored in a set of blocks, and
123
+ // a block is the unit of reading from disk).
124
+
125
+ // If non-null, use the specified cache for blocks.
126
+ // If null, leveldb will automatically create and use an 8MB internal cache.
127
+ Cache *block_cache = nullptr;
128
+
129
+ // Approximate size of user data packed per block. Note that the
130
+ // block size specified here corresponds to uncompressed data. The
131
+ // actual size of the unit read from disk may be smaller if
132
+ // compression is enabled. This parameter can be changed dynamically.
133
+ size_t block_size = 4 * 1024;
134
+
135
+ // Number of keys between restart points for delta encoding of keys.
136
+ // This parameter can be changed dynamically. Most clients should
137
+ // leave this parameter alone.
138
+ int block_restart_interval = 16;
139
+
140
+ // Leveldb will write up to this amount of bytes to a file before
141
+ // switching to a new one.
142
+ // Most clients should leave this parameter alone. However if your
143
+ // filesystem is more efficient with larger files, you could
144
+ // consider increasing the value. The downside will be longer
145
+ // compactions and hence longer latency/performance hiccups.
146
+ // Another reason to increase this parameter might be when you are
147
+ // initially populating a large database.
148
+ size_t max_file_size = 2 * 1024 * 1024;
149
+
150
+ // Compress blocks using the specified compression algorithm. This
151
+ // parameter can be changed dynamically.
152
+ //
153
+ // Default: kSnappyCompression, which gives lightweight but fast
154
+ // compression.
155
+ //
156
+ // Typical speeds of kSnappyCompression on an Intel(R) Core(TM)2 2.4GHz:
157
+ // ~200-500MB/s compression
158
+ // ~400-800MB/s decompression
159
+ // Note that these speeds are significantly faster than most
160
+ // persistent storage speeds, and therefore it is typically never
161
+ // worth switching to kNoCompression. Even if the input data is
162
+ // incompressible, the kSnappyCompression implementation will
163
+ // efficiently detect that and will switch to uncompressed mode.
164
+ CompressionType compression = kSnappyCompression;
165
+
166
+ // EXPERIMENTAL: If true, append to existing MANIFEST and log files
167
+ // when a database is opened. This can significantly speed up open.
168
+ //
169
+ // Default: currently false, but may become true later.
170
+ bool reuse_logs = false;
171
+
172
+ // If non-null, use the specified filter policy to reduce disk reads.
173
+ // Many applications will benefit from passing the result of
174
+ // NewBloomFilterPolicy() here.
175
+ const FilterPolicy *filter_policy = nullptr;
176
+ };
177
+ ----
178
+
179
+
180
+ // Open the database with the specified "name".
181
+ // Stores a pointer to a heap-allocated database in *dbptr and returns
182
+ // OK on success.
183
+ // Stores nullptr in *dbptr and returns a non-OK status on error.
184
+ // Caller should delete *dbptr when it is no longer needed.
185
+ static Status Open(const Options &options, const std::string &name,
186
+ DB **dbptr);
61
187
A leveldb database has a name which corresponds to a file system directory. All
62
188
of the contents of database are stored in this directory. The following example
63
189
shows how to open a database, creating it if necessary:
0 commit comments