1/* mtest3.c - memory-mapped database tester/toy */
2/*
3 * Copyright 2011-2018 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 */
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
27int 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[32];
39 char kval[sizeof(int)];
40
41 srand(time(NULL));
42
43 memset(sval, 0, sizeof(sval));
44
45 count = (rand()%384) + 64;
46 values = (int *)malloc(count*sizeof(int));
47
48 for(i = 0;i<count;i++) {
49 values[i] = rand()%1024;
50 }
51
52 E(mdb_env_create(&env));
53 E(mdb_env_set_mapsize(env, 10485760));
54 E(mdb_env_set_maxdbs(env, 4));
55 E(mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664));
56
57 E(mdb_txn_begin(env, NULL, 0, &txn));
58 E(mdb_dbi_open(txn, "id2", MDB_CREATE|MDB_DUPSORT, &dbi));
59
60 key.mv_size = sizeof(int);
61 key.mv_data = kval;
62 data.mv_size = sizeof(sval);
63 data.mv_data = sval;
64
65 printf("Adding %d values\n", count);
66 for (i=0;i<count;i++) {
67 if (!(i & 0x0f))
68 sprintf(kval, "%03x", values[i]);
69 sprintf(sval, "%03x %d foo bar", values[i], values[i]);
70 if (RES(MDB_KEYEXIST, mdb_put(txn, dbi, &key, &data, MDB_NODUPDATA)))
71 j++;
72 }
73 if (j) printf("%d duplicates skipped\n", j);
74 E(mdb_txn_commit(txn));
75 E(mdb_env_stat(env, &mst));
76
77 E(mdb_txn_begin(env, NULL, MDB_RDONLY, &txn));
78 E(mdb_cursor_open(txn, dbi, &cursor));
79 while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
80 printf("key: %p %.*s, data: %p %.*s\n",
81 key.mv_data, (int) key.mv_size, (char *) key.mv_data,
82 data.mv_data, (int) data.mv_size, (char *) data.mv_data);
83 }
84 CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get");
85 mdb_cursor_close(cursor);
86 mdb_txn_abort(txn);
87
88 j=0;
89
90 for (i= count - 1; i > -1; i-= (rand()%5)) {
91 j++;
92 txn=NULL;
93 E(mdb_txn_begin(env, NULL, 0, &txn));
94 sprintf(kval, "%03x", values[i & ~0x0f]);
95 sprintf(sval, "%03x %d foo bar", values[i], values[i]);
96 key.mv_size = sizeof(int);
97 key.mv_data = kval;
98 data.mv_size = sizeof(sval);
99 data.mv_data = sval;
100 if (RES(MDB_NOTFOUND, mdb_del(txn, dbi, &key, &data))) {
101 j--;
102 mdb_txn_abort(txn);
103 } else {
104 E(mdb_txn_commit(txn));
105 }
106 }
107 free(values);
108 printf("Deleted %d values\n", j);
109
110 E(mdb_env_stat(env, &mst));
111 E(mdb_txn_begin(env, NULL, MDB_RDONLY, &txn));
112 E(mdb_cursor_open(txn, dbi, &cursor));
113 printf("Cursor next\n");
114 while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
115 printf("key: %.*s, data: %.*s\n",
116 (int) key.mv_size, (char *) key.mv_data,
117 (int) data.mv_size, (char *) data.mv_data);
118 }
119 CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get");
120 printf("Cursor prev\n");
121 while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_PREV)) == 0) {
122 printf("key: %.*s, data: %.*s\n",
123 (int) key.mv_size, (char *) key.mv_data,
124 (int) data.mv_size, (char *) data.mv_data);
125 }
126 CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get");
127 mdb_cursor_close(cursor);
128 mdb_txn_abort(txn);
129
130 mdb_dbi_close(env, dbi);
131 mdb_env_close(env);
132 return 0;
133}
134