fdb_def.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * Copyright (c) 2020, Armink, <armink.ztl@gmail.com>
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief Public definition.
  9. */
  10. #ifndef _FDB_DEF_H_
  11. #define _FDB_DEF_H_
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. /* software version number */
  16. #define FDB_SW_VERSION "1.1.0"
  17. #define FDB_SW_VERSION_NUM 0x10100
  18. /* the KV max name length must less then it */
  19. #ifndef FDB_KV_NAME_MAX
  20. #define FDB_KV_NAME_MAX 64
  21. #endif
  22. /* the KV cache table size, it will improve KV search speed when using cache */
  23. #ifndef FDB_KV_CACHE_TABLE_SIZE
  24. #define FDB_KV_CACHE_TABLE_SIZE 64
  25. #endif
  26. /* the sector cache table size, it will improve KV save speed when using cache */
  27. #ifndef FDB_SECTOR_CACHE_TABLE_SIZE
  28. #define FDB_SECTOR_CACHE_TABLE_SIZE 4
  29. #endif
  30. #if (FDB_KV_CACHE_TABLE_SIZE > 0) && (FDB_SECTOR_CACHE_TABLE_SIZE > 0)
  31. #define FDB_KV_USING_CACHE
  32. #endif
  33. #if defined(FDB_USING_FILE_LIBC_MODE) || defined(FDB_USING_FILE_POSIX_MODE)
  34. #define FDB_USING_FILE_MODE
  35. #endif
  36. #ifndef FDB_WRITE_GRAN
  37. #define FDB_WRITE_GRAN 1
  38. #endif
  39. /* log function. default FDB_PRINT macro is printf() */
  40. #ifndef FDB_PRINT
  41. #define FDB_PRINT(...) printf(__VA_ARGS__)
  42. #endif
  43. #define FDB_LOG_PREFIX1() FDB_PRINT("[FlashDB]" FDB_LOG_TAG)
  44. #define FDB_LOG_PREFIX2() FDB_PRINT(" ")
  45. #define FDB_LOG_PREFIX() FDB_LOG_PREFIX1();FDB_LOG_PREFIX2()
  46. #ifdef FDB_DEBUG_ENABLE
  47. #define FDB_DEBUG(...) FDB_PRINT(__VA_ARGS__)
  48. #else
  49. #define FDB_DEBUG(...)
  50. #endif
  51. /* routine print function. Must be implement by user. */
  52. #define FDB_INFO(...) FDB_PRINT(__VA_ARGS__)
  53. /* assert for developer. */
  54. #if 0
  55. #define FDB_ASSERT(EXPR) \
  56. if (!(EXPR)) \
  57. { \
  58. FDB_DEBUG("(%s) has assert failed at %s.\n", #EXPR, __FUNCTION__); \
  59. while (1); \
  60. }
  61. #else
  62. #define FDB_ASSERT(EXPR)
  63. #endif
  64. #define FDB_KVDB_CTRL_SET_SEC_SIZE 0x00 /**< set sector size control command, this change MUST before database initialization */
  65. #define FDB_KVDB_CTRL_GET_SEC_SIZE 0x01 /**< get sector size control command */
  66. #define FDB_KVDB_CTRL_SET_LOCK 0x02 /**< set lock function control command */
  67. #define FDB_KVDB_CTRL_SET_UNLOCK 0x03 /**< set unlock function control command */
  68. #define FDB_KVDB_CTRL_SET_FILE_MODE 0x09 /**< set file mode control command, this change MUST before database initialization */
  69. #define FDB_KVDB_CTRL_SET_MAX_SIZE 0x0A /**< set database max size in file mode control command, this change MUST before database initialization */
  70. #define FDB_KVDB_CTRL_SET_NOT_FORMAT 0x0B /**< set database NOT format mode control command, this change MUST before database initialization */
  71. #define FDB_TSDB_CTRL_SET_SEC_SIZE 0x00 /**< set sector size control command, this change MUST before database initialization */
  72. #define FDB_TSDB_CTRL_GET_SEC_SIZE 0x01 /**< get sector size control command */
  73. #define FDB_TSDB_CTRL_SET_LOCK 0x02 /**< set lock function control command */
  74. #define FDB_TSDB_CTRL_SET_UNLOCK 0x03 /**< set unlock function control command */
  75. #define FDB_TSDB_CTRL_SET_ROLLOVER 0x04 /**< set rollover control command, this change MUST after database initialization */
  76. #define FDB_TSDB_CTRL_GET_ROLLOVER 0x05 /**< get rollover control command */
  77. #define FDB_TSDB_CTRL_GET_LAST_TIME 0x06 /**< get last save time control command */
  78. #define FDB_TSDB_CTRL_SET_FILE_MODE 0x09 /**< set file mode control command, this change MUST before database initialization */
  79. #define FDB_TSDB_CTRL_SET_MAX_SIZE 0x0A /**< set database max size in file mode control command, this change MUST before database initialization */
  80. #define FDB_TSDB_CTRL_SET_NOT_FORMAT 0x0B /**< set database NOT formatable mode control command, this change MUST before database initialization */
  81. #ifdef FDB_USING_TIMESTAMP_64BIT
  82. typedef int64_t fdb_time_t;
  83. #else
  84. typedef int32_t fdb_time_t;
  85. #endif /* FDB_USING_TIMESTAMP_64BIT */
  86. typedef fdb_time_t (*fdb_get_time)(void);
  87. struct fdb_default_kv_node {
  88. char *key;
  89. void *value;
  90. size_t value_len;
  91. };
  92. struct fdb_default_kv {
  93. struct fdb_default_kv_node *kvs;
  94. size_t num;
  95. };
  96. /* error code */
  97. typedef enum {
  98. FDB_NO_ERR,
  99. FDB_ERASE_ERR,
  100. FDB_READ_ERR,
  101. FDB_WRITE_ERR,
  102. FDB_PART_NOT_FOUND,
  103. FDB_KV_NAME_ERR,
  104. FDB_KV_NAME_EXIST,
  105. FDB_SAVED_FULL,
  106. FDB_INIT_FAILED,
  107. } fdb_err_t;
  108. enum fdb_kv_status {
  109. FDB_KV_UNUSED,
  110. FDB_KV_PRE_WRITE,
  111. FDB_KV_WRITE,
  112. FDB_KV_PRE_DELETE,
  113. FDB_KV_DELETED,
  114. FDB_KV_ERR_HDR,
  115. #define FDB_KV_STATUS_NUM 6
  116. };
  117. typedef enum fdb_kv_status fdb_kv_status_t;
  118. enum fdb_tsl_status {
  119. FDB_TSL_UNUSED,
  120. FDB_TSL_PRE_WRITE,
  121. FDB_TSL_WRITE,
  122. FDB_TSL_USER_STATUS1,
  123. FDB_TSL_DELETED,
  124. FDB_TSL_USER_STATUS2,
  125. #define FDB_TSL_STATUS_NUM 6
  126. };
  127. typedef enum fdb_tsl_status fdb_tsl_status_t;
  128. /* key-value node object */
  129. struct fdb_kv {
  130. fdb_kv_status_t status; /**< node status, @see fdb_kv_status_t */
  131. bool crc_is_ok; /**< node CRC32 check is OK */
  132. uint8_t name_len; /**< name length */
  133. uint32_t magic; /**< magic word(`K`, `V`, `4`, `0`) */
  134. uint32_t len; /**< node total length (header + name + value), must align by FDB_WRITE_GRAN */
  135. uint32_t value_len; /**< value length */
  136. char name[FDB_KV_NAME_MAX]; /**< name */
  137. struct {
  138. uint32_t start; /**< node start address */
  139. uint32_t value; /**< value start address */
  140. } addr;
  141. };
  142. typedef struct fdb_kv *fdb_kv_t;
  143. struct fdb_kv_iterator {
  144. struct fdb_kv curr_kv; /**< Current KV we get from the iterator */
  145. uint32_t iterated_cnt; /**< How many KVs have we iterated already */
  146. size_t iterated_obj_bytes; /**< Total storage size of KVs we have iterated. */
  147. size_t iterated_value_bytes; /**< Total value size of KVs we have iterated. */
  148. uint32_t sector_addr; /**< Current sector address we're iterating. DO NOT touch it. */
  149. };
  150. typedef struct fdb_kv_iterator *fdb_kv_iterator_t;
  151. /* time series log node object */
  152. struct fdb_tsl {
  153. fdb_tsl_status_t status; /**< node status, @see fdb_log_status_t */
  154. fdb_time_t time; /**< node timestamp */
  155. uint32_t log_len; /**< log length, must align by FDB_WRITE_GRAN */
  156. struct {
  157. uint32_t index; /**< node index address */
  158. uint32_t log; /**< log data address */
  159. } addr;
  160. };
  161. typedef struct fdb_tsl *fdb_tsl_t;
  162. typedef bool (*fdb_tsl_cb)(fdb_tsl_t tsl, void *arg);
  163. typedef enum {
  164. FDB_DB_TYPE_KV,
  165. FDB_DB_TYPE_TS,
  166. } fdb_db_type;
  167. /* the flash sector store status */
  168. enum fdb_sector_store_status {
  169. FDB_SECTOR_STORE_UNUSED,
  170. FDB_SECTOR_STORE_EMPTY,
  171. FDB_SECTOR_STORE_USING,
  172. FDB_SECTOR_STORE_FULL,
  173. #define FDB_SECTOR_STORE_STATUS_NUM 4
  174. };
  175. typedef enum fdb_sector_store_status fdb_sector_store_status_t;
  176. /* the flash sector dirty status */
  177. enum fdb_sector_dirty_status {
  178. FDB_SECTOR_DIRTY_UNUSED,
  179. FDB_SECTOR_DIRTY_FALSE,
  180. FDB_SECTOR_DIRTY_TRUE,
  181. FDB_SECTOR_DIRTY_GC,
  182. #define FDB_SECTOR_DIRTY_STATUS_NUM 4
  183. };
  184. typedef enum fdb_sector_dirty_status fdb_sector_dirty_status_t;
  185. /* KVDB section information */
  186. struct kvdb_sec_info {
  187. bool check_ok; /**< sector header check is OK */
  188. struct {
  189. fdb_sector_store_status_t store; /**< sector store status @see fdb_sector_store_status_t */
  190. fdb_sector_dirty_status_t dirty; /**< sector dirty status @see sector_dirty_status_t */
  191. } status;
  192. uint32_t addr; /**< sector start address */
  193. uint32_t magic; /**< magic word(`E`, `F`, `4`, `0`) */
  194. uint32_t combined; /**< the combined next sector number, 0xFFFFFFFF: not combined */
  195. size_t remain; /**< remain size */
  196. uint32_t empty_kv; /**< the next empty KV node start address */
  197. };
  198. typedef struct kvdb_sec_info *kv_sec_info_t;
  199. /* TSDB section information */
  200. struct tsdb_sec_info {
  201. bool check_ok; /**< sector header check is OK */
  202. fdb_sector_store_status_t status; /**< sector store status @see fdb_sector_store_status_t */
  203. uint32_t addr; /**< sector start address */
  204. uint32_t magic; /**< magic word(`T`, `S`, `L`, `0`) */
  205. fdb_time_t start_time; /**< the first start node's timestamp, 0xFFFFFFFF: unused */
  206. fdb_time_t end_time; /**< the last end node's timestamp, 0xFFFFFFFF: unused */
  207. uint32_t end_idx; /**< the last end node's index, 0xFFFFFFFF: unused */
  208. fdb_tsl_status_t end_info_stat[2]; /**< the last end node's info status */
  209. size_t remain; /**< remain size */
  210. uint32_t empty_idx; /**< the next empty node index address */
  211. uint32_t empty_data; /**< the next empty node's data end address */
  212. };
  213. typedef struct tsdb_sec_info *tsdb_sec_info_t;
  214. struct kv_cache_node {
  215. uint16_t name_crc; /**< KV name's CRC32 low 16bit value */
  216. uint16_t active; /**< KV node access active degree */
  217. uint32_t addr; /**< KV node address */
  218. };
  219. typedef struct kv_cache_node *kv_cache_node_t;
  220. struct sector_cache_node {
  221. uint32_t addr; /**< sector start address */
  222. uint32_t empty_addr; /**< sector empty address */
  223. };
  224. typedef struct sector_cache_node *sector_cache_node_t;
  225. /* database structure */
  226. typedef struct fdb_db *fdb_db_t;
  227. struct fdb_db {
  228. const char *name; /**< database name */
  229. fdb_db_type type; /**< database type */
  230. union {
  231. #ifdef FDB_USING_FAL_MODE
  232. const struct fal_partition *part; /**< flash partition for saving database */
  233. #endif
  234. #ifdef FDB_USING_FILE_MODE
  235. const char *dir; /**< directory path for saving database */
  236. #endif
  237. } storage;
  238. uint32_t sec_size; /**< flash section size. It's a multiple of block size */
  239. uint32_t max_size; /**< database max size. It's a multiple of section size */
  240. bool init_ok; /**< initialized successfully */
  241. bool file_mode; /**< is file mode, default is false */
  242. bool not_formatable; /**< is can NOT be formated mode, default is false */
  243. #ifdef FDB_USING_FILE_MODE
  244. #if defined(FDB_USING_FILE_POSIX_MODE)
  245. int cur_file; /**< current file object */
  246. #elif defined(FDB_USING_FILE_LIBC_MODE)
  247. FILE *cur_file; /**< current file object */
  248. #endif
  249. uint32_t cur_sec; /**< current operate sector address */
  250. #endif
  251. void (*lock)(fdb_db_t db); /**< lock the database operate */
  252. void (*unlock)(fdb_db_t db); /**< unlock the database operate */
  253. void *user_data;
  254. };
  255. /* KVDB structure */
  256. struct fdb_kvdb {
  257. struct fdb_db parent; /**< inherit from fdb_db */
  258. struct fdb_default_kv default_kvs; /**< default KV */
  259. bool gc_request; /**< request a GC check */
  260. bool in_recovery_check; /**< is in recovery check status when first reboot */
  261. struct fdb_kv cur_kv;
  262. struct kvdb_sec_info cur_sector;
  263. bool last_is_complete_del;
  264. #ifdef FDB_KV_USING_CACHE
  265. /* KV cache table */
  266. struct kv_cache_node kv_cache_table[FDB_KV_CACHE_TABLE_SIZE];
  267. /* sector cache table, it caching the sector info which status is current using */
  268. struct sector_cache_node sector_cache_table[FDB_SECTOR_CACHE_TABLE_SIZE];
  269. #endif /* FDB_KV_USING_CACHE */
  270. #ifdef FDB_KV_AUTO_UPDATE
  271. uint32_t ver_num; /**< setting version number for update */
  272. #endif
  273. void *user_data;
  274. };
  275. typedef struct fdb_kvdb *fdb_kvdb_t;
  276. /* TSDB structure */
  277. struct fdb_tsdb {
  278. struct fdb_db parent; /**< inherit from fdb_db */
  279. struct tsdb_sec_info cur_sec; /**< current using sector */
  280. fdb_time_t last_time; /**< last TSL timestamp */
  281. fdb_get_time get_time; /**< the current timestamp get function */
  282. size_t max_len; /**< the maximum length of each log */
  283. uint32_t oldest_addr; /**< the oldest sector start address */
  284. bool rollover; /**< the oldest data will rollover by newest data, default is true */
  285. void *user_data;
  286. };
  287. typedef struct fdb_tsdb *fdb_tsdb_t;
  288. /* blob structure */
  289. struct fdb_blob {
  290. void *buf; /**< blob data buffer */
  291. size_t size; /**< blob data buffer size */
  292. struct {
  293. uint32_t meta_addr; /**< saved KV or TSL index address */
  294. uint32_t addr; /**< blob data saved address */
  295. size_t len; /**< blob data saved length */
  296. } saved;
  297. };
  298. typedef struct fdb_blob *fdb_blob_t;
  299. #ifdef __cplusplus
  300. }
  301. #endif
  302. #endif /* _FDB_DEF_H_ */