Home | History | Annotate | Line # | Download | only in db
      1  1.1  christos /*-
      2  1.1  christos  * Copyright (c) 2015 The NetBSD Foundation, Inc.
      3  1.1  christos  * All rights reserved.
      4  1.1  christos  *
      5  1.1  christos  * This code is derived from software contributed to The NetBSD Foundation
      6  1.1  christos  * by Christos Zoulas.
      7  1.1  christos  *
      8  1.1  christos  * Redistribution and use in source and binary forms, with or without
      9  1.1  christos  * modification, are permitted provided that the following conditions
     10  1.1  christos  * are met:
     11  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     12  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     13  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  christos  *    documentation and/or other materials provided with the distribution.
     16  1.1  christos  *
     17  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18  1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     28  1.1  christos  */
     29  1.1  christos #include <sys/cdefs.h>
     30  1.2      gson __RCSID("$NetBSD: h_lfsr.c,v 1.2 2017/06/24 10:25:23 gson Exp $");
     31  1.1  christos 
     32  1.1  christos #include <stdio.h>
     33  1.1  christos #include <stdlib.h>
     34  1.1  christos #include <fcntl.h>
     35  1.1  christos #include <err.h>
     36  1.1  christos #include <string.h>
     37  1.1  christos #include <unistd.h>
     38  1.1  christos #include <db.h>
     39  1.1  christos 
     40  1.2      gson #define MAXKEY 1000
     41  1.1  christos #ifdef DEBUG
     42  1.1  christos #define DPRINTF(...)	printf(__VA_ARGS__)
     43  1.1  christos #else
     44  1.1  christos #define DPRINTF(...)
     45  1.1  christos #endif
     46  1.1  christos 
     47  1.1  christos static uint16_t
     48  1.1  christos next(uint16_t *cur)
     49  1.1  christos {
     50  1.1  christos         uint16_t lsb = *cur & 1;
     51  1.1  christos         *cur >>= 1;
     52  1.1  christos         *cur ^= (-lsb) & 0xB400u;
     53  1.1  christos 	return *cur;
     54  1.1  christos }
     55  1.1  christos 
     56  1.1  christos int
     57  1.1  christos main(int argc, char *argv[])
     58  1.1  christos {
     59  1.1  christos 	char buf[65536];
     60  1.1  christos 	char kb[256];
     61  1.1  christos 	DBT key, val;
     62  1.1  christos 	DB *db;
     63  1.1  christos 	HASHINFO hi;
     64  1.1  christos 	uint8_t c;
     65  1.1  christos 	uint16_t len;
     66  1.1  christos 	uint32_t pagesize = atoi(argv[1]);
     67  1.1  christos 
     68  1.1  christos 	memset(&hi, 0, sizeof(hi));
     69  1.1  christos 	memset(buf, 'a', sizeof(buf));
     70  1.1  christos 	hi.bsize = pagesize;
     71  1.1  christos 	hi.nelem = 65536;
     72  1.1  christos 	hi.ffactor = 128;
     73  1.1  christos 
     74  1.1  christos 	key.data = kb;
     75  1.1  christos 	val.data = buf;
     76  1.1  christos 
     77  1.1  christos 	db = dbopen(NULL, O_CREAT|O_TRUNC|O_RDWR, 0, DB_HASH, &hi);
     78  1.1  christos 	if (db == NULL)
     79  1.1  christos 		err(EXIT_FAILURE, "dbopen");
     80  1.1  christos 
     81  1.1  christos 	len = 0xaec1;
     82  1.1  christos 	for (size_t i = 0; i < MAXKEY; i++) {
     83  1.1  christos 		key.size = (len & 0xff) + 1;
     84  1.1  christos 		c = len >> 8;
     85  1.1  christos 		memset(kb, c, key.size);
     86  1.1  christos 		val.size = (next(&len) & 0xff) + 1;
     87  1.1  christos 		switch ((*db->put)(db, &key, &val, R_NOOVERWRITE)) {
     88  1.1  christos 		case 0:
     89  1.1  christos 			DPRINTF("put %zu %zu %#x\n",
     90  1.1  christos 			    key.size, val.size, c);
     91  1.1  christos 			break;
     92  1.1  christos 		case -1:
     93  1.1  christos 			err(EXIT_FAILURE, "put error %zu %zu %#x",
     94  1.1  christos 			    key.size, val.size, c);
     95  1.1  christos 		case 1:
     96  1.1  christos 			errx(EXIT_FAILURE, "put overwrite %zu %zu %#x",
     97  1.1  christos 			    key.size, val.size, c);
     98  1.1  christos 		default:
     99  1.1  christos 			abort();
    100  1.1  christos 		}
    101  1.1  christos 	}
    102  1.1  christos 
    103  1.1  christos 	len = 0xaec1;
    104  1.1  christos 	for (size_t i = 0; i < MAXKEY; i++) {
    105  1.1  christos 		key.size = (len & 0xff) + 1;
    106  1.1  christos 		c = len >> 8;
    107  1.1  christos 		memset(kb, c, key.size);
    108  1.1  christos 		next(&len);
    109  1.1  christos 		switch ((*db->get)(db, &key, &val, 0)) {
    110  1.1  christos 		case 0:
    111  1.1  christos 			DPRINTF("get %zu %zu %#x\n",
    112  1.1  christos 			    key.size, val.size, c);
    113  1.1  christos 			break;
    114  1.1  christos 		case -1:
    115  1.1  christos 			err(EXIT_FAILURE, "get %zu %zu %#x",
    116  1.1  christos 			    key.size, val.size, c);
    117  1.1  christos 		case 1:
    118  1.1  christos 			errx(EXIT_FAILURE, "get not found %zu %zu %#x",
    119  1.1  christos 			    key.size, val.size, c);
    120  1.1  christos 		default:
    121  1.1  christos 			abort();
    122  1.1  christos 		}
    123  1.1  christos 		if (memcmp(key.data, kb, key.size) != 0)
    124  1.1  christos 			errx(EXIT_FAILURE, "get badkey %zu %zu %#x",
    125  1.1  christos 			    key.size, val.size, c);
    126  1.1  christos 		if (val.size != (len & 0xff) + 1U)
    127  1.1  christos 			errx(EXIT_FAILURE, "get badvallen %zu %zu %#x",
    128  1.1  christos 			    key.size, val.size, c);
    129  1.1  christos 		if (memcmp(val.data, buf, val.size) != 0)
    130  1.1  christos 			errx(EXIT_FAILURE, "get badval %zu %zu %#x",
    131  1.1  christos 			    key.size, val.size, c);
    132  1.1  christos 	}
    133  1.1  christos 
    134  1.1  christos 	len = 0xaec1;
    135  1.1  christos 	for (size_t i = 0; i < MAXKEY; i++) {
    136  1.1  christos 		key.size = (len & 0xff) + 1;
    137  1.1  christos 		c = len >> 8;
    138  1.1  christos 		memset(kb, c, key.size);
    139  1.1  christos 		next(&len);
    140  1.1  christos 		switch ((*db->del)(db, &key, 0)) {
    141  1.1  christos 		case 0:
    142  1.1  christos 			DPRINTF("del %zu %zu %#x\n",
    143  1.1  christos 			    key.size, val.size, c);
    144  1.1  christos 			break;
    145  1.1  christos 		case -1:
    146  1.1  christos 			err(EXIT_FAILURE, "del %zu %zu %#x", key.size,
    147  1.1  christos 			    val.size, c);
    148  1.1  christos 		case 1:
    149  1.1  christos 			errx(EXIT_FAILURE, "del not found %zu %zu %#x",
    150  1.1  christos 			    key.size, val.size, c);
    151  1.1  christos 		default:
    152  1.1  christos 			abort();
    153  1.1  christos 		}
    154  1.1  christos 	}
    155  1.1  christos 
    156  1.1  christos 	len = 0xaec1;
    157  1.1  christos 	for (size_t i = 0; i < MAXKEY; i++) {
    158  1.1  christos 		key.size = (len & 0xff) + 1;
    159  1.1  christos 		c = len >> 8;
    160  1.1  christos 		memset(kb, c, key.size);
    161  1.1  christos 		next(&len);
    162  1.1  christos 		switch ((*db->get)(db, &key, &val, 0)) {
    163  1.1  christos 		case 0:
    164  1.1  christos 			errx(EXIT_FAILURE, "get2 found %zu %zu %#x",
    165  1.1  christos 			    key.size, val.size, c);
    166  1.1  christos 			break;
    167  1.1  christos 		case -1:
    168  1.1  christos 			err(EXIT_FAILURE, "get2 %zu %zu %#x",
    169  1.1  christos 			    key.size, val.size, c);
    170  1.1  christos 		case 1:
    171  1.1  christos 			DPRINTF("get2 %zu %zu %#x\n",
    172  1.1  christos 			    key.size, val.size, c);
    173  1.1  christos 			break;
    174  1.1  christos 		default:
    175  1.1  christos 			abort();
    176  1.1  christos 		}
    177  1.1  christos 	}
    178  1.1  christos 	return 0;
    179  1.1  christos }
    180