mmap加载配置文件,uring实现持久化

This commit is contained in:
1iaan
2026-01-22 12:38:34 +00:00
parent f031e107b5
commit ba2004c258
15 changed files with 627 additions and 639 deletions

View File

@@ -1,17 +1,13 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include "kvstore.h"
#include "kvs_rw_tools.h"
#include "mem_pool/mem_pool.h"
#include <arpa/inet.h>
#include "diskuring/diskuring.h"
// Key, Value -->
// Modify
@@ -269,30 +265,64 @@ int kvs_hash_exist_bin(kvs_hash_t *hash, const void *key, uint32_t key_len) {
// 0 suc, <0 error
int kvs_hash_save(kvs_hash_t *inst, const char* filename){
if(!inst || !filename) return -1;
FILE *fp = fopen(filename, "wb");
if(!fp) return -2;
int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if(fd < 0) return -2;
off_t current_off = 0;
for(int i = 0;i < inst->max_slots; ++ i){
for (hashnode_t *n = inst->nodes[i]; n != NULL; n = n->next) {
if (!n->key || n->key_len == 0) continue;
if (n->value_len > 0 && !n->value) { fclose(fp); return -3; }
if (n->value_len > 0 && !n->value) { close(fd); return -3; }
uint32_t klen = htonl((uint32_t)n->key_len);
uint32_t vlen = htonl((uint32_t)n->value_len);
if (kvs_write_file(fp, &klen, 4) < 0) { fclose(fp); return -4; }
if (kvs_write_file(fp, &vlen, 4) < 0) { fclose(fp); return -4; }
void *bufs[4];
size_t lens[4];
int count = 0;
if (kvs_write_file(fp, n->key, n->key_len) < 0) { fclose(fp); return -4; }
if (n->value_len > 0) {
if (kvs_write_file(fp, n->value, n->value_len) < 0) { fclose(fp); return -4; }
bufs[count] = &klen;
lens[count] = sizeof(klen);
count++;
bufs[count] = &vlen;
lens[count] = sizeof(vlen);
count++;
if (n->key_len > 0){
bufs[count] = n->key;
lens[count] = n->key_len;
count++;
}
if (n->value_len > 0) {
bufs[count] = n->value;
lens[count] = n->value_len;
count++;
}
size_t total = 0;
for (int i = 0; i < count; i++) total += lens[i];
task_t *t = submit_write(&global_uring_ctx, fd, bufs, lens, count, current_off);
if (!t) { close(fd); return -4; }
int res = task_wait(t);
task_destroy(t);
if (res < 0) {
close(fd);
return -5;
}
current_off += (off_t) total;
}
}
fclose(fp);
close(fd);
return 0;
}