1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 1998,2008 Oracle.  All rights reserved.
5 *
6 * $Id: bt_reclaim.c,v 12.12 2008/01/31 14:58:41 bostic Exp $
7 */
8
9#include "db_config.h"
10
11#include "db_int.h"
12#include "dbinc/db_page.h"
13#include "dbinc/btree.h"
14#include "dbinc/lock.h"
15
16/*
17 * __bam_reclaim --
18 *	Free a database.
19 *
20 * PUBLIC: int __bam_reclaim __P((DB *, DB_THREAD_INFO *, DB_TXN *));
21 */
22int
23__bam_reclaim(dbp, ip, txn)
24	DB *dbp;
25	DB_THREAD_INFO *ip;
26	DB_TXN *txn;
27{
28	DBC *dbc;
29	DB_LOCK meta_lock;
30	int ret, t_ret;
31
32	/* Acquire a cursor. */
33	if ((ret = __db_cursor(dbp, ip, txn, &dbc, 0)) != 0)
34		return (ret);
35
36	/* Write lock the metapage for deallocations. */
37	if ((ret = __db_lget(dbc,
38	    0, PGNO_BASE_MD, DB_LOCK_WRITE, 0, &meta_lock)) != 0)
39		goto err;
40
41	/* Avoid locking every page, we have the handle locked exclusive. */
42	F_SET(dbc, DBC_DONTLOCK);
43
44	/* Walk the tree, freeing pages. */
45	ret = __bam_traverse(dbc,
46	    DB_LOCK_WRITE, dbc->internal->root, __db_reclaim_callback, NULL);
47
48	if ((t_ret = __TLPUT(dbc, meta_lock)) != 0 && ret == 0)
49		ret = t_ret;
50
51	/* Discard the cursor. */
52err:	if ((t_ret = __dbc_close(dbc)) != 0 && ret == 0)
53		ret = t_ret;
54
55	return (ret);
56}
57
58/*
59 * __bam_truncate --
60 *	Truncate a database.
61 *
62 * PUBLIC: int __bam_truncate __P((DBC *, u_int32_t *));
63 */
64int
65__bam_truncate(dbc, countp)
66	DBC *dbc;
67	u_int32_t *countp;
68{
69	u_int32_t count;
70	int ret;
71
72	count = 0;
73
74	/* Walk the tree, freeing pages. */
75	ret = __bam_traverse(dbc,
76	    DB_LOCK_WRITE, dbc->internal->root, __db_truncate_callback, &count);
77
78	if (countp != NULL)
79		*countp = count;
80
81	return (ret);
82}
83