-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatsvr_kv.cpp
More file actions
121 lines (98 loc) · 2.81 KB
/
statsvr_kv.cpp
File metadata and controls
121 lines (98 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* OS headers */
#include <sys/file.h>
#include <fstream>
#include <time.h>
/* TFC headers */
#include "tfc_debug_log.h"
#include "kv_define.h"
#include "kv_errno.h"
/* module headers */
#include "statsvr_mcd_proc.h"
#include "statsvr_kv.h"
using namespace std;
using namespace ad::kv_server;
#define KV_BUFF_SIZE (10 * 1024 * 1024)
static char kv_server_buffer[KV_BUFF_SIZE];
int KVGetKeyValue(KvCacheCtrl &ckv_cache, const string &key, string &value)
{
//LogTrace("[KV_GET] key: %s", key.c_str());
value.clear();
if (0 == key.length() || key.length() > MAX_KEY_LEN)
{
LogError();
return ERRNO_KV_INVALID_KEY_LEN;
}
ssize_t date_len;
int ret;
ret = ckv_cache.get((char *)key.c_str() , key.length() , kv_server_buffer , sizeof(kv_server_buffer) , date_len);
if (ret >= 0)
{
value.assign(kv_server_buffer, date_len);
LogTrace("[KV_GET] success to get key: %s, value:%s", key.c_str(), value.c_str());
return 0;
}
else
{
//LogError("[KV_GET] key:%s not exist!", key.c_str());
return ERRNO_KV_KEY_NOT_EXIST;
}
}
int KVSetKeyValue(KvCacheCtrl &ckv_cache, const string &key, const string &value)
{
//LogTrace("[KV_SET] key: %s, value:%s", key.c_str(), value.c_str());
if (0 == key.length() || key.length() > MAX_KEY_LEN)
{
LogError("[KV_SET] Invalid key len!");
return ERRNO_KV_INVALID_KEY_LEN;
}
if (0 == value.length())
{
LogError("[KV_SET] value is empty, try delete key-value!");
return KVDelKeyValue(ckv_cache, key);
}
int ret = 0;
ret = ckv_cache.set((char *)key.c_str(), key.length(), (char*)value.c_str(), value.length());
ret = ret >= 0 ? 0 : -1;
if (0 == ret)
{
//LogTrace("Success to set key: %s, value: %s", key.c_str(), value.c_str());
}
else
{
LogError("Failed to set key: %s, value: %s", key.c_str(), value.c_str());
}
return ret;
}
int KVDelKeyValue(KvCacheCtrl &ckv_cache, const string &key)
{
//LogTrace("[KV_DEL] key: %s", key.c_str());
if (0 == key.length() || key.length() > MAX_KEY_LEN)
{
LogDebug("[KEY_DEL] Invalid key len!");
return ERRNO_KV_INVALID_KEY_LEN;
}
/*if(key.length() > KV_KEY_LEN)
{
return -3;
}*/
int ret;
ret = ckv_cache.del((char *)key.c_str() , key.length());
if (0 == ret)
{
LogTrace("Success to del key: %s", key.c_str());
}
else
{
LogTrace("Failed to del key: %s", key.c_str());
}
return ret == 0 ? 0 : ERRNO_KV_DEL_ERR;
}
#if 0
int KVAppendKeyValue(KvCacheCtrl &ckv_cache, const string &key, const string &value)
{
string old_value;
KVGetKeyValue(ckv_cache, key, old_value);
old_value.append(value);
return KVSetKeyValue(ckv_cache, key, old_value);
}
#endif