低配NOSQL

东西写的太简单了 都不好意思说是NOSQL

其实就是STL 的map容器记录了写入的信息

解析了下数据仅此。

分析的时候想了很多

比如学习redis的自写hash,动态调整hash表容量。

比如右值或者C语言直接操作内存 提升效率

比如多线程操作互斥 网络连接 记录操作时间等等

但是c++写起来,心智负担太多。

实在是太繁琐 一点激情都没了

还是简单一点 写个完整的获益更多。

最后就是这个简单完整的小代码

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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include <iostream>
#include <unordered_map>
#include <string>
#include <map>
#include <vector>
#include <assert.h>
using namespace std;
 
enum Command{
    AddKeyValue = 101,
    GetKeyValue,
    SetValue,
    ReplaceValue,
    DeleteKeyValue,
    AppendValue,
    PreappendValue,
    InvalidCommand
};
 
enum TokenVecIndex{
    commandIndex = 0,
    keyIndex,
    valueIndex,
    invalidIndex
};
 
std::unordered_map<std::string,Command> CommandString =
{
    {"add",AddKeyValue},
    {"get",GetKeyValue},
    {"set",SetValue},
    {"replace",ReplaceValue},
    {"del",DeleteKeyValue},
    {"append",AppendValue},
    {"preapp",PreappendValue}
};
 
std::unordered_map<std::string,std::string> ItemHashStorage;
 
void splitWithSTLFind(const string& str, const string& delim, vector<string>& ret)
{
    size_t front = str.find_first_not_of(delim);
    size_t back = str.find_first_of(delim, front) ;
 
    while(back != std::string::npos &&
          front !=  std::string::npos){
        ret.emplace(ret.end(),str.substr(front, back - front));
        front = str.find_first_not_of(delim, back +1);
        back = str.find_first_of(delim, front);
    }
    if(front != std::string::npos){
        ret.emplace(ret.end(),str.substr(front, back - front));
    }
}
 
 
bool CheckToken(std::vector<std::string>& tokenVec)
{
    bool bRet = false;
 
    if( (tokenVec[commandIndex] == "get" || tokenVec[commandIndex] == "del")
            && tokenVec.size() != 2){
        return bRet;
    }
 
    if(tokenVec.size() != 3)
        return bRet;
 
    bRet = true;
    return bRet;
}
 
bool GetCommand(const std::string& input,std::vector<std::string>& tokenVec){
    std::string delim = " ";
    tokenVec.clear();
    splitWithSTLFind(input,delim,tokenVec);
    return CheckToken(tokenVec);
}
 
bool SetValueFunc(const std::vector<std::string>& tokenVec){
    ItemHashStorage[tokenVec[keyIndex]] = tokenVec[valueIndex];
    return true;
}
 
 
bool AddKeyValueFunc(const std::vector<std::string>& tokenVec){
    if( ItemHashStorage.find(tokenVec[keyIndex]) != ItemHashStorage.end())
        return true;
    SetValueFunc(tokenVec);
    return true;
}
 
bool ReplaceValueFunc(const std::vector<std::string>& tokenVec){
    if( ItemHashStorage.find(tokenVec[keyIndex]) == ItemHashStorage.end())
        return false;
    SetValueFunc(tokenVec);
    return true;
}
 
bool GetKeyValueFunc(const std::vector<std::string>& tokenVec,string& retValueString){
    auto it = ItemHashStorage.find(tokenVec[keyIndex]);
    if(  it == ItemHashStorage.end())
        return false;
    retValueString = it->second;
    return true;
}
 
bool PreappendValueFunc(const std::vector<std::string>& tokenVec){
    auto it = ItemHashStorage.find(tokenVec[keyIndex]);
    if(  it == ItemHashStorage.end())
        return false;
    string s = tokenVec[valueIndex];
    s.append(it->second);
    std::swap(s,it->second);
    return true;
}
 
bool DeleteKeyValueFunc(const std::vector<std::string>& tokenVec){
    auto it = ItemHashStorage.find(tokenVec[keyIndex]);
    if(  it == ItemHashStorage.end())
        return true;
    ItemHashStorage.erase(it);
    return true;
}
 
bool AppendValueFunc(const std::vector<std::string>& tokenVec){
    auto it = ItemHashStorage.find(tokenVec[keyIndex]);
    if(  it == ItemHashStorage.end())
        return false;
    (it->second).append(tokenVec[valueIndex]);
    return true;
}
 
