forked from pkumod/gStore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Signature.cpp
332 lines (290 loc) · 8.5 KB
/
Signature.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
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
/*=============================================================================
# Filename: Signature.cpp
# Author: Bookug Lobert
# Mail: zengli-bookug@pku.edu.cn
# Last Modified: 2016-04-11 13:18
# Description:
=============================================================================*/
#include "Signature.h"
using namespace std;
std::string
Signature::BitSet2str(const EntityBitSet& _bitset)
{
std::stringstream _ss;
bool any = false;
for (unsigned i = 0; i < _bitset.size(); i++)
{
if (_bitset.test(i))
{
_ss << "[" << i << "] ";
any = true;
}
}
if (!any)
{
_ss << "empty" << endl;
}
_ss << endl;
return _ss.str();
}
void
Signature::encodeEdge2Entity(EntityBitSet& _entity_bs, TYPE_PREDICATE_ID _pre_id, TYPE_ENTITY_LITERAL_ID _neighbor_id, const char _type)
{
Signature::encodePredicate2Entity(_entity_bs, _pre_id, _type);
#ifdef DEBUG
//if(_neighbor_id == 438460)
//{
//cout<<"predicate encoded"<<endl;
//}
#endif
Signature::encodeStr2Entity(_entity_bs, _neighbor_id, _type);
// for(int i = 800; i < _entity_bs.size(); i++){
// _entity_bs.set(i);
// }
//encode predicate and entity together
int x = _pre_id % Signature::STR_AND_EDGE_INTERVAL_BASE;
int y = _neighbor_id % Signature::STR_AND_EDGE_INTERVAL_BASE;
int seed = x + (x + y + 1) * (x + y) / 2;
seed %= Signature::STR_AND_EDGE_INTERVAL_BASE;
seed = seed + Signature::STR_SIG_LENGTH + Signature::EDGE_SIG_LENGTH;
if(Util::is_literal_ele(_neighbor_id))
{
seed += (Signature::STR_AND_EDGE_INTERVAL_BASE * 2);
}
else //entity part
{
//entity can be in edge or out edge
if (_type == Util::EDGE_OUT)
{
seed += Signature::STR_AND_EDGE_INTERVAL_BASE;
}
}
_entity_bs.set(seed);
}
void
Signature::encodePredicate2Entity(EntityBitSet& _entity_bs, TYPE_PREDICATE_ID _pre_id, const char _type)
{
//NOTICE:this not used now
if (Signature::PREDICATE_ENCODE_METHOD == 0)
{
//WARN:change if need to use again, because the encoding method has changed now!
unsigned pos = ((_pre_id + 10) % Signature::EDGE_SIG_LENGTH) + Signature::STR_SIG_LENGTH;
_entity_bs.set(pos);
}
else
{
//NOTICE: in * maybe the unsigned will overflow
long long id = _pre_id;
unsigned seed_num = id % Signature::EDGE_SIG_INTERVAL_NUM_HALF;
//int pos = Signature::STR_SIG_LENGTH;
if (_type == Util::EDGE_OUT)
{
seed_num += Signature::EDGE_SIG_INTERVAL_NUM_HALF;
//pos += Signature::EDGE_SIG_IN;
}
//unsigned primeSize = 5;
//unsigned prime1[]={5003,5009,5011,5021,5023};
//unsigned prime2[]={49943,49957,49991,49993,49999};
//NOTICE: more ones in the bitset(use more primes) means less conflicts, but also weakens the filtration of VSTree.
// when the data set is big enough, cutting down the size of candidate list should come up to our primary consideration.
// in this case we should not encode too many ones in entities' signature.
// also, when the data set is small, hash conflicts can hardly happen.
// therefore, I think using 2 primes(set up two ones in bitset) is enough.
// --by hanshuo.
//unsigned primeSize = 2;
//unsigned prime1[] = {5003, 5011};
//unsigned prime2[] = {49957, 49993};
//for(unsigned i = 0; i < primeSize; i++)
//{
//unsigned seed = _pre_id * prime1[i] % prime2[i];
//unsigned pos = (seed % Signature::EDGE_SIG_INTERVAL_BASE) + Signature::STR_SIG_LENGTH + Signature::EDGE_SIG_INTERVAL_BASE * seed_num;
//_entity_bs.set(pos);
//}
//unsigned seed = id * 5003 % 49957;
//unsigned pos = (seed % Signature::EDGE_SIG_INTERVAL_BASE) + Signature::STR_SIG_LENGTH + Signature::EDGE_SIG_INTERVAL_BASE * seed_num;
//_entity_bs.set(pos);
long long seed = id * 5003 % 49957;
seed = (seed % Signature::EDGE_SIG_INTERVAL_BASE) + Signature::STR_SIG_LENGTH + Signature::EDGE_SIG_INTERVAL_BASE * seed_num;
_entity_bs.set(seed);
}
}
//void
//Signature::encodePredicate2Edge(unsigned _pre_id, EdgeBitSet& _edge_bs)
//{
//if (Signature::PREDICATE_ENCODE_METHOD == 0)
//{
//unsigned pos = (_pre_id + 10) % Signature::EDGE_SIG_LENGTH;
//_edge_bs.set(pos);
//}
//else
//{
//unsigned seed_num = _pre_id % Signature::EDGE_SIG_INTERVAL_NUM_HALF;
////unsigned primeSize = 5;
////unsigned prime1[]={5003,5009,5011,5021,5023};
////unsigned prime2[]={49943,49957,49991,49993,49999};
////unsigned primeSize = 2;
////unsigned prime1[] = {5003,5011};
////unsigned prime2[] = {49957,49993};
////for (unsigned i = 0; i < primeSize; i++)
////{
////unsigned seed = _pre_id * prime1[i] % prime2[i];
////unsigned pos = (seed % Signature::EDGE_SIG_INTERVAL_BASE) + Signature::EDGE_SIG_INTERVAL_BASE * seed_num;
////_edge_bs.set(pos);
////}
//unsigned seed = _pre_id * 5003 % 49957;
//unsigned pos = (seed % Signature::EDGE_SIG_INTERVAL_BASE) + Signature::EDGE_SIG_INTERVAL_BASE * seed_num;
//_edge_bs.set(pos);
//}
//}
//NOTICE: no need to encode itself because only variable in query need to be filtered!
//So only consider all neighbors!
void
Signature::encodeStr2Entity(EntityBitSet& _entity_bs, TYPE_ENTITY_LITERAL_ID _neighbor_id, const char _type)
{
//NOTICE: we assume the parameter is always valid(invalid args should not be passed here)
long long id = _neighbor_id;
//NOTICE: in * maybe the unsigned will overflow
//long long seed = id * 5003 % 49957;
//seed = seed % Signature::STR_SIG_INTERVAL_BASE;
//seed = seed + (id % Signature::STR_SIG_INTERVAL_NUM) * Signature::STR_SIG_INTERVAL_BASE;
int seed = _neighbor_id % Signature::STR_SIG_LITERAL;
if(Util::is_literal_ele(_neighbor_id))
{
seed += Signature::STR_SIG_ENTITY;
}
else //entity part
{
//entity can be in edge or out edge
if (_type == Util::EDGE_OUT)
{
seed += Signature::STR_SIG_LITERAL;
}
}
//if(_neighbor_id == 438460)
//{
//cout<<_neighbor_id<<" "<<seed<<endl;
//}
_entity_bs.set(seed);
//_str is subject or object or literal
//if (strlen(_str) >0 && _str[0] == '?')
//return;
//unsigned length = (unsigned)strlen(_str);
//unsigned unsigned hashKey = 0;
//unsigned unsigned pos = 0;
//char *str2 = (char*)calloc(length + 1, sizeof(char));
//strcpy(str2, _str);
//char *str = str2;
//unsigned base = Signature::STR_SIG_BASE * (Signature::HASH_NUM - 1);
//for (unsigned i = Signature::HASH_NUM - 1; i >= 0; --i)
//{
//HashFunction hf = Util::hash[i];
//if (hf == NULL)
//break;
//hashKey = hf(str);
//str = str2;
//pos = base + hashKey % Signature::STR_SIG_BASE;
//base -= Signature::STR_SIG_BASE;
//if (_str[0] == '"')
//{
//pos += Signature::STR_SIG_LENGTH2;
//}
//else if (_str[0] != '<')
//{
//#ifdef DEBUG_VSTREE
//cerr << "error in encodeStr2Entity(): neighbor is neither a literal or entity!" << endl;
//#endif
//}
//_entity_bs.set(pos);
//}
//BETTER: use multiple threads for different hash functions
#ifdef DEBUG_VSTREE
//std::stringstream _ss;
//_ss << "encodeStr2Entity:" << str2 << endl;
//Util::logging(_ss.str());
#endif
//free(str2);
}
//void
//Signature::encodeStrID2Entity(unsigned _str_id, EntityBitSet& _entity_bs)
//{
////NOT USED NOW
//}
EntitySig::EntitySig()
{
this->entityBitSet.reset();
}
EntitySig::EntitySig(const EntitySig* _p_sig)
{
this->entityBitSet.reset();
this->entityBitSet |= _p_sig->entityBitSet;
}
EntitySig::EntitySig(const EntitySig& _sig)
{
this->entityBitSet.reset();
this->entityBitSet |= _sig.entityBitSet;
}
EntitySig::EntitySig(const EntityBitSet& _bitset)
{
this->entityBitSet.reset();
this->entityBitSet |= _bitset;
}
EntitySig&
EntitySig::operator|=(const EntitySig& _sig)
{
this->entityBitSet |= _sig.entityBitSet;
return *this;
}
bool
EntitySig::operator==(const EntitySig& _sig)const
{
return (this->entityBitSet == _sig.entityBitSet);
}
bool
EntitySig::operator!=(const EntitySig& _sig)const
{
return (this->entityBitSet != _sig.entityBitSet);
}
EntitySig&
EntitySig::operator=(const EntitySig& _sig)
{
this->entityBitSet.reset();
this->entityBitSet |= _sig.getBitset();
return *this;
}
const EntityBitSet&
EntitySig::getBitset()const
{
return this->entityBitSet;
}
//EdgeSig::EdgeSig()
//{
//this->edgeBitSet.reset();
//}
//EdgeSig::EdgeSig(const EdgeSig* _p_sig)
//{
//this->edgeBitSet.reset();
//this->edgeBitSet |= _p_sig->edgeBitSet;
//}
//EdgeSig::EdgeSig(const EdgeSig& _sig)
//{
//this->edgeBitSet.reset();
//this->edgeBitSet |= _sig.edgeBitSet;
//}
//EdgeSig::EdgeSig(const EdgeBitSet& _bitset)
//{
//this->edgeBitSet.reset();
//this->edgeBitSet |= _bitset;
//}
//EdgeSig&
//EdgeSig::operator|=(const EdgeSig& _sig)
//{
//this->edgeBitSet |= _sig.edgeBitSet;
//return *this;
//}
string
EntitySig::to_str() const
{
std::stringstream _ss;
_ss << Signature::BitSet2str(this->entityBitSet);
return _ss.str();
}