作者: 韩晨旭 10225101440 李畅 10225102463
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

610 linhas
16 KiB

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