]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/traverse.c
tdb2: log a message on allocation failure in tdb_check()
[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 static int64_t traverse(struct tdb_context *tdb, int ltype,
22                         tdb_traverse_func fn, void *p)
23 {
24         int ret;
25         struct traverse_info tinfo;
26         struct tdb_data k, d;
27         int64_t count = 0;
28
29         k.dptr = NULL;
30         for (ret = first_in_hash(tdb, ltype, &tinfo, &k, &d.dsize);
31              ret == 1;
32              ret = next_in_hash(tdb, ltype, &tinfo, &k, &d.dsize)) {
33                 d.dptr = k.dptr + k.dsize;
34                 
35                 count++;
36                 if (fn && fn(tdb, k, d, p)) {
37                         free(k.dptr);
38                         break;
39                 }
40                 free(k.dptr);
41         }
42
43         if (ret < 0)
44                 return -1;
45         return count;
46 }
47
48 int64_t tdb_traverse(struct tdb_context *tdb, tdb_traverse_func fn, void *p)
49 {
50         return traverse(tdb, F_WRLCK, fn, p);
51 }
52         
53 int64_t tdb_traverse_read(struct tdb_context *tdb,
54                           tdb_traverse_func fn, void *p)
55 {
56         int64_t ret;
57         bool was_ro = tdb->read_only;
58         tdb->read_only = true;
59         ret = traverse(tdb, F_RDLCK, fn, p);
60         tdb->read_only = was_ro;
61         return ret;
62 }
63
64 TDB_DATA tdb_firstkey(struct tdb_context *tdb)
65 {
66         struct traverse_info tinfo;
67         struct tdb_data k;
68         switch (first_in_hash(tdb, F_RDLCK, &tinfo, &k, NULL)) {
69         case 1:
70                 return k;
71         case 0:
72                 tdb->ecode = TDB_SUCCESS;
73                 /* Fall thru... */
74         default:
75                 return tdb_null;
76         }
77 }               
78
79 /* We lock twice, not very efficient.  We could keep last key & tinfo cached. */
80 TDB_DATA tdb_nextkey(struct tdb_context *tdb, TDB_DATA key)
81 {
82         struct traverse_info tinfo;
83         struct hash_info h;
84         struct tdb_used_record rec;
85
86         tinfo.prev = find_and_lock(tdb, key, F_RDLCK, &h, &rec, &tinfo);
87         if (unlikely(tinfo.prev == TDB_OFF_ERR))
88                 return tdb_null;
89         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
90
91         switch (next_in_hash(tdb, F_RDLCK, &tinfo, &key, NULL)) {
92         case 1:
93                 return key;
94         case 0:
95                 tdb->ecode = TDB_SUCCESS;
96                 /* Fall thru... */
97         default:
98                 return tdb_null;
99         }
100 }