]> git.sur5r.net Git - openldap/blob - libraries/libmdb/mdb_stata.c
ITS#7369 mdb_stat: cleanup in case something went wrong.
[openldap] / libraries / libmdb / mdb_stata.c
1 /* mdb_stat.c - memory-mapped database status tool */
2 /*
3  * Copyright 2011 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 "mdb.h"
18
19 int main(int argc,char * argv[])
20 {
21         int rc;
22         MDB_env *env;
23         MDB_txn *txn;
24         MDB_dbi dbi;
25         MDB_stat mst;
26         MDB_cursor *cursor;
27         MDB_val key;
28         char *envname = argv[1];
29
30         rc = mdb_env_create(&env);
31
32         mdb_env_set_maxdbs(env, 4);
33
34         rc = mdb_env_open(env, envname, MDB_RDONLY, 0);
35         if (rc) {
36                 printf("mdb_env_open failed, error %d\n", rc);
37                 goto env_close;
38         }
39         rc = mdb_txn_begin(env, NULL, 1, &txn);
40         if (rc) {
41                 printf("mdb_txn_begin failed, error %d\n", rc);
42                 goto env_close;
43         }
44         rc = mdb_open(txn, NULL, 0, &dbi);
45         if (rc) {
46                 printf("mdb_open failed, error %d\n", rc);
47                 goto txn_abort;
48         }
49    
50         rc = mdb_stat(txn, dbi, &mst);
51         printf("Page size: %u\n", mst.ms_psize);
52         printf("Tree depth: %u\n", mst.ms_depth);
53         printf("Branch pages: %zu\n", mst.ms_branch_pages);
54         printf("Leaf pages: %zu\n", mst.ms_leaf_pages);
55         printf("Overflow pages: %zu\n", mst.ms_overflow_pages);
56         printf("Entries: %zu\n", mst.ms_entries);
57
58         rc = mdb_cursor_open(txn, dbi, &cursor);
59         while ((rc = mdb_cursor_get(cursor, &key, NULL, MDB_NEXT)) == 0) {
60                 char *str = malloc(key.mv_size+1);
61                 MDB_dbi db2;
62                 memcpy(str, key.mv_data, key.mv_size);
63                 str[key.mv_size] = '\0';
64                 printf("\n%s\n", str);
65                 rc = mdb_open(txn, str, 0, &db2);
66                 if (rc) break;
67                 free(str);
68                 rc = mdb_stat(txn, db2, &mst);
69                 printf("Tree depth: %u\n", mst.ms_depth);
70                 printf("Branch pages: %zu\n", mst.ms_branch_pages);
71                 printf("Leaf pages: %zu\n", mst.ms_leaf_pages);
72                 printf("Overflow pages: %zu\n", mst.ms_overflow_pages);
73                 printf("Entries: %zu\n", mst.ms_entries);
74                 mdb_close(env, db2);
75         }
76         mdb_cursor_close(cursor);
77         mdb_close(env, dbi);
78 txn_abort:
79         mdb_txn_abort(txn);
80 env_close:
81         mdb_env_close(env);
82
83         return rc ? EXIT_FAILURE : EXIT_SUCCESS;
84 }