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