2011-08-05 20:40:49 +00:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
|
|
|
#include "leveldb/c.h"
|
|
|
|
|
2019-05-28 10:17:03 -07:00
|
|
|
#include <cstdint>
|
|
|
|
#include <cstdlib>
|
2019-05-02 11:01:00 -07:00
|
|
|
|
2011-08-05 20:40:49 +00:00
|
|
|
#include "leveldb/cache.h"
|
|
|
|
#include "leveldb/comparator.h"
|
|
|
|
#include "leveldb/db.h"
|
|
|
|
#include "leveldb/env.h"
|
2012-04-17 08:36:46 -07:00
|
|
|
#include "leveldb/filter_policy.h"
|
2011-08-05 20:40:49 +00:00
|
|
|
#include "leveldb/iterator.h"
|
|
|
|
#include "leveldb/options.h"
|
|
|
|
#include "leveldb/status.h"
|
|
|
|
#include "leveldb/write_batch.h"
|
|
|
|
|
2011-08-16 01:21:01 +00:00
|
|
|
using leveldb::Cache;
|
|
|
|
using leveldb::Comparator;
|
|
|
|
using leveldb::CompressionType;
|
|
|
|
using leveldb::DB;
|
|
|
|
using leveldb::Env;
|
|
|
|
using leveldb::FileLock;
|
2012-04-17 08:36:46 -07:00
|
|
|
using leveldb::FilterPolicy;
|
2011-08-16 01:21:01 +00:00
|
|
|
using leveldb::Iterator;
|
2012-10-16 16:17:53 -07:00
|
|
|
using leveldb::kMajorVersion;
|
|
|
|
using leveldb::kMinorVersion;
|
2011-08-16 01:21:01 +00:00
|
|
|
using leveldb::Logger;
|
2012-04-17 08:36:46 -07:00
|
|
|
using leveldb::NewBloomFilterPolicy;
|
2011-08-16 01:21:01 +00:00
|
|
|
using leveldb::NewLRUCache;
|
|
|
|
using leveldb::Options;
|
|
|
|
using leveldb::RandomAccessFile;
|
|
|
|
using leveldb::Range;
|
|
|
|
using leveldb::ReadOptions;
|
|
|
|
using leveldb::SequentialFile;
|
|
|
|
using leveldb::Slice;
|
|
|
|
using leveldb::Snapshot;
|
|
|
|
using leveldb::Status;
|
|
|
|
using leveldb::WritableFile;
|
|
|
|
using leveldb::WriteBatch;
|
|
|
|
using leveldb::WriteOptions;
|
2011-08-05 20:40:49 +00:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
struct leveldb_t {
|
|
|
|
DB* rep;
|
|
|
|
};
|
|
|
|
struct leveldb_iterator_t {
|
|
|
|
Iterator* rep;
|
|
|
|
};
|
|
|
|
struct leveldb_writebatch_t {
|
|
|
|
WriteBatch rep;
|
|
|
|
};
|
|
|
|
struct leveldb_snapshot_t {
|
|
|
|
const Snapshot* rep;
|
|
|
|
};
|
|
|
|
struct leveldb_readoptions_t {
|
|
|
|
ReadOptions rep;
|
|
|
|
};
|
|
|
|
struct leveldb_writeoptions_t {
|
|
|
|
WriteOptions rep;
|
|
|
|
};
|
|
|
|
struct leveldb_options_t {
|
|
|
|
Options rep;
|
|
|
|
};
|
|
|
|
struct leveldb_cache_t {
|
|
|
|
Cache* rep;
|
|
|
|
};
|
|
|
|
struct leveldb_seqfile_t {
|
|
|
|
SequentialFile* rep;
|
|
|
|
};
|
|
|
|
struct leveldb_randomfile_t {
|
|
|
|
RandomAccessFile* rep;
|
|
|
|
};
|
|
|
|
struct leveldb_writablefile_t {
|
|
|
|
WritableFile* rep;
|
|
|
|
};
|
|
|
|
struct leveldb_logger_t {
|
|
|
|
Logger* rep;
|
|
|
|
};
|
|
|
|
struct leveldb_filelock_t {
|
|
|
|
FileLock* rep;
|
|
|
|
};
|
2011-08-05 20:40:49 +00:00
|
|
|
|
|
|
|
struct leveldb_comparator_t : public Comparator {
|
2019-05-09 14:00:07 -07:00
|
|
|
~leveldb_comparator_t() override { (*destructor_)(state_); }
|
2011-08-05 20:40:49 +00:00
|
|
|
|
2019-05-09 14:00:07 -07:00
|
|
|
int Compare(const Slice& a, const Slice& b) const override {
|
2011-08-05 20:40:49 +00:00
|
|
|
return (*compare_)(state_, a.data(), a.size(), b.data(), b.size());
|
|
|
|
}
|
|
|
|
|
2019-05-09 14:00:07 -07:00
|
|
|
const char* Name() const override { return (*name_)(state_); }
|
2011-08-05 20:40:49 +00:00
|
|
|
|
|
|
|
// No-ops since the C binding does not support key shortening methods.
|
2019-05-09 14:00:07 -07:00
|
|
|
void FindShortestSeparator(std::string*, const Slice&) const override {}
|
|
|
|
void FindShortSuccessor(std::string* key) const override {}
|
2011-08-05 20:40:49 +00:00
|
|
|
|
2012-04-17 08:36:46 -07:00
|
|
|
void* state_;
|
|
|
|
void (*destructor_)(void*);
|
2019-05-03 09:31:18 -07:00
|
|
|
int (*compare_)(void*, const char* a, size_t alen, const char* b,
|
|
|
|
size_t blen);
|
2012-04-17 08:36:46 -07:00
|
|
|
const char* (*name_)(void*);
|
2019-05-03 09:31:18 -07:00
|
|
|
};
|
2012-04-17 08:36:46 -07:00
|
|
|
|
2019-05-03 09:31:18 -07:00
|
|
|
struct leveldb_filterpolicy_t : public FilterPolicy {
|
2019-05-09 14:00:07 -07:00
|
|
|
~leveldb_filterpolicy_t() override { (*destructor_)(state_); }
|
2019-05-02 11:01:00 -07:00
|
|
|
|
2019-05-09 14:00:07 -07:00
|
|
|
const char* Name() const override { return (*name_)(state_); }
|
2012-04-17 08:36:46 -07:00
|
|
|
|
2019-05-09 14:00:07 -07:00
|
|
|
void CreateFilter(const Slice* keys, int n, std::string* dst) const override {
|
2012-04-17 08:36:46 -07:00
|
|
|
std::vector<const char*> key_pointers(n);
|
|
|
|
std::vector<size_t> key_sizes(n);
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
key_pointers[i] = keys[i].data();
|
|
|
|
key_sizes[i] = keys[i].size();
|
|
|
|
}
|
|
|
|
size_t len;
|
|
|
|
char* filter = (*create_)(state_, &key_pointers[0], &key_sizes[0], n, &len);
|
|
|
|
dst->append(filter, len);
|
|
|
|
free(filter);
|
|
|
|
}
|
|
|
|
|
2019-05-09 14:00:07 -07:00
|
|
|
bool KeyMayMatch(const Slice& key, const Slice& filter) const override {
|
2019-05-02 11:01:00 -07:00
|
|
|
return (*key_match_)(state_, key.data(), key.size(), filter.data(),
|
|
|
|
filter.size());
|
2012-04-17 08:36:46 -07:00
|
|
|
}
|
2019-05-03 09:31:18 -07:00
|
|
|
|
|
|
|
void* state_;
|
|
|
|
void (*destructor_)(void*);
|
|
|
|
const char* (*name_)(void*);
|
|
|
|
char* (*create_)(void*, const char* const* key_array,
|
|
|
|
const size_t* key_length_array, int num_keys,
|
|
|
|
size_t* filter_length);
|
2019-05-28 10:17:03 -07:00
|
|
|
uint8_t (*key_match_)(void*, const char* key, size_t length,
|
|
|
|
const char* filter, size_t filter_length);
|
2012-04-17 08:36:46 -07:00
|
|
|
};
|
|
|
|
|
2011-08-05 20:40:49 +00:00
|
|
|
struct leveldb_env_t {
|
|
|
|
Env* rep;
|
|
|
|
bool is_default;
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool SaveError(char** errptr, const Status& s) {
|
2018-04-10 16:18:06 -07:00
|
|
|
assert(errptr != nullptr);
|
2011-08-05 20:40:49 +00:00
|
|
|
if (s.ok()) {
|
|
|
|
return false;
|
2018-04-10 16:18:06 -07:00
|
|
|
} else if (*errptr == nullptr) {
|
2011-08-05 20:40:49 +00:00
|
|
|
*errptr = strdup(s.ToString().c_str());
|
|
|
|
} else {
|
|
|
|
// TODO(sanjay): Merge with existing error?
|
|
|
|
free(*errptr);
|
|
|
|
*errptr = strdup(s.ToString().c_str());
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char* CopyString(const std::string& str) {
|
|
|
|
char* result = reinterpret_cast<char*>(malloc(sizeof(char) * str.size()));
|
2020-04-29 22:31:41 +00:00
|
|
|
std::memcpy(result, str.data(), sizeof(char) * str.size());
|
2011-08-05 20:40:49 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
leveldb_t* leveldb_open(const leveldb_options_t* options, const char* name,
|
|
|
|
char** errptr) {
|
2011-08-05 20:40:49 +00:00
|
|
|
DB* db;
|
|
|
|
if (SaveError(errptr, DB::Open(options->rep, std::string(name), &db))) {
|
2018-04-10 16:18:06 -07:00
|
|
|
return nullptr;
|
2011-08-05 20:40:49 +00:00
|
|
|
}
|
|
|
|
leveldb_t* result = new leveldb_t;
|
|
|
|
result->rep = db;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void leveldb_close(leveldb_t* db) {
|
|
|
|
delete db->rep;
|
|
|
|
delete db;
|
|
|
|
}
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
void leveldb_put(leveldb_t* db, const leveldb_writeoptions_t* options,
|
|
|
|
const char* key, size_t keylen, const char* val, size_t vallen,
|
|
|
|
char** errptr) {
|
2011-08-05 20:40:49 +00:00
|
|
|
SaveError(errptr,
|
|
|
|
db->rep->Put(options->rep, Slice(key, keylen), Slice(val, vallen)));
|
|
|
|
}
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
void leveldb_delete(leveldb_t* db, const leveldb_writeoptions_t* options,
|
|
|
|
const char* key, size_t keylen, char** errptr) {
|
2011-08-05 20:40:49 +00:00
|
|
|
SaveError(errptr, db->rep->Delete(options->rep, Slice(key, keylen)));
|
|
|
|
}
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
void leveldb_write(leveldb_t* db, const leveldb_writeoptions_t* options,
|
|
|
|
leveldb_writebatch_t* batch, char** errptr) {
|
2011-08-05 20:40:49 +00:00
|
|
|
SaveError(errptr, db->rep->Write(options->rep, &batch->rep));
|
|
|
|
}
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
char* leveldb_get(leveldb_t* db, const leveldb_readoptions_t* options,
|
|
|
|
const char* key, size_t keylen, size_t* vallen,
|
|
|
|
char** errptr) {
|
2018-04-10 16:18:06 -07:00
|
|
|
char* result = nullptr;
|
2011-08-05 20:40:49 +00:00
|
|
|
std::string tmp;
|
|
|
|
Status s = db->rep->Get(options->rep, Slice(key, keylen), &tmp);
|
|
|
|
if (s.ok()) {
|
|
|
|
*vallen = tmp.size();
|
|
|
|
result = CopyString(tmp);
|
|
|
|
} else {
|
|
|
|
*vallen = 0;
|
|
|
|
if (!s.IsNotFound()) {
|
|
|
|
SaveError(errptr, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
leveldb_iterator_t* leveldb_create_iterator(
|
2019-05-02 11:01:00 -07:00
|
|
|
leveldb_t* db, const leveldb_readoptions_t* options) {
|
2011-08-05 20:40:49 +00:00
|
|
|
leveldb_iterator_t* result = new leveldb_iterator_t;
|
|
|
|
result->rep = db->rep->NewIterator(options->rep);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
const leveldb_snapshot_t* leveldb_create_snapshot(leveldb_t* db) {
|
2011-08-05 20:40:49 +00:00
|
|
|
leveldb_snapshot_t* result = new leveldb_snapshot_t;
|
|
|
|
result->rep = db->rep->GetSnapshot();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
void leveldb_release_snapshot(leveldb_t* db,
|
|
|
|
const leveldb_snapshot_t* snapshot) {
|
2011-08-05 20:40:49 +00:00
|
|
|
db->rep->ReleaseSnapshot(snapshot->rep);
|
|
|
|
delete snapshot;
|
|
|
|
}
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
char* leveldb_property_value(leveldb_t* db, const char* propname) {
|
2011-08-05 20:40:49 +00:00
|
|
|
std::string tmp;
|
|
|
|
if (db->rep->GetProperty(Slice(propname), &tmp)) {
|
2011-08-22 21:08:51 +00:00
|
|
|
// We use strdup() since we expect human readable output.
|
|
|
|
return strdup(tmp.c_str());
|
2011-08-05 20:40:49 +00:00
|
|
|
} else {
|
2018-04-10 16:18:06 -07:00
|
|
|
return nullptr;
|
2011-08-05 20:40:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
void leveldb_approximate_sizes(leveldb_t* db, int num_ranges,
|
|
|
|
const char* const* range_start_key,
|
|
|
|
const size_t* range_start_key_len,
|
|
|
|
const char* const* range_limit_key,
|
|
|
|
const size_t* range_limit_key_len,
|
|
|
|
uint64_t* sizes) {
|
2011-08-05 20:40:49 +00:00
|
|
|
Range* ranges = new Range[num_ranges];
|
|
|
|
for (int i = 0; i < num_ranges; i++) {
|
|
|
|
ranges[i].start = Slice(range_start_key[i], range_start_key_len[i]);
|
|
|
|
ranges[i].limit = Slice(range_limit_key[i], range_limit_key_len[i]);
|
|
|
|
}
|
|
|
|
db->rep->GetApproximateSizes(ranges, num_ranges, sizes);
|
|
|
|
delete[] ranges;
|
|
|
|
}
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
void leveldb_compact_range(leveldb_t* db, const char* start_key,
|
|
|
|
size_t start_key_len, const char* limit_key,
|
|
|
|
size_t limit_key_len) {
|
2012-04-17 08:36:46 -07:00
|
|
|
Slice a, b;
|
|
|
|
db->rep->CompactRange(
|
2018-04-10 16:18:06 -07:00
|
|
|
// Pass null Slice if corresponding "const char*" is null
|
|
|
|
(start_key ? (a = Slice(start_key, start_key_len), &a) : nullptr),
|
|
|
|
(limit_key ? (b = Slice(limit_key, limit_key_len), &b) : nullptr));
|
2012-04-17 08:36:46 -07:00
|
|
|
}
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
void leveldb_destroy_db(const leveldb_options_t* options, const char* name,
|
|
|
|
char** errptr) {
|
2011-08-05 20:40:49 +00:00
|
|
|
SaveError(errptr, DestroyDB(name, options->rep));
|
|
|
|
}
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
void leveldb_repair_db(const leveldb_options_t* options, const char* name,
|
|
|
|
char** errptr) {
|
2011-08-05 20:40:49 +00:00
|
|
|
SaveError(errptr, RepairDB(name, options->rep));
|
|
|
|
}
|
|
|
|
|
|
|
|
void leveldb_iter_destroy(leveldb_iterator_t* iter) {
|
|
|
|
delete iter->rep;
|
|
|
|
delete iter;
|
|
|
|
}
|
|
|
|
|
2019-05-28 10:17:03 -07:00
|
|
|
uint8_t leveldb_iter_valid(const leveldb_iterator_t* iter) {
|
2011-08-05 20:40:49 +00:00
|
|
|
return iter->rep->Valid();
|
|
|
|
}
|
|
|
|
|
|
|
|
void leveldb_iter_seek_to_first(leveldb_iterator_t* iter) {
|
|
|
|
iter->rep->SeekToFirst();
|
|
|
|
}
|
|
|
|
|
|
|
|
void leveldb_iter_seek_to_last(leveldb_iterator_t* iter) {
|
|
|
|
iter->rep->SeekToLast();
|
|
|
|
}
|
|
|
|
|
|
|
|
void leveldb_iter_seek(leveldb_iterator_t* iter, const char* k, size_t klen) {
|
|
|
|
iter->rep->Seek(Slice(k, klen));
|
|
|
|
}
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
void leveldb_iter_next(leveldb_iterator_t* iter) { iter->rep->Next(); }
|
2011-08-05 20:40:49 +00:00
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
void leveldb_iter_prev(leveldb_iterator_t* iter) { iter->rep->Prev(); }
|
2011-08-05 20:40:49 +00:00
|
|
|
|
|
|
|
const char* leveldb_iter_key(const leveldb_iterator_t* iter, size_t* klen) {
|
|
|
|
Slice s = iter->rep->key();
|
|
|
|
*klen = s.size();
|
|
|
|
return s.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* leveldb_iter_value(const leveldb_iterator_t* iter, size_t* vlen) {
|
|
|
|
Slice s = iter->rep->value();
|
|
|
|
*vlen = s.size();
|
|
|
|
return s.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
void leveldb_iter_get_error(const leveldb_iterator_t* iter, char** errptr) {
|
|
|
|
SaveError(errptr, iter->rep->status());
|
|
|
|
}
|
|
|
|
|
|
|
|
leveldb_writebatch_t* leveldb_writebatch_create() {
|
|
|
|
return new leveldb_writebatch_t;
|
|
|
|
}
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
void leveldb_writebatch_destroy(leveldb_writebatch_t* b) { delete b; }
|
2011-08-05 20:40:49 +00:00
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
void leveldb_writebatch_clear(leveldb_writebatch_t* b) { b->rep.Clear(); }
|
2011-08-05 20:40:49 +00:00
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
void leveldb_writebatch_put(leveldb_writebatch_t* b, const char* key,
|
|
|
|
size_t klen, const char* val, size_t vlen) {
|
2011-08-05 20:40:49 +00:00
|
|
|
b->rep.Put(Slice(key, klen), Slice(val, vlen));
|
|
|
|
}
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
void leveldb_writebatch_delete(leveldb_writebatch_t* b, const char* key,
|
|
|
|
size_t klen) {
|
2011-08-05 20:40:49 +00:00
|
|
|
b->rep.Delete(Slice(key, klen));
|
|
|
|
}
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
void leveldb_writebatch_iterate(const leveldb_writebatch_t* b, void* state,
|
|
|
|
void (*put)(void*, const char* k, size_t klen,
|
|
|
|
const char* v, size_t vlen),
|
|
|
|
void (*deleted)(void*, const char* k,
|
|
|
|
size_t klen)) {
|
2011-08-05 20:40:49 +00:00
|
|
|
class H : public WriteBatch::Handler {
|
|
|
|
public:
|
|
|
|
void* state_;
|
|
|
|
void (*put_)(void*, const char* k, size_t klen, const char* v, size_t vlen);
|
|
|
|
void (*deleted_)(void*, const char* k, size_t klen);
|
2019-05-09 14:00:07 -07:00
|
|
|
void Put(const Slice& key, const Slice& value) override {
|
2011-08-05 20:40:49 +00:00
|
|
|
(*put_)(state_, key.data(), key.size(), value.data(), value.size());
|
|
|
|
}
|
2019-05-09 14:00:07 -07:00
|
|
|
void Delete(const Slice& key) override {
|
2011-08-05 20:40:49 +00:00
|
|
|
(*deleted_)(state_, key.data(), key.size());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
H handler;
|
|
|
|
handler.state_ = state;
|
|
|
|
handler.put_ = put;
|
|
|
|
handler.deleted_ = deleted;
|
|
|
|
b->rep.Iterate(&handler);
|
|
|
|
}
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
void leveldb_writebatch_append(leveldb_writebatch_t* destination,
|
|
|
|
const leveldb_writebatch_t* source) {
|
2018-08-19 15:17:37 -07:00
|
|
|
destination->rep.Append(source->rep);
|
|
|
|
}
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
leveldb_options_t* leveldb_options_create() { return new leveldb_options_t; }
|
2011-08-05 20:40:49 +00:00
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
void leveldb_options_destroy(leveldb_options_t* options) { delete options; }
|
2011-08-05 20:40:49 +00:00
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
void leveldb_options_set_comparator(leveldb_options_t* opt,
|
|
|
|
leveldb_comparator_t* cmp) {
|
2011-08-05 20:40:49 +00:00
|
|
|
opt->rep.comparator = cmp;
|
|
|
|
}
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
void leveldb_options_set_filter_policy(leveldb_options_t* opt,
|
|
|
|
leveldb_filterpolicy_t* policy) {
|
2012-04-17 08:36:46 -07:00
|
|
|
opt->rep.filter_policy = policy;
|
|
|
|
}
|
|
|
|
|
2019-05-28 10:17:03 -07:00
|
|
|
void leveldb_options_set_create_if_missing(leveldb_options_t* opt, uint8_t v) {
|
2011-08-05 20:40:49 +00:00
|
|
|
opt->rep.create_if_missing = v;
|
|
|
|
}
|
|
|
|
|
2019-05-28 10:17:03 -07:00
|
|
|
void leveldb_options_set_error_if_exists(leveldb_options_t* opt, uint8_t v) {
|
2011-08-05 20:40:49 +00:00
|
|
|
opt->rep.error_if_exists = v;
|
|
|
|
}
|
|
|
|
|
2019-05-28 10:17:03 -07:00
|
|
|
void leveldb_options_set_paranoid_checks(leveldb_options_t* opt, uint8_t v) {
|
2011-08-05 20:40:49 +00:00
|
|
|
opt->rep.paranoid_checks = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
void leveldb_options_set_env(leveldb_options_t* opt, leveldb_env_t* env) {
|
2018-04-10 16:18:06 -07:00
|
|
|
opt->rep.env = (env ? env->rep : nullptr);
|
2011-08-05 20:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void leveldb_options_set_info_log(leveldb_options_t* opt, leveldb_logger_t* l) {
|
2018-04-10 16:18:06 -07:00
|
|
|
opt->rep.info_log = (l ? l->rep : nullptr);
|
2011-08-05 20:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void leveldb_options_set_write_buffer_size(leveldb_options_t* opt, size_t s) {
|
|
|
|
opt->rep.write_buffer_size = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void leveldb_options_set_max_open_files(leveldb_options_t* opt, int n) {
|
|
|
|
opt->rep.max_open_files = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
void leveldb_options_set_cache(leveldb_options_t* opt, leveldb_cache_t* c) {
|
|
|
|
opt->rep.block_cache = c->rep;
|
|
|
|
}
|
|
|
|
|
|
|
|
void leveldb_options_set_block_size(leveldb_options_t* opt, size_t s) {
|
|
|
|
opt->rep.block_size = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void leveldb_options_set_block_restart_interval(leveldb_options_t* opt, int n) {
|
|
|
|
opt->rep.block_restart_interval = n;
|
|
|
|
}
|
|
|
|
|
2017-10-25 16:13:51 -07:00
|
|
|
void leveldb_options_set_max_file_size(leveldb_options_t* opt, size_t s) {
|
|
|
|
opt->rep.max_file_size = s;
|
|
|
|
}
|
|
|
|
|
2011-08-05 20:40:49 +00:00
|
|
|
void leveldb_options_set_compression(leveldb_options_t* opt, int t) {
|
|
|
|
opt->rep.compression = static_cast<CompressionType>(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
leveldb_comparator_t* leveldb_comparator_create(
|
2019-05-02 11:01:00 -07:00
|
|
|
void* state, void (*destructor)(void*),
|
|
|
|
int (*compare)(void*, const char* a, size_t alen, const char* b,
|
|
|
|
size_t blen),
|
2011-08-05 20:40:49 +00:00
|
|
|
const char* (*name)(void*)) {
|
|
|
|
leveldb_comparator_t* result = new leveldb_comparator_t;
|
|
|
|
result->state_ = state;
|
|
|
|
result->destructor_ = destructor;
|
|
|
|
result->compare_ = compare;
|
|
|
|
result->name_ = name;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
void leveldb_comparator_destroy(leveldb_comparator_t* cmp) { delete cmp; }
|
2011-08-05 20:40:49 +00:00
|
|
|
|
2012-04-17 08:36:46 -07:00
|
|
|
leveldb_filterpolicy_t* leveldb_filterpolicy_create(
|
2019-05-02 11:01:00 -07:00
|
|
|
void* state, void (*destructor)(void*),
|
|
|
|
char* (*create_filter)(void*, const char* const* key_array,
|
|
|
|
const size_t* key_length_array, int num_keys,
|
|
|
|
size_t* filter_length),
|
2019-05-28 10:17:03 -07:00
|
|
|
uint8_t (*key_may_match)(void*, const char* key, size_t length,
|
|
|
|
const char* filter, size_t filter_length),
|
2012-04-17 08:36:46 -07:00
|
|
|
const char* (*name)(void*)) {
|
|
|
|
leveldb_filterpolicy_t* result = new leveldb_filterpolicy_t;
|
|
|
|
result->state_ = state;
|
|
|
|
result->destructor_ = destructor;
|
|
|
|
result->create_ = create_filter;
|
|
|
|
result->key_match_ = key_may_match;
|
|
|
|
result->name_ = name;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void leveldb_filterpolicy_destroy(leveldb_filterpolicy_t* filter) {
|
|
|
|
delete filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
leveldb_filterpolicy_t* leveldb_filterpolicy_create_bloom(int bits_per_key) {
|
|
|
|
// Make a leveldb_filterpolicy_t, but override all of its methods so
|
|
|
|
// they delegate to a NewBloomFilterPolicy() instead of user
|
|
|
|
// supplied C functions.
|
|
|
|
struct Wrapper : public leveldb_filterpolicy_t {
|
2019-05-03 09:31:18 -07:00
|
|
|
static void DoNothing(void*) {}
|
|
|
|
|
2012-04-17 08:36:46 -07:00
|
|
|
~Wrapper() { delete rep_; }
|
|
|
|
const char* Name() const { return rep_->Name(); }
|
|
|
|
void CreateFilter(const Slice* keys, int n, std::string* dst) const {
|
|
|
|
return rep_->CreateFilter(keys, n, dst);
|
|
|
|
}
|
|
|
|
bool KeyMayMatch(const Slice& key, const Slice& filter) const {
|
|
|
|
return rep_->KeyMayMatch(key, filter);
|
|
|
|
}
|
2019-05-03 09:31:18 -07:00
|
|
|
|
|
|
|
const FilterPolicy* rep_;
|
2012-04-17 08:36:46 -07:00
|
|
|
};
|
|
|
|
Wrapper* wrapper = new Wrapper;
|
|
|
|
wrapper->rep_ = NewBloomFilterPolicy(bits_per_key);
|
2018-04-10 16:18:06 -07:00
|
|
|
wrapper->state_ = nullptr;
|
2012-04-17 08:36:46 -07:00
|
|
|
wrapper->destructor_ = &Wrapper::DoNothing;
|
|
|
|
return wrapper;
|
|
|
|
}
|
|
|
|
|
2011-08-05 20:40:49 +00:00
|
|
|
leveldb_readoptions_t* leveldb_readoptions_create() {
|
|
|
|
return new leveldb_readoptions_t;
|
|
|
|
}
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
void leveldb_readoptions_destroy(leveldb_readoptions_t* opt) { delete opt; }
|
2011-08-05 20:40:49 +00:00
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
void leveldb_readoptions_set_verify_checksums(leveldb_readoptions_t* opt,
|
2019-05-28 10:17:03 -07:00
|
|
|
uint8_t v) {
|
2011-08-05 20:40:49 +00:00
|
|
|
opt->rep.verify_checksums = v;
|
|
|
|
}
|
|
|
|
|
2019-05-28 10:17:03 -07:00
|
|
|
void leveldb_readoptions_set_fill_cache(leveldb_readoptions_t* opt, uint8_t v) {
|
2011-08-05 20:40:49 +00:00
|
|
|
opt->rep.fill_cache = v;
|
|
|
|
}
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
void leveldb_readoptions_set_snapshot(leveldb_readoptions_t* opt,
|
|
|
|
const leveldb_snapshot_t* snap) {
|
2018-04-10 16:18:06 -07:00
|
|
|
opt->rep.snapshot = (snap ? snap->rep : nullptr);
|
2011-08-05 20:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
leveldb_writeoptions_t* leveldb_writeoptions_create() {
|
|
|
|
return new leveldb_writeoptions_t;
|
|
|
|
}
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
void leveldb_writeoptions_destroy(leveldb_writeoptions_t* opt) { delete opt; }
|
2011-08-05 20:40:49 +00:00
|
|
|
|
2019-05-28 10:17:03 -07:00
|
|
|
void leveldb_writeoptions_set_sync(leveldb_writeoptions_t* opt, uint8_t v) {
|
2011-08-05 20:40:49 +00:00
|
|
|
opt->rep.sync = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
leveldb_cache_t* leveldb_cache_create_lru(size_t capacity) {
|
|
|
|
leveldb_cache_t* c = new leveldb_cache_t;
|
|
|
|
c->rep = NewLRUCache(capacity);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
void leveldb_cache_destroy(leveldb_cache_t* cache) {
|
|
|
|
delete cache->rep;
|
|
|
|
delete cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
leveldb_env_t* leveldb_create_default_env() {
|
|
|
|
leveldb_env_t* result = new leveldb_env_t;
|
|
|
|
result->rep = Env::Default();
|
|
|
|
result->is_default = true;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void leveldb_env_destroy(leveldb_env_t* env) {
|
|
|
|
if (!env->is_default) delete env->rep;
|
|
|
|
delete env;
|
|
|
|
}
|
|
|
|
|
2018-03-09 10:32:55 -08:00
|
|
|
char* leveldb_env_get_test_directory(leveldb_env_t* env) {
|
|
|
|
std::string result;
|
|
|
|
if (!env->rep->GetTestDirectory(&result).ok()) {
|
2018-04-10 16:18:06 -07:00
|
|
|
return nullptr;
|
2018-03-09 10:32:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
char* buffer = static_cast<char*>(malloc(result.size() + 1));
|
2020-04-29 22:31:41 +00:00
|
|
|
std::memcpy(buffer, result.data(), result.size());
|
2018-03-09 10:32:55 -08:00
|
|
|
buffer[result.size()] = '\0';
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
void leveldb_free(void* ptr) { free(ptr); }
|
2012-10-12 11:53:12 -07:00
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
int leveldb_major_version() { return kMajorVersion; }
|
2012-10-16 16:17:53 -07:00
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
int leveldb_minor_version() { return kMinorVersion; }
|
2012-10-16 16:17:53 -07:00
|
|
|
|
2011-08-05 20:40:49 +00:00
|
|
|
} // end extern "C"
|