Commit e4cf9502 authored by Howard Chu's avatar Howard Chu
Browse files

mdb_dbi_open tweak

Always save the terminating NUL of the incoming DB name
Stop using the old mdb_open/mdb_close function names
 .. should consider renaming mdb_stat/mdb_drop to mdb_dbi_... too.
parent 3585a1eb
...@@ -11110,7 +11110,7 @@ int mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *db ...@@ -11110,7 +11110,7 @@ int mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *db
int rc, dbflag, exact; int rc, dbflag, exact;
unsigned int unused = 0, seq; unsigned int unused = 0, seq;
char *namedup; char *namedup;
size_t len; size_t size;
if (flags & ~VALID_FLAGS) if (flags & ~VALID_FLAGS)
return EINVAL; return EINVAL;
...@@ -11137,15 +11137,15 @@ int mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *db ...@@ -11137,15 +11137,15 @@ int mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *db
} }
/* Is the DB already open? */ /* Is the DB already open? */
len = strlen(name); size = strlen(name) + 1;
for (i=CORE_DBS; i<txn->mt_numdbs; i++) { for (i=CORE_DBS; i<txn->mt_numdbs; i++) {
if (!txn->mt_dbxs[i].md_name.mv_size) { if (!txn->mt_dbxs[i].md_name.mv_size) {
/* Remember this free slot */ /* Remember this free slot */
if (!unused) unused = i; if (!unused) unused = i;
continue; continue;
} }
if (len == txn->mt_dbxs[i].md_name.mv_size && if (size == txn->mt_dbxs[i].md_name.mv_size &&
!strncmp(name, txn->mt_dbxs[i].md_name.mv_data, len)) { !strcmp(name, txn->mt_dbxs[i].md_name.mv_data)) {
*dbi = i; *dbi = i;
return MDB_SUCCESS; return MDB_SUCCESS;
} }
...@@ -11162,7 +11162,7 @@ int mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *db ...@@ -11162,7 +11162,7 @@ int mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *db
/* Find the DB info */ /* Find the DB info */
dbflag = DB_NEW|DB_VALID|DB_USRVALID; dbflag = DB_NEW|DB_VALID|DB_USRVALID;
exact = 0; exact = 0;
key.mv_size = len; key.mv_size = size;
key.mv_data = (void *)name; key.mv_data = (void *)name;
mdb_cursor_init(&mc, txn, MAIN_DBI, NULL); mdb_cursor_init(&mc, txn, MAIN_DBI, NULL);
rc = mdb_cursor_set(&mc, &key, &data, MDB_SET, &exact); rc = mdb_cursor_set(&mc, &key, &data, MDB_SET, &exact);
...@@ -11197,7 +11197,7 @@ int mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *db ...@@ -11197,7 +11197,7 @@ int mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *db
/* Got info, register DBI in this txn */ /* Got info, register DBI in this txn */
unsigned int slot = unused ? unused : txn->mt_numdbs; unsigned int slot = unused ? unused : txn->mt_numdbs;
txn->mt_dbxs[slot].md_name.mv_data = namedup; txn->mt_dbxs[slot].md_name.mv_data = namedup;
txn->mt_dbxs[slot].md_name.mv_size = len; txn->mt_dbxs[slot].md_name.mv_size = size;
txn->mt_dbxs[slot].md_rel = NULL; txn->mt_dbxs[slot].md_rel = NULL;
txn->mt_dbflags[slot] = dbflag; txn->mt_dbflags[slot] = dbflag;
/* txn-> and env-> are the same in read txns, use /* txn-> and env-> are the same in read txns, use
......
...@@ -254,9 +254,9 @@ int main(int argc, char *argv[]) ...@@ -254,9 +254,9 @@ int main(int argc, char *argv[])
goto env_close; goto env_close;
} }
rc = mdb_open(txn, subname, 0, &dbi); rc = mdb_dbi_open(txn, subname, 0, &dbi);
if (rc) { if (rc) {
fprintf(stderr, "mdb_open failed, error %d %s\n", rc, mdb_strerror(rc)); fprintf(stderr, "mdb_dbi_open failed, error %d %s\n", rc, mdb_strerror(rc));
goto txn_abort; goto txn_abort;
} }
...@@ -271,27 +271,22 @@ int main(int argc, char *argv[]) ...@@ -271,27 +271,22 @@ int main(int argc, char *argv[])
goto txn_abort; goto txn_abort;
} }
while ((rc = mdb_cursor_get(cursor, &key, NULL, MDB_NEXT_NODUP)) == 0) { while ((rc = mdb_cursor_get(cursor, &key, NULL, MDB_NEXT_NODUP)) == 0) {
char *str;
MDB_dbi db2; MDB_dbi db2;
if (memchr(key.mv_data, '\0', key.mv_size)) if (memchr(key.mv_data, '\0', key.mv_size-1) || ((char *)key.mv_data)[key.mv_size=1] != '\0')
continue; continue;
count++; count++;
str = malloc(key.mv_size+1); rc = mdb_dbi_open(txn, key.mv_data, 0, &db2);
memcpy(str, key.mv_data, key.mv_size);
str[key.mv_size] = '\0';
rc = mdb_open(txn, str, 0, &db2);
if (rc == MDB_SUCCESS) { if (rc == MDB_SUCCESS) {
if (list) { if (list) {
printf("%s\n", str); printf("%s\n", (char *)key.mv_data);
list++; list++;
} else { } else {
rc = dumpit(txn, db2, str); rc = dumpit(txn, db2, key.mv_data);
if (rc) if (rc)
break; break;
} }
mdb_close(env, db2); mdb_dbi_close(env, db2);
} }
free(str);
if (rc) continue; if (rc) continue;
} }
mdb_cursor_close(cursor); mdb_cursor_close(cursor);
......
...@@ -384,9 +384,9 @@ int main(int argc, char *argv[]) ...@@ -384,9 +384,9 @@ int main(int argc, char *argv[])
goto env_close; goto env_close;
} }
rc = mdb_open(txn, subname, flags|MDB_CREATE, &dbi); rc = mdb_dbi_open(txn, subname, flags|MDB_CREATE, &dbi);
if (rc) { if (rc) {
fprintf(stderr, "mdb_open failed, error %d %s\n", rc, mdb_strerror(rc)); fprintf(stderr, "mdb_dbi_open failed, error %d %s\n", rc, mdb_strerror(rc));
goto txn_abort; goto txn_abort;
} }
......
...@@ -204,9 +204,9 @@ int main(int argc, char *argv[]) ...@@ -204,9 +204,9 @@ int main(int argc, char *argv[])
printf(" Free pages: %"Yu"\n", pages); printf(" Free pages: %"Yu"\n", pages);
} }
rc = mdb_open(txn, subname, 0, &dbi); rc = mdb_dbi_open(txn, subname, 0, &dbi);
if (rc) { if (rc) {
fprintf(stderr, "mdb_open failed, error %d %s\n", rc, mdb_strerror(rc)); fprintf(stderr, "mdb_dbi_open failed, error %d %s\n", rc, mdb_strerror(rc));
goto txn_abort; goto txn_abort;
} }
...@@ -228,17 +228,12 @@ int main(int argc, char *argv[]) ...@@ -228,17 +228,12 @@ int main(int argc, char *argv[])
goto txn_abort; goto txn_abort;
} }
while ((rc = mdb_cursor_get(cursor, &key, NULL, MDB_NEXT_NODUP)) == 0) { while ((rc = mdb_cursor_get(cursor, &key, NULL, MDB_NEXT_NODUP)) == 0) {
char *str;
MDB_dbi db2; MDB_dbi db2;
if (memchr(key.mv_data, '\0', key.mv_size)) if (memchr(key.mv_data, '\0', key.mv_size-1) || ((char *)key.mv_data)[key.mv_size-1] != '\0')
continue; continue;
str = malloc(key.mv_size+1); rc = mdb_dbi_open(txn, key.mv_data, 0, &db2);
memcpy(str, key.mv_data, key.mv_size);
str[key.mv_size] = '\0';
rc = mdb_open(txn, str, 0, &db2);
if (rc == MDB_SUCCESS) if (rc == MDB_SUCCESS)
printf("Status of %s\n", str); printf("Status of %s\n", (char *)key.mv_data);
free(str);
if (rc) continue; if (rc) continue;
rc = mdb_stat(txn, db2, &mst); rc = mdb_stat(txn, db2, &mst);
if (rc) { if (rc) {
...@@ -246,7 +241,7 @@ int main(int argc, char *argv[]) ...@@ -246,7 +241,7 @@ int main(int argc, char *argv[])
goto txn_abort; goto txn_abort;
} }
prstat(&mst); prstat(&mst);
mdb_close(env, db2); mdb_dbi_close(env, db2);
} }
mdb_cursor_close(cursor); mdb_cursor_close(cursor);
} }
...@@ -254,7 +249,7 @@ int main(int argc, char *argv[]) ...@@ -254,7 +249,7 @@ int main(int argc, char *argv[])
if (rc == MDB_NOTFOUND) if (rc == MDB_NOTFOUND)
rc = MDB_SUCCESS; rc = MDB_SUCCESS;
mdb_close(env, dbi); mdb_dbi_close(env, dbi);
txn_abort: txn_abort:
mdb_txn_abort(txn); mdb_txn_abort(txn);
env_close: env_close:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment