rbtree和hash的全量持久化操作。rbtree的二进制安全。

粗略测试。
This commit is contained in:
2026-01-08 21:42:20 +08:00
parent de21fe94ec
commit 4b4e06b33d
16 changed files with 1997 additions and 1450 deletions

View File

@@ -8,7 +8,8 @@
#include "kvstore.h"
#include "kvs_rw_tools.h"
#include <arpa/inet.h>
// Key, Value -->
// Modify
@@ -114,7 +115,7 @@ void kvs_hash_destroy(kvs_hash_t *hash) {
* @return: <0 error; =0 success; >0 exist
*/
int kvs_hash_set_bin(kvs_hash_t *hash, const void *key, uint32_t key_len, const void *value, uint32_t value_len) {
if (!hash || !hash->nodes || !key || key_len == 0 || !value) return -1;
if (!hash || !hash->nodes || !key || key_len == 0) return -1;
int idx = _hash(key, key_len, MAX_TABLE_SIZE);
if (idx < 0) return -1;
@@ -208,16 +209,16 @@ int kvs_hash_count(kvs_hash_t *hash) {
}
/*
* @return 0 success; <0 error/noexist
* @return < 0, error; =0, success; >0, no exist
*/
int kvs_hash_del_bin(kvs_hash_t *hash, const void *key, uint32_t key_len) {
if (!hash || !key || key_len == 0) return -2;
if (!hash || !key || key_len == 0) return -1;
int idx = _hash(key, key_len, MAX_TABLE_SIZE);
if (idx < 0) return -2;
if (idx < 0) return -1;
hashnode_t *head = hash->nodes[idx];
if (head == NULL) return -1; // noexist
if (head == NULL) return 1; // noexist
// head node
if (_key_equal(head, key, key_len)) {
@@ -241,7 +242,7 @@ int kvs_hash_del_bin(kvs_hash_t *hash, const void *key, uint32_t key_len) {
if (cur->next == NULL) {
return -1;
return 1;
}
hashnode_t *tmp = cur->next;
@@ -264,37 +265,86 @@ int kvs_hash_exist_bin(kvs_hash_t *hash, const void *key, uint32_t key_len) {
return value ? 0 : 1;
}
#if 0
int main() {
// 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;
kvs_hash_create(&hash);
kvs_hash_set(&hash, "Teacher1", "King");
kvs_hash_set(&hash, "Teacher2", "Darren");
kvs_hash_set(&hash, "Teacher3", "Mark");
kvs_hash_set(&hash, "Teacher4", "Vico");
kvs_hash_set(&hash, "Teacher5", "Nick");
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; }
char *value1 = kvs_hash_get(&hash, "Teacher1");
printf("Teacher1 : %s\n", value1);
uint32_t klen = htonl((uint32_t)n->key_len);
uint32_t vlen = htonl((uint32_t)n->value_len);
int ret = kvs_hash_mod(&hash, "Teacher1", "King1");
printf("mode Teacher1 ret : %d\n", ret);
char *value2 = kvs_hash_get(&hash, "Teacher1");
printf("Teacher2 : %s\n", value1);
if (kvs_write_file(fp, &klen, 4) < 0) { fclose(fp); return -4; }
if (kvs_write_file(fp, &vlen, 4) < 0) { fclose(fp); return -4; }
ret = kvs_hash_del(&hash, "Teacher1");
printf("delete Teacher1 ret : %d\n", ret);
ret = kvs_hash_exist(&hash, "Teacher1");
printf("Exist Teacher1 ret : %d\n", ret);
kvs_hash_destroy(&hash);
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; }
}
}
}
fclose(fp);
return 0;
}
#endif
int kvs_hash_load(kvs_hash_t *inst, const char* filename){
if (!inst || !filename) return -1;
if (!inst->nodes || inst->max_slots <= 0) return -1;
FILE *fp = fopen(filename, "rb");
if (!fp) return -2;
while(1){
uint32_t klen_n = 0, vlen_n = 0;
if (kvs_read_file(fp, &klen_n, 4) < 0) { fclose(fp); return -3; }
if (kvs_read_file(fp, &vlen_n, 4) < 0) { fclose(fp); return -3; }
uint32_t klen = ntohl(klen_n);
uint32_t vlen = ntohl(vlen_n);
if (klen == 0) { fclose(fp); return -3; }
uint8_t *keybuf = (uint8_t*)kvs_malloc((size_t)klen);
if (!keybuf) { fclose(fp); return -4; }
if (kvs_read_file(fp, keybuf, (size_t)klen) < 0) {
kvs_free(keybuf);
fclose(fp);
return -3;
}
uint8_t *valbuf = NULL;
if (vlen > 0) {
valbuf = (uint8_t*)kvs_malloc((size_t)vlen);
if (!valbuf) {
kvs_free(keybuf);
fclose(fp);
return -4;
}
if (kvs_read_file(fp, valbuf, (size_t)vlen) < 0) {
kvs_free(valbuf);
kvs_free(keybuf);
fclose(fp);
return -3;
}
}
int rc = kvs_hash_set_bin(inst, keybuf, klen, valbuf, vlen);
kvs_free(keybuf);
if (vlen > 0) kvs_free(valbuf);
if (rc < 0) { // error
fclose(fp);
return -5;
}
}
fclose(fp);
return 0;
}