c.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 uint8_t (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. #include <stdarg.h>
  34. #include <stddef.h>
  35. #include <stdint.h>
  36. #include "leveldb/export.h"
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  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 uint8_t 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(void);
  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. const 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. LEVELDB_EXPORT void leveldb_writebatch_append(
  129. leveldb_writebatch_t* destination, const leveldb_writebatch_t* source);
  130. /* Options */
  131. LEVELDB_EXPORT leveldb_options_t* leveldb_options_create(void);
  132. LEVELDB_EXPORT void leveldb_options_destroy(leveldb_options_t*);
  133. LEVELDB_EXPORT void leveldb_options_set_comparator(leveldb_options_t*,
  134. leveldb_comparator_t*);
  135. LEVELDB_EXPORT void leveldb_options_set_filter_policy(leveldb_options_t*,
  136. leveldb_filterpolicy_t*);
  137. LEVELDB_EXPORT void leveldb_options_set_create_if_missing(leveldb_options_t*,
  138. uint8_t);
  139. LEVELDB_EXPORT void leveldb_options_set_error_if_exists(leveldb_options_t*,
  140. uint8_t);
  141. LEVELDB_EXPORT void leveldb_options_set_paranoid_checks(leveldb_options_t*,
  142. uint8_t);
  143. LEVELDB_EXPORT void leveldb_options_set_env(leveldb_options_t*, leveldb_env_t*);
  144. LEVELDB_EXPORT void leveldb_options_set_info_log(leveldb_options_t*,
  145. leveldb_logger_t*);
  146. LEVELDB_EXPORT void leveldb_options_set_write_buffer_size(leveldb_options_t*,
  147. size_t);
  148. LEVELDB_EXPORT void leveldb_options_set_max_open_files(leveldb_options_t*, int);
  149. LEVELDB_EXPORT void leveldb_options_set_cache(leveldb_options_t*,
  150. leveldb_cache_t*);
  151. LEVELDB_EXPORT void leveldb_options_set_block_size(leveldb_options_t*, size_t);
  152. LEVELDB_EXPORT void leveldb_options_set_block_restart_interval(
  153. leveldb_options_t*, int);
  154. LEVELDB_EXPORT void leveldb_options_set_max_file_size(leveldb_options_t*,
  155. size_t);
  156. enum { leveldb_no_compression = 0, leveldb_snappy_compression = 1 };
  157. LEVELDB_EXPORT void leveldb_options_set_compression(leveldb_options_t*, int);
  158. /* Comparator */
  159. LEVELDB_EXPORT leveldb_comparator_t* leveldb_comparator_create(
  160. void* state, void (*destructor)(void*),
  161. int (*compare)(void*, const char* a, size_t alen, const char* b,
  162. size_t blen),
  163. const char* (*name)(void*));
  164. LEVELDB_EXPORT void leveldb_comparator_destroy(leveldb_comparator_t*);
  165. /* Filter policy */
  166. LEVELDB_EXPORT leveldb_filterpolicy_t* leveldb_filterpolicy_create(
  167. void* state, void (*destructor)(void*),
  168. char* (*create_filter)(void*, const char* const* key_array,
  169. const size_t* key_length_array, int num_keys,
  170. size_t* filter_length),
  171. uint8_t (*key_may_match)(void*, const char* key, size_t length,
  172. const char* filter, size_t filter_length),
  173. const char* (*name)(void*));
  174. LEVELDB_EXPORT void leveldb_filterpolicy_destroy(leveldb_filterpolicy_t*);
  175. LEVELDB_EXPORT leveldb_filterpolicy_t* leveldb_filterpolicy_create_bloom(
  176. int bits_per_key);
  177. /* Read options */
  178. LEVELDB_EXPORT leveldb_readoptions_t* leveldb_readoptions_create(void);
  179. LEVELDB_EXPORT void leveldb_readoptions_destroy(leveldb_readoptions_t*);
  180. LEVELDB_EXPORT void leveldb_readoptions_set_verify_checksums(
  181. leveldb_readoptions_t*, uint8_t);
  182. LEVELDB_EXPORT void leveldb_readoptions_set_fill_cache(leveldb_readoptions_t*,
  183. uint8_t);
  184. LEVELDB_EXPORT void leveldb_readoptions_set_snapshot(leveldb_readoptions_t*,
  185. const leveldb_snapshot_t*);
  186. /* Write options */
  187. LEVELDB_EXPORT leveldb_writeoptions_t* leveldb_writeoptions_create(void);
  188. LEVELDB_EXPORT void leveldb_writeoptions_destroy(leveldb_writeoptions_t*);
  189. LEVELDB_EXPORT void leveldb_writeoptions_set_sync(leveldb_writeoptions_t*,
  190. uint8_t);
  191. /* Cache */
  192. LEVELDB_EXPORT leveldb_cache_t* leveldb_cache_create_lru(size_t capacity);
  193. LEVELDB_EXPORT void leveldb_cache_destroy(leveldb_cache_t* cache);
  194. /* Env */
  195. LEVELDB_EXPORT leveldb_env_t* leveldb_create_default_env(void);
  196. LEVELDB_EXPORT void leveldb_env_destroy(leveldb_env_t*);
  197. /* If not NULL, the returned buffer must be released using leveldb_free(). */
  198. LEVELDB_EXPORT char* leveldb_env_get_test_directory(leveldb_env_t*);
  199. /* Utility */
  200. /* Calls free(ptr).
  201. REQUIRES: ptr was malloc()-ed and returned by one of the routines
  202. in this file. Note that in certain cases (typically on Windows), you
  203. may need to call this routine instead of free(ptr) to dispose of
  204. malloc()-ed memory returned by this library. */
  205. LEVELDB_EXPORT void leveldb_free(void* ptr);
  206. /* Return the major version number for this release. */
  207. LEVELDB_EXPORT int leveldb_major_version(void);
  208. /* Return the minor version number for this release. */
  209. LEVELDB_EXPORT int leveldb_minor_version(void);
  210. #ifdef __cplusplus
  211. } /* end extern "C" */
  212. #endif
  213. #endif /* STORAGE_LEVELDB_INCLUDE_C_H_ */