bool Excute(const std::vector<std::string>& tokenVec,string& retValueString){
    bool bRet = false;
    auto it =  CommandString.find(tokenVec[commandIndex]);
    if( it  == CommandString.end())
        return bRet;
    switch(it->second){
    case AddKeyValue:
        bRet = AddKeyValueFunc(tokenVec);
        break;
    case GetKeyValue:
        bRet = GetKeyValueFunc(tokenVec,retValueString);
        break;
    case SetValue:
        bRet = SetValueFunc(tokenVec);
        break;
    case ReplaceValue:
        bRet = ReplaceValueFunc(tokenVec);
        break;
    case DeleteKeyValue:
        bRet = DeleteKeyValueFunc(tokenVec);
        break;
    case AppendValue:
        bRet = AppendValueFunc(tokenVec);
        break;
    case PreappendValue:
        bRet = PreappendValueFunc(tokenVec);
        break;
    default:
        break;
    }
 
    return bRet;
}
 
 
std::vector<std::string> testStringVec1={
    "  add ",
    "  add  testkey1 testvalue1",
    "  add  testkey1 testvalue1",
    "  add  testkey1 testvalue1",
    "  add"
};
 
std::vector<std::string> testStringVec2={
    "  add ",
    "  add  testkey2 testvalue1",
    "  add  testkey3 testvalue1",
    "  add  testkey4 testvalue1",
    "  add"
};
 
int main(int argc, char *argv[])
{
    // test
    for(auto it:testStringVec1 ){
        std::vector<std::string> tokenVec;
        if( GetCommand(it,tokenVec)){
            std::string s;
            Excute(tokenVec,s);
        }
    }
    assert(ItemHashStorage.size()==1);
 
    for(auto it:testStringVec2 ){
        std::vector<std::string> tokenVec;
        if( GetCommand(it,tokenVec)){
            std::string s;
            Excute(tokenVec,s);
        }
    }
    assert(ItemHashStorage.size()==4);
 
    {
        std::vector<std::string> tokenVec;
        string commandStr= "get testkey4";
        string s;
        GetCommand(commandStr,tokenVec);
        Excute(tokenVec,s);
        assert(s==string("testvalue1"));
    }
 
    {
        std::vector<std::string> tokenVec;
        string commandStr= "get testkey4 testkey4";
        string s;
        GetCommand(commandStr,tokenVec);
        Excute(tokenVec,s);
        assert(s==string("testvalue1"));
    }
 
    {
        std::vector<std::string> tokenVec;
        string commandStr= "get nothing testkey4";
        string s;
        GetCommand(commandStr,tokenVec);
        assert( false == Excute(tokenVec,s));
        assert(s == string(""));
    }
 
    {
        std::vector<std::string> tokenVec;
        string commandStr= "set testkey2 testkey4";
        string s;
        GetCommand(commandStr,tokenVec);
        Excute(tokenVec,s);
        GetCommand("get testkey2",tokenVec);
        Excute(tokenVec,s);
        assert(s == ("testkey4"));
    }
 
    {
        std::vector<std::string> tokenVec;
        string commandStr= "replace testkey3 testkey33";
        string s;
        GetCommand(commandStr,tokenVec);
        Excute(tokenVec,s);
        GetCommand("get testkey3",tokenVec);
        Excute(tokenVec,s);
        assert(s == ("testkey33"));
    }
 
    {
        std::vector<std::string> tokenVec;
        string commandStr= "del testkey3 testkey33";
        string s;
        GetCommand(commandStr,tokenVec);
        assert(Excute(tokenVec,s));
        GetCommand("get testkey3",tokenVec);
        assert(false == Excute(tokenVec,s));
        assert(s== (""));
    }
 
    {
        std::vector<std::string> tokenVec;
        string commandStr= "append testkey1 -appendValue";
        string s;
        GetCommand(commandStr,tokenVec);
        assert(Excute(tokenVec,s));
        GetCommand("get testkey1",tokenVec);
        assert(Excute(tokenVec,s));
        assert(s== "testvalue1-appendValue");
    }
 
    {
        std::vector<std::string> tokenVec;
        string commandStr= "preapp testkey1 Pre-";
        string s;
        GetCommand(commandStr,tokenVec);
        assert(Excute(tokenVec,s));
        GetCommand("get testkey1",tokenVec);
        assert(Excute(tokenVec,s));
        assert(s== "Pre-testvalue1-appendValue");
    }
    return 0;
}

  

posted on   itdef  阅读(177)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
历史上的今天:
2016-06-08 文件打包代码更新 使用json记录打包文件信息
2015-06-08 c++11多线程学习笔记之三 condition_variable使用

导航

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示