]> git.ozlabs.org Git - ccan/blob - junkcode/rusty@rustcorp.com.au-ntdb/traverse.c
tal: rename tal_len to tal_bytelen.
[ccan] / junkcode / rusty@rustcorp.com.au-ntdb / 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 _PUBLIC_ int64_t ntdb_traverse_(struct ntdb_context *ntdb,
22                       int (*fn)(struct ntdb_context *,
23                                 NTDB_DATA, NTDB_DATA, void *),
24                       void *p)
25 {
26         enum NTDB_ERROR ecode;
27         struct hash_info h;
28         NTDB_DATA k, d;
29         int64_t count = 0;
30
31         k.dptr = NULL;
32         for (ecode = first_in_hash(ntdb, &h, &k, &d.dsize);
33              ecode == NTDB_SUCCESS;
34              ecode = next_in_hash(ntdb, &h, &k, &d.dsize)) {
35                 d.dptr = k.dptr + k.dsize;
36
37                 count++;
38                 if (fn && fn(ntdb, k, d, p)) {
39                         ntdb->free_fn(k.dptr, ntdb->alloc_data);
40                         return count;
41                 }
42                 ntdb->free_fn(k.dptr, ntdb->alloc_data);
43         }
44
45         if (ecode != NTDB_ERR_NOEXIST) {
46                 return NTDB_ERR_TO_OFF(ecode);
47         }
48         return count;
49 }
50
51 _PUBLIC_ enum NTDB_ERROR ntdb_firstkey(struct ntdb_context *ntdb, NTDB_DATA *key)
52 {
53         struct hash_info h;
54
55         return first_in_hash(ntdb, &h, key, NULL);
56 }
57
58 /* We lock twice, not very efficient.  We could keep last key & h cached. */
59 _PUBLIC_ enum NTDB_ERROR ntdb_nextkey(struct ntdb_context *ntdb, NTDB_DATA *key)
60 {
61         struct hash_info h;
62         struct ntdb_used_record rec;
63         ntdb_off_t off;
64
65         off = find_and_lock(ntdb, *key, F_RDLCK, &h, &rec, NULL);
66         ntdb->free_fn(key->dptr, ntdb->alloc_data);
67         if (NTDB_OFF_IS_ERR(off)) {
68                 return NTDB_OFF_TO_ERR(off);
69         }
70         ntdb_unlock_hash(ntdb, h.h, F_RDLCK);
71
72         /* If we found something, skip to next. */
73         if (off)
74                 h.bucket++;
75         return next_in_hash(ntdb, &h, key, NULL);
76 }
77
78 static int wipe_one(struct ntdb_context *ntdb,
79                     NTDB_DATA key, NTDB_DATA data, enum NTDB_ERROR *ecode)
80 {
81         *ecode = ntdb_delete(ntdb, key);
82         return (*ecode != NTDB_SUCCESS);
83 }
84
85 _PUBLIC_ enum NTDB_ERROR ntdb_wipe_all(struct ntdb_context *ntdb)
86 {
87         enum NTDB_ERROR ecode;
88         int64_t count;
89
90         ecode = ntdb_allrecord_lock(ntdb, F_WRLCK, NTDB_LOCK_WAIT, false);
91         if (ecode != NTDB_SUCCESS)
92                 return ecode;
93
94         /* FIXME: Be smarter. */
95         count = ntdb_traverse(ntdb, wipe_one, &ecode);
96         if (count < 0)
97                 ecode = NTDB_OFF_TO_ERR(count);
98         ntdb_allrecord_unlock(ntdb, F_WRLCK);
99         return ecode;
100 }