mmap加载配置文件,uring实现持久化
This commit is contained in:
@@ -1,15 +1,10 @@
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
#include "kvstore.h"
|
||||
#include "kvs_rw_tools.h"
|
||||
#include "mem_pool/mem_pool.h"
|
||||
#include <arpa/inet.h>
|
||||
#include "diskuring/diskuring.h"
|
||||
|
||||
int kvs_keycmp(const uint8_t *a, uint32_t alen,
|
||||
const uint8_t *b, uint32_t blen) {
|
||||
@@ -469,31 +464,59 @@ int kvs_rbtree_exist(kvs_rbtree_t *inst, const void *key, uint32_t key_len) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int kvs_rbtree_save_node(FILE *fp, kvs_rbtree_t *inst, rbtree_node *node) {
|
||||
if (!fp || !inst || !node) return -1;
|
||||
static int kvs_rbtree_save_node(int fd, off_t *current_off, kvs_rbtree_t *inst, rbtree_node *node) {
|
||||
if (!current_off || !inst || !node) return -1;
|
||||
if (node == inst->nil) return 0;
|
||||
|
||||
int rc = 0;
|
||||
|
||||
rc = kvs_rbtree_save_node(fp, inst, node->left);
|
||||
rc = kvs_rbtree_save_node(fd, current_off, inst, node->left);
|
||||
if (rc < 0) return rc;
|
||||
|
||||
uint32_t klen_n = htonl(node->key_len);
|
||||
uint32_t vlen_n = htonl(node->value_len);
|
||||
uint32_t klen = htonl(node->key_len);
|
||||
uint32_t vlen = htonl(node->value_len);
|
||||
|
||||
if (kvs_write_file(fp, &klen_n, sizeof(klen_n)) < 0) return -1;
|
||||
if (kvs_write_file(fp, &vlen_n, sizeof(vlen_n)) < 0) return -1;
|
||||
void *bufs[4];
|
||||
size_t lens[4];
|
||||
int count = 0;
|
||||
|
||||
if (node->key_len) {
|
||||
if (!node->key) return -1;
|
||||
if (kvs_write_file(fp, node->key, node->key_len) < 0) return -1;
|
||||
}
|
||||
if (node->value_len) {
|
||||
if (!node->value) return -1;
|
||||
if (kvs_write_file(fp, node->value, node->value_len) < 0) return -1;
|
||||
}
|
||||
bufs[count] = &klen;
|
||||
lens[count] = sizeof(klen);
|
||||
count++;
|
||||
|
||||
rc = kvs_rbtree_save_node(fp, inst, node->right);
|
||||
bufs[count] = &vlen;
|
||||
lens[count] = sizeof(vlen);
|
||||
count++;
|
||||
|
||||
if (node->key_len > 0) {
|
||||
bufs[count] = node->key;
|
||||
lens[count] = node->key_len;
|
||||
count++;
|
||||
}
|
||||
|
||||
if (node->value_len > 0) {
|
||||
bufs[count] = node->value;
|
||||
lens[count] = node->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) { return -4; }
|
||||
int res = task_wait(t);
|
||||
task_destroy(t);
|
||||
|
||||
if (res < 0) {
|
||||
return -5;
|
||||
}
|
||||
|
||||
*current_off += (off_t) total;
|
||||
|
||||
rc = kvs_rbtree_save_node(fd, current_off, inst, node->right);
|
||||
if (rc < 0) return rc;
|
||||
|
||||
return 0;
|
||||
@@ -503,14 +526,14 @@ static int kvs_rbtree_save_node(FILE *fp, kvs_rbtree_t *inst, rbtree_node *node)
|
||||
int kvs_rbtree_save(kvs_rbtree_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;
|
||||
|
||||
int rc = kvs_rbtree_save_node(fp, inst, inst->root);
|
||||
|
||||
if (fflush(fp) != 0) rc = -3;
|
||||
int rc = kvs_rbtree_save_node(fd, ¤t_off, inst, inst->root);
|
||||
|
||||
fclose(fp);
|
||||
close(fd);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user