fdb_def.h 14 KB

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