提供基本的ttl测试用例
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

470 satır
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. return CopyString(tmp);
  176. } else {
  177. return NULL;
  178. }
  179. }
  180. void leveldb_approximate_sizes(
  181. leveldb_t* db,
  182. int num_ranges,
  183. const char* const* range_start_key, const size_t* range_start_key_len,
  184. const char* const* range_limit_key, const size_t* range_limit_key_len,
  185. uint64_t* sizes) {
  186. Range* ranges = new Range[num_ranges];
  187. for (int i = 0; i < num_ranges; i++) {
  188. ranges[i].start = Slice(range_start_key[i], range_start_key_len[i]);
  189. ranges[i].limit = Slice(range_limit_key[i], range_limit_key_len[i]);
  190. }
  191. db->rep->GetApproximateSizes(ranges, num_ranges, sizes);
  192. delete[] ranges;
  193. }
  194. void leveldb_destroy_db(
  195. const leveldb_options_t* options,
  196. const char* name,
  197. char** errptr) {
  198. SaveError(errptr, DestroyDB(name, options->rep));
  199. }
  200. void leveldb_repair_db(
  201. const leveldb_options_t* options,
  202. const char* name,
  203. char** errptr) {
  204. SaveError(errptr, RepairDB(name, options->rep));
  205. }
  206. void leveldb_iter_destroy(leveldb_iterator_t* iter) {
  207. delete iter->rep;
  208. delete iter;
  209. }
  210. unsigned char leveldb_iter_valid(const leveldb_iterator_t* iter) {
  211. return iter->rep->Valid();
  212. }
  213. void leveldb_iter_seek_to_first(leveldb_iterator_t* iter) {
  214. iter->rep->SeekToFirst();
  215. }
  216. void leveldb_iter_seek_to_last(leveldb_iterator_t* iter) {
  217. iter->rep->SeekToLast();
  218. }
  219. void leveldb_iter_seek(leveldb_iterator_t* iter, const char* k, size_t klen) {
  220. iter->rep->Seek(Slice(k, klen));
  221. }
  222. void leveldb_iter_next(leveldb_iterator_t* iter) {
  223. iter->rep->Next();
  224. }
  225. void leveldb_iter_prev(leveldb_iterator_t* iter) {
  226. iter->rep->Prev();
  227. }
  228. const char* leveldb_iter_key(const leveldb_iterator_t* iter, size_t* klen) {
  229. Slice s = iter->rep->key();
  230. *klen = s.size();
  231. return s.data();
  232. }
  233. const char* leveldb_iter_value(const leveldb_iterator_t* iter, size_t* vlen) {
  234. Slice s = iter->rep->value();
  235. *vlen = s.size();
  236. return s.data();
  237. }
  238. void leveldb_iter_get_error(const leveldb_iterator_t* iter, char** errptr) {
  239. SaveError(errptr, iter->rep->status());
  240. }
  241. leveldb_writebatch_t* leveldb_writebatch_create() {
  242. return new leveldb_writebatch_t;
  243. }
  244. void leveldb_writebatch_destroy(leveldb_writebatch_t* b) {
  245. delete b;
  246. }
  247. void leveldb_writebatch_clear(leveldb_writebatch_t* b) {
  248. b->rep.Clear();
  249. }
  250. void leveldb_writebatch_put(
  251. leveldb_writebatch_t* b,
  252. const char* key, size_t klen,
  253. const char* val, size_t vlen) {
  254. b->rep.Put(Slice(key, klen), Slice(val, vlen));
  255. }
  256. void leveldb_writebatch_delete(
  257. leveldb_writebatch_t* b,
  258. const char* key, size_t klen) {
  259. b->rep.Delete(Slice(key, klen));
  260. }
  261. void leveldb_writebatch_iterate(
  262. leveldb_writebatch_t* b,
  263. void* state,
  264. void (*put)(void*, const char* k, size_t klen, const char* v, size_t vlen),
  265. void (*deleted)(void*, const char* k, size_t klen)) {
  266. class H : public WriteBatch::Handler {
  267. public:
  268. void* state_;
  269. void (*put_)(void*, const char* k, size_t klen, const char* v, size_t vlen);
  270. void (*deleted_)(void*, const char* k, size_t klen);
  271. virtual void Put(const Slice& key, const Slice& value) {
  272. (*put_)(state_, key.data(), key.size(), value.data(), value.size());
  273. }
  274. virtual void Delete(const Slice& key) {
  275. (*deleted_)(state_, key.data(), key.size());
  276. }
  277. };
  278. H handler;
  279. handler.state_ = state;
  280. handler.put_ = put;
  281. handler.deleted_ = deleted;
  282. b->rep.Iterate(&handler);
  283. }
  284. leveldb_options_t* leveldb_options_create() {
  285. return new leveldb_options_t;
  286. }
  287. void leveldb_options_destroy(leveldb_options_t* options) {
  288. delete options;
  289. }
  290. void leveldb_options_set_comparator(
  291. leveldb_options_t* opt,
  292. leveldb_comparator_t* cmp) {
  293. opt->rep.comparator = cmp;
  294. }
  295. void leveldb_options_set_create_if_missing(
  296. leveldb_options_t* opt, unsigned char v) {
  297. opt->rep.create_if_missing = v;
  298. }
  299. void leveldb_options_set_error_if_exists(
  300. leveldb_options_t* opt, unsigned char v) {
  301. opt->rep.error_if_exists = v;
  302. }
  303. void leveldb_options_set_paranoid_checks(
  304. leveldb_options_t* opt, unsigned char v) {
  305. opt->rep.paranoid_checks = v;
  306. }
  307. void leveldb_options_set_env(leveldb_options_t* opt, leveldb_env_t* env) {
  308. opt->rep.env = (env ? env->rep : NULL);
  309. }
  310. void leveldb_options_set_info_log(leveldb_options_t* opt, leveldb_logger_t* l) {
  311. opt->rep.info_log = (l ? l->rep : NULL);
  312. }
  313. void leveldb_options_set_write_buffer_size(leveldb_options_t* opt, size_t s) {
  314. opt->rep.write_buffer_size = s;
  315. }
  316. void leveldb_options_set_max_open_files(leveldb_options_t* opt, int n) {
  317. opt->rep.max_open_files = n;
  318. }
  319. void leveldb_options_set_cache(leveldb_options_t* opt, leveldb_cache_t* c) {
  320. opt->rep.block_cache = c->rep;
  321. }
  322. void leveldb_options_set_block_size(leveldb_options_t* opt, size_t s) {
  323. opt->rep.block_size = s;
  324. }
  325. void leveldb_options_set_block_restart_interval(leveldb_options_t* opt, int n) {
  326. opt->rep.block_restart_interval = n;
  327. }
  328. void leveldb_options_set_compression(leveldb_options_t* opt, int t) {
  329. opt->rep.compression = static_cast<CompressionType>(t);
  330. }
  331. leveldb_comparator_t* leveldb_comparator_create(
  332. void* state,
  333. void (*destructor)(void*),
  334. int (*compare)(
  335. void*,
  336. const char* a, size_t alen,
  337. const char* b, size_t blen),
  338. const char* (*name)(void*)) {
  339. leveldb_comparator_t* result = new leveldb_comparator_t;
  340. result->state_ = state;
  341. result->destructor_ = destructor;
  342. result->compare_ = compare;
  343. result->name_ = name;
  344. return result;
  345. }
  346. void leveldb_comparator_destroy(leveldb_comparator_t* cmp) {
  347. delete cmp;
  348. }
  349. leveldb_readoptions_t* leveldb_readoptions_create() {
  350. return new leveldb_readoptions_t;
  351. }
  352. void leveldb_readoptions_destroy(leveldb_readoptions_t* opt) {
  353. delete opt;
  354. }
  355. void leveldb_readoptions_set_verify_checksums(
  356. leveldb_readoptions_t* opt,
  357. unsigned char v) {
  358. opt->rep.verify_checksums = v;
  359. }
  360. void leveldb_readoptions_set_fill_cache(
  361. leveldb_readoptions_t* opt, unsigned char v) {
  362. opt->rep.fill_cache = v;
  363. }
  364. void leveldb_readoptions_set_snapshot(
  365. leveldb_readoptions_t* opt,
  366. const leveldb_snapshot_t* snap) {
  367. opt->rep.snapshot = (snap ? snap->rep : NULL);
  368. }
  369. leveldb_writeoptions_t* leveldb_writeoptions_create() {
  370. return new leveldb_writeoptions_t;
  371. }
  372. void leveldb_writeoptions_destroy(leveldb_writeoptions_t* opt) {
  373. delete opt;
  374. }
  375. void leveldb_writeoptions_set_sync(
  376. leveldb_writeoptions_t* opt, unsigned char v) {
  377. opt->rep.sync = v;
  378. }
  379. leveldb_cache_t* leveldb_cache_create_lru(size_t capacity) {
  380. leveldb_cache_t* c = new leveldb_cache_t;
  381. c->rep = NewLRUCache(capacity);
  382. return c;
  383. }
  384. void leveldb_cache_destroy(leveldb_cache_t* cache) {
  385. delete cache->rep;
  386. delete cache;
  387. }
  388. leveldb_env_t* leveldb_create_default_env() {
  389. leveldb_env_t* result = new leveldb_env_t;
  390. result->rep = Env::Default();
  391. result->is_default = true;
  392. return result;
  393. }
  394. void leveldb_env_destroy(leveldb_env_t* env) {
  395. if (!env->is_default) delete env->rep;
  396. delete env;
  397. }
  398. } // end extern "C"