-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathload_sketches_redis_selectdb.cpp
More file actions
197 lines (142 loc) · 4.79 KB
/
load_sketches_redis_selectdb.cpp
File metadata and controls
197 lines (142 loc) · 4.79 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
/*
compile : g++ -o load_sketches_redis_selectdb -O3 -I ../hiredis -pthread load_sketches_redis_selectdb.cpp -g -ggdb -L../hiredis -lhiredis -lm
*/
#include <iostream>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <cstring>
#include <vector>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <functional>
#include <string>
#include <fstream>
#include <inttypes.h>
#include "sketch.h"
#include "hiredis/hiredis.h"
#include "opencv2/imgproc/imgproc.hpp"
#define CHECK(X) if ( !X || X->type == REDIS_REPLY_ERROR ) { printf("Error\n"); exit(-1); }
using namespace std;
string sketches_train_relpaths("./sketches_train_list.txt");
string sketches_train_basepath("./data/FlickrLogos-v2/sketches_train/");
int select_database = 1;
void fill_db( redisContext *c, unsigned long long int *a1, unsigned long long int *a2, unsigned long long int *a3, int size, char* filename)
{
redisReply *reply;
reply = (redisReply *) redisCommand(c,"SELECT %d",select_database);
CHECK(reply);
freeReplyObject(reply);
int cmd=0;
for (int i=0; i<size; i++ )
{
redisAppendCommand(c,"APPEND %llu %s", *(a1+i), filename);
redisAppendCommand(c,"APPEND %llu %s", *(a2+i), filename);
redisAppendCommand(c,"APPEND %llu %s", *(a3+i), filename);
cmd += 3;
}
while ( cmd-- > 0 )
{
int r = redisGetReply(c, (void **) &reply );
if ( r == REDIS_ERR ) { printf("Error at cmd = %d and filename : %s\n", cmd, filename); exit(-1); }
CHECK(reply);
freeReplyObject(reply);
}
}
/*
Hashing a single sketch tuple (central_VW, minHash1)
sigbit is used to keep all 3 sketches separate by adding corresponding long int
*/
void hash_sketch(sketch* s, unsigned long long int *h, int size, int sigbit)
{
unsigned long long int temp=0, temp1=0;
unsigned int a=0, b=0;
for(int i=0; i<size; i++)
{
a = (s+i)->a;
b = (s+i)->b;
temp1 = a+b;
temp1 = temp1*(temp1+1)/2 + a;
if(sigbit == 1) temp = temp1 + 1000000000000000; // to keep sketch from hash1 separate from hash2 and hash3
if(sigbit == 2) temp = temp1 + 2000000000000000; // to keep sketch from hash1 separate from hash1 and hash3
if(sigbit == 3) temp = temp1 + 3000000000000000; // to keep sketch from hash1 separate from hash1 and hash2
*(h+i) = temp;
//cout<<a<<"\t"<<b<<"\t"<<temp1<<"\t"<<sigbit<<"\t"<<temp<<endl;exit(0);
}
}
void read_sketches(ifstream& in, sketch* s_coll1, sketch* s_coll2, sketch* s_coll3, int num){
unsigned int xa=0, xb=0, yb=0, zb=0;
int count=0;
while(count<num){
in >> xa;
in >> xb;
in >> yb;
in >> zb;
// cout<<x<<"\t"<<y<<endl;
(s_coll1+count)->a = xa;
(s_coll1+count)->b = xb;
(s_coll2+count)->a = xa;
(s_coll2+count)->b = yb;
(s_coll3+count)->a = xa;
(s_coll3+count)->b = zb;
count++;
}
}
int main(int argc, char *argv[] ) {
redisContext *c;
redisReply *reply;
int n = 0;
int db = 0;
c = redisConnect( "localhost", 6379);
/* c = redisConnectUnix("/tmp/redis.sock"); */
if (c->err) {
printf("Connection error: %s\n", c->errstr);
exit(1);
}
reply = (redisReply *) redisCommand(c,"CONFIG RESETSTAT");
CHECK(reply);
freeReplyObject(reply);
ifstream infile;
infile.open(sketches_train_relpaths.c_str());
string temp;
int file_num=0;
string name_store;
ifstream sketch_file;
while(getline(infile, temp))
{
sketch_file.open((sketches_train_basepath + temp).c_str());
int sketch_size1 = count(istreambuf_iterator<char>(sketch_file), istreambuf_iterator<char>(), '\n');
sketch_file.seekg(0, sketch_file.beg);
sketch *sketches_test1 = new sketch[sketch_size1];
sketch *sketches_test2 = new sketch[sketch_size1];
sketch *sketches_test3 = new sketch[sketch_size1];
read_sketches(sketch_file, sketches_test1, sketches_test2, sketches_test3, sketch_size1);
sketch_file.close();
unsigned long long int *sk_ind1 = new long long unsigned int[sketch_size1];
unsigned long long int *sk_ind2 = new long long unsigned int[sketch_size1];
unsigned long long int *sk_ind3 = new long long unsigned int[sketch_size1];
hash_sketch(sketches_test1, sk_ind1, sketch_size1, 1);
hash_sketch(sketches_test2, sk_ind2, sketch_size1, 2);
hash_sketch(sketches_test3, sk_ind3, sketch_size1, 3);
delete[] sketches_test1;
delete[] sketches_test2;
delete[] sketches_test3;
name_store = temp + ",";
fill_db(c, sk_ind1, sk_ind2, sk_ind3, sketch_size1, (char*)name_store.c_str());
/* printing hashes once ever 100 files */
if(file_num>100 && file_num%100==0)
cout<<*(sk_ind1+0)<<"\t"<<*(sk_ind2+0)<<"\t"<<*(sk_ind3+0)<<"\n"
<<name_store<<endl;
///////////////////////////////////////////////////////////
delete[] sk_ind1;
delete[] sk_ind2;
delete[] sk_ind3;
cout<<"Completed filling DB for file num : "<<++file_num<<endl;
}
infile.close();
redisFree(c);
return 0;
}