作者: 韩晨旭 10225101440 李畅 10225102463
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

267 lines
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. 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. #include "leveldb/export.h"
  40. /* Exported types */
  41. typedef struct leveldb_t leveldb_t;
  42. typedef struct leveldb_cache_t leveldb_cache_t;
  43. typedef struct leveldb_comparator_t leveldb_comparator_t;
  44. typedef struct leveldb_env_t leveldb_env_t;
  45. typedef struct leveldb_filelock_t leveldb_filelock_t;
  46. typedef struct leveldb_filterpolicy_t leveldb_filterpolicy_t;
  47. typedef struct leveldb_iterator_t leveldb_iterator_t;
  48. typedef struct leveldb_logger_t leveldb_logger_t;
  49. typedef struct leveldb_options_t leveldb_options_t;
  50. typedef struct leveldb_randomfile_t leveldb_randomfile_t;
  51. typedef struct leveldb_readoptions_t leveldb_readoptions_t;
  52. typedef struct leveldb_seqfile_t leveldb_seqfile_t;
  53. typedef struct leveldb_snapshot_t leveldb_snapshot_t;
  54. typedef struct leveldb_writablefile_t leveldb_writablefile_t;
  55. typedef struct leveldb_writebatch_t leveldb_writebatch_t;
  56. typedef struct leveldb_writeoptions_t leveldb_writeoptions_t;
  57. /* DB operations */
  58. LEVELDB_EXPORT leveldb_t* leveldb_open(const leveldb_options_t* options,
  59. const char* name, char** errptr);
  60. LEVELDB_EXPORT void leveldb_close(leveldb_t* db);
  61. LEVELDB_EXPORT void leveldb_put(leveldb_t* db,
  62. const leveldb_writeoptions_t* options,
  63. const char* key, size_t keylen, const char* val,
  64. size_t vallen, char** errptr);
  65. LEVELDB_EXPORT void leveldb_delete(leveldb_t* db,
  66. const leveldb_writeoptions_t* options,
  67. const char* key, size_t keylen,
  68. char** errptr);
  69. LEVELDB_EXPORT void leveldb_write(leveldb_t* db,
  70. const leveldb_writeoptions_t* options,
  71. leveldb_writebatch_t* batch, char** errptr);
  72. /* Returns NULL if not found. A malloc()ed array otherwise.
  73. Stores the length of the array in *vallen. */
  74. LEVELDB_EXPORT char* leveldb_get(leveldb_t* db,
  75. const leveldb_readoptions_t* options,
  76. const char* key, size_t keylen, size_t* vallen,
  77. char** errptr);
  78. LEVELDB_EXPORT leveldb_iterator_t* leveldb_create_iterator(
  79. leveldb_t* db, const leveldb_readoptions_t* options);
  80. LEVELDB_EXPORT const leveldb_snapshot_t* leveldb_create_snapshot(leveldb_t* db);
  81. LEVELDB_EXPORT void leveldb_release_snapshot(
  82. leveldb_t* db, const leveldb_snapshot_t* snapshot);
  83. /* Returns NULL if property name is unknown.
  84. Else returns a pointer to a malloc()-ed null-terminated value. */
  85. LEVELDB_EXPORT char* leveldb_property_value(leveldb_t* db,
  86. const char* propname);
  87. LEVELDB_EXPORT void leveldb_approximate_sizes(
  88. leveldb_t* db, int num_ranges, const char* const* range_start_key,
  89. const size_t* range_start_key_len, const char* const* range_limit_key,
  90. const size_t* range_limit_key_len, uint64_t* sizes);
  91. LEVELDB_EXPORT void leveldb_compact_range(leveldb_t* db, const char* start_key,
  92. size_t start_key_len,
  93. const char* limit_key,
  94. size_t limit_key_len);
  95. /* Management operations */
  96. LEVELDB_EXPORT void leveldb_destroy_db(const leveldb_options_t* options,
  97. const char* name, char** errptr);
  98. LEVELDB_EXPORT void leveldb_repair_db(const leveldb_options_t* options,
  99. const char* name, char** errptr);
  100. /* Iterator */
  101. LEVELDB_EXPORT void leveldb_iter_destroy(leveldb_iterator_t*);
  102. LEVELDB_EXPORT unsigned char leveldb_iter_valid(const leveldb_iterator_t*);
  103. LEVELDB_EXPORT void leveldb_iter_seek_to_first(leveldb_iterator_t*);
  104. LEVELDB_EXPORT void leveldb_iter_seek_to_last(leveldb_iterator_t*);
  105. LEVELDB_EXPORT void leveldb_iter_seek(leveldb_iterator_t*, const char* k,
  106. size_t klen);
  107. LEVELDB_EXPORT void leveldb_iter_next(leveldb_iterator_t*);
  108. LEVELDB_EXPORT void leveldb_iter_prev(leveldb_iterator_t*);
  109. LEVELDB_EXPORT const char* leveldb_iter_key(const leveldb_iterator_t*,
  110. size_t* klen);
  111. LEVELDB_EXPORT const char* leveldb_iter_value(const leveldb_iterator_t*,
  112. size_t* vlen);
  113. LEVELDB_EXPORT void leveldb_iter_get_error(const leveldb_iterator_t*,
  114. char** errptr);
  115. /* Write batch */
  116. LEVELDB_EXPORT leveldb_writebatch_t* leveldb_writebatch_create();
  117. LEVELDB_EXPORT void leveldb_writebatch_destroy(leveldb_writebatch_t*);
  118. LEVELDB_EXPORT void leveldb_writebatch_clear(leveldb_writebatch_t*);
  119. LEVELDB_EXPORT void leveldb_writebatch_put(leveldb_writebatch_t*,
  120. const char* key, size_t klen,
  121. const char* val, size_t vlen);
  122. LEVELDB_EXPORT void leveldb_writebatch_delete(leveldb_writebatch_t*,
  123. const char* key, size_t klen);
  124. LEVELDB_EXPORT void leveldb_writebatch_iterate(
  125. leveldb_writebatch_t*, void* state,
  126. void (*put)(void*, const char* k, size_t klen, const char* v, size_t vlen),
  127. void (*deleted)(void*, const char* k, size_t klen));
  128. /* Options */
  129. LEVELDB_EXPORT leveldb_options_t* leveldb_options_create();
  130. LEVELDB_EXPORT void leveldb_options_destroy(leveldb_options_t*);
  131. LEVELDB_EXPORT void leveldb_options_set_comparator(leveldb_options_t*,
  132. leveldb_comparator_t*);
  133. LEVELDB_EXPORT void leveldb_options_set_filter_policy(leveldb_options_t*,
  134. leveldb_filterpolicy_t*);
  135. LEVELDB_EXPORT void leveldb_options_set_create_if_missing(leveldb_options_t*,
  136. unsigned char);
  137. LEVELDB_EXPORT void leveldb_options_set_error_if_exists(leveldb_options_t*,
  138. unsigned char);
  139. LEVELDB_EXPORT void leveldb_options_set_paranoid_checks(leveldb_options_t*,
  140. unsigned char);
  141. LEVELDB_EXPORT void leveldb_options_set_env(leveldb_options_t*, leveldb_env_t*);
  142. LEVELDB_EXPORT void leveldb_options_set_info_log(leveldb_options_t*,
  143. leveldb_logger_t*);
  144. LEVELDB_EXPORT void leveldb_options_set_write_buffer_size(leveldb_options_t*,
  145. size_t);
  146. LEVELDB_EXPORT void leveldb_options_set_max_open_files(leveldb_options_t*, int);
  147. LEVELDB_EXPORT void leveldb_options_set_cache(leveldb_options_t*,
  148. leveldb_cache_t*);
  149. LEVELDB_EXPORT void leveldb_options_set_block_size(leveldb_options_t*, size_t);
  150. LEVELDB_EXPORT void leveldb_options_set_block_restart_interval(
  151. leveldb_options_t*, int);
  152. LEVELDB_EXPORT void leveldb_options_set_max_file_size(leveldb_options_t*,
  153. size_t);
  154. enum {
  155. leveldb_no_compression = 0,
  156. leveldb_snappy_compression = 1
  157. };
  158. LEVELDB_EXPORT void leveldb_options_set_compression(leveldb_options_t*, int);
  159. /* Comparator */
  160. LEVELDB_EXPORT leveldb_comparator_t* leveldb_comparator_create(
  161. void* state, void (*destructor)(void*),
  162. int (*compare)(void*, const char* a, size_t alen, const char* b,
  163. size_t blen),
  164. const char* (*name)(void*));
  165. LEVELDB_EXPORT void leveldb_comparator_destroy(leveldb_comparator_t*);
  166. /* Filter policy */
  167. LEVELDB_EXPORT leveldb_filterpolicy_t* leveldb_filterpolicy_create(
  168. void* state, void (*destructor)(void*),
  169. char* (*create_filter)(void*, const char* const* key_array,
  170. const size_t* key_length_array, int num_keys,
  171. size_t* filter_length),
  172. unsigned char (*key_may_match)(void*, const char* key, size_t length,
  173. const char* filter, size_t filter_length),
  174. const char* (*name)(void*));
  175. LEVELDB_EXPORT void leveldb_filterpolicy_destroy(leveldb_filterpolicy_t*);
  176. LEVELDB_EXPORT leveldb_filterpolicy_t* leveldb_filterpolicy_create_bloom(
  177. int bits_per_key);
  178. /* Read options */
  179. LEVELDB_EXPORT leveldb_readoptions_t* leveldb_readoptions_create();
  180. LEVELDB_EXPORT void leveldb_readoptions_destroy(leveldb_readoptions_t*);
  181. LEVELDB_EXPORT void leveldb_readoptions_set_verify_checksums(
  182. leveldb_readoptions_t*, unsigned char);
  183. LEVELDB_EXPORT void leveldb_readoptions_set_fill_cache(leveldb_readoptions_t*,
  184. unsigned char);
  185. LEVELDB_EXPORT void leveldb_readoptions_set_snapshot(leveldb_readoptions_t*,
  186. const leveldb_snapshot_t*);
  187. /* Write options */
  188. LEVELDB_EXPORT leveldb_writeoptions_t* leveldb_writeoptions_create();
  189. LEVELDB_EXPORT void leveldb_writeoptions_destroy(leveldb_writeoptions_t*);
  190. LEVELDB_EXPORT void leveldb_writeoptions_set_sync(leveldb_writeoptions_t*,
  191. unsigned char);
  192. /* Cache */
  193. LEVELDB_EXPORT leveldb_cache_t* leveldb_cache_create_lru(size_t capacity);
  194. LEVELDB_EXPORT void leveldb_cache_destroy(leveldb_cache_t* cache);
  195. /* Env */
  196. LEVELDB_EXPORT leveldb_env_t* leveldb_create_default_env();
  197. LEVELDB_EXPORT void leveldb_env_destroy(leveldb_env_t*);
  198. /* Utility */
  199. /* Calls free(ptr).
  200. REQUIRES: ptr was malloc()-ed and returned by one of the routines
  201. in this file. Note that in certain cases (typically on Windows), you
  202. may need to call this routine instead of free(ptr) to dispose of
  203. malloc()-ed memory returned by this library. */
  204. LEVELDB_EXPORT void leveldb_free(void* ptr);
  205. /* Return the major version number for this release. */
  206. LEVELDB_EXPORT int leveldb_major_version();
  207. /* Return the minor version number for this release. */
  208. LEVELDB_EXPORT int leveldb_minor_version();
  209. #ifdef __cplusplus
  210. } /* end extern "C" */
  211. #endif
  212. #endif /* STORAGE_LEVELDB_INCLUDE_C_H_ */