traverse.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. Trivial Database 2: traverse function.
  3. Copyright (C) Rusty Russell 2010
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 3 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "private.h"
  16. #include <ccan/likely/likely.h>
  17. _PUBLIC_ int64_t ntdb_traverse_(struct ntdb_context *ntdb,
  18. int (*fn)(struct ntdb_context *,
  19. NTDB_DATA, NTDB_DATA, void *),
  20. void *p)
  21. {
  22. enum NTDB_ERROR ecode;
  23. struct hash_info h;
  24. NTDB_DATA k, d;
  25. int64_t count = 0;
  26. k.dptr = NULL;
  27. for (ecode = first_in_hash(ntdb, &h, &k, &d.dsize);
  28. ecode == NTDB_SUCCESS;
  29. ecode = next_in_hash(ntdb, &h, &k, &d.dsize)) {
  30. d.dptr = k.dptr + k.dsize;
  31. count++;
  32. if (fn && fn(ntdb, k, d, p)) {
  33. ntdb->free_fn(k.dptr, ntdb->alloc_data);
  34. return count;
  35. }
  36. ntdb->free_fn(k.dptr, ntdb->alloc_data);
  37. }
  38. if (ecode != NTDB_ERR_NOEXIST) {
  39. return NTDB_ERR_TO_OFF(ecode);
  40. }
  41. return count;
  42. }
  43. _PUBLIC_ enum NTDB_ERROR ntdb_firstkey(struct ntdb_context *ntdb, NTDB_DATA *key)
  44. {
  45. struct hash_info h;
  46. return first_in_hash(ntdb, &h, key, NULL);
  47. }
  48. /* We lock twice, not very efficient. We could keep last key & h cached. */
  49. _PUBLIC_ enum NTDB_ERROR ntdb_nextkey(struct ntdb_context *ntdb, NTDB_DATA *key)
  50. {
  51. struct hash_info h;
  52. struct ntdb_used_record rec;
  53. ntdb_off_t off;
  54. off = find_and_lock(ntdb, *key, F_RDLCK, &h, &rec, NULL);
  55. ntdb->free_fn(key->dptr, ntdb->alloc_data);
  56. if (NTDB_OFF_IS_ERR(off)) {
  57. return NTDB_OFF_TO_ERR(off);
  58. }
  59. ntdb_unlock_hash(ntdb, h.h, F_RDLCK);
  60. /* If we found something, skip to next. */
  61. if (off)
  62. h.bucket++;
  63. return next_in_hash(ntdb, &h, key, NULL);
  64. }
  65. static int wipe_one(struct ntdb_context *ntdb,
  66. NTDB_DATA key, NTDB_DATA data, enum NTDB_ERROR *ecode)
  67. {
  68. *ecode = ntdb_delete(ntdb, key);
  69. return (*ecode != NTDB_SUCCESS);
  70. }
  71. _PUBLIC_ enum NTDB_ERROR ntdb_wipe_all(struct ntdb_context *ntdb)
  72. {
  73. enum NTDB_ERROR ecode;
  74. int64_t count;
  75. ecode = ntdb_allrecord_lock(ntdb, F_WRLCK, NTDB_LOCK_WAIT, false);
  76. if (ecode != NTDB_SUCCESS)
  77. return ecode;
  78. /* FIXME: Be smarter. */
  79. count = ntdb_traverse(ntdb, wipe_one, &ecode);
  80. if (count < 0)
  81. ecode = NTDB_OFF_TO_ERR(count);
  82. ntdb_allrecord_unlock(ntdb, F_WRLCK);
  83. return ecode;
  84. }