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