作者: 韩晨旭 10225101440 李畅 10225102463
Você não pode selecionar mais de 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.

290 linhas
9.6 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. C bindings for leveldb. May be useful as a stable ABI that can be
  5. used by programs that keep leveldb in a shared library, or for
  6. a JNI api.
  7. Does not support:
  8. . getters for the option types
  9. . custom comparators that implement key shortening
  10. . custom iter, db, env, cache implementations using just the C bindings
  11. Some conventions:
  12. (1) We expose just opaque struct pointers and functions to clients.
  13. This allows us to change internal representations without having to
  14. recompile clients.
  15. (2) For simplicity, there is no equivalent to the Slice type. Instead,
  16. the caller has to pass the pointer and length as separate
  17. arguments.
  18. (3) Errors are represented by a null-terminated c string. NULL
  19. means no error. All operations that can raise an error are passed
  20. a "char** errptr" as the last argument. One of the following must
  21. be true on entry:
  22. *errptr == NULL
  23. *errptr points to a malloc()ed null-terminated error message
  24. (On Windows, *errptr must have been malloc()-ed by this library.)
  25. On success, a leveldb routine leaves *errptr unchanged.
  26. On failure, leveldb frees the old value of *errptr and
  27. set *errptr to a malloc()ed error message.
  28. (4) Bools have the type unsigned char (0 == false; rest == true)
  29. (5) All of the pointer arguments must be non-NULL.
  30. */
  31. #ifndef STORAGE_LEVELDB_INCLUDE_C_H_
  32. #define STORAGE_LEVELDB_INCLUDE_C_H_
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. #include <stdarg.h>
  37. #include <stddef.h>
  38. #include <stdint.h>
  39. /* Exported types */
  40. typedef struct leveldb_t leveldb_t;
  41. typedef struct leveldb_cache_t leveldb_cache_t;
  42. typedef struct leveldb_comparator_t leveldb_comparator_t;
  43. typedef struct leveldb_env_t leveldb_env_t;
  44. typedef struct leveldb_filelock_t leveldb_filelock_t;
  45. typedef struct leveldb_filterpolicy_t leveldb_filterpolicy_t;
  46. typedef struct leveldb_iterator_t leveldb_iterator_t;
  47. typedef struct leveldb_logger_t leveldb_logger_t;
  48. typedef struct leveldb_options_t leveldb_options_t;
  49. typedef struct leveldb_randomfile_t leveldb_randomfile_t;
  50. typedef struct leveldb_readoptions_t leveldb_readoptions_t;
  51. typedef struct leveldb_seqfile_t leveldb_seqfile_t;
  52. typedef struct leveldb_snapshot_t leveldb_snapshot_t;
  53. typedef struct leveldb_writablefile_t leveldb_writablefile_t;
  54. typedef struct leveldb_writebatch_t leveldb_writebatch_t;
  55. typedef struct leveldb_writeoptions_t leveldb_writeoptions_t;
  56. /* DB operations */
  57. extern leveldb_t* leveldb_open(
  58. const leveldb_options_t* options,
  59. const char* name,
  60. char** errptr);
  61. extern void leveldb_close(leveldb_t* db);
  62. extern void leveldb_put(
  63. leveldb_t* db,
  64. const leveldb_writeoptions_t* options,
  65. const char* key, size_t keylen,
  66. const char* val, size_t vallen,
  67. char** errptr);
  68. extern void leveldb_delete(
  69. leveldb_t* db,
  70. const leveldb_writeoptions_t* options,
  71. const char* key, size_t keylen,
  72. char** errptr);
  73. extern void leveldb_write(
  74. leveldb_t* db,
  75. const leveldb_writeoptions_t* options,
  76. leveldb_writebatch_t* batch,
  77. char** errptr);
  78. /* Returns NULL if not found. A malloc()ed array otherwise.
  79. Stores the length of the array in *vallen. */
  80. extern char* leveldb_get(
  81. leveldb_t* db,
  82. const leveldb_readoptions_t* options,
  83. const char* key, size_t keylen,
  84. size_t* vallen,
  85. char** errptr);
  86. extern leveldb_iterator_t* leveldb_create_iterator(
  87. leveldb_t* db,
  88. const leveldb_readoptions_t* options);
  89. extern const leveldb_snapshot_t* leveldb_create_snapshot(
  90. leveldb_t* db);
  91. extern void leveldb_release_snapshot(
  92. leveldb_t* db,
  93. const leveldb_snapshot_t* snapshot);
  94. /* Returns NULL if property name is unknown.
  95. Else returns a pointer to a malloc()-ed null-terminated value. */
  96. extern char* leveldb_property_value(
  97. leveldb_t* db,
  98. const char* propname);
  99. extern void leveldb_approximate_sizes(
  100. leveldb_t* db,
  101. int num_ranges,
  102. const char* const* range_start_key, const size_t* range_start_key_len,
  103. const char* const* range_limit_key, const size_t* range_limit_key_len,
  104. uint64_t* sizes);
  105. extern void leveldb_compact_range(
  106. leveldb_t* db,
  107. const char* start_key, size_t start_key_len,
  108. const char* limit_key, size_t limit_key_len);
  109. /* Management operations */
  110. extern void leveldb_destroy_db(
  111. const leveldb_options_t* options,
  112. const char* name,
  113. char** errptr);
  114. extern void leveldb_repair_db(
  115. const leveldb_options_t* options,
  116. const char* name,
  117. char** errptr);
  118. /* Iterator */
  119. extern void leveldb_iter_destroy(leveldb_iterator_t*);
  120. extern unsigned char leveldb_iter_valid(const leveldb_iterator_t*);
  121. extern void leveldb_iter_seek_to_first(leveldb_iterator_t*);
  122. extern void leveldb_iter_seek_to_last(leveldb_iterator_t*);
  123. extern void leveldb_iter_seek(leveldb_iterator_t*, const char* k, size_t klen);
  124. extern void leveldb_iter_next(leveldb_iterator_t*);
  125. extern void leveldb_iter_prev(leveldb_iterator_t*);
  126. extern const char* leveldb_iter_key(const leveldb_iterator_t*, size_t* klen);
  127. extern const char* leveldb_iter_value(const leveldb_iterator_t*, size_t* vlen);
  128. extern void leveldb_iter_get_error(const leveldb_iterator_t*, char** errptr);
  129. /* Write batch */
  130. extern leveldb_writebatch_t* leveldb_writebatch_create();
  131. extern void leveldb_writebatch_destroy(leveldb_writebatch_t*);
  132. extern void leveldb_writebatch_clear(leveldb_writebatch_t*);
  133. extern void leveldb_writebatch_put(
  134. leveldb_writebatch_t*,
  135. const char* key, size_t klen,
  136. const char* val, size_t vlen);
  137. extern void leveldb_writebatch_delete(
  138. leveldb_writebatch_t*,
  139. const char* key, size_t klen);
  140. extern void leveldb_writebatch_iterate(
  141. leveldb_writebatch_t*,
  142. void* state,
  143. void (*put)(void*, const char* k, size_t klen, const char* v, size_t vlen),
  144. void (*deleted)(void*, const char* k, size_t klen));
  145. /* Options */
  146. extern leveldb_options_t* leveldb_options_create();
  147. extern void leveldb_options_destroy(leveldb_options_t*);
  148. extern void leveldb_options_set_comparator(
  149. leveldb_options_t*,
  150. leveldb_comparator_t*);
  151. extern void leveldb_options_set_filter_policy(
  152. leveldb_options_t*,
  153. leveldb_filterpolicy_t*);
  154. extern void leveldb_options_set_create_if_missing(
  155. leveldb_options_t*, unsigned char);
  156. extern void leveldb_options_set_error_if_exists(
  157. leveldb_options_t*, unsigned char);
  158. extern void leveldb_options_set_paranoid_checks(
  159. leveldb_options_t*, unsigned char);
  160. extern void leveldb_options_set_env(leveldb_options_t*, leveldb_env_t*);
  161. extern void leveldb_options_set_info_log(leveldb_options_t*, leveldb_logger_t*);
  162. extern void leveldb_options_set_write_buffer_size(leveldb_options_t*, size_t);
  163. extern void leveldb_options_set_max_open_files(leveldb_options_t*, int);
  164. extern void leveldb_options_set_cache(leveldb_options_t*, leveldb_cache_t*);
  165. extern void leveldb_options_set_block_size(leveldb_options_t*, size_t);
  166. extern void leveldb_options_set_block_restart_interval(leveldb_options_t*, int);
  167. enum {
  168. leveldb_no_compression = 0,
  169. leveldb_snappy_compression = 1
  170. };
  171. extern void leveldb_options_set_compression(leveldb_options_t*, int);
  172. /* Comparator */
  173. extern leveldb_comparator_t* leveldb_comparator_create(
  174. void* state,
  175. void (*destructor)(void*),
  176. int (*compare)(
  177. void*,
  178. const char* a, size_t alen,
  179. const char* b, size_t blen),
  180. const char* (*name)(void*));
  181. extern void leveldb_comparator_destroy(leveldb_comparator_t*);
  182. /* Filter policy */
  183. extern leveldb_filterpolicy_t* leveldb_filterpolicy_create(
  184. void* state,
  185. void (*destructor)(void*),
  186. char* (*create_filter)(
  187. void*,
  188. const char* const* key_array, const size_t* key_length_array,
  189. int num_keys,
  190. size_t* filter_length),
  191. unsigned char (*key_may_match)(
  192. void*,
  193. const char* key, size_t length,
  194. const char* filter, size_t filter_length),
  195. const char* (*name)(void*));
  196. extern void leveldb_filterpolicy_destroy(leveldb_filterpolicy_t*);
  197. extern leveldb_filterpolicy_t* leveldb_filterpolicy_create_bloom(
  198. int bits_per_key);
  199. /* Read options */
  200. extern leveldb_readoptions_t* leveldb_readoptions_create();
  201. extern void leveldb_readoptions_destroy(leveldb_readoptions_t*);
  202. extern void leveldb_readoptions_set_verify_checksums(
  203. leveldb_readoptions_t*,
  204. unsigned char);
  205. extern void leveldb_readoptions_set_fill_cache(
  206. leveldb_readoptions_t*, unsigned char);
  207. extern void leveldb_readoptions_set_snapshot(
  208. leveldb_readoptions_t*,
  209. const leveldb_snapshot_t*);
  210. /* Write options */
  211. extern leveldb_writeoptions_t* leveldb_writeoptions_create();
  212. extern void leveldb_writeoptions_destroy(leveldb_writeoptions_t*);
  213. extern void leveldb_writeoptions_set_sync(
  214. leveldb_writeoptions_t*, unsigned char);
  215. /* Cache */
  216. extern leveldb_cache_t* leveldb_cache_create_lru(size_t capacity);
  217. extern void leveldb_cache_destroy(leveldb_cache_t* cache);
  218. /* Env */
  219. extern leveldb_env_t* leveldb_create_default_env();
  220. extern void leveldb_env_destroy(leveldb_env_t*);
  221. /* Utility */
  222. /* Calls free(ptr).
  223. REQUIRES: ptr was malloc()-ed and returned by one of the routines
  224. in this file. Note that in certain cases (typically on Windows), you
  225. may need to call this routine instead of free(ptr) to dispose of
  226. malloc()-ed memory returned by this library. */
  227. extern void leveldb_free(void* ptr);
  228. /* Return the major version number for this release. */
  229. extern int leveldb_major_version();
  230. /* Return the minor version number for this release. */
  231. extern int leveldb_minor_version();
  232. #ifdef __cplusplus
  233. } /* end extern "C" */
  234. #endif
  235. #endif /* STORAGE_LEVELDB_INCLUDE_C_H_ */