lmdb 0.6 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ext/lmdb_ext/extconf.rb +7 -3
- data/ext/lmdb_ext/lmdb_ext.c +120 -110
- data/lib/lmdb/version.rb +1 -1
- data/lmdb.gemspec +4 -4
- data/{ext/lmdb_ext → vendor/libraries}/liblmdb/.gitignore +8 -0
- data/{ext/lmdb_ext → vendor/libraries}/liblmdb/COPYRIGHT +1 -1
- data/vendor/libraries/liblmdb/Doxyfile +1631 -0
- data/{ext/lmdb_ext → vendor/libraries}/liblmdb/LICENSE +0 -0
- data/vendor/libraries/liblmdb/Makefile +118 -0
- data/vendor/libraries/liblmdb/intro.doc +192 -0
- data/{ext/lmdb_ext → vendor/libraries}/liblmdb/lmdb.h +161 -61
- data/{ext/lmdb_ext → vendor/libraries}/liblmdb/mdb.c +3244 -1302
- data/vendor/libraries/liblmdb/mdb_copy.1 +61 -0
- data/vendor/libraries/liblmdb/mdb_copy.c +84 -0
- data/vendor/libraries/liblmdb/mdb_drop.1 +40 -0
- data/vendor/libraries/liblmdb/mdb_drop.c +135 -0
- data/vendor/libraries/liblmdb/mdb_dump.1 +81 -0
- data/vendor/libraries/liblmdb/mdb_dump.c +319 -0
- data/vendor/libraries/liblmdb/mdb_load.1 +84 -0
- data/vendor/libraries/liblmdb/mdb_load.c +492 -0
- data/vendor/libraries/liblmdb/mdb_stat.1 +70 -0
- data/vendor/libraries/liblmdb/mdb_stat.c +264 -0
- data/{ext/lmdb_ext → vendor/libraries}/liblmdb/midl.c +66 -5
- data/{ext/lmdb_ext → vendor/libraries}/liblmdb/midl.h +19 -5
- data/vendor/libraries/liblmdb/mtest.c +177 -0
- data/vendor/libraries/liblmdb/mtest2.c +124 -0
- data/vendor/libraries/liblmdb/mtest3.c +133 -0
- data/vendor/libraries/liblmdb/mtest4.c +168 -0
- data/vendor/libraries/liblmdb/mtest5.c +135 -0
- data/vendor/libraries/liblmdb/mtest6.c +141 -0
- data/vendor/libraries/liblmdb/sample-bdb.txt +73 -0
- data/vendor/libraries/liblmdb/sample-mdb.txt +62 -0
- data/vendor/libraries/liblmdb/tooltag +27 -0
- metadata +34 -14
- data/.gitignore +0 -15
- data/.travis.yml +0 -14
- data/ext/lmdb_ext/liblmdb/CHANGES +0 -112
@@ -0,0 +1,124 @@
|
|
1
|
+
/* mtest2.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
|
+
/* Just like mtest.c, but using a subDB instead of the main DB */
|
16
|
+
|
17
|
+
#include <stdio.h>
|
18
|
+
#include <stdlib.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[32] = "";
|
39
|
+
|
40
|
+
srand(time(NULL));
|
41
|
+
|
42
|
+
count = (rand()%384) + 64;
|
43
|
+
values = (int *)malloc(count*sizeof(int));
|
44
|
+
|
45
|
+
for(i = 0;i<count;i++) {
|
46
|
+
values[i] = rand()%1024;
|
47
|
+
}
|
48
|
+
|
49
|
+
E(mdb_env_create(&env));
|
50
|
+
E(mdb_env_set_maxreaders(env, 1));
|
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, "id1", MDB_CREATE, &dbi));
|
57
|
+
|
58
|
+
key.mv_size = sizeof(int);
|
59
|
+
key.mv_data = sval;
|
60
|
+
|
61
|
+
printf("Adding %d values\n", count);
|
62
|
+
for (i=0;i<count;i++) {
|
63
|
+
sprintf(sval, "%03x %d foo bar", values[i], values[i]);
|
64
|
+
data.mv_size = sizeof(sval);
|
65
|
+
data.mv_data = sval;
|
66
|
+
if (RES(MDB_KEYEXIST, mdb_put(txn, dbi, &key, &data, MDB_NOOVERWRITE)))
|
67
|
+
j++;
|
68
|
+
}
|
69
|
+
if (j) printf("%d duplicates skipped\n", j);
|
70
|
+
E(mdb_txn_commit(txn));
|
71
|
+
E(mdb_env_stat(env, &mst));
|
72
|
+
|
73
|
+
E(mdb_txn_begin(env, NULL, MDB_RDONLY, &txn));
|
74
|
+
E(mdb_cursor_open(txn, dbi, &cursor));
|
75
|
+
while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
|
76
|
+
printf("key: %p %.*s, data: %p %.*s\n",
|
77
|
+
key.mv_data, (int) key.mv_size, (char *) key.mv_data,
|
78
|
+
data.mv_data, (int) data.mv_size, (char *) data.mv_data);
|
79
|
+
}
|
80
|
+
CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get");
|
81
|
+
mdb_cursor_close(cursor);
|
82
|
+
mdb_txn_abort(txn);
|
83
|
+
|
84
|
+
j=0;
|
85
|
+
key.mv_data = sval;
|
86
|
+
for (i= count - 1; i > -1; i-= (rand()%5)) {
|
87
|
+
j++;
|
88
|
+
txn=NULL;
|
89
|
+
E(mdb_txn_begin(env, NULL, 0, &txn));
|
90
|
+
sprintf(sval, "%03x ", values[i]);
|
91
|
+
if (RES(MDB_NOTFOUND, mdb_del(txn, dbi, &key, NULL))) {
|
92
|
+
j--;
|
93
|
+
mdb_txn_abort(txn);
|
94
|
+
} else {
|
95
|
+
E(mdb_txn_commit(txn));
|
96
|
+
}
|
97
|
+
}
|
98
|
+
free(values);
|
99
|
+
printf("Deleted %d values\n", j);
|
100
|
+
|
101
|
+
E(mdb_env_stat(env, &mst));
|
102
|
+
E(mdb_txn_begin(env, NULL, MDB_RDONLY, &txn));
|
103
|
+
E(mdb_cursor_open(txn, dbi, &cursor));
|
104
|
+
printf("Cursor next\n");
|
105
|
+
while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
|
106
|
+
printf("key: %.*s, data: %.*s\n",
|
107
|
+
(int) key.mv_size, (char *) key.mv_data,
|
108
|
+
(int) data.mv_size, (char *) data.mv_data);
|
109
|
+
}
|
110
|
+
CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get");
|
111
|
+
printf("Cursor prev\n");
|
112
|
+
while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_PREV)) == 0) {
|
113
|
+
printf("key: %.*s, data: %.*s\n",
|
114
|
+
(int) key.mv_size, (char *) key.mv_data,
|
115
|
+
(int) data.mv_size, (char *) data.mv_data);
|
116
|
+
}
|
117
|
+
CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get");
|
118
|
+
mdb_cursor_close(cursor);
|
119
|
+
mdb_txn_abort(txn);
|
120
|
+
|
121
|
+
mdb_dbi_close(env, dbi);
|
122
|
+
mdb_env_close(env);
|
123
|
+
return 0;
|
124
|
+
}
|
@@ -0,0 +1,133 @@
|
|
1
|
+
/* mtest3.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 */
|
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[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
|
+
}
|
@@ -0,0 +1,168 @@
|
|
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
|
+
}
|
@@ -0,0 +1,135 @@
|
|
1
|
+
/* mtest5.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 using cursor_put */
|
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[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
|
+
E(mdb_cursor_open(txn, dbi, &cursor));
|
60
|
+
|
61
|
+
key.mv_size = sizeof(int);
|
62
|
+
key.mv_data = kval;
|
63
|
+
data.mv_size = sizeof(sval);
|
64
|
+
data.mv_data = sval;
|
65
|
+
|
66
|
+
printf("Adding %d values\n", count);
|
67
|
+
for (i=0;i<count;i++) {
|
68
|
+
if (!(i & 0x0f))
|
69
|
+
sprintf(kval, "%03x", values[i]);
|
70
|
+
sprintf(sval, "%03x %d foo bar", values[i], values[i]);
|
71
|
+
if (RES(MDB_KEYEXIST, mdb_cursor_put(cursor, &key, &data, MDB_NODUPDATA)))
|
72
|
+
j++;
|
73
|
+
}
|
74
|
+
if (j) printf("%d duplicates skipped\n", j);
|
75
|
+
mdb_cursor_close(cursor);
|
76
|
+
E(mdb_txn_commit(txn));
|
77
|
+
E(mdb_env_stat(env, &mst));
|
78
|
+
|
79
|
+
E(mdb_txn_begin(env, NULL, MDB_RDONLY, &txn));
|
80
|
+
E(mdb_cursor_open(txn, dbi, &cursor));
|
81
|
+
while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
|
82
|
+
printf("key: %p %.*s, data: %p %.*s\n",
|
83
|
+
key.mv_data, (int) key.mv_size, (char *) key.mv_data,
|
84
|
+
data.mv_data, (int) data.mv_size, (char *) data.mv_data);
|
85
|
+
}
|
86
|
+
CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get");
|
87
|
+
mdb_cursor_close(cursor);
|
88
|
+
mdb_txn_abort(txn);
|
89
|
+
|
90
|
+
j=0;
|
91
|
+
|
92
|
+
for (i= count - 1; i > -1; i-= (rand()%5)) {
|
93
|
+
j++;
|
94
|
+
txn=NULL;
|
95
|
+
E(mdb_txn_begin(env, NULL, 0, &txn));
|
96
|
+
sprintf(kval, "%03x", values[i & ~0x0f]);
|
97
|
+
sprintf(sval, "%03x %d foo bar", values[i], values[i]);
|
98
|
+
key.mv_size = sizeof(int);
|
99
|
+
key.mv_data = kval;
|
100
|
+
data.mv_size = sizeof(sval);
|
101
|
+
data.mv_data = sval;
|
102
|
+
if (RES(MDB_NOTFOUND, mdb_del(txn, dbi, &key, &data))) {
|
103
|
+
j--;
|
104
|
+
mdb_txn_abort(txn);
|
105
|
+
} else {
|
106
|
+
E(mdb_txn_commit(txn));
|
107
|
+
}
|
108
|
+
}
|
109
|
+
free(values);
|
110
|
+
printf("Deleted %d values\n", j);
|
111
|
+
|
112
|
+
E(mdb_env_stat(env, &mst));
|
113
|
+
E(mdb_txn_begin(env, NULL, MDB_RDONLY, &txn));
|
114
|
+
E(mdb_cursor_open(txn, dbi, &cursor));
|
115
|
+
printf("Cursor next\n");
|
116
|
+
while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 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
|
+
printf("Cursor prev\n");
|
123
|
+
while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_PREV)) == 0) {
|
124
|
+
printf("key: %.*s, data: %.*s\n",
|
125
|
+
(int) key.mv_size, (char *) key.mv_data,
|
126
|
+
(int) data.mv_size, (char *) data.mv_data);
|
127
|
+
}
|
128
|
+
CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get");
|
129
|
+
mdb_cursor_close(cursor);
|
130
|
+
mdb_txn_abort(txn);
|
131
|
+
|
132
|
+
mdb_dbi_close(env, dbi);
|
133
|
+
mdb_env_close(env);
|
134
|
+
return 0;
|
135
|
+
}
|
@@ -0,0 +1,141 @@
|
|
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
|
+
}
|