traverse.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. int64_t tdb_traverse_(struct tdb_context *tdb,
  18. int (*fn)(struct tdb_context *,
  19. TDB_DATA, TDB_DATA, void *),
  20. void *p)
  21. {
  22. enum TDB_ERROR ecode;
  23. struct traverse_info tinfo;
  24. struct tdb_data k, d;
  25. int64_t count = 0;
  26. k.dptr = NULL;
  27. for (ecode = first_in_hash(tdb, &tinfo, &k, &d.dsize);
  28. ecode == TDB_SUCCESS;
  29. ecode = next_in_hash(tdb, &tinfo, &k, &d.dsize)) {
  30. d.dptr = k.dptr + k.dsize;
  31. count++;
  32. if (fn && fn(tdb, k, d, p)) {
  33. free(k.dptr);
  34. tdb->last_error = TDB_SUCCESS;
  35. return count;
  36. }
  37. free(k.dptr);
  38. }
  39. if (ecode != TDB_ERR_NOEXIST) {
  40. return tdb->last_error = ecode;
  41. }
  42. tdb->last_error = TDB_SUCCESS;
  43. return count;
  44. }
  45. enum TDB_ERROR tdb_firstkey(struct tdb_context *tdb, struct tdb_data *key)
  46. {
  47. struct traverse_info tinfo;
  48. return tdb->last_error = first_in_hash(tdb, &tinfo, key, NULL);
  49. }
  50. /* We lock twice, not very efficient. We could keep last key & tinfo cached. */
  51. enum TDB_ERROR tdb_nextkey(struct tdb_context *tdb, struct tdb_data *key)
  52. {
  53. struct traverse_info tinfo;
  54. struct hash_info h;
  55. struct tdb_used_record rec;
  56. tinfo.prev = find_and_lock(tdb, *key, F_RDLCK, &h, &rec, &tinfo);
  57. free(key->dptr);
  58. if (TDB_OFF_IS_ERR(tinfo.prev)) {
  59. return tdb->last_error = tinfo.prev;
  60. }
  61. tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
  62. return tdb->last_error = next_in_hash(tdb, &tinfo, key, NULL);
  63. }
  64. static int wipe_one(struct tdb_context *tdb,
  65. TDB_DATA key, TDB_DATA data, enum TDB_ERROR *ecode)
  66. {
  67. *ecode = tdb_delete(tdb, key);
  68. return (*ecode != TDB_SUCCESS);
  69. }
  70. enum TDB_ERROR tdb_wipe_all(struct tdb_context *tdb)
  71. {
  72. enum TDB_ERROR ecode;
  73. int64_t count;
  74. ecode = tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT, false);
  75. if (ecode != TDB_SUCCESS)
  76. return tdb->last_error = ecode;
  77. /* FIXME: Be smarter. */
  78. count = tdb_traverse(tdb, wipe_one, &ecode);
  79. if (count < 0)
  80. ecode = count;
  81. tdb_allrecord_unlock(tdb, F_WRLCK);
  82. return tdb->last_error = ecode;
  83. }