nostrdb

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

mtest4.c (4791B)


      1 /* mtest4.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 sorted duplicate DBs with fixed-size keys */
     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 int main(int argc,char * argv[])
     28 {
     29 	int i = 0, j = 0, rc;
     30 	MDB_env *env;
     31 	MDB_dbi dbi;
     32 	MDB_val key, data;
     33 	MDB_txn *txn;
     34 	MDB_stat mst;
     35 	MDB_cursor *cursor;
     36 	int count;
     37 	int *values;
     38 	char sval[8];
     39 	char kval[sizeof(int)];
     40 
     41 	memset(sval, 0, sizeof(sval));
     42 
     43 	count = 510;
     44 	values = (int *)malloc(count*sizeof(int));
     45 
     46 	for(i = 0;i<count;i++) {
     47 		values[i] = i*5;
     48 	}
     49 
     50 	E(mdb_env_create(&env));
     51 	E(mdb_env_set_mapsize(env, 10485760));
     52 	E(mdb_env_set_maxdbs(env, 4));
     53 	E(mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664));
     54 
     55 	E(mdb_txn_begin(env, NULL, 0, &txn));
     56 	E(mdb_dbi_open(txn, "id4", MDB_CREATE|MDB_DUPSORT|MDB_DUPFIXED, &dbi));
     57 
     58 	key.mv_size = sizeof(int);
     59 	key.mv_data = kval;
     60 	data.mv_size = sizeof(sval);
     61 	data.mv_data = sval;
     62 
     63 	printf("Adding %d values\n", count);
     64 	strcpy(kval, "001");
     65 	for (i=0;i<count;i++) {
     66 		sprintf(sval, "%07x", values[i]);
     67 		if (RES(MDB_KEYEXIST, mdb_put(txn, dbi, &key, &data, MDB_NODUPDATA)))
     68 			j++;
     69 	}
     70 	if (j) printf("%d duplicates skipped\n", j);
     71 	E(mdb_txn_commit(txn));
     72 	E(mdb_env_stat(env, &mst));
     73 
     74 	/* there should be one full page of dups now.
     75 	 */
     76 	E(mdb_txn_begin(env, NULL, MDB_RDONLY, &txn));
     77 	E(mdb_cursor_open(txn, dbi, &cursor));
     78 	while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
     79 		printf("key: %p %.*s, data: %p %.*s\n",
     80 			key.mv_data,  (int) key.mv_size,  (char *) key.mv_data,
     81 			data.mv_data, (int) data.mv_size, (char *) data.mv_data);
     82 	}
     83 	CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get");
     84 	mdb_cursor_close(cursor);
     85 	mdb_txn_abort(txn);
     86 
     87 	/* test all 3 branches of split code:
     88 	 * 1: new key in lower half
     89 	 * 2: new key at split point
     90 	 * 3: new key in upper half
     91 	 */
     92 
     93 	key.mv_size = sizeof(int);
     94 	key.mv_data = kval;
     95 	data.mv_size = sizeof(sval);
     96 	data.mv_data = sval;
     97 
     98 	sprintf(sval, "%07x", values[3]+1);
     99 	E(mdb_txn_begin(env, NULL, 0, &txn));
    100 	(void)RES(MDB_KEYEXIST, mdb_put(txn, dbi, &key, &data, MDB_NODUPDATA));
    101 	mdb_txn_abort(txn);
    102 
    103 	sprintf(sval, "%07x", values[255]+1);
    104 	E(mdb_txn_begin(env, NULL, 0, &txn));
    105 	(void)RES(MDB_KEYEXIST, mdb_put(txn, dbi, &key, &data, MDB_NODUPDATA));
    106 	mdb_txn_abort(txn);
    107 
    108 	sprintf(sval, "%07x", values[500]+1);
    109 	E(mdb_txn_begin(env, NULL, 0, &txn));
    110 	(void)RES(MDB_KEYEXIST, mdb_put(txn, dbi, &key, &data, MDB_NODUPDATA));
    111 	E(mdb_txn_commit(txn));
    112 
    113 	/* Try MDB_NEXT_MULTIPLE */
    114 	E(mdb_txn_begin(env, NULL, 0, &txn));
    115 	E(mdb_cursor_open(txn, dbi, &cursor));
    116 	while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT_MULTIPLE)) == 0) {
    117 		printf("key: %.*s, data: %.*s\n",
    118 			(int) key.mv_size,  (char *) key.mv_data,
    119 			(int) data.mv_size, (char *) data.mv_data);
    120 	}
    121 	CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get");
    122 	mdb_cursor_close(cursor);
    123 	mdb_txn_abort(txn);
    124 	j=0;
    125 
    126 	for (i= count - 1; i > -1; i-= (rand()%3)) {
    127 		j++;
    128 		txn=NULL;
    129 		E(mdb_txn_begin(env, NULL, 0, &txn));
    130 		sprintf(sval, "%07x", values[i]);
    131 		key.mv_size = sizeof(int);
    132 		key.mv_data = kval;
    133 		data.mv_size = sizeof(sval);
    134 		data.mv_data = sval;
    135 		if (RES(MDB_NOTFOUND, mdb_del(txn, dbi, &key, &data))) {
    136 			j--;
    137 			mdb_txn_abort(txn);
    138 		} else {
    139 			E(mdb_txn_commit(txn));
    140 		}
    141 	}
    142 	free(values);
    143 	printf("Deleted %d values\n", j);
    144 
    145 	E(mdb_env_stat(env, &mst));
    146 	E(mdb_txn_begin(env, NULL, MDB_RDONLY, &txn));
    147 	E(mdb_cursor_open(txn, dbi, &cursor));
    148 	printf("Cursor next\n");
    149 	while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
    150 		printf("key: %.*s, data: %.*s\n",
    151 			(int) key.mv_size,  (char *) key.mv_data,
    152 			(int) data.mv_size, (char *) data.mv_data);
    153 	}
    154 	CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get");
    155 	printf("Cursor prev\n");
    156 	while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_PREV)) == 0) {
    157 		printf("key: %.*s, data: %.*s\n",
    158 			(int) key.mv_size,  (char *) key.mv_data,
    159 			(int) data.mv_size, (char *) data.mv_data);
    160 	}
    161 	CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get");
    162 	mdb_cursor_close(cursor);
    163 	mdb_txn_abort(txn);
    164 
    165 	mdb_dbi_close(env, dbi);
    166 	mdb_env_close(env);
    167 	return 0;
    168 }