]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/traverse.c
tdb2: rework hash.c functions to return enum TDB_ERROR.
[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, tdb_traverse_func fn, void *p)
22 {
23         enum TDB_ERROR ecode;
24         struct traverse_info tinfo;
25         struct tdb_data k, d;
26         int64_t count = 0;
27
28         k.dptr = NULL;
29         for (ecode = first_in_hash(tdb, &tinfo, &k, &d.dsize);
30              ecode == TDB_SUCCESS;
31              ecode = next_in_hash(tdb, &tinfo, &k, &d.dsize)) {
32                 d.dptr = k.dptr + k.dsize;
33                 
34                 count++;
35                 if (fn && fn(tdb, k, d, p)) {
36                         free(k.dptr);
37                         break;
38                 }
39                 free(k.dptr);
40         }
41
42         if (ecode != TDB_ERR_NOEXIST) {
43                 tdb->ecode = ecode;
44                 return -1;
45         }
46         return count;
47 }
48         
49 TDB_DATA tdb_firstkey(struct tdb_context *tdb)
50 {
51         struct traverse_info tinfo;
52         struct tdb_data k;
53         enum TDB_ERROR ecode;
54
55         ecode = first_in_hash(tdb, &tinfo, &k, NULL);
56         if (ecode == TDB_SUCCESS) {
57                 return k;
58         }
59         if (ecode == TDB_ERR_NOEXIST)
60                 ecode = TDB_SUCCESS;
61         tdb->ecode = ecode;
62         return tdb_null;
63 }
64
65 /* We lock twice, not very efficient.  We could keep last key & tinfo cached. */
66 TDB_DATA tdb_nextkey(struct tdb_context *tdb, TDB_DATA key)
67 {
68         struct traverse_info tinfo;
69         struct hash_info h;
70         struct tdb_used_record rec;
71         enum TDB_ERROR ecode;
72
73         tinfo.prev = find_and_lock(tdb, key, F_RDLCK, &h, &rec, &tinfo);
74         if (TDB_OFF_IS_ERR(tinfo.prev)) {
75                 tdb->ecode = tinfo.prev;
76                 return tdb_null;
77         }
78         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
79
80         ecode = next_in_hash(tdb, &tinfo, &key, NULL);
81         if (ecode == TDB_SUCCESS) {
82                 return key;
83         }
84         if (ecode == TDB_ERR_NOEXIST)
85                 ecode = TDB_SUCCESS;
86         tdb->ecode = ecode;
87         return tdb_null;
88 }