2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997
5 * Sleepycat Software. All rights reserved.
11 static const char copyright
[] =
12 "@(#) Copyright (c) 1997\n\
13 Sleepycat Software Inc. All rights reserved.\n";
14 static const char sccsid
[] = "@(#)db_dump185.c 10.8 (Sleepycat) 9/21/97";
17 #ifndef NO_SYSTEM_INCLUDES
18 #include <sys/types.h>
32 /* Hash Table Information */
33 typedef struct hashhdr
{ /* Disk resident portion */
34 int magic
; /* Magic NO for hash tables */
35 int version
; /* Version ID */
36 u_int32_t lorder
; /* Byte Order */
37 int bsize
; /* Bucket/Page Size */
38 int bshift
; /* Bucket shift */
39 int dsize
; /* Directory Size */
40 int ssize
; /* Segment Size */
41 int sshift
; /* Segment shift */
42 int ovfl_point
; /* Where overflow pages are being
44 int last_freed
; /* Last overflow page freed */
45 int max_bucket
; /* ID of Maximum bucket in use */
46 int high_mask
; /* Mask to modulo into entire table */
47 int low_mask
; /* Mask to modulo into lower half of
49 int ffactor
; /* Fill factor */
50 int nkeys
; /* Number of keys in hash table */
53 typedef struct htab
{ /* Memory resident data structure */
54 HASHHDR hdr
; /* Header */
57 typedef struct _epgno
{
58 u_int32_t pgno
; /* the page number */
59 u_int16_t index
; /* the index on the page */
63 void *page
; /* the (pinned) page */
64 u_int16_t index
; /* the index on the page */
67 typedef struct _cursor
{
68 EPGNO pg
; /* B: Saved tree reference. */
69 DBT key
; /* B: Saved key, or key.data == NULL. */
70 u_int32_t rcursor
; /* R: recno cursor (1-based) */
72 #define CURS_ACQUIRE 0x01 /* B: Cursor needs to be reacquired. */
73 #define CURS_AFTER 0x02 /* B: Unreturned cursor after key. */
74 #define CURS_BEFORE 0x04 /* B: Unreturned cursor before key. */
75 #define CURS_INIT 0x08 /* RB: Cursor initialized. */
79 /* The in-memory btree/recno data structure. */
80 typedef struct _btree
{
81 void *bt_mp
; /* memory pool cookie */
83 void *bt_dbp
; /* pointer to enclosing DB */
85 EPG bt_cur
; /* current (pinned) page */
86 void *bt_pinned
; /* page pinned across calls */
88 CURSOR bt_cursor
; /* cursor */
90 EPGNO bt_stack
[50]; /* stack of parent pages */
91 EPGNO
*bt_sp
; /* current stack pointer */
93 DBT bt_rkey
; /* returned key */
94 DBT bt_rdata
; /* returned data */
96 int bt_fd
; /* tree file descriptor */
98 u_int32_t bt_free
; /* next free page */
99 u_int32_t bt_psize
; /* page size */
100 u_int16_t bt_ovflsize
; /* cut-off for key/data overflow */
101 int bt_lorder
; /* byte order */
103 enum { NOT
, BACK
, FORWARD
} bt_order
;
104 EPGNO bt_last
; /* last insert */
106 /* B: key comparison function */
107 int (*bt_cmp
) __P((const DBT
*, const DBT
*));
108 /* B: prefix comparison function */
109 size_t (*bt_pfx
) __P((const DBT
*, const DBT
*));
110 /* R: recno input function */
111 int (*bt_irec
) __P((struct _btree
*, u_int32_t
));
113 FILE *bt_rfp
; /* R: record FILE pointer */
114 int bt_rfd
; /* R: record file descriptor */
116 void *bt_cmap
; /* R: current point in mapped space */
117 void *bt_smap
; /* R: start of mapped space */
118 void *bt_emap
; /* R: end of mapped space */
119 size_t bt_msize
; /* R: size of mapped region. */
121 u_int32_t bt_nrecs
; /* R: number of records */
122 size_t bt_reclen
; /* R: fixed record length */
123 u_char bt_bval
; /* R: delimiting byte/pad character */
127 * B_NODUPS and R_RECNO are stored on disk, and may not be changed.
129 #define B_INMEM 0x00001 /* in-memory tree */
130 #define B_METADIRTY 0x00002 /* need to write metadata */
131 #define B_MODIFIED 0x00004 /* tree modified */
132 #define B_NEEDSWAP 0x00008 /* if byte order requires swapping */
133 #define B_RDONLY 0x00010 /* read-only tree */
135 #define B_NODUPS 0x00020 /* no duplicate keys permitted */
136 #define R_RECNO 0x00080 /* record oriented tree */
138 #define R_CLOSEFP 0x00040 /* opened a file pointer */
139 #define R_EOF 0x00100 /* end of input file reached. */
140 #define R_FIXLEN 0x00200 /* fixed length records */
141 #define R_MEMMAPPED 0x00400 /* memory mapped file. */
142 #define R_INMEM 0x00800 /* in-memory file */
143 #define R_MODIFIED 0x01000 /* modified file */
144 #define R_RDONLY 0x02000 /* read-only file */
146 #define B_DB_LOCK 0x04000 /* DB_LOCK specified. */
147 #define B_DB_SHMEM 0x08000 /* DB_SHMEM specified. */
148 #define B_DB_TXN 0x10000 /* DB_TXN specified. */
152 void db_185_btree
__P((DB
*, int));
153 void db_185_hash
__P((DB
*, int));
154 void dbt_dump
__P((DBT
*));
155 void dbt_print
__P((DBT
*));
156 int main
__P((int, char *[]));
157 void usage
__P((void));
160 *progname
= "db_dump185"; /* Program name. */
174 while ((ch
= getopt(argc
, argv
, "f:p")) != EOF
)
177 if (freopen(optarg
, "w", stdout
) == NULL
)
178 err(1, "%s", optarg
);
193 if ((dbp
= dbopen(argv
[0], O_RDONLY
, 0, DB_BTREE
, NULL
)) == NULL
) {
194 if ((dbp
= dbopen(argv
[0], O_RDONLY
, 0, DB_HASH
, NULL
)) == NULL
)
195 err(1, "%s", argv
[0]);
196 db_185_hash(dbp
, pflag
);
198 db_185_btree(dbp
, pflag
);
202 * DB 1.85 DBTs are a subset of DB 2.0 DBTs, so we just use the
203 * new dump/print routines.
206 while (!(rval
= dbp
->seq(dbp
, &key
, &data
, R_NEXT
))) {
211 while (!(rval
= dbp
->seq(dbp
, &key
, &data
, R_NEXT
))) {
223 * Dump out hash header information.
226 db_185_hash(dbp
, pflag
)
232 hashp
= dbp
->internal
;
234 printf("format=%s\n", pflag
? "print" : "bytevalue");
235 printf("type=hash\n");
236 printf("h_ffactor=%lu\n", (u_long
)hashp
->hdr
.ffactor
);
237 #ifdef NOT_AVAILABLE_IN_DB_185
238 printf("h_nelem=%lu\n", (u_long
)hashp
->hdr
.nelem
);
240 if (hashp
->hdr
.lorder
!= 0)
241 printf("db_lorder=%lu\n", (u_long
)hashp
->hdr
.lorder
);
242 printf("db_pagesize=%lu\n", (u_long
)hashp
->hdr
.bsize
);
243 printf("HEADER=END\n");
248 * Dump out btree header information.
251 db_185_btree(dbp
, pflag
)
259 printf("format=%s\n", pflag
? "print" : "bytevalue");
260 printf("type=btree\n");
261 #ifdef NOT_AVAILABLE_IN_185
262 printf("bt_minkey=%lu\n", (u_long
)XXX
);
263 printf("bt_maxkey=%lu\n", (u_long
)XXX
);
265 if (btp
->bt_lorder
!= 0)
266 printf("db_lorder=%lu\n", (u_long
)btp
->bt_lorder
);
267 printf("db_pagesize=%lu\n", (u_long
)btp
->bt_psize
);
268 if (!(btp
->flags
& B_NODUPS
))
269 printf("duplicates=1\n");
270 printf("HEADER=END\n");
273 static char hex
[] = "0123456789abcdef";
277 * Write out a key or data item using byte values.
286 for (len
= dbtp
->size
, p
= dbtp
->data
; len
--; ++p
)
288 hex
[(*p
& 0xf0) >> 4], hex
[*p
& 0x0f]);
294 * Write out a key or data item using printable characters.
303 for (len
= dbtp
->size
, p
= dbtp
->data
; len
--; ++p
)
307 (void)printf("%c", *p
);
309 (void)printf("\\%c%c",
310 hex
[(*p
& 0xf0) >> 4], hex
[*p
& 0x0f]);
316 * Display the usage message.
321 (void)fprintf(stderr
, "usage: db_dump [-p] [-f file] db_file\n");