2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1996, 2010 Oracle and/or its affiliates. All rights reserved.
12
#include "dbinc/db_page.h"
13
#include "dbinc/btree.h"
14
#include "dbinc/lock.h"
16
#include "dbinc/partition.h"
18
#ifdef HAVE_STATISTICS
21
* Gather/print the btree statistics
23
* PUBLIC: int __bam_stat __P((DBC *, void *, u_int32_t));
26
__bam_stat(dbc, spp, flags)
35
DB_LOCK lock, metalock;
40
int ret, t_ret, write_meta;
54
/* Allocate and clear the structure. */
55
if ((ret = __os_umalloc(env, sizeof(*sp), &sp)) != 0)
57
memset(sp, 0, sizeof(*sp));
59
/* Get the metadata page for the entire database. */
61
if ((ret = __db_lget(dbc, 0, pgno, DB_LOCK_READ, 0, &metalock)) != 0)
63
if ((ret = __memp_fget(mpf, &pgno,
64
dbc->thread_info, dbc->txn, 0, &meta)) != 0)
67
if (flags == DB_FAST_STAT)
70
/* Walk the metadata free list, counting pages. */
71
for (sp->bt_free = 0, pgno = meta->dbmeta.free; pgno != PGNO_INVALID;) {
74
if ((ret = __memp_fget(mpf, &pgno,
75
dbc->thread_info, dbc->txn, 0, &h)) != 0)
79
if ((ret = __memp_fput(mpf,
80
dbc->thread_info, h, dbc->priority)) != 0)
85
/* Get the root page. */
86
BAM_GET_ROOT(dbc, pgno, h, 0, DB_LOCK_READ, lock, ret);
89
DB_ASSERT(env, h != NULL);
91
/* Get the levels from the root page. */
92
sp->bt_levels = h->level;
94
/* Discard the root page. */
95
ret = __memp_fput(mpf, dbc->thread_info, h, dbc->priority);
97
if ((t_ret = __LPUT(dbc, lock)) != 0 && ret == 0)
103
if ((ret = __bam_traverse(dbc,
104
DB_LOCK_READ, PGNO_INVALID, __bam_stat_callback, sp)) != 0)
107
#ifdef HAVE_COMPRESSION
108
if (DB_IS_COMPRESSED(dbp) && (ret = __bam_compress_count(dbc,
109
&sp->bt_nkeys, &sp->bt_ndata)) != 0)
114
* Get the subdatabase metadata page if it's not the same as the
115
* one we already have.
117
write_meta = !F_ISSET(dbp, DB_AM_RDONLY) &&
118
(!MULTIVERSION(dbp) || dbc->txn != NULL);
120
if (t->bt_meta != PGNO_BASE_MD || write_meta) {
121
ret = __memp_fput(mpf, dbc->thread_info, meta, dbc->priority);
123
if ((t_ret = __LPUT(dbc, metalock)) != 0 && ret == 0)
128
if ((ret = __db_lget(dbc,
129
0, t->bt_meta, write_meta ? DB_LOCK_WRITE : DB_LOCK_READ,
132
if ((ret = __memp_fget(mpf, &t->bt_meta,
133
dbc->thread_info, dbc->txn,
134
write_meta ? DB_MPOOL_DIRTY : 0, &meta)) != 0)
137
if (flags == DB_FAST_STAT) {
138
if (dbp->type == DB_RECNO ||
139
(dbp->type == DB_BTREE && F_ISSET(dbp, DB_AM_RECNUM))) {
140
BAM_GET_ROOT(dbc, pgno, h, 0, DB_LOCK_READ, lock, ret);
144
sp->bt_nkeys = RE_NREC(h);
146
sp->bt_nkeys = meta->dbmeta.key_count;
148
sp->bt_ndata = dbp->type == DB_RECNO ?
149
sp->bt_nkeys : meta->dbmeta.record_count;
152
/* Get metadata page statistics. */
153
sp->bt_metaflags = meta->dbmeta.flags;
154
sp->bt_minkey = meta->minkey;
155
sp->bt_re_len = meta->re_len;
156
sp->bt_re_pad = meta->re_pad;
158
* Don't take the page number from the meta-data page -- that value is
159
* only maintained in the primary database, we may have been called on
160
* a subdatabase. (Yes, I read the primary database meta-data page
161
* earlier in this function, but I'm asking the underlying cache so the
162
* code for the Hash and Btree methods is the same.)
164
if ((ret = __memp_get_last_pgno(dbp->mpf, &pgno)) != 0)
166
sp->bt_pagecnt = pgno + 1;
167
sp->bt_pagesize = meta->dbmeta.pagesize;
168
sp->bt_magic = meta->dbmeta.magic;
169
sp->bt_version = meta->dbmeta.version;
171
if (write_meta != 0) {
172
meta->dbmeta.key_count = sp->bt_nkeys;
173
meta->dbmeta.record_count = sp->bt_ndata;
176
*(DB_BTREE_STAT **)spp = sp;
178
err: /* Discard the second page. */
179
if ((t_ret = __LPUT(dbc, lock)) != 0 && ret == 0)
181
if (h != NULL && (t_ret = __memp_fput(mpf,
182
dbc->thread_info, h, dbc->priority)) != 0 && ret == 0)
185
/* Discard the metadata page. */
186
if ((t_ret = __LPUT(dbc, metalock)) != 0 && ret == 0)
188
if (meta != NULL && (t_ret = __memp_fput(mpf,
189
dbc->thread_info, meta, dbc->priority)) != 0 && ret == 0)
192
if (ret != 0 && sp != NULL) {
194
*(DB_BTREE_STAT **)spp = NULL;
201
* __bam_stat_print --
202
* Display btree/recno statistics.
204
* PUBLIC: int __bam_stat_print __P((DBC *, u_int32_t));
207
__bam_stat_print(dbc, flags)
211
static const FN fn[] = {
212
{ BTM_DUP, "duplicates" },
213
{ BTM_RECNO, "recno" },
214
{ BTM_RECNUM, "record-numbers" },
215
{ BTM_FIXEDLEN, "fixed-length" },
216
{ BTM_RENUMBER, "renumber" },
217
{ BTM_SUBDB, "multiple-databases" },
218
{ BTM_DUPSORT, "sorted duplicates" },
219
{ BTM_COMPRESS, "compressed" },
230
#ifdef HAVE_PARTITION
231
if (DB_IS_PARTITIONED(dbp)) {
232
if ((ret = __partition_stat(dbc, &sp, flags)) != 0)
236
if ((ret = __bam_stat(dbc, &sp, LF_ISSET(DB_FAST_STAT))) != 0)
239
if (LF_ISSET(DB_STAT_ALL)) {
240
__db_msg(env, "%s", DB_GLOBAL(db_line));
241
__db_msg(env, "Default Btree/Recno database information:");
244
__db_msg(env, "%lx\tBtree magic number", (u_long)sp->bt_magic);
245
__db_msg(env, "%lu\tBtree version number", (u_long)sp->bt_version);
247
(void)__db_get_lorder(dbp, &lorder);
256
s = "Unrecognized byte order";
259
__db_msg(env, "%s\tByte order", s);
260
__db_prflags(env, NULL, sp->bt_metaflags, fn, NULL, "\tFlags");
261
if (dbp->type == DB_BTREE)
262
__db_dl(env, "Minimum keys per-page", (u_long)sp->bt_minkey);
263
if (dbp->type == DB_RECNO) {
265
"Fixed-length record size", (u_long)sp->bt_re_len);
267
"%#x\tFixed-length record pad", (u_int)sp->bt_re_pad);
270
"Underlying database page size", (u_long)sp->bt_pagesize);
271
if (dbp->type == DB_BTREE)
272
__db_dl(env, "Overflow key/data size",
273
((BTREE_CURSOR *)dbc->internal)->ovflsize);
274
__db_dl(env, "Number of levels in the tree", (u_long)sp->bt_levels);
275
__db_dl(env, dbp->type == DB_BTREE ?
276
"Number of unique keys in the tree" :
277
"Number of records in the tree", (u_long)sp->bt_nkeys);
279
"Number of data items in the tree", (u_long)sp->bt_ndata);
282
"Number of tree internal pages", (u_long)sp->bt_int_pg);
284
"Number of bytes free in tree internal pages",
285
(u_long)sp->bt_int_pgfree,
286
DB_PCT_PG(sp->bt_int_pgfree, sp->bt_int_pg, sp->bt_pagesize), "ff");
289
"Number of tree leaf pages", (u_long)sp->bt_leaf_pg);
290
__db_dl_pct(env, "Number of bytes free in tree leaf pages",
291
(u_long)sp->bt_leaf_pgfree, DB_PCT_PG(
292
sp->bt_leaf_pgfree, sp->bt_leaf_pg, sp->bt_pagesize), "ff");
295
"Number of tree duplicate pages", (u_long)sp->bt_dup_pg);
297
"Number of bytes free in tree duplicate pages",
298
(u_long)sp->bt_dup_pgfree,
299
DB_PCT_PG(sp->bt_dup_pgfree, sp->bt_dup_pg, sp->bt_pagesize), "ff");
302
"Number of tree overflow pages", (u_long)sp->bt_over_pg);
303
__db_dl_pct(env, "Number of bytes free in tree overflow pages",
304
(u_long)sp->bt_over_pgfree, DB_PCT_PG(
305
sp->bt_over_pgfree, sp->bt_over_pg, sp->bt_pagesize), "ff");
306
__db_dl(env, "Number of empty pages", (u_long)sp->bt_empty_pg);
308
__db_dl(env, "Number of pages on the free list", (u_long)sp->bt_free);
316
* __bam_stat_callback --
317
* Statistics callback.
319
* PUBLIC: int __bam_stat_callback __P((DBC *, PAGE *, void *, int *));
322
__bam_stat_callback(dbc, h, cookie, putp)
330
db_indx_t indx, *inp, top;
343
sp->bt_int_pgfree += P_FREESPACE(dbp, h);
349
/* Correct for on-page duplicates and deleted items. */
350
for (indx = 0; indx < top; indx += P_INDX) {
351
type = GET_BKEYDATA(dbp, h, indx + O_INDX)->type;
352
/* Ignore deleted items. */
356
/* Ignore duplicate keys. */
357
if (indx + P_INDX >= top ||
358
inp[indx] != inp[indx + P_INDX])
361
/* Ignore off-page duplicates. */
362
if (B_TYPE(type) != B_DUPLICATE)
367
sp->bt_leaf_pgfree += P_FREESPACE(dbp, h);
374
* If walking a recno tree, then each of these items is a key.
375
* Otherwise, we're walking an off-page duplicate set.
377
if (dbp->type == DB_RECNO) {
379
* Correct for deleted items in non-renumbering Recno
382
if (F_ISSET(dbp, DB_AM_RENUMBER)) {
386
for (indx = 0; indx < top; indx += O_INDX) {
387
type = GET_BKEYDATA(dbp, h, indx)->type;
388
if (!B_DISSET(type)) {
395
sp->bt_leaf_pgfree += P_FREESPACE(dbp, h);
400
sp->bt_dup_pgfree += P_FREESPACE(dbp, h);
407
/* Correct for deleted items. */
408
for (indx = 0; indx < top; indx += O_INDX)
409
if (!B_DISSET(GET_BKEYDATA(dbp, h, indx)->type))
413
sp->bt_dup_pgfree += P_FREESPACE(dbp, h);
417
sp->bt_over_pgfree += P_OVFLSPACE(dbp, dbp->pgsize, h);
420
return (__db_pgfmt(dbp->env, h->pgno));
426
* __bam_print_cursor --
427
* Display the current internal cursor.
429
* PUBLIC: void __bam_print_cursor __P((DBC *));
432
__bam_print_cursor(dbc)
435
static const FN fn[] = {
436
{ C_DELETED, "C_DELETED" },
437
{ C_RECNUM, "C_RECNUM" },
438
{ C_RENUMBER, "C_RENUMBER" },
445
cp = (BTREE_CURSOR *)dbc->internal;
447
STAT_ULONG("Overflow size", cp->ovflsize);
448
if (dbc->dbtype == DB_RECNO)
449
STAT_ULONG("Recno", cp->recno);
450
STAT_ULONG("Order", cp->order);
451
__db_prflags(env, NULL, cp->flags, fn, NULL, "\tInternal Flags");
454
#else /* !HAVE_STATISTICS */
457
__bam_stat(dbc, spp, flags)
462
COMPQUIET(spp, NULL);
465
return (__db_stat_not_built(dbc->env));
469
__bam_stat_print(dbc, flags)
475
return (__db_stat_not_built(dbc->env));
482
* Return proportion of keys relative to given key. The numbers are
483
* slightly skewed due to on page duplicates.
485
* PUBLIC: int __bam_key_range __P((DBC *, DBT *, DB_KEY_RANGE *, u_int32_t));
488
__bam_key_range(dbc, dbt, kp, flags)
501
if ((ret = __bam_search(dbc, PGNO_INVALID,
502
dbt, SR_STK_ONLY, 1, NULL, &exact)) != 0)
505
cp = (BTREE_CURSOR *)dbc->internal;
506
kp->less = kp->greater = 0.0;
510
/* Correct the leaf page. */
511
cp->csp->entries /= 2;
513
for (sp = cp->sp; sp <= cp->csp; ++sp) {
515
* At each level we know that pages greater than indx contain
516
* keys greater than what we are looking for and those less
517
* than indx are less than. The one pointed to by indx may
518
* have some less, some greater or even equal. If indx is
519
* equal to the number of entries, then the key is out of range
520
* and everything is less.
523
kp->greater += factor * (sp->entries - 1)/sp->entries;
524
else if (sp->indx == sp->entries)
527
kp->less += factor * sp->indx / sp->entries;
528
kp->greater += factor *
529
((sp->entries - sp->indx) - 1) / sp->entries;
531
factor *= 1.0/sp->entries;
535
* If there was an exact match then assign 1 n'th to the key itself.
536
* Otherwise that factor belongs to those greater than the key, unless
537
* the key was out of range.
543
kp->greater += factor;
547
if ((ret = __bam_stkrel(dbc, 0)) != 0)
556
* Walk a Btree database.
558
* PUBLIC: int __bam_traverse __P((DBC *, db_lockmode_t,
559
* PUBLIC: db_pgno_t, int (*)(DBC *, PAGE *, void *, int *), void *));
562
__bam_traverse(dbc, mode, root_pgno, callback, cookie)
566
int (*callback)__P((DBC *, PAGE *, void *, int *));
576
db_indx_t indx, *inp;
577
int already_put, ret, t_ret;
585
BAM_GET_ROOT(dbc, root_pgno, h, 0, mode, lock, ret);
591
for (indx = 0; indx < NUM_ENT(h); indx += O_INDX) {
592
bi = GET_BINTERNAL(dbp, h, indx);
593
if (B_TYPE(bi->type) == B_OVERFLOW &&
594
(ret = __db_traverse_big(dbc,
595
((BOVERFLOW *)bi->data)->pgno,
596
callback, cookie)) != 0)
598
if ((ret = __bam_traverse(
599
dbc, mode, bi->pgno, callback, cookie)) != 0)
604
for (indx = 0; indx < NUM_ENT(h); indx += O_INDX) {
605
ri = GET_RINTERNAL(dbp, h, indx);
606
if ((ret = __bam_traverse(
607
dbc, mode, ri->pgno, callback, cookie)) != 0)
613
for (indx = 0; indx < NUM_ENT(h); indx += P_INDX) {
614
bk = GET_BKEYDATA(dbp, h, indx);
615
if (B_TYPE(bk->type) == B_OVERFLOW &&
616
(indx + P_INDX >= NUM_ENT(h) ||
617
inp[indx] != inp[indx + P_INDX])) {
618
if ((ret = __db_traverse_big(dbc,
619
GET_BOVERFLOW(dbp, h, indx)->pgno,
620
callback, cookie)) != 0)
623
bk = GET_BKEYDATA(dbp, h, indx + O_INDX);
624
if (B_TYPE(bk->type) == B_DUPLICATE &&
625
(ret = __bam_traverse(dbc, mode,
626
GET_BOVERFLOW(dbp, h, indx + O_INDX)->pgno,
627
callback, cookie)) != 0)
629
if (B_TYPE(bk->type) == B_OVERFLOW &&
630
(ret = __db_traverse_big(dbc,
631
GET_BOVERFLOW(dbp, h, indx + O_INDX)->pgno,
632
callback, cookie)) != 0)
638
for (indx = 0; indx < NUM_ENT(h); indx += O_INDX) {
639
bk = GET_BKEYDATA(dbp, h, indx);
640
if (B_TYPE(bk->type) == B_OVERFLOW &&
641
(ret = __db_traverse_big(dbc,
642
GET_BOVERFLOW(dbp, h, indx)->pgno,
643
callback, cookie)) != 0)
648
return (__db_pgfmt(dbp->env, h->pgno));
651
ret = callback(dbc, h, cookie, &already_put);
653
err: if (!already_put && (t_ret = __memp_fput(mpf,
654
dbc->thread_info, h, dbc->priority)) != 0 && ret == 0)
656
err1: if ((t_ret = __TLPUT(dbc, lock)) != 0 && ret == 0)