实现全量持久化:save操作落盘,启动时读取到内存
增量持久化:执行修改操作时将cmd追加到log中,启动时逐条取出顺序执行
This commit is contained in:
@@ -66,10 +66,11 @@ int kvs_write_u32(uint8_t **pp, const uint8_t *end, uint32_t v) {
|
||||
|
||||
|
||||
int getcmd(uint8_t op, const char *key, const char *value, uint8_t *buf){
|
||||
if(!key || !buf) return -1;
|
||||
if(!buf) return -1;
|
||||
uint8_t *end = buf + CMD_SIZE;
|
||||
uint8_t *p = buf;
|
||||
uint8_t argc = ((value == NULL)?1 : 2);
|
||||
uint8_t argc = (key == NULL)?0:1;
|
||||
argc += (value == NULL)?0:1;
|
||||
|
||||
|
||||
if (kvs_write_u8(&p, end, op) < 0) return -1;
|
||||
@@ -77,12 +78,14 @@ int getcmd(uint8_t op, const char *key, const char *value, uint8_t *buf){
|
||||
|
||||
|
||||
// 写入 key
|
||||
int keylen = strlen(key);
|
||||
if (kvs_write_u32(&p, end, keylen) < 0) return -1;
|
||||
if (kvs_need(p, end, keylen) < 0) return -1;
|
||||
if (keylen > 0) {
|
||||
memcpy(p, key, keylen);
|
||||
p += keylen;
|
||||
if(key){
|
||||
int keylen = strlen(key);
|
||||
if (kvs_write_u32(&p, end, keylen) < 0) return -1;
|
||||
if (kvs_need(p, end, keylen) < 0) return -1;
|
||||
if (keylen > 0) {
|
||||
memcpy(p, key, keylen);
|
||||
p += keylen;
|
||||
}
|
||||
}
|
||||
|
||||
if(value){
|
||||
|
||||
@@ -18,8 +18,8 @@
|
||||
#define BATCH_SIZE (65536)
|
||||
#define TIME_SUB_MS(tv1, tv2) ((tv1.tv_sec - tv2.tv_sec) * 1000 + (tv1.tv_usec - tv2.tv_usec) / 1000)
|
||||
|
||||
// #define PRESP print_response
|
||||
#define PRESP
|
||||
#define PRESP print_response
|
||||
// #define PRESP
|
||||
|
||||
|
||||
typedef enum {
|
||||
@@ -50,7 +50,8 @@ enum {
|
||||
KVS_CMD_HDEL,
|
||||
KVS_CMD_HMOD,
|
||||
KVS_CMD_HEXIST,
|
||||
|
||||
KVS_CMD_SAVE,
|
||||
|
||||
KVS_CMD_COUNT,
|
||||
};
|
||||
|
||||
|
||||
@@ -67,21 +67,23 @@ void testcase(int connfd, uint8_t op, const char* key, const char* value, rsp_re
|
||||
|
||||
void array_testcase_1w(int connfd) {
|
||||
|
||||
int count = 10000;
|
||||
int count = 1000;
|
||||
int i = 0;
|
||||
|
||||
struct timeval tv_begin;
|
||||
gettimeofday(&tv_begin, NULL);
|
||||
|
||||
for (i = 0;i < count;i ++) {
|
||||
testcase(connfd, KVS_CMD_HSET, "nage", "lian", KVS_STATUS_OK, NULL, "SET NAME");
|
||||
testcase(connfd, KVS_CMD_HGET, "nage", NULL, KVS_STATUS_OK, "lian", "GET NAME");
|
||||
testcase(connfd, KVS_CMD_HMOD, "nage", "liu", KVS_STATUS_OK, NULL, "MOD NAME");
|
||||
testcase(connfd, KVS_CMD_HGET, "nage", NULL, KVS_STATUS_OK, "liu", "GET NAME");
|
||||
|
||||
testcase(connfd, KVS_CMD_HEXIST, "nage", NULL, KVS_STATUS_EXIST, NULL, "EXIST NAME");
|
||||
testcase(connfd, KVS_CMD_HDEL, "nage", NULL, KVS_STATUS_OK, NULL, "DEL NAME");
|
||||
testcase(connfd, KVS_CMD_HEXIST, "nage", NULL, KVS_STATUS_NO_EXIST, NULL, "NOT EXIST NAME");
|
||||
testcase(connfd, KVS_CMD_SET, "name", "lian", KVS_STATUS_OK, NULL, "SET NAME");
|
||||
testcase(connfd, KVS_CMD_GET, "name", NULL, KVS_STATUS_OK, "lian", "GET NAME");
|
||||
testcase(connfd, KVS_CMD_MOD, "name", "liu", KVS_STATUS_OK, NULL, "MOD NAME");
|
||||
testcase(connfd, KVS_CMD_GET, "name", NULL, KVS_STATUS_OK, "liu", "GET NAME");
|
||||
testcase(connfd, KVS_CMD_EXIST, "name", NULL, KVS_STATUS_EXIST, NULL, "EXIST NAME");
|
||||
testcase(connfd, KVS_CMD_DEL, "name", NULL, KVS_STATUS_OK, NULL, "DEL NAME");
|
||||
testcase(connfd, KVS_CMD_EXIST, "name", NULL, KVS_STATUS_NO_EXIST, NULL, "NOT EXIST NAME");
|
||||
|
||||
testcase(connfd, KVS_CMD_MOD, "stu", "liu", KVS_STATUS_NO_EXIST, NULL, "MOD NAME");
|
||||
testcase(connfd, KVS_CMD_DEL, "stu", NULL, KVS_STATUS_NO_EXIST, NULL, "DEL SUT");
|
||||
}
|
||||
|
||||
struct timeval tv_end;
|
||||
@@ -89,7 +91,7 @@ void array_testcase_1w(int connfd) {
|
||||
|
||||
int time_used = TIME_SUB_MS(tv_end, tv_begin); // ms
|
||||
|
||||
printf("array testcase --> time_used: %d, qps: %d\n", time_used, 70000 * 1000 / time_used);
|
||||
printf("array testcase --> time_used: %d, qps: %d\n", time_used, 9000 * 1000 / time_used);
|
||||
|
||||
}
|
||||
|
||||
@@ -104,7 +106,7 @@ void do_batch_example(int fd)
|
||||
for(int i = 0;i < 24; ++ i){
|
||||
int len = sprintf(key, "k%d", i);
|
||||
len = sprintf(val, "v%d", i);
|
||||
kvs_batch_add(&batch, KVS_CMD_HSET, key, val);
|
||||
kvs_batch_add(&batch, KVS_CMD_SET, key, val);
|
||||
}
|
||||
|
||||
// 一次性发送
|
||||
@@ -122,28 +124,37 @@ void do_batch_example(int fd)
|
||||
}
|
||||
|
||||
|
||||
for(int i = 0;i < 24; ++ i){
|
||||
int len = sprintf(key, "k%d", i);
|
||||
len = sprintf(val, "v%d", i);
|
||||
testcase(fd, KVS_CMD_HGET, key, NULL, KVS_STATUS_OK, val, "GET K");
|
||||
}
|
||||
// for(int i = 0;i < 24; ++ i){
|
||||
// int len = sprintf(key, "k%d", i);
|
||||
// len = sprintf(val, "v%d", i);
|
||||
// testcase(fd, KVS_CMD_GET, key, NULL, KVS_STATUS_OK, val, "GET K");
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
void save(int connfd){
|
||||
testcase(connfd, KVS_CMD_SAVE, NULL, NULL, KVS_STATUS_OK, NULL, "SAVE");
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc != 3) {
|
||||
if (argc != 4) {
|
||||
printf("arg error\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
char *ip = argv[1];
|
||||
int port = atoi(argv[2]);
|
||||
int mode = atoi(argv[3]);
|
||||
|
||||
int connfd = connect_tcpserver(ip, port);
|
||||
|
||||
array_testcase_1w(connfd);
|
||||
// do_batch_example(connfd);
|
||||
|
||||
if(mode == 0){
|
||||
do_batch_example(connfd);
|
||||
}else if(mode == 1){
|
||||
array_testcase_1w(connfd);
|
||||
}else if(mode == 2){
|
||||
save(connfd);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user