]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/traverse.c
tdb2: unify tdb1_traverse into tdb_traverse
[ccan] / ccan / tdb2 / traverse.c
1  /*
2    Trivial Database 2: traverse function.
3    Copyright (C) Rusty Russell 2010
4
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 3 of the License, or (at your option) any later version.
9
10    This library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "private.h"
19 #include <ccan/likely/likely.h>
20
21 int64_t tdb_traverse_(struct tdb_context *tdb,
22                       int (*fn)(struct tdb_context *,
23                                 TDB_DATA, TDB_DATA, void *),
24                       void *p)
25 {
26         enum TDB_ERROR ecode;
27         struct traverse_info tinfo;
28         struct tdb_data k, d;
29         int64_t count = 0;
30
31         if (tdb->flags & TDB_VERSION1) {
32                 count = tdb1_traverse(tdb, fn, p);
33                 if (count == -1)
34                         return tdb->last_error;
35                 return count;
36         }
37
38         k.dptr = NULL;
39         for (ecode = first_in_hash(tdb, &tinfo, &k, &d.dsize);
40              ecode == TDB_SUCCESS;
41              ecode = next_in_hash(tdb, &tinfo, &k, &d.dsize)) {
42                 d.dptr = k.dptr + k.dsize;
43                 
44                 count++;
45                 if (fn && fn(tdb, k, d, p)) {
46                         free(k.dptr);
47                         tdb->last_error = TDB_SUCCESS;
48                         return count;
49                 }
50                 free(k.dptr);
51         }
52
53         if (ecode != TDB_ERR_NOEXIST) {
54                 return tdb->last_error = ecode;
55         }
56         tdb->last_error = TDB_SUCCESS;
57         return count;
58 }
59         
60 enum TDB_ERROR tdb_firstkey(struct tdb_context *tdb, struct tdb_data *key)
61 {
62         struct traverse_info tinfo;
63
64         return tdb->last_error = first_in_hash(tdb, &tinfo, key, NULL);
65 }
66
67 /* We lock twice, not very efficient.  We could keep last key & tinfo cached. */
68 enum TDB_ERROR tdb_nextkey(struct tdb_context *tdb, struct tdb_data *key)
69 {
70         struct traverse_info tinfo;
71         struct hash_info h;
72         struct tdb_used_record rec;
73
74         tinfo.prev = find_and_lock(tdb, *key, F_RDLCK, &h, &rec, &tinfo);
75         free(key->dptr);
76         if (TDB_OFF_IS_ERR(tinfo.prev)) {
77                 return tdb->last_error = tinfo.prev;
78         }
79         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
80
81         return tdb->last_error = next_in_hash(tdb, &tinfo, key, NULL);
82 }
83
84 static int wipe_one(struct tdb_context *tdb,
85                     TDB_DATA key, TDB_DATA data, enum TDB_ERROR *ecode)
86 {
87         *ecode = tdb_delete(tdb, key);
88         return (*ecode != TDB_SUCCESS);
89 }
90
91 enum TDB_ERROR tdb_wipe_all(struct tdb_context *tdb)
92 {
93         enum TDB_ERROR ecode;
94         int64_t count;
95
96         ecode = tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT, false);
97         if (ecode != TDB_SUCCESS)
98                 return tdb->last_error = ecode;
99
100         /* FIXME: Be smarter. */
101         count = tdb_traverse(tdb, wipe_one, &ecode);
102         if (count < 0)
103                 ecode = count;
104         tdb_allrecord_unlock(tdb, F_WRLCK);
105         return tdb->last_error = ecode;
106 }