-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTestRedisHashStore.cpp
237 lines (177 loc) · 5.83 KB
/
TestRedisHashStore.cpp
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
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cassert>
#include <unistd.h>
#include <vector>
#include "RedisHashStore.hpp"
#include "TableEntry.hpp"
using namespace std;
static string m_idname = "idname";
uint64_t assign_random_long(){
uint64_t result = rand();
result <<= 32;
result |= rand();
return result;
}
void assert_equal_table_entrys(const ClipTBLEntry &entry1, const ClipTBLEntry &entry2){
assert(entry1.id == entry2.id);
assert(entry1.idnum == entry2.idnum);
assert(entry1.seqnum == entry2.seqnum);
assert(entry1.total == entry2.total);
}
static int m_idvalue = 0;
int create_keyvalue_set(vector<struct ImgHash> &hashes, vector<ClipTBLEntry> &entries, const int N){
int current_id = ++m_idvalue;
hashes.clear();
entries.clear();
hashes.reserve(N);
entries.reserve(N);
struct ImgHash hash;
ClipTBLEntry entry;
entry.id = m_idname + to_string(current_id);
entry.idnum = current_id;
entry.total = N;
for (int i=0;i<N;i++){
hash.hashc = assign_random_long();
entry.seqnum = i;
hashes.push_back(hash);
entries.push_back(entry);
}
return 0;
}
void prime_random_generator(){
const unsigned int seed = 29839813U;
srand(seed);
for (int i=0;i<2000;i++){
rand();
}
}
void run_test(const string &host, const int port){
int N1 = 55, N2 = 125, N3 = 980, N4 = 4500;
vector<struct ImgHash> keys1;
vector<ClipTBLEntry> entries1;
create_keyvalue_set(keys1, entries1, N1);
vector<struct ImgHash> keys2;
vector<ClipTBLEntry> entries2;
create_keyvalue_set(keys2, entries2, N2);
vector<struct ImgHash> keys3;
vector<ClipTBLEntry> entries3;
create_keyvalue_set(keys3, entries3, N3);
vector<struct ImgHash> keys4;
vector<ClipTBLEntry> entries4;
create_keyvalue_set(keys4, entries4, N4);
ClipTBLEntry *plookup = new ClipTBLEntry();
try {
HashStore *pStore = new RedisHashStore(host, port); //open data store
assert(pStore != NULL);
int initial_no_entries = pStore->GetCount();
cout << "initial no. entries " << initial_no_entries << endl;
assert(initial_no_entries >= 0);
vector<string> ids = pStore->GetEntries();
int initial_no_ids = ids.size();
cout << "initial no. id's " << initial_no_ids << endl;
assert(initial_no_ids >= 0);
cout << "Add entries." << endl;
// add first group of key/value pairs
for (int i=0;i<N1;i++){
pStore->PutHashKeyValue(&keys1[i], &entries1[i], true);
}
// add second group of key/value pairs
for (int i=0;i<N2;i++){
pStore->PutHashKeyValue(&keys2[i], &entries2[i], true);
}
// add third group of key value pairs
for (int i=0;i<N3;i++){
pStore->PutHashKeyValue(&keys3[i], &entries3[i], true);
}
// add fourth group of key value pairs
for (int i=0;i<N4;i++){
pStore->PutHashKeyValue(&keys4[i], &entries4[i], true);
}
cout << "Test Retrieval." << endl;
int index = rand()%N1;
pStore->GetHashValue(&keys1[index], &plookup);
assert_equal_table_entrys(entries1[index], *plookup);
index = rand()%N2;
pStore->GetHashValue(&keys2[index], &plookup);
assert_equal_table_entrys(entries2[index], *plookup);
index = rand()%N3;
pStore->GetHashValue(&keys3[index], &plookup);
assert_equal_table_entrys(entries3[index], *plookup);
index = rand()%N4;
pStore->GetHashValue(&keys4[index], &plookup);
assert_equal_table_entrys(entries4[index], *plookup);
int n = pStore->GetCount();
assert(n == N1 + N2 + N3 + N4 + initial_no_entries);
n = pStore->GetCount(entries1[0].id);
assert(n == N1);
n = pStore->GetCount(entries2[0].id);
assert(n == N2);
n = pStore->GetCount(entries3[0].id);
assert(n == N3);
n = pStore->GetCount(entries4[0].id);
assert(n == N4);
vector<string> ids2 = pStore->GetEntries();
assert(ids2.size() == 4);
cout << "Delete entries." << endl;
for (int i=0;i<N1;i++){
pStore->DeleteEntry(&keys1[i]);
}
for (int i=0;i<N2;i++){
pStore->DeleteEntry(&keys2[i]);
}
for (int i=0;i<N3;i++){
pStore->DeleteEntry(&keys3[i]);
}
for (int i=0;i<N4;i++){
pStore->DeleteEntry(&keys4[i]);
}
n = pStore->GetCount();
assert(n == initial_no_entries);
n = pStore->GetCount(entries1[0].id);
assert(n == 0);
n = pStore->GetCount(entries2[0].id);
assert(n == 0);
n = pStore->GetCount(entries3[0].id);
assert(n == 0);
n = pStore->GetCount(entries4[0].id);
assert(n == 0);
vector<string> ids3 = pStore->GetEntries();
assert(ids3.size() == 0);
cout << "Add ID." << endl;
for (int i=0;i<N1/2;i++){
pStore->PutHashKeyValue(&keys1[i], &entries1[i], true);
}
cout << "Delete by ID" << endl;
pStore->DeleteEntry(entries1[0].id);
cout << "Test Overwrite." << endl;
pStore->PutHashKeyValue(&keys1[10], &entries1[10], false); // put in one entry for given key
pStore->PutHashKeyValue(&keys1[10], &entries1[12], true); // overwrite another entry to same key
pStore->GetHashValue(&keys1[10], &plookup);
assert_equal_table_entrys(entries1[12], *plookup);
pStore->DeleteEntry(&keys1[10]);
vector<string> ids4 = pStore->GetEntries();
cout << "final no. id's " << ids4.size() << endl;
assert((int)ids4.size() == initial_no_ids);
n = pStore->GetCount();
cout << "final no. entries " << n << endl;
assert(n == initial_no_entries);
delete pStore;
} catch (RedisStoreException &ex){
cout << "unable to complete redis op: " << ex.what() << endl;
assert(false);
}
delete plookup;
return;
}
int main(int argc, char **argv){
const string host = "tcp://127.0.0.1";
const int port = 6379;
prime_random_generator();
cout << "Test Redis Key/Value Hash Store." << endl;
run_test(host, port);
cout << "Done." << endl;
return 0;
}