filiptepper-leveldb-ruby 0.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (123) hide show
  1. data/LICENSE +24 -0
  2. data/README +72 -0
  3. data/ext/leveldb/extconf.rb +14 -0
  4. data/ext/leveldb/leveldb.cc +530 -0
  5. data/ext/leveldb/platform.rb +83 -0
  6. data/leveldb/Makefile +191 -0
  7. data/leveldb/build_detect_platform +160 -0
  8. data/leveldb/db/builder.cc +88 -0
  9. data/leveldb/db/builder.h +34 -0
  10. data/leveldb/db/c.cc +581 -0
  11. data/leveldb/db/corruption_test.cc +359 -0
  12. data/leveldb/db/db_bench.cc +970 -0
  13. data/leveldb/db/db_impl.cc +1448 -0
  14. data/leveldb/db/db_impl.h +194 -0
  15. data/leveldb/db/db_iter.cc +299 -0
  16. data/leveldb/db/db_iter.h +26 -0
  17. data/leveldb/db/db_test.cc +1901 -0
  18. data/leveldb/db/dbformat.cc +140 -0
  19. data/leveldb/db/dbformat.h +227 -0
  20. data/leveldb/db/dbformat_test.cc +112 -0
  21. data/leveldb/db/filename.cc +139 -0
  22. data/leveldb/db/filename.h +80 -0
  23. data/leveldb/db/filename_test.cc +122 -0
  24. data/leveldb/db/log_format.h +35 -0
  25. data/leveldb/db/log_reader.cc +259 -0
  26. data/leveldb/db/log_reader.h +108 -0
  27. data/leveldb/db/log_test.cc +500 -0
  28. data/leveldb/db/log_writer.cc +103 -0
  29. data/leveldb/db/log_writer.h +48 -0
  30. data/leveldb/db/memtable.cc +145 -0
  31. data/leveldb/db/memtable.h +91 -0
  32. data/leveldb/db/repair.cc +389 -0
  33. data/leveldb/db/skiplist.h +379 -0
  34. data/leveldb/db/skiplist_test.cc +378 -0
  35. data/leveldb/db/snapshot.h +66 -0
  36. data/leveldb/db/table_cache.cc +121 -0
  37. data/leveldb/db/table_cache.h +61 -0
  38. data/leveldb/db/version_edit.cc +266 -0
  39. data/leveldb/db/version_edit.h +107 -0
  40. data/leveldb/db/version_edit_test.cc +46 -0
  41. data/leveldb/db/version_set.cc +1402 -0
  42. data/leveldb/db/version_set.h +370 -0
  43. data/leveldb/db/version_set_test.cc +179 -0
  44. data/leveldb/db/write_batch.cc +147 -0
  45. data/leveldb/db/write_batch_internal.h +49 -0
  46. data/leveldb/db/write_batch_test.cc +120 -0
  47. data/leveldb/helpers/memenv/memenv.cc +374 -0
  48. data/leveldb/helpers/memenv/memenv.h +20 -0
  49. data/leveldb/helpers/memenv/memenv_test.cc +232 -0
  50. data/leveldb/include/leveldb/c.h +275 -0
  51. data/leveldb/include/leveldb/cache.h +99 -0
  52. data/leveldb/include/leveldb/comparator.h +63 -0
  53. data/leveldb/include/leveldb/db.h +161 -0
  54. data/leveldb/include/leveldb/env.h +323 -0
  55. data/leveldb/include/leveldb/filter_policy.h +70 -0
  56. data/leveldb/include/leveldb/iterator.h +100 -0
  57. data/leveldb/include/leveldb/options.h +195 -0
  58. data/leveldb/include/leveldb/slice.h +109 -0
  59. data/leveldb/include/leveldb/status.h +106 -0
  60. data/leveldb/include/leveldb/table.h +85 -0
  61. data/leveldb/include/leveldb/table_builder.h +92 -0
  62. data/leveldb/include/leveldb/write_batch.h +64 -0
  63. data/leveldb/port/atomic_pointer.h +144 -0
  64. data/leveldb/port/port.h +21 -0
  65. data/leveldb/port/port_android.cc +64 -0
  66. data/leveldb/port/port_android.h +159 -0
  67. data/leveldb/port/port_example.h +125 -0
  68. data/leveldb/port/port_posix.cc +50 -0
  69. data/leveldb/port/port_posix.h +129 -0
  70. data/leveldb/port/win/stdint.h +24 -0
  71. data/leveldb/table/block.cc +267 -0
  72. data/leveldb/table/block.h +44 -0
  73. data/leveldb/table/block_builder.cc +109 -0
  74. data/leveldb/table/block_builder.h +57 -0
  75. data/leveldb/table/filter_block.cc +111 -0
  76. data/leveldb/table/filter_block.h +68 -0
  77. data/leveldb/table/filter_block_test.cc +128 -0
  78. data/leveldb/table/format.cc +145 -0
  79. data/leveldb/table/format.h +108 -0
  80. data/leveldb/table/iterator.cc +67 -0
  81. data/leveldb/table/iterator_wrapper.h +63 -0
  82. data/leveldb/table/merger.cc +197 -0
  83. data/leveldb/table/merger.h +26 -0
  84. data/leveldb/table/table.cc +276 -0
  85. data/leveldb/table/table_builder.cc +270 -0
  86. data/leveldb/table/table_test.cc +838 -0
  87. data/leveldb/table/two_level_iterator.cc +182 -0
  88. data/leveldb/table/two_level_iterator.h +34 -0
  89. data/leveldb/util/arena.cc +68 -0
  90. data/leveldb/util/arena.h +68 -0
  91. data/leveldb/util/arena_test.cc +68 -0
  92. data/leveldb/util/bloom.cc +95 -0
  93. data/leveldb/util/bloom_test.cc +159 -0
  94. data/leveldb/util/cache.cc +328 -0
  95. data/leveldb/util/cache_test.cc +186 -0
  96. data/leveldb/util/coding.cc +194 -0
  97. data/leveldb/util/coding.h +104 -0
  98. data/leveldb/util/coding_test.cc +173 -0
  99. data/leveldb/util/comparator.cc +76 -0
  100. data/leveldb/util/crc32c.cc +332 -0
  101. data/leveldb/util/crc32c.h +45 -0
  102. data/leveldb/util/crc32c_test.cc +72 -0
  103. data/leveldb/util/env.cc +96 -0
  104. data/leveldb/util/env_posix.cc +609 -0
  105. data/leveldb/util/env_test.cc +104 -0
  106. data/leveldb/util/filter_policy.cc +11 -0
  107. data/leveldb/util/hash.cc +45 -0
  108. data/leveldb/util/hash.h +19 -0
  109. data/leveldb/util/histogram.cc +139 -0
  110. data/leveldb/util/histogram.h +42 -0
  111. data/leveldb/util/logging.cc +81 -0
  112. data/leveldb/util/logging.h +47 -0
  113. data/leveldb/util/mutexlock.h +39 -0
  114. data/leveldb/util/options.cc +29 -0
  115. data/leveldb/util/posix_logger.h +98 -0
  116. data/leveldb/util/random.h +59 -0
  117. data/leveldb/util/status.cc +75 -0
  118. data/leveldb/util/testharness.cc +77 -0
  119. data/leveldb/util/testharness.h +138 -0
  120. data/leveldb/util/testutil.cc +51 -0
  121. data/leveldb/util/testutil.h +53 -0
  122. data/lib/leveldb.rb +76 -0
  123. metadata +175 -0
