fdb_def.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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.0.0"
  17. #define FDB_SW_VERSION_NUM 0x10000
  18. /* the KV max name length must less then it */
  19. #ifndef FDB_KV_NAME_MAX
  20. #define FDB_KV_NAME_MAX 32
  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 16
  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. /* log function. default FDB_PRINT macro is printf() */
  34. #ifndef FDB_PRINT
  35. #define FDB_PRINT(...) printf(__VA_ARGS__)
  36. #endif
  37. #define FDB_LOG_PREFIX1() FDB_PRINT("[FlashDB]"FDB_LOG_TAG)
  38. #define FDB_LOG_PREFIX2() FDB_PRINT(" ")
  39. #define FDB_LOG_PREFIX() FDB_LOG_PREFIX1();FDB_LOG_PREFIX2()
  40. #ifdef FDB_DEBUG_ENABLE
  41. #define FDB_DEBUG(...) FDB_LOG_PREFIX();FDB_PRINT("(%s:%d) ", __FILE__, __LINE__);FDB_PRINT(__VA_ARGS__)
  42. #else
  43. #define FDB_DEBUG(...)
  44. #endif
  45. /* routine print function. Must be implement by user. */
  46. #define FDB_INFO(...) FDB_LOG_PREFIX();FDB_PRINT(__VA_ARGS__)
  47. /* assert for developer. */
  48. #define FDB_ASSERT(EXPR) \
  49. if (!(EXPR)) \
  50. { \
  51. FDB_DEBUG("(%s) has assert failed at %s.\n", #EXPR, __FUNCTION__); \
  52. while (1); \
  53. }
  54. #define FDB_KVDB_CTRL_SET_SEC_SIZE 0x0 /**< set sector size control command */
  55. #define FDB_KVDB_CTRL_GET_SEC_SIZE 0x1 /**< get sector size control command */
  56. #define FDB_KVDB_CTRL_SET_LOCK 0x2 /**< set lock function control command */
  57. #define FDB_KVDB_CTRL_SET_UNLOCK 0x3 /**< set unlock function control command */
  58. #define FDB_TSDB_CTRL_SET_SEC_SIZE 0x0 /**< set sector size control command */
  59. #define FDB_TSDB_CTRL_GET_SEC_SIZE 0x1 /**< get sector size control command */
  60. #define FDB_TSDB_CTRL_SET_LOCK 0x2 /**< set lock function control command */
  61. #define FDB_TSDB_CTRL_SET_UNLOCK 0x3 /**< set unlock function control command */
  62. #define FDB_TSDB_CTRL_SET_ROLLOVER 0x4 /**< set rollover control command */
  63. #define FDB_TSDB_CTRL_GET_ROLLOVER 0x5 /**< get rollover control command */
  64. #define FDB_TSDB_CTRL_GET_LAST_TIME 0x6 /**< get last save time control command */
  65. typedef time_t fdb_time_t;
  66. #ifdef FDB_USING_TIMESTAMP_64BIT
  67. typedef int64_t fdb_time_t;
  68. #endif
  69. typedef fdb_time_t (*fdb_get_time)(void);
  70. struct fdb_default_kv_node {
  71. char *key;
  72. void *value;
  73. size_t value_len;
  74. };
  75. struct fdb_default_kv {
  76. struct fdb_default_kv_node *kvs;
  77. size_t num;
  78. };
  79. /* error code */
  80. typedef enum {
  81. FDB_NO_ERR,
  82. FDB_ERASE_ERR,
  83. FDB_READ_ERR,
  84. FDB_WRITE_ERR,
  85. FDB_PART_NOT_FOUND,
  86. FDB_KV_NAME_ERR,
  87. FDB_KV_NAME_EXIST,
  88. FDB_SAVED_FULL,
  89. FDB_INIT_FAILED,
  90. } fdb_err_t;
  91. enum fdb_kv_status {
  92. FDB_KV_UNUSED,
  93. FDB_KV_PRE_WRITE,
  94. FDB_KV_WRITE,
  95. FDB_KV_PRE_DELETE,
  96. FDB_KV_DELETED,
  97. FDB_KV_ERR_HDR,
  98. FDB_KV_STATUS_NUM,
  99. };
  100. typedef enum fdb_kv_status fdb_kv_status_t;
  101. enum fdb_tsl_status {
  102. FDB_TSL_UNUSED,
  103. FDB_TSL_PRE_WRITE,
  104. FDB_TSL_WRITE,
  105. FDB_TSL_USER_STATUS1,
  106. FDB_TSL_DELETED,
  107. FDB_TSL_USER_STATUS2,
  108. FDB_TSL_STATUS_NUM,
  109. };
  110. typedef enum fdb_tsl_status fdb_tsl_status_t;
  111. /* key-value node object */
  112. struct fdb_kv {
  113. fdb_kv_status_t status; /**< node status, @see fdb_kv_status_t */
  114. bool crc_is_ok; /**< node CRC32 check is OK */
  115. uint8_t name_len; /**< name length */
  116. uint32_t magic; /**< magic word(`K`, `V`, `4`, `0`) */
  117. uint32_t len; /**< node total length (header + name + value), must align by FDB_WRITE_GRAN */
  118. uint32_t value_len; /**< value length */
  119. char name[FDB_KV_NAME_MAX]; /**< name */
  120. struct {
  121. uint32_t start; /**< node start address */
  122. uint32_t value; /**< value start address */
  123. } addr;
  124. };
  125. typedef struct fdb_kv *fdb_kv_t;
  126. struct fdb_kv_iterator {
  127. struct fdb_kv curr_kv; /**< Current KV we get from the iterator */
  128. uint32_t iterated_cnt; /**< How many KVs have we iterated already */
  129. size_t iterated_obj_bytes; /**< Total storage size of KVs we have iterated. */
  130. size_t iterated_value_bytes; /**< Total value size of KVs we have iterated. */
  131. uint32_t sector_addr; /**< Current sector address we're iterating. DO NOT touch it. */
  132. };
  133. typedef struct fdb_kv_iterator *fdb_kv_iterator_t;
  134. /* time series log node object */
  135. struct fdb_tsl {
  136. fdb_tsl_status_t status; /**< node status, @see fdb_log_status_t */
  137. fdb_time_t time; /**< node timestamp */
  138. uint32_t log_len; /**< log length, must align by FDB_WRITE_GRAN */
  139. struct {
  140. uint32_t index; /**< node index address */
  141. uint32_t log; /**< log data address */
  142. } addr;
  143. };
  144. typedef struct fdb_tsl *fdb_tsl_t;
  145. typedef bool (*fdb_tsl_cb)(fdb_tsl_t tsl, void *arg);
  146. typedef enum {
  147. FDB_DB_TYPE_KV,
  148. FDB_DB_TYPE_TS,
  149. } fdb_db_type;
  150. /* the flash sector store status */
  151. enum fdb_sector_store_status {
  152. FDB_SECTOR_STORE_UNUSED,
  153. FDB_SECTOR_STORE_EMPTY,
  154. FDB_SECTOR_STORE_USING,
  155. FDB_SECTOR_STORE_FULL,
  156. FDB_SECTOR_STORE_STATUS_NUM,
  157. };
  158. typedef enum fdb_sector_store_status fdb_sector_store_status_t;
  159. /* the flash sector dirty status */
  160. enum fdb_sector_dirty_status {
  161. FDB_SECTOR_DIRTY_UNUSED,
  162. FDB_SECTOR_DIRTY_FALSE,
  163. FDB_SECTOR_DIRTY_TRUE,
  164. FDB_SECTOR_DIRTY_GC,
  165. FDB_SECTOR_DIRTY_STATUS_NUM,
  166. };
  167. typedef enum fdb_sector_dirty_status fdb_sector_dirty_status_t;
  168. /* KVDB section information */
  169. struct kvdb_sec_info {
  170. bool check_ok; /**< sector header check is OK */
  171. struct {
  172. fdb_sector_store_status_t store; /**< sector store status @see fdb_sector_store_status_t */
  173. fdb_sector_dirty_status_t dirty; /**< sector dirty status @see sector_dirty_status_t */
  174. } status;
  175. uint32_t addr; /**< sector start address */
  176. uint32_t magic; /**< magic word(`E`, `F`, `4`, `0`) */
  177. uint32_t combined; /**< the combined next sector number, 0xFFFFFFFF: not combined */
  178. size_t remain; /**< remain size */
  179. uint32_t empty_kv; /**< the next empty KV node start address */
  180. };
  181. typedef struct kvdb_sec_info *kv_sec_info_t;
  182. /* TSDB section information */
  183. struct tsdb_sec_info {
  184. bool check_ok; /**< sector header check is OK */
  185. fdb_sector_store_status_t status; /**< sector store status @see fdb_sector_store_status_t */
  186. uint32_t addr; /**< sector start address */
  187. uint32_t magic; /**< magic word(`T`, `S`, `L`, `0`) */
  188. fdb_time_t start_time; /**< the first start node's timestamp, 0xFFFFFFFF: unused */
  189. fdb_time_t end_time; /**< the last end node's timestamp, 0xFFFFFFFF: unused */
  190. uint32_t end_idx; /**< the last end node's index, 0xFFFFFFFF: unused */
  191. fdb_tsl_status_t end_info_stat[2]; /**< the last end node's info status */
  192. size_t remain; /**< remain size */
  193. uint32_t empty_idx; /**< the next empty node index address */
  194. uint32_t empty_data; /**< the next empty node's data end address */
  195. };
  196. typedef struct tsdb_sec_info *tsdb_sec_info_t;
  197. struct kv_cache_node {
  198. uint16_t name_crc; /**< KV name's CRC32 low 16bit value */
  199. uint16_t active; /**< KV node access active degree */
  200. uint32_t addr; /**< KV node address */
  201. };
  202. typedef struct kv_cache_node *kv_cache_node_t;
  203. struct sector_cache_node {
  204. uint32_t addr; /**< sector start address */
  205. uint32_t empty_addr; /**< sector empty address */
  206. };
  207. typedef struct sector_cache_node *sector_cache_node_t;
  208. /* database structure */
  209. typedef struct fdb_db *fdb_db_t;
  210. struct fdb_db {
  211. const char *name; /**< database name */
  212. fdb_db_type type; /**< database type */
  213. const struct fal_partition *part; /**< flash partition */
  214. uint32_t sec_size; /**< flash section size. It's a multiple of block size */
  215. bool init_ok; /**< initialized successfully */
  216. void (*lock)(fdb_db_t db); /**< lock the database operate */
  217. void (*unlock)(fdb_db_t db); /**< unlock the database operate */
  218. void *user_data;
  219. };
  220. /* KVDB structure */
  221. struct fdb_kvdb {
  222. struct fdb_db parent; /**< inherit from fdb_db */
  223. struct fdb_default_kv default_kvs; /**< default KV */
  224. bool gc_request; /**< request a GC check */
  225. bool in_recovery_check; /**< is in recovery check status when first reboot */
  226. #ifdef FDB_KV_USING_CACHE
  227. /* KV cache table */
  228. struct kv_cache_node kv_cache_table[FDB_KV_CACHE_TABLE_SIZE];
  229. /* sector cache table, it caching the sector info which status is current using */
  230. struct sector_cache_node sector_cache_table[FDB_SECTOR_CACHE_TABLE_SIZE];
  231. #endif /* FDB_KV_USING_CACHE */
  232. #ifdef FDB_KV_AUTO_UPDATE
  233. uint32_t ver_num; /**< setting version number for update */
  234. #endif
  235. void *user_data;
  236. };
  237. typedef struct fdb_kvdb *fdb_kvdb_t;
  238. /* TSDB structure */
  239. struct fdb_tsdb {
  240. struct fdb_db parent; /**< inherit from fdb_db */
  241. struct tsdb_sec_info cur_sec; /**< current using sector */
  242. fdb_time_t last_time; /**< last TSL timestamp */
  243. fdb_get_time get_time; /**< the current timestamp get function */
  244. size_t max_len; /**< the max log length */
  245. uint32_t oldest_addr; /**< the oldest sector start address */
  246. bool rollover; /**< the oldest data will rollover by newest data, default is true */
  247. void *user_data;
  248. };
  249. typedef struct fdb_tsdb *fdb_tsdb_t;
  250. /* blob structure */
  251. struct fdb_blob {
  252. void *buf; /**< blob data buffer */
  253. size_t size; /**< blob data buffer size */
  254. struct {
  255. uint32_t meta_addr; /**< saved KV or TSL index address */
  256. uint32_t addr; /**< blob data saved address */
  257. size_t len; /**< blob data saved length */
  258. } saved;
  259. };
  260. typedef struct fdb_blob *fdb_blob_t;
  261. #ifdef __cplusplus
  262. }
  263. #endif
  264. #endif /* _FDB_DEF_H_ */