-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkvStore.cpp
More file actions
385 lines (370 loc) · 12.9 KB
/
kvStore.cpp
File metadata and controls
385 lines (370 loc) · 12.9 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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
using namespace std;
#define rep(n) for(register int i=0;i<(n);i++)
#define jrep(n) for( register int j=0;j<(n);j++)
// globals
int fen[8503056+5],arr[8503056+5];
char decc[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
struct Slice{ uint8_t size; char* data; };
struct LL{ Slice *data; Slice *key; };
LL *hasht[8503056+5];
bool mmutex = false;
int nop;
// end globals
// Linked List
Slice *nsl(Slice *s){
Slice *d = new Slice;
d->size = s->size;
d->data = new char[d->size];
register int i=0;
for(;i+3<s->size;i+=4){
d->data[i] = s->data[i];
d->data[i+1] = s->data[i+1];
d->data[i+2] = s->data[i+2];
d->data[i+3] = s->data[i+3];
}
for(;i<s->size;i++) d->data[i]=s->data[i];
return d;
}
bool eq(Slice *a, Slice *b){
if(a==NULL||b==NULL) return false;
if(a->size!=b->size) return false;
rep(a->size){ if(a->data[i]!=b->data[i]) return false; } return true;
}
string sliceToStra(Slice& a) {
string ret = "";
for (int i = 0; i < a.size; i++)
ret += a.data[i];
return ret;
}
void gv(int index){ rep(arr[index]) cout<<sliceToStra(*hasht[index][i].key)<<" "; cout<<endl; }
bool les(LL a, LL b){
int n=a.key->size; if(n>(b.key->size)) n=b.key->size;
rep(n){
if((a.key->data[i])<(b.key->data[i])) return true;
else if((a.key->data[i])>(b.key->data[i])) return false;
} return a.key->size < b.key->size;
}
void ins(int ind, Slice *d, Slice *k){
arr[ind]++;
if(arr[ind]>1){
hasht[ind] = (LL*) realloc(hasht[ind],arr[ind]*sizeof(LL));
} else hasht[ind] = (LL*) malloc(sizeof(LL));
// if(arr[ind]==1){
// hasht[ind] = (LL*) malloc(sizeof(LL)*3);
// } //else hasht[ind] = (LL*) malloc(sizeof(LL));
hasht[ind][arr[ind]-1].key = k; hasht[ind][arr[ind]-1].data = d;
for(int i=arr[ind]-2;i>=0;i--) if(les(hasht[ind][i+1],hasht[ind][i])){
LL tmp = hasht[ind][i+1]; hasht[ind][i+1]=hasht[ind][i]; hasht[ind][i]=tmp;
}
}
bool rdel(int ind,Slice *k,bool tp=false){
if(tp) cout<<k->data<<" "<<(int)k->size<<endl;
bool found=false;
if(k==NULL) return false;
rep(arr[ind]){
if(ind>=0&&ind<=8503056&&eq(hasht[ind][i].key,k)){
found=true;
// delete hasht[ind][i].key;
// delete hasht[ind][i].data;
}
if(found) hasht[ind][i]=hasht[ind][i+1];
}
if(found) arr[ind]--;//,hasht[ind] = (LL*) realloc(hasht[ind],arr[ind]*sizeof(LL));
return found;
}
Slice *rget(int ind,Slice *k){
rep(arr[ind]){ if(eq(hasht[ind][i].key,k)) return hasht[ind][i].data; } return NULL;
}
// end link list
// hash
int dec(char a){
if(a<'a') return a-'A'+1; else return a-'a'+27;
}
int shash(Slice *s){
int ret = 0; int up=1; // [9, 486, 26244, 1417176, 76527504]
//cout<<s->data<<" "<<dec(s->data[0])<<endl;
// if(s->size>5) ret+=(dec(s->data[4])/6);
// if(s->size>4) ret+=(dec(s->data[3])*9);
// if(s->size>3) ret+=(dec(s->data[2])*486);
// if(s->size>2) ret+=(dec(s->data[1])*26244);
// if(s->size>1) ret+=(dec(s->data[0])*1417176);
// if(s->size>5) ret+=(dec(s->data[4])/6);
if(s->size>4) ret+=(dec(s->data[3]));
if(s->size>3) ret+=(dec(s->data[2])*54);
if(s->size>2) ret+=(dec(s->data[1])*2916);
if(s->size>1) ret+=(dec(s->data[0])*157464);
return ret;
}
// end hash
// fenwick
void addf(int index){ index++;
while(index<=8503056) fen[index]++,index+=(index&-index);
}
void delf(int index){ index++;
while(index<=8503056) fen[index]--,index+=(index&-index);
}
int query(int index){ index++; int su=0;
while(index>0) su+=fen[index],index-=(index&-index);
return su;
}
int gib(int index){
int l=0,r=8503056,ans=-1; int mid=(l+r)/2;
while(l<=r){
if(query(mid)>=index){ ans=mid;r=mid-1; }
else l=mid+1;
mid=(l+r)/2;
}
if(ans>=0) {
if(ans==0 || (ans!=0 && query(ans-1)!=index)) return ans;
else if (ans !=0 && query(ans-1)==index){
l=0,r=ans-1;mid=(l+r)/2;
while(l<=r){
if(query(mid)==index){ ans=mid;r=mid-1; }
else if(query(mid)>index) r=mid-1;
else l=mid+1;
mid=(l+r)/2;
} return ans;
}
}
else return -1;
}
// end fenwick
// class
class kvStore {
// pthread_mutex_t lock[55];
public:
kvStore(uint64_t max_entries){
// rep(55) pthread_mutex_init(&lock[i], NULL) ;
};
bool put(Slice &key,Slice &value){
// return true;
// if(mmutex){ cout<<"CAUGHT PUT\n"; return false; } mmutex=true;
//cout<<"PUT\n";
// if(mmutex) cout<<"ERROR!!!\n";
Slice *ckey = nsl(&key);
Slice *cvalue = nsl(&value);
// Slice *ckey = &key;
// Slice *cvalue = &value;
//if(ckey->data[ckey->size-1]=='\n'){
//cout<<"ENDS WITH nl\n";
//ckey->size--;
nop++;
//}
int index = shash(ckey);
// pthread_mutex_lock(&lock[0]);
//if(key.size==3 && key.data[0]=='B' && key.data[1]=='e') cout<<value.data<<endl;
//cout<<"PUT into ||" <<index<<"||\n";
//cout<<"KEY = ||"<<ckey->data<<"||\n";
// Slice *ckey = &key;
// Slice *cvalue = &value;
// bool tp=false;
// if(key.data[0]=='H'&&key.size==1){
// cout<<"CHECK FOR H put\n";
// tp=true;
nop++;
// }
if(rget(index,ckey)==NULL){
//if(key.size==3 && key.data[0]=='B' && key.data[1]=='e') cout<<"NEW VALue\n";
// if(key.data[0]=='H'&&key.size==1) cout<<"HERE\n";
ins(index,cvalue,ckey);
addf(index);
// pthread_mutex_unlock(&lock[0]);
// mmutex=false;
return false;
} else {
//if(key.size==3 && key.data[0]=='B' && key.data[1]=='e') cout<<"EXISTING VALue\n";
//cout<<"here\n";
rdel(index,ckey);
ins(index,cvalue,ckey);
// mmutex=false;
// pthread_mutex_unlock(&lock[0]);
return true;
}
};
bool get(Slice &key , Slice & value){
// return true;
// if(mmutex){ cout<<"CAUGHT GET\n"; return false; } mmutex=true;
//cout<<"GET\n";
//if(mmutex) return false; mmutex=true;
// if(mmutex) cout<<"ERROR!!!\n";
//cout<<"GET"<<'\n';
//if(key.size<10) return false;
//key.size--;
int index = shash(&key);
// pthread_mutex_lock(&lock[0]);
//cout<<key.data<<endl;
Slice *g = rget(index,&key);
//if(g==NULL) cout<<"NOT FOUND\n"; else cout<<"FOUND\n";
//if(key.size==2 && key.data[0]=='B' && key.data[1]=='e') cout<<g->data<<endl;
if(g==NULL){
// pthread_mutex_unlock(&lock[0]);
return false;
}
value = *g;
// pthread_mutex_unlock(&lock[0]);
// mmutex=false;
return true;
// else if(eq(g,&value)) return true;
// else return false;
};
bool del(Slice &key){
// return true;
// return true;
// if(mmutex){ cout<<"CAUGHT DEL\n"; return false; } mmutex=true;
//cout<<"DEL\n";
//if(mmutex) return false; mmutex=true;
// if(mmutex) cout<<"ERROR!!!\n";
//key.size--;
int index = shash(&key);
// pthread_mutex_lock(&lock[0]);
// try{
if(rdel(index,&key)){
delf(index);
//key.size++;
// pthread_mutex_unlock(&lock[0]);
// mmutex=false;
return true;
}
// } catch(...){}
// pthread_mutex_unlock(&lock[0]);
//key.size++;
// mmutex=false;
return false;
};
bool get(int n ,Slice& key, Slice& value ){
// return true;
// if(mmutex){ cout<<"CAUGHT\n"; return false; } mmutex=true;
// if(mmutex) return false; mmutex=true;
// if(mmutex) cout<<"ERROR!!!\n";
// if(n>=query(76527504)-1){ mmutex=false; return false; }
//cout<<"GET N\n";
n++;
int index = gib(n);
// pthread_mutex_lock(&lock[0]);
//cout<<index<<" " <<n<<" "<<query(index-1)<<endl;
if(index>0) n-=(query(index-1)+1);
else n--;
if(index<0){
// pthread_mutex_unlock(&lock[0]);
return false;
}
if(n>=0&&n<arr[index]){
key = *hasht[index][n].key;
value = *hasht[index][n].data;
}// cout<<key.data<<endl;
// mmutex=false;
// pthread_mutex_unlock(&lock[0]);
return true;
};
bool del(int n){
// return true;
// if(mmutex){ cout<<"CAUGHT\n"; return false; } mmutex=true;
// if(n>=query(76527504)-1){ mmutex=false; return false; }
//cout<<"DEL N\n";
//if(mmutex) return false; mmutex=true;
// if(mmutex) cout<<"ERROR!!!\n";
n++;
int index = gib(n);
// pthread_mutex_lock(&lock[0]);
// cout<<index<<" "<<n<<" "<<query(index-1)<<endl;
if(index>0) n-=(query(index-1)+1);
else n--;
// if(index<0){ pthread_mutex_unlock(&lock); return false; }
// try{
// cout<<"REACHED HERE\n";
if(n>=0){
// cout<<index<<" Deleting!!\n";
if(index<0||index>=8503056) return false;
// Slice *key = hasht[index][n].key;
for(int i=n;i<arr[index]-1;i++) hasht[index][i]=hasht[index][i+1]; arr[index]--;
// cout<<key->data<<endl;
// int index=0;
// Slice *key = hasht[index][n].key;
// rdel(index,key);
delf(index);
}
// } catch(...){}
// mmutex=false;
// pthread_mutex_unlock(&lock[0]);
return true;
};
};
// end class
//kvStore kv(10);
//#include<bits/stdc++.h>
//int main(){
// int n=10000000;
// double tdiff = 0;
// struct timespec st, en;
// rep(n){
// Slice *d = new Slice, *k = new Slice;
// k->data = new char[32]; d->data = new char[128];
// d->size = 128; k->size=32;
// cin>>k->data>>d->data;
// //cout<<k->data<<" "<<d->data<<endl;
// //ins(0,*d,*k);
// //if(i==999999) cout<<k->data<<endl;
// clock_gettime(CLOCK_MONOTONIC, &st);
// kv.put(*k,*d);
// clock_gettime(CLOCK_MONOTONIC, &en);
// tdiff += (en.tv_sec - st.tv_sec) + 1e-9*(en.tv_nsec - st.tv_nsec);
// if(i%100000==0) cout<<"AT "<<i/100000<<endl;
// //gv(0);
// //cout<<d.data<<" "<<k.data<<endl;
// }
// cout<<tdiff<<endl;
//}
// map<int,int> ast;
// rep(76527504) ast[arr[i]]++;
// for(auto i:ast) cout<<i.first<<" : "<<i.second<<endl;
// //while(false){
// // Slice *d = new Slice, *k = new Slice;
// // d->data = new char[255]; k->data = new char[64];
// // d->size = 255; k->size=64;
// // cin>>k->data>>d->data;
// // int typ; cin>>typ;
// // if(typ==0){
// // cout<<kv.get(*k,*d)<<endl;
// // } else if(typ==1) {
// // cout<<kv.del(*k)<<endl;
// // } else if(typ==2) {
// // int nv; cin>>nv;
// // cout<<kv.get(nv,*k,*d)<<endl;
// // } else if(typ==3){
// // cout<<"Deleting\n";
// // int nv; cin>>nv; cout<<kv.del(n)<<endl;
// // }
// //}
// //while(true){
// // Slice *d = new Slice, *k = new Slice;
// // d->data = new char[64]; k->data = new char[255];
// // d->size = 10; k->size=10;
// // cin>>k->data;
// // Slice *a = rget(0,k);
// // if(a!=NULL) cout<<a->data<<endl;
// // del(0,k);
// // gv(0);
// //}
// //rep(10000){
// // //cout<<"=======================================\n";
// // //rep(10) cout<<arr[i]<<" "; cout<<endl;
// // int type; cin>>type;
// // if(type==0){
// // int a; cin>>a;
// // arr[a]--; rdelf(a);
// // } else if(type==1) {
// // int a; cin>>a;
// // arr[a]++; addf(a);
// // } else {
// // int a; cin>>a;
// // cout<<gi(a)<<"="<<gib(a)<<endl;
// // }
// //}
// }