nostrdb

an unfairly fast embedded nostr database backed by lmdb
git clone git://jb55.com/nostrdb
Log | Files | Refs | Submodules | README | LICENSE

mtest6.c (3953B)


      1 /* mtest6.c - memory-mapped database tester/toy */
      2 /*
      3  * Copyright 2011-2021 Howard Chu, Symas Corp.
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted only as authorized by the OpenLDAP
      8  * Public License.
      9  *
     10  * A copy of this license is available in the file LICENSE in the
     11  * top-level directory of the distribution or, alternatively, at
     12  * <http://www.OpenLDAP.org/license.html>.
     13  */
     14 
     15 /* Tests for DB splits and merges */
     16 #include <stdio.h>
     17 #include <stdlib.h>
     18 #include <string.h>
     19 #include <time.h>
     20 #include "lmdb.h"
     21 
     22 #define E(expr) CHECK((rc = (expr)) == MDB_SUCCESS, #expr)
     23 #define RES(err, expr) ((rc = expr) == (err) || (CHECK(!rc, #expr), 0))
     24 #define CHECK(test, msg) ((test) ? (void)0 : ((void)fprintf(stderr, \
     25 	"%s:%d: %s: %s\n", __FILE__, __LINE__, msg, mdb_strerror(rc)), abort()))
     26 
     27 char dkbuf[1024];
     28 
     29 int main(int argc,char * argv[])
     30 {
     31 	int i = 0, j = 0, rc;
     32 	MDB_env *env;
     33 	MDB_dbi dbi;
     34 	MDB_val key, data, sdata;
     35 	MDB_txn *txn;
     36 	MDB_stat mst;
     37 	MDB_cursor *cursor;
     38 	int count;
     39 	int *values;
     40 	long kval;
     41 	char *sval;
     42 
     43 	srand(time(NULL));
     44 
     45 	E(mdb_env_create(&env));
     46 	E(mdb_env_set_mapsize(env, 10485760));
     47 	E(mdb_env_set_maxdbs(env, 4));
     48 	E(mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664));
     49 
     50 	E(mdb_txn_begin(env, NULL, 0, &txn));
     51 	E(mdb_dbi_open(txn, "id6", MDB_CREATE|MDB_INTEGERKEY, &dbi));
     52 	E(mdb_cursor_open(txn, dbi, &cursor));
     53 	E(mdb_stat(txn, dbi, &mst));
     54 
     55 	sval = calloc(1, mst.ms_psize / 4);
     56 	key.mv_size = sizeof(long);
     57 	key.mv_data = &kval;
     58 	sdata.mv_size = mst.ms_psize / 4 - 30;
     59 	sdata.mv_data = sval;
     60 
     61 	printf("Adding 12 values, should yield 3 splits\n");
     62 	for (i=0;i<12;i++) {
     63 		kval = i*5;
     64 		sprintf(sval, "%08x", kval);
     65 		data = sdata;
     66 		(void)RES(MDB_KEYEXIST, mdb_cursor_put(cursor, &key, &data, MDB_NOOVERWRITE));
     67 	}
     68 	printf("Adding 12 more values, should yield 3 splits\n");
     69 	for (i=0;i<12;i++) {
     70 		kval = i*5+4;
     71 		sprintf(sval, "%08x", kval);
     72 		data = sdata;
     73 		(void)RES(MDB_KEYEXIST, mdb_cursor_put(cursor, &key, &data, MDB_NOOVERWRITE));
     74 	}
     75 	printf("Adding 12 more values, should yield 3 splits\n");
     76 	for (i=0;i<12;i++) {
     77 		kval = i*5+1;
     78 		sprintf(sval, "%08x", kval);
     79 		data = sdata;
     80 		(void)RES(MDB_KEYEXIST, mdb_cursor_put(cursor, &key, &data, MDB_NOOVERWRITE));
     81 	}
     82 	E(mdb_cursor_get(cursor, &key, &data, MDB_FIRST));
     83 
     84 	do {
     85 		printf("key: %p %s, data: %p %.*s\n",
     86 			key.mv_data,  mdb_dkey(&key, dkbuf),
     87 			data.mv_data, (int) data.mv_size, (char *) data.mv_data);
     88 	} while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0);
     89 	CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get");
     90 	mdb_cursor_close(cursor);
     91 	mdb_txn_commit(txn);
     92 
     93 #if 0
     94 	j=0;
     95 
     96 	for (i= count - 1; i > -1; i-= (rand()%5)) {
     97 		j++;
     98 		txn=NULL;
     99 		E(mdb_txn_begin(env, NULL, 0, &txn));
    100 		sprintf(kval, "%03x", values[i & ~0x0f]);
    101 		sprintf(sval, "%03x %d foo bar", values[i], values[i]);
    102 		key.mv_size = sizeof(int);
    103 		key.mv_data = kval;
    104 		data.mv_size = sizeof(sval);
    105 		data.mv_data = sval;
    106 		if (RES(MDB_NOTFOUND, mdb_del(txn, dbi, &key, &data))) {
    107 			j--;
    108 			mdb_txn_abort(txn);
    109 		} else {
    110 			E(mdb_txn_commit(txn));
    111 		}
    112 	}
    113 	free(values);
    114 	printf("Deleted %d values\n", j);
    115 
    116 	E(mdb_env_stat(env, &mst));
    117 	E(mdb_txn_begin(env, NULL, MDB_RDONLY, &txn));
    118 	E(mdb_cursor_open(txn, dbi, &cursor));
    119 	printf("Cursor next\n");
    120 	while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
    121 		printf("key: %.*s, data: %.*s\n",
    122 			(int) key.mv_size,  (char *) key.mv_data,
    123 			(int) data.mv_size, (char *) data.mv_data);
    124 	}
    125 	CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get");
    126 	printf("Cursor prev\n");
    127 	while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_PREV)) == 0) {
    128 		printf("key: %.*s, data: %.*s\n",
    129 			(int) key.mv_size,  (char *) key.mv_data,
    130 			(int) data.mv_size, (char *) data.mv_data);
    131 	}
    132 	CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get");
    133 	mdb_cursor_close(cursor);
    134 	mdb_txn_abort(txn);
    135 
    136 	mdb_dbi_close(env, dbi);
    137 #endif
    138 	mdb_env_close(env);
    139 
    140 	return 0;
    141 }