fdb_def.h 13 KB

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