summaryrefslogtreecommitdiff
path: root/lib/tdb2/traverse.c
blob: a93e4d1d682ebc3979b77d72b94f7a506a4792fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
 /*
   Trivial Database 2: traverse function.
   Copyright (C) Rusty Russell 2010

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 3 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include "private.h"
#include <ccan/likely/likely.h>

_PUBLIC_ int64_t tdb_traverse_(struct tdb_context *tdb,
		      int (*fn)(struct tdb_context *,
				TDB_DATA, TDB_DATA, void *),
		      void *p)
{
	enum TDB_ERROR ecode;
	struct traverse_info tinfo;
	struct tdb_data k, d;
	int64_t count = 0;

	if (tdb->flags & TDB_VERSION1) {
		count = tdb1_traverse(tdb, fn, p);
		if (count == -1)
			return TDB_ERR_TO_OFF(tdb->last_error);
		return count;
	}

	k.dptr = NULL;
	for (ecode = first_in_hash(tdb, &tinfo, &k, &d.dsize);
	     ecode == TDB_SUCCESS;
	     ecode = next_in_hash(tdb, &tinfo, &k, &d.dsize)) {
		d.dptr = k.dptr + k.dsize;

		count++;
		if (fn && fn(tdb, k, d, p)) {
			free(k.dptr);
			tdb->last_error = TDB_SUCCESS;
			return count;
		}
		free(k.dptr);
	}

	if (ecode != TDB_ERR_NOEXIST) {
		return TDB_ERR_TO_OFF(tdb->last_error = ecode);
	}
	tdb->last_error = TDB_SUCCESS;
	return count;
}

_PUBLIC_ enum TDB_ERROR tdb_firstkey(struct tdb_context *tdb, struct tdb_data *key)
{
	struct traverse_info tinfo;

	if (tdb->flags & TDB_VERSION1) {
		tdb->last_error = TDB_SUCCESS;
		*key = tdb1_firstkey(tdb);
		/* TDB1 didn't set error for last key. */
		if (!key->dptr && tdb->last_error == TDB_SUCCESS) {
			tdb->last_error = TDB_ERR_NOEXIST;
		}
		return tdb->last_error;
	}

	return tdb->last_error = first_in_hash(tdb, &tinfo, key, NULL);
}

/* We lock twice, not very efficient.  We could keep last key & tinfo cached. */
_PUBLIC_ enum TDB_ERROR tdb_nextkey(struct tdb_context *tdb, struct tdb_data *key)
{
	struct traverse_info tinfo;
	struct hash_info h;
	struct tdb_used_record rec;

	if (tdb->flags & TDB_VERSION1) {
		struct tdb_data last_key = *key;
		tdb->last_error = TDB_SUCCESS;
		*key = tdb1_nextkey(tdb, last_key);
		free(last_key.dptr);
		/* TDB1 didn't set error for last key. */
		if (!key->dptr && tdb->last_error == TDB_SUCCESS) {
			tdb->last_error = TDB_ERR_NOEXIST;
		}
		return tdb->last_error;
	}

	tinfo.prev = find_and_lock(tdb, *key, F_RDLCK, &h, &rec, &tinfo);
	free(key->dptr);
	if (TDB_OFF_IS_ERR(tinfo.prev)) {
		return tdb->last_error = TDB_OFF_TO_ERR(tinfo.prev);
	}
	tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);

	return tdb->last_error = next_in_hash(tdb, &tinfo, key, NULL);
}

static int wipe_one(struct tdb_context *tdb,
		    TDB_DATA key, TDB_DATA data, enum TDB_ERROR *ecode)
{
	*ecode = tdb_delete(tdb, key);
	return (*ecode != TDB_SUCCESS);
}

_PUBLIC_ enum TDB_ERROR tdb_wipe_all(struct tdb_context *tdb)
{
	enum TDB_ERROR ecode;
	int64_t count;

	if (tdb->flags & TDB_VERSION1) {
		if (tdb1_wipe_all(tdb) == -1)
			return tdb->last_error;
		return TDB_SUCCESS;
	}

	ecode = tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT, false);
	if (ecode != TDB_SUCCESS)
		return tdb->last_error = ecode;

	/* FIXME: Be smarter. */
	count = tdb_traverse(tdb, wipe_one, &ecode);
	if (count < 0)
		ecode = TDB_OFF_TO_ERR(count);
	tdb_allrecord_unlock(tdb, F_WRLCK);
	return tdb->last_error = ecode;
}