1
Fork 0
turbonss/src/brz.c

592 lines
17 KiB
C
Raw Normal View History

2005-07-28 00:13:02 +03:00
#include "graph.h"
2005-09-06 17:11:37 +03:00
#include "bmz8.h"
#include "bmz8_structs.h"
2005-07-28 00:13:02 +03:00
#include "brz.h"
#include "cmph_structs.h"
#include "brz_structs.h"
#include "buffer_manager.h"
2005-07-28 00:13:02 +03:00
#include "cmph.h"
#include "hash.h"
#include "bitbool.h"
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
2005-08-07 03:45:39 +03:00
#define MAX_BUCKET_SIZE 255
2005-07-29 21:29:30 +03:00
//#define DEBUG
2005-07-28 00:13:02 +03:00
#include "debug.h"
static int brz_gen_mphf(cmph_config_t *mph);
2005-08-06 23:20:04 +03:00
static cmph_uint32 brz_min_index(cmph_uint32 * vector, cmph_uint32 n);
2005-07-28 00:13:02 +03:00
static void brz_destroy_keys_vd(char ** keys_vd, cmph_uint8 nkeys);
static char * brz_copy_partial_mphf(brz_config_data_t *brz, bmz8_data_t * bmzf, cmph_uint32 index, cmph_uint32 *buflen);
2005-07-28 00:13:02 +03:00
brz_config_data_t *brz_config_new()
{
brz_config_data_t *brz = NULL;
brz = (brz_config_data_t *)malloc(sizeof(brz_config_data_t));
brz->b = 128;
2005-07-28 00:13:02 +03:00
brz->hashfuncs[0] = CMPH_HASH_JENKINS;
brz->hashfuncs[1] = CMPH_HASH_JENKINS;
brz->hashfuncs[2] = CMPH_HASH_JENKINS;
brz->size = NULL;
brz->offset = NULL;
brz->g = NULL;
brz->h1 = NULL;
brz->h2 = NULL;
brz->h0 = NULL;
2005-09-06 17:11:37 +03:00
brz->memory_availability = 1024*1024;
brz->tmp_dir = (cmph_uint8 *)calloc(10, sizeof(cmph_uint8));
brz->mphf_fd = NULL;
strcpy((char *)(brz->tmp_dir), "/var/tmp/");
2005-07-28 00:13:02 +03:00
assert(brz);
return brz;
}
void brz_config_destroy(cmph_config_t *mph)
{
brz_config_data_t *data = (brz_config_data_t *)mph->data;
free(data->tmp_dir);
2005-07-28 00:13:02 +03:00
DEBUGP("Destroying algorithm dependent data\n");
free(data);
}
void brz_config_set_hashfuncs(cmph_config_t *mph, CMPH_HASH *hashfuncs)
{
brz_config_data_t *brz = (brz_config_data_t *)mph->data;
CMPH_HASH *hashptr = hashfuncs;
cmph_uint32 i = 0;
while(*hashptr != CMPH_HASH_COUNT)
{
if (i >= 3) break; //brz only uses three hash functions
brz->hashfuncs[i] = *hashptr;
++i, ++hashptr;
}
}
2005-09-06 17:11:37 +03:00
void brz_config_set_memory_availability(cmph_config_t *mph, cmph_uint32 memory_availability)
{
brz_config_data_t *brz = (brz_config_data_t *)mph->data;
if(memory_availability > 0) brz->memory_availability = memory_availability*1024*1024;
2005-09-06 17:11:37 +03:00
}
void brz_config_set_tmp_dir(cmph_config_t *mph, cmph_uint8 *tmp_dir)
2005-07-29 21:29:30 +03:00
{
brz_config_data_t *brz = (brz_config_data_t *)mph->data;
if(tmp_dir)
2005-07-29 21:29:30 +03:00
{
cmph_uint32 len = strlen((char *)tmp_dir);
free(brz->tmp_dir);
if(tmp_dir[len-1] != '/')
2005-07-29 21:29:30 +03:00
{
brz->tmp_dir = calloc(len+2, sizeof(cmph_uint8));
sprintf((char *)(brz->tmp_dir), "%s/", (char *)tmp_dir);
2005-07-29 21:29:30 +03:00
}
else
2005-07-29 21:29:30 +03:00
{
brz->tmp_dir = calloc(len+1, sizeof(cmph_uint8));
sprintf((char *)(brz->tmp_dir), "%s", (char *)tmp_dir);
2005-07-29 21:29:30 +03:00
}
2005-07-29 21:29:30 +03:00
}
}
2005-07-28 00:13:02 +03:00
void brz_config_set_mphf_fd(cmph_config_t *mph, FILE *mphf_fd)
{
brz_config_data_t *brz = (brz_config_data_t *)mph->data;
brz->mphf_fd = mphf_fd;
assert(brz->mphf_fd);
}
void brz_config_set_b(cmph_config_t *mph, cmph_uint8 b)
{
brz_config_data_t *brz = (brz_config_data_t *)mph->data;
brz->b = b;
}
2005-07-28 00:13:02 +03:00
cmph_t *brz_new(cmph_config_t *mph, float c)
{
cmph_t *mphf = NULL;
brz_data_t *brzf = NULL;
cmph_uint32 i;
cmph_uint32 iterations = 20;
2005-08-07 03:45:39 +03:00
DEBUGP("c: %f\n");
2005-07-28 00:13:02 +03:00
brz_config_data_t *brz = (brz_config_data_t *)mph->data;
2005-08-07 03:45:39 +03:00
brz->c = c;
brz->m = mph->key_source->nkeys;
2005-07-29 03:00:01 +03:00
DEBUGP("m: %u\n", brz->m);
brz->k = ceil(brz->m/(brz->b));
2005-07-29 03:00:01 +03:00
DEBUGP("k: %u\n", brz->k);
2005-08-06 23:20:04 +03:00
brz->size = (cmph_uint8 *) calloc(brz->k, sizeof(cmph_uint8));
2005-07-28 00:13:02 +03:00
2005-08-06 23:20:04 +03:00
// Clustering the keys by graph id.
if (mph->verbosity)
2005-07-28 00:13:02 +03:00
{
2005-08-06 23:20:04 +03:00
fprintf(stderr, "Partioning the set of keys.\n");
}
2005-07-28 00:13:02 +03:00
while(1)
{
int ok;
DEBUGP("hash function 3\n");
brz->h0 = hash_state_new(brz->hashfuncs[2], brz->k);
2005-07-28 00:13:02 +03:00
DEBUGP("Generating graphs\n");
ok = brz_gen_mphf(mph);
2005-07-28 00:13:02 +03:00
if (!ok)
{
--iterations;
hash_state_destroy(brz->h0);
brz->h0 = NULL;
2005-07-28 00:13:02 +03:00
DEBUGP("%u iterations remaining to create the graphs in a external file\n", iterations);
if (mph->verbosity)
{
fprintf(stderr, "Failure: A graph with more than 255 keys was created - %u iterations remaining\n", iterations);
}
if (iterations == 0) break;
}
else break;
}
if (iterations == 0)
{
DEBUGP("Graphs with more than 255 keys were created in all 20 iterations\n");
free(brz->size);
return NULL;
}
2005-08-06 23:20:04 +03:00
DEBUGP("Graphs generated\n");
brz->offset = (cmph_uint32 *)calloc(brz->k, sizeof(cmph_uint32));
for (i = 1; i < brz->k; ++i)
2005-07-28 00:13:02 +03:00
{
2005-08-06 23:20:04 +03:00
brz->offset[i] = brz->size[i-1] + brz->offset[i-1];
2005-07-28 00:13:02 +03:00
}
// Generating a mphf
mphf = (cmph_t *)malloc(sizeof(cmph_t));
mphf->algo = mph->algo;
brzf = (brz_data_t *)malloc(sizeof(brz_data_t));
brzf->g = brz->g;
brz->g = NULL; //transfer memory ownership
brzf->h1 = brz->h1;
brz->h1 = NULL; //transfer memory ownership
brzf->h2 = brz->h2;
brz->h2 = NULL; //transfer memory ownership
brzf->h0 = brz->h0;
brz->h0 = NULL; //transfer memory ownership
2005-07-28 00:13:02 +03:00
brzf->size = brz->size;
brz->size = NULL; //transfer memory ownership
brzf->offset = brz->offset;
brz->offset = NULL; //transfer memory ownership
brzf->k = brz->k;
2005-07-29 21:29:30 +03:00
brzf->c = brz->c;
brzf->m = brz->m;
2005-07-28 00:13:02 +03:00
mphf->data = brzf;
mphf->size = brz->m;
DEBUGP("Successfully generated minimal perfect hash\n");
if (mph->verbosity)
{
fprintf(stderr, "Successfully generated minimal perfect hash function\n");
}
return mphf;
}
static int brz_gen_mphf(cmph_config_t *mph)
2005-07-28 00:13:02 +03:00
{
2005-08-06 23:20:04 +03:00
cmph_uint32 i, e;
2005-07-28 00:13:02 +03:00
brz_config_data_t *brz = (brz_config_data_t *)mph->data;
2005-08-06 23:20:04 +03:00
cmph_uint32 memory_usage = 0;
cmph_uint32 nkeys_in_buffer = 0;
2005-09-06 17:11:37 +03:00
cmph_uint8 *buffer = (cmph_uint8 *)malloc(brz->memory_availability);
cmph_uint32 *buckets_size = (cmph_uint32 *)calloc(brz->k, sizeof(cmph_uint32));
2005-08-06 23:20:04 +03:00
cmph_uint32 *keys_index = NULL;
cmph_uint8 **buffer_merge = NULL;
cmph_uint32 *buffer_h0 = NULL;
2005-08-06 23:20:04 +03:00
cmph_uint32 nflushes = 0;
cmph_uint32 h0;
2005-08-06 23:20:04 +03:00
FILE * tmp_fd = NULL;
buffer_manager_t * buff_manager = NULL;
char *filename = NULL;
2005-08-06 23:20:04 +03:00
char *key = NULL;
cmph_uint32 keylen;
2005-08-07 03:45:39 +03:00
cmph_uint32 cur_bucket = 0;
cmph_uint8 nkeys_vd = 0;
char ** keys_vd = NULL;
2005-07-28 00:13:02 +03:00
mph->key_source->rewind(mph->key_source->data);
2005-08-06 23:20:04 +03:00
DEBUGP("Generating graphs from %u keys\n", brz->m);
// Partitioning
2005-07-28 00:13:02 +03:00
for (e = 0; e < brz->m; ++e)
{
mph->key_source->read(mph->key_source->data, &key, &keylen);
2005-08-07 03:45:39 +03:00
/* Buffers management */
2005-09-06 17:11:37 +03:00
if (memory_usage + keylen + 1 > brz->memory_availability) // flush buffers
{
2005-08-07 03:45:39 +03:00
if(mph->verbosity)
{
fprintf(stderr, "Flushing %u\n", nkeys_in_buffer);
}
2005-08-06 23:20:04 +03:00
cmph_uint32 value = buckets_size[0];
cmph_uint32 sum = 0;
cmph_uint32 keylen1 = 0;
buckets_size[0] = 0;
2005-08-06 23:20:04 +03:00
for(i = 1; i < brz->k; i++)
{
if(buckets_size[i] == 0) continue;
sum += value;
value = buckets_size[i];
buckets_size[i] = sum;
}
memory_usage = 0;
keys_index = (cmph_uint32 *)calloc(nkeys_in_buffer, sizeof(cmph_uint32));
for(i = 0; i < nkeys_in_buffer; i++)
{
keylen1 = strlen((char *)(buffer + memory_usage));
h0 = hash(brz->h0, (char *)(buffer + memory_usage), keylen1) % brz->k;
keys_index[buckets_size[h0]] = memory_usage;
buckets_size[h0]++;
2005-08-06 23:20:04 +03:00
memory_usage = memory_usage + keylen1 + 1;
}
filename = (char *)calloc(strlen((char *)(brz->tmp_dir)) + 11, sizeof(char));
sprintf(filename, "%s%u.cmph",brz->tmp_dir, nflushes);
2005-08-06 23:20:04 +03:00
tmp_fd = fopen(filename, "wb");
free(filename);
filename = NULL;
2005-08-06 23:20:04 +03:00
for(i = 0; i < nkeys_in_buffer; i++)
{
keylen1 = strlen((char *)(buffer + keys_index[i])) + 1;
2005-08-06 23:20:04 +03:00
fwrite(buffer + keys_index[i], 1, keylen1, tmp_fd);
}
nkeys_in_buffer = 0;
memory_usage = 0;
bzero(buckets_size, brz->k*sizeof(cmph_uint32));
nflushes++;
free(keys_index);
fclose(tmp_fd);
}
memcpy(buffer + memory_usage, key, keylen + 1);
memory_usage = memory_usage + keylen + 1;
h0 = hash(brz->h0, key, keylen) % brz->k;
if ((brz->size[h0] == MAX_BUCKET_SIZE) || ((brz->c >= 1.0) && (cmph_uint8)(brz->c * brz->size[h0]) < brz->size[h0]))
2005-08-06 23:20:04 +03:00
{
free(buffer);
free(buckets_size);
return 0;
}
brz->size[h0] = brz->size[h0] + 1;
buckets_size[h0] ++;
2005-08-06 23:20:04 +03:00
nkeys_in_buffer++;
2005-07-28 00:13:02 +03:00
mph->key_source->dispose(mph->key_source->data, key, keylen);
2005-08-06 23:20:04 +03:00
}
2005-07-29 03:00:01 +03:00
2005-08-06 23:20:04 +03:00
if (memory_usage != 0) // flush buffers
{
2005-08-07 03:45:39 +03:00
if(mph->verbosity)
{
fprintf(stderr, "Flushing %u\n", nkeys_in_buffer);
}
2005-08-06 23:20:04 +03:00
cmph_uint32 value = buckets_size[0];
cmph_uint32 sum = 0;
cmph_uint32 keylen1 = 0;
buckets_size[0] = 0;
for(i = 1; i < brz->k; i++)
{
if(buckets_size[i] == 0) continue;
sum += value;
value = buckets_size[i];
buckets_size[i] = sum;
}
memory_usage = 0;
keys_index = (cmph_uint32 *)calloc(nkeys_in_buffer, sizeof(cmph_uint32));
for(i = 0; i < nkeys_in_buffer; i++)
{
keylen1 = strlen((char *)(buffer + memory_usage));
h0 = hash(brz->h0, (char *)(buffer + memory_usage), keylen1) % brz->k;
keys_index[buckets_size[h0]] = memory_usage;
buckets_size[h0]++;
2005-08-06 23:20:04 +03:00
memory_usage = memory_usage + keylen1 + 1;
}
filename = (char *)calloc(strlen((char *)(brz->tmp_dir)) + 11, sizeof(char));
sprintf(filename, "%s%u.cmph",brz->tmp_dir, nflushes);
2005-08-06 23:20:04 +03:00
tmp_fd = fopen(filename, "wb");
free(filename);
filename = NULL;
2005-08-06 23:20:04 +03:00
for(i = 0; i < nkeys_in_buffer; i++)
{
keylen1 = strlen((char *)(buffer + keys_index[i])) + 1;
2005-08-06 23:20:04 +03:00
fwrite(buffer + keys_index[i], 1, keylen1, tmp_fd);
}
nkeys_in_buffer = 0;
memory_usage = 0;
bzero(buckets_size, brz->k*sizeof(cmph_uint32));
nflushes++;
free(keys_index);
fclose(tmp_fd);
2005-07-28 00:13:02 +03:00
}
2005-08-06 23:20:04 +03:00
free(buffer);
free(buckets_size);
if(nflushes > 1024) return 0; // Too many files generated.
2005-08-07 03:45:39 +03:00
// mphf generation
if(mph->verbosity)
{
fprintf(stderr, "\nMPHF generation \n");
}
/* Starting to dump to disk the resultant MPHF: __cmph_dump function */
fwrite(cmph_names[CMPH_BRZ], (cmph_uint32)(strlen(cmph_names[CMPH_BRZ]) + 1), 1, brz->mphf_fd);
fwrite(&(brz->m), sizeof(brz->m), 1, brz->mphf_fd);
fwrite(&(brz->c), sizeof(cmph_float32), 1, brz->mphf_fd);
fwrite(&(brz->k), sizeof(cmph_uint32), 1, brz->mphf_fd); // number of MPHFs
fwrite(brz->size, sizeof(cmph_uint8)*(brz->k), 1, brz->mphf_fd);
//tmp_fds = (FILE **)calloc(nflushes, sizeof(FILE *));
buff_manager = buffer_manager_new(brz->memory_availability, nflushes);
2005-08-06 23:20:04 +03:00
buffer_merge = (cmph_uint8 **)calloc(nflushes, sizeof(cmph_uint8 *));
buffer_h0 = (cmph_uint32 *)calloc(nflushes, sizeof(cmph_uint32));
memory_usage = 0;
2005-08-06 23:20:04 +03:00
for(i = 0; i < nflushes; i++)
2005-07-28 00:13:02 +03:00
{
filename = (char *)calloc(strlen((char *)(brz->tmp_dir)) + 11, sizeof(char));
sprintf(filename, "%s%u.cmph",brz->tmp_dir, i);
buffer_manager_open(buff_manager, i, filename);
free(filename);
filename = NULL;
key = (char *)buffer_manager_read_key(buff_manager, i);
2005-08-06 23:20:04 +03:00
keylen = strlen(key);
h0 = hash(brz->h0, key, keylen) % brz->k;
buffer_h0[i] = h0;
2005-08-06 23:20:04 +03:00
buffer_merge[i] = (cmph_uint8 *)calloc(keylen + 1, sizeof(cmph_uint8));
memcpy(buffer_merge[i], key, keylen + 1);
free(key);
2005-07-28 00:13:02 +03:00
}
2005-08-06 23:20:04 +03:00
e = 0;
2005-08-07 03:45:39 +03:00
keys_vd = (char **)calloc(MAX_BUCKET_SIZE, sizeof(char *));
nkeys_vd = 0;
2005-08-06 23:20:04 +03:00
while(e < brz->m)
{
i = brz_min_index(buffer_h0, nflushes);
cur_bucket = buffer_h0[i];
key = (char *)buffer_manager_read_key(buff_manager, i);
2005-08-06 23:20:04 +03:00
if(key)
{
while(key)
{
keylen = strlen(key);
h0 = hash(brz->h0, key, keylen) % brz->k;
if (h0 != buffer_h0[i]) break;
2005-08-07 03:45:39 +03:00
keys_vd[nkeys_vd++] = key;
key = NULL; //transfer memory ownership
2005-08-06 23:20:04 +03:00
e++;
key = (char *)buffer_manager_read_key(buff_manager, i);
2005-08-06 23:20:04 +03:00
}
if (key)
{
2005-08-07 03:45:39 +03:00
assert(nkeys_vd < brz->size[cur_bucket]);
keys_vd[nkeys_vd++] = (char *)buffer_merge[i];
buffer_merge[i] = NULL; //transfer memory ownership
2005-08-06 23:20:04 +03:00
e++;
buffer_h0[i] = h0;
2005-08-06 23:20:04 +03:00
buffer_merge[i] = (cmph_uint8 *)calloc(keylen + 1, sizeof(cmph_uint8));
memcpy(buffer_merge[i], key, keylen + 1);
free(key);
}
}
if(!key)
{
2005-08-07 03:45:39 +03:00
assert(nkeys_vd < brz->size[cur_bucket]);
keys_vd[nkeys_vd++] = (char *)buffer_merge[i];
buffer_merge[i] = NULL; //transfer memory ownership
2005-08-06 23:20:04 +03:00
e++;
buffer_h0[i] = UINT_MAX;
2005-08-06 23:20:04 +03:00
}
2005-08-07 03:45:39 +03:00
2005-09-06 17:11:37 +03:00
if(nkeys_vd == brz->size[cur_bucket]) // Generating mphf for each bucket.
2005-08-07 03:45:39 +03:00
{
cmph_io_adapter_t *source = NULL;
cmph_config_t *config = NULL;
cmph_t *mphf_tmp = NULL;
2005-09-06 17:11:37 +03:00
bmz8_data_t * bmzf = NULL;
char *bufmphf = NULL;
cmph_uint32 buflenmphf = 0;
2005-08-07 03:45:39 +03:00
// Source of keys
source = cmph_io_vector_adapter(keys_vd, (cmph_uint32)nkeys_vd);
config = cmph_config_new(source);
2005-09-06 17:11:37 +03:00
cmph_config_set_algo(config, CMPH_BMZ8);
2005-08-07 03:45:39 +03:00
cmph_config_set_graphsize(config, brz->c);
mphf_tmp = cmph_new(config);
2005-09-06 17:11:37 +03:00
bmzf = (bmz8_data_t *)mphf_tmp->data;
bufmphf = brz_copy_partial_mphf(brz, bmzf, cur_bucket, &buflenmphf);
bmzf = NULL;
fwrite(bufmphf, buflenmphf, 1, brz->mphf_fd);
free(bufmphf);
bufmphf = NULL;
2005-08-07 03:45:39 +03:00
cmph_config_destroy(config);
brz_destroy_keys_vd(keys_vd, nkeys_vd);
2005-08-07 03:45:39 +03:00
cmph_destroy(mphf_tmp);
cmph_io_vector_adapter_destroy(source);
2005-08-07 03:45:39 +03:00
nkeys_vd = 0;
}
2005-08-06 23:20:04 +03:00
}
buffer_manager_destroy(buff_manager);
2005-08-07 03:45:39 +03:00
free(keys_vd);
2005-08-06 23:20:04 +03:00
free(buffer_merge);
free(buffer_h0);
2005-07-28 00:13:02 +03:00
return 1;
}
2005-08-06 23:20:04 +03:00
static cmph_uint32 brz_min_index(cmph_uint32 * vector, cmph_uint32 n)
{
cmph_uint32 i, min_index = 0;
for(i = 1; i < n; i++)
{
if(vector[i] < vector[min_index]) min_index = i;
}
return min_index;
}
2005-07-28 00:13:02 +03:00
static void brz_destroy_keys_vd(char ** keys_vd, cmph_uint8 nkeys)
{
cmph_uint8 i;
for(i = 0; i < nkeys; i++) { free(keys_vd[i]); keys_vd[i] = NULL;}
2005-07-28 00:13:02 +03:00
}
static char * brz_copy_partial_mphf(brz_config_data_t *brz, bmz8_data_t * bmzf, cmph_uint32 index, cmph_uint32 *buflen)
2005-07-28 00:13:02 +03:00
{
cmph_uint32 buflenh1 = 0;
cmph_uint32 buflenh2 = 0;
char * bufh1 = NULL;
char * bufh2 = NULL;
char * buf = NULL;
2005-07-29 21:29:30 +03:00
cmph_uint32 n = ceil(brz->c * brz->size[index]);
hash_state_dump(bmzf->hashes[0], &bufh1, &buflenh1);
hash_state_dump(bmzf->hashes[1], &bufh2, &buflenh2);
*buflen = buflenh1 + buflenh2 + n + 2*sizeof(cmph_uint32);
buf = (char *)malloc(*buflen);
//fprintf(stderr,"entrei passei\n");
memcpy(buf, &buflenh1, sizeof(cmph_uint32));
memcpy(buf+sizeof(cmph_uint32), bufh1, buflenh1);
memcpy(buf+sizeof(cmph_uint32)+buflenh1, &buflenh2, sizeof(cmph_uint32));
memcpy(buf+2*sizeof(cmph_uint32)+buflenh1, bufh2, buflenh2);
memcpy(buf+2*sizeof(cmph_uint32)+buflenh1+buflenh2,bmzf->g, n);
free(bufh1);
free(bufh2);
return buf;
}
2005-07-28 00:13:02 +03:00
int brz_dump(cmph_t *mphf, FILE *fd)
{
brz_data_t *data = (brz_data_t *)mphf->data;
2005-07-29 21:29:30 +03:00
char *buf = NULL;
2005-07-28 00:13:02 +03:00
cmph_uint32 buflen;
2005-07-29 21:29:30 +03:00
DEBUGP("Dumping brzf\n");
// The initial part of the MPHF have already been dumped to disk during construction
// Dumping h0
hash_state_dump(data->h0, &buf, &buflen);
DEBUGP("Dumping hash state with %u bytes to disk\n", buflen);
fwrite(&buflen, sizeof(cmph_uint32), 1, fd);
fwrite(buf, buflen, 1, fd);
free(buf);
// Dumping m and the vector offset.
fwrite(&(data->m), sizeof(cmph_uint32), 1, fd);
2005-07-29 21:29:30 +03:00
fwrite(data->offset, sizeof(cmph_uint32)*(data->k), 1, fd);
2005-07-28 00:13:02 +03:00
return 1;
}
2005-07-28 00:13:02 +03:00
void brz_load(FILE *f, cmph_t *mphf)
{
char *buf = NULL;
cmph_uint32 buflen;
cmph_uint32 i, n;
2005-07-28 00:13:02 +03:00
brz_data_t *brz = (brz_data_t *)malloc(sizeof(brz_data_t));
DEBUGP("Loading brz mphf\n");
mphf->data = brz;
fread(&(brz->c), sizeof(cmph_float32), 1, f);
2005-07-29 21:29:30 +03:00
fread(&(brz->k), sizeof(cmph_uint32), 1, f);
brz->size = (cmph_uint8 *) malloc(sizeof(cmph_uint8)*brz->k);
fread(brz->size, sizeof(cmph_uint8)*(brz->k), 1, f);
2005-07-29 21:29:30 +03:00
brz->h1 = (hash_state_t **)malloc(sizeof(hash_state_t *)*brz->k);
brz->h2 = (hash_state_t **)malloc(sizeof(hash_state_t *)*brz->k);
brz->g = (cmph_uint8 **) calloc(brz->k, sizeof(cmph_uint8 *));
2005-07-29 21:29:30 +03:00
DEBUGP("Reading %u h1 and %u h2\n", brz->k, brz->k);
//loading h_i1, h_i2 and g_i.
2005-07-29 21:29:30 +03:00
for(i = 0; i < brz->k; i++)
2005-07-28 00:13:02 +03:00
{
2005-07-29 21:29:30 +03:00
// h1
2005-07-28 00:13:02 +03:00
fread(&buflen, sizeof(cmph_uint32), 1, f);
DEBUGP("Hash state has %u bytes\n", buflen);
buf = (char *)malloc(buflen);
fread(buf, buflen, 1, f);
2005-07-29 21:29:30 +03:00
brz->h1[i] = hash_state_load(buf, buflen);
2005-07-28 00:13:02 +03:00
free(buf);
2005-07-29 21:29:30 +03:00
//h2
fread(&buflen, sizeof(cmph_uint32), 1, f);
DEBUGP("Hash state has %u bytes\n", buflen);
buf = (char *)malloc(buflen);
fread(buf, buflen, 1, f);
brz->h2[i] = hash_state_load(buf, buflen);
free(buf);
n = ceil(brz->c * brz->size[i]);
DEBUGP("g_i has %u bytes\n", n);
brz->g[i] = (cmph_uint8 *)calloc(n, sizeof(cmph_uint8));
fread(brz->g[i], sizeof(cmph_uint8)*n, 1, f);
2005-07-28 00:13:02 +03:00
}
//loading h0
2005-07-29 21:29:30 +03:00
fread(&buflen, sizeof(cmph_uint32), 1, f);
DEBUGP("Hash state has %u bytes\n", buflen);
buf = (char *)malloc(buflen);
fread(buf, buflen, 1, f);
brz->h0 = hash_state_load(buf, buflen);
free(buf);
2005-07-28 00:13:02 +03:00
//loading c, m, and the vector offset.
2005-07-29 21:29:30 +03:00
fread(&(brz->m), sizeof(cmph_uint32), 1, f);
brz->offset = (cmph_uint32 *)malloc(sizeof(cmph_uint32)*brz->k);
fread(brz->offset, sizeof(cmph_uint32)*(brz->k), 1, f);
2005-07-28 00:13:02 +03:00
return;
}
cmph_uint32 brz_search(cmph_t *mphf, const char *key, cmph_uint32 keylen)
{
2005-07-29 21:29:30 +03:00
brz_data_t *brz = mphf->data;
cmph_uint32 h0 = hash(brz->h0, key, keylen) % brz->k;
cmph_uint32 m = brz->size[h0];
2005-07-29 21:29:30 +03:00
cmph_uint32 n = ceil(brz->c * m);
cmph_uint32 h1 = hash(brz->h1[h0], key, keylen) % n;
cmph_uint32 h2 = hash(brz->h2[h0], key, keylen) % n;
2005-09-06 17:11:37 +03:00
cmph_uint8 mphf_bucket;
2005-07-29 21:29:30 +03:00
if (h1 == h2 && ++h2 >= n) h2 = 0;
mphf_bucket = brz->g[h0][h1] + brz->g[h0][h2];
DEBUGP("key: %s h1: %u h2: %u h0: %u\n", key, h1, h2, h0);
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);
DEBUGP("Address: %u\n", mphf_bucket + brz->offset[h0]);
return (mphf_bucket + brz->offset[h0]);
2005-07-28 00:13:02 +03:00
}
void brz_destroy(cmph_t *mphf)
{
cmph_uint32 i;
brz_data_t *data = (brz_data_t *)mphf->data;
if(data->g)
2005-07-28 00:13:02 +03:00
{
for(i = 0; i < data->k; i++)
{
free(data->g[i]);
hash_state_destroy(data->h1[i]);
hash_state_destroy(data->h2[i]);
}
free(data->g);
free(data->h1);
free(data->h2);
2005-07-28 00:13:02 +03:00
}
hash_state_destroy(data->h0);
2005-07-28 00:13:02 +03:00
free(data->size);
free(data->offset);
free(data);
free(mphf);
}