leveldb-ruby 0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +17 -0
- data/ext/leveldb/extconf.rb +10 -0
- data/ext/leveldb/leveldb.cc +181 -0
- data/leveldb/Makefile +172 -0
- data/leveldb/db/builder.cc +90 -0
- data/leveldb/db/builder.h +36 -0
- data/leveldb/db/corruption_test.cc +354 -0
- data/leveldb/db/db_bench.cc +677 -0
- data/leveldb/db/db_impl.cc +1236 -0
- data/leveldb/db/db_impl.h +180 -0
- data/leveldb/db/db_iter.cc +298 -0
- data/leveldb/db/db_iter.h +26 -0
- data/leveldb/db/db_test.cc +1192 -0
- data/leveldb/db/dbformat.cc +87 -0
- data/leveldb/db/dbformat.h +165 -0
- data/leveldb/db/dbformat_test.cc +112 -0
- data/leveldb/db/filename.cc +135 -0
- data/leveldb/db/filename.h +80 -0
- data/leveldb/db/filename_test.cc +122 -0
- data/leveldb/db/log_format.h +35 -0
- data/leveldb/db/log_reader.cc +254 -0
- data/leveldb/db/log_reader.h +108 -0
- data/leveldb/db/log_test.cc +500 -0
- data/leveldb/db/log_writer.cc +103 -0
- data/leveldb/db/log_writer.h +48 -0
- data/leveldb/db/memtable.cc +108 -0
- data/leveldb/db/memtable.h +85 -0
- data/leveldb/db/repair.cc +384 -0
- data/leveldb/db/skiplist.h +378 -0
- data/leveldb/db/skiplist_test.cc +378 -0
- data/leveldb/db/snapshot.h +66 -0
- data/leveldb/db/table_cache.cc +95 -0
- data/leveldb/db/table_cache.h +50 -0
- data/leveldb/db/version_edit.cc +268 -0
- data/leveldb/db/version_edit.h +106 -0
- data/leveldb/db/version_edit_test.cc +46 -0
- data/leveldb/db/version_set.cc +1060 -0
- data/leveldb/db/version_set.h +306 -0
- data/leveldb/db/write_batch.cc +138 -0
- data/leveldb/db/write_batch_internal.h +45 -0
- data/leveldb/db/write_batch_test.cc +89 -0
- data/leveldb/include/leveldb/cache.h +99 -0
- data/leveldb/include/leveldb/comparator.h +63 -0
- data/leveldb/include/leveldb/db.h +148 -0
- data/leveldb/include/leveldb/env.h +302 -0
- data/leveldb/include/leveldb/iterator.h +100 -0
- data/leveldb/include/leveldb/options.h +198 -0
- data/leveldb/include/leveldb/slice.h +109 -0
- data/leveldb/include/leveldb/status.h +100 -0
- data/leveldb/include/leveldb/table.h +70 -0
- data/leveldb/include/leveldb/table_builder.h +91 -0
- data/leveldb/include/leveldb/write_batch.h +64 -0
- data/leveldb/port/port.h +23 -0
- data/leveldb/port/port_android.cc +64 -0
- data/leveldb/port/port_android.h +150 -0
- data/leveldb/port/port_chromium.cc +80 -0
- data/leveldb/port/port_chromium.h +97 -0
- data/leveldb/port/port_example.h +115 -0
- data/leveldb/port/port_osx.cc +50 -0
- data/leveldb/port/port_osx.h +125 -0
- data/leveldb/port/port_posix.cc +50 -0
- data/leveldb/port/port_posix.h +94 -0
- data/leveldb/port/sha1_portable.cc +298 -0
- data/leveldb/port/sha1_portable.h +25 -0
- data/leveldb/port/sha1_test.cc +39 -0
- data/leveldb/port/win/stdint.h +24 -0
- data/leveldb/table/block.cc +263 -0
- data/leveldb/table/block.h +43 -0
- data/leveldb/table/block_builder.cc +109 -0
- data/leveldb/table/block_builder.h +57 -0
- data/leveldb/table/format.cc +131 -0
- data/leveldb/table/format.h +103 -0
- data/leveldb/table/iterator.cc +67 -0
- data/leveldb/table/iterator_wrapper.h +63 -0
- data/leveldb/table/merger.cc +197 -0
- data/leveldb/table/merger.h +26 -0
- data/leveldb/table/table.cc +175 -0
- data/leveldb/table/table_builder.cc +227 -0
- data/leveldb/table/table_test.cc +845 -0
- data/leveldb/table/two_level_iterator.cc +182 -0
- data/leveldb/table/two_level_iterator.h +34 -0
- data/leveldb/util/arena.cc +68 -0
- data/leveldb/util/arena.h +68 -0
- data/leveldb/util/arena_test.cc +68 -0
- data/leveldb/util/cache.cc +255 -0
- data/leveldb/util/cache_test.cc +169 -0
- data/leveldb/util/coding.cc +194 -0
- data/leveldb/util/coding.h +104 -0
- data/leveldb/util/coding_test.cc +173 -0
- data/leveldb/util/comparator.cc +72 -0
- data/leveldb/util/crc32c.cc +332 -0
- data/leveldb/util/crc32c.h +45 -0
- data/leveldb/util/crc32c_test.cc +72 -0
- data/leveldb/util/env.cc +77 -0
- data/leveldb/util/env_chromium.cc +612 -0
- data/leveldb/util/env_posix.cc +606 -0
- data/leveldb/util/env_test.cc +102 -0
- data/leveldb/util/hash.cc +45 -0
- data/leveldb/util/hash.h +19 -0
- data/leveldb/util/histogram.cc +128 -0
- data/leveldb/util/histogram.h +41 -0
- data/leveldb/util/logging.cc +81 -0
- data/leveldb/util/logging.h +47 -0
- data/leveldb/util/mutexlock.h +39 -0
- data/leveldb/util/options.cc +28 -0
- data/leveldb/util/random.h +59 -0
- data/leveldb/util/status.cc +75 -0
- data/leveldb/util/testharness.cc +65 -0
- data/leveldb/util/testharness.h +129 -0
- data/leveldb/util/testutil.cc +51 -0
- data/leveldb/util/testutil.h +53 -0
- data/lib/leveldb.rb +36 -0
- metadata +183 -0
|
@@ -0,0 +1,89 @@
|
|
|
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
|
+
#include "leveldb/db.h"
|
|
6
|
+
|
|
7
|
+
#include "db/memtable.h"
|
|
8
|
+
#include "db/write_batch_internal.h"
|
|
9
|
+
#include "leveldb/env.h"
|
|
10
|
+
#include "util/logging.h"
|
|
11
|
+
#include "util/testharness.h"
|
|
12
|
+
|
|
13
|
+
namespace leveldb {
|
|
14
|
+
|
|
15
|
+
static std::string PrintContents(WriteBatch* b) {
|
|
16
|
+
InternalKeyComparator cmp(BytewiseComparator());
|
|
17
|
+
MemTable* mem = new MemTable(cmp);
|
|
18
|
+
mem->Ref();
|
|
19
|
+
std::string state;
|
|
20
|
+
Status s = WriteBatchInternal::InsertInto(b, mem);
|
|
21
|
+
Iterator* iter = mem->NewIterator();
|
|
22
|
+
for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
|
|
23
|
+
ParsedInternalKey ikey;
|
|
24
|
+
ASSERT_TRUE(ParseInternalKey(iter->key(), &ikey));
|
|
25
|
+
switch (ikey.type) {
|
|
26
|
+
case kTypeValue:
|
|
27
|
+
state.append("Put(");
|
|
28
|
+
state.append(ikey.user_key.ToString());
|
|
29
|
+
state.append(", ");
|
|
30
|
+
state.append(iter->value().ToString());
|
|
31
|
+
state.append(")");
|
|
32
|
+
break;
|
|
33
|
+
case kTypeDeletion:
|
|
34
|
+
state.append("Delete(");
|
|
35
|
+
state.append(ikey.user_key.ToString());
|
|
36
|
+
state.append(")");
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
state.append("@");
|
|
40
|
+
state.append(NumberToString(ikey.sequence));
|
|
41
|
+
}
|
|
42
|
+
delete iter;
|
|
43
|
+
if (!s.ok()) {
|
|
44
|
+
state.append("ParseError()");
|
|
45
|
+
}
|
|
46
|
+
mem->Unref();
|
|
47
|
+
return state;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
class WriteBatchTest { };
|
|
51
|
+
|
|
52
|
+
TEST(WriteBatchTest, Empty) {
|
|
53
|
+
WriteBatch batch;
|
|
54
|
+
ASSERT_EQ("", PrintContents(&batch));
|
|
55
|
+
ASSERT_EQ(0, WriteBatchInternal::Count(&batch));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
TEST(WriteBatchTest, Multiple) {
|
|
59
|
+
WriteBatch batch;
|
|
60
|
+
batch.Put(Slice("foo"), Slice("bar"));
|
|
61
|
+
batch.Delete(Slice("box"));
|
|
62
|
+
batch.Put(Slice("baz"), Slice("boo"));
|
|
63
|
+
WriteBatchInternal::SetSequence(&batch, 100);
|
|
64
|
+
ASSERT_EQ(100, WriteBatchInternal::Sequence(&batch));
|
|
65
|
+
ASSERT_EQ(3, WriteBatchInternal::Count(&batch));
|
|
66
|
+
ASSERT_EQ("Put(baz, boo)@102"
|
|
67
|
+
"Delete(box)@101"
|
|
68
|
+
"Put(foo, bar)@100",
|
|
69
|
+
PrintContents(&batch));
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
TEST(WriteBatchTest, Corruption) {
|
|
73
|
+
WriteBatch batch;
|
|
74
|
+
batch.Put(Slice("foo"), Slice("bar"));
|
|
75
|
+
batch.Delete(Slice("box"));
|
|
76
|
+
WriteBatchInternal::SetSequence(&batch, 200);
|
|
77
|
+
Slice contents = WriteBatchInternal::Contents(&batch);
|
|
78
|
+
WriteBatchInternal::SetContents(&batch,
|
|
79
|
+
Slice(contents.data(),contents.size()-1));
|
|
80
|
+
ASSERT_EQ("Put(foo, bar)@200"
|
|
81
|
+
"ParseError()",
|
|
82
|
+
PrintContents(&batch));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
int main(int argc, char** argv) {
|
|
88
|
+
return leveldb::test::RunAllTests();
|
|
89
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
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
|
+
// A Cache is an interface that maps keys to values. It has internal
|
|
6
|
+
// synchronization and may be safely accessed concurrently from
|
|
7
|
+
// multiple threads. It may automatically evict entries to make room
|
|
8
|
+
// for new entries. Values have a specified charge against the cache
|
|
9
|
+
// capacity. For example, a cache where the values are variable
|
|
10
|
+
// length strings, may use the length of the string as the charge for
|
|
11
|
+
// the string.
|
|
12
|
+
//
|
|
13
|
+
// A builtin cache implementation with a least-recently-used eviction
|
|
14
|
+
// policy is provided. Clients may use their own implementations if
|
|
15
|
+
// they want something more sophisticated (like scan-resistance, a
|
|
16
|
+
// custom eviction policy, variable cache sizing, etc.)
|
|
17
|
+
|
|
18
|
+
#ifndef STORAGE_LEVELDB_INCLUDE_CACHE_H_
|
|
19
|
+
#define STORAGE_LEVELDB_INCLUDE_CACHE_H_
|
|
20
|
+
|
|
21
|
+
#include <stdint.h>
|
|
22
|
+
#include "leveldb/slice.h"
|
|
23
|
+
|
|
24
|
+
namespace leveldb {
|
|
25
|
+
|
|
26
|
+
class Cache;
|
|
27
|
+
|
|
28
|
+
// Create a new cache with a fixed size capacity. This implementation
|
|
29
|
+
// of Cache uses a least-recently-used eviction policy.
|
|
30
|
+
extern Cache* NewLRUCache(size_t capacity);
|
|
31
|
+
|
|
32
|
+
class Cache {
|
|
33
|
+
public:
|
|
34
|
+
Cache() { }
|
|
35
|
+
|
|
36
|
+
// Destroys all existing entries by calling the "deleter"
|
|
37
|
+
// function that was passed to the constructor.
|
|
38
|
+
virtual ~Cache();
|
|
39
|
+
|
|
40
|
+
// Opaque handle to an entry stored in the cache.
|
|
41
|
+
struct Handle { };
|
|
42
|
+
|
|
43
|
+
// Insert a mapping from key->value into the cache and assign it
|
|
44
|
+
// the specified charge against the total cache capacity.
|
|
45
|
+
//
|
|
46
|
+
// Returns a handle that corresponds to the mapping. The caller
|
|
47
|
+
// must call this->Release(handle) when the returned mapping is no
|
|
48
|
+
// longer needed.
|
|
49
|
+
//
|
|
50
|
+
// When the inserted entry is no longer needed, the key and
|
|
51
|
+
// value will be passed to "deleter".
|
|
52
|
+
virtual Handle* Insert(const Slice& key, void* value, size_t charge,
|
|
53
|
+
void (*deleter)(const Slice& key, void* value)) = 0;
|
|
54
|
+
|
|
55
|
+
// If the cache has no mapping for "key", returns NULL.
|
|
56
|
+
//
|
|
57
|
+
// Else return a handle that corresponds to the mapping. The caller
|
|
58
|
+
// must call this->Release(handle) when the returned mapping is no
|
|
59
|
+
// longer needed.
|
|
60
|
+
virtual Handle* Lookup(const Slice& key) = 0;
|
|
61
|
+
|
|
62
|
+
// Release a mapping returned by a previous Lookup().
|
|
63
|
+
// REQUIRES: handle must not have been released yet.
|
|
64
|
+
// REQUIRES: handle must have been returned by a method on *this.
|
|
65
|
+
virtual void Release(Handle* handle) = 0;
|
|
66
|
+
|
|
67
|
+
// Return the value encapsulated in a handle returned by a
|
|
68
|
+
// successful Lookup().
|
|
69
|
+
// REQUIRES: handle must not have been released yet.
|
|
70
|
+
// REQUIRES: handle must have been returned by a method on *this.
|
|
71
|
+
virtual void* Value(Handle* handle) = 0;
|
|
72
|
+
|
|
73
|
+
// If the cache contains entry for key, erase it. Note that the
|
|
74
|
+
// underlying entry will be kept around until all existing handles
|
|
75
|
+
// to it have been released.
|
|
76
|
+
virtual void Erase(const Slice& key) = 0;
|
|
77
|
+
|
|
78
|
+
// Return a new numeric id. May be used by multiple clients who are
|
|
79
|
+
// sharing the same cache to partition the key space. Typically the
|
|
80
|
+
// client will allocate a new id at startup and prepend the id to
|
|
81
|
+
// its cache keys.
|
|
82
|
+
virtual uint64_t NewId() = 0;
|
|
83
|
+
|
|
84
|
+
private:
|
|
85
|
+
void LRU_Remove(Handle* e);
|
|
86
|
+
void LRU_Append(Handle* e);
|
|
87
|
+
void Unref(Handle* e);
|
|
88
|
+
|
|
89
|
+
struct Rep;
|
|
90
|
+
Rep* rep_;
|
|
91
|
+
|
|
92
|
+
// No copying allowed
|
|
93
|
+
Cache(const Cache&);
|
|
94
|
+
void operator=(const Cache&);
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
#endif // STORAGE_LEVELDB_UTIL_CACHE_H_
|
|
@@ -0,0 +1,63 @@
|
|
|
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
|
+
#ifndef STORAGE_LEVELDB_INCLUDE_COMPARATOR_H_
|
|
6
|
+
#define STORAGE_LEVELDB_INCLUDE_COMPARATOR_H_
|
|
7
|
+
|
|
8
|
+
#include <string>
|
|
9
|
+
|
|
10
|
+
namespace leveldb {
|
|
11
|
+
|
|
12
|
+
class Slice;
|
|
13
|
+
|
|
14
|
+
// A Comparator object provides a total order across slices that are
|
|
15
|
+
// used as keys in an sstable or a database. A Comparator implementation
|
|
16
|
+
// must be thread-safe since leveldb may invoke its methods concurrently
|
|
17
|
+
// from multiple threads.
|
|
18
|
+
class Comparator {
|
|
19
|
+
public:
|
|
20
|
+
virtual ~Comparator();
|
|
21
|
+
|
|
22
|
+
// Three-way comparison. Returns value:
|
|
23
|
+
// < 0 iff "a" < "b",
|
|
24
|
+
// == 0 iff "a" == "b",
|
|
25
|
+
// > 0 iff "a" > "b"
|
|
26
|
+
virtual int Compare(const Slice& a, const Slice& b) const = 0;
|
|
27
|
+
|
|
28
|
+
// The name of the comparator. Used to check for comparator
|
|
29
|
+
// mismatches (i.e., a DB created with one comparator is
|
|
30
|
+
// accessed using a different comparator.
|
|
31
|
+
//
|
|
32
|
+
// The client of this package should switch to a new name whenever
|
|
33
|
+
// the comparator implementation changes in a way that will cause
|
|
34
|
+
// the relative ordering of any two keys to change.
|
|
35
|
+
//
|
|
36
|
+
// Names starting with "leveldb." are reserved and should not be used
|
|
37
|
+
// by any clients of this package.
|
|
38
|
+
virtual const char* Name() const = 0;
|
|
39
|
+
|
|
40
|
+
// Advanced functions: these are used to reduce the space requirements
|
|
41
|
+
// for internal data structures like index blocks.
|
|
42
|
+
|
|
43
|
+
// If *start < limit, changes *start to a short string in [start,limit).
|
|
44
|
+
// Simple comparator implementations may return with *start unchanged,
|
|
45
|
+
// i.e., an implementation of this method that does nothing is correct.
|
|
46
|
+
virtual void FindShortestSeparator(
|
|
47
|
+
std::string* start,
|
|
48
|
+
const Slice& limit) const = 0;
|
|
49
|
+
|
|
50
|
+
// Changes *key to a short string >= *key.
|
|
51
|
+
// Simple comparator implementations may return with *key unchanged,
|
|
52
|
+
// i.e., an implementation of this method that does nothing is correct.
|
|
53
|
+
virtual void FindShortSuccessor(std::string* key) const = 0;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
// Return a builtin comparator that uses lexicographic byte-wise
|
|
57
|
+
// ordering. The result remains the property of this module and
|
|
58
|
+
// must not be deleted.
|
|
59
|
+
extern const Comparator* BytewiseComparator();
|
|
60
|
+
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
#endif // STORAGE_LEVELDB_INCLUDE_COMPARATOR_H_
|
|
@@ -0,0 +1,148 @@
|
|
|
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
|
+
#ifndef STORAGE_LEVELDB_INCLUDE_DB_H_
|
|
6
|
+
#define STORAGE_LEVELDB_INCLUDE_DB_H_
|
|
7
|
+
|
|
8
|
+
#include <stdint.h>
|
|
9
|
+
#include <stdio.h>
|
|
10
|
+
#include "leveldb/iterator.h"
|
|
11
|
+
#include "leveldb/options.h"
|
|
12
|
+
|
|
13
|
+
namespace leveldb {
|
|
14
|
+
|
|
15
|
+
static const int kMajorVersion = 1;
|
|
16
|
+
static const int kMinorVersion = 2;
|
|
17
|
+
|
|
18
|
+
struct Options;
|
|
19
|
+
struct ReadOptions;
|
|
20
|
+
struct WriteOptions;
|
|
21
|
+
class WriteBatch;
|
|
22
|
+
|
|
23
|
+
// Abstract handle to particular state of a DB.
|
|
24
|
+
// A Snapshot is an immutable object and can therefore be safely
|
|
25
|
+
// accessed from multiple threads without any external synchronization.
|
|
26
|
+
class Snapshot {
|
|
27
|
+
protected:
|
|
28
|
+
virtual ~Snapshot();
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
// A range of keys
|
|
32
|
+
struct Range {
|
|
33
|
+
Slice start; // Included in the range
|
|
34
|
+
Slice limit; // Not included in the range
|
|
35
|
+
|
|
36
|
+
Range(const Slice& s, const Slice& l) : start(s), limit(l) { }
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// A DB is a persistent ordered map from keys to values.
|
|
40
|
+
// A DB is safe for concurrent access from multiple threads without
|
|
41
|
+
// any external synchronization.
|
|
42
|
+
class DB {
|
|
43
|
+
public:
|
|
44
|
+
// Open the database with the specified "name".
|
|
45
|
+
// Stores a pointer to a heap-allocated database in *dbptr and returns
|
|
46
|
+
// OK on success.
|
|
47
|
+
// Stores NULL in *dbptr and returns a non-OK status on error.
|
|
48
|
+
// Caller should delete *dbptr when it is no longer needed.
|
|
49
|
+
static Status Open(const Options& options,
|
|
50
|
+
const std::string& name,
|
|
51
|
+
DB** dbptr);
|
|
52
|
+
|
|
53
|
+
DB() { }
|
|
54
|
+
virtual ~DB();
|
|
55
|
+
|
|
56
|
+
// Set the database entry for "key" to "value". Returns OK on success,
|
|
57
|
+
// and a non-OK status on error.
|
|
58
|
+
// Note: consider setting options.sync = true.
|
|
59
|
+
virtual Status Put(const WriteOptions& options,
|
|
60
|
+
const Slice& key,
|
|
61
|
+
const Slice& value) = 0;
|
|
62
|
+
|
|
63
|
+
// Remove the database entry (if any) for "key". Returns OK on
|
|
64
|
+
// success, and a non-OK status on error. It is not an error if "key"
|
|
65
|
+
// did not exist in the database.
|
|
66
|
+
// Note: consider setting options.sync = true.
|
|
67
|
+
virtual Status Delete(const WriteOptions& options, const Slice& key) = 0;
|
|
68
|
+
|
|
69
|
+
// Apply the specified updates to the database.
|
|
70
|
+
// Returns OK on success, non-OK on failure.
|
|
71
|
+
// Note: consider setting options.sync = true.
|
|
72
|
+
virtual Status Write(const WriteOptions& options, WriteBatch* updates) = 0;
|
|
73
|
+
|
|
74
|
+
// If the database contains an entry for "key" store the
|
|
75
|
+
// corresponding value in *value and return OK.
|
|
76
|
+
//
|
|
77
|
+
// If there is no entry for "key" leave *value unchanged and return
|
|
78
|
+
// a status for which Status::IsNotFound() returns true.
|
|
79
|
+
//
|
|
80
|
+
// May return some other Status on an error.
|
|
81
|
+
virtual Status Get(const ReadOptions& options,
|
|
82
|
+
const Slice& key, std::string* value) = 0;
|
|
83
|
+
|
|
84
|
+
// Return a heap-allocated iterator over the contents of the database.
|
|
85
|
+
// The result of NewIterator() is initially invalid (caller must
|
|
86
|
+
// call one of the Seek methods on the iterator before using it).
|
|
87
|
+
//
|
|
88
|
+
// Caller should delete the iterator when it is no longer needed.
|
|
89
|
+
// The returned iterator should be deleted before this db is deleted.
|
|
90
|
+
virtual Iterator* NewIterator(const ReadOptions& options) = 0;
|
|
91
|
+
|
|
92
|
+
// Return a handle to the current DB state. Iterators created with
|
|
93
|
+
// this handle will all observe a stable snapshot of the current DB
|
|
94
|
+
// state. The caller must call ReleaseSnapshot(result) when the
|
|
95
|
+
// snapshot is no longer needed.
|
|
96
|
+
virtual const Snapshot* GetSnapshot() = 0;
|
|
97
|
+
|
|
98
|
+
// Release a previously acquired snapshot. The caller must not
|
|
99
|
+
// use "snapshot" after this call.
|
|
100
|
+
virtual void ReleaseSnapshot(const Snapshot* snapshot) = 0;
|
|
101
|
+
|
|
102
|
+
// DB implementations can export properties about their state
|
|
103
|
+
// via this method. If "property" is a valid property understood by this
|
|
104
|
+
// DB implementation, fills "*value" with its current value and returns
|
|
105
|
+
// true. Otherwise returns false.
|
|
106
|
+
//
|
|
107
|
+
//
|
|
108
|
+
// Valid property names include:
|
|
109
|
+
//
|
|
110
|
+
// "leveldb.num-files-at-level<N>" - return the number of files at level <N>,
|
|
111
|
+
// where <N> is an ASCII representation of a level number (e.g. "0").
|
|
112
|
+
// "leveldb.stats" - returns a multi-line string that describes statistics
|
|
113
|
+
// about the internal operation of the DB.
|
|
114
|
+
virtual bool GetProperty(const Slice& property, std::string* value) = 0;
|
|
115
|
+
|
|
116
|
+
// For each i in [0,n-1], store in "sizes[i]", the approximate
|
|
117
|
+
// file system space used by keys in "[range[i].start .. range[i].limit)".
|
|
118
|
+
//
|
|
119
|
+
// Note that the returned sizes measure file system space usage, so
|
|
120
|
+
// if the user data compresses by a factor of ten, the returned
|
|
121
|
+
// sizes will be one-tenth the size of the corresponding user data size.
|
|
122
|
+
//
|
|
123
|
+
// The results may not include the sizes of recently written data.
|
|
124
|
+
virtual void GetApproximateSizes(const Range* range, int n,
|
|
125
|
+
uint64_t* sizes) = 0;
|
|
126
|
+
|
|
127
|
+
// Possible extensions:
|
|
128
|
+
// (1) Add a method to compact a range of keys
|
|
129
|
+
|
|
130
|
+
private:
|
|
131
|
+
// No copying allowed
|
|
132
|
+
DB(const DB&);
|
|
133
|
+
void operator=(const DB&);
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
// Destroy the contents of the specified database.
|
|
137
|
+
// Be very careful using this method.
|
|
138
|
+
Status DestroyDB(const std::string& name, const Options& options);
|
|
139
|
+
|
|
140
|
+
// If a DB cannot be opened, you may attempt to call this method to
|
|
141
|
+
// resurrect as much of the contents of the database as possible.
|
|
142
|
+
// Some data may be lost, so be careful when calling this function
|
|
143
|
+
// on a database that contains important information.
|
|
144
|
+
Status RepairDB(const std::string& dbname, const Options& options);
|
|
145
|
+
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
#endif // STORAGE_LEVELDB_INCLUDE_DB_H_
|
|
@@ -0,0 +1,302 @@
|
|
|
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 RandomAccessFile;
|
|
26
|
+
class SequentialFile;
|
|
27
|
+
class Slice;
|
|
28
|
+
class WritableFile;
|
|
29
|
+
|
|
30
|
+
class Env {
|
|
31
|
+
public:
|
|
32
|
+
Env() { }
|
|
33
|
+
virtual ~Env();
|
|
34
|
+
|
|
35
|
+
// Return a default environment suitable for the current operating
|
|
36
|
+
// system. Sophisticated users may wish to provide their own Env
|
|
37
|
+
// implementation instead of relying on this default environment.
|
|
38
|
+
//
|
|
39
|
+
// The result of Default() belongs to leveldb and must never be deleted.
|
|
40
|
+
static Env* Default();
|
|
41
|
+
|
|
42
|
+
// Create a brand new sequentially-readable file with the specified name.
|
|
43
|
+
// On success, stores a pointer to the new file in *result and returns OK.
|
|
44
|
+
// On failure stores NULL in *result and returns non-OK. If the file does
|
|
45
|
+
// not exist, returns a non-OK status.
|
|
46
|
+
//
|
|
47
|
+
// The returned file will only be accessed by one thread at a time.
|
|
48
|
+
virtual Status NewSequentialFile(const std::string& fname,
|
|
49
|
+
SequentialFile** result) = 0;
|
|
50
|
+
|
|
51
|
+
// Create a brand new random access read-only file with the
|
|
52
|
+
// specified name. On success, stores a pointer to the new file in
|
|
53
|
+
// *result and returns OK. On failure stores NULL in *result and
|
|
54
|
+
// returns non-OK. If the file does not exist, returns a non-OK
|
|
55
|
+
// status.
|
|
56
|
+
//
|
|
57
|
+
// The returned file may be concurrently accessed by multiple threads.
|
|
58
|
+
virtual Status NewRandomAccessFile(const std::string& fname,
|
|
59
|
+
RandomAccessFile** result) = 0;
|
|
60
|
+
|
|
61
|
+
// Create an object that writes to a new file with the specified
|
|
62
|
+
// name. Deletes any existing file with the same name and creates a
|
|
63
|
+
// new file. On success, stores a pointer to the new file in
|
|
64
|
+
// *result and returns OK. On failure stores NULL in *result and
|
|
65
|
+
// returns non-OK.
|
|
66
|
+
//
|
|
67
|
+
// The returned file will only be accessed by one thread at a time.
|
|
68
|
+
virtual Status NewWritableFile(const std::string& fname,
|
|
69
|
+
WritableFile** result) = 0;
|
|
70
|
+
|
|
71
|
+
// Returns true iff the named file exists.
|
|
72
|
+
virtual bool FileExists(const std::string& fname) = 0;
|
|
73
|
+
|
|
74
|
+
// Store in *result the names of the children of the specified directory.
|
|
75
|
+
// The names are relative to "dir".
|
|
76
|
+
// Original contents of *results are dropped.
|
|
77
|
+
virtual Status GetChildren(const std::string& dir,
|
|
78
|
+
std::vector<std::string>* result) = 0;
|
|
79
|
+
|
|
80
|
+
// Delete the named file.
|
|
81
|
+
virtual Status DeleteFile(const std::string& fname) = 0;
|
|
82
|
+
|
|
83
|
+
// Create the specified directory.
|
|
84
|
+
virtual Status CreateDir(const std::string& dirname) = 0;
|
|
85
|
+
|
|
86
|
+
// Delete the specified directory.
|
|
87
|
+
virtual Status DeleteDir(const std::string& dirname) = 0;
|
|
88
|
+
|
|
89
|
+
// Store the size of fname in *file_size.
|
|
90
|
+
virtual Status GetFileSize(const std::string& fname, uint64_t* file_size) = 0;
|
|
91
|
+
|
|
92
|
+
// Rename file src to target.
|
|
93
|
+
virtual Status RenameFile(const std::string& src,
|
|
94
|
+
const std::string& target) = 0;
|
|
95
|
+
|
|
96
|
+
// Lock the specified file. Used to prevent concurrent access to
|
|
97
|
+
// the same db by multiple processes. On failure, stores NULL in
|
|
98
|
+
// *lock and returns non-OK.
|
|
99
|
+
//
|
|
100
|
+
// On success, stores a pointer to the object that represents the
|
|
101
|
+
// acquired lock in *lock and returns OK. The caller should call
|
|
102
|
+
// UnlockFile(*lock) to release the lock. If the process exits,
|
|
103
|
+
// the lock will be automatically released.
|
|
104
|
+
//
|
|
105
|
+
// If somebody else already holds the lock, finishes immediately
|
|
106
|
+
// with a failure. I.e., this call does not wait for existing locks
|
|
107
|
+
// to go away.
|
|
108
|
+
//
|
|
109
|
+
// May create the named file if it does not already exist.
|
|
110
|
+
virtual Status LockFile(const std::string& fname, FileLock** lock) = 0;
|
|
111
|
+
|
|
112
|
+
// Release the lock acquired by a previous successful call to LockFile.
|
|
113
|
+
// REQUIRES: lock was returned by a successful LockFile() call
|
|
114
|
+
// REQUIRES: lock has not already been unlocked.
|
|
115
|
+
virtual Status UnlockFile(FileLock* lock) = 0;
|
|
116
|
+
|
|
117
|
+
// Arrange to run "(*function)(arg)" once in a background thread.
|
|
118
|
+
//
|
|
119
|
+
// "function" may run in an unspecified thread. Multiple functions
|
|
120
|
+
// added to the same Env may run concurrently in different threads.
|
|
121
|
+
// I.e., the caller may not assume that background work items are
|
|
122
|
+
// serialized.
|
|
123
|
+
virtual void Schedule(
|
|
124
|
+
void (*function)(void* arg),
|
|
125
|
+
void* arg) = 0;
|
|
126
|
+
|
|
127
|
+
// Start a new thread, invoking "function(arg)" within the new thread.
|
|
128
|
+
// When "function(arg)" returns, the thread will be destroyed.
|
|
129
|
+
virtual void StartThread(void (*function)(void* arg), void* arg) = 0;
|
|
130
|
+
|
|
131
|
+
// *path is set to a temporary directory that can be used for testing. It may
|
|
132
|
+
// or many not have just been created. The directory may or may not differ
|
|
133
|
+
// between runs of the same process, but subsequent calls will return the
|
|
134
|
+
// same directory.
|
|
135
|
+
virtual Status GetTestDirectory(std::string* path) = 0;
|
|
136
|
+
|
|
137
|
+
// Write an entry to the log file with the specified format.
|
|
138
|
+
virtual void Logv(WritableFile* log, const char* format, va_list ap) = 0;
|
|
139
|
+
|
|
140
|
+
// Returns the number of micro-seconds since some fixed point in time. Only
|
|
141
|
+
// useful for computing deltas of time.
|
|
142
|
+
virtual uint64_t NowMicros() = 0;
|
|
143
|
+
|
|
144
|
+
// Sleep/delay the thread for the perscribed number of micro-seconds.
|
|
145
|
+
virtual void SleepForMicroseconds(int micros) = 0;
|
|
146
|
+
|
|
147
|
+
private:
|
|
148
|
+
// No copying allowed
|
|
149
|
+
Env(const Env&);
|
|
150
|
+
void operator=(const Env&);
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
// A file abstraction for reading sequentially through a file
|
|
154
|
+
class SequentialFile {
|
|
155
|
+
public:
|
|
156
|
+
SequentialFile() { }
|
|
157
|
+
virtual ~SequentialFile();
|
|
158
|
+
|
|
159
|
+
// Read up to "n" bytes from the file. "scratch[0..n-1]" may be
|
|
160
|
+
// written by this routine. Sets "*result" to the data that was
|
|
161
|
+
// read (including if fewer than "n" bytes were successfully read).
|
|
162
|
+
// If an error was encountered, returns a non-OK status.
|
|
163
|
+
//
|
|
164
|
+
// REQUIRES: External synchronization
|
|
165
|
+
virtual Status Read(size_t n, Slice* result, char* scratch) = 0;
|
|
166
|
+
|
|
167
|
+
// Skip "n" bytes from the file. This is guaranteed to be no
|
|
168
|
+
// slower that reading the same data, but may be faster.
|
|
169
|
+
//
|
|
170
|
+
// If end of file is reached, skipping will stop at the end of the
|
|
171
|
+
// file, and Skip will return OK.
|
|
172
|
+
//
|
|
173
|
+
// REQUIRES: External synchronization
|
|
174
|
+
virtual Status Skip(uint64_t n) = 0;
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
// A file abstraction for randomly reading the contents of a file.
|
|
178
|
+
class RandomAccessFile {
|
|
179
|
+
public:
|
|
180
|
+
RandomAccessFile() { }
|
|
181
|
+
virtual ~RandomAccessFile();
|
|
182
|
+
|
|
183
|
+
// Read up to "n" bytes from the file starting at "offset".
|
|
184
|
+
// "scratch[0..n-1]" may be written by this routine. Sets "*result"
|
|
185
|
+
// to the data that was read (including if fewer than "n" bytes were
|
|
186
|
+
// successfully read). If an error was encountered, returns a
|
|
187
|
+
// non-OK status.
|
|
188
|
+
//
|
|
189
|
+
// Safe for concurrent use by multiple threads.
|
|
190
|
+
virtual Status Read(uint64_t offset, size_t n, Slice* result,
|
|
191
|
+
char* scratch) const = 0;
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
// A file abstraction for sequential writing. The implementation
|
|
195
|
+
// must provide buffering since callers may append small fragments
|
|
196
|
+
// at a time to the file.
|
|
197
|
+
class WritableFile {
|
|
198
|
+
public:
|
|
199
|
+
WritableFile() { }
|
|
200
|
+
virtual ~WritableFile();
|
|
201
|
+
|
|
202
|
+
virtual Status Append(const Slice& data) = 0;
|
|
203
|
+
virtual Status Close() = 0;
|
|
204
|
+
virtual Status Flush() = 0;
|
|
205
|
+
virtual Status Sync() = 0;
|
|
206
|
+
|
|
207
|
+
private:
|
|
208
|
+
// No copying allowed
|
|
209
|
+
WritableFile(const WritableFile&);
|
|
210
|
+
void operator=(const WritableFile&);
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
// Identifies a locked file.
|
|
214
|
+
class FileLock {
|
|
215
|
+
public:
|
|
216
|
+
FileLock() { }
|
|
217
|
+
virtual ~FileLock();
|
|
218
|
+
private:
|
|
219
|
+
// No copying allowed
|
|
220
|
+
FileLock(const FileLock&);
|
|
221
|
+
void operator=(const FileLock&);
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
// Log the specified data to *info_log if info_log is non-NULL.
|
|
225
|
+
extern void Log(Env* env, WritableFile* info_log, const char* format, ...)
|
|
226
|
+
# if defined(__GNUC__) || defined(__clang__)
|
|
227
|
+
__attribute__((__format__ (__printf__, 3, 4)))
|
|
228
|
+
# endif
|
|
229
|
+
;
|
|
230
|
+
|
|
231
|
+
// A utility routine: write "data" to the named file.
|
|
232
|
+
extern Status WriteStringToFile(Env* env, const Slice& data,
|
|
233
|
+
const std::string& fname);
|
|
234
|
+
|
|
235
|
+
// A utility routine: read contents of named file into *data
|
|
236
|
+
extern Status ReadFileToString(Env* env, const std::string& fname,
|
|
237
|
+
std::string* data);
|
|
238
|
+
|
|
239
|
+
// An implementation of Env that forwards all calls to another Env.
|
|
240
|
+
// May be useful to clients who wish to override just part of the
|
|
241
|
+
// functionality of another Env.
|
|
242
|
+
class EnvWrapper : public Env {
|
|
243
|
+
public:
|
|
244
|
+
// Initialize an EnvWrapper that delegates all calls to *target
|
|
245
|
+
explicit EnvWrapper(Env* target) : target_(target) { }
|
|
246
|
+
virtual ~EnvWrapper();
|
|
247
|
+
|
|
248
|
+
// Return the target to which this Env forwards all calls
|
|
249
|
+
Env* target() const { return target_; }
|
|
250
|
+
|
|
251
|
+
// The following text is boilerplate that forwards all methods to target()
|
|
252
|
+
Status NewSequentialFile(const std::string& f, SequentialFile** r) {
|
|
253
|
+
return target_->NewSequentialFile(f, r);
|
|
254
|
+
}
|
|
255
|
+
Status NewRandomAccessFile(const std::string& f, RandomAccessFile** r) {
|
|
256
|
+
return target_->NewRandomAccessFile(f, r);
|
|
257
|
+
}
|
|
258
|
+
Status NewWritableFile(const std::string& f, WritableFile** r) {
|
|
259
|
+
return target_->NewWritableFile(f, r);
|
|
260
|
+
}
|
|
261
|
+
bool FileExists(const std::string& f) { return target_->FileExists(f); }
|
|
262
|
+
Status GetChildren(const std::string& dir, std::vector<std::string>* r) {
|
|
263
|
+
return target_->GetChildren(dir, r);
|
|
264
|
+
}
|
|
265
|
+
Status DeleteFile(const std::string& f) { return target_->DeleteFile(f); }
|
|
266
|
+
Status CreateDir(const std::string& d) { return target_->CreateDir(d); }
|
|
267
|
+
Status DeleteDir(const std::string& d) { return target_->DeleteDir(d); }
|
|
268
|
+
Status GetFileSize(const std::string& f, uint64_t* s) {
|
|
269
|
+
return target_->GetFileSize(f, s);
|
|
270
|
+
}
|
|
271
|
+
Status RenameFile(const std::string& s, const std::string& t) {
|
|
272
|
+
return target_->RenameFile(s, t);
|
|
273
|
+
}
|
|
274
|
+
Status LockFile(const std::string& f, FileLock** l) {
|
|
275
|
+
return target_->LockFile(f, l);
|
|
276
|
+
}
|
|
277
|
+
Status UnlockFile(FileLock* l) { return target_->UnlockFile(l); }
|
|
278
|
+
void Schedule(void (*f)(void*), void* a) {
|
|
279
|
+
return target_->Schedule(f, a);
|
|
280
|
+
}
|
|
281
|
+
void StartThread(void (*f)(void*), void* a) {
|
|
282
|
+
return target_->StartThread(f, a);
|
|
283
|
+
}
|
|
284
|
+
virtual Status GetTestDirectory(std::string* path) {
|
|
285
|
+
return target_->GetTestDirectory(path);
|
|
286
|
+
}
|
|
287
|
+
virtual void Logv(WritableFile* log, const char* format, va_list ap) {
|
|
288
|
+
return target_->Logv(log, format, ap);
|
|
289
|
+
}
|
|
290
|
+
uint64_t NowMicros() {
|
|
291
|
+
return target_->NowMicros();
|
|
292
|
+
}
|
|
293
|
+
void SleepForMicroseconds(int micros) {
|
|
294
|
+
target_->SleepForMicroseconds(micros);
|
|
295
|
+
}
|
|
296
|
+
private:
|
|
297
|
+
Env* target_;
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
#endif // STORAGE_LEVELDB_INCLUDE_ENV_H_
|