实现全量持久化:save操作落盘,启动时读取到内存

增量持久化:执行修改操作时将cmd追加到log中,启动时逐条取出顺序执行
This commit is contained in:
2026-01-07 18:43:28 +08:00
parent cb0134a852
commit 3cc97b9454
9 changed files with 373 additions and 74 deletions

View File

@@ -4,6 +4,10 @@
#include "kvstore.h"
#include "kvs_rw_tools.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#if ENABLE_ARRAY
extern kvs_array_t global_array;
@@ -17,6 +21,8 @@ extern kvs_rbtree_t global_rbtree;
extern kvs_hash_t global_hash;
#endif
int global_cmd_log_fd = -1;
void *kvs_malloc(size_t size) {
return malloc(size);
}
@@ -60,10 +66,7 @@ int kvs_split_token(char *msg, char *tokens[]) {
// tokens[1] : Key
// tokens[2] : Value
#if BIN_SAFE
#else
#if !BIN_SAFE
int kvs_filter_protocol(char **tokens, int count, char *response) {
if (tokens[0] == NULL || count == 0 || response == NULL) return -1;
@@ -282,6 +285,12 @@ int kvs_protocol(char *request, int request_length, int *consumed_out, char *res
*consumed_out = consumed;
*response_length = out_len;
return KVS_ERROR;
}else{
// 执行成功,在这里保存到日志中。
if(req.op == KVS_CMD_SET || req.op == KVS_CMD_MOD || req.op == KVS_CMD_DEL){
printf("%d:%d\n", req.op, req.argc);
kvs_save_cmd_to_logfile(p, len, global_cmd_log_fd);
}
}
if (out_len >= KVS_MAX_RESPONSE) {
@@ -338,11 +347,28 @@ int kvs_protocol(char *msg, int length, char *response) { //
}
#endif
int init_cmd_log(const char *file, int *logfd){
if(!file) return -1;
int fd = open(file, O_RDWR | O_CREAT | O_APPEND, 0644);
if(fd < 0) return -2;
*logfd = fd;
return 0;
}
int destroy_cmd_log(int logfd){
close(logfd);
return 0;
}
int init_kvengine(void) {
#if ENABLE_ARRAY
memset(&global_array, 0, sizeof(kvs_array_t));
kvs_array_create(&global_array);
kvs_array_load(&global_array, KVS_ARRAY_FILE);
#endif
#if ENABLE_RBTREE
@@ -355,6 +381,9 @@ int init_kvengine(void) {
kvs_hash_create(&global_hash);
#endif
init_cmd_log(KVS_CMD_LOG_FILE, &global_cmd_log_fd);
kvs_replay_log(KVS_CMD_LOG_FILE, global_cmd_log_fd);
return 0;
}
@@ -369,6 +398,7 @@ void dest_kvengine(void) {
kvs_hash_destroy(&global_hash);
#endif
destroy_cmd_log(global_cmd_log_fd);
}