@@ -0,0 +1,34 @@
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_BUILDER_H_
6
+ #define STORAGE_LEVELDB_DB_BUILDER_H_
7
+
8
+ #include "leveldb/status.h"
9
+
10
+ namespace leveldb {
11
+
12
+ struct Options;
13
+ struct FileMetaData;
14
+
15
+ class Env;
16
+ class Iterator;
17
+ class TableCache;
18
+ class VersionEdit;
19
+
20
+ // Build a Table file from the contents of *iter. The generated file
21
+ // will be named according to meta->number. On success, the rest of
22
+ // *meta will be filled with metadata about the generated table.
23
+ // If no data is present in *iter, meta->file_size will be set to
24
+ // zero, and no Table file will be produced.
25
+ extern Status BuildTable(const std::string& dbname,
26
+ Env* env,
27
+ const Options& options,
28
+ TableCache* table_cache,
29
+ Iterator* iter,
30
+ FileMetaData* meta);
31
+
32
+ } // namespace leveldb
33
+
34
+ #endif // STORAGE_LEVELDB_DB_BUILDER_H_
@@ -0,0 +1,581 @@
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/c.h"
6
+
7
+ #include <stdlib.h>
8
+ #include <unistd.h>
9
+ #include "leveldb/cache.h"
10
+ #include "leveldb/comparator.h"
11
+ #include "leveldb/db.h"
12
+ #include "leveldb/env.h"
13
+ #include "leveldb/filter_policy.h"
14
+ #include "leveldb/iterator.h"
15
+ #include "leveldb/options.h"
16
+ #include "leveldb/status.h"
17
+ #include "leveldb/write_batch.h"
18
+
19
+ using leveldb::Cache;
20
+ using leveldb::Comparator;
21
+ using leveldb::CompressionType;
22
+ using leveldb::DB;
23
+ using leveldb::Env;
24
+ using leveldb::FileLock;
25
+ using leveldb::FilterPolicy;
26
+ using leveldb::Iterator;
27
+ using leveldb::Logger;
28
+ using leveldb::NewBloomFilterPolicy;
29
+ using leveldb::NewLRUCache;
30
+ using leveldb::Options;
31
+ using leveldb::RandomAccessFile;
32
+ using leveldb::Range;
33
+ using leveldb::ReadOptions;
34
+ using leveldb::SequentialFile;
35
+ using leveldb::Slice;
36
+ using leveldb::Snapshot;
37
+ using leveldb::Status;
38
+ using leveldb::WritableFile;
39
+ using leveldb::WriteBatch;
40
+ using leveldb::WriteOptions;
41
+
42
+ extern "C" {
43
+
44
+ struct leveldb_t { DB* rep; };
45
+ struct leveldb_iterator_t { Iterator* rep; };
46
+ struct leveldb_writebatch_t { WriteBatch rep; };
47
+ struct leveldb_snapshot_t { const Snapshot* rep; };
48
+ struct leveldb_readoptions_t { ReadOptions rep; };
49
+ struct leveldb_writeoptions_t { WriteOptions rep; };
50
+ struct leveldb_options_t { Options rep; };
51
+ struct leveldb_cache_t { Cache* rep; };
52
+ struct leveldb_seqfile_t { SequentialFile* rep; };
53
+ struct leveldb_randomfile_t { RandomAccessFile* rep; };
54
+ struct leveldb_writablefile_t { WritableFile* rep; };
55
+ struct leveldb_logger_t { Logger* rep; };
56
+ struct leveldb_filelock_t { FileLock* rep; };
57
+
58
+ struct leveldb_comparator_t : public Comparator {
59
+ void* state_;
60
+ void (*destructor_)(void*);
61
+ int (*compare_)(
62
+ void*,
63
+ const char* a, size_t alen,
64
+ const char* b, size_t blen);
65
+ const char* (*name_)(void*);
66
+
67
+ virtual ~leveldb_comparator_t() {
68
+ (*destructor_)(state_);
69
+ }
70
+
71
+ virtual int Compare(const Slice& a, const Slice& b) const {
72
+ return (*compare_)(state_, a.data(), a.size(), b.data(), b.size());
73
+ }
74
+
75
+ virtual const char* Name() const {
76
+ return (*name_)(state_);
77
+ }
78
+
79
+ // No-ops since the C binding does not support key shortening methods.
80
+ virtual void FindShortestSeparator(std::string*, const Slice&) const { }
81
+ virtual void FindShortSuccessor(std::string* key) const { }
82
+ };
83
+
84
+ struct leveldb_filterpolicy_t : public FilterPolicy {
85
+ void* state_;
86
+ void (*destructor_)(void*);
87
+ const char* (*name_)(void*);
88
+ char* (*create_)(
89
+ void*,
90
+ const char* const* key_array, const size_t* key_length_array,
91
+ int num_keys,
92
+ size_t* filter_length);
93
+ unsigned char (*key_match_)(
94
+ void*,
95
+ const char* key, size_t length,
96
+ const char* filter, size_t filter_length);
97
+
98
+ virtual ~leveldb_filterpolicy_t() {
99
+ (*destructor_)(state_);
100
+ }
101
+
102
+ virtual const char* Name() const {
103
+ return (*name_)(state_);
104
+ }
105
+
106
+ virtual void CreateFilter(const Slice* keys, int n, std::string* dst) const {
107
+ std::vector<const char*> key_pointers(n);
108
+ std::vector<size_t> key_sizes(n);
109
+ for (int i = 0; i < n; i++) {
110
+ key_pointers[i] = keys[i].data();
111
+ key_sizes[i] = keys[i].size();
112
+ }
113
+ size_t len;
114
+ char* filter = (*create_)(state_, &key_pointers[0], &key_sizes[0], n, &len);
115
+ dst->append(filter, len);
116
+ free(filter);
117
+ }
118
+
119
+ virtual bool KeyMayMatch(const Slice& key, const Slice& filter) const {
120
+ return (*key_match_)(state_, key.data(), key.size(),
121
+ filter.data(), filter.size());
122
+ }
123
+ };
124
+
125
+ struct leveldb_env_t {
126
+ Env* rep;
127
+ bool is_default;
128
+ };
129
+
130
+ static bool SaveError(char** errptr, const Status& s) {
131
+ assert(errptr != NULL);
132
+ if (s.ok()) {
133
+ return false;
134
+ } else if (*errptr == NULL) {
135
+ *errptr = strdup(s.ToString().c_str());
136
+ } else {
137
+ // TODO(sanjay): Merge with existing error?
138
+ free(*errptr);
139
+ *errptr = strdup(s.ToString().c_str());
140
+ }
141
+ return true;
142
+ }
143
+
144
+ static char* CopyString(const std::string& str) {
145
+ char* result = reinterpret_cast<char*>(malloc(sizeof(char) * str.size()));
146
+ memcpy(result, str.data(), sizeof(char) * str.size());
147
+ return result;
148
+ }
149
+
150
+ leveldb_t* leveldb_open(
151
+ const leveldb_options_t* options,
152
+ const char* name,
153
+ char** errptr) {
154
+ DB* db;
155
+ if (SaveError(errptr, DB::Open(options->rep, std::string(name), &db))) {
156
+ return NULL;
157
+ }
158
+ leveldb_t* result = new leveldb_t;
159
+ result->rep = db;
160
+ return result;
161
+ }
162
+
163
+ void leveldb_close(leveldb_t* db) {
164
+ delete db->rep;
165
+ delete db;
166
+ }
167
+
168
+ void leveldb_put(
169
+ leveldb_t* db,
170
+ const leveldb_writeoptions_t* options,
171
+ const char* key, size_t keylen,
172
+ const char* val, size_t vallen,
173
+ char** errptr) {
174
+ SaveError(errptr,
175
+ db->rep->Put(options->rep, Slice(key, keylen), Slice(val, vallen)));
176
+ }
177
+
178
+ void leveldb_delete(
179
+ leveldb_t* db,
180
+ const leveldb_writeoptions_t* options,
181
+ const char* key, size_t keylen,
182
+ char** errptr) {
183
+ SaveError(errptr, db->rep->Delete(options->rep, Slice(key, keylen)));
184
+ }
185
+
186
+
187
+ void leveldb_write(
188
+ leveldb_t* db,
189
+ const leveldb_writeoptions_t* options,
190
+ leveldb_writebatch_t* batch,
191
+ char** errptr) {
192
+ SaveError(errptr, db->rep->Write(options->rep, &batch->rep));
193
+ }
194
+
195
+ char* leveldb_get(
196
+ leveldb_t* db,
197
+ const leveldb_readoptions_t* options,
198
+ const char* key, size_t keylen,
199
+ size_t* vallen,
200
+ char** errptr) {
201
+ char* result = NULL;
202
+ std::string tmp;
203
+ Status s = db->rep->Get(options->rep, Slice(key, keylen), &tmp);
204
+ if (s.ok()) {
205
+ *vallen = tmp.size();
206
+ result = CopyString(tmp);
207
+ } else {
208
+ *vallen = 0;
209
+ if (!s.IsNotFound()) {
210
+ SaveError(errptr, s);
211
+ }
212
+ }
213
+ return result;
214
+ }
215
+
216
+ leveldb_iterator_t* leveldb_create_iterator(
217
+ leveldb_t* db,
218
+ const leveldb_readoptions_t* options) {
219
+ leveldb_iterator_t* result = new leveldb_iterator_t;
220
+ result->rep = db->rep->NewIterator(options->rep);
221
+ return result;
222
+ }
223
+
224
+ const leveldb_snapshot_t* leveldb_create_snapshot(
225
+ leveldb_t* db) {
226
+ leveldb_snapshot_t* result = new leveldb_snapshot_t;
227
+ result->rep = db->rep->GetSnapshot();
228
+ return result;
229
+ }
230
+
231
+ void leveldb_release_snapshot(
232
+ leveldb_t* db,
233
+ const leveldb_snapshot_t* snapshot) {
234
+ db->rep->ReleaseSnapshot(snapshot->rep);
235
+ delete snapshot;
236
+ }
237
+
238
+ char* leveldb_property_value(
239
+ leveldb_t* db,
240
+ const char* propname) {
241
+ std::string tmp;
242
+ if (db->rep->GetProperty(Slice(propname), &tmp)) {
243
+ // We use strdup() since we expect human readable output.
244
+ return strdup(tmp.c_str());
245
+ } else {
246
+ return NULL;
247
+ }
248
+ }
249
+
250
+ void leveldb_approximate_sizes(
251
+ leveldb_t* db,
252
+ int num_ranges,
253
+ const char* const* range_start_key, const size_t* range_start_key_len,
254
+ const char* const* range_limit_key, const size_t* range_limit_key_len,
255
+ uint64_t* sizes) {
256
+ Range* ranges = new Range[num_ranges];
257
+ for (int i = 0; i < num_ranges; i++) {
258
+ ranges[i].start = Slice(range_start_key[i], range_start_key_len[i]);
259
+ ranges[i].limit = Slice(range_limit_key[i], range_limit_key_len[i]);
260
+ }
261
+ db->rep->GetApproximateSizes(ranges, num_ranges, sizes);
262
+ delete[] ranges;
263
+ }
264
+
265
+ void leveldb_compact_range(
266
+ leveldb_t* db,
267
+ const char* start_key, size_t start_key_len,
268
+ const char* limit_key, size_t limit_key_len) {
269
+ Slice a, b;
270
+ db->rep->CompactRange(
271
+ // Pass NULL Slice if corresponding "const char*" is NULL
272
+ (start_key ? (a = Slice(start_key, start_key_len), &a) : NULL),
273
+ (limit_key ? (b = Slice(limit_key, limit_key_len), &b) : NULL));
274
+ }
275
+
276
+ void leveldb_destroy_db(
277
+ const leveldb_options_t* options,
278
+ const char* name,
279
+ char** errptr) {
280
+ SaveError(errptr, DestroyDB(name, options->rep));
281
+ }
282
+
283
+ void leveldb_repair_db(
284
+ const leveldb_options_t* options,
285
+ const char* name,
286
+ char** errptr) {
287
+ SaveError(errptr, RepairDB(name, options->rep));
288
+ }
289
+
290
+ void leveldb_iter_destroy(leveldb_iterator_t* iter) {
291
+ delete iter->rep;
292
+ delete iter;
293
+ }
294
+
295
+ unsigned char leveldb_iter_valid(const leveldb_iterator_t* iter) {
296
+ return iter->rep->Valid();
297
+ }
298
+
299
+ void leveldb_iter_seek_to_first(leveldb_iterator_t* iter) {
300
+ iter->rep->SeekToFirst();
301
+ }
302
+
303
+ void leveldb_iter_seek_to_last(leveldb_iterator_t* iter) {
304
+ iter->rep->SeekToLast();
305
+ }
306
+
307
+ void leveldb_iter_seek(leveldb_iterator_t* iter, const char* k, size_t klen) {
308
+ iter->rep->Seek(Slice(k, klen));
309
+ }
310
+
311
+ void leveldb_iter_next(leveldb_iterator_t* iter) {
312
+ iter->rep->Next();
313
+ }
314
+
315
+ void leveldb_iter_prev(leveldb_iterator_t* iter) {
316
+ iter->rep->Prev();
317
+ }
318
+
319
+ const char* leveldb_iter_key(const leveldb_iterator_t* iter, size_t* klen) {
320
+ Slice s = iter->rep->key();
321
+ *klen = s.size();
322
+ return s.data();
323
+ }
324
+
325
+ const char* leveldb_iter_value(const leveldb_iterator_t* iter, size_t* vlen) {
326
+ Slice s = iter->rep->value();
327
+ *vlen = s.size();
328
+ return s.data();
329
+ }
330
+
331
+ void leveldb_iter_get_error(const leveldb_iterator_t* iter, char** errptr) {
332
+ SaveError(errptr, iter->rep->status());
333
+ }
334
+
335
+ leveldb_writebatch_t* leveldb_writebatch_create() {
336
+ return new leveldb_writebatch_t;
337
+ }
338
+
339
+ void leveldb_writebatch_destroy(leveldb_writebatch_t* b) {
340
+ delete b;
341
+ }
342
+
343
+ void leveldb_writebatch_clear(leveldb_writebatch_t* b) {
344
+ b->rep.Clear();
345
+ }
346
+
347
+ void leveldb_writebatch_put(
348
+ leveldb_writebatch_t* b,
349
+ const char* key, size_t klen,
350
+ const char* val, size_t vlen) {
351
+ b->rep.Put(Slice(key, klen), Slice(val, vlen));
352
+ }
353
+
354
+ void leveldb_writebatch_delete(
355
+ leveldb_writebatch_t* b,
356
+ const char* key, size_t klen) {
357
+ b->rep.Delete(Slice(key, klen));
358
+ }
359
+
360
+ void leveldb_writebatch_iterate(
361
+ leveldb_writebatch_t* b,
362
+ void* state,
363
+ void (*put)(void*, const char* k, size_t klen, const char* v, size_t vlen),
364
+ void (*deleted)(void*, const char* k, size_t klen)) {
365
+ class H : public WriteBatch::Handler {
366
+ public:
367
+ void* state_;
368
+ void (*put_)(void*, const char* k, size_t klen, const char* v, size_t vlen);
369
+ void (*deleted_)(void*, const char* k, size_t klen);
370
+ virtual void Put(const Slice& key, const Slice& value) {
371
+ (*put_)(state_, key.data(), key.size(), value.data(), value.size());
372
+ }
373
+ virtual void Delete(const Slice& key) {
374
+ (*deleted_)(state_, key.data(), key.size());
375
+ }
376
+ };
377
+ H handler;
378
+ handler.state_ = state;
379
+ handler.put_ = put;
380
+ handler.deleted_ = deleted;
381
+ b->rep.Iterate(&handler);
382
+ }
383
+
384
+ leveldb_options_t* leveldb_options_create() {
385
+ return new leveldb_options_t;
386
+ }
387
+
388
+ void leveldb_options_destroy(leveldb_options_t* options) {
389
+ delete options;
390
+ }
391
+
392
+ void leveldb_options_set_comparator(
393
+ leveldb_options_t* opt,
394
+ leveldb_comparator_t* cmp) {
395
+ opt->rep.comparator = cmp;
396
+ }
397
+
398
+ void leveldb_options_set_filter_policy(
399
+ leveldb_options_t* opt,
400
+ leveldb_filterpolicy_t* policy) {
401
+ opt->rep.filter_policy = policy;
402
+ }
403
+
404
+ void leveldb_options_set_create_if_missing(
405
+ leveldb_options_t* opt, unsigned char v) {
406
+ opt->rep.create_if_missing = v;
407
+ }
408
+
409
+ void leveldb_options_set_error_if_exists(
410
+ leveldb_options_t* opt, unsigned char v) {
411
+ opt->rep.error_if_exists = v;
412
+ }
413
+
414
+ void leveldb_options_set_paranoid_checks(
415
+ leveldb_options_t* opt, unsigned char v) {
416
+ opt->rep.paranoid_checks = v;
417
+ }
418
+
419
+ void leveldb_options_set_env(leveldb_options_t* opt, leveldb_env_t* env) {
420
+ opt->rep.env = (env ? env->rep : NULL);
421
+ }
422
+
423
+ void leveldb_options_set_info_log(leveldb_options_t* opt, leveldb_logger_t* l) {
424
+ opt->rep.info_log = (l ? l->rep : NULL);
425
+ }
426
+
427
+ void leveldb_options_set_write_buffer_size(leveldb_options_t* opt, size_t s) {
428
+ opt->rep.write_buffer_size = s;
429
+ }
430
+
431
+ void leveldb_options_set_max_open_files(leveldb_options_t* opt, int n) {
432
+ opt->rep.max_open_files = n;
433
+ }
434
+
435
+ void leveldb_options_set_cache(leveldb_options_t* opt, leveldb_cache_t* c) {
436
+ opt->rep.block_cache = c->rep;
437
+ }
438
+
439
+ void leveldb_options_set_block_size(leveldb_options_t* opt, size_t s) {
440
+ opt->rep.block_size = s;
441
+ }
442
+
443
+ void leveldb_options_set_block_restart_interval(leveldb_options_t* opt, int n) {
444
+ opt->rep.block_restart_interval = n;
445
+ }
446
+
447
+ void leveldb_options_set_compression(leveldb_options_t* opt, int t) {
448
+ opt->rep.compression = static_cast<CompressionType>(t);
449
+ }
450
+
451
+ leveldb_comparator_t* leveldb_comparator_create(
452
+ void* state,
453
+ void (*destructor)(void*),
454
+ int (*compare)(
455
+ void*,
456
+ const char* a, size_t alen,
457
+ const char* b, size_t blen),
458
+ const char* (*name)(void*)) {
459
+ leveldb_comparator_t* result = new leveldb_comparator_t;
460
+ result->state_ = state;
461
+ result->destructor_ = destructor;
462
+ result->compare_ = compare;
463
+ result->name_ = name;
464
+ return result;
465
+ }
466
+
467
+ void leveldb_comparator_destroy(leveldb_comparator_t* cmp) {
468
+ delete cmp;
469
+ }
470
+
471
+ leveldb_filterpolicy_t* leveldb_filterpolicy_create(
472
+ void* state,
473
+ void (*destructor)(void*),
474
+ char* (*create_filter)(
475
+ void*,
476
+ const char* const* key_array, const size_t* key_length_array,
477
+ int num_keys,
478
+ size_t* filter_length),
479
+ unsigned char (*key_may_match)(
480
+ void*,
481
+ const char* key, size_t length,
482
+ const char* filter, size_t filter_length),
483
+ const char* (*name)(void*)) {
484
+ leveldb_filterpolicy_t* result = new leveldb_filterpolicy_t;
485
+ result->state_ = state;
486
+ result->destructor_ = destructor;
487
+ result->create_ = create_filter;
488
+ result->key_match_ = key_may_match;
489
+ result->name_ = name;
490
+ return result;
491
+ }
492
+
493
+ void leveldb_filterpolicy_destroy(leveldb_filterpolicy_t* filter) {
494
+ delete filter;
495
+ }
496
+
497
+ leveldb_filterpolicy_t* leveldb_filterpolicy_create_bloom(int bits_per_key) {
498
+ // Make a leveldb_filterpolicy_t, but override all of its methods so
499
+ // they delegate to a NewBloomFilterPolicy() instead of user
500
+ // supplied C functions.
501
+ struct Wrapper : public leveldb_filterpolicy_t {
502
+ const FilterPolicy* rep_;
503
+ ~Wrapper() { delete rep_; }
504
+ const char* Name() const { return rep_->Name(); }
505
+ void CreateFilter(const Slice* keys, int n, std::string* dst) const {
506
+ return rep_->CreateFilter(keys, n, dst);
507
+ }
508
+ bool KeyMayMatch(const Slice& key, const Slice& filter) const {
509
+ return rep_->KeyMayMatch(key, filter);
510
+ }
511
+ static void DoNothing(void*) { }
512
+ };
513
+ Wrapper* wrapper = new Wrapper;
514
+ wrapper->rep_ = NewBloomFilterPolicy(bits_per_key);
515
+ wrapper->state_ = NULL;
516
+ wrapper->destructor_ = &Wrapper::DoNothing;
517
+ return wrapper;
518
+ }
519
+
520
+ leveldb_readoptions_t* leveldb_readoptions_create() {
521
+ return new leveldb_readoptions_t;
522
+ }
523
+
524
+ void leveldb_readoptions_destroy(leveldb_readoptions_t* opt) {
525
+ delete opt;
526
+ }
527
+
528
+ void leveldb_readoptions_set_verify_checksums(
529
+ leveldb_readoptions_t* opt,
530
+ unsigned char v) {
531
+ opt->rep.verify_checksums = v;
532
+ }
533
+
534
+ void leveldb_readoptions_set_fill_cache(
535
+ leveldb_readoptions_t* opt, unsigned char v) {
536
+ opt->rep.fill_cache = v;
537
+ }
538
+
539
+ void leveldb_readoptions_set_snapshot(
540
+ leveldb_readoptions_t* opt,
541
+ const leveldb_snapshot_t* snap) {
542
+ opt->rep.snapshot = (snap ? snap->rep : NULL);
543
+ }
544
+
545
+ leveldb_writeoptions_t* leveldb_writeoptions_create() {
546
+ return new leveldb_writeoptions_t;
547
+ }
548
+
549
+ void leveldb_writeoptions_destroy(leveldb_writeoptions_t* opt) {
550
+ delete opt;
551
+ }
552
+
553
+ void leveldb_writeoptions_set_sync(
554
+ leveldb_writeoptions_t* opt, unsigned char v) {
555
+ opt->rep.sync = v;
556
+ }
557
+
558
+ leveldb_cache_t* leveldb_cache_create_lru(size_t capacity) {
559
+ leveldb_cache_t* c = new leveldb_cache_t;
560
+ c->rep = NewLRUCache(capacity);
561
+ return c;
562
+ }
563
+
564
+ void leveldb_cache_destroy(leveldb_cache_t* cache) {
565
+ delete cache->rep;
566
+ delete cache;
567
+ }
568
+
569
+ leveldb_env_t* leveldb_create_default_env() {
570
+ leveldb_env_t* result = new leveldb_env_t;
571
+ result->rep = Env::Default();
572
+ result->is_default = true;
573
+ return result;
574
+ }
575
+
576
+ void leveldb_env_destroy(leveldb_env_t* env) {
577
+ if (!env->is_default) delete env->rep;
578
+ delete env;
579
+ }
580
+
581
+ } // end extern "C"