leveldb 0.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.
Files changed (128) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +22 -0
  3. data/README.md +95 -0
  4. data/ext/Rakefile +11 -0
  5. data/ext/leveldb/LICENSE +27 -0
  6. data/ext/leveldb/Makefile +206 -0
  7. data/ext/leveldb/build_config.mk +13 -0
  8. data/ext/leveldb/db/builder.cc +88 -0
  9. data/ext/leveldb/db/builder.h +34 -0
  10. data/ext/leveldb/db/c.cc +595 -0
  11. data/ext/leveldb/db/c_test.c +390 -0
  12. data/ext/leveldb/db/corruption_test.cc +359 -0
  13. data/ext/leveldb/db/db_bench.cc +979 -0
  14. data/ext/leveldb/db/db_impl.cc +1485 -0
  15. data/ext/leveldb/db/db_impl.h +203 -0
  16. data/ext/leveldb/db/db_iter.cc +299 -0
  17. data/ext/leveldb/db/db_iter.h +26 -0
  18. data/ext/leveldb/db/db_test.cc +2092 -0
  19. data/ext/leveldb/db/dbformat.cc +140 -0
  20. data/ext/leveldb/db/dbformat.h +227 -0
  21. data/ext/leveldb/db/dbformat_test.cc +112 -0
  22. data/ext/leveldb/db/filename.cc +139 -0
  23. data/ext/leveldb/db/filename.h +80 -0
  24. data/ext/leveldb/db/filename_test.cc +122 -0
  25. data/ext/leveldb/db/leveldb_main.cc +238 -0
  26. data/ext/leveldb/db/log_format.h +35 -0
  27. data/ext/leveldb/db/log_reader.cc +259 -0
  28. data/ext/leveldb/db/log_reader.h +108 -0
  29. data/ext/leveldb/db/log_test.cc +500 -0
  30. data/ext/leveldb/db/log_writer.cc +103 -0
  31. data/ext/leveldb/db/log_writer.h +48 -0
  32. data/ext/leveldb/db/memtable.cc +145 -0
  33. data/ext/leveldb/db/memtable.h +91 -0
  34. data/ext/leveldb/db/repair.cc +389 -0
  35. data/ext/leveldb/db/skiplist.h +379 -0
  36. data/ext/leveldb/db/skiplist_test.cc +378 -0
  37. data/ext/leveldb/db/snapshot.h +66 -0
  38. data/ext/leveldb/db/table_cache.cc +121 -0
  39. data/ext/leveldb/db/table_cache.h +61 -0
  40. data/ext/leveldb/db/version_edit.cc +266 -0
  41. data/ext/leveldb/db/version_edit.h +107 -0
  42. data/ext/leveldb/db/version_edit_test.cc +46 -0
  43. data/ext/leveldb/db/version_set.cc +1443 -0
  44. data/ext/leveldb/db/version_set.h +383 -0
  45. data/ext/leveldb/db/version_set_test.cc +179 -0
  46. data/ext/leveldb/db/write_batch.cc +147 -0
  47. data/ext/leveldb/db/write_batch_internal.h +49 -0
  48. data/ext/leveldb/db/write_batch_test.cc +120 -0
  49. data/ext/leveldb/doc/bench/db_bench_sqlite3.cc +718 -0
  50. data/ext/leveldb/doc/bench/db_bench_tree_db.cc +528 -0
  51. data/ext/leveldb/helpers/memenv/memenv.cc +384 -0
  52. data/ext/leveldb/helpers/memenv/memenv.h +20 -0
  53. data/ext/leveldb/helpers/memenv/memenv_test.cc +232 -0
  54. data/ext/leveldb/include/leveldb/c.h +291 -0
  55. data/ext/leveldb/include/leveldb/cache.h +99 -0
  56. data/ext/leveldb/include/leveldb/comparator.h +63 -0
  57. data/ext/leveldb/include/leveldb/db.h +161 -0
  58. data/ext/leveldb/include/leveldb/env.h +333 -0
  59. data/ext/leveldb/include/leveldb/filter_policy.h +70 -0
  60. data/ext/leveldb/include/leveldb/iterator.h +100 -0
  61. data/ext/leveldb/include/leveldb/options.h +195 -0
  62. data/ext/leveldb/include/leveldb/slice.h +109 -0
  63. data/ext/leveldb/include/leveldb/status.h +106 -0
  64. data/ext/leveldb/include/leveldb/table.h +85 -0
  65. data/ext/leveldb/include/leveldb/table_builder.h +92 -0
  66. data/ext/leveldb/include/leveldb/write_batch.h +64 -0
  67. data/ext/leveldb/issues/issue178_test.cc +92 -0
  68. data/ext/leveldb/port/atomic_pointer.h +224 -0
  69. data/ext/leveldb/port/port.h +19 -0
  70. data/ext/leveldb/port/port_example.h +135 -0
  71. data/ext/leveldb/port/port_posix.cc +54 -0
  72. data/ext/leveldb/port/port_posix.h +157 -0
  73. data/ext/leveldb/port/thread_annotations.h +59 -0
  74. data/ext/leveldb/port/win/stdint.h +24 -0
  75. data/ext/leveldb/table/block.cc +268 -0
  76. data/ext/leveldb/table/block.h +44 -0
  77. data/ext/leveldb/table/block_builder.cc +109 -0
  78. data/ext/leveldb/table/block_builder.h +57 -0
  79. data/ext/leveldb/table/filter_block.cc +111 -0
  80. data/ext/leveldb/table/filter_block.h +68 -0
  81. data/ext/leveldb/table/filter_block_test.cc +128 -0
  82. data/ext/leveldb/table/format.cc +145 -0
  83. data/ext/leveldb/table/format.h +108 -0
  84. data/ext/leveldb/table/iterator.cc +67 -0
  85. data/ext/leveldb/table/iterator_wrapper.h +63 -0
  86. data/ext/leveldb/table/merger.cc +197 -0
  87. data/ext/leveldb/table/merger.h +26 -0
  88. data/ext/leveldb/table/table.cc +275 -0
  89. data/ext/leveldb/table/table_builder.cc +270 -0
  90. data/ext/leveldb/table/table_test.cc +868 -0
  91. data/ext/leveldb/table/two_level_iterator.cc +182 -0
  92. data/ext/leveldb/table/two_level_iterator.h +34 -0
  93. data/ext/leveldb/util/arena.cc +68 -0
  94. data/ext/leveldb/util/arena.h +68 -0
  95. data/ext/leveldb/util/arena_test.cc +68 -0
  96. data/ext/leveldb/util/bloom.cc +95 -0
  97. data/ext/leveldb/util/bloom_test.cc +160 -0
  98. data/ext/leveldb/util/cache.cc +325 -0
  99. data/ext/leveldb/util/cache_test.cc +186 -0
  100. data/ext/leveldb/util/coding.cc +194 -0
  101. data/ext/leveldb/util/coding.h +104 -0
  102. data/ext/leveldb/util/coding_test.cc +196 -0
  103. data/ext/leveldb/util/comparator.cc +81 -0
  104. data/ext/leveldb/util/crc32c.cc +332 -0
  105. data/ext/leveldb/util/crc32c.h +45 -0
  106. data/ext/leveldb/util/crc32c_test.cc +72 -0
  107. data/ext/leveldb/util/env.cc +96 -0
  108. data/ext/leveldb/util/env_posix.cc +698 -0
  109. data/ext/leveldb/util/env_test.cc +104 -0
  110. data/ext/leveldb/util/filter_policy.cc +11 -0
  111. data/ext/leveldb/util/hash.cc +52 -0
  112. data/ext/leveldb/util/hash.h +19 -0
  113. data/ext/leveldb/util/histogram.cc +139 -0
  114. data/ext/leveldb/util/histogram.h +42 -0
  115. data/ext/leveldb/util/logging.cc +81 -0
  116. data/ext/leveldb/util/logging.h +47 -0
  117. data/ext/leveldb/util/mutexlock.h +41 -0
  118. data/ext/leveldb/util/options.cc +29 -0
  119. data/ext/leveldb/util/posix_logger.h +98 -0
  120. data/ext/leveldb/util/random.h +59 -0
  121. data/ext/leveldb/util/status.cc +75 -0
  122. data/ext/leveldb/util/testharness.cc +77 -0
  123. data/ext/leveldb/util/testharness.h +138 -0
  124. data/ext/leveldb/util/testutil.cc +51 -0
  125. data/ext/leveldb/util/testutil.h +53 -0
  126. data/lib/leveldb/version.rb +3 -0
  127. data/lib/leveldb.rb +1006 -0
  128. metadata +228 -0
@@ -0,0 +1,378 @@
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 "db/skiplist.h"
6
+ #include <set>
7
+ #include "leveldb/env.h"
8
+ #include "util/arena.h"
9
+ #include "util/hash.h"
10
+ #include "util/random.h"
11
+ #include "util/testharness.h"
12
+
13
+ namespace leveldb {
14
+
15
+ typedef uint64_t Key;
16
+
17
+ struct Comparator {
18
+ int operator()(const Key& a, const Key& b) const {
19
+ if (a < b) {
20
+ return -1;
21
+ } else if (a > b) {
22
+ return +1;
23
+ } else {
24
+ return 0;
25
+ }
26
+ }
27
+ };
28
+
29
+ class SkipTest { };
30
+
31
+ TEST(SkipTest, Empty) {
32
+ Arena arena;
33
+ Comparator cmp;
34
+ SkipList<Key, Comparator> list(cmp, &arena);
35
+ ASSERT_TRUE(!list.Contains(10));
36
+
37
+ SkipList<Key, Comparator>::Iterator iter(&list);
38
+ ASSERT_TRUE(!iter.Valid());
39
+ iter.SeekToFirst();
40
+ ASSERT_TRUE(!iter.Valid());
41
+ iter.Seek(100);
42
+ ASSERT_TRUE(!iter.Valid());
43
+ iter.SeekToLast();
44
+ ASSERT_TRUE(!iter.Valid());
45
+ }
46
+
47
+ TEST(SkipTest, InsertAndLookup) {
48
+ const int N = 2000;
49
+ const int R = 5000;
50
+ Random rnd(1000);
51
+ std::set<Key> keys;
52
+ Arena arena;
53
+ Comparator cmp;
54
+ SkipList<Key, Comparator> list(cmp, &arena);
55
+ for (int i = 0; i < N; i++) {
56
+ Key key = rnd.Next() % R;
57
+ if (keys.insert(key).second) {
58
+ list.Insert(key);
59
+ }
60
+ }
61
+
62
+ for (int i = 0; i < R; i++) {
63
+ if (list.Contains(i)) {
64
+ ASSERT_EQ(keys.count(i), 1);
65
+ } else {
66
+ ASSERT_EQ(keys.count(i), 0);
67
+ }
68
+ }
69
+
70
+ // Simple iterator tests
71
+ {
72
+ SkipList<Key, Comparator>::Iterator iter(&list);
73
+ ASSERT_TRUE(!iter.Valid());
74
+
75
+ iter.Seek(0);
76
+ ASSERT_TRUE(iter.Valid());
77
+ ASSERT_EQ(*(keys.begin()), iter.key());
78
+
79
+ iter.SeekToFirst();
80
+ ASSERT_TRUE(iter.Valid());
81
+ ASSERT_EQ(*(keys.begin()), iter.key());
82
+
83
+ iter.SeekToLast();
84
+ ASSERT_TRUE(iter.Valid());
85
+ ASSERT_EQ(*(keys.rbegin()), iter.key());
86
+ }
87
+
88
+ // Forward iteration test
89
+ for (int i = 0; i < R; i++) {
90
+ SkipList<Key, Comparator>::Iterator iter(&list);
91
+ iter.Seek(i);
92
+
93
+ // Compare against model iterator
94
+ std::set<Key>::iterator model_iter = keys.lower_bound(i);
95
+ for (int j = 0; j < 3; j++) {
96
+ if (model_iter == keys.end()) {
97
+ ASSERT_TRUE(!iter.Valid());
98
+ break;
99
+ } else {
100
+ ASSERT_TRUE(iter.Valid());
101
+ ASSERT_EQ(*model_iter, iter.key());
102
+ ++model_iter;
103
+ iter.Next();
104
+ }
105
+ }
106
+ }
107
+
108
+ // Backward iteration test
109
+ {
110
+ SkipList<Key, Comparator>::Iterator iter(&list);
111
+ iter.SeekToLast();
112
+
113
+ // Compare against model iterator
114
+ for (std::set<Key>::reverse_iterator model_iter = keys.rbegin();
115
+ model_iter != keys.rend();
116
+ ++model_iter) {
117
+ ASSERT_TRUE(iter.Valid());
118
+ ASSERT_EQ(*model_iter, iter.key());
119
+ iter.Prev();
120
+ }
121
+ ASSERT_TRUE(!iter.Valid());
122
+ }
123
+ }
124
+
125
+ // We want to make sure that with a single writer and multiple
126
+ // concurrent readers (with no synchronization other than when a
127
+ // reader's iterator is created), the reader always observes all the
128
+ // data that was present in the skip list when the iterator was
129
+ // constructor. Because insertions are happening concurrently, we may
130
+ // also observe new values that were inserted since the iterator was
131
+ // constructed, but we should never miss any values that were present
132
+ // at iterator construction time.
133
+ //
134
+ // We generate multi-part keys:
135
+ // <key,gen,hash>
136
+ // where:
137
+ // key is in range [0..K-1]
138
+ // gen is a generation number for key
139
+ // hash is hash(key,gen)
140
+ //
141
+ // The insertion code picks a random key, sets gen to be 1 + the last
142
+ // generation number inserted for that key, and sets hash to Hash(key,gen).
143
+ //
144
+ // At the beginning of a read, we snapshot the last inserted
145
+ // generation number for each key. We then iterate, including random
146
+ // calls to Next() and Seek(). For every key we encounter, we
147
+ // check that it is either expected given the initial snapshot or has
148
+ // been concurrently added since the iterator started.
149
+ class ConcurrentTest {
150
+ private:
151
+ static const uint32_t K = 4;
152
+
153
+ static uint64_t key(Key key) { return (key >> 40); }
154
+ static uint64_t gen(Key key) { return (key >> 8) & 0xffffffffu; }
155
+ static uint64_t hash(Key key) { return key & 0xff; }
156
+
157
+ static uint64_t HashNumbers(uint64_t k, uint64_t g) {
158
+ uint64_t data[2] = { k, g };
159
+ return Hash(reinterpret_cast<char*>(data), sizeof(data), 0);
160
+ }
161
+
162
+ static Key MakeKey(uint64_t k, uint64_t g) {
163
+ assert(sizeof(Key) == sizeof(uint64_t));
164
+ assert(k <= K); // We sometimes pass K to seek to the end of the skiplist
165
+ assert(g <= 0xffffffffu);
166
+ return ((k << 40) | (g << 8) | (HashNumbers(k, g) & 0xff));
167
+ }
168
+
169
+ static bool IsValidKey(Key k) {
170
+ return hash(k) == (HashNumbers(key(k), gen(k)) & 0xff);
171
+ }
172
+
173
+ static Key RandomTarget(Random* rnd) {
174
+ switch (rnd->Next() % 10) {
175
+ case 0:
176
+ // Seek to beginning
177
+ return MakeKey(0, 0);
178
+ case 1:
179
+ // Seek to end
180
+ return MakeKey(K, 0);
181
+ default:
182
+ // Seek to middle
183
+ return MakeKey(rnd->Next() % K, 0);
184
+ }
185
+ }
186
+
187
+ // Per-key generation
188
+ struct State {
189
+ port::AtomicPointer generation[K];
190
+ void Set(int k, intptr_t v) {
191
+ generation[k].Release_Store(reinterpret_cast<void*>(v));
192
+ }
193
+ intptr_t Get(int k) {
194
+ return reinterpret_cast<intptr_t>(generation[k].Acquire_Load());
195
+ }
196
+
197
+ State() {
198
+ for (int k = 0; k < K; k++) {
199
+ Set(k, 0);
200
+ }
201
+ }
202
+ };
203
+
204
+ // Current state of the test
205
+ State current_;
206
+
207
+ Arena arena_;
208
+
209
+ // SkipList is not protected by mu_. We just use a single writer
210
+ // thread to modify it.
211
+ SkipList<Key, Comparator> list_;
212
+
213
+ public:
214
+ ConcurrentTest() : list_(Comparator(), &arena_) { }
215
+
216
+ // REQUIRES: External synchronization
217
+ void WriteStep(Random* rnd) {
218
+ const uint32_t k = rnd->Next() % K;
219
+ const intptr_t g = current_.Get(k) + 1;
220
+ const Key key = MakeKey(k, g);
221
+ list_.Insert(key);
222
+ current_.Set(k, g);
223
+ }
224
+
225
+ void ReadStep(Random* rnd) {
226
+ // Remember the initial committed state of the skiplist.
227
+ State initial_state;
228
+ for (int k = 0; k < K; k++) {
229
+ initial_state.Set(k, current_.Get(k));
230
+ }
231
+
232
+ Key pos = RandomTarget(rnd);
233
+ SkipList<Key, Comparator>::Iterator iter(&list_);
234
+ iter.Seek(pos);
235
+ while (true) {
236
+ Key current;
237
+ if (!iter.Valid()) {
238
+ current = MakeKey(K, 0);
239
+ } else {
240
+ current = iter.key();
241
+ ASSERT_TRUE(IsValidKey(current)) << current;
242
+ }
243
+ ASSERT_LE(pos, current) << "should not go backwards";
244
+
245
+ // Verify that everything in [pos,current) was not present in
246
+ // initial_state.
247
+ while (pos < current) {
248
+ ASSERT_LT(key(pos), K) << pos;
249
+
250
+ // Note that generation 0 is never inserted, so it is ok if
251
+ // <*,0,*> is missing.
252
+ ASSERT_TRUE((gen(pos) == 0) ||
253
+ (gen(pos) > initial_state.Get(key(pos)))
254
+ ) << "key: " << key(pos)
255
+ << "; gen: " << gen(pos)
256
+ << "; initgen: "
257
+ << initial_state.Get(key(pos));
258
+
259
+ // Advance to next key in the valid key space
260
+ if (key(pos) < key(current)) {
261
+ pos = MakeKey(key(pos) + 1, 0);
262
+ } else {
263
+ pos = MakeKey(key(pos), gen(pos) + 1);
264
+ }
265
+ }
266
+
267
+ if (!iter.Valid()) {
268
+ break;
269
+ }
270
+
271
+ if (rnd->Next() % 2) {
272
+ iter.Next();
273
+ pos = MakeKey(key(pos), gen(pos) + 1);
274
+ } else {
275
+ Key new_target = RandomTarget(rnd);
276
+ if (new_target > pos) {
277
+ pos = new_target;
278
+ iter.Seek(new_target);
279
+ }
280
+ }
281
+ }
282
+ }
283
+ };
284
+ const uint32_t ConcurrentTest::K;
285
+
286
+ // Simple test that does single-threaded testing of the ConcurrentTest
287
+ // scaffolding.
288
+ TEST(SkipTest, ConcurrentWithoutThreads) {
289
+ ConcurrentTest test;
290
+ Random rnd(test::RandomSeed());
291
+ for (int i = 0; i < 10000; i++) {
292
+ test.ReadStep(&rnd);
293
+ test.WriteStep(&rnd);
294
+ }
295
+ }
296
+
297
+ class TestState {
298
+ public:
299
+ ConcurrentTest t_;
300
+ int seed_;
301
+ port::AtomicPointer quit_flag_;
302
+
303
+ enum ReaderState {
304
+ STARTING,
305
+ RUNNING,
306
+ DONE
307
+ };
308
+
309
+ explicit TestState(int s)
310
+ : seed_(s),
311
+ quit_flag_(NULL),
312
+ state_(STARTING),
313
+ state_cv_(&mu_) {}
314
+
315
+ void Wait(ReaderState s) {
316
+ mu_.Lock();
317
+ while (state_ != s) {
318
+ state_cv_.Wait();
319
+ }
320
+ mu_.Unlock();
321
+ }
322
+
323
+ void Change(ReaderState s) {
324
+ mu_.Lock();
325
+ state_ = s;
326
+ state_cv_.Signal();
327
+ mu_.Unlock();
328
+ }
329
+
330
+ private:
331
+ port::Mutex mu_;
332
+ ReaderState state_;
333
+ port::CondVar state_cv_;
334
+ };
335
+
336
+ static void ConcurrentReader(void* arg) {
337
+ TestState* state = reinterpret_cast<TestState*>(arg);
338
+ Random rnd(state->seed_);
339
+ int64_t reads = 0;
340
+ state->Change(TestState::RUNNING);
341
+ while (!state->quit_flag_.Acquire_Load()) {
342
+ state->t_.ReadStep(&rnd);
343
+ ++reads;
344
+ }
345
+ state->Change(TestState::DONE);
346
+ }
347
+
348
+ static void RunConcurrent(int run) {
349
+ const int seed = test::RandomSeed() + (run * 100);
350
+ Random rnd(seed);
351
+ const int N = 1000;
352
+ const int kSize = 1000;
353
+ for (int i = 0; i < N; i++) {
354
+ if ((i % 100) == 0) {
355
+ fprintf(stderr, "Run %d of %d\n", i, N);
356
+ }
357
+ TestState state(seed + 1);
358
+ Env::Default()->Schedule(ConcurrentReader, &state);
359
+ state.Wait(TestState::RUNNING);
360
+ for (int i = 0; i < kSize; i++) {
361
+ state.t_.WriteStep(&rnd);
362
+ }
363
+ state.quit_flag_.Release_Store(&state); // Any non-NULL arg will do
364
+ state.Wait(TestState::DONE);
365
+ }
366
+ }
367
+
368
+ TEST(SkipTest, Concurrent1) { RunConcurrent(1); }
369
+ TEST(SkipTest, Concurrent2) { RunConcurrent(2); }
370
+ TEST(SkipTest, Concurrent3) { RunConcurrent(3); }
371
+ TEST(SkipTest, Concurrent4) { RunConcurrent(4); }
372
+ TEST(SkipTest, Concurrent5) { RunConcurrent(5); }
373
+
374
+ } // namespace leveldb
375
+
376
+ int main(int argc, char** argv) {
377
+ return leveldb::test::RunAllTests();
378
+ }
@@ -0,0 +1,66 @@
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_DB_SNAPSHOT_H_
6
+ #define STORAGE_LEVELDB_DB_SNAPSHOT_H_
7
+
8
+ #include "leveldb/db.h"
9
+
10
+ namespace leveldb {
11
+
12
+ class SnapshotList;
13
+
14
+ // Snapshots are kept in a doubly-linked list in the DB.
15
+ // Each SnapshotImpl corresponds to a particular sequence number.
16
+ class SnapshotImpl : public Snapshot {
17
+ public:
18
+ SequenceNumber number_; // const after creation
19
+
20
+ private:
21
+ friend class SnapshotList;
22
+
23
+ // SnapshotImpl is kept in a doubly-linked circular list
24
+ SnapshotImpl* prev_;
25
+ SnapshotImpl* next_;
26
+
27
+ SnapshotList* list_; // just for sanity checks
28
+ };
29
+
30
+ class SnapshotList {
31
+ public:
32
+ SnapshotList() {
33
+ list_.prev_ = &list_;
34
+ list_.next_ = &list_;
35
+ }
36
+
37
+ bool empty() const { return list_.next_ == &list_; }
38
+ SnapshotImpl* oldest() const { assert(!empty()); return list_.next_; }
39
+ SnapshotImpl* newest() const { assert(!empty()); return list_.prev_; }
40
+
41
+ const SnapshotImpl* New(SequenceNumber seq) {
42
+ SnapshotImpl* s = new SnapshotImpl;
43
+ s->number_ = seq;
44
+ s->list_ = this;
45
+ s->next_ = &list_;
46
+ s->prev_ = list_.prev_;
47
+ s->prev_->next_ = s;
48
+ s->next_->prev_ = s;
49
+ return s;
50
+ }
51
+
52
+ void Delete(const SnapshotImpl* s) {
53
+ assert(s->list_ == this);
54
+ s->prev_->next_ = s->next_;
55
+ s->next_->prev_ = s->prev_;
56
+ delete s;
57
+ }
58
+
59
+ private:
60
+ // Dummy head of doubly-linked list of snapshots
61
+ SnapshotImpl list_;
62
+ };
63
+
64
+ } // namespace leveldb
65
+
66
+ #endif // STORAGE_LEVELDB_DB_SNAPSHOT_H_
@@ -0,0 +1,121 @@
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 "db/table_cache.h"
6
+
7
+ #include "db/filename.h"
8
+ #include "leveldb/env.h"
9
+ #include "leveldb/table.h"
10
+ #include "util/coding.h"
11
+
12
+ namespace leveldb {
13
+
14
+ struct TableAndFile {
15
+ RandomAccessFile* file;
16
+ Table* table;
17
+ };
18
+
19
+ static void DeleteEntry(const Slice& key, void* value) {
20
+ TableAndFile* tf = reinterpret_cast<TableAndFile*>(value);
21
+ delete tf->table;
22
+ delete tf->file;
23
+ delete tf;
24
+ }
25
+
26
+ static void UnrefEntry(void* arg1, void* arg2) {
27
+ Cache* cache = reinterpret_cast<Cache*>(arg1);
28
+ Cache::Handle* h = reinterpret_cast<Cache::Handle*>(arg2);
29
+ cache->Release(h);
30
+ }
31
+
32
+ TableCache::TableCache(const std::string& dbname,
33
+ const Options* options,
34
+ int entries)
35
+ : env_(options->env),
36
+ dbname_(dbname),
37
+ options_(options),
38
+ cache_(NewLRUCache(entries)) {
39
+ }
40
+
41
+ TableCache::~TableCache() {
42
+ delete cache_;
43
+ }
44
+
45
+ Status TableCache::FindTable(uint64_t file_number, uint64_t file_size,
46
+ Cache::Handle** handle) {
47
+ Status s;
48
+ char buf[sizeof(file_number)];
49
+ EncodeFixed64(buf, file_number);
50
+ Slice key(buf, sizeof(buf));
51
+ *handle = cache_->Lookup(key);
52
+ if (*handle == NULL) {
53
+ std::string fname = TableFileName(dbname_, file_number);
54
+ RandomAccessFile* file = NULL;
55
+ Table* table = NULL;
56
+ s = env_->NewRandomAccessFile(fname, &file);
57
+ if (s.ok()) {
58
+ s = Table::Open(*options_, file, file_size, &table);
59
+ }
60
+
61
+ if (!s.ok()) {
62
+ assert(table == NULL);
63
+ delete file;
64
+ // We do not cache error results so that if the error is transient,
65
+ // or somebody repairs the file, we recover automatically.
66
+ } else {
67
+ TableAndFile* tf = new TableAndFile;
68
+ tf->file = file;
69
+ tf->table = table;
70
+ *handle = cache_->Insert(key, tf, 1, &DeleteEntry);
71
+ }
72
+ }
73
+ return s;
74
+ }
75
+
76
+ Iterator* TableCache::NewIterator(const ReadOptions& options,
77
+ uint64_t file_number,
78
+ uint64_t file_size,
79
+ Table** tableptr) {
80
+ if (tableptr != NULL) {
81
+ *tableptr = NULL;
82
+ }
83
+
84
+ Cache::Handle* handle = NULL;
85
+ Status s = FindTable(file_number, file_size, &handle);
86
+ if (!s.ok()) {
87
+ return NewErrorIterator(s);
88
+ }
89
+
90
+ Table* table = reinterpret_cast<TableAndFile*>(cache_->Value(handle))->table;
91
+ Iterator* result = table->NewIterator(options);
92
+ result->RegisterCleanup(&UnrefEntry, cache_, handle);
93
+ if (tableptr != NULL) {
94
+ *tableptr = table;
95
+ }
96
+ return result;
97
+ }
98
+
99
+ Status TableCache::Get(const ReadOptions& options,
100
+ uint64_t file_number,
101
+ uint64_t file_size,
102
+ const Slice& k,
103
+ void* arg,
104
+ void (*saver)(void*, const Slice&, const Slice&)) {
105
+ Cache::Handle* handle = NULL;
106
+ Status s = FindTable(file_number, file_size, &handle);
107
+ if (s.ok()) {
108
+ Table* t = reinterpret_cast<TableAndFile*>(cache_->Value(handle))->table;
109
+ s = t->InternalGet(options, k, arg, saver);
110
+ cache_->Release(handle);
111
+ }
112
+ return s;
113
+ }
114
+
115
+ void TableCache::Evict(uint64_t file_number) {
116
+ char buf[sizeof(file_number)];
117
+ EncodeFixed64(buf, file_number);
118
+ cache_->Erase(Slice(buf, sizeof(buf)));
119
+ }
120
+
121
+ } // namespace leveldb
@@ -0,0 +1,61 @@
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
+ // Thread-safe (provides internal synchronization)
6
+
7
+ #ifndef STORAGE_LEVELDB_DB_TABLE_CACHE_H_
8
+ #define STORAGE_LEVELDB_DB_TABLE_CACHE_H_
9
+
10
+ #include <string>
11
+ #include <stdint.h>
12
+ #include "db/dbformat.h"
13
+ #include "leveldb/cache.h"
14
+ #include "leveldb/table.h"
15
+ #include "port/port.h"
16
+
17
+ namespace leveldb {
18
+
19
+ class Env;
20
+
21
+ class TableCache {
22
+ public:
23
+ TableCache(const std::string& dbname, const Options* options, int entries);
24
+ ~TableCache();
25
+
26
+ // Return an iterator for the specified file number (the corresponding
27
+ // file length must be exactly "file_size" bytes). If "tableptr" is
28
+ // non-NULL, also sets "*tableptr" to point to the Table object
29
+ // underlying the returned iterator, or NULL if no Table object underlies
30
+ // the returned iterator. The returned "*tableptr" object is owned by
31
+ // the cache and should not be deleted, and is valid for as long as the
32
+ // returned iterator is live.
33
+ Iterator* NewIterator(const ReadOptions& options,
34
+ uint64_t file_number,
35
+ uint64_t file_size,
36
+ Table** tableptr = NULL);
37
+
38
+ // If a seek to internal key "k" in specified file finds an entry,
39
+ // call (*handle_result)(arg, found_key, found_value).
40
+ Status Get(const ReadOptions& options,
41
+ uint64_t file_number,
42
+ uint64_t file_size,
43
+ const Slice& k,
44
+ void* arg,
45
+ void (*handle_result)(void*, const Slice&, const Slice&));
46
+
47
+ // Evict any entry for the specified file number
48
+ void Evict(uint64_t file_number);
49
+
50
+ private:
51
+ Env* const env_;
52
+ const std::string dbname_;
53
+ const Options* options_;
54
+ Cache* cache_;
55
+
56
+ Status FindTable(uint64_t file_number, uint64_t file_size, Cache::Handle**);
57
+ };
58
+
59
+ } // namespace leveldb
60
+
61
+ #endif // STORAGE_LEVELDB_DB_TABLE_CACHE_H_