| 1 |
#include "graph.h" |
| 2 |
#include "fch.h" |
| 3 |
#include "fch_structs.h" |
| 4 |
#include "bmz8.h" |
| 5 |
#include "bmz8_structs.h" |
| 6 |
#include "brz.h" |
| 7 |
#include "cmph_structs.h" |
| 8 |
#include "brz_structs.h" |
| 9 |
#include "buffer_manager.h" |
| 10 |
#include "cmph.h" |
| 11 |
#include "hash.h" |
| 12 |
#include "bitbool.h" |
| 13 |
#include <math.h> |
| 14 |
#include <stdlib.h> |
| 15 |
#include <stdint.h> |
| 16 |
#include <stdio.h> |
| 17 |
#include <assert.h> |
| 18 |
#include <string.h> |
| 19 |
#include <errno.h> |
| 20 |
#define MAX_BUCKET_SIZE 255 |
| 21 |
//#define DEBUG |
| 22 |
#include "debug.h" |
| 23 |
|
| 24 |
|
| 25 |
static int brz_gen_mphf(cmph_config_t *mph); |
| 26 |
static cmph_uint32 brz_min_index(cmph_uint32 * vector, cmph_uint32 n); |
| 27 |
static void brz_destroy_keys_vd(cmph_uint8 ** keys_vd, cmph_uint32 nkeys); |
| 28 |
static char * brz_copy_partial_fch_mphf(brz_config_data_t *brz, fch_data_t * fchf, cmph_uint32 index, cmph_uint32 *buflen); |
| 29 |
static char * brz_copy_partial_bmz8_mphf(brz_config_data_t *brz, bmz8_data_t * bmzf, cmph_uint32 index, cmph_uint32 *buflen); |
| 30 |
brz_config_data_t *brz_config_new(void) |
| 31 |
{ |
| 32 |
brz_config_data_t *brz = NULL; |
| 33 |
brz = (brz_config_data_t *)malloc(sizeof(brz_config_data_t)); |
| 34 |
brz->algo = CMPH_FCH; |
| 35 |
brz->b = 128; |
| 36 |
brz->hashfuncs[0] = CMPH_HASH_JENKINS; |
| 37 |
brz->hashfuncs[1] = CMPH_HASH_JENKINS; |
| 38 |
brz->hashfuncs[2] = CMPH_HASH_JENKINS; |
| 39 |
brz->size = NULL; |
| 40 |
brz->offset = NULL; |
| 41 |
brz->g = NULL; |
| 42 |
brz->h1 = NULL; |
| 43 |
brz->h2 = NULL; |
| 44 |
brz->h0 = NULL; |
| 45 |
brz->memory_availability = 1024*1024; |
| 46 |
brz->tmp_dir = (cmph_uint8 *)calloc((size_t)10, sizeof(cmph_uint8)); |
| 47 |
brz->mphf_fd = NULL; |
| 48 |
strcpy((char *)(brz->tmp_dir), "/var/tmp/"); |
| 49 |
assert(brz); |
| 50 |
return brz; |
| 51 |
} |
| 52 |
|
| 53 |
void brz_config_destroy(cmph_config_t *mph) |
| 54 |
{ |
| 55 |
brz_config_data_t *data = (brz_config_data_t *)mph->data; |
| 56 |
free(data->tmp_dir); |
| 57 |
DEBUGP("Destroying algorithm dependent data\n"); |
| 58 |
free(data); |
| 59 |
} |
| 60 |
|
| 61 |
void brz_config_set_hashfuncs(cmph_config_t *mph, CMPH_HASH *hashfuncs) |
| 62 |
{ |
| 63 |
brz_config_data_t *brz = (brz_config_data_t *)mph->data; |
| 64 |
CMPH_HASH *hashptr = hashfuncs; |
| 65 |
cmph_uint32 i = 0; |
| 66 |
while(*hashptr != CMPH_HASH_COUNT) |
| 67 |
{ |
| 68 |
if (i >= 3) break; //brz only uses three hash functions |
| 69 |
brz->hashfuncs[i] = *hashptr; |
| 70 |
++i, ++hashptr; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
void brz_config_set_memory_availability(cmph_config_t *mph, cmph_uint32 memory_availability) |
| 75 |
{ |
| 76 |
brz_config_data_t *brz = (brz_config_data_t *)mph->data; |
| 77 |
if(memory_availability > 0) brz->memory_availability = memory_availability*1024*1024; |
| 78 |
} |
| 79 |
|
| 80 |
void brz_config_set_tmp_dir(cmph_config_t *mph, cmph_uint8 *tmp_dir) |
| 81 |
{ |
| 82 |
brz_config_data_t *brz = (brz_config_data_t *)mph->data; |
| 83 |
if(tmp_dir) |
| 84 |
{ |
| 85 |
size_t len = strlen((char *)tmp_dir); |
| 86 |
free(brz->tmp_dir); |
| 87 |
if(tmp_dir[len-1] != '/') |
| 88 |
{ |
| 89 |
brz->tmp_dir = (cmph_uint8 *)calloc((size_t)len+2, sizeof(cmph_uint8)); |
| 90 |
sprintf((char *)(brz->tmp_dir), "%s/", (char *)tmp_dir); |
| 91 |
} |
| 92 |
else |
| 93 |
{ |
| 94 |
brz->tmp_dir = (cmph_uint8 *)calloc((size_t)len+1, sizeof(cmph_uint8)); |
| 95 |
sprintf((char *)(brz->tmp_dir), "%s", (char *)tmp_dir); |
| 96 |
} |
| 97 |
|
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
void brz_config_set_mphf_fd(cmph_config_t *mph, FILE *mphf_fd) |
| 102 |
{ |
| 103 |
brz_config_data_t *brz = (brz_config_data_t *)mph->data; |
| 104 |
brz->mphf_fd = mphf_fd; |
| 105 |
assert(brz->mphf_fd); |
| 106 |
} |
| 107 |
|
| 108 |
void brz_config_set_b(cmph_config_t *mph, cmph_uint32 b) |
| 109 |
{ |
| 110 |
brz_config_data_t *brz = (brz_config_data_t *)mph->data; |
| 111 |
if(b <= 64 || b >= 175) |
| 112 |
{ |
| 113 |
b = 128; |
| 114 |
} |
| 115 |
brz->b = (cmph_uint8)b; |
| 116 |
} |
| 117 |
|
| 118 |
void brz_config_set_algo(cmph_config_t *mph, CMPH_ALGO algo) |
| 119 |
{ |
| 120 |
if (algo == CMPH_BMZ8 || algo == CMPH_FCH) // supported algorithms |
| 121 |
{ |
| 122 |
brz_config_data_t *brz = (brz_config_data_t *)mph->data; |
| 123 |
brz->algo = algo; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
cmph_t *brz_new(cmph_config_t *mph, double c) |
| 128 |
{ |
| 129 |
cmph_t *mphf = NULL; |
| 130 |
brz_data_t *brzf = NULL; |
| 131 |
cmph_uint32 i; |
| 132 |
cmph_uint32 iterations = 20; |
| 133 |
brz_config_data_t *brz; |
| 134 |
|
| 135 |
DEBUGP("c: %f\n", c); |
| 136 |
brz = (brz_config_data_t *)mph->data; |
| 137 |
switch(brz->algo) // validating restrictions over parameter c. |
| 138 |
{ |
| 139 |
case CMPH_BMZ8: |
| 140 |
if (c == 0 || c >= 2.0) c = 1; |
| 141 |
break; |
| 142 |
case CMPH_FCH: |
| 143 |
if (c <= 2.0) c = 2.6; |
| 144 |
break; |
| 145 |
default: |
| 146 |
assert(0); |
| 147 |
} |
| 148 |
brz->c = c; |
| 149 |
brz->m = mph->key_source->nkeys; |
| 150 |
DEBUGP("m: %u\n", brz->m); |
| 151 |
brz->k = (cmph_uint32)ceil(brz->m/((double)brz->b)); |
| 152 |
DEBUGP("k: %u\n", brz->k); |
| 153 |
brz->size = (cmph_uint8 *) calloc((size_t)brz->k, sizeof(cmph_uint8)); |
| 154 |
|
| 155 |
// Clustering the keys by graph id. |
| 156 |
if (mph->verbosity) |
| 157 |
{ |
| 158 |
fprintf(stderr, "Partioning the set of keys.\n"); |
| 159 |
} |
| 160 |
|
| 161 |
while(1) |
| 162 |
{ |
| 163 |
int ok; |
| 164 |
DEBUGP("hash function 3\n"); |
| 165 |
brz->h0 = hash_state_new(brz->hashfuncs[2], brz->k); |
| 166 |
DEBUGP("Generating graphs\n"); |
| 167 |
ok = brz_gen_mphf(mph); |
| 168 |
if (!ok) |
| 169 |
{ |
| 170 |
--iterations; |
| 171 |
hash_state_destroy(brz->h0); |
| 172 |
brz->h0 = NULL; |
| 173 |
DEBUGP("%u iterations remaining to create the graphs in a external file\n", iterations); |
| 174 |
if (mph->verbosity) |
| 175 |
{ |
| 176 |
fprintf(stderr, "Failure: A graph with more than 255 keys was created - %u iterations remaining\n", iterations); |
| 177 |
} |
| 178 |
if (iterations == 0) break; |
| 179 |
} |
| 180 |
else break; |
| 181 |
} |
| 182 |
if (iterations == 0) |
| 183 |
{ |
| 184 |
DEBUGP("Graphs with more than 255 keys were created in all 20 iterations\n"); |
| 185 |
free(brz->size); |
| 186 |
return NULL; |
| 187 |
} |
| 188 |
DEBUGP("Graphs generated\n"); |
| 189 |
|
| 190 |
brz->offset = (cmph_uint32 *)calloc((size_t)brz->k, sizeof(cmph_uint32)); |
| 191 |
for (i = 1; i < brz->k; ++i) |
| 192 |
{ |
| 193 |
brz->offset[i] = brz->size[i-1] + brz->offset[i-1]; |
| 194 |
} |
| 195 |
// Generating a mphf |
| 196 |
mphf = (cmph_t *)malloc(sizeof(cmph_t)); |
| 197 |
mphf->algo = mph->algo; |
| 198 |
brzf = (brz_data_t *)malloc(sizeof(brz_data_t)); |
| 199 |
brzf->g = brz->g; |
| 200 |
brz->g = NULL; //transfer memory ownership |
| 201 |
brzf->h1 = brz->h1; |
| 202 |
brz->h1 = NULL; //transfer memory ownership |
| 203 |
brzf->h2 = brz->h2; |
| 204 |
brz->h2 = NULL; //transfer memory ownership |
| 205 |
brzf->h0 = brz->h0; |
| 206 |
brz->h0 = NULL; //transfer memory ownership |
| 207 |
brzf->size = brz->size; |
| 208 |
brz->size = NULL; //transfer memory ownership |
| 209 |
brzf->offset = brz->offset; |
| 210 |
brz->offset = NULL; //transfer memory ownership |
| 211 |
brzf->k = brz->k; |
| 212 |
brzf->c = brz->c; |
| 213 |
brzf->m = brz->m; |
| 214 |
brzf->algo = brz->algo; |
| 215 |
mphf->data = brzf; |
| 216 |
mphf->size = brz->m; |
| 217 |
DEBUGP("Successfully generated minimal perfect hash\n"); |
| 218 |
if (mph->verbosity) |
| 219 |
{ |
| 220 |
fprintf(stderr, "Successfully generated minimal perfect hash function\n"); |
| 221 |
} |
| 222 |
return mphf; |
| 223 |
} |
| 224 |
|
| 225 |
static int brz_gen_mphf(cmph_config_t *mph) |
| 226 |
{ |
| 227 |
cmph_uint32 i, e, error; |
| 228 |
brz_config_data_t *brz = (brz_config_data_t *)mph->data; |
| 229 |
cmph_uint32 memory_usage = 0; |
| 230 |
cmph_uint32 nkeys_in_buffer = 0; |
| 231 |
cmph_uint8 *buffer = (cmph_uint8 *)malloc((size_t)brz->memory_availability); |
| 232 |
cmph_uint32 *buckets_size = (cmph_uint32 *)calloc((size_t)brz->k, sizeof(cmph_uint32)); |
| 233 |
cmph_uint32 *keys_index = NULL; |
| 234 |
cmph_uint8 **buffer_merge = NULL; |
| 235 |
cmph_uint32 *buffer_h0 = NULL; |
| 236 |
cmph_uint32 nflushes = 0; |
| 237 |
cmph_uint32 h0; |
| 238 |
register size_t nbytes; |
| 239 |
FILE * tmp_fd = NULL; |
| 240 |
buffer_manager_t * buff_manager = NULL; |
| 241 |
char *filename = NULL; |
| 242 |
char *key = NULL; |
| 243 |
cmph_uint32 keylen; |
| 244 |
cmph_uint32 cur_bucket = 0; |
| 245 |
cmph_uint8 nkeys_vd = 0; |
| 246 |
cmph_uint8 ** keys_vd = NULL; |
| 247 |
|
| 248 |
mph->key_source->rewind(mph->key_source->data); |
| 249 |
DEBUGP("Generating graphs from %u keys\n", brz->m); |
| 250 |
// Partitioning |
| 251 |
for (e = 0; e < brz->m; ++e) |
| 252 |
{ |
| 253 |
mph->key_source->read(mph->key_source->data, &key, &keylen); |
| 254 |
|
| 255 |
/* Buffers management */ |
| 256 |
if (memory_usage + keylen + sizeof(keylen) > brz->memory_availability) // flush buffers |
| 257 |
{ |
| 258 |
cmph_uint32 value, sum, keylen1; |
| 259 |
if(mph->verbosity) |
| 260 |
{ |
| 261 |
fprintf(stderr, "Flushing %u\n", nkeys_in_buffer); |
| 262 |
} |
| 263 |
value = buckets_size[0]; |
| 264 |
sum = 0; |
| 265 |
keylen1 = 0; |
| 266 |
buckets_size[0] = 0; |
| 267 |
for(i = 1; i < brz->k; i++) |
| 268 |
{ |
| 269 |
if(buckets_size[i] == 0) continue; |
| 270 |
sum += value; |
| 271 |
value = buckets_size[i]; |
| 272 |
buckets_size[i] = sum; |
| 273 |
|
| 274 |
} |
| 275 |
memory_usage = 0; |
| 276 |
keys_index = (cmph_uint32 *)calloc((size_t)nkeys_in_buffer, sizeof(cmph_uint32)); |
| 277 |
for(i = 0; i < nkeys_in_buffer; i++) |
| 278 |
{ |
| 279 |
memcpy(&keylen1, buffer + memory_usage, sizeof(keylen1)); |
| 280 |
h0 = hash(brz->h0, (char *)(buffer + memory_usage + sizeof(keylen1)), keylen1) % brz->k; |
| 281 |
keys_index[buckets_size[h0]] = memory_usage; |
| 282 |
buckets_size[h0]++; |
| 283 |
memory_usage += keylen1 + (cmph_uint32)sizeof(keylen1); |
| 284 |
} |
| 285 |
filename = (char *)calloc(strlen((char *)(brz->tmp_dir)) + 11, sizeof(char)); |
| 286 |
sprintf(filename, "%s%u.cmph",brz->tmp_dir, nflushes); |
| 287 |
tmp_fd = fopen(filename, "wb"); |
| 288 |
free(filename); |
| 289 |
filename = NULL; |
| 290 |
for(i = 0; i < nkeys_in_buffer; i++) |
| 291 |
{ |
| 292 |
memcpy(&keylen1, buffer + keys_index[i], sizeof(keylen1)); |
| 293 |
nbytes = fwrite(buffer + keys_index[i], (size_t)1, keylen1 + sizeof(keylen1), tmp_fd); |
| 294 |
} |
| 295 |
nkeys_in_buffer = 0; |
| 296 |
memory_usage = 0; |
| 297 |
memset((void *)buckets_size, 0, brz->k*sizeof(cmph_uint32)); |
| 298 |
nflushes++; |
| 299 |
free(keys_index); |
| 300 |
fclose(tmp_fd); |
| 301 |
} |
| 302 |
memcpy(buffer + memory_usage, &keylen, sizeof(keylen)); |
| 303 |
memcpy(buffer + memory_usage + sizeof(keylen), key, (size_t)keylen); |
| 304 |
memory_usage += keylen + (cmph_uint32)sizeof(keylen); |
| 305 |
h0 = hash(brz->h0, key, keylen) % brz->k; |
| 306 |
|
| 307 |
if ((brz->size[h0] == MAX_BUCKET_SIZE) || (brz->algo == CMPH_BMZ8 && ((brz->c >= 1.0) && (cmph_uint8)(brz->c * brz->size[h0]) < brz->size[h0]))) |
| 308 |
{ |
| 309 |
free(buffer); |
| 310 |
free(buckets_size); |
| 311 |
return 0; |
| 312 |
} |
| 313 |
brz->size[h0] = (cmph_uint8)(brz->size[h0] + 1U); |
| 314 |
buckets_size[h0] ++; |
| 315 |
nkeys_in_buffer++; |
| 316 |
mph->key_source->dispose(mph->key_source->data, key, keylen); |
| 317 |
} |
| 318 |
if (memory_usage != 0) // flush buffers |
| 319 |
{ |
| 320 |
cmph_uint32 value; |
| 321 |
cmph_uint32 sum, keylen1; |
| 322 |
if(mph->verbosity) |
| 323 |
{ |
| 324 |
fprintf(stderr, "Flushing %u\n", nkeys_in_buffer); |
| 325 |
} |
| 326 |
value = buckets_size[0]; |
| 327 |
sum = 0; |
| 328 |
keylen1 = 0; |
| 329 |
buckets_size[0] = 0; |
| 330 |
for(i = 1; i < brz->k; i++) |
| 331 |
{ |
| 332 |
if(buckets_size[i] == 0) continue; |
| 333 |
sum += value; |
| 334 |
value = buckets_size[i]; |
| 335 |
buckets_size[i] = sum; |
| 336 |
} |
| 337 |
memory_usage = 0; |
| 338 |
keys_index = (cmph_uint32 *)calloc((size_t)nkeys_in_buffer, sizeof(cmph_uint32)); |
| 339 |
for(i = 0; i < nkeys_in_buffer; i++) |
| 340 |
{ |
| 341 |
memcpy(&keylen1, buffer + memory_usage, sizeof(keylen1)); |
| 342 |
h0 = hash(brz->h0, (char *)(buffer + memory_usage + sizeof(keylen1)), keylen1) % brz->k; |
| 343 |
keys_index[buckets_size[h0]] = memory_usage; |
| 344 |
buckets_size[h0]++; |
| 345 |
memory_usage += keylen1 + (cmph_uint32)sizeof(keylen1); |
| 346 |
} |
| 347 |
filename = (char *)calloc(strlen((char *)(brz->tmp_dir)) + 11, sizeof(char)); |
| 348 |
sprintf(filename, "%s%u.cmph",brz->tmp_dir, nflushes); |
| 349 |
tmp_fd = fopen(filename, "wb"); |
| 350 |
free(filename); |
| 351 |
filename = NULL; |
| 352 |
for(i = 0; i < nkeys_in_buffer; i++) |
| 353 |
{ |
| 354 |
memcpy(&keylen1, buffer + keys_index[i], sizeof(keylen1)); |
| 355 |
nbytes = fwrite(buffer + keys_index[i], (size_t)1, keylen1 + sizeof(keylen1), tmp_fd); |
| 356 |
} |
| 357 |
nkeys_in_buffer = 0; |
| 358 |
memory_usage = 0; |
| 359 |
memset((void *)buckets_size, 0, brz->k*sizeof(cmph_uint32)); |
| 360 |
nflushes++; |
| 361 |
free(keys_index); |
| 362 |
fclose(tmp_fd); |
| 363 |
} |
| 364 |
|
| 365 |
free(buffer); |
| 366 |
free(buckets_size); |
| 367 |
if(nflushes > 1024) return 0; // Too many files generated. |
| 368 |
// mphf generation |
| 369 |
if(mph->verbosity) |
| 370 |
{ |
| 371 |
fprintf(stderr, "\nMPHF generation \n"); |
| 372 |
} |
| 373 |
/* Starting to dump to disk the resultant MPHF: __cmph_dump function */ |
| 374 |
nbytes = fwrite(cmph_names[CMPH_BRZ], (size_t)(strlen(cmph_names[CMPH_BRZ]) + 1), (size_t)1, brz->mphf_fd); |
| 375 |
nbytes = fwrite(&(brz->m), sizeof(brz->m), (size_t)1, brz->mphf_fd); |
| 376 |
nbytes = fwrite(&(brz->c), sizeof(double), (size_t)1, brz->mphf_fd); |
| 377 |
nbytes = fwrite(&(brz->algo), sizeof(brz->algo), (size_t)1, brz->mphf_fd); |
| 378 |
nbytes = fwrite(&(brz->k), sizeof(cmph_uint32), (size_t)1, brz->mphf_fd); // number of MPHFs |
| 379 |
nbytes = fwrite(brz->size, sizeof(cmph_uint8)*(brz->k), (size_t)1, brz->mphf_fd); |
| 380 |
if (nbytes == 0 && ferror(brz->mphf_fd)) { |
| 381 |
fprintf(stderr, "ERROR: %s\n", strerror(errno)); |
| 382 |
return 0; |
| 383 |
} |
| 384 |
|
| 385 |
//tmp_fds = (FILE **)calloc(nflushes, sizeof(FILE *)); |
| 386 |
buff_manager = buffer_manager_new(brz->memory_availability, nflushes); |
| 387 |
buffer_merge = (cmph_uint8 **)calloc((size_t)nflushes, sizeof(cmph_uint8 *)); |
| 388 |
buffer_h0 = (cmph_uint32 *)calloc((size_t)nflushes, sizeof(cmph_uint32)); |
| 389 |
|
| 390 |
memory_usage = 0; |
| 391 |
for(i = 0; i < nflushes; i++) |
| 392 |
{ |
| 393 |
filename = (char *)calloc(strlen((char *)(brz->tmp_dir)) + 11, sizeof(char)); |
| 394 |
sprintf(filename, "%s%u.cmph",brz->tmp_dir, i); |
| 395 |
buffer_manager_open(buff_manager, i, filename); |
| 396 |
free(filename); |
| 397 |
filename = NULL; |
| 398 |
key = (char *)buffer_manager_read_key(buff_manager, i, &keylen); |
| 399 |
h0 = hash(brz->h0, key+sizeof(keylen), keylen) % brz->k; |
| 400 |
buffer_h0[i] = h0; |
| 401 |
buffer_merge[i] = (cmph_uint8 *)key; |
| 402 |
key = NULL; //transfer memory ownership |
| 403 |
} |
| 404 |
e = 0; |
| 405 |
keys_vd = (cmph_uint8 **)calloc((size_t)MAX_BUCKET_SIZE, sizeof(cmph_uint8 *)); |
| 406 |
nkeys_vd = 0; |
| 407 |
error = 0; |
| 408 |
while(e < brz->m) |
| 409 |
{ |
| 410 |
i = brz_min_index(buffer_h0, nflushes); |
| 411 |
cur_bucket = buffer_h0[i]; |
| 412 |
key = (char *)buffer_manager_read_key(buff_manager, i, &keylen); |
| 413 |
if(key) |
| 414 |
{ |
| 415 |
while(key) |
| 416 |
{ |
| 417 |
//keylen = strlen(key); |
| 418 |
h0 = hash(brz->h0, key+sizeof(keylen), keylen) % brz->k; |
| 419 |
if (h0 != buffer_h0[i]) break; |
| 420 |
keys_vd[nkeys_vd++] = (cmph_uint8 *)key; |
| 421 |
key = NULL; //transfer memory ownership |
| 422 |
e++; |
| 423 |
key = (char *)buffer_manager_read_key(buff_manager, i, &keylen); |
| 424 |
} |
| 425 |
if (key) |
| 426 |
{ |
| 427 |
assert(nkeys_vd < brz->size[cur_bucket]); |
| 428 |
keys_vd[nkeys_vd++] = buffer_merge[i]; |
| 429 |
buffer_merge[i] = NULL; //transfer memory ownership |
| 430 |
e++; |
| 431 |
buffer_h0[i] = h0; |
| 432 |
buffer_merge[i] = (cmph_uint8 *)key; |
| 433 |
} |
| 434 |
} |
| 435 |
if(!key) |
| 436 |
{ |
| 437 |
assert(nkeys_vd < brz->size[cur_bucket]); |
| 438 |
keys_vd[nkeys_vd++] = buffer_merge[i]; |
| 439 |
buffer_merge[i] = NULL; //transfer memory ownership |
| 440 |
e++; |
| 441 |
buffer_h0[i] = UINT_MAX; |
| 442 |
} |
| 443 |
|
| 444 |
if(nkeys_vd == brz->size[cur_bucket]) // Generating mphf for each bucket. |
| 445 |
{ |
| 446 |
cmph_io_adapter_t *source = NULL; |
| 447 |
cmph_config_t *config = NULL; |
| 448 |
cmph_t *mphf_tmp = NULL; |
| 449 |
char *bufmphf = NULL; |
| 450 |
cmph_uint32 buflenmphf = 0; |
| 451 |
// Source of keys |
| 452 |
source = cmph_io_byte_vector_adapter(keys_vd, (cmph_uint32)nkeys_vd); |
| 453 |
config = cmph_config_new(source); |
| 454 |
cmph_config_set_algo(config, brz->algo); |
| 455 |
//cmph_config_set_algo(config, CMPH_BMZ8); |
| 456 |
cmph_config_set_graphsize(config, brz->c); |
| 457 |
mphf_tmp = cmph_new(config); |
| 458 |
if (mphf_tmp == NULL) |
| 459 |
{ |
| 460 |
if(mph->verbosity) fprintf(stderr, "ERROR: Can't generate MPHF for bucket %u out of %u\n", cur_bucket + 1, brz->k); |
| 461 |
error = 1; |
| 462 |
cmph_config_destroy(config); |
| 463 |
brz_destroy_keys_vd(keys_vd, nkeys_vd); |
| 464 |
cmph_io_byte_vector_adapter_destroy(source); |
| 465 |
break; |
| 466 |
} |
| 467 |
if(mph->verbosity) |
| 468 |
{ |
| 469 |
if (cur_bucket % 1000 == 0) |
| 470 |
{ |
| 471 |
fprintf(stderr, "MPHF for bucket %u out of %u was generated.\n", cur_bucket + 1, brz->k); |
| 472 |
} |
| 473 |
} |
| 474 |
switch(brz->algo) |
| 475 |
{ |
| 476 |
case CMPH_FCH: |
| 477 |
{ |
| 478 |
fch_data_t * fchf = NULL; |
| 479 |
fchf = (fch_data_t *)mphf_tmp->data; |
| 480 |
bufmphf = brz_copy_partial_fch_mphf(brz, fchf, cur_bucket, &buflenmphf); |
| 481 |
} |
| 482 |
break; |
| 483 |
case CMPH_BMZ8: |
| 484 |
{ |
| 485 |
bmz8_data_t * bmzf = NULL; |
| 486 |
bmzf = (bmz8_data_t *)mphf_tmp->data; |
| 487 |
bufmphf = brz_copy_partial_bmz8_mphf(brz, bmzf, cur_bucket, &buflenmphf); |
| 488 |
} |
| 489 |
break; |
| 490 |
default: assert(0); |
| 491 |
} |
| 492 |
nbytes = fwrite(bufmphf, (size_t)buflenmphf, (size_t)1, brz->mphf_fd); |
| 493 |
free(bufmphf); |
| 494 |
bufmphf = NULL; |
| 495 |
cmph_config_destroy(config); |
| 496 |
brz_destroy_keys_vd(keys_vd, nkeys_vd); |
| 497 |
cmph_destroy(mphf_tmp); |
| 498 |
cmph_io_byte_vector_adapter_destroy(source); |
| 499 |
nkeys_vd = 0; |
| 500 |
} |
| 501 |
} |
| 502 |
buffer_manager_destroy(buff_manager); |
| 503 |
free(keys_vd); |
| 504 |
free(buffer_merge); |
| 505 |
free(buffer_h0); |
| 506 |
if (error) return 0; |
| 507 |
return 1; |
| 508 |
} |
| 509 |
|
| 510 |
static cmph_uint32 brz_min_index(cmph_uint32 * vector, cmph_uint32 n) |
| 511 |
{ |
| 512 |
cmph_uint32 i, min_index = 0; |
| 513 |
for(i = 1; i < n; i++) |
| 514 |
{ |
| 515 |
if(vector[i] < vector[min_index]) min_index = i; |
| 516 |
} |
| 517 |
return min_index; |
| 518 |
} |
| 519 |
|
| 520 |
static void brz_destroy_keys_vd(cmph_uint8 ** keys_vd, cmph_uint32 nkeys) |
| 521 |
{ |
| 522 |
cmph_uint8 i; |
| 523 |
for(i = 0; i < nkeys; i++) { free(keys_vd[i]); keys_vd[i] = NULL;} |
| 524 |
} |
| 525 |
|
| 526 |
static char * brz_copy_partial_fch_mphf(brz_config_data_t *brz, fch_data_t * fchf, cmph_uint32 index, cmph_uint32 *buflen) |
| 527 |
{ |
| 528 |
cmph_uint32 i = 0; |
| 529 |
cmph_uint32 buflenh1 = 0; |
| 530 |
cmph_uint32 buflenh2 = 0; |
| 531 |
char * bufh1 = NULL; |
| 532 |
char * bufh2 = NULL; |
| 533 |
char * buf = NULL; |
| 534 |
cmph_uint32 n = fchf->b;//brz->size[index]; |
| 535 |
hash_state_dump(fchf->h1, &bufh1, &buflenh1); |
| 536 |
hash_state_dump(fchf->h2, &bufh2, &buflenh2); |
| 537 |
*buflen = buflenh1 + buflenh2 + n + 2U * (cmph_uint32)sizeof(cmph_uint32); |
| 538 |
buf = (char *)malloc((size_t)(*buflen)); |
| 539 |
memcpy(buf, &buflenh1, sizeof(cmph_uint32)); |
| 540 |
memcpy(buf+sizeof(cmph_uint32), bufh1, (size_t)buflenh1); |
| 541 |
memcpy(buf+sizeof(cmph_uint32)+buflenh1, &buflenh2, sizeof(cmph_uint32)); |
| 542 |
memcpy(buf+2*sizeof(cmph_uint32)+buflenh1, bufh2, (size_t)buflenh2); |
| 543 |
for (i = 0; i < n; i++) memcpy(buf+2*sizeof(cmph_uint32)+buflenh1+buflenh2+i,(fchf->g + i), (size_t)1); |
| 544 |
free(bufh1); |
| 545 |
free(bufh2); |
| 546 |
return buf; |
| 547 |
} |
| 548 |
static char * brz_copy_partial_bmz8_mphf(brz_config_data_t *brz, bmz8_data_t * bmzf, cmph_uint32 index, cmph_uint32 *buflen) |
| 549 |
{ |
| 550 |
cmph_uint32 buflenh1 = 0; |
| 551 |
cmph_uint32 buflenh2 = 0; |
| 552 |
char * bufh1 = NULL; |
| 553 |
char * bufh2 = NULL; |
| 554 |
char * buf = NULL; |
| 555 |
cmph_uint32 n = (cmph_uint32)ceil(brz->c * brz->size[index]); |
| 556 |
hash_state_dump(bmzf->hashes[0], &bufh1, &buflenh1); |
| 557 |
hash_state_dump(bmzf->hashes[1], &bufh2, &buflenh2); |
| 558 |
*buflen = buflenh1 + buflenh2 + n + 2U * (cmph_uint32)sizeof(cmph_uint32); |
| 559 |
buf = (char *)malloc((size_t)(*buflen)); |
| 560 |
memcpy(buf, &buflenh1, sizeof(cmph_uint32)); |
| 561 |
memcpy(buf+sizeof(cmph_uint32), bufh1, (size_t)buflenh1); |
| 562 |
memcpy(buf+sizeof(cmph_uint32)+buflenh1, &buflenh2, sizeof(cmph_uint32)); |
| 563 |
memcpy(buf+2*sizeof(cmph_uint32)+buflenh1, bufh2, (size_t)buflenh2); |
| 564 |
memcpy(buf+2*sizeof(cmph_uint32)+buflenh1+buflenh2,bmzf->g, (size_t)n); |
| 565 |
free(bufh1); |
| 566 |
free(bufh2); |
| 567 |
return buf; |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
int brz_dump(cmph_t *mphf, FILE *fd) |
| 572 |
{ |
| 573 |
brz_data_t *data = (brz_data_t *)mphf->data; |
| 574 |
char *buf = NULL; |
| 575 |
cmph_uint32 buflen; |
| 576 |
register size_t nbytes; |
| 577 |
DEBUGP("Dumping brzf\n"); |
| 578 |
// The initial part of the MPHF have already been dumped to disk during construction |
| 579 |
// Dumping h0 |
| 580 |
hash_state_dump(data->h0, &buf, &buflen); |
| 581 |
DEBUGP("Dumping hash state with %u bytes to disk\n", buflen); |
| 582 |
nbytes = fwrite(&buflen, sizeof(cmph_uint32), (size_t)1, fd); |
| 583 |
nbytes = fwrite(buf, (size_t)buflen, (size_t)1, fd); |
| 584 |
free(buf); |
| 585 |
// Dumping m and the vector offset. |
| 586 |
nbytes = fwrite(&(data->m), sizeof(cmph_uint32), (size_t)1, fd); |
| 587 |
nbytes = fwrite(data->offset, sizeof(cmph_uint32)*(data->k), (size_t)1, fd); |
| 588 |
if (nbytes == 0 && ferror(fd)) { |
| 589 |
fprintf(stderr, "ERROR: %s\n", strerror(errno)); |
| 590 |
return 0; |
| 591 |
} |
| 592 |
return 1; |
| 593 |
} |
| 594 |
|
| 595 |
void brz_load(FILE *f, cmph_t *mphf) |
| 596 |
{ |
| 597 |
char *buf = NULL; |
| 598 |
cmph_uint32 buflen; |
| 599 |
register size_t nbytes; |
| 600 |
cmph_uint32 i, n; |
| 601 |
brz_data_t *brz = (brz_data_t *)malloc(sizeof(brz_data_t)); |
| 602 |
|
| 603 |
DEBUGP("Loading brz mphf\n"); |
| 604 |
mphf->data = brz; |
| 605 |
nbytes = fread(&(brz->c), sizeof(double), (size_t)1, f); |
| 606 |
nbytes = fread(&(brz->algo), sizeof(brz->algo), (size_t)1, f); // Reading algo. |
| 607 |
nbytes = fread(&(brz->k), sizeof(cmph_uint32), (size_t)1, f); |
| 608 |
brz->size = (cmph_uint8 *) malloc(sizeof(cmph_uint8)*brz->k); |
| 609 |
nbytes = fread(brz->size, sizeof(cmph_uint8)*(brz->k), (size_t)1, f); |
| 610 |
brz->h1 = (hash_state_t **)malloc(sizeof(hash_state_t *)*brz->k); |
| 611 |
brz->h2 = (hash_state_t **)malloc(sizeof(hash_state_t *)*brz->k); |
| 612 |
brz->g = (cmph_uint8 **) calloc((size_t)brz->k, sizeof(cmph_uint8 *)); |
| 613 |
DEBUGP("Reading c = %f k = %u algo = %u \n", brz->c, brz->k, brz->algo); |
| 614 |
//loading h_i1, h_i2 and g_i. |
| 615 |
for(i = 0; i < brz->k; i++) |
| 616 |
{ |
| 617 |
// h1 |
| 618 |
nbytes = fread(&buflen, sizeof(cmph_uint32), (size_t)1, f); |
| 619 |
DEBUGP("Hash state 1 has %u bytes\n", buflen); |
| 620 |
buf = (char *)malloc((size_t)buflen); |
| 621 |
nbytes = fread(buf, (size_t)buflen, (size_t)1, f); |
| 622 |
brz->h1[i] = hash_state_load(buf, buflen); |
| 623 |
free(buf); |
| 624 |
//h2 |
| 625 |
nbytes = fread(&buflen, sizeof(cmph_uint32), (size_t)1, f); |
| 626 |
DEBUGP("Hash state 2 has %u bytes\n", buflen); |
| 627 |
buf = (char *)malloc((size_t)buflen); |
| 628 |
nbytes = fread(buf, (size_t)buflen, (size_t)1, f); |
| 629 |
brz->h2[i] = hash_state_load(buf, buflen); |
| 630 |
free(buf); |
| 631 |
switch(brz->algo) |
| 632 |
{ |
| 633 |
case CMPH_FCH: |
| 634 |
n = fch_calc_b(brz->c, brz->size[i]); |
| 635 |
break; |
| 636 |
case CMPH_BMZ8: |
| 637 |
n = (cmph_uint32)ceil(brz->c * brz->size[i]); |
| 638 |
break; |
| 639 |
default: assert(0); |
| 640 |
} |
| 641 |
DEBUGP("g_i has %u bytes\n", n); |
| 642 |
brz->g[i] = (cmph_uint8 *)calloc((size_t)n, sizeof(cmph_uint8)); |
| 643 |
nbytes = fread(brz->g[i], sizeof(cmph_uint8)*n, (size_t)1, f); |
| 644 |
} |
| 645 |
//loading h0 |
| 646 |
nbytes = fread(&buflen, sizeof(cmph_uint32), (size_t)1, f); |
| 647 |
DEBUGP("Hash state has %u bytes\n", buflen); |
| 648 |
buf = (char *)malloc((size_t)buflen); |
| 649 |
nbytes = fread(buf, (size_t)buflen, (size_t)1, f); |
| 650 |
brz->h0 = hash_state_load(buf, buflen); |
| 651 |
free(buf); |
| 652 |
|
| 653 |
//loading c, m, and the vector offset. |
| 654 |
nbytes = fread(&(brz->m), sizeof(cmph_uint32), (size_t)1, f); |
| 655 |
brz->offset = (cmph_uint32 *)malloc(sizeof(cmph_uint32)*brz->k); |
| 656 |
nbytes = fread(brz->offset, sizeof(cmph_uint32)*(brz->k), (size_t)1, f); |
| 657 |
if (nbytes == 0 && ferror(f)) { |
| 658 |
fprintf(stderr, "ERROR: %s\n", strerror(errno)); |
| 659 |
} |
| 660 |
|
| 661 |
return; |
| 662 |
} |
| 663 |
|
| 664 |
static cmph_uint32 brz_bmz8_search(brz_data_t *brz, const char *key, cmph_uint32 keylen, cmph_uint32 * fingerprint) |
| 665 |
{ |
| 666 |
register cmph_uint32 h0; |
| 667 |
register cmph_uint32 m, n, h1, h2; |
| 668 |
register cmph_uint8 mphf_bucket; |
| 669 |
|
| 670 |
hash_vector(brz->h0, key, keylen, fingerprint); |
| 671 |
h0 = fingerprint[2] % brz->k; |
| 672 |
|
| 673 |
m = brz->size[h0]; |
| 674 |
n = (cmph_uint32)ceil(brz->c * m); |
| 675 |
h1 = hash(brz->h1[h0], key, keylen) % n; |
| 676 |
h2 = hash(brz->h2[h0], key, keylen) % n; |
| 677 |
|
| 678 |
if (h1 == h2 && ++h2 >= n) h2 = 0; |
| 679 |
mphf_bucket = (cmph_uint8)(brz->g[h0][h1] + brz->g[h0][h2]); |
| 680 |
DEBUGP("key: %s h1: %u h2: %u h0: %u\n", key, h1, h2, h0); |
| 681 |
DEBUGP("key: %s g[h1]: %u g[h2]: %u offset[h0]: %u edges: %u\n", key, brz->g[h0][h1], brz->g[h0][h2], brz->offset[h0], brz->m); |
| 682 |
DEBUGP("Address: %u\n", mphf_bucket + brz->offset[h0]); |
| 683 |
return (mphf_bucket + brz->offset[h0]); |
| 684 |
} |
| 685 |
|
| 686 |
static cmph_uint32 brz_fch_search(brz_data_t *brz, const char *key, cmph_uint32 keylen, cmph_uint32 * fingerprint) |
| 687 |
{ |
| 688 |
register cmph_uint32 h0; |
| 689 |
register cmph_uint32 m, b, h1, h2; |
| 690 |
register double p1, p2; |
| 691 |
register cmph_uint8 mphf_bucket; |
| 692 |
|
| 693 |
hash_vector(brz->h0, key, keylen, fingerprint); |
| 694 |
h0 = fingerprint[2] % brz->k; |
| 695 |
|
| 696 |
m = brz->size[h0]; |
| 697 |
b = fch_calc_b(brz->c, m); |
| 698 |
p1 = fch_calc_p1(m); |
| 699 |
p2 = fch_calc_p2(b); |
| 700 |
h1 = hash(brz->h1[h0], key, keylen) % m; |
| 701 |
h2 = hash(brz->h2[h0], key, keylen) % m; |
| 702 |
mphf_bucket = 0; |
| 703 |
h1 = mixh10h11h12(b, p1, p2, h1); |
| 704 |
mphf_bucket = (cmph_uint8)((h2 + brz->g[h0][h1]) % m); |
| 705 |
return (mphf_bucket + brz->offset[h0]); |
| 706 |
} |
| 707 |
|
| 708 |
cmph_uint32 brz_search(cmph_t *mphf, const char *key, cmph_uint32 keylen) |
| 709 |
{ |
| 710 |
brz_data_t *brz = mphf->data; |
| 711 |
cmph_uint32 fingerprint[3]; |
| 712 |
switch(brz->algo) |
| 713 |
{ |
| 714 |
case CMPH_FCH: |
| 715 |
return brz_fch_search(brz, key, keylen, fingerprint); |
| 716 |
case CMPH_BMZ8: |
| 717 |
return brz_bmz8_search(brz, key, keylen, fingerprint); |
| 718 |
default: assert(0); |
| 719 |
} |
| 720 |
return 0; |
| 721 |
} |
| 722 |
void brz_destroy(cmph_t *mphf) |
| 723 |
{ |
| 724 |
cmph_uint32 i; |
| 725 |
brz_data_t *data = (brz_data_t *)mphf->data; |
| 726 |
if(data->g) |
| 727 |
{ |
| 728 |
for(i = 0; i < data->k; i++) |
| 729 |
{ |
| 730 |
free(data->g[i]); |
| 731 |
hash_state_destroy(data->h1[i]); |
| 732 |
hash_state_destroy(data->h2[i]); |
| 733 |
} |
| 734 |
free(data->g); |
| 735 |
free(data->h1); |
| 736 |
free(data->h2); |
| 737 |
} |
| 738 |
hash_state_destroy(data->h0); |
| 739 |
free(data->size); |
| 740 |
free(data->offset); |
| 741 |
free(data); |
| 742 |
free(mphf); |
| 743 |
} |
| 744 |
|
| 745 |
/** \fn void brz_pack(cmph_t *mphf, void *packed_mphf); |
| 746 |
* \brief Support the ability to pack a perfect hash function into a preallocated contiguous memory space pointed by packed_mphf. |
| 747 |
* \param mphf pointer to the resulting mphf |
| 748 |
* \param packed_mphf pointer to the contiguous memory area used to store the resulting mphf. The size of packed_mphf must be at least cmph_packed_size() |
| 749 |
*/ |
| 750 |
void brz_pack(cmph_t *mphf, void *packed_mphf) |
| 751 |
{ |
| 752 |
brz_data_t *data = (brz_data_t *)mphf->data; |
| 753 |
cmph_uint8 * ptr = packed_mphf; |
| 754 |
cmph_uint32 i,n; |
| 755 |
CMPH_HASH h0_type, h1_type, h2_type; |
| 756 |
uintptr_t *g_is_ptr; |
| 757 |
cmph_uint8 * g_i; |
| 758 |
|
| 759 |
// packing internal algo type |
| 760 |
memcpy(ptr, &(data->algo), sizeof(data->algo)); |
| 761 |
ptr += sizeof(data->algo); |
| 762 |
|
| 763 |
// packing h0 type |
| 764 |
h0_type = hash_get_type(data->h0); |
| 765 |
memcpy(ptr, &h0_type, sizeof(h0_type)); |
| 766 |
ptr += sizeof(h0_type); |
| 767 |
|
| 768 |
// packing h0 |
| 769 |
hash_state_pack(data->h0, ptr); |
| 770 |
ptr += hash_state_packed_size(h0_type); |
| 771 |
|
| 772 |
// packing k |
| 773 |
memcpy(ptr, &(data->k), sizeof(data->k)); |
| 774 |
ptr += sizeof(data->k); |
| 775 |
|
| 776 |
// packing c |
| 777 |
*((cmph_uint64 *)ptr) = (cmph_uint64)data->c; |
| 778 |
ptr += sizeof(data->c); |
| 779 |
|
| 780 |
// packing h1 type |
| 781 |
h1_type = hash_get_type(data->h1[0]); |
| 782 |
memcpy(ptr, &h1_type, sizeof(h1_type)); |
| 783 |
ptr += sizeof(h1_type); |
| 784 |
|
| 785 |
// packing h2 type |
| 786 |
h2_type = hash_get_type(data->h2[0]); |
| 787 |
memcpy(ptr, &h2_type, sizeof(h2_type)); |
| 788 |
ptr += sizeof(h2_type); |
| 789 |
|
| 790 |
// packing size |
| 791 |
memcpy(ptr, data->size, sizeof(cmph_uint8)*data->k); |
| 792 |
ptr += data->k; |
| 793 |
|
| 794 |
// packing offset |
| 795 |
memcpy(ptr, data->offset, sizeof(cmph_uint32)*data->k); |
| 796 |
ptr += sizeof(cmph_uint32)*data->k; |
| 797 |
|
| 798 |
g_is_ptr = (uintptr_t *)ptr; |
| 799 |
|
| 800 |
g_i = (cmph_uint8 *) (g_is_ptr + data->k); |
| 801 |
|
| 802 |
for(i = 0; i < data->k; i++) |
| 803 |
{ |
| 804 |
*g_is_ptr++ = (uintptr_t)g_i; |
| 805 |
// packing h1[i] |
| 806 |
hash_state_pack(data->h1[i], g_i); |
| 807 |
g_i += hash_state_packed_size(h1_type); |
| 808 |
|
| 809 |
// packing h2[i] |
| 810 |
hash_state_pack(data->h2[i], g_i); |
| 811 |
g_i += hash_state_packed_size(h2_type); |
| 812 |
|
| 813 |
// packing g_i |
| 814 |
switch(data->algo) |
| 815 |
{ |
| 816 |
case CMPH_FCH: |
| 817 |
n = fch_calc_b(data->c, data->size[i]); |
| 818 |
break; |
| 819 |
case CMPH_BMZ8: |
| 820 |
n = (cmph_uint32)ceil(data->c * data->size[i]); |
| 821 |
break; |
| 822 |
default: assert(0); |
| 823 |
} |
| 824 |
memcpy(g_i, data->g[i], sizeof(cmph_uint8)*n); |
| 825 |
g_i += n; |
| 826 |
|
| 827 |
} |
| 828 |
|
| 829 |
} |
| 830 |
|
| 831 |
/** \fn cmph_uint32 brz_packed_size(cmph_t *mphf); |
| 832 |
* \brief Return the amount of space needed to pack mphf. |
| 833 |
* \param mphf pointer to a mphf |
| 834 |
* \return the size of the packed function or zero for failures |
| 835 |
*/ |
| 836 |
cmph_uint32 brz_packed_size(cmph_t *mphf) |
| 837 |
{ |
| 838 |
cmph_uint32 i; |
| 839 |
cmph_uint32 size = 0; |
| 840 |
brz_data_t *data = (brz_data_t *)mphf->data; |
| 841 |
CMPH_HASH h0_type = hash_get_type(data->h0); |
| 842 |
CMPH_HASH h1_type = hash_get_type(data->h1[0]); |
| 843 |
CMPH_HASH h2_type = hash_get_type(data->h2[0]); |
| 844 |
cmph_uint32 n; |
| 845 |
size = (cmph_uint32)(2*sizeof(CMPH_ALGO) + 3*sizeof(CMPH_HASH) + hash_state_packed_size(h0_type) + sizeof(cmph_uint32) + |
| 846 |
sizeof(double) + sizeof(cmph_uint8)*data->k + sizeof(cmph_uint32)*data->k); |
| 847 |
// pointers to g_is |
| 848 |
size += (cmph_uint32) sizeof(uintptr_t) * data->k; |
| 849 |
size += hash_state_packed_size(h1_type) * data->k; |
| 850 |
size += hash_state_packed_size(h2_type) * data->k; |
| 851 |
|
| 852 |
n = 0; |
| 853 |
for(i = 0; i < data->k; i++) |
| 854 |
{ |
| 855 |
switch(data->algo) |
| 856 |
{ |
| 857 |
case CMPH_FCH: |
| 858 |
n = fch_calc_b(data->c, data->size[i]); |
| 859 |
break; |
| 860 |
case CMPH_BMZ8: |
| 861 |
n = (cmph_uint32)ceil(data->c * data->size[i]); |
| 862 |
break; |
| 863 |
default: assert(0); |
| 864 |
} |
| 865 |
size += n; |
| 866 |
} |
| 867 |
return size; |
| 868 |
} |
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
static cmph_uint32 brz_bmz8_search_packed(cmph_uint32 *packed_mphf, const char *key, cmph_uint32 keylen, cmph_uint32 * fingerprint) |
| 873 |
{ |
| 874 |
register CMPH_HASH h0_type = *packed_mphf++; |
| 875 |
register cmph_uint32 *h0_ptr = packed_mphf; |
| 876 |
register cmph_uint32 k, h0, m, n, h1, h2; |
| 877 |
register cmph_uint32 *offset; |
| 878 |
register double c; |
| 879 |
register CMPH_HASH h1_type, h2_type; |
| 880 |
register cmph_uint8 * size; |
| 881 |
register uintptr_t *g_is_ptr; |
| 882 |
register cmph_uint8 *h1_ptr, *h2_ptr, *g; |
| 883 |
register cmph_uint8 mphf_bucket; |
| 884 |
|
| 885 |
packed_mphf = (cmph_uint32 *)(((cmph_uint8 *)packed_mphf) + hash_state_packed_size(h0_type)); |
| 886 |
|
| 887 |
k = *packed_mphf++; |
| 888 |
|
| 889 |
c = (double)(*((cmph_uint64*)packed_mphf)); |
| 890 |
packed_mphf += 2; |
| 891 |
|
| 892 |
h1_type = *packed_mphf++; |
| 893 |
|
| 894 |
h2_type = *packed_mphf++; |
| 895 |
|
| 896 |
size = (cmph_uint8 *) packed_mphf; |
| 897 |
packed_mphf = (cmph_uint32 *)(size + k); |
| 898 |
|
| 899 |
offset = packed_mphf; |
| 900 |
packed_mphf += k; |
| 901 |
|
| 902 |
|
| 903 |
hash_vector_packed(h0_ptr, h0_type, key, keylen, fingerprint); |
| 904 |
h0 = fingerprint[2] % k; |
| 905 |
|
| 906 |
m = size[h0]; |
| 907 |
n = (cmph_uint32)ceil(c * m); |
| 908 |
|
| 909 |
g_is_ptr = (uintptr_t *)packed_mphf; |
| 910 |
|
| 911 |
h1_ptr = (cmph_uint8 *) g_is_ptr[h0]; |
| 912 |
|
| 913 |
h2_ptr = h1_ptr + hash_state_packed_size(h1_type); |
| 914 |
|
| 915 |
g = h2_ptr + hash_state_packed_size(h2_type); |
| 916 |
|
| 917 |
h1 = hash_packed(h1_ptr, h1_type, key, keylen) % n; |
| 918 |
h2 = hash_packed(h2_ptr, h2_type, key, keylen) % n; |
| 919 |
|
| 920 |
if (h1 == h2 && ++h2 >= n) h2 = 0; |
| 921 |
mphf_bucket = (cmph_uint8)(g[h1] + g[h2]); |
| 922 |
DEBUGP("key: %s h1: %u h2: %u h0: %u\n", key, h1, h2, h0); |
| 923 |
DEBUGP("Address: %u\n", mphf_bucket + offset[h0]); |
| 924 |
return (mphf_bucket + offset[h0]); |
| 925 |
} |
| 926 |
|
| 927 |
static cmph_uint32 brz_fch_search_packed(cmph_uint32 *packed_mphf, const char *key, cmph_uint32 keylen, cmph_uint32 * fingerprint) |
| 928 |
{ |
| 929 |
register CMPH_HASH h0_type = *packed_mphf++; |
| 930 |
|
| 931 |
register cmph_uint32 *h0_ptr = packed_mphf; |
| 932 |
register cmph_uint32 k, h0, m, b, h1, h2; |
| 933 |
register double c, p1, p2; |
| 934 |
register CMPH_HASH h1_type, h2_type; |
| 935 |
register cmph_uint8 *size, *h1_ptr, *h2_ptr, *g; |
| 936 |
register cmph_uint32 *offset; |
| 937 |
register uintptr_t *g_is_ptr; |
| 938 |
register cmph_uint8 mphf_bucket; |
| 939 |
|
| 940 |
packed_mphf = (cmph_uint32 *)(((cmph_uint8 *)packed_mphf) + hash_state_packed_size(h0_type)); |
| 941 |
|
| 942 |
k = *packed_mphf++; |
| 943 |
|
| 944 |
c = (double)(*((cmph_uint64*)packed_mphf)); |
| 945 |
packed_mphf += 2; |
| 946 |
|
| 947 |
h1_type = *packed_mphf++; |
| 948 |
|
| 949 |
h2_type = *packed_mphf++; |
| 950 |
|
| 951 |
size = (cmph_uint8 *) packed_mphf; |
| 952 |
packed_mphf = (cmph_uint32 *)(size + k); |
| 953 |
|
| 954 |
offset = packed_mphf; |
| 955 |
packed_mphf += k; |
| 956 |
|
| 957 |
hash_vector_packed(h0_ptr, h0_type, key, keylen, fingerprint); |
| 958 |
h0 = fingerprint[2] % k; |
| 959 |
|
| 960 |
m = size[h0]; |
| 961 |
b = fch_calc_b(c, m); |
| 962 |
p1 = fch_calc_p1(m); |
| 963 |
p2 = fch_calc_p2(b); |
| 964 |
|
| 965 |
g_is_ptr = (uintptr_t *)packed_mphf; |
| 966 |
|
| 967 |
h1_ptr = (cmph_uint8 *) g_is_ptr[h0]; |
| 968 |
|
| 969 |
h2_ptr = h1_ptr + hash_state_packed_size(h1_type); |
| 970 |
|
| 971 |
g = h2_ptr + hash_state_packed_size(h2_type); |
| 972 |
|
| 973 |
h1 = hash_packed(h1_ptr, h1_type, key, keylen) % m; |
| 974 |
h2 = hash_packed(h2_ptr, h2_type, key, keylen) % m; |
| 975 |
|
| 976 |
mphf_bucket = 0; |
| 977 |
h1 = mixh10h11h12(b, p1, p2, h1); |
| 978 |
mphf_bucket = (cmph_uint8)((h2 + g[h1]) % m); |
| 979 |
return (mphf_bucket + offset[h0]); |
| 980 |
} |
| 981 |
|
| 982 |
/** cmph_uint32 brz_search(void *packed_mphf, const char *key, cmph_uint32 keylen); |
| 983 |
* \brief Use the packed mphf to do a search. |
| 984 |
* \param packed_mphf pointer to the packed mphf |
| 985 |
* \param key key to be hashed |
| 986 |
* \param keylen key legth in bytes |
| 987 |
* \return The mphf value |
| 988 |
*/ |
| 989 |
cmph_uint32 brz_search_packed(void *packed_mphf, const char *key, cmph_uint32 keylen) |
| 990 |
{ |
| 991 |
register cmph_uint32 *ptr = (cmph_uint32 *)packed_mphf; |
| 992 |
register CMPH_ALGO algo = *ptr++; |
| 993 |
cmph_uint32 fingerprint[3]; |
| 994 |
switch(algo) |
| 995 |
{ |
| 996 |
case CMPH_FCH: |
| 997 |
return brz_fch_search_packed(ptr, key, keylen, fingerprint); |
| 998 |
case CMPH_BMZ8: |
| 999 |
return brz_bmz8_search_packed(ptr, key, keylen, fingerprint); |
| 1000 |
default: |
| 1001 |
assert(0); |
| 1002 |
return 0; /* To avoid warnings that value must be returned */ |
| 1003 |
} |
| 1004 |
} |
| 1005 |
|