forked from dbratcher/MediaKhan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.cpp
200 lines (186 loc) · 5.39 KB
/
database.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
#include "utils.h"
#include "database.h"
#include <pthread.h>
struct timespec start, stop;
double time_spent;
pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;
int vold_calls=0;
int readdir_calls=0;
int access_calls=0;
int getattr_calls=0;
int read_calls=0;
int write_calls=0;
int create_calls=0;
int rename_calls=0;
double tot_time=0;
double vold_avg_time=0;
double readdir_avg_time=0;
double access_avg_time=0;
double getattr_avg_time=0;
double read_avg_time=0;
double write_avg_time=0;
double create_avg_time=0;
double rename_avg_time=0;
double localize_time=0;
int redis_calls=0;
double redis_avg_time=0;
bool init_database(){
#ifdef VOLDEMORT_FOUND
if(DATABASE==VOLDEMORT) {
return voldemort_init();
}
#endif
#ifdef REDIS_FOUND
if(DATABASE==REDIS){
return redis_init();
}
#endif
#ifdef BDB_FOUND
if(DATABASE==BDB) {
return bdb_init();
}
#endif
}
string database_setval(string file_id, string col, string val){
file_id=trim(file_id);
col=trim(col);
val=trim(val);
#ifdef VOLDEMORT_FOUND
if(DATABASE==VOLDEMORT){
vold_calls++;
clock_gettime(CLOCK_REALTIME,&start);
string retstring="fail";
retstring=voldemort_setval(file_id,col,val);
clock_gettime(CLOCK_REALTIME,&stop);
time_spent = (stop.tv_sec-start.tv_sec)+(stop.tv_nsec-start.tv_nsec)/BILLION; tot_time += time_spent;
vold_avg_time=(vold_avg_time*(vold_calls-1)+time_spent)/vold_calls;
return retstring;
}
#endif
#ifdef REDIS_FOUND
if(DATABASE==REDIS){
redis_calls++;
clock_gettime(CLOCK_REALTIME,&start);
string retstring="fail";
pthread_mutex_lock(&mymutex);
retstring=redis_setval(file_id,col,val);
pthread_mutex_unlock(&mymutex);
clock_gettime(CLOCK_REALTIME,&stop);
time_spent = (stop.tv_sec-start.tv_sec)+(stop.tv_nsec-start.tv_nsec)/BILLION; tot_time += time_spent;
redis_avg_time=(redis_avg_time*(redis_calls-1)+time_spent)/redis_calls;
return retstring;
}
#endif
#ifdef BDB_FOUND
if(DATABASE==BDB) {
string retstring = bdb_setval(file_id, col, val);
return retstring;
}
#endif
}
string database_getval(string col, string val){
//fprintf(stderr, "in getval with %s %s", col.c_str(), val.c_str());
col=trim(col);
val=trim(val);
#ifdef VOLDEMORT_FOUND
if(DATABASE==VOLDEMORT){
//log_msg("using vold");
vold_calls++;
clock_gettime(CLOCK_REALTIME,&start);
string retstring=voldemort_getval(col,val);
clock_gettime(CLOCK_REALTIME,&stop);
time_spent = (stop.tv_sec-start.tv_sec)+(stop.tv_nsec-start.tv_nsec)/BILLION; tot_time += time_spent;;
vold_avg_time=(vold_avg_time*(vold_calls-1)+time_spent)/vold_calls;
return retstring;
}
#endif
#ifdef REDIS_FOUND
if(DATABASE==REDIS){
redis_calls++;
clock_gettime(CLOCK_REALTIME,&start);
pthread_mutex_lock(&mymutex);
string retstring=redis_getval(col,val);
pthread_mutex_unlock(&mymutex);
//cout<<"just got a "<<retstring<<endl;
clock_gettime(CLOCK_REALTIME,&stop);
time_spent = (stop.tv_sec-start.tv_sec)+(stop.tv_nsec-start.tv_nsec)/BILLION; tot_time += time_spent;;
redis_avg_time=(redis_avg_time*(redis_calls-1)+time_spent)/redis_calls;
return retstring;
}
#endif
#ifdef BDB_FOUND
if(DATABASE==BDB) {
string retstring = bdb_getval(col, val);
return retstring;
}
#endif
}
string database_getvals(string col){
col=trim(col);
#ifdef VOLDEMORT_FOUND
if(DATABASE==VOLDEMORT){
vold_calls++;
clock_gettime(CLOCK_REALTIME,&start);
string retstr=voldemort_getkey_cols(col);
clock_gettime(CLOCK_REALTIME,&stop);
time_spent = (stop.tv_sec-start.tv_sec)+(stop.tv_nsec-start.tv_nsec)/BILLION; tot_time += time_spent;
vold_avg_time=(vold_avg_time*(vold_calls-1)+time_spent)/vold_calls;
return retstr;
}
#endif
#ifdef REDIS_FOUND
if(DATABASE==REDIS){
redis_calls++;
clock_gettime(CLOCK_REALTIME,&start);
pthread_mutex_lock(&mymutex);
string retstr=redis_getkey_cols(col);
pthread_mutex_unlock(&mymutex);
clock_gettime(CLOCK_REALTIME,&stop);
time_spent = (stop.tv_sec-start.tv_sec)+(stop.tv_nsec-start.tv_nsec)/BILLION; tot_time += time_spent;
redis_avg_time=(redis_avg_time*(redis_calls-1)+time_spent)/redis_calls;
return retstr;
}
#endif
#ifdef BDB_FOUND
if(DATABASE==BDB) {
string retstring = bdb_getkey_cols(col);
return retstring;
}
#endif
}
void database_remove_val(string file, string col, string val){
file=trim(file);
col=trim(col);
val=trim(val);
#ifdef VOLDEMORT_FOUND
if(DATABASE==VOLDEMORT){
vold_calls++;
clock_gettime(CLOCK_REALTIME,&start);
voldemort_remove_val(file,col,val);
clock_gettime(CLOCK_REALTIME,&stop);
time_spent = (stop.tv_sec-start.tv_sec)+(stop.tv_nsec-start.tv_nsec)/BILLION; tot_time += time_spent;
vold_avg_time=(vold_avg_time*(vold_calls-1)+time_spent)/vold_calls;
return;
}
#endif
#ifdef REDIS_FOUND
if(DATABASE==REDIS){
redis_calls++;
clock_gettime(CLOCK_REALTIME,&start);
pthread_mutex_lock(&mymutex);
redis_remove_val(file,col,val);
pthread_mutex_unlock(&mymutex);
clock_gettime(CLOCK_REALTIME,&stop);
time_spent = (stop.tv_sec-start.tv_sec)+(stop.tv_nsec-start.tv_nsec)/BILLION; tot_time += time_spent;
redis_avg_time=(redis_avg_time*(redis_calls-1)+time_spent)/redis_calls;
return;
}
return;
#endif
#ifdef BDB_FOUND
if(DATABASE==BDB) {
bdb_remove_val(file, col, val);
return;
}
#endif
}