@ohos-ports/lmdb 2.8.6-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +580 -0
- package/SECURITY.md +12 -0
- package/bin/download-prebuilds.js +15 -0
- package/binding.gyp +113 -0
- package/build/Release/lmdb.node +0 -0
- package/caching.js +212 -0
- package/dependencies/lmdb/libraries/liblmdb/COPYRIGHT +20 -0
- package/dependencies/lmdb/libraries/liblmdb/Doxyfile +1631 -0
- package/dependencies/lmdb/libraries/liblmdb/LICENSE +47 -0
- package/dependencies/lmdb/libraries/liblmdb/Makefile +136 -0
- package/dependencies/lmdb/libraries/liblmdb/chacha8.c +183 -0
- package/dependencies/lmdb/libraries/liblmdb/chacha8.h +14 -0
- package/dependencies/lmdb/libraries/liblmdb/crypto.c +121 -0
- package/dependencies/lmdb/libraries/liblmdb/intro.doc +192 -0
- package/dependencies/lmdb/libraries/liblmdb/lmdb.h +1817 -0
- package/dependencies/lmdb/libraries/liblmdb/mdb.c +12555 -0
- package/dependencies/lmdb/libraries/liblmdb/mdb_copy.1 +74 -0
- package/dependencies/lmdb/libraries/liblmdb/mdb_copy.c +106 -0
- package/dependencies/lmdb/libraries/liblmdb/mdb_drop.1 +53 -0
- package/dependencies/lmdb/libraries/liblmdb/mdb_drop.c +154 -0
- package/dependencies/lmdb/libraries/liblmdb/mdb_dump.1 +94 -0
- package/dependencies/lmdb/libraries/liblmdb/mdb_dump.c +333 -0
- package/dependencies/lmdb/libraries/liblmdb/mdb_load.1 +97 -0
- package/dependencies/lmdb/libraries/liblmdb/mdb_load.c +530 -0
- package/dependencies/lmdb/libraries/liblmdb/mdb_stat.1 +83 -0
- package/dependencies/lmdb/libraries/liblmdb/mdb_stat.c +276 -0
- package/dependencies/lmdb/libraries/liblmdb/midl-sparse.c +452 -0
- package/dependencies/lmdb/libraries/liblmdb/midl.c +452 -0
- package/dependencies/lmdb/libraries/liblmdb/midl.h +208 -0
- package/dependencies/lmdb/libraries/liblmdb/module.c +101 -0
- package/dependencies/lmdb/libraries/liblmdb/module.h +16 -0
- package/dependencies/lmdb/libraries/liblmdb/mtest.c +178 -0
- package/dependencies/lmdb/libraries/liblmdb/mtest2.c +124 -0
- package/dependencies/lmdb/libraries/liblmdb/mtest3.c +133 -0
- package/dependencies/lmdb/libraries/liblmdb/mtest4.c +168 -0
- package/dependencies/lmdb/libraries/liblmdb/mtest5.c +135 -0
- package/dependencies/lmdb/libraries/liblmdb/mtest6.c +141 -0
- package/dependencies/lmdb/libraries/liblmdb/mtest_enc.c +190 -0
- package/dependencies/lmdb/libraries/liblmdb/mtest_enc2.c +189 -0
- package/dependencies/lmdb/libraries/liblmdb/mtest_remap.c +177 -0
- package/dependencies/lmdb/libraries/liblmdb/sample-bdb.txt +73 -0
- package/dependencies/lmdb/libraries/liblmdb/sample-mdb.txt +62 -0
- package/dependencies/lmdb/libraries/liblmdb/tooltag +27 -0
- package/dependencies/lmdb-data-v1/libraries/liblmdb/CHANGES +266 -0
- package/dependencies/lmdb-data-v1/libraries/liblmdb/COPYRIGHT +20 -0
- package/dependencies/lmdb-data-v1/libraries/liblmdb/Doxyfile +1631 -0
- package/dependencies/lmdb-data-v1/libraries/liblmdb/LICENSE +47 -0
- package/dependencies/lmdb-data-v1/libraries/liblmdb/intro.doc +192 -0
- package/dependencies/lmdb-data-v1/libraries/liblmdb/lmdb.h +1608 -0
- package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb.c +10320 -0
- package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_copy.1 +55 -0
- package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_copy.c +82 -0
- package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_dump.1 +75 -0
- package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_dump.c +319 -0
- package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_load.1 +84 -0
- package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_load.c +496 -0
- package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_stat.1 +64 -0
- package/dependencies/lmdb-data-v1/libraries/liblmdb/mdb_stat.c +263 -0
- package/dependencies/lmdb-data-v1/libraries/liblmdb/midl.c +359 -0
- package/dependencies/lmdb-data-v1/libraries/liblmdb/midl.h +186 -0
- package/dependencies/lmdb-data-v1/libraries/liblmdb/mtest.c +177 -0
- package/dependencies/lmdb-data-v1/libraries/liblmdb/mtest2.c +124 -0
- package/dependencies/lmdb-data-v1/libraries/liblmdb/mtest3.c +133 -0
- package/dependencies/lmdb-data-v1/libraries/liblmdb/mtest4.c +168 -0
- package/dependencies/lmdb-data-v1/libraries/liblmdb/mtest5.c +135 -0
- package/dependencies/lmdb-data-v1/libraries/liblmdb/mtest6.c +141 -0
- package/dependencies/lmdb-data-v1/libraries/liblmdb/sample-bdb.txt +73 -0
- package/dependencies/lmdb-data-v1/libraries/liblmdb/sample-mdb.txt +62 -0
- package/dependencies/lmdb-data-v1/libraries/liblmdb/tooltag +22 -0
- package/dependencies/lz4/LICENSE +11 -0
- package/dependencies/lz4/lib/LICENSE +24 -0
- package/dependencies/lz4/lib/Makefile +225 -0
- package/dependencies/lz4/lib/README.md +169 -0
- package/dependencies/lz4/lib/dll/example/Makefile +63 -0
- package/dependencies/lz4/lib/dll/example/README.md +69 -0
- package/dependencies/lz4/lib/dll/example/fullbench-dll.sln +25 -0
- package/dependencies/lz4/lib/dll/example/fullbench-dll.vcxproj +182 -0
- package/dependencies/lz4/lib/dll/liblz4.def +62 -0
- package/dependencies/lz4/lib/liblz4-dll.rc.in +35 -0
- package/dependencies/lz4/lib/liblz4.pc.in +14 -0
- package/dependencies/lz4/lib/lz4.c +2722 -0
- package/dependencies/lz4/lib/lz4.h +842 -0
- package/dependencies/lz4/lib/lz4file.c +311 -0
- package/dependencies/lz4/lib/lz4file.h +93 -0
- package/dependencies/lz4/lib/lz4frame.c +2078 -0
- package/dependencies/lz4/lib/lz4frame.h +692 -0
- package/dependencies/lz4/lib/lz4frame_static.h +47 -0
- package/dependencies/lz4/lib/lz4hc.c +1631 -0
- package/dependencies/lz4/lib/lz4hc.h +413 -0
- package/dependencies/lz4/lib/xxhash.c +1030 -0
- package/dependencies/lz4/lib/xxhash.h +328 -0
- package/dependencies/v8/v8-fast-api-calls-v16.h +791 -0
- package/dependencies/v8/v8-fast-api-calls.h +888 -0
- package/dict/dict.txt +1 -0
- package/dict/dict2.txt +1 -0
- package/dist/index.cjs +2950 -0
- package/dist/index.cjs.map +1 -0
- package/index.d.cts +420 -0
- package/index.d.ts +450 -0
- package/index.js +38 -0
- package/keys.js +116 -0
- package/level.js +27 -0
- package/native.js +85 -0
- package/node-index.js +32 -0
- package/open.js +448 -0
- package/package.json +124 -0
- package/read.js +833 -0
- package/rollup.config.js +12 -0
- package/src/compression.cpp +225 -0
- package/src/cursor.cpp +395 -0
- package/src/dbi.cpp +382 -0
- package/src/env.cpp +1030 -0
- package/src/lmdb-js.cpp +64 -0
- package/src/lmdb-js.h +633 -0
- package/src/misc.cpp +593 -0
- package/src/ordered-binary.cpp +46 -0
- package/src/txn.cpp +210 -0
- package/src/v8-functions.cpp +170 -0
- package/src/windows.c +35 -0
- package/src/writer.cpp +537 -0
- package/util/RangeIterable.js +236 -0
- package/util/set-optional-deps.cjs +14 -0
- package/util/when.js +8 -0
- package/write.js +912 -0
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
/* mdb_stat.c - memory-mapped database status tool */
|
|
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
|
+
#include <stdio.h>
|
|
15
|
+
#include <stdlib.h>
|
|
16
|
+
#include <string.h>
|
|
17
|
+
#include <unistd.h>
|
|
18
|
+
#include "lmdb.h"
|
|
19
|
+
#include "module.h"
|
|
20
|
+
|
|
21
|
+
#define Z MDB_FMT_Z
|
|
22
|
+
#define Yu MDB_PRIy(u)
|
|
23
|
+
|
|
24
|
+
static void prstat(MDB_stat *ms)
|
|
25
|
+
{
|
|
26
|
+
printf(" Page size: %u\n", ms->ms_psize);
|
|
27
|
+
printf(" Tree depth: %u\n", ms->ms_depth);
|
|
28
|
+
printf(" Branch pages: %"Yu"\n", ms->ms_branch_pages);
|
|
29
|
+
printf(" Leaf pages: %"Yu"\n", ms->ms_leaf_pages);
|
|
30
|
+
printf(" Overflow pages: %"Yu"\n", ms->ms_overflow_pages);
|
|
31
|
+
printf(" Entries: %"Yu"\n", ms->ms_entries);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
static void usage(char *prog)
|
|
35
|
+
{
|
|
36
|
+
fprintf(stderr, "usage: %s [-V] [-n] [-e] [-r[r]] [-f[f[f]]] [-v] [-m module [-w password]] [-a|-s subdb] dbpath\n", prog);
|
|
37
|
+
exit(EXIT_FAILURE);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
int main(int argc, char *argv[])
|
|
41
|
+
{
|
|
42
|
+
int i, rc;
|
|
43
|
+
MDB_env *env;
|
|
44
|
+
MDB_txn *txn;
|
|
45
|
+
MDB_dbi dbi;
|
|
46
|
+
MDB_stat mst;
|
|
47
|
+
MDB_envinfo mei;
|
|
48
|
+
char *prog = argv[0];
|
|
49
|
+
char *envname;
|
|
50
|
+
char *subname = NULL;
|
|
51
|
+
int alldbs = 0, envinfo = 0, envflags = 0, freinfo = 0, rdrinfo = 0;
|
|
52
|
+
char *module = NULL, *password = NULL, *errmsg;
|
|
53
|
+
void *mlm = NULL;
|
|
54
|
+
|
|
55
|
+
if (argc < 2) {
|
|
56
|
+
usage(prog);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/* -a: print stat of main DB and all subDBs
|
|
60
|
+
* -s: print stat of only the named subDB
|
|
61
|
+
* -e: print env info
|
|
62
|
+
* -f: print freelist info
|
|
63
|
+
* -r: print reader info
|
|
64
|
+
* -n: use NOSUBDIR flag on env_open
|
|
65
|
+
* -v: use previous snapshot
|
|
66
|
+
* -V: print version and exit
|
|
67
|
+
* (default) print stat of only the main DB
|
|
68
|
+
*/
|
|
69
|
+
while ((i = getopt(argc, argv, "Vaefm:nrs:vw:")) != EOF) {
|
|
70
|
+
switch(i) {
|
|
71
|
+
case 'V':
|
|
72
|
+
printf("%s\n", MDB_VERSION_STRING);
|
|
73
|
+
exit(0);
|
|
74
|
+
break;
|
|
75
|
+
case 'a':
|
|
76
|
+
if (subname)
|
|
77
|
+
usage(prog);
|
|
78
|
+
alldbs++;
|
|
79
|
+
break;
|
|
80
|
+
case 'e':
|
|
81
|
+
envinfo++;
|
|
82
|
+
break;
|
|
83
|
+
case 'f':
|
|
84
|
+
freinfo++;
|
|
85
|
+
break;
|
|
86
|
+
case 'n':
|
|
87
|
+
envflags |= MDB_NOSUBDIR;
|
|
88
|
+
break;
|
|
89
|
+
case 'v':
|
|
90
|
+
envflags |= MDB_PREVSNAPSHOT;
|
|
91
|
+
break;
|
|
92
|
+
case 'r':
|
|
93
|
+
rdrinfo++;
|
|
94
|
+
break;
|
|
95
|
+
case 's':
|
|
96
|
+
if (alldbs)
|
|
97
|
+
usage(prog);
|
|
98
|
+
subname = optarg;
|
|
99
|
+
break;
|
|
100
|
+
case 'm':
|
|
101
|
+
module = optarg;
|
|
102
|
+
break;
|
|
103
|
+
case 'w':
|
|
104
|
+
password = optarg;
|
|
105
|
+
break;
|
|
106
|
+
default:
|
|
107
|
+
usage(prog);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (optind != argc - 1)
|
|
112
|
+
usage(prog);
|
|
113
|
+
|
|
114
|
+
envname = argv[optind];
|
|
115
|
+
rc = mdb_env_create(&env);
|
|
116
|
+
if (rc) {
|
|
117
|
+
fprintf(stderr, "mdb_env_create failed, error %d %s\n", rc, mdb_strerror(rc));
|
|
118
|
+
return EXIT_FAILURE;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (module) {
|
|
122
|
+
mlm = mlm_setup(env, module, password, &errmsg);
|
|
123
|
+
if (!mlm) {
|
|
124
|
+
fprintf(stderr, "Failed to load crypto module: %s\n", errmsg);
|
|
125
|
+
goto env_close;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (alldbs || subname) {
|
|
130
|
+
mdb_env_set_maxdbs(env, 4);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
rc = mdb_env_open(env, envname, envflags | MDB_RDONLY, 0664);
|
|
134
|
+
if (rc) {
|
|
135
|
+
fprintf(stderr, "mdb_env_open failed, error %d %s\n", rc, mdb_strerror(rc));
|
|
136
|
+
goto env_close;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (envinfo) {
|
|
140
|
+
(void)mdb_env_stat(env, &mst);
|
|
141
|
+
(void)mdb_env_info(env, &mei);
|
|
142
|
+
printf("Environment Info\n");
|
|
143
|
+
printf(" Map address: %p\n", mei.me_mapaddr);
|
|
144
|
+
printf(" Map size: %"Yu"\n", mei.me_mapsize);
|
|
145
|
+
printf(" Page size: %u\n", mst.ms_psize);
|
|
146
|
+
printf(" Max pages: %"Yu"\n", mei.me_mapsize / mst.ms_psize);
|
|
147
|
+
printf(" Number of pages used: %"Yu"\n", mei.me_last_pgno+1);
|
|
148
|
+
printf(" Last transaction ID: %"Yu"\n", mei.me_last_txnid);
|
|
149
|
+
printf(" Max readers: %u\n", mei.me_maxreaders);
|
|
150
|
+
printf(" Number of readers used: %u\n", mei.me_numreaders);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (rdrinfo) {
|
|
154
|
+
printf("Reader Table Status\n");
|
|
155
|
+
rc = mdb_reader_list(env, (MDB_msg_func *)fputs, stdout);
|
|
156
|
+
if (rdrinfo > 1) {
|
|
157
|
+
int dead;
|
|
158
|
+
mdb_reader_check(env, &dead);
|
|
159
|
+
printf(" %d stale readers cleared.\n", dead);
|
|
160
|
+
rc = mdb_reader_list(env, (MDB_msg_func *)fputs, stdout);
|
|
161
|
+
}
|
|
162
|
+
if (!(subname || alldbs || freinfo))
|
|
163
|
+
goto env_close;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
rc = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn);
|
|
167
|
+
if (rc) {
|
|
168
|
+
fprintf(stderr, "mdb_txn_begin failed, error %d %s\n", rc, mdb_strerror(rc));
|
|
169
|
+
goto env_close;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (freinfo) {
|
|
173
|
+
MDB_cursor *cursor;
|
|
174
|
+
MDB_val key, data;
|
|
175
|
+
mdb_size_t pages = 0, *iptr;
|
|
176
|
+
|
|
177
|
+
printf("Freelist Status\n");
|
|
178
|
+
dbi = 0;
|
|
179
|
+
rc = mdb_cursor_open(txn, dbi, &cursor);
|
|
180
|
+
if (rc) {
|
|
181
|
+
fprintf(stderr, "mdb_cursor_open failed, error %d %s\n", rc, mdb_strerror(rc));
|
|
182
|
+
goto txn_abort;
|
|
183
|
+
}
|
|
184
|
+
rc = mdb_stat(txn, dbi, &mst);
|
|
185
|
+
if (rc) {
|
|
186
|
+
fprintf(stderr, "mdb_stat failed, error %d %s\n", rc, mdb_strerror(rc));
|
|
187
|
+
goto txn_abort;
|
|
188
|
+
}
|
|
189
|
+
prstat(&mst);
|
|
190
|
+
while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
|
|
191
|
+
iptr = data.mv_data;
|
|
192
|
+
pages += *iptr;
|
|
193
|
+
if (freinfo > 1) {
|
|
194
|
+
char *bad = "";
|
|
195
|
+
mdb_size_t pg, prev;
|
|
196
|
+
ssize_t i, j, span = 0;
|
|
197
|
+
j = *iptr++;
|
|
198
|
+
for (i = j, prev = 1; --i >= 0; ) {
|
|
199
|
+
pg = iptr[i];
|
|
200
|
+
if (pg <= prev)
|
|
201
|
+
bad = " [bad sequence]";
|
|
202
|
+
prev = pg;
|
|
203
|
+
pg += span;
|
|
204
|
+
for (; i >= span && iptr[i-span] == pg; span++, pg++) ;
|
|
205
|
+
}
|
|
206
|
+
printf(" Transaction %"Yu", %"Z"d pages, maxspan %"Z"d%s\n",
|
|
207
|
+
*(mdb_size_t *)key.mv_data, j, span, bad);
|
|
208
|
+
if (freinfo > 2) {
|
|
209
|
+
for (--j; j >= 0; ) {
|
|
210
|
+
pg = iptr[j];
|
|
211
|
+
for (span=1; --j >= 0 && iptr[j] == pg+span; span++) ;
|
|
212
|
+
printf(span>1 ? " %9"Yu"[%"Z"d]\n" : " %9"Yu"\n",
|
|
213
|
+
pg, span);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
mdb_cursor_close(cursor);
|
|
219
|
+
printf(" Free pages: %"Yu"\n", pages);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
rc = mdb_dbi_open(txn, subname, 0, &dbi);
|
|
223
|
+
if (rc) {
|
|
224
|
+
fprintf(stderr, "mdb_dbi_open failed, error %d %s\n", rc, mdb_strerror(rc));
|
|
225
|
+
goto txn_abort;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
rc = mdb_stat(txn, dbi, &mst);
|
|
229
|
+
if (rc) {
|
|
230
|
+
fprintf(stderr, "mdb_stat failed, error %d %s\n", rc, mdb_strerror(rc));
|
|
231
|
+
goto txn_abort;
|
|
232
|
+
}
|
|
233
|
+
printf("Status of %s\n", subname ? subname : "Main DB");
|
|
234
|
+
prstat(&mst);
|
|
235
|
+
|
|
236
|
+
if (alldbs) {
|
|
237
|
+
MDB_cursor *cursor;
|
|
238
|
+
MDB_val key;
|
|
239
|
+
|
|
240
|
+
rc = mdb_cursor_open(txn, dbi, &cursor);
|
|
241
|
+
if (rc) {
|
|
242
|
+
fprintf(stderr, "mdb_cursor_open failed, error %d %s\n", rc, mdb_strerror(rc));
|
|
243
|
+
goto txn_abort;
|
|
244
|
+
}
|
|
245
|
+
while ((rc = mdb_cursor_get(cursor, &key, NULL, MDB_NEXT_NODUP)) == 0) {
|
|
246
|
+
MDB_dbi db2;
|
|
247
|
+
if (!mdb_cursor_is_db(cursor))
|
|
248
|
+
continue;
|
|
249
|
+
rc = mdb_dbi_open(txn, key.mv_data, 0, &db2);
|
|
250
|
+
if (rc == MDB_SUCCESS)
|
|
251
|
+
printf("Status of %s\n", (char *)key.mv_data);
|
|
252
|
+
if (rc) continue;
|
|
253
|
+
rc = mdb_stat(txn, db2, &mst);
|
|
254
|
+
if (rc) {
|
|
255
|
+
fprintf(stderr, "mdb_stat failed, error %d %s\n", rc, mdb_strerror(rc));
|
|
256
|
+
goto txn_abort;
|
|
257
|
+
}
|
|
258
|
+
prstat(&mst);
|
|
259
|
+
mdb_dbi_close(env, db2);
|
|
260
|
+
}
|
|
261
|
+
mdb_cursor_close(cursor);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (rc == MDB_NOTFOUND)
|
|
265
|
+
rc = MDB_SUCCESS;
|
|
266
|
+
|
|
267
|
+
mdb_dbi_close(env, dbi);
|
|
268
|
+
txn_abort:
|
|
269
|
+
mdb_txn_abort(txn);
|
|
270
|
+
env_close:
|
|
271
|
+
mdb_env_close(env);
|
|
272
|
+
if (mlm)
|
|
273
|
+
mlm_unload(mlm);
|
|
274
|
+
|
|
275
|
+
return rc ? EXIT_FAILURE : EXIT_SUCCESS;
|
|
276
|
+
}
|
|
@@ -0,0 +1,452 @@
|
|
|
1
|
+
/** @file midl.c
|
|
2
|
+
* @brief ldap bdb back-end ID List functions */
|
|
3
|
+
/* $OpenLDAP$ */
|
|
4
|
+
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
|
5
|
+
*
|
|
6
|
+
* Copyright 2000-2021 The OpenLDAP Foundation.
|
|
7
|
+
* Portions Copyright 2001-2021 Howard Chu, Symas Corp.
|
|
8
|
+
* All rights reserved.
|
|
9
|
+
*
|
|
10
|
+
* Redistribution and use in source and binary forms, with or without
|
|
11
|
+
* modification, are permitted only as authorized by the OpenLDAP
|
|
12
|
+
* Public License.
|
|
13
|
+
*
|
|
14
|
+
* A copy of this license is available in the file LICENSE in the
|
|
15
|
+
* top-level directory of the distribution or, alternatively, at
|
|
16
|
+
* <http://www.OpenLDAP.org/license.html>.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
#include <limits.h>
|
|
20
|
+
#include <string.h>
|
|
21
|
+
#include <stdlib.h>
|
|
22
|
+
#include <errno.h>
|
|
23
|
+
#include <sys/types.h>
|
|
24
|
+
#include "midl.h"
|
|
25
|
+
|
|
26
|
+
/** @defgroup internal LMDB Internals
|
|
27
|
+
* @{
|
|
28
|
+
*/
|
|
29
|
+
/** @defgroup idls ID List Management
|
|
30
|
+
* @{
|
|
31
|
+
*/
|
|
32
|
+
#define CMP(x,y) ( (x) < (y) ? -1 : (x) > (y) )
|
|
33
|
+
|
|
34
|
+
unsigned mdb_midl_search( MDB_IDL ids, MDB_ID id )
|
|
35
|
+
{
|
|
36
|
+
/*
|
|
37
|
+
* binary search of id in ids
|
|
38
|
+
* if found, returns position of id
|
|
39
|
+
* if not found, returns first position greater than id
|
|
40
|
+
*/
|
|
41
|
+
unsigned base = 0;
|
|
42
|
+
unsigned cursor = 1;
|
|
43
|
+
int val = 0;
|
|
44
|
+
unsigned n = ids[0];
|
|
45
|
+
|
|
46
|
+
while( 0 < n ) {
|
|
47
|
+
unsigned pivot = n >> 1;
|
|
48
|
+
cursor = base + pivot + 1;
|
|
49
|
+
val = CMP( ids[cursor], id );
|
|
50
|
+
|
|
51
|
+
if( val < 0 ) {
|
|
52
|
+
n = pivot;
|
|
53
|
+
|
|
54
|
+
} else if ( val > 0 ) {
|
|
55
|
+
base = cursor;
|
|
56
|
+
n -= pivot + 1;
|
|
57
|
+
|
|
58
|
+
} else {
|
|
59
|
+
return cursor;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if( val > 0 ) {
|
|
64
|
+
++cursor;
|
|
65
|
+
}
|
|
66
|
+
return cursor;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
#if 0 /* superseded by append/sort */
|
|
70
|
+
int mdb_midl_insert( MDB_IDL ids, MDB_ID id )
|
|
71
|
+
{
|
|
72
|
+
unsigned x, i;
|
|
73
|
+
|
|
74
|
+
x = mdb_midl_search( ids, id );
|
|
75
|
+
assert( x > 0 );
|
|
76
|
+
|
|
77
|
+
if( x < 1 ) {
|
|
78
|
+
/* internal error */
|
|
79
|
+
return -2;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if ( x <= ids[0] && ids[x] == id ) {
|
|
83
|
+
/* duplicate */
|
|
84
|
+
assert(0);
|
|
85
|
+
return -1;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if ( ++ids[0] >= MDB_IDL_DB_MAX ) {
|
|
89
|
+
/* no room */
|
|
90
|
+
--ids[0];
|
|
91
|
+
return -2;
|
|
92
|
+
|
|
93
|
+
} else {
|
|
94
|
+
/* insert id */
|
|
95
|
+
for (i=ids[0]; i>x; i--)
|
|
96
|
+
ids[i] = ids[i-1];
|
|
97
|
+
ids[x] = id;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return 0;
|
|
101
|
+
}
|
|
102
|
+
#endif
|
|
103
|
+
|
|
104
|
+
MDB_IDL mdb_midl_alloc(int num)
|
|
105
|
+
{
|
|
106
|
+
MDB_IDL ids = malloc((num+2) * sizeof(MDB_ID));
|
|
107
|
+
if (ids) {
|
|
108
|
+
*ids++ = num;
|
|
109
|
+
*ids = 0;
|
|
110
|
+
}
|
|
111
|
+
return ids;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
void mdb_midl_free(MDB_IDL ids)
|
|
115
|
+
{
|
|
116
|
+
if (ids)
|
|
117
|
+
free(ids-1);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
void mdb_midl_shrink( MDB_IDL *idp )
|
|
121
|
+
{
|
|
122
|
+
MDB_IDL ids = *idp;
|
|
123
|
+
if (*(--ids) > MDB_IDL_UM_MAX &&
|
|
124
|
+
(ids = realloc(ids, (MDB_IDL_UM_MAX+2) * sizeof(MDB_ID))))
|
|
125
|
+
{
|
|
126
|
+
*ids++ = MDB_IDL_UM_MAX;
|
|
127
|
+
*idp = ids;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
static int mdb_midl_grow( MDB_IDL *idp, int num )
|
|
132
|
+
{
|
|
133
|
+
MDB_IDL idn = *idp-1;
|
|
134
|
+
/* grow it */
|
|
135
|
+
idn = realloc(idn, (*idn + num + 2) * sizeof(MDB_ID));
|
|
136
|
+
if (!idn)
|
|
137
|
+
return ENOMEM;
|
|
138
|
+
*idn++ += num;
|
|
139
|
+
*idp = idn;
|
|
140
|
+
return 0;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
int mdb_midl_need( MDB_IDL *idp, unsigned num )
|
|
144
|
+
{
|
|
145
|
+
MDB_IDL ids = *idp;
|
|
146
|
+
num += ids[0];
|
|
147
|
+
if (num > ids[-1]) {
|
|
148
|
+
num = (num + num/4 + (256 + 2)) & -256;
|
|
149
|
+
if (!(ids = realloc(ids-1, num * sizeof(MDB_ID))))
|
|
150
|
+
return ENOMEM;
|
|
151
|
+
*ids++ = num - 2;
|
|
152
|
+
*idp = ids;
|
|
153
|
+
}
|
|
154
|
+
return 0;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
int mdb_midl_append( MDB_IDL *idp, MDB_ID id )
|
|
158
|
+
{
|
|
159
|
+
MDB_IDL ids = *idp;
|
|
160
|
+
/* Too big? */
|
|
161
|
+
if (ids[0] >= ids[-1]) {
|
|
162
|
+
if (mdb_midl_grow(idp, MDB_IDL_UM_MAX))
|
|
163
|
+
return ENOMEM;
|
|
164
|
+
ids = *idp;
|
|
165
|
+
}
|
|
166
|
+
ids[0]++;
|
|
167
|
+
ids[ids[0]] = id;
|
|
168
|
+
return 0;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
int mdb_midl_append_list( MDB_IDL *idp, MDB_IDL app )
|
|
172
|
+
{
|
|
173
|
+
MDB_IDL ids = *idp;
|
|
174
|
+
/* Too big? */
|
|
175
|
+
if (ids[0] + app[0] >= ids[-1]) {
|
|
176
|
+
if (mdb_midl_grow(idp, app[0]))
|
|
177
|
+
return ENOMEM;
|
|
178
|
+
ids = *idp;
|
|
179
|
+
}
|
|
180
|
+
memcpy(&ids[ids[0]+1], &app[1], app[0] * sizeof(MDB_ID));
|
|
181
|
+
ids[0] += app[0];
|
|
182
|
+
return 0;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
int mdb_midl_append_range( MDB_IDL *idp, MDB_ID id, unsigned n )
|
|
186
|
+
{
|
|
187
|
+
MDB_ID *ids = *idp, len = ids[0];
|
|
188
|
+
/* Too big? */
|
|
189
|
+
if (len + n > ids[-1]) {
|
|
190
|
+
if (mdb_midl_grow(idp, n | MDB_IDL_UM_MAX))
|
|
191
|
+
return ENOMEM;
|
|
192
|
+
ids = *idp;
|
|
193
|
+
}
|
|
194
|
+
ids[0] = len + n;
|
|
195
|
+
ids += len;
|
|
196
|
+
while (n)
|
|
197
|
+
ids[n--] = id++;
|
|
198
|
+
return 0;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
void mdb_midl_xmerge( MDB_IDL idl, MDB_IDL merge )
|
|
202
|
+
{
|
|
203
|
+
MDB_ID old_id, merge_id, i = merge[0], j = idl[0], k = i+j, total = k;
|
|
204
|
+
idl[0] = (MDB_ID)-1; /* delimiter for idl scan below */
|
|
205
|
+
old_id = idl[j];
|
|
206
|
+
while (i) {
|
|
207
|
+
merge_id = merge[i--];
|
|
208
|
+
for (; old_id < merge_id; old_id = idl[--j])
|
|
209
|
+
idl[k--] = old_id;
|
|
210
|
+
idl[k--] = merge_id;
|
|
211
|
+
}
|
|
212
|
+
idl[0] = total;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/* Quicksort + Insertion sort for small arrays */
|
|
216
|
+
|
|
217
|
+
#define SMALL 8
|
|
218
|
+
#define MIDL_SWAP(a,b) { itmp=(a); (a)=(b); (b)=itmp; }
|
|
219
|
+
|
|
220
|
+
void
|
|
221
|
+
mdb_midl_sort( MDB_IDL ids )
|
|
222
|
+
{
|
|
223
|
+
/* Max possible depth of int-indexed tree * 2 items/level */
|
|
224
|
+
int istack[sizeof(int)*CHAR_BIT * 2];
|
|
225
|
+
int i,j,k,l,ir,jstack;
|
|
226
|
+
MDB_ID a, itmp;
|
|
227
|
+
|
|
228
|
+
ir = (int)ids[0];
|
|
229
|
+
l = 1;
|
|
230
|
+
jstack = 0;
|
|
231
|
+
for(;;) {
|
|
232
|
+
if (ir - l < SMALL) { /* Insertion sort */
|
|
233
|
+
for (j=l+1;j<=ir;j++) {
|
|
234
|
+
a = ids[j];
|
|
235
|
+
for (i=j-1;i>=1;i--) {
|
|
236
|
+
if (ids[i] >= a) break;
|
|
237
|
+
ids[i+1] = ids[i];
|
|
238
|
+
}
|
|
239
|
+
ids[i+1] = a;
|
|
240
|
+
}
|
|
241
|
+
if (jstack == 0) break;
|
|
242
|
+
ir = istack[jstack--];
|
|
243
|
+
l = istack[jstack--];
|
|
244
|
+
} else {
|
|
245
|
+
k = (l + ir) >> 1; /* Choose median of left, center, right */
|
|
246
|
+
MIDL_SWAP(ids[k], ids[l+1]);
|
|
247
|
+
if (ids[l] < ids[ir]) {
|
|
248
|
+
MIDL_SWAP(ids[l], ids[ir]);
|
|
249
|
+
}
|
|
250
|
+
if (ids[l+1] < ids[ir]) {
|
|
251
|
+
MIDL_SWAP(ids[l+1], ids[ir]);
|
|
252
|
+
}
|
|
253
|
+
if (ids[l] < ids[l+1]) {
|
|
254
|
+
MIDL_SWAP(ids[l], ids[l+1]);
|
|
255
|
+
}
|
|
256
|
+
i = l+1;
|
|
257
|
+
j = ir;
|
|
258
|
+
a = ids[l+1];
|
|
259
|
+
for(;;) {
|
|
260
|
+
do i++; while(ids[i] > a);
|
|
261
|
+
do j--; while(ids[j] < a);
|
|
262
|
+
if (j < i) break;
|
|
263
|
+
MIDL_SWAP(ids[i],ids[j]);
|
|
264
|
+
}
|
|
265
|
+
ids[l+1] = ids[j];
|
|
266
|
+
ids[j] = a;
|
|
267
|
+
jstack += 2;
|
|
268
|
+
if (ir-i+1 >= j-l) {
|
|
269
|
+
istack[jstack] = ir;
|
|
270
|
+
istack[jstack-1] = i;
|
|
271
|
+
ir = j-1;
|
|
272
|
+
} else {
|
|
273
|
+
istack[jstack] = j-1;
|
|
274
|
+
istack[jstack-1] = l;
|
|
275
|
+
l = i;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
unsigned mdb_mid2l_search( MDB_ID2L ids, MDB_ID id )
|
|
282
|
+
{
|
|
283
|
+
/*
|
|
284
|
+
* binary search of id in ids
|
|
285
|
+
* if found, returns position of id
|
|
286
|
+
* if not found, returns first position greater than id
|
|
287
|
+
*/
|
|
288
|
+
unsigned base = 0;
|
|
289
|
+
unsigned cursor = 1;
|
|
290
|
+
int val = 0;
|
|
291
|
+
unsigned n = (unsigned)ids[0].mid;
|
|
292
|
+
|
|
293
|
+
while( 0 < n ) {
|
|
294
|
+
unsigned pivot = n >> 1;
|
|
295
|
+
cursor = base + pivot + 1;
|
|
296
|
+
val = CMP( id, ids[cursor].mid );
|
|
297
|
+
|
|
298
|
+
if( val < 0 ) {
|
|
299
|
+
n = pivot;
|
|
300
|
+
|
|
301
|
+
} else if ( val > 0 ) {
|
|
302
|
+
base = cursor;
|
|
303
|
+
n -= pivot + 1;
|
|
304
|
+
|
|
305
|
+
} else {
|
|
306
|
+
return cursor;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
if( val > 0 ) {
|
|
311
|
+
++cursor;
|
|
312
|
+
}
|
|
313
|
+
return cursor;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
int mdb_mid2l_insert( MDB_ID2L ids, MDB_ID2 *id )
|
|
317
|
+
{
|
|
318
|
+
unsigned x, i;
|
|
319
|
+
|
|
320
|
+
x = mdb_mid2l_search( ids, id->mid );
|
|
321
|
+
|
|
322
|
+
if( x < 1 ) {
|
|
323
|
+
/* internal error */
|
|
324
|
+
return -2;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
if ( x <= ids[0].mid && ids[x].mid == id->mid ) {
|
|
328
|
+
/* duplicate */
|
|
329
|
+
return -1;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
if ( ids[0].mid >= MDB_IDL_UM_MAX ) {
|
|
333
|
+
/* too big */
|
|
334
|
+
return -2;
|
|
335
|
+
|
|
336
|
+
} else {
|
|
337
|
+
/* insert id */
|
|
338
|
+
ids[0].mid++;
|
|
339
|
+
for (i=(unsigned)ids[0].mid; i>x; i--)
|
|
340
|
+
ids[i] = ids[i-1];
|
|
341
|
+
ids[x] = *id;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
return 0;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
int mdb_mid2l_append( MDB_ID2L ids, MDB_ID2 *id )
|
|
348
|
+
{
|
|
349
|
+
/* Too big? */
|
|
350
|
+
if (ids[0].mid >= MDB_IDL_UM_MAX) {
|
|
351
|
+
return -2;
|
|
352
|
+
}
|
|
353
|
+
ids[0].mid++;
|
|
354
|
+
ids[ids[0].mid] = *id;
|
|
355
|
+
return 0;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
MDB_ID2L mdb_mid2l_alloc(int num)
|
|
359
|
+
{
|
|
360
|
+
MDB_ID2L ids = malloc((num+2) * sizeof(MDB_ID2));
|
|
361
|
+
if (ids) {
|
|
362
|
+
ids->mid = num;
|
|
363
|
+
ids++;
|
|
364
|
+
ids->mid = 0;
|
|
365
|
+
}
|
|
366
|
+
return ids;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
void mdb_mid2l_free(MDB_ID2L ids)
|
|
370
|
+
{
|
|
371
|
+
if (ids)
|
|
372
|
+
free(ids-1);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
int mdb_mid2l_need( MDB_ID2L *idp, unsigned num )
|
|
376
|
+
{
|
|
377
|
+
MDB_ID2L ids = *idp;
|
|
378
|
+
num += ids[0].mid;
|
|
379
|
+
if (num > ids[-1].mid) {
|
|
380
|
+
num = (num + num/4 + (256 + 2)) & -256;
|
|
381
|
+
if (!(ids = realloc(ids-1, num * sizeof(MDB_ID2))))
|
|
382
|
+
return ENOMEM;
|
|
383
|
+
ids[0].mid = num - 2;
|
|
384
|
+
*idp = ids+1;
|
|
385
|
+
}
|
|
386
|
+
return 0;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
#if MDB_RPAGE_CACHE
|
|
390
|
+
unsigned mdb_mid3l_search( MDB_ID3L ids, MDB_ID id )
|
|
391
|
+
{
|
|
392
|
+
/*
|
|
393
|
+
* binary search of id in ids
|
|
394
|
+
* if found, returns position of id
|
|
395
|
+
* if not found, returns first position greater than id
|
|
396
|
+
*/
|
|
397
|
+
unsigned base = 0;
|
|
398
|
+
unsigned cursor = 1;
|
|
399
|
+
int val = 0;
|
|
400
|
+
unsigned n = (unsigned)ids[0].mid;
|
|
401
|
+
|
|
402
|
+
while( 0 < n ) {
|
|
403
|
+
unsigned pivot = n >> 1;
|
|
404
|
+
cursor = base + pivot + 1;
|
|
405
|
+
val = CMP( id, ids[cursor].mid );
|
|
406
|
+
|
|
407
|
+
if( val < 0 ) {
|
|
408
|
+
n = pivot;
|
|
409
|
+
|
|
410
|
+
} else if ( val > 0 ) {
|
|
411
|
+
base = cursor;
|
|
412
|
+
n -= pivot + 1;
|
|
413
|
+
|
|
414
|
+
} else {
|
|
415
|
+
return cursor;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
if( val > 0 ) {
|
|
420
|
+
++cursor;
|
|
421
|
+
}
|
|
422
|
+
return cursor;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
int mdb_mid3l_insert( MDB_ID3L ids, MDB_ID3 *id )
|
|
426
|
+
{
|
|
427
|
+
unsigned x, i;
|
|
428
|
+
|
|
429
|
+
x = mdb_mid3l_search( ids, id->mid );
|
|
430
|
+
|
|
431
|
+
if( x < 1 ) {
|
|
432
|
+
/* internal error */
|
|
433
|
+
return -2;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
if ( x <= ids[0].mid && ids[x].mid == id->mid ) {
|
|
437
|
+
/* duplicate */
|
|
438
|
+
return -1;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/* insert id */
|
|
442
|
+
ids[0].mid++;
|
|
443
|
+
for (i=(unsigned)ids[0].mid; i>x; i--)
|
|
444
|
+
ids[i] = ids[i-1];
|
|
445
|
+
ids[x] = *id;
|
|
446
|
+
|
|
447
|
+
return 0;
|
|
448
|
+
}
|
|
449
|
+
#endif /* MDB_RPAGE_CACHE */
|
|
450
|
+
|
|
451
|
+
/** @} */
|
|
452
|
+
/** @} */
|