10225501448 李度 10225101546 陈胤遒 10215501422 高宇菲
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.

471 linhas
12 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 <unistd.h>
  7. #include "leveldb/cache.h"
  8. #include "leveldb/comparator.h"
  9. #include "leveldb/db.h"
  10. #include "leveldb/env.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::Iterator;
  22. using leveldb::Logger;
  23. using leveldb::NewLRUCache;
  24. using leveldb::Options;
  25. using leveldb::RandomAccessFile;
  26. using leveldb::Range;
  27. using leveldb::ReadOptions;
  28. using leveldb::SequentialFile;
  29. using leveldb::Slice;
  30. using leveldb::Snapshot;
  31. using leveldb::Status;
  32. using leveldb::WritableFile;
  33. using leveldb::WriteBatch;
  34. using leveldb::WriteOptions;
  35. extern "C" {
  36. struct leveldb_t { DB* rep; };
  37. struct leveldb_iterator_t { Iterator* rep; };
  38. struct leveldb_writebatch_t { WriteBatch rep; };
  39. struct leveldb_snapshot_t { const Snapshot* rep; };
  40. struct leveldb_readoptions_t { ReadOptions rep; };
  41. struct leveldb_writeoptions_t { WriteOptions rep; };
  42. struct leveldb_options_t { Options rep; };
  43. struct leveldb_cache_t { Cache* rep; };
  44. struct leveldb_seqfile_t { SequentialFile* rep; };
  45. struct leveldb_randomfile_t { RandomAccessFile* rep; };
  46. struct leveldb_writablefile_t { WritableFile* rep; };
  47. struct leveldb_logger_t { Logger* rep; };
  48. struct leveldb_filelock_t { FileLock* rep; };
  49. struct leveldb_comparator_t : public Comparator {
  50. void* state_;
  51. void (*destructor_)(void*);
  52. int (*compare_)(
  53. void*,
  54. const char* a, size_t alen,
  55. const char* b, size_t blen);
  56. const char* (*name_)(void*);
  57. virtual ~leveldb_comparator_t() {
  58. (*destructor_)(state_);
  59. }
  60. virtual int Compare(const Slice& a, const Slice& b) const {
  61. return (*compare_)(state_, a.data(), a.size(), b.data(), b.size());
  62. }
  63. virtual const char* Name() const {
  64. return (*name_)(state_);
  65. }
  66. // No-ops since the C binding does not support key shortening methods.
  67. virtual void FindShortestSeparator(std::string*, const Slice&) const { }
  68. virtual void FindShortSuccessor(std::string* key) const { }
  69. };
  70. struct leveldb_env_t {
  71. Env* rep;
  72. bool is_default;
  73. };
  74. static bool SaveError(char** errptr, const Status& s) {
  75. assert(errptr != NULL);
  76. if (s.ok()) {
  77. return false;
  78. } else if (*errptr == NULL) {
  79. *errptr = strdup(s.ToString().c_str());
  80. } else {
  81. // TODO(sanjay): Merge with existing error?
  82. free(*errptr);
  83. *errptr = strdup(s.ToString().c_str());
  84. }
  85. return true;
  86. }
  87. static char* CopyString(const std::string& str) {
  88. char* result = reinterpret_cast<char*>(malloc(sizeof(char) * str.size()));
  89. memcpy(result, str.data(), sizeof(char) * str.size());
  90. return result;
  91. }
  92. leveldb_t* leveldb_open(
  93. const leveldb_options_t* options,
  94. const char* name,
  95. char** errptr) {
  96. DB* db;
  97. if (SaveError(errptr, DB::Open(options->rep, std::string(name), &db))) {
  98. return NULL;
  99. }
  100. leveldb_t* result = new leveldb_t;
  101. result->rep = db;
  102. return result;
  103. }
  104. void leveldb_close(leveldb_t* db) {
  105. delete db->rep;
  106. delete db;
  107. }
  108. void leveldb_put(
  109. leveldb_t* db,
  110. const leveldb_writeoptions_t* options,
  111. const char* key, size_t keylen,
  112. const char* val, size_t vallen,
  113. char** errptr) {
  114. SaveError(errptr,
  115. db->rep->Put(options->rep, Slice(key, keylen), Slice(val, vallen)));
  116. }
  117. void leveldb_delete(
  118. leveldb_t* db,
  119. const leveldb_writeoptions_t* options,
  120. const char* key, size_t keylen,
  121. char** errptr) {
  122. SaveError(errptr, db->rep->Delete(options->rep, Slice(key, keylen)));
  123. }
  124. void leveldb_write(
  125. leveldb_t* db,
  126. const leveldb_writeoptions_t* options,
  127. leveldb_writebatch_t* batch,
  128. char** errptr) {
  129. SaveError(errptr, db->rep->Write(options->rep, &batch->rep));
  130. }
  131. char* leveldb_get(
  132. leveldb_t* db,
  133. const leveldb_readoptions_t* options,
  134. const char* key, size_t keylen,
  135. size_t* vallen,
  136. char** errptr) {
  137. char* result = NULL;
  138. std::string tmp;
  139. Status s = db->rep->Get(options->rep, Slice(key, keylen), &tmp);
  140. if (s.ok()) {
  141. *vallen = tmp.size();
  142. result = CopyString(tmp);
  143. } else {
  144. *vallen = 0;
  145. if (!s.IsNotFound()) {
  146. SaveError(errptr, s);
  147. }
  148. }
  149. return result;
  150. }
  151. leveldb_iterator_t* leveldb_create_iterator(
  152. leveldb_t* db,
  153. const leveldb_readoptions_t* options) {
  154. leveldb_iterator_t* result = new leveldb_iterator_t;
  155. result->rep = db->rep->NewIterator(options->rep);
  156. return result;
  157. }
  158. const leveldb_snapshot_t* leveldb_create_snapshot(
  159. leveldb_t* db) {
  160. leveldb_snapshot_t* result = new leveldb_snapshot_t;
  161. result->rep = db->rep->GetSnapshot();
  162. return result;
  163. }
  164. void leveldb_release_snapshot(
  165. leveldb_t* db,
  166. const leveldb_snapshot_t* snapshot) {
  167. db->rep->ReleaseSnapshot(snapshot->rep);
  168. delete snapshot;
  169. }
  170. char* leveldb_property_value(
  171. leveldb_t* db,
  172. const char* propname) {
  173. std::string tmp;
  174. if (db->rep->GetProperty(Slice(propname), &tmp)) {
  175. // We use strdup() since we expect human readable output.
  176. return strdup(tmp.c_str());
  177. } else {
  178. return NULL;
  179. }
  180. }
  181. void leveldb_approximate_sizes(
  182. leveldb_t* db,
  183. int num_ranges,
  184. const char* const* range_start_key, const size_t* range_start_key_len,
  185. const char* const* range_limit_key, const size_t* range_limit_key_len,
  186. uint64_t* sizes) {
  187. Range* ranges = new Range[num_ranges];
  188. for (int i = 0; i < num_ranges; i++) {
  189. ranges[i].start = Slice(range_start_key[i], range_start_key_len[i]);
  190. ranges[i].limit = Slice(range_limit_key[i], range_limit_key_len[i]);
  191. }
  192. db->rep->GetApproximateSizes(ranges, num_ranges, sizes);
  193. delete[] ranges;
  194. }
  195. void leveldb_destroy_db(
  196. const leveldb_options_t* options,
  197. const char* name,
  198. char** errptr) {
  199. SaveError(errptr, DestroyDB(name, options->rep));
  200. }
  201. void leveldb_repair_db(
  202. const leveldb_options_t* options,
  203. const char* name,
  204. char** errptr) {
  205. SaveError(errptr, RepairDB(name, options->rep));
  206. }
  207. void leveldb_iter_destroy(leveldb_iterator_t* iter) {
  208. delete iter->rep;
  209. delete iter;
  210. }
  211. unsigned char leveldb_iter_valid(const leveldb_iterator_t* iter) {
  212. return iter->rep->Valid();
  213. }
  214. void leveldb_iter_seek_to_first(leveldb_iterator_t* iter) {
  215. iter->rep->SeekToFirst();
  216. }
  217. void leveldb_iter_seek_to_last(leveldb_iterator_t* iter) {
  218. iter->rep->SeekToLast();
  219. }
  220. void leveldb_iter_seek(leveldb_iterator_t* iter, const char* k, size_t klen) {
  221. iter->rep->Seek(Slice(k, klen));
  222. }
  223. void leveldb_iter_next(leveldb_iterator_t* iter) {
  224. iter->rep->Next();
  225. }
  226. void leveldb_iter_prev(leveldb_iterator_t* iter) {
  227. iter->rep->Prev();
  228. }
  229. const char* leveldb_iter_key(const leveldb_iterator_t* iter, size_t* klen) {
  230. Slice s = iter->rep->key();
  231. *klen = s.size();
  232. return s.data();
  233. }
  234. const char* leveldb_iter_value(const leveldb_iterator_t* iter, size_t* vlen) {
  235. Slice s = iter->rep->value();
  236. *vlen = s.size();
  237. return s.data();
  238. }
  239. void leveldb_iter_get_error(const leveldb_iterator_t* iter, char** errptr) {
  240. SaveError(errptr, iter->rep->status());
  241. }
  242. leveldb_writebatch_t* leveldb_writebatch_create() {
  243. return new leveldb_writebatch_t;
  244. }
  245. void leveldb_writebatch_destroy(leveldb_writebatch_t* b) {
  246. delete b;
  247. }
  248. void leveldb_writebatch_clear(leveldb_writebatch_t* b) {
  249. b->rep.Clear();
  250. }
  251. void leveldb_writebatch_put(
  252. leveldb_writebatch_t* b,
  253. const char* key, size_t klen,
  254. const char* val, size_t vlen) {
  255. b->rep.Put(Slice(key, klen), Slice(val, vlen));
  256. }
  257. void leveldb_writebatch_delete(
  258. leveldb_writebatch_t* b,
  259. const char* key, size_t klen) {
  260. b->rep.Delete(Slice(key, klen));
  261. }
  262. void leveldb_writebatch_iterate(
  263. leveldb_writebatch_t* b,
  264. void* state,
  265. void (*put)(void*, const char* k, size_t klen, const char* v, size_t vlen),
  266. void (*deleted)(void*, const char* k, size_t klen)) {
  267. class H : public WriteBatch::Handler {
  268. public:
  269. void* state_;
  270. void (*put_)(void*, const char* k, size_t klen, const char* v, size_t vlen);
  271. void (*deleted_)(void*, const char* k, size_t klen);
  272. virtual void Put(const Slice& key, const Slice& value) {
  273. (*put_)(state_, key.data(), key.size(), value.data(), value.size());
  274. }
  275. virtual void Delete(const Slice& key) {
  276. (*deleted_)(state_, key.data(), key.size());
  277. }
  278. };
  279. H handler;
  280. handler.state_ = state;
  281. handler.put_ = put;
  282. handler.deleted_ = deleted;
  283. b->rep.Iterate(&handler);
  284. }
  285. leveldb_options_t* leveldb_options_create() {
  286. return new leveldb_options_t;
  287. }
  288. void leveldb_options_destroy(leveldb_options_t* options) {
  289. delete options;
  290. }
  291. void leveldb_options_set_comparator(
  292. leveldb_options_t* opt,
  293. leveldb_comparator_t* cmp) {
  294. opt->rep.comparator = cmp;
  295. }
  296. void leveldb_options_set_create_if_missing(
  297. leveldb_options_t* opt, unsigned char v) {
  298. opt->rep.create_if_missing = v;
  299. }
  300. void leveldb_options_set_error_if_exists(
  301. leveldb_options_t* opt, unsigned char v) {
  302. opt->rep.error_if_exists = v;
  303. }
  304. void leveldb_options_set_paranoid_checks(
  305. leveldb_options_t* opt, unsigned char v) {
  306. opt->rep.paranoid_checks = v;
  307. }
  308. void leveldb_options_set_env(leveldb_options_t* opt, leveldb_env_t* env) {
  309. opt->rep.env = (env ? env->rep : NULL);
  310. }
  311. void leveldb_options_set_info_log(leveldb_options_t* opt, leveldb_logger_t* l) {
  312. opt->rep.info_log = (l ? l->rep : NULL);
  313. }
  314. void leveldb_options_set_write_buffer_size(leveldb_options_t* opt, size_t s) {
  315. opt->rep.write_buffer_size = s;
  316. }
  317. void leveldb_options_set_max_open_files(leveldb_options_t* opt, int n) {
  318. opt->rep.max_open_files = n;
  319. }
  320. void leveldb_options_set_cache(leveldb_options_t* opt, leveldb_cache_t* c) {
  321. opt->rep.block_cache = c->rep;
  322. }
  323. void leveldb_options_set_block_size(leveldb_options_t* opt, size_t s) {
  324. opt->rep.block_size = s;
  325. }
  326. void leveldb_options_set_block_restart_interval(leveldb_options_t* opt, int n) {
  327. opt->rep.block_restart_interval = n;
  328. }
  329. void leveldb_options_set_compression(leveldb_options_t* opt, int t) {
  330. opt->rep.compression = static_cast<CompressionType>(t);
  331. }
  332. leveldb_comparator_t* leveldb_comparator_create(
  333. void* state,
  334. void (*destructor)(void*),
  335. int (*compare)(
  336. void*,
  337. const char* a, size_t alen,
  338. const char* b, size_t blen),
  339. const char* (*name)(void*)) {
  340. leveldb_comparator_t* result = new leveldb_comparator_t;
  341. result->state_ = state;
  342. result->destructor_ = destructor;
  343. result->compare_ = compare;
  344. result->name_ = name;
  345. return result;
  346. }
  347. void leveldb_comparator_destroy(leveldb_comparator_t* cmp) {
  348. delete cmp;
  349. }
  350. leveldb_readoptions_t* leveldb_readoptions_create() {
  351. return new leveldb_readoptions_t;
  352. }
  353. void leveldb_readoptions_destroy(leveldb_readoptions_t* opt) {
  354. delete opt;
  355. }
  356. void leveldb_readoptions_set_verify_checksums(
  357. leveldb_readoptions_t* opt,
  358. unsigned char v) {
  359. opt->rep.verify_checksums = v;
  360. }
  361. void leveldb_readoptions_set_fill_cache(
  362. leveldb_readoptions_t* opt, unsigned char v) {
  363. opt->rep.fill_cache = v;
  364. }
  365. void leveldb_readoptions_set_snapshot(
  366. leveldb_readoptions_t* opt,
  367. const leveldb_snapshot_t* snap) {
  368. opt->rep.snapshot = (snap ? snap->rep : NULL);
  369. }
  370. leveldb_writeoptions_t* leveldb_writeoptions_create() {
  371. return new leveldb_writeoptions_t;
  372. }
  373. void leveldb_writeoptions_destroy(leveldb_writeoptions_t* opt) {
  374. delete opt;
  375. }
  376. void leveldb_writeoptions_set_sync(
  377. leveldb_writeoptions_t* opt, unsigned char v) {
  378. opt->rep.sync = v;
  379. }
  380. leveldb_cache_t* leveldb_cache_create_lru(size_t capacity) {
  381. leveldb_cache_t* c = new leveldb_cache_t;
  382. c->rep = NewLRUCache(capacity);
  383. return c;
  384. }
  385. void leveldb_cache_destroy(leveldb_cache_t* cache) {
  386. delete cache->rep;
  387. delete cache;
  388. }
  389. leveldb_env_t* leveldb_create_default_env() {
  390. leveldb_env_t* result = new leveldb_env_t;
  391. result->rep = Env::Default();
  392. result->is_default = true;
  393. return result;
  394. }
  395. void leveldb_env_destroy(leveldb_env_t* env) {
  396. if (!env->is_default) delete env->rep;
  397. delete env;
  398. }
  399. } // end extern "C"