Home | History | Annotate | Line # | Download | only in cdb
cdbw.c revision 1.3
      1  1.2  christos /*	$NetBSD: cdbw.c,v 1.3 2012/03/13 21:32:12 joerg Exp $	*/
      2  1.1     joerg /*-
      3  1.1     joerg  * Copyright (c) 2009, 2010 The NetBSD Foundation, Inc.
      4  1.1     joerg  * All rights reserved.
      5  1.1     joerg  *
      6  1.1     joerg  * This code is derived from software contributed to The NetBSD Foundation
      7  1.1     joerg  * by Joerg Sonnenberger.
      8  1.1     joerg  *
      9  1.1     joerg  * Redistribution and use in source and binary forms, with or without
     10  1.1     joerg  * modification, are permitted provided that the following conditions
     11  1.1     joerg  * are met:
     12  1.1     joerg  *
     13  1.1     joerg  * 1. Redistributions of source code must retain the above copyright
     14  1.1     joerg  *    notice, this list of conditions and the following disclaimer.
     15  1.1     joerg  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1     joerg  *    notice, this list of conditions and the following disclaimer in
     17  1.1     joerg  *    the documentation and/or other materials provided with the
     18  1.1     joerg  *    distribution.
     19  1.1     joerg  *
     20  1.1     joerg  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  1.1     joerg  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     22  1.1     joerg  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     23  1.1     joerg  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
     24  1.1     joerg  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  1.1     joerg  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
     26  1.1     joerg  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     27  1.1     joerg  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     28  1.1     joerg  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     29  1.1     joerg  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     30  1.1     joerg  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1     joerg  * SUCH DAMAGE.
     32  1.1     joerg  */
     33  1.1     joerg 
     34  1.1     joerg #if HAVE_NBTOOL_CONFIG_H
     35  1.1     joerg #include "nbtool_config.h"
     36  1.1     joerg #endif
     37  1.1     joerg 
     38  1.1     joerg #include <sys/cdefs.h>
     39  1.2  christos __RCSID("$NetBSD: cdbw.c,v 1.3 2012/03/13 21:32:12 joerg Exp $");
     40  1.1     joerg 
     41  1.1     joerg #include "namespace.h"
     42  1.1     joerg 
     43  1.1     joerg #include <sys/endian.h>
     44  1.1     joerg #include <sys/queue.h>
     45  1.1     joerg #include <cdbw.h>
     46  1.1     joerg #include <stdlib.h>
     47  1.1     joerg #include <string.h>
     48  1.1     joerg #include <unistd.h>
     49  1.1     joerg 
     50  1.1     joerg #ifdef __weak_alias
     51  1.1     joerg __weak_alias(cdbw_close,_cdbw_close)
     52  1.1     joerg __weak_alias(cdbw_open,_cdbw_open)
     53  1.1     joerg __weak_alias(cdbw_output,_cdbw_output)
     54  1.1     joerg __weak_alias(cdbw_put,_cdbw_put)
     55  1.1     joerg __weak_alias(cdbw_put_data,_cdbw_put_data)
     56  1.1     joerg __weak_alias(cdbw_put_key,_cdbw_put_key)
     57  1.1     joerg #endif
     58  1.1     joerg 
     59  1.1     joerg struct key_hash {
     60  1.1     joerg 	SLIST_ENTRY(key_hash) link;
     61  1.1     joerg 	uint32_t hashes[3];
     62  1.1     joerg 	uint32_t idx;
     63  1.1     joerg 	void *key;
     64  1.1     joerg 	size_t keylen;
     65  1.1     joerg };
     66  1.1     joerg 
     67  1.1     joerg SLIST_HEAD(key_hash_head, key_hash);
     68  1.1     joerg 
     69  1.1     joerg struct cdbw {
     70  1.1     joerg 	size_t data_counter;
     71  1.1     joerg 	size_t data_allocated;
     72  1.1     joerg 	size_t data_size;
     73  1.1     joerg 	size_t *data_len;
     74  1.1     joerg 	void **data_ptr;
     75  1.1     joerg 
     76  1.1     joerg 	size_t hash_size;
     77  1.1     joerg 	struct key_hash_head *hash;
     78  1.1     joerg 	size_t key_counter;
     79  1.1     joerg };
     80  1.1     joerg 
     81  1.1     joerg  /* Max. data counter that allows the index size to be 32bit. */
     82  1.1     joerg static const uint32_t max_data_counter = 0xccccccccU;
     83  1.1     joerg 
     84  1.1     joerg struct cdbw *
     85  1.1     joerg cdbw_open(void)
     86  1.1     joerg {
     87  1.1     joerg 	struct cdbw *cdbw;
     88  1.1     joerg 	size_t i;
     89  1.1     joerg 
     90  1.1     joerg 	cdbw = calloc(sizeof(*cdbw), 1);
     91  1.1     joerg 	if (cdbw == NULL)
     92  1.1     joerg 		return NULL;
     93  1.1     joerg 
     94  1.1     joerg 	cdbw->hash_size = 1024;
     95  1.1     joerg 	cdbw->hash = calloc(cdbw->hash_size, sizeof(*cdbw->hash));
     96  1.1     joerg 	if (cdbw->hash == NULL) {
     97  1.1     joerg 		free(cdbw);
     98  1.1     joerg 		return NULL;
     99  1.1     joerg 	}
    100  1.1     joerg 
    101  1.1     joerg 	for (i = 0; i < cdbw->hash_size; ++i)
    102  1.1     joerg 		SLIST_INIT(cdbw->hash + i);
    103  1.1     joerg 
    104  1.1     joerg 	return cdbw;
    105  1.1     joerg }
    106  1.1     joerg 
    107  1.1     joerg int
    108  1.1     joerg cdbw_put(struct cdbw *cdbw, const void *key, size_t keylen,
    109  1.1     joerg     const void *data, size_t datalen)
    110  1.1     joerg {
    111  1.1     joerg 	uint32_t idx;
    112  1.1     joerg 	int rv;
    113  1.1     joerg 
    114  1.1     joerg 	rv = cdbw_put_data(cdbw, data, datalen, &idx);
    115  1.1     joerg 	if (rv)
    116  1.1     joerg 		return rv;
    117  1.1     joerg 	rv = cdbw_put_key(cdbw, key, keylen, idx);
    118  1.1     joerg 	if (rv) {
    119  1.1     joerg 		--cdbw->data_counter;
    120  1.1     joerg 		free(cdbw->data_ptr[cdbw->data_counter]);
    121  1.1     joerg 		cdbw->data_size -= datalen;
    122  1.1     joerg 		return rv;
    123  1.1     joerg 	}
    124  1.1     joerg 	return 0;
    125  1.1     joerg }
    126  1.1     joerg 
    127  1.1     joerg int
    128  1.1     joerg cdbw_put_data(struct cdbw *cdbw, const void *data, size_t datalen,
    129  1.1     joerg     uint32_t *idx)
    130  1.1     joerg {
    131  1.1     joerg 
    132  1.1     joerg 	if (cdbw->data_counter == max_data_counter)
    133  1.1     joerg 		return -1;
    134  1.1     joerg 
    135  1.1     joerg 	if (cdbw->data_size + datalen < cdbw->data_size ||
    136  1.1     joerg 	    cdbw->data_size + datalen > 0xffffffffU)
    137  1.1     joerg 		return -1; /* Overflow */
    138  1.1     joerg 
    139  1.1     joerg 	if (cdbw->data_allocated == cdbw->data_counter) {
    140  1.1     joerg 		void **new_data_ptr;
    141  1.1     joerg 		size_t *new_data_len;
    142  1.1     joerg 		size_t new_allocated;
    143  1.1     joerg 
    144  1.1     joerg 		if (cdbw->data_allocated == 0)
    145  1.1     joerg 			new_allocated = 256;
    146  1.1     joerg 		else
    147  1.1     joerg 			new_allocated = cdbw->data_allocated * 2;
    148  1.1     joerg 
    149  1.1     joerg 		new_data_ptr = realloc(cdbw->data_ptr,
    150  1.1     joerg 		    sizeof(*cdbw->data_ptr) * new_allocated);
    151  1.1     joerg 		if (new_data_ptr == NULL)
    152  1.1     joerg 			return -1;
    153  1.1     joerg 		cdbw->data_ptr = new_data_ptr;
    154  1.1     joerg 
    155  1.1     joerg 		new_data_len = realloc(cdbw->data_len,
    156  1.1     joerg 		    sizeof(*cdbw->data_len) * new_allocated);
    157  1.1     joerg 		if (new_data_len == NULL)
    158  1.1     joerg 			return -1;
    159  1.1     joerg 		cdbw->data_len = new_data_len;
    160  1.1     joerg 
    161  1.1     joerg 		cdbw->data_allocated = new_allocated;
    162  1.1     joerg 	}
    163  1.1     joerg 
    164  1.1     joerg 	cdbw->data_ptr[cdbw->data_counter] = malloc(datalen);
    165  1.1     joerg 	if (cdbw->data_ptr[cdbw->data_counter] == NULL)
    166  1.1     joerg 		return -1;
    167  1.1     joerg 	memcpy(cdbw->data_ptr[cdbw->data_counter], data, datalen);
    168  1.1     joerg 	cdbw->data_len[cdbw->data_counter] = datalen;
    169  1.1     joerg 	cdbw->data_size += datalen;
    170  1.3     joerg 	*idx = cdbw->data_counter++;
    171  1.1     joerg 	return 0;
    172  1.1     joerg }
    173  1.1     joerg 
    174  1.1     joerg int
    175  1.1     joerg cdbw_put_key(struct cdbw *cdbw, const void *key, size_t keylen, uint32_t idx)
    176  1.1     joerg {
    177  1.1     joerg 	uint32_t hashes[3];
    178  1.1     joerg 	struct key_hash_head *head, *head2, *new_head;
    179  1.1     joerg 	struct key_hash *key_hash;
    180  1.1     joerg 	size_t new_hash_size, i;
    181  1.1     joerg 
    182  1.1     joerg 	if (idx >= cdbw->data_counter ||
    183  1.1     joerg 	    cdbw->key_counter == max_data_counter)
    184  1.1     joerg 		return -1;
    185  1.1     joerg 
    186  1.1     joerg 	mi_vector_hash(key, keylen, 0, hashes);
    187  1.1     joerg 
    188  1.1     joerg 	head = cdbw->hash + (hashes[0] & (cdbw->hash_size - 1));
    189  1.1     joerg 	SLIST_FOREACH(key_hash, head, link) {
    190  1.1     joerg 		if (key_hash->keylen != keylen)
    191  1.1     joerg 			continue;
    192  1.1     joerg 		if (key_hash->hashes[0] != hashes[0])
    193  1.1     joerg 			continue;
    194  1.1     joerg 		if (key_hash->hashes[1] != hashes[1])
    195  1.1     joerg 			continue;
    196  1.1     joerg 		if (key_hash->hashes[2] != hashes[2])
    197  1.1     joerg 			continue;
    198  1.1     joerg 		if (memcmp(key, key_hash->key, keylen))
    199  1.1     joerg 			continue;
    200  1.1     joerg 		return -1;
    201  1.1     joerg 	}
    202  1.1     joerg 	key_hash = malloc(sizeof(*key_hash));
    203  1.1     joerg 	if (key_hash == NULL)
    204  1.1     joerg 		return -1;
    205  1.1     joerg 	key_hash->key = malloc(keylen);
    206  1.1     joerg 	if (key_hash->key == NULL) {
    207  1.1     joerg 		free(key_hash);
    208  1.1     joerg 		return -1;
    209  1.1     joerg 	}
    210  1.1     joerg 	memcpy(key_hash->key, key, keylen);
    211  1.1     joerg 	key_hash->hashes[0] = hashes[0];
    212  1.1     joerg 	key_hash->hashes[1] = hashes[1];
    213  1.1     joerg 	key_hash->hashes[2] = hashes[2];
    214  1.1     joerg 	key_hash->keylen = keylen;
    215  1.1     joerg 	key_hash->idx = idx;
    216  1.1     joerg 	SLIST_INSERT_HEAD(head, key_hash, link);
    217  1.1     joerg 	++cdbw->key_counter;
    218  1.1     joerg 
    219  1.1     joerg 	if (cdbw->key_counter <= cdbw->hash_size)
    220  1.1     joerg 		return 0;
    221  1.1     joerg 
    222  1.1     joerg 	/* Try to resize the hash table, but ignore errors. */
    223  1.1     joerg 	new_hash_size = cdbw->hash_size * 2;
    224  1.1     joerg 	new_head = calloc(sizeof(*new_head), new_hash_size);
    225  1.1     joerg 	if (new_head == NULL)
    226  1.1     joerg 		return 0;
    227  1.1     joerg 
    228  1.1     joerg 	head = &cdbw->hash[hashes[0] & (cdbw->hash_size - 1)];
    229  1.1     joerg 	for (i = 0; i < new_hash_size; ++i)
    230  1.1     joerg 		SLIST_INIT(new_head + i);
    231  1.1     joerg 
    232  1.1     joerg 	for (i = 0; i < cdbw->hash_size; ++i) {
    233  1.1     joerg 		head = cdbw->hash + i;
    234  1.1     joerg 
    235  1.1     joerg 		while ((key_hash = SLIST_FIRST(head)) != NULL) {
    236  1.1     joerg 			SLIST_REMOVE_HEAD(head, link);
    237  1.1     joerg 			head2 = new_head +
    238  1.1     joerg 			    (key_hash->hashes[0] & (new_hash_size - 1));
    239  1.1     joerg 			SLIST_INSERT_HEAD(head2, key_hash, link);
    240  1.1     joerg 		}
    241  1.1     joerg 	}
    242  1.1     joerg 	free(cdbw->hash);
    243  1.1     joerg 	cdbw->hash_size = new_hash_size;
    244  1.1     joerg 	cdbw->hash = new_head;
    245  1.1     joerg 
    246  1.1     joerg 	return 0;
    247  1.1     joerg }
    248  1.1     joerg 
    249  1.1     joerg void
    250  1.1     joerg cdbw_close(struct cdbw *cdbw)
    251  1.1     joerg {
    252  1.1     joerg 	struct key_hash_head *head;
    253  1.1     joerg 	struct key_hash *key_hash;
    254  1.1     joerg 	size_t i;
    255  1.1     joerg 
    256  1.1     joerg 	for (i = 0; i < cdbw->hash_size; ++i) {
    257  1.1     joerg 		head = cdbw->hash + i;
    258  1.1     joerg 		while ((key_hash = SLIST_FIRST(head)) != NULL) {
    259  1.1     joerg 			SLIST_REMOVE_HEAD(head, link);
    260  1.1     joerg 			free(key_hash->key);
    261  1.1     joerg 			free(key_hash);
    262  1.1     joerg 		}
    263  1.1     joerg 	}
    264  1.1     joerg 
    265  1.1     joerg 	for (i = 0; i < cdbw->data_counter; ++i)
    266  1.1     joerg 		free(cdbw->data_ptr[i]);
    267  1.1     joerg 	free(cdbw->data_ptr);
    268  1.1     joerg 	free(cdbw->data_len);
    269  1.1     joerg 	free(cdbw->hash);
    270  1.1     joerg 	free(cdbw);
    271  1.1     joerg }
    272  1.1     joerg 
    273  1.1     joerg #define unused 0xffffffffU
    274  1.1     joerg 
    275  1.1     joerg struct vertex {
    276  1.1     joerg 	uint32_t l_edge, m_edge, r_edge;
    277  1.1     joerg };
    278  1.1     joerg 
    279  1.1     joerg struct edge {
    280  1.1     joerg 	uint32_t idx;
    281  1.1     joerg 
    282  1.1     joerg 	uint32_t left, middle, right;
    283  1.1     joerg 	uint32_t l_prev, m_prev, l_next;
    284  1.1     joerg 	uint32_t r_prev, m_next, r_next;
    285  1.1     joerg };
    286  1.1     joerg 
    287  1.1     joerg struct state {
    288  1.1     joerg 	uint32_t data_entries;
    289  1.1     joerg 	uint32_t entries;
    290  1.1     joerg 	uint32_t keys;
    291  1.1     joerg 	uint32_t seed;
    292  1.1     joerg 
    293  1.1     joerg 	uint32_t *g;
    294  1.1     joerg 	char *visited;
    295  1.1     joerg 
    296  1.1     joerg 	struct vertex *verts;
    297  1.1     joerg 	struct edge *edges;
    298  1.1     joerg 	uint32_t output_index;
    299  1.1     joerg 	uint32_t *output_order;
    300  1.1     joerg };
    301  1.1     joerg 
    302  1.1     joerg static void
    303  1.1     joerg remove_vertex(struct state *state, struct vertex *v)
    304  1.1     joerg {
    305  1.1     joerg 	struct edge *e;
    306  1.1     joerg 	struct vertex *vl, *vm, *vr;
    307  1.1     joerg 
    308  1.1     joerg 	if (v->l_edge != unused && v->m_edge != unused)
    309  1.1     joerg 		return;
    310  1.1     joerg 	if (v->l_edge != unused && v->r_edge != unused)
    311  1.1     joerg 		return;
    312  1.1     joerg 	if (v->m_edge != unused && v->r_edge != unused)
    313  1.1     joerg 		return;
    314  1.1     joerg 	if (v->l_edge == unused && v->m_edge == unused && v->r_edge == unused)
    315  1.1     joerg 		return;
    316  1.1     joerg 
    317  1.1     joerg 	if (v->l_edge != unused) {
    318  1.1     joerg 		e = &state->edges[v->l_edge];
    319  1.1     joerg 		if (e->l_next != unused)
    320  1.1     joerg 			return;
    321  1.1     joerg 	} else if (v->m_edge != unused) {
    322  1.1     joerg 		e = &state->edges[v->m_edge];
    323  1.1     joerg 		if (e->m_next != unused)
    324  1.1     joerg 			return;
    325  1.1     joerg 	} else {
    326  1.1     joerg 		if (v->r_edge == unused)
    327  1.1     joerg 			abort();
    328  1.1     joerg 		e = &state->edges[v->r_edge];
    329  1.1     joerg 		if (e->r_next != unused)
    330  1.1     joerg 			return;
    331  1.1     joerg 	}
    332  1.1     joerg 
    333  1.3     joerg 	state->output_order[--state->output_index] = e - state->edges;
    334  1.1     joerg 
    335  1.1     joerg 	vl = &state->verts[e->left];
    336  1.1     joerg 	vm = &state->verts[e->middle];
    337  1.1     joerg 	vr = &state->verts[e->right];
    338  1.1     joerg 
    339  1.1     joerg 	if (e->l_prev == unused)
    340  1.1     joerg 		vl->l_edge = e->l_next;
    341  1.1     joerg 	else
    342  1.1     joerg 		state->edges[e->l_prev].l_next = e->l_next;
    343  1.1     joerg 	if (e->l_next != unused)
    344  1.1     joerg 		state->edges[e->l_next].l_prev = e->l_prev;
    345  1.1     joerg 
    346  1.1     joerg 	if (e->m_prev == unused)
    347  1.1     joerg 		vm->m_edge = e->m_next;
    348  1.1     joerg 	else
    349  1.1     joerg 		state->edges[e->m_prev].m_next = e->m_next;
    350  1.1     joerg 	if (e->m_next != unused)
    351  1.1     joerg 		state->edges[e->m_next].m_prev = e->m_prev;
    352  1.1     joerg 
    353  1.1     joerg 	if (e->r_prev == unused)
    354  1.1     joerg 		vr->r_edge = e->r_next;
    355  1.1     joerg 	else
    356  1.1     joerg 		state->edges[e->r_prev].r_next = e->r_next;
    357  1.1     joerg 	if (e->r_next != unused)
    358  1.1     joerg 		state->edges[e->r_next].r_prev = e->r_prev;
    359  1.1     joerg }
    360  1.1     joerg 
    361  1.1     joerg static int
    362  1.1     joerg build_graph(struct cdbw *cdbw, struct state *state)
    363  1.1     joerg {
    364  1.1     joerg 	struct key_hash_head *head;
    365  1.1     joerg 	struct key_hash *key_hash;
    366  1.1     joerg 	struct vertex *v;
    367  1.1     joerg 	struct edge *e;
    368  1.3     joerg 	uint32_t hashes[3];
    369  1.3     joerg 	size_t i;
    370  1.1     joerg 
    371  1.1     joerg 	e = state->edges;
    372  1.1     joerg 	for (i = 0; i < cdbw->hash_size; ++i) {
    373  1.1     joerg 		head = &cdbw->hash[i];
    374  1.1     joerg 		SLIST_FOREACH(key_hash, head, link) {
    375  1.1     joerg 			e->idx = key_hash->idx;
    376  1.1     joerg 			mi_vector_hash(key_hash->key, key_hash->keylen,
    377  1.1     joerg 			    state->seed, hashes);
    378  1.1     joerg 			e->left = hashes[0] % state->entries;
    379  1.1     joerg 			e->middle = hashes[1] % state->entries;
    380  1.1     joerg 			e->right = hashes[2] % state->entries;
    381  1.1     joerg 
    382  1.1     joerg 			++e;
    383  1.1     joerg 		}
    384  1.1     joerg 	}
    385  1.1     joerg 
    386  1.1     joerg 	for (i = 0; i < state->entries; ++i) {
    387  1.1     joerg 		v = state->verts + i;
    388  1.1     joerg 		v->l_edge = unused;
    389  1.1     joerg 		v->m_edge = unused;
    390  1.1     joerg 		v->r_edge = unused;
    391  1.1     joerg 	}
    392  1.1     joerg 
    393  1.1     joerg 	for (i = 0; i < state->keys; ++i) {
    394  1.1     joerg 		e = state->edges + i;
    395  1.1     joerg 		v = state->verts + e->left;
    396  1.1     joerg 		if (v->l_edge != unused)
    397  1.1     joerg 			state->edges[v->l_edge].l_prev = i;
    398  1.1     joerg 		e->l_next = v->l_edge;
    399  1.1     joerg 		e->l_prev = unused;
    400  1.1     joerg 		v->l_edge = i;
    401  1.1     joerg 
    402  1.1     joerg 		v = &state->verts[e->middle];
    403  1.1     joerg 		if (v->m_edge != unused)
    404  1.1     joerg 			state->edges[v->m_edge].m_prev = i;
    405  1.1     joerg 		e->m_next = v->m_edge;
    406  1.1     joerg 		e->m_prev = unused;
    407  1.1     joerg 		v->m_edge = i;
    408  1.1     joerg 
    409  1.1     joerg 		v = &state->verts[e->right];
    410  1.1     joerg 		if (v->r_edge != unused)
    411  1.1     joerg 			state->edges[v->r_edge].r_prev = i;
    412  1.1     joerg 		e->r_next = v->r_edge;
    413  1.1     joerg 		e->r_prev = unused;
    414  1.1     joerg 		v->r_edge = i;
    415  1.1     joerg 	}
    416  1.1     joerg 
    417  1.1     joerg 	state->output_index = state->keys;
    418  1.1     joerg 	for (i = 0; i < state->entries; ++i)
    419  1.1     joerg 		remove_vertex(state, state->verts + i);
    420  1.1     joerg 
    421  1.1     joerg 	i = state->keys;
    422  1.1     joerg 	while (i > 0 && i > state->output_index) {
    423  1.1     joerg 		--i;
    424  1.1     joerg 		e = state->edges + state->output_order[i];
    425  1.1     joerg 		remove_vertex(state, state->verts + e->left);
    426  1.1     joerg 		remove_vertex(state, state->verts + e->middle);
    427  1.1     joerg 		remove_vertex(state, state->verts + e->right);
    428  1.1     joerg 	}
    429  1.1     joerg 
    430  1.1     joerg 	return state->output_index == 0 ? 0 : -1;
    431  1.1     joerg }
    432  1.1     joerg 
    433  1.1     joerg static void
    434  1.1     joerg assign_nodes(struct state *state)
    435  1.1     joerg {
    436  1.1     joerg 	struct edge *e;
    437  1.1     joerg 	size_t i;
    438  1.1     joerg 
    439  1.1     joerg 	for (i = 0; i < state->keys; ++i) {
    440  1.1     joerg 		e = state->edges + state->output_order[i];
    441  1.1     joerg 
    442  1.1     joerg 		if (!state->visited[e->left]) {
    443  1.1     joerg 			state->g[e->left] =
    444  1.1     joerg 			    (2 * state->data_entries + e->idx
    445  1.1     joerg 			    - state->g[e->middle] - state->g[e->right])
    446  1.1     joerg 			    % state->data_entries;
    447  1.1     joerg 		} else if (!state->visited[e->middle]) {
    448  1.1     joerg 			state->g[e->middle] =
    449  1.1     joerg 			    (2 * state->data_entries + e->idx
    450  1.1     joerg 			    - state->g[e->left] - state->g[e->right])
    451  1.1     joerg 			    % state->data_entries;
    452  1.1     joerg 		} else {
    453  1.1     joerg 			state->g[e->right] =
    454  1.1     joerg 			    (2 * state->data_entries + e->idx
    455  1.1     joerg 			    - state->g[e->left] - state->g[e->middle])
    456  1.1     joerg 			    % state->data_entries;
    457  1.1     joerg 		}
    458  1.1     joerg 		state->visited[e->left] = 1;
    459  1.1     joerg 		state->visited[e->middle] = 1;
    460  1.1     joerg 		state->visited[e->right] = 1;
    461  1.1     joerg 	}
    462  1.1     joerg }
    463  1.1     joerg 
    464  1.1     joerg static size_t
    465  1.1     joerg compute_size(uint32_t size)
    466  1.1     joerg {
    467  1.1     joerg 	if (size < 0x100)
    468  1.1     joerg 		return 1;
    469  1.1     joerg 	else if (size < 0x10000)
    470  1.1     joerg 		return 2;
    471  1.1     joerg 	else
    472  1.1     joerg 		return 4;
    473  1.1     joerg }
    474  1.1     joerg 
    475  1.1     joerg #define COND_FLUSH_BUFFER(n) do { 				\
    476  1.1     joerg 	if (__predict_false(cur_pos + (n) >= sizeof(buf))) {	\
    477  1.1     joerg 		ret = write(fd, buf, cur_pos);			\
    478  1.1     joerg 		if (ret == -1 || (size_t)ret != cur_pos)	\
    479  1.1     joerg 			return -1;				\
    480  1.1     joerg 		cur_pos = 0;					\
    481  1.1     joerg 	}							\
    482  1.1     joerg } while (/* CONSTCOND */ 0)
    483  1.1     joerg 
    484  1.1     joerg static int
    485  1.1     joerg print_hash(struct cdbw *cdbw, struct state *state, int fd, const char *descr)
    486  1.1     joerg {
    487  1.1     joerg 	uint32_t data_size;
    488  1.1     joerg 	uint8_t buf[90000];
    489  1.1     joerg 	size_t i, size, size2, cur_pos;
    490  1.1     joerg 	ssize_t ret;
    491  1.1     joerg 
    492  1.1     joerg 	memcpy(buf, "NBCDB\n\0", 7);
    493  1.1     joerg 	buf[7] = 1;
    494  1.1     joerg 	strncpy((char *)buf + 8, descr, 16);
    495  1.3     joerg 	le32enc(buf + 24, cdbw->data_size);
    496  1.3     joerg 	le32enc(buf + 28, cdbw->data_counter);
    497  1.1     joerg 	le32enc(buf + 32, state->entries);
    498  1.1     joerg 	le32enc(buf + 36, state->seed);
    499  1.1     joerg 	cur_pos = 40;
    500  1.1     joerg 
    501  1.1     joerg 	size = compute_size(state->entries);
    502  1.1     joerg 	for (i = 0; i < state->entries; ++i) {
    503  1.1     joerg 		COND_FLUSH_BUFFER(4);
    504  1.1     joerg 		le32enc(buf + cur_pos, state->g[i]);
    505  1.1     joerg 		cur_pos += size;
    506  1.1     joerg 	}
    507  1.3     joerg 	size2 = compute_size(cdbw->data_size);
    508  1.1     joerg 	size = size * state->entries % size2;
    509  1.1     joerg 	if (size != 0) {
    510  1.1     joerg 		size = size2 - size;
    511  1.1     joerg 		COND_FLUSH_BUFFER(4);
    512  1.1     joerg 		le32enc(buf + cur_pos, 0);
    513  1.1     joerg 		cur_pos += size;
    514  1.1     joerg 	}
    515  1.1     joerg 	for (data_size = 0, i = 0; i < cdbw->data_counter; ++i) {
    516  1.1     joerg 		COND_FLUSH_BUFFER(4);
    517  1.1     joerg 		le32enc(buf + cur_pos, data_size);
    518  1.1     joerg 		cur_pos += size2;
    519  1.3     joerg 		data_size += cdbw->data_len[i];
    520  1.1     joerg 	}
    521  1.1     joerg 	COND_FLUSH_BUFFER(4);
    522  1.1     joerg 	le32enc(buf + cur_pos, data_size);
    523  1.1     joerg 	cur_pos += size2;
    524  1.1     joerg 
    525  1.1     joerg 	for (i = 0; i < cdbw->data_counter; ++i) {
    526  1.1     joerg 		COND_FLUSH_BUFFER(cdbw->data_len[i]);
    527  1.1     joerg 		if (cdbw->data_len[i] < sizeof(buf)) {
    528  1.1     joerg 			memcpy(buf + cur_pos, cdbw->data_ptr[i],
    529  1.1     joerg 			    cdbw->data_len[i]);
    530  1.1     joerg 			cur_pos += cdbw->data_len[i];
    531  1.1     joerg 		} else {
    532  1.1     joerg 			ret = write(fd, cdbw->data_ptr[i], cdbw->data_len[i]);
    533  1.1     joerg 			if (ret == -1 || (size_t)ret != cdbw->data_len[i])
    534  1.1     joerg 				return -1;
    535  1.1     joerg 		}
    536  1.1     joerg 	}
    537  1.1     joerg 	if (cur_pos != 0) {
    538  1.1     joerg 		ret = write(fd, buf, cur_pos);
    539  1.1     joerg 		if (ret == -1 || (size_t)ret != cur_pos)
    540  1.1     joerg 			return -1;
    541  1.1     joerg 	}
    542  1.1     joerg 	return 0;
    543  1.1     joerg }
    544  1.1     joerg 
    545  1.1     joerg int
    546  1.1     joerg cdbw_output(struct cdbw *cdbw, int fd, const char descr[16],
    547  1.1     joerg     uint32_t (*seedgen)(void))
    548  1.1     joerg {
    549  1.1     joerg 	struct state state;
    550  1.1     joerg 	int rv;
    551  1.1     joerg 
    552  1.1     joerg 	if (cdbw->data_counter == 0 || cdbw->key_counter == 0) {
    553  1.1     joerg 		state.entries = 0;
    554  1.1     joerg 		state.seed = 0;
    555  1.1     joerg 		print_hash(cdbw, &state, fd, descr);
    556  1.1     joerg 		return 0;
    557  1.1     joerg 	}
    558  1.1     joerg 
    559  1.1     joerg 	if (seedgen == NULL)
    560  1.1     joerg 		seedgen = arc4random;
    561  1.1     joerg 
    562  1.1     joerg 	rv = 0;
    563  1.1     joerg 
    564  1.3     joerg 	state.keys = cdbw->key_counter;
    565  1.3     joerg 	state.data_entries = cdbw->data_counter;
    566  1.1     joerg 	state.entries = state.keys + (state.keys + 3) / 4;
    567  1.1     joerg 	if (state.entries < 10)
    568  1.1     joerg 		state.entries = 10;
    569  1.1     joerg 
    570  1.1     joerg #define	NALLOC(var, n)	var = calloc(sizeof(*var), n)
    571  1.1     joerg 	NALLOC(state.g, state.entries);
    572  1.1     joerg 	NALLOC(state.visited, state.entries);
    573  1.1     joerg 	NALLOC(state.verts, state.entries);
    574  1.1     joerg 	NALLOC(state.edges, state.entries);
    575  1.1     joerg 	NALLOC(state.output_order, state.keys);
    576  1.1     joerg #undef NALLOC
    577  1.1     joerg 
    578  1.1     joerg 	if (state.g == NULL || state.visited == NULL || state.verts == NULL ||
    579  1.1     joerg 	    state.edges == NULL || state.output_order == NULL) {
    580  1.1     joerg 		rv = -1;
    581  1.1     joerg 		goto release;
    582  1.1     joerg 	}
    583  1.1     joerg 
    584  1.1     joerg 	do {
    585  1.1     joerg 		state.seed = (*seedgen)();
    586  1.1     joerg 	} while (build_graph(cdbw, &state));
    587  1.1     joerg 
    588  1.1     joerg 	assign_nodes(&state);
    589  1.1     joerg 	rv = print_hash(cdbw, &state, fd, descr);
    590  1.1     joerg 
    591  1.1     joerg release:
    592  1.1     joerg 	free(state.g);
    593  1.1     joerg 	free(state.visited);
    594  1.1     joerg 	free(state.verts);
    595  1.1     joerg 	free(state.edges);
    596  1.1     joerg 	free(state.output_order);
    597  1.1     joerg 
    598  1.1     joerg 	return rv;
    599  1.1     joerg }
    600