1d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com// Use of this source code is governed by a BSD-style license that can be
3d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com// found in the LICENSE file. See the AUTHORS file for names of contributors.
4d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
5d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com#include "leveldb/c.h"
6d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
7d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com#include <stdlib.h>
8d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com#include <unistd.h>
9d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com#include "leveldb/cache.h"
10d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com#include "leveldb/comparator.h"
11d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com#include "leveldb/db.h"
12d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com#include "leveldb/env.h"
1399a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com#include "leveldb/filter_policy.h"
14d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com#include "leveldb/iterator.h"
15d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com#include "leveldb/options.h"
16d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com#include "leveldb/status.h"
17d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com#include "leveldb/write_batch.h"
18d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
197e50a01f8a2820ed7a50c673b5affe0131560ff5gabor@google.comusing leveldb::Cache;
207e50a01f8a2820ed7a50c673b5affe0131560ff5gabor@google.comusing leveldb::Comparator;
217e50a01f8a2820ed7a50c673b5affe0131560ff5gabor@google.comusing leveldb::CompressionType;
227e50a01f8a2820ed7a50c673b5affe0131560ff5gabor@google.comusing leveldb::DB;
237e50a01f8a2820ed7a50c673b5affe0131560ff5gabor@google.comusing leveldb::Env;
247e50a01f8a2820ed7a50c673b5affe0131560ff5gabor@google.comusing leveldb::FileLock;
2599a7585544fc162a5f8dd39a6add00776a981efesanjay@google.comusing leveldb::FilterPolicy;
267e50a01f8a2820ed7a50c673b5affe0131560ff5gabor@google.comusing leveldb::Iterator;
27b0edd034dee25db7a3e42243b95c66eaf95e8a81sanjay@google.comusing leveldb::kMajorVersion;
28b0edd034dee25db7a3e42243b95c66eaf95e8a81sanjay@google.comusing leveldb::kMinorVersion;
297e50a01f8a2820ed7a50c673b5affe0131560ff5gabor@google.comusing leveldb::Logger;
3099a7585544fc162a5f8dd39a6add00776a981efesanjay@google.comusing leveldb::NewBloomFilterPolicy;
317e50a01f8a2820ed7a50c673b5affe0131560ff5gabor@google.comusing leveldb::NewLRUCache;
327e50a01f8a2820ed7a50c673b5affe0131560ff5gabor@google.comusing leveldb::Options;
337e50a01f8a2820ed7a50c673b5affe0131560ff5gabor@google.comusing leveldb::RandomAccessFile;
347e50a01f8a2820ed7a50c673b5affe0131560ff5gabor@google.comusing leveldb::Range;
357e50a01f8a2820ed7a50c673b5affe0131560ff5gabor@google.comusing leveldb::ReadOptions;
367e50a01f8a2820ed7a50c673b5affe0131560ff5gabor@google.comusing leveldb::SequentialFile;
377e50a01f8a2820ed7a50c673b5affe0131560ff5gabor@google.comusing leveldb::Slice;
387e50a01f8a2820ed7a50c673b5affe0131560ff5gabor@google.comusing leveldb::Snapshot;
397e50a01f8a2820ed7a50c673b5affe0131560ff5gabor@google.comusing leveldb::Status;
407e50a01f8a2820ed7a50c673b5affe0131560ff5gabor@google.comusing leveldb::WritableFile;
417e50a01f8a2820ed7a50c673b5affe0131560ff5gabor@google.comusing leveldb::WriteBatch;
427e50a01f8a2820ed7a50c673b5affe0131560ff5gabor@google.comusing leveldb::WriteOptions;
43d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
44d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comextern "C" {
45d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
46d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comstruct leveldb_t              { DB*               rep; };
47d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comstruct leveldb_iterator_t     { Iterator*         rep; };
48d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comstruct leveldb_writebatch_t   { WriteBatch        rep; };
49d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comstruct leveldb_snapshot_t     { const Snapshot*   rep; };
50d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comstruct leveldb_readoptions_t  { ReadOptions       rep; };
51d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comstruct leveldb_writeoptions_t { WriteOptions      rep; };
52d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comstruct leveldb_options_t      { Options           rep; };
53d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comstruct leveldb_cache_t        { Cache*            rep; };
54d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comstruct leveldb_seqfile_t      { SequentialFile*   rep; };
55d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comstruct leveldb_randomfile_t   { RandomAccessFile* rep; };
56d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comstruct leveldb_writablefile_t { WritableFile*     rep; };
57d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comstruct leveldb_logger_t       { Logger*           rep; };
58d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comstruct leveldb_filelock_t     { FileLock*         rep; };
59d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
60d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comstruct leveldb_comparator_t : public Comparator {
61d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  void* state_;
62d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  void (*destructor_)(void*);
63d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  int (*compare_)(
64d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com      void*,
65d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com      const char* a, size_t alen,
66d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com      const char* b, size_t blen);
67d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  const char* (*name_)(void*);
68d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
69d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  virtual ~leveldb_comparator_t() {
70d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    (*destructor_)(state_);
71d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  }
72d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
73d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  virtual int Compare(const Slice& a, const Slice& b) const {
74d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    return (*compare_)(state_, a.data(), a.size(), b.data(), b.size());
75d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  }
76d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
77d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  virtual const char* Name() const {
78d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    return (*name_)(state_);
79d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  }
80d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
81d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  // No-ops since the C binding does not support key shortening methods.
82d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  virtual void FindShortestSeparator(std::string*, const Slice&) const { }
83d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  virtual void FindShortSuccessor(std::string* key) const { }
84d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com};
85d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
8699a7585544fc162a5f8dd39a6add00776a981efesanjay@google.comstruct leveldb_filterpolicy_t : public FilterPolicy {
8799a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  void* state_;
8899a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  void (*destructor_)(void*);
8999a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  const char* (*name_)(void*);
9099a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  char* (*create_)(
9199a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com      void*,
9299a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com      const char* const* key_array, const size_t* key_length_array,
9399a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com      int num_keys,
9499a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com      size_t* filter_length);
9599a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  unsigned char (*key_match_)(
9699a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com      void*,
9799a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com      const char* key, size_t length,
9899a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com      const char* filter, size_t filter_length);
9999a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com
10099a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  virtual ~leveldb_filterpolicy_t() {
10199a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com    (*destructor_)(state_);
10299a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  }
10399a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com
10499a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  virtual const char* Name() const {
10599a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com    return (*name_)(state_);
10699a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  }
10799a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com
10899a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  virtual void CreateFilter(const Slice* keys, int n, std::string* dst) const {
10999a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com    std::vector<const char*> key_pointers(n);
11099a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com    std::vector<size_t> key_sizes(n);
11199a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com    for (int i = 0; i < n; i++) {
11299a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com      key_pointers[i] = keys[i].data();
11399a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com      key_sizes[i] = keys[i].size();
11499a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com    }
11599a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com    size_t len;
11699a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com    char* filter = (*create_)(state_, &key_pointers[0], &key_sizes[0], n, &len);
11799a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com    dst->append(filter, len);
11899a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com    free(filter);
11999a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  }
12099a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com
12199a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  virtual bool KeyMayMatch(const Slice& key, const Slice& filter) const {
12299a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com    return (*key_match_)(state_, key.data(), key.size(),
12399a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com                         filter.data(), filter.size());
12499a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  }
12599a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com};
12699a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com
127d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comstruct leveldb_env_t {
128d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  Env* rep;
129d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  bool is_default;
130d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com};
131d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
132d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comstatic bool SaveError(char** errptr, const Status& s) {
133d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  assert(errptr != NULL);
134d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  if (s.ok()) {
135d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    return false;
136d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  } else if (*errptr == NULL) {
137d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    *errptr = strdup(s.ToString().c_str());
138d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  } else {
139d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    // TODO(sanjay): Merge with existing error?
140d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    free(*errptr);
141d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    *errptr = strdup(s.ToString().c_str());
142d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  }
143d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  return true;
144d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
145d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
146d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comstatic char* CopyString(const std::string& str) {
147d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  char* result = reinterpret_cast<char*>(malloc(sizeof(char) * str.size()));
148d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  memcpy(result, str.data(), sizeof(char) * str.size());
149d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  return result;
150d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
151d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
152d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comleveldb_t* leveldb_open(
153d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    const leveldb_options_t* options,
154d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    const char* name,
155d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    char** errptr) {
156d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  DB* db;
157d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  if (SaveError(errptr, DB::Open(options->rep, std::string(name), &db))) {
158d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    return NULL;
159d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  }
160d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  leveldb_t* result = new leveldb_t;
161d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  result->rep = db;
162d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  return result;
163d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
164d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
165d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_close(leveldb_t* db) {
166d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  delete db->rep;
167d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  delete db;
168d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
169d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
170d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_put(
171d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    leveldb_t* db,
172d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    const leveldb_writeoptions_t* options,
173d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    const char* key, size_t keylen,
174d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    const char* val, size_t vallen,
175d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    char** errptr) {
176d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  SaveError(errptr,
177d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com            db->rep->Put(options->rep, Slice(key, keylen), Slice(val, vallen)));
178d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
179d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
180d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_delete(
181d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    leveldb_t* db,
182d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    const leveldb_writeoptions_t* options,
183d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    const char* key, size_t keylen,
184d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    char** errptr) {
185d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  SaveError(errptr, db->rep->Delete(options->rep, Slice(key, keylen)));
186d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
187d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
188d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
189d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_write(
190d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    leveldb_t* db,
191d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    const leveldb_writeoptions_t* options,
192d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    leveldb_writebatch_t* batch,
193d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    char** errptr) {
194d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  SaveError(errptr, db->rep->Write(options->rep, &batch->rep));
195d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
196d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
197d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comchar* leveldb_get(
198d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    leveldb_t* db,
199d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    const leveldb_readoptions_t* options,
200d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    const char* key, size_t keylen,
201d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    size_t* vallen,
202d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    char** errptr) {
203d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  char* result = NULL;
204d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  std::string tmp;
205d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  Status s = db->rep->Get(options->rep, Slice(key, keylen), &tmp);
206d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  if (s.ok()) {
207d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    *vallen = tmp.size();
208d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    result = CopyString(tmp);
209d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  } else {
210d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    *vallen = 0;
211d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    if (!s.IsNotFound()) {
212d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com      SaveError(errptr, s);
213d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    }
214d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  }
215d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  return result;
216d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
217d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
218d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comleveldb_iterator_t* leveldb_create_iterator(
219d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    leveldb_t* db,
220d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    const leveldb_readoptions_t* options) {
221d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  leveldb_iterator_t* result = new leveldb_iterator_t;
222d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  result->rep = db->rep->NewIterator(options->rep);
223d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  return result;
224d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
225d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
226d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comconst leveldb_snapshot_t* leveldb_create_snapshot(
227d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    leveldb_t* db) {
228d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  leveldb_snapshot_t* result = new leveldb_snapshot_t;
229d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  result->rep = db->rep->GetSnapshot();
230d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  return result;
231d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
232d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
233d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_release_snapshot(
234d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    leveldb_t* db,
235d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    const leveldb_snapshot_t* snapshot) {
236d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  db->rep->ReleaseSnapshot(snapshot->rep);
237d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  delete snapshot;
238d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
239d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
2407e50a01f8a2820ed7a50c673b5affe0131560ff5gabor@google.comchar* leveldb_property_value(
241d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    leveldb_t* db,
242d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    const char* propname) {
243d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  std::string tmp;
244d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  if (db->rep->GetProperty(Slice(propname), &tmp)) {
245d36ce84e66c7d3cee978fbeb52721c30dfb842a5gabor@google.com    // We use strdup() since we expect human readable output.
246d36ce84e66c7d3cee978fbeb52721c30dfb842a5gabor@google.com    return strdup(tmp.c_str());
247d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  } else {
248d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    return NULL;
249d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  }
250d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
251d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
252d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_approximate_sizes(
253d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    leveldb_t* db,
254d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    int num_ranges,
255d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    const char* const* range_start_key, const size_t* range_start_key_len,
256d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    const char* const* range_limit_key, const size_t* range_limit_key_len,
257d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    uint64_t* sizes) {
258d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  Range* ranges = new Range[num_ranges];
259d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  for (int i = 0; i < num_ranges; i++) {
260d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    ranges[i].start = Slice(range_start_key[i], range_start_key_len[i]);
261d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    ranges[i].limit = Slice(range_limit_key[i], range_limit_key_len[i]);
262d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  }
263d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  db->rep->GetApproximateSizes(ranges, num_ranges, sizes);
264d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  delete[] ranges;
265d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
266d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
26799a7585544fc162a5f8dd39a6add00776a981efesanjay@google.comvoid leveldb_compact_range(
26899a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com    leveldb_t* db,
26999a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com    const char* start_key, size_t start_key_len,
27099a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com    const char* limit_key, size_t limit_key_len) {
27199a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  Slice a, b;
27299a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  db->rep->CompactRange(
27399a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com      // Pass NULL Slice if corresponding "const char*" is NULL
27499a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com      (start_key ? (a = Slice(start_key, start_key_len), &a) : NULL),
27599a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com      (limit_key ? (b = Slice(limit_key, limit_key_len), &b) : NULL));
27699a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com}
27799a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com
278d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_destroy_db(
279d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    const leveldb_options_t* options,
280d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    const char* name,
281d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    char** errptr) {
282d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  SaveError(errptr, DestroyDB(name, options->rep));
283d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
284d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
285d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_repair_db(
286d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    const leveldb_options_t* options,
287d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    const char* name,
288d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    char** errptr) {
289d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  SaveError(errptr, RepairDB(name, options->rep));
290d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
291d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
292d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_iter_destroy(leveldb_iterator_t* iter) {
293d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  delete iter->rep;
294d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  delete iter;
295d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
296d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
297d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comunsigned char leveldb_iter_valid(const leveldb_iterator_t* iter) {
298d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  return iter->rep->Valid();
299d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
300d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
301d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_iter_seek_to_first(leveldb_iterator_t* iter) {
302d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  iter->rep->SeekToFirst();
303d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
304d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
305d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_iter_seek_to_last(leveldb_iterator_t* iter) {
306d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  iter->rep->SeekToLast();
307d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
308d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
309d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_iter_seek(leveldb_iterator_t* iter, const char* k, size_t klen) {
310d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  iter->rep->Seek(Slice(k, klen));
311d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
312d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
313d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_iter_next(leveldb_iterator_t* iter) {
314d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  iter->rep->Next();
315d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
316d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
317d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_iter_prev(leveldb_iterator_t* iter) {
318d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  iter->rep->Prev();
319d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
320d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
321d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comconst char* leveldb_iter_key(const leveldb_iterator_t* iter, size_t* klen) {
322d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  Slice s = iter->rep->key();
323d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  *klen = s.size();
324d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  return s.data();
325d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
326d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
327d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comconst char* leveldb_iter_value(const leveldb_iterator_t* iter, size_t* vlen) {
328d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  Slice s = iter->rep->value();
329d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  *vlen = s.size();
330d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  return s.data();
331d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
332d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
333d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_iter_get_error(const leveldb_iterator_t* iter, char** errptr) {
334d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  SaveError(errptr, iter->rep->status());
335d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
336d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
337d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comleveldb_writebatch_t* leveldb_writebatch_create() {
338d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  return new leveldb_writebatch_t;
339d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
340d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
341d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_writebatch_destroy(leveldb_writebatch_t* b) {
342d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  delete b;
343d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
344d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
345d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_writebatch_clear(leveldb_writebatch_t* b) {
346d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  b->rep.Clear();
347d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
348d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
349d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_writebatch_put(
350d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    leveldb_writebatch_t* b,
351d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    const char* key, size_t klen,
352d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    const char* val, size_t vlen) {
353d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  b->rep.Put(Slice(key, klen), Slice(val, vlen));
354d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
355d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
356d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_writebatch_delete(
357d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    leveldb_writebatch_t* b,
358d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    const char* key, size_t klen) {
359d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  b->rep.Delete(Slice(key, klen));
360d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
361d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
362d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_writebatch_iterate(
363d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    leveldb_writebatch_t* b,
364d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    void* state,
365d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    void (*put)(void*, const char* k, size_t klen, const char* v, size_t vlen),
366d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    void (*deleted)(void*, const char* k, size_t klen)) {
367d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  class H : public WriteBatch::Handler {
368d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com   public:
369d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    void* state_;
370d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    void (*put_)(void*, const char* k, size_t klen, const char* v, size_t vlen);
371d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    void (*deleted_)(void*, const char* k, size_t klen);
372d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    virtual void Put(const Slice& key, const Slice& value) {
373d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com      (*put_)(state_, key.data(), key.size(), value.data(), value.size());
374d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    }
375d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    virtual void Delete(const Slice& key) {
376d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com      (*deleted_)(state_, key.data(), key.size());
377d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    }
378d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  };
379d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  H handler;
380d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  handler.state_ = state;
381d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  handler.put_ = put;
382d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  handler.deleted_ = deleted;
383d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  b->rep.Iterate(&handler);
384d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
385d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
386d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comleveldb_options_t* leveldb_options_create() {
387d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  return new leveldb_options_t;
388d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
389d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
390d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_options_destroy(leveldb_options_t* options) {
391d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  delete options;
392d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
393d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
394d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_options_set_comparator(
395d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    leveldb_options_t* opt,
396d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    leveldb_comparator_t* cmp) {
397d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  opt->rep.comparator = cmp;
398d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
399d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
40099a7585544fc162a5f8dd39a6add00776a981efesanjay@google.comvoid leveldb_options_set_filter_policy(
40199a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com    leveldb_options_t* opt,
40299a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com    leveldb_filterpolicy_t* policy) {
40399a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  opt->rep.filter_policy = policy;
40499a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com}
40599a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com
406d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_options_set_create_if_missing(
407d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    leveldb_options_t* opt, unsigned char v) {
408d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  opt->rep.create_if_missing = v;
409d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
410d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
411d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_options_set_error_if_exists(
412d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    leveldb_options_t* opt, unsigned char v) {
413d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  opt->rep.error_if_exists = v;
414d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
415d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
416d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_options_set_paranoid_checks(
417d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    leveldb_options_t* opt, unsigned char v) {
418d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  opt->rep.paranoid_checks = v;
419d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
420d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
421d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_options_set_env(leveldb_options_t* opt, leveldb_env_t* env) {
422d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  opt->rep.env = (env ? env->rep : NULL);
423d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
424d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
425d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_options_set_info_log(leveldb_options_t* opt, leveldb_logger_t* l) {
426d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  opt->rep.info_log = (l ? l->rep : NULL);
427d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
428d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
429d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_options_set_write_buffer_size(leveldb_options_t* opt, size_t s) {
430d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  opt->rep.write_buffer_size = s;
431d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
432d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
433d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_options_set_max_open_files(leveldb_options_t* opt, int n) {
434d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  opt->rep.max_open_files = n;
435d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
436d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
437d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_options_set_cache(leveldb_options_t* opt, leveldb_cache_t* c) {
438d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  opt->rep.block_cache = c->rep;
439d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
440d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
441d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_options_set_block_size(leveldb_options_t* opt, size_t s) {
442d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  opt->rep.block_size = s;
443d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
444d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
445d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_options_set_block_restart_interval(leveldb_options_t* opt, int n) {
446d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  opt->rep.block_restart_interval = n;
447d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
448d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
449d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_options_set_compression(leveldb_options_t* opt, int t) {
450d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  opt->rep.compression = static_cast<CompressionType>(t);
451d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
452d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
453d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comleveldb_comparator_t* leveldb_comparator_create(
454d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    void* state,
455d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    void (*destructor)(void*),
456d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    int (*compare)(
457d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com        void*,
458d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com        const char* a, size_t alen,
459d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com        const char* b, size_t blen),
460d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    const char* (*name)(void*)) {
461d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  leveldb_comparator_t* result = new leveldb_comparator_t;
462d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  result->state_ = state;
463d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  result->destructor_ = destructor;
464d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  result->compare_ = compare;
465d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  result->name_ = name;
466d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  return result;
467d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
468d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
469d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_comparator_destroy(leveldb_comparator_t* cmp) {
470d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  delete cmp;
471d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
472d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
47399a7585544fc162a5f8dd39a6add00776a981efesanjay@google.comleveldb_filterpolicy_t* leveldb_filterpolicy_create(
47499a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com    void* state,
47599a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com    void (*destructor)(void*),
47699a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com    char* (*create_filter)(
47799a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com        void*,
47899a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com        const char* const* key_array, const size_t* key_length_array,
47999a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com        int num_keys,
48099a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com        size_t* filter_length),
48199a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com    unsigned char (*key_may_match)(
48299a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com        void*,
48399a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com        const char* key, size_t length,
48499a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com        const char* filter, size_t filter_length),
48599a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com    const char* (*name)(void*)) {
48699a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  leveldb_filterpolicy_t* result = new leveldb_filterpolicy_t;
48799a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  result->state_ = state;
48899a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  result->destructor_ = destructor;
48999a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  result->create_ = create_filter;
49099a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  result->key_match_ = key_may_match;
49199a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  result->name_ = name;
49299a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  return result;
49399a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com}
49499a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com
49599a7585544fc162a5f8dd39a6add00776a981efesanjay@google.comvoid leveldb_filterpolicy_destroy(leveldb_filterpolicy_t* filter) {
49699a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  delete filter;
49799a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com}
49899a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com
49999a7585544fc162a5f8dd39a6add00776a981efesanjay@google.comleveldb_filterpolicy_t* leveldb_filterpolicy_create_bloom(int bits_per_key) {
50099a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  // Make a leveldb_filterpolicy_t, but override all of its methods so
50199a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  // they delegate to a NewBloomFilterPolicy() instead of user
50299a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  // supplied C functions.
50399a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  struct Wrapper : public leveldb_filterpolicy_t {
50499a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com    const FilterPolicy* rep_;
50599a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com    ~Wrapper() { delete rep_; }
50699a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com    const char* Name() const { return rep_->Name(); }
50799a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com    void CreateFilter(const Slice* keys, int n, std::string* dst) const {
50899a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com      return rep_->CreateFilter(keys, n, dst);
50999a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com    }
51099a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com    bool KeyMayMatch(const Slice& key, const Slice& filter) const {
51199a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com      return rep_->KeyMayMatch(key, filter);
51299a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com    }
51399a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com    static void DoNothing(void*) { }
51499a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  };
51599a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  Wrapper* wrapper = new Wrapper;
51699a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  wrapper->rep_ = NewBloomFilterPolicy(bits_per_key);
51799a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  wrapper->state_ = NULL;
51899a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  wrapper->destructor_ = &Wrapper::DoNothing;
51999a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  return wrapper;
52099a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com}
52199a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com
522d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comleveldb_readoptions_t* leveldb_readoptions_create() {
523d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  return new leveldb_readoptions_t;
524d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
525d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
526d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_readoptions_destroy(leveldb_readoptions_t* opt) {
527d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  delete opt;
528d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
529d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
530d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_readoptions_set_verify_checksums(
531d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    leveldb_readoptions_t* opt,
532d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    unsigned char v) {
533d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  opt->rep.verify_checksums = v;
534d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
535d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
536d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_readoptions_set_fill_cache(
537d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    leveldb_readoptions_t* opt, unsigned char v) {
538d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  opt->rep.fill_cache = v;
539d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
540d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
541d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_readoptions_set_snapshot(
542d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    leveldb_readoptions_t* opt,
543d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    const leveldb_snapshot_t* snap) {
544d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  opt->rep.snapshot = (snap ? snap->rep : NULL);
545d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
546d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
547d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comleveldb_writeoptions_t* leveldb_writeoptions_create() {
548d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  return new leveldb_writeoptions_t;
549d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
550d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
551d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_writeoptions_destroy(leveldb_writeoptions_t* opt) {
552d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  delete opt;
553d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
554d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
555d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_writeoptions_set_sync(
556d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com    leveldb_writeoptions_t* opt, unsigned char v) {
557d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  opt->rep.sync = v;
558d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
559d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
560d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comleveldb_cache_t* leveldb_cache_create_lru(size_t capacity) {
561d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  leveldb_cache_t* c = new leveldb_cache_t;
562d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  c->rep = NewLRUCache(capacity);
563d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  return c;
564d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
565d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
566d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_cache_destroy(leveldb_cache_t* cache) {
567d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  delete cache->rep;
568d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  delete cache;
569d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
570d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
571d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comleveldb_env_t* leveldb_create_default_env() {
572d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  leveldb_env_t* result = new leveldb_env_t;
573d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  result->rep = Env::Default();
574d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  result->is_default = true;
575d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  return result;
576d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
577d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
578d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.comvoid leveldb_env_destroy(leveldb_env_t* env) {
579d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  if (!env->is_default) delete env->rep;
580d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com  delete env;
581d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}
582d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com
58329c68f16466b1704dc7a663cf51bf9c5579830c3dgrogan@chromium.orgvoid leveldb_free(void* ptr) {
58429c68f16466b1704dc7a663cf51bf9c5579830c3dgrogan@chromium.org  free(ptr);
58529c68f16466b1704dc7a663cf51bf9c5579830c3dgrogan@chromium.org}
58629c68f16466b1704dc7a663cf51bf9c5579830c3dgrogan@chromium.org
587b0edd034dee25db7a3e42243b95c66eaf95e8a81sanjay@google.comint leveldb_major_version() {
588b0edd034dee25db7a3e42243b95c66eaf95e8a81sanjay@google.com  return kMajorVersion;
589b0edd034dee25db7a3e42243b95c66eaf95e8a81sanjay@google.com}
590b0edd034dee25db7a3e42243b95c66eaf95e8a81sanjay@google.com
591b0edd034dee25db7a3e42243b95c66eaf95e8a81sanjay@google.comint leveldb_minor_version() {
592b0edd034dee25db7a3e42243b95c66eaf95e8a81sanjay@google.com  return kMinorVersion;
593b0edd034dee25db7a3e42243b95c66eaf95e8a81sanjay@google.com}
594b0edd034dee25db7a3e42243b95c66eaf95e8a81sanjay@google.com
595d2bd50ef02756a6a92bd6d7c65c045f3c7297090gabor@google.com}  // end extern "C"
596