Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

db_dump185.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1996-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: db_dump185.c,v 12.1 2005/06/16 20:21:22 bostic Exp $
00008  */
00009 
00010 #ifndef lint
00011 static char copyright[] =
00012     "Copyright (c) 1996-2004\nSleepycat Software Inc.  All rights reserved.\n";
00013 #endif
00014 
00015 #include <sys/types.h>
00016 
00017 #include <ctype.h>
00018 #include <errno.h>
00019 #include <fcntl.h>
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include <string.h>
00023 
00024 #include <db.h>
00025 
00026 /* Hash Table Information */
00027 typedef struct hashhdr185 {             /* Disk resident portion */
00028         int             magic;          /* Magic NO for hash tables */
00029         int             version;        /* Version ID */
00030         u_int32_t       lorder;         /* Byte Order */
00031         int             bsize;          /* Bucket/Page Size */
00032         int             bshift;         /* Bucket shift */
00033         int             dsize;          /* Directory Size */
00034         int             ssize;          /* Segment Size */
00035         int             sshift;         /* Segment shift */
00036         int             ovfl_point;     /* Where overflow pages are being
00037                                          * allocated */
00038         int             last_freed;     /* Last overflow page freed */
00039         int             max_bucket;     /* ID of Maximum bucket in use */
00040         int             high_mask;      /* Mask to modulo into entire table */
00041         int             low_mask;       /* Mask to modulo into lower half of
00042                                          * table */
00043         int             ffactor;        /* Fill factor */
00044         int             nkeys;          /* Number of keys in hash table */
00045 } HASHHDR185;
00046 typedef struct htab185   {              /* Memory resident data structure */
00047         HASHHDR185      hdr;            /* Header */
00048 } HTAB185;
00049 
00050 /* Hash Table Information */
00051 typedef struct hashhdr186 {     /* Disk resident portion */
00052         int32_t magic;          /* Magic NO for hash tables */
00053         int32_t version;        /* Version ID */
00054         int32_t lorder;         /* Byte Order */
00055         int32_t bsize;          /* Bucket/Page Size */
00056         int32_t bshift;         /* Bucket shift */
00057         int32_t ovfl_point;     /* Where overflow pages are being allocated */
00058         int32_t last_freed;     /* Last overflow page freed */
00059         int32_t max_bucket;     /* ID of Maximum bucket in use */
00060         int32_t high_mask;      /* Mask to modulo into entire table */
00061         int32_t low_mask;       /* Mask to modulo into lower half of table */
00062         int32_t ffactor;        /* Fill factor */
00063         int32_t nkeys;          /* Number of keys in hash table */
00064         int32_t hdrpages;       /* Size of table header */
00065         int32_t h_charkey;      /* value of hash(CHARKEY) */
00066 #define NCACHED 32              /* number of bit maps and spare points */
00067         int32_t spares[NCACHED];/* spare pages for overflow */
00068                                 /* address of overflow page bitmaps */
00069         u_int16_t bitmaps[NCACHED];
00070 } HASHHDR186;
00071 typedef struct htab186   {              /* Memory resident data structure */
00072         void *unused[2];
00073         HASHHDR186      hdr;            /* Header */
00074 } HTAB186;
00075 
00076 typedef struct _epgno {
00077         u_int32_t pgno;                 /* the page number */
00078         u_int16_t index;                /* the index on the page */
00079 } EPGNO;
00080 
00081 typedef struct _epg {
00082         void    *page;                  /* the (pinned) page */
00083         u_int16_t index;                /* the index on the page */
00084 } EPG;
00085 
00086 typedef struct _cursor {
00087         EPGNO    pg;                    /* B: Saved tree reference. */
00088         DBT      key;                   /* B: Saved key, or key.data == NULL. */
00089         u_int32_t rcursor;              /* R: recno cursor (1-based) */
00090 
00091 #define CURS_ACQUIRE    0x01            /*  B: Cursor needs to be reacquired. */
00092 #define CURS_AFTER      0x02            /*  B: Unreturned cursor after key. */
00093 #define CURS_BEFORE     0x04            /*  B: Unreturned cursor before key. */
00094 #define CURS_INIT       0x08            /* RB: Cursor initialized. */
00095         u_int8_t flags;
00096 } CURSOR;
00097 
00098 /* The in-memory btree/recno data structure. */
00099 typedef struct _btree {
00100         void     *bt_mp;                /* memory pool cookie */
00101 
00102         void     *bt_dbp;               /* pointer to enclosing DB */
00103 
00104         EPG       bt_cur;               /* current (pinned) page */
00105         void     *bt_pinned;            /* page pinned across calls */
00106 
00107         CURSOR    bt_cursor;            /* cursor */
00108 
00109         EPGNO     bt_stack[50];         /* stack of parent pages */
00110         EPGNO    *bt_sp;                /* current stack pointer */
00111 
00112         DBT       bt_rkey;              /* returned key */
00113         DBT       bt_rdata;             /* returned data */
00114 
00115         int       bt_fd;                /* tree file descriptor */
00116 
00117         u_int32_t bt_free;              /* next free page */
00118         u_int32_t bt_psize;             /* page size */
00119         u_int16_t bt_ovflsize;          /* cut-off for key/data overflow */
00120         int       bt_lorder;            /* byte order */
00121                                         /* sorted order */
00122         enum { NOT, BACK, FORWARD } bt_order;
00123         EPGNO     bt_last;              /* last insert */
00124 
00125                                         /* B: key comparison function */
00126         int     (*bt_cmp) __P((DBT *, DBT *));
00127                                         /* B: prefix comparison function */
00128         size_t  (*bt_pfx) __P((DBT *, DBT *));
00129                                         /* R: recno input function */
00130         int     (*bt_irec) __P((struct _btree *, u_int32_t));
00131 
00132         FILE     *bt_rfp;               /* R: record FILE pointer */
00133         int       bt_rfd;               /* R: record file descriptor */
00134 
00135         void     *bt_cmap;              /* R: current point in mapped space */
00136         void     *bt_smap;              /* R: start of mapped space */
00137         void     *bt_emap;              /* R: end of mapped space */
00138         size_t    bt_msize;             /* R: size of mapped region. */
00139 
00140         u_int32_t bt_nrecs;             /* R: number of records */
00141         size_t    bt_reclen;            /* R: fixed record length */
00142         u_char    bt_bval;              /* R: delimiting byte/pad character */
00143 
00144 /*
00145  * NB:
00146  * B_NODUPS and R_RECNO are stored on disk, and may not be changed.
00147  */
00148 #define B_INMEM         0x00001         /* in-memory tree */
00149 #define B_METADIRTY     0x00002         /* need to write metadata */
00150 #define B_MODIFIED      0x00004         /* tree modified */
00151 #define B_NEEDSWAP      0x00008         /* if byte order requires swapping */
00152 #define B_RDONLY        0x00010         /* read-only tree */
00153 
00154 #define B_NODUPS        0x00020         /* no duplicate keys permitted */
00155 #define R_RECNO         0x00080         /* record oriented tree */
00156 
00157 #define R_CLOSEFP       0x00040         /* opened a file pointer */
00158 #define R_EOF           0x00100         /* end of input file reached. */
00159 #define R_FIXLEN        0x00200         /* fixed length records */
00160 #define R_MEMMAPPED     0x00400         /* memory mapped file. */
00161 #define R_INMEM         0x00800         /* in-memory file */
00162 #define R_MODIFIED      0x01000         /* modified file */
00163 #define R_RDONLY        0x02000         /* read-only file */
00164 
00165 #define B_DB_LOCK       0x04000         /* DB_LOCK specified. */
00166 #define B_DB_SHMEM      0x08000         /* DB_SHMEM specified. */
00167 #define B_DB_TXN        0x10000         /* DB_TXN specified. */
00168         u_int32_t flags;
00169 } BTREE;
00170 
00171 void    db_btree __P((DB *, int));
00172 void    db_hash __P((DB *, int));
00173 void    dbt_dump __P((DBT *));
00174 void    dbt_print __P((DBT *));
00175 int     main __P((int, char *[]));
00176 int     usage __P((void));
00177 
00178 int
00179 main(argc, argv)
00180         int argc;
00181         char *argv[];
00182 {
00183         extern char *optarg;
00184         extern int optind;
00185         DB *dbp;
00186         DBT key, data;
00187         int ch, pflag, rval;
00188 
00189         pflag = 0;
00190         while ((ch = getopt(argc, argv, "f:p")) != EOF)
00191                 switch (ch) {
00192                 case 'f':
00193                         if (freopen(optarg, "w", stdout) == NULL) {
00194                                 fprintf(stderr, "db_dump185: %s: %s\n",
00195                                     optarg, strerror(errno));
00196                                 return (EXIT_FAILURE);
00197                         }
00198                         break;
00199                 case 'p':
00200                         pflag = 1;
00201                         break;
00202                 case '?':
00203                 default:
00204                         return (usage());
00205                 }
00206         argc -= optind;
00207         argv += optind;
00208 
00209         if (argc != 1)
00210                 return (usage());
00211 
00212         if ((dbp = dbopen(argv[0], O_RDONLY, 0, DB_BTREE, NULL)) == NULL) {
00213                 if ((dbp =
00214                     dbopen(argv[0], O_RDONLY, 0, DB_HASH, NULL)) == NULL) {
00215                         fprintf(stderr,
00216                             "db_dump185: %s: %s\n", argv[0], strerror(errno));
00217                         return (EXIT_FAILURE);
00218                 }
00219                 db_hash(dbp, pflag);
00220         } else
00221                 db_btree(dbp, pflag);
00222 
00223         /*
00224          * !!!
00225          * DB 1.85 DBTs are a subset of DB 2.0 DBTs, so we just use the
00226          * new dump/print routines.
00227          */
00228         if (pflag)
00229                 while (!(rval = dbp->seq(dbp, &key, &data, R_NEXT))) {
00230                         dbt_print(&key);
00231                         dbt_print(&data);
00232                 }
00233         else
00234                 while (!(rval = dbp->seq(dbp, &key, &data, R_NEXT))) {
00235                         dbt_dump(&key);
00236                         dbt_dump(&data);
00237                 }
00238 
00239         if (rval == -1) {
00240                 fprintf(stderr, "db_dump185: seq: %s\n", strerror(errno));
00241                 return (EXIT_FAILURE);
00242         }
00243         return (EXIT_SUCCESS);
00244 }
00245 
00246 /*
00247  * db_hash --
00248  *      Dump out hash header information.
00249  */
00250 void
00251 db_hash(dbp, pflag)
00252         DB *dbp;
00253         int pflag;
00254 {
00255         HTAB185 *hash185p;
00256         HTAB186 *hash186p;
00257 
00258         printf("format=%s\n", pflag ? "print" : "bytevalue");
00259         printf("type=hash\n");
00260 
00261         /* DB 1.85 was version 2, DB 1.86 was version 3. */
00262         hash185p = dbp->internal;
00263         if (hash185p->hdr.version > 2) {
00264                 hash186p = dbp->internal;
00265                 printf("h_ffactor=%lu\n", (u_long)hash186p->hdr.ffactor);
00266                 if (hash186p->hdr.lorder != 0)
00267                         printf("db_lorder=%lu\n", (u_long)hash186p->hdr.lorder);
00268                 printf("db_pagesize=%lu\n", (u_long)hash186p->hdr.bsize);
00269         } else {
00270                 printf("h_ffactor=%lu\n", (u_long)hash185p->hdr.ffactor);
00271                 if (hash185p->hdr.lorder != 0)
00272                         printf("db_lorder=%lu\n", (u_long)hash185p->hdr.lorder);
00273                 printf("db_pagesize=%lu\n", (u_long)hash185p->hdr.bsize);
00274         }
00275         printf("HEADER=END\n");
00276 }
00277 
00278 /*
00279  * db_btree --
00280  *      Dump out btree header information.
00281  */
00282 void
00283 db_btree(dbp, pflag)
00284         DB *dbp;
00285         int pflag;
00286 {
00287         BTREE *btp;
00288 
00289         btp = dbp->internal;
00290 
00291         printf("format=%s\n", pflag ? "print" : "bytevalue");
00292         printf("type=btree\n");
00293 #ifdef NOT_AVAILABLE_IN_185
00294         printf("bt_minkey=%lu\n", (u_long)XXX);
00295         printf("bt_maxkey=%lu\n", (u_long)XXX);
00296 #endif
00297         if (btp->bt_lorder != 0)
00298                 printf("db_lorder=%lu\n", (u_long)btp->bt_lorder);
00299         printf("db_pagesize=%lu\n", (u_long)btp->bt_psize);
00300         if (!(btp->flags & B_NODUPS))
00301                 printf("duplicates=1\n");
00302         printf("HEADER=END\n");
00303 }
00304 
00305 static char hex[] = "0123456789abcdef";
00306 
00307 /*
00308  * dbt_dump --
00309  *      Write out a key or data item using byte values.
00310  */
00311 void
00312 dbt_dump(dbtp)
00313         DBT *dbtp;
00314 {
00315         size_t len;
00316         u_int8_t *p;
00317 
00318         for (len = dbtp->size, p = dbtp->data; len--; ++p)
00319                 (void)printf("%c%c",
00320                     hex[(*p & 0xf0) >> 4], hex[*p & 0x0f]);
00321         printf("\n");
00322 }
00323 
00324 /*
00325  * dbt_print --
00326  *      Write out a key or data item using printable characters.
00327  */
00328 void
00329 dbt_print(dbtp)
00330         DBT *dbtp;
00331 {
00332         size_t len;
00333         u_int8_t *p;
00334 
00335         for (len = dbtp->size, p = dbtp->data; len--; ++p)
00336                 if (isprint((int)*p)) {
00337                         if (*p == '\\')
00338                                 (void)printf("\\");
00339                         (void)printf("%c", *p);
00340                 } else
00341                         (void)printf("\\%c%c",
00342                             hex[(*p & 0xf0) >> 4], hex[*p & 0x0f]);
00343         printf("\n");
00344 }
00345 
00346 /*
00347  * usage --
00348  *      Display the usage message.
00349  */
00350 int
00351 usage()
00352 {
00353         (void)fprintf(stderr, "usage: db_dump185 [-p] [-f file] db_file\n");
00354         return (EXIT_FAILURE);
00355 }

Generated on Sun Dec 25 12:14:22 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2