leveldb 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +22 -0
- data/README.md +95 -0
- data/ext/Rakefile +11 -0
- data/ext/leveldb/LICENSE +27 -0
- data/ext/leveldb/Makefile +206 -0
- data/ext/leveldb/build_config.mk +13 -0
- data/ext/leveldb/db/builder.cc +88 -0
- data/ext/leveldb/db/builder.h +34 -0
- data/ext/leveldb/db/c.cc +595 -0
- data/ext/leveldb/db/c_test.c +390 -0
- data/ext/leveldb/db/corruption_test.cc +359 -0
- data/ext/leveldb/db/db_bench.cc +979 -0
- data/ext/leveldb/db/db_impl.cc +1485 -0
- data/ext/leveldb/db/db_impl.h +203 -0
- data/ext/leveldb/db/db_iter.cc +299 -0
- data/ext/leveldb/db/db_iter.h +26 -0
- data/ext/leveldb/db/db_test.cc +2092 -0
- data/ext/leveldb/db/dbformat.cc +140 -0
- data/ext/leveldb/db/dbformat.h +227 -0
- data/ext/leveldb/db/dbformat_test.cc +112 -0
- data/ext/leveldb/db/filename.cc +139 -0
- data/ext/leveldb/db/filename.h +80 -0
- data/ext/leveldb/db/filename_test.cc +122 -0
- data/ext/leveldb/db/leveldb_main.cc +238 -0
- data/ext/leveldb/db/log_format.h +35 -0
- data/ext/leveldb/db/log_reader.cc +259 -0
- data/ext/leveldb/db/log_reader.h +108 -0
- data/ext/leveldb/db/log_test.cc +500 -0
- data/ext/leveldb/db/log_writer.cc +103 -0
- data/ext/leveldb/db/log_writer.h +48 -0
- data/ext/leveldb/db/memtable.cc +145 -0
- data/ext/leveldb/db/memtable.h +91 -0
- data/ext/leveldb/db/repair.cc +389 -0
- data/ext/leveldb/db/skiplist.h +379 -0
- data/ext/leveldb/db/skiplist_test.cc +378 -0
- data/ext/leveldb/db/snapshot.h +66 -0
- data/ext/leveldb/db/table_cache.cc +121 -0
- data/ext/leveldb/db/table_cache.h +61 -0
- data/ext/leveldb/db/version_edit.cc +266 -0
- data/ext/leveldb/db/version_edit.h +107 -0
- data/ext/leveldb/db/version_edit_test.cc +46 -0
- data/ext/leveldb/db/version_set.cc +1443 -0
- data/ext/leveldb/db/version_set.h +383 -0
- data/ext/leveldb/db/version_set_test.cc +179 -0
- data/ext/leveldb/db/write_batch.cc +147 -0
- data/ext/leveldb/db/write_batch_internal.h +49 -0
- data/ext/leveldb/db/write_batch_test.cc +120 -0
- data/ext/leveldb/doc/bench/db_bench_sqlite3.cc +718 -0
- data/ext/leveldb/doc/bench/db_bench_tree_db.cc +528 -0
- data/ext/leveldb/helpers/memenv/memenv.cc +384 -0
- data/ext/leveldb/helpers/memenv/memenv.h +20 -0
- data/ext/leveldb/helpers/memenv/memenv_test.cc +232 -0
- data/ext/leveldb/include/leveldb/c.h +291 -0
- data/ext/leveldb/include/leveldb/cache.h +99 -0
- data/ext/leveldb/include/leveldb/comparator.h +63 -0
- data/ext/leveldb/include/leveldb/db.h +161 -0
- data/ext/leveldb/include/leveldb/env.h +333 -0
- data/ext/leveldb/include/leveldb/filter_policy.h +70 -0
- data/ext/leveldb/include/leveldb/iterator.h +100 -0
- data/ext/leveldb/include/leveldb/options.h +195 -0
- data/ext/leveldb/include/leveldb/slice.h +109 -0
- data/ext/leveldb/include/leveldb/status.h +106 -0
- data/ext/leveldb/include/leveldb/table.h +85 -0
- data/ext/leveldb/include/leveldb/table_builder.h +92 -0
- data/ext/leveldb/include/leveldb/write_batch.h +64 -0
- data/ext/leveldb/issues/issue178_test.cc +92 -0
- data/ext/leveldb/port/atomic_pointer.h +224 -0
- data/ext/leveldb/port/port.h +19 -0
- data/ext/leveldb/port/port_example.h +135 -0
- data/ext/leveldb/port/port_posix.cc +54 -0
- data/ext/leveldb/port/port_posix.h +157 -0
- data/ext/leveldb/port/thread_annotations.h +59 -0
- data/ext/leveldb/port/win/stdint.h +24 -0
- data/ext/leveldb/table/block.cc +268 -0
- data/ext/leveldb/table/block.h +44 -0
- data/ext/leveldb/table/block_builder.cc +109 -0
- data/ext/leveldb/table/block_builder.h +57 -0
- data/ext/leveldb/table/filter_block.cc +111 -0
- data/ext/leveldb/table/filter_block.h +68 -0
- data/ext/leveldb/table/filter_block_test.cc +128 -0
- data/ext/leveldb/table/format.cc +145 -0
- data/ext/leveldb/table/format.h +108 -0
- data/ext/leveldb/table/iterator.cc +67 -0
- data/ext/leveldb/table/iterator_wrapper.h +63 -0
- data/ext/leveldb/table/merger.cc +197 -0
- data/ext/leveldb/table/merger.h +26 -0
- data/ext/leveldb/table/table.cc +275 -0
- data/ext/leveldb/table/table_builder.cc +270 -0
- data/ext/leveldb/table/table_test.cc +868 -0
- data/ext/leveldb/table/two_level_iterator.cc +182 -0
- data/ext/leveldb/table/two_level_iterator.h +34 -0
- data/ext/leveldb/util/arena.cc +68 -0
- data/ext/leveldb/util/arena.h +68 -0
- data/ext/leveldb/util/arena_test.cc +68 -0
- data/ext/leveldb/util/bloom.cc +95 -0
- data/ext/leveldb/util/bloom_test.cc +160 -0
- data/ext/leveldb/util/cache.cc +325 -0
- data/ext/leveldb/util/cache_test.cc +186 -0
- data/ext/leveldb/util/coding.cc +194 -0
- data/ext/leveldb/util/coding.h +104 -0
- data/ext/leveldb/util/coding_test.cc +196 -0
- data/ext/leveldb/util/comparator.cc +81 -0
- data/ext/leveldb/util/crc32c.cc +332 -0
- data/ext/leveldb/util/crc32c.h +45 -0
- data/ext/leveldb/util/crc32c_test.cc +72 -0
- data/ext/leveldb/util/env.cc +96 -0
- data/ext/leveldb/util/env_posix.cc +698 -0
- data/ext/leveldb/util/env_test.cc +104 -0
- data/ext/leveldb/util/filter_policy.cc +11 -0
- data/ext/leveldb/util/hash.cc +52 -0
- data/ext/leveldb/util/hash.h +19 -0
- data/ext/leveldb/util/histogram.cc +139 -0
- data/ext/leveldb/util/histogram.h +42 -0
- data/ext/leveldb/util/logging.cc +81 -0
- data/ext/leveldb/util/logging.h +47 -0
- data/ext/leveldb/util/mutexlock.h +41 -0
- data/ext/leveldb/util/options.cc +29 -0
- data/ext/leveldb/util/posix_logger.h +98 -0
- data/ext/leveldb/util/random.h +59 -0
- data/ext/leveldb/util/status.cc +75 -0
- data/ext/leveldb/util/testharness.cc +77 -0
- data/ext/leveldb/util/testharness.h +138 -0
- data/ext/leveldb/util/testutil.cc +51 -0
- data/ext/leveldb/util/testutil.h +53 -0
- data/lib/leveldb/version.rb +3 -0
- data/lib/leveldb.rb +1006 -0
- metadata +228 -0
@@ -0,0 +1,333 @@
|
|
1
|
+
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
2
|
+
// Use of this source code is governed by a BSD-style license that can be
|
3
|
+
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
4
|
+
//
|
5
|
+
// An Env is an interface used by the leveldb implementation to access
|
6
|
+
// operating system functionality like the filesystem etc. Callers
|
7
|
+
// may wish to provide a custom Env object when opening a database to
|
8
|
+
// get fine gain control; e.g., to rate limit file system operations.
|
9
|
+
//
|
10
|
+
// All Env implementations are safe for concurrent access from
|
11
|
+
// multiple threads without any external synchronization.
|
12
|
+
|
13
|
+
#ifndef STORAGE_LEVELDB_INCLUDE_ENV_H_
|
14
|
+
#define STORAGE_LEVELDB_INCLUDE_ENV_H_
|
15
|
+
|
16
|
+
#include <cstdarg>
|
17
|
+
#include <string>
|
18
|
+
#include <vector>
|
19
|
+
#include <stdint.h>
|
20
|
+
#include "leveldb/status.h"
|
21
|
+
|
22
|
+
namespace leveldb {
|
23
|
+
|
24
|
+
class FileLock;
|
25
|
+
class Logger;
|
26
|
+
class RandomAccessFile;
|
27
|
+
class SequentialFile;
|
28
|
+
class Slice;
|
29
|
+
class WritableFile;
|
30
|
+
|
31
|
+
class Env {
|
32
|
+
public:
|
33
|
+
Env() { }
|
34
|
+
virtual ~Env();
|
35
|
+
|
36
|
+
// Return a default environment suitable for the current operating
|
37
|
+
// system. Sophisticated users may wish to provide their own Env
|
38
|
+
// implementation instead of relying on this default environment.
|
39
|
+
//
|
40
|
+
// The result of Default() belongs to leveldb and must never be deleted.
|
41
|
+
static Env* Default();
|
42
|
+
|
43
|
+
// Create a brand new sequentially-readable file with the specified name.
|
44
|
+
// On success, stores a pointer to the new file in *result and returns OK.
|
45
|
+
// On failure stores NULL in *result and returns non-OK. If the file does
|
46
|
+
// not exist, returns a non-OK status.
|
47
|
+
//
|
48
|
+
// The returned file will only be accessed by one thread at a time.
|
49
|
+
virtual Status NewSequentialFile(const std::string& fname,
|
50
|
+
SequentialFile** result) = 0;
|
51
|
+
|
52
|
+
// Create a brand new random access read-only file with the
|
53
|
+
// specified name. On success, stores a pointer to the new file in
|
54
|
+
// *result and returns OK. On failure stores NULL in *result and
|
55
|
+
// returns non-OK. If the file does not exist, returns a non-OK
|
56
|
+
// status.
|
57
|
+
//
|
58
|
+
// The returned file may be concurrently accessed by multiple threads.
|
59
|
+
virtual Status NewRandomAccessFile(const std::string& fname,
|
60
|
+
RandomAccessFile** result) = 0;
|
61
|
+
|
62
|
+
// Create an object that writes to a new file with the specified
|
63
|
+
// name. Deletes any existing file with the same name and creates a
|
64
|
+
// new file. On success, stores a pointer to the new file in
|
65
|
+
// *result and returns OK. On failure stores NULL in *result and
|
66
|
+
// returns non-OK.
|
67
|
+
//
|
68
|
+
// The returned file will only be accessed by one thread at a time.
|
69
|
+
virtual Status NewWritableFile(const std::string& fname,
|
70
|
+
WritableFile** result) = 0;
|
71
|
+
|
72
|
+
// Returns true iff the named file exists.
|
73
|
+
virtual bool FileExists(const std::string& fname) = 0;
|
74
|
+
|
75
|
+
// Store in *result the names of the children of the specified directory.
|
76
|
+
// The names are relative to "dir".
|
77
|
+
// Original contents of *results are dropped.
|
78
|
+
virtual Status GetChildren(const std::string& dir,
|
79
|
+
std::vector<std::string>* result) = 0;
|
80
|
+
|
81
|
+
// Delete the named file.
|
82
|
+
virtual Status DeleteFile(const std::string& fname) = 0;
|
83
|
+
|
84
|
+
// Create the specified directory.
|
85
|
+
virtual Status CreateDir(const std::string& dirname) = 0;
|
86
|
+
|
87
|
+
// Delete the specified directory.
|
88
|
+
virtual Status DeleteDir(const std::string& dirname) = 0;
|
89
|
+
|
90
|
+
// Store the size of fname in *file_size.
|
91
|
+
virtual Status GetFileSize(const std::string& fname, uint64_t* file_size) = 0;
|
92
|
+
|
93
|
+
// Rename file src to target.
|
94
|
+
virtual Status RenameFile(const std::string& src,
|
95
|
+
const std::string& target) = 0;
|
96
|
+
|
97
|
+
// Lock the specified file. Used to prevent concurrent access to
|
98
|
+
// the same db by multiple processes. On failure, stores NULL in
|
99
|
+
// *lock and returns non-OK.
|
100
|
+
//
|
101
|
+
// On success, stores a pointer to the object that represents the
|
102
|
+
// acquired lock in *lock and returns OK. The caller should call
|
103
|
+
// UnlockFile(*lock) to release the lock. If the process exits,
|
104
|
+
// the lock will be automatically released.
|
105
|
+
//
|
106
|
+
// If somebody else already holds the lock, finishes immediately
|
107
|
+
// with a failure. I.e., this call does not wait for existing locks
|
108
|
+
// to go away.
|
109
|
+
//
|
110
|
+
// May create the named file if it does not already exist.
|
111
|
+
virtual Status LockFile(const std::string& fname, FileLock** lock) = 0;
|
112
|
+
|
113
|
+
// Release the lock acquired by a previous successful call to LockFile.
|
114
|
+
// REQUIRES: lock was returned by a successful LockFile() call
|
115
|
+
// REQUIRES: lock has not already been unlocked.
|
116
|
+
virtual Status UnlockFile(FileLock* lock) = 0;
|
117
|
+
|
118
|
+
// Arrange to run "(*function)(arg)" once in a background thread.
|
119
|
+
//
|
120
|
+
// "function" may run in an unspecified thread. Multiple functions
|
121
|
+
// added to the same Env may run concurrently in different threads.
|
122
|
+
// I.e., the caller may not assume that background work items are
|
123
|
+
// serialized.
|
124
|
+
virtual void Schedule(
|
125
|
+
void (*function)(void* arg),
|
126
|
+
void* arg) = 0;
|
127
|
+
|
128
|
+
// Start a new thread, invoking "function(arg)" within the new thread.
|
129
|
+
// When "function(arg)" returns, the thread will be destroyed.
|
130
|
+
virtual void StartThread(void (*function)(void* arg), void* arg) = 0;
|
131
|
+
|
132
|
+
// *path is set to a temporary directory that can be used for testing. It may
|
133
|
+
// or many not have just been created. The directory may or may not differ
|
134
|
+
// between runs of the same process, but subsequent calls will return the
|
135
|
+
// same directory.
|
136
|
+
virtual Status GetTestDirectory(std::string* path) = 0;
|
137
|
+
|
138
|
+
// Create and return a log file for storing informational messages.
|
139
|
+
virtual Status NewLogger(const std::string& fname, Logger** result) = 0;
|
140
|
+
|
141
|
+
// Returns the number of micro-seconds since some fixed point in time. Only
|
142
|
+
// useful for computing deltas of time.
|
143
|
+
virtual uint64_t NowMicros() = 0;
|
144
|
+
|
145
|
+
// Sleep/delay the thread for the perscribed number of micro-seconds.
|
146
|
+
virtual void SleepForMicroseconds(int micros) = 0;
|
147
|
+
|
148
|
+
private:
|
149
|
+
// No copying allowed
|
150
|
+
Env(const Env&);
|
151
|
+
void operator=(const Env&);
|
152
|
+
};
|
153
|
+
|
154
|
+
// A file abstraction for reading sequentially through a file
|
155
|
+
class SequentialFile {
|
156
|
+
public:
|
157
|
+
SequentialFile() { }
|
158
|
+
virtual ~SequentialFile();
|
159
|
+
|
160
|
+
// Read up to "n" bytes from the file. "scratch[0..n-1]" may be
|
161
|
+
// written by this routine. Sets "*result" to the data that was
|
162
|
+
// read (including if fewer than "n" bytes were successfully read).
|
163
|
+
// May set "*result" to point at data in "scratch[0..n-1]", so
|
164
|
+
// "scratch[0..n-1]" must be live when "*result" is used.
|
165
|
+
// If an error was encountered, returns a non-OK status.
|
166
|
+
//
|
167
|
+
// REQUIRES: External synchronization
|
168
|
+
virtual Status Read(size_t n, Slice* result, char* scratch) = 0;
|
169
|
+
|
170
|
+
// Skip "n" bytes from the file. This is guaranteed to be no
|
171
|
+
// slower that reading the same data, but may be faster.
|
172
|
+
//
|
173
|
+
// If end of file is reached, skipping will stop at the end of the
|
174
|
+
// file, and Skip will return OK.
|
175
|
+
//
|
176
|
+
// REQUIRES: External synchronization
|
177
|
+
virtual Status Skip(uint64_t n) = 0;
|
178
|
+
|
179
|
+
private:
|
180
|
+
// No copying allowed
|
181
|
+
SequentialFile(const SequentialFile&);
|
182
|
+
void operator=(const SequentialFile&);
|
183
|
+
};
|
184
|
+
|
185
|
+
// A file abstraction for randomly reading the contents of a file.
|
186
|
+
class RandomAccessFile {
|
187
|
+
public:
|
188
|
+
RandomAccessFile() { }
|
189
|
+
virtual ~RandomAccessFile();
|
190
|
+
|
191
|
+
// Read up to "n" bytes from the file starting at "offset".
|
192
|
+
// "scratch[0..n-1]" may be written by this routine. Sets "*result"
|
193
|
+
// to the data that was read (including if fewer than "n" bytes were
|
194
|
+
// successfully read). May set "*result" to point at data in
|
195
|
+
// "scratch[0..n-1]", so "scratch[0..n-1]" must be live when
|
196
|
+
// "*result" is used. If an error was encountered, returns a non-OK
|
197
|
+
// status.
|
198
|
+
//
|
199
|
+
// Safe for concurrent use by multiple threads.
|
200
|
+
virtual Status Read(uint64_t offset, size_t n, Slice* result,
|
201
|
+
char* scratch) const = 0;
|
202
|
+
|
203
|
+
private:
|
204
|
+
// No copying allowed
|
205
|
+
RandomAccessFile(const RandomAccessFile&);
|
206
|
+
void operator=(const RandomAccessFile&);
|
207
|
+
};
|
208
|
+
|
209
|
+
// A file abstraction for sequential writing. The implementation
|
210
|
+
// must provide buffering since callers may append small fragments
|
211
|
+
// at a time to the file.
|
212
|
+
class WritableFile {
|
213
|
+
public:
|
214
|
+
WritableFile() { }
|
215
|
+
virtual ~WritableFile();
|
216
|
+
|
217
|
+
virtual Status Append(const Slice& data) = 0;
|
218
|
+
virtual Status Close() = 0;
|
219
|
+
virtual Status Flush() = 0;
|
220
|
+
virtual Status Sync() = 0;
|
221
|
+
|
222
|
+
private:
|
223
|
+
// No copying allowed
|
224
|
+
WritableFile(const WritableFile&);
|
225
|
+
void operator=(const WritableFile&);
|
226
|
+
};
|
227
|
+
|
228
|
+
// An interface for writing log messages.
|
229
|
+
class Logger {
|
230
|
+
public:
|
231
|
+
Logger() { }
|
232
|
+
virtual ~Logger();
|
233
|
+
|
234
|
+
// Write an entry to the log file with the specified format.
|
235
|
+
virtual void Logv(const char* format, va_list ap) = 0;
|
236
|
+
|
237
|
+
private:
|
238
|
+
// No copying allowed
|
239
|
+
Logger(const Logger&);
|
240
|
+
void operator=(const Logger&);
|
241
|
+
};
|
242
|
+
|
243
|
+
|
244
|
+
// Identifies a locked file.
|
245
|
+
class FileLock {
|
246
|
+
public:
|
247
|
+
FileLock() { }
|
248
|
+
virtual ~FileLock();
|
249
|
+
private:
|
250
|
+
// No copying allowed
|
251
|
+
FileLock(const FileLock&);
|
252
|
+
void operator=(const FileLock&);
|
253
|
+
};
|
254
|
+
|
255
|
+
// Log the specified data to *info_log if info_log is non-NULL.
|
256
|
+
extern void Log(Logger* info_log, const char* format, ...)
|
257
|
+
# if defined(__GNUC__) || defined(__clang__)
|
258
|
+
__attribute__((__format__ (__printf__, 2, 3)))
|
259
|
+
# endif
|
260
|
+
;
|
261
|
+
|
262
|
+
// A utility routine: write "data" to the named file.
|
263
|
+
extern Status WriteStringToFile(Env* env, const Slice& data,
|
264
|
+
const std::string& fname);
|
265
|
+
|
266
|
+
// A utility routine: read contents of named file into *data
|
267
|
+
extern Status ReadFileToString(Env* env, const std::string& fname,
|
268
|
+
std::string* data);
|
269
|
+
|
270
|
+
// An implementation of Env that forwards all calls to another Env.
|
271
|
+
// May be useful to clients who wish to override just part of the
|
272
|
+
// functionality of another Env.
|
273
|
+
class EnvWrapper : public Env {
|
274
|
+
public:
|
275
|
+
// Initialize an EnvWrapper that delegates all calls to *t
|
276
|
+
explicit EnvWrapper(Env* t) : target_(t) { }
|
277
|
+
virtual ~EnvWrapper();
|
278
|
+
|
279
|
+
// Return the target to which this Env forwards all calls
|
280
|
+
Env* target() const { return target_; }
|
281
|
+
|
282
|
+
// The following text is boilerplate that forwards all methods to target()
|
283
|
+
Status NewSequentialFile(const std::string& f, SequentialFile** r) {
|
284
|
+
return target_->NewSequentialFile(f, r);
|
285
|
+
}
|
286
|
+
Status NewRandomAccessFile(const std::string& f, RandomAccessFile** r) {
|
287
|
+
return target_->NewRandomAccessFile(f, r);
|
288
|
+
}
|
289
|
+
Status NewWritableFile(const std::string& f, WritableFile** r) {
|
290
|
+
return target_->NewWritableFile(f, r);
|
291
|
+
}
|
292
|
+
bool FileExists(const std::string& f) { return target_->FileExists(f); }
|
293
|
+
Status GetChildren(const std::string& dir, std::vector<std::string>* r) {
|
294
|
+
return target_->GetChildren(dir, r);
|
295
|
+
}
|
296
|
+
Status DeleteFile(const std::string& f) { return target_->DeleteFile(f); }
|
297
|
+
Status CreateDir(const std::string& d) { return target_->CreateDir(d); }
|
298
|
+
Status DeleteDir(const std::string& d) { return target_->DeleteDir(d); }
|
299
|
+
Status GetFileSize(const std::string& f, uint64_t* s) {
|
300
|
+
return target_->GetFileSize(f, s);
|
301
|
+
}
|
302
|
+
Status RenameFile(const std::string& s, const std::string& t) {
|
303
|
+
return target_->RenameFile(s, t);
|
304
|
+
}
|
305
|
+
Status LockFile(const std::string& f, FileLock** l) {
|
306
|
+
return target_->LockFile(f, l);
|
307
|
+
}
|
308
|
+
Status UnlockFile(FileLock* l) { return target_->UnlockFile(l); }
|
309
|
+
void Schedule(void (*f)(void*), void* a) {
|
310
|
+
return target_->Schedule(f, a);
|
311
|
+
}
|
312
|
+
void StartThread(void (*f)(void*), void* a) {
|
313
|
+
return target_->StartThread(f, a);
|
314
|
+
}
|
315
|
+
virtual Status GetTestDirectory(std::string* path) {
|
316
|
+
return target_->GetTestDirectory(path);
|
317
|
+
}
|
318
|
+
virtual Status NewLogger(const std::string& fname, Logger** result) {
|
319
|
+
return target_->NewLogger(fname, result);
|
320
|
+
}
|
321
|
+
uint64_t NowMicros() {
|
322
|
+
return target_->NowMicros();
|
323
|
+
}
|
324
|
+
void SleepForMicroseconds(int micros) {
|
325
|
+
target_->SleepForMicroseconds(micros);
|
326
|
+
}
|
327
|
+
private:
|
328
|
+
Env* target_;
|
329
|
+
};
|
330
|
+
|
331
|
+
} // namespace leveldb
|
332
|
+
|
333
|
+
#endif // STORAGE_LEVELDB_INCLUDE_ENV_H_
|
@@ -0,0 +1,70 @@
|
|
1
|
+
// Copyright (c) 2012 The LevelDB Authors. All rights reserved.
|
2
|
+
// Use of this source code is governed by a BSD-style license that can be
|
3
|
+
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
4
|
+
//
|
5
|
+
// A database can be configured with a custom FilterPolicy object.
|
6
|
+
// This object is responsible for creating a small filter from a set
|
7
|
+
// of keys. These filters are stored in leveldb and are consulted
|
8
|
+
// automatically by leveldb to decide whether or not to read some
|
9
|
+
// information from disk. In many cases, a filter can cut down the
|
10
|
+
// number of disk seeks form a handful to a single disk seek per
|
11
|
+
// DB::Get() call.
|
12
|
+
//
|
13
|
+
// Most people will want to use the builtin bloom filter support (see
|
14
|
+
// NewBloomFilterPolicy() below).
|
15
|
+
|
16
|
+
#ifndef STORAGE_LEVELDB_INCLUDE_FILTER_POLICY_H_
|
17
|
+
#define STORAGE_LEVELDB_INCLUDE_FILTER_POLICY_H_
|
18
|
+
|
19
|
+
#include <string>
|
20
|
+
|
21
|
+
namespace leveldb {
|
22
|
+
|
23
|
+
class Slice;
|
24
|
+
|
25
|
+
class FilterPolicy {
|
26
|
+
public:
|
27
|
+
virtual ~FilterPolicy();
|
28
|
+
|
29
|
+
// Return the name of this policy. Note that if the filter encoding
|
30
|
+
// changes in an incompatible way, the name returned by this method
|
31
|
+
// must be changed. Otherwise, old incompatible filters may be
|
32
|
+
// passed to methods of this type.
|
33
|
+
virtual const char* Name() const = 0;
|
34
|
+
|
35
|
+
// keys[0,n-1] contains a list of keys (potentially with duplicates)
|
36
|
+
// that are ordered according to the user supplied comparator.
|
37
|
+
// Append a filter that summarizes keys[0,n-1] to *dst.
|
38
|
+
//
|
39
|
+
// Warning: do not change the initial contents of *dst. Instead,
|
40
|
+
// append the newly constructed filter to *dst.
|
41
|
+
virtual void CreateFilter(const Slice* keys, int n, std::string* dst)
|
42
|
+
const = 0;
|
43
|
+
|
44
|
+
// "filter" contains the data appended by a preceding call to
|
45
|
+
// CreateFilter() on this class. This method must return true if
|
46
|
+
// the key was in the list of keys passed to CreateFilter().
|
47
|
+
// This method may return true or false if the key was not on the
|
48
|
+
// list, but it should aim to return false with a high probability.
|
49
|
+
virtual bool KeyMayMatch(const Slice& key, const Slice& filter) const = 0;
|
50
|
+
};
|
51
|
+
|
52
|
+
// Return a new filter policy that uses a bloom filter with approximately
|
53
|
+
// the specified number of bits per key. A good value for bits_per_key
|
54
|
+
// is 10, which yields a filter with ~ 1% false positive rate.
|
55
|
+
//
|
56
|
+
// Callers must delete the result after any database that is using the
|
57
|
+
// result has been closed.
|
58
|
+
//
|
59
|
+
// Note: if you are using a custom comparator that ignores some parts
|
60
|
+
// of the keys being compared, you must not use NewBloomFilterPolicy()
|
61
|
+
// and must provide your own FilterPolicy that also ignores the
|
62
|
+
// corresponding parts of the keys. For example, if the comparator
|
63
|
+
// ignores trailing spaces, it would be incorrect to use a
|
64
|
+
// FilterPolicy (like NewBloomFilterPolicy) that does not ignore
|
65
|
+
// trailing spaces in keys.
|
66
|
+
extern const FilterPolicy* NewBloomFilterPolicy(int bits_per_key);
|
67
|
+
|
68
|
+
}
|
69
|
+
|
70
|
+
#endif // STORAGE_LEVELDB_INCLUDE_FILTER_POLICY_H_
|
@@ -0,0 +1,100 @@
|
|
1
|
+
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
2
|
+
// Use of this source code is governed by a BSD-style license that can be
|
3
|
+
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
4
|
+
//
|
5
|
+
// An iterator yields a sequence of key/value pairs from a source.
|
6
|
+
// The following class defines the interface. Multiple implementations
|
7
|
+
// are provided by this library. In particular, iterators are provided
|
8
|
+
// to access the contents of a Table or a DB.
|
9
|
+
//
|
10
|
+
// Multiple threads can invoke const methods on an Iterator without
|
11
|
+
// external synchronization, but if any of the threads may call a
|
12
|
+
// non-const method, all threads accessing the same Iterator must use
|
13
|
+
// external synchronization.
|
14
|
+
|
15
|
+
#ifndef STORAGE_LEVELDB_INCLUDE_ITERATOR_H_
|
16
|
+
#define STORAGE_LEVELDB_INCLUDE_ITERATOR_H_
|
17
|
+
|
18
|
+
#include "leveldb/slice.h"
|
19
|
+
#include "leveldb/status.h"
|
20
|
+
|
21
|
+
namespace leveldb {
|
22
|
+
|
23
|
+
class Iterator {
|
24
|
+
public:
|
25
|
+
Iterator();
|
26
|
+
virtual ~Iterator();
|
27
|
+
|
28
|
+
// An iterator is either positioned at a key/value pair, or
|
29
|
+
// not valid. This method returns true iff the iterator is valid.
|
30
|
+
virtual bool Valid() const = 0;
|
31
|
+
|
32
|
+
// Position at the first key in the source. The iterator is Valid()
|
33
|
+
// after this call iff the source is not empty.
|
34
|
+
virtual void SeekToFirst() = 0;
|
35
|
+
|
36
|
+
// Position at the last key in the source. The iterator is
|
37
|
+
// Valid() after this call iff the source is not empty.
|
38
|
+
virtual void SeekToLast() = 0;
|
39
|
+
|
40
|
+
// Position at the first key in the source that at or past target
|
41
|
+
// The iterator is Valid() after this call iff the source contains
|
42
|
+
// an entry that comes at or past target.
|
43
|
+
virtual void Seek(const Slice& target) = 0;
|
44
|
+
|
45
|
+
// Moves to the next entry in the source. After this call, Valid() is
|
46
|
+
// true iff the iterator was not positioned at the last entry in the source.
|
47
|
+
// REQUIRES: Valid()
|
48
|
+
virtual void Next() = 0;
|
49
|
+
|
50
|
+
// Moves to the previous entry in the source. After this call, Valid() is
|
51
|
+
// true iff the iterator was not positioned at the first entry in source.
|
52
|
+
// REQUIRES: Valid()
|
53
|
+
virtual void Prev() = 0;
|
54
|
+
|
55
|
+
// Return the key for the current entry. The underlying storage for
|
56
|
+
// the returned slice is valid only until the next modification of
|
57
|
+
// the iterator.
|
58
|
+
// REQUIRES: Valid()
|
59
|
+
virtual Slice key() const = 0;
|
60
|
+
|
61
|
+
// Return the value for the current entry. The underlying storage for
|
62
|
+
// the returned slice is valid only until the next modification of
|
63
|
+
// the iterator.
|
64
|
+
// REQUIRES: !AtEnd() && !AtStart()
|
65
|
+
virtual Slice value() const = 0;
|
66
|
+
|
67
|
+
// If an error has occurred, return it. Else return an ok status.
|
68
|
+
virtual Status status() const = 0;
|
69
|
+
|
70
|
+
// Clients are allowed to register function/arg1/arg2 triples that
|
71
|
+
// will be invoked when this iterator is destroyed.
|
72
|
+
//
|
73
|
+
// Note that unlike all of the preceding methods, this method is
|
74
|
+
// not abstract and therefore clients should not override it.
|
75
|
+
typedef void (*CleanupFunction)(void* arg1, void* arg2);
|
76
|
+
void RegisterCleanup(CleanupFunction function, void* arg1, void* arg2);
|
77
|
+
|
78
|
+
private:
|
79
|
+
struct Cleanup {
|
80
|
+
CleanupFunction function;
|
81
|
+
void* arg1;
|
82
|
+
void* arg2;
|
83
|
+
Cleanup* next;
|
84
|
+
};
|
85
|
+
Cleanup cleanup_;
|
86
|
+
|
87
|
+
// No copying allowed
|
88
|
+
Iterator(const Iterator&);
|
89
|
+
void operator=(const Iterator&);
|
90
|
+
};
|
91
|
+
|
92
|
+
// Return an empty iterator (yields nothing).
|
93
|
+
extern Iterator* NewEmptyIterator();
|
94
|
+
|
95
|
+
// Return an empty iterator with the specified status.
|
96
|
+
extern Iterator* NewErrorIterator(const Status& status);
|
97
|
+
|
98
|
+
} // namespace leveldb
|
99
|
+
|
100
|
+
#endif // STORAGE_LEVELDB_INCLUDE_ITERATOR_H_
|