run-traverse.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include "ntdb-source.h"
  2. #include "tap-interface.h"
  3. #include "logging.h"
  4. #include "helprun-external-agent.h"
  5. #define NUM_RECORDS 1000
  6. /* We use the same seed which we saw a failure on. */
  7. static uint32_t fixedhash(const void *key, size_t len, uint32_t seed, void *p)
  8. {
  9. return hash64_stable((const unsigned char *)key, len,
  10. *(uint64_t *)p);
  11. }
  12. static bool store_records(struct ntdb_context *ntdb)
  13. {
  14. int i;
  15. NTDB_DATA key = { (unsigned char *)&i, sizeof(i) };
  16. NTDB_DATA data = { (unsigned char *)&i, sizeof(i) };
  17. for (i = 0; i < NUM_RECORDS; i++)
  18. if (ntdb_store(ntdb, key, data, NTDB_REPLACE) != 0)
  19. return false;
  20. return true;
  21. }
  22. struct trav_data {
  23. unsigned int calls, call_limit;
  24. int low, high;
  25. bool mismatch;
  26. bool delete;
  27. enum NTDB_ERROR delete_error;
  28. };
  29. static int trav(struct ntdb_context *ntdb, NTDB_DATA key, NTDB_DATA dbuf,
  30. struct trav_data *td)
  31. {
  32. int val;
  33. td->calls++;
  34. if (key.dsize != sizeof(val) || dbuf.dsize != sizeof(val)
  35. || memcmp(key.dptr, dbuf.dptr, key.dsize) != 0) {
  36. td->mismatch = true;
  37. return -1;
  38. }
  39. memcpy(&val, dbuf.dptr, dbuf.dsize);
  40. if (val < td->low)
  41. td->low = val;
  42. if (val > td->high)
  43. td->high = val;
  44. if (td->delete) {
  45. td->delete_error = ntdb_delete(ntdb, key);
  46. if (td->delete_error != NTDB_SUCCESS) {
  47. return -1;
  48. }
  49. }
  50. if (td->calls == td->call_limit)
  51. return 1;
  52. return 0;
  53. }
  54. struct trav_grow_data {
  55. unsigned int calls;
  56. unsigned int num_large;
  57. bool mismatch;
  58. enum NTDB_ERROR error;
  59. };
  60. static int trav_grow(struct ntdb_context *ntdb, NTDB_DATA key, NTDB_DATA dbuf,
  61. struct trav_grow_data *tgd)
  62. {
  63. int val;
  64. unsigned char buffer[128] = { 0 };
  65. tgd->calls++;
  66. if (key.dsize != sizeof(val) || dbuf.dsize < sizeof(val)
  67. || memcmp(key.dptr, dbuf.dptr, key.dsize) != 0) {
  68. tgd->mismatch = true;
  69. return -1;
  70. }
  71. if (dbuf.dsize > sizeof(val))
  72. /* We must have seen this before! */
  73. tgd->num_large++;
  74. /* Make a big difference to the database. */
  75. dbuf.dptr = buffer;
  76. dbuf.dsize = sizeof(buffer);
  77. tgd->error = ntdb_append(ntdb, key, dbuf);
  78. if (tgd->error != NTDB_SUCCESS) {
  79. return -1;
  80. }
  81. return 0;
  82. }
  83. int main(int argc, char *argv[])
  84. {
  85. unsigned int i;
  86. int num;
  87. struct trav_data td;
  88. struct trav_grow_data tgd;
  89. struct ntdb_context *ntdb;
  90. uint64_t seed = 16014841315512641303ULL;
  91. int flags[] = { NTDB_INTERNAL, NTDB_DEFAULT, NTDB_NOMMAP,
  92. NTDB_INTERNAL|NTDB_CONVERT, NTDB_CONVERT,
  93. NTDB_NOMMAP|NTDB_CONVERT };
  94. union ntdb_attribute hattr = { .hash = { .base = { NTDB_ATTRIBUTE_HASH },
  95. .fn = fixedhash,
  96. .data = &seed } };
  97. hattr.base.next = &tap_log_attr;
  98. plan_tests(sizeof(flags) / sizeof(flags[0]) * 32 + 1);
  99. for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
  100. ntdb = ntdb_open("run-traverse.ntdb", flags[i]|MAYBE_NOSYNC,
  101. O_RDWR|O_CREAT|O_TRUNC, 0600, &hattr);
  102. ok1(ntdb);
  103. if (!ntdb)
  104. continue;
  105. ok1(ntdb_traverse(ntdb, NULL, NULL) == 0);
  106. ok1(store_records(ntdb));
  107. num = ntdb_traverse(ntdb, NULL, NULL);
  108. ok1(num == NUM_RECORDS);
  109. /* Full traverse. */
  110. td.calls = 0;
  111. td.call_limit = UINT_MAX;
  112. td.low = INT_MAX;
  113. td.high = INT_MIN;
  114. td.mismatch = false;
  115. td.delete = false;
  116. num = ntdb_traverse(ntdb, trav, &td);
  117. ok1(num == NUM_RECORDS);
  118. ok1(!td.mismatch);
  119. ok1(td.calls == NUM_RECORDS);
  120. ok1(td.low == 0);
  121. ok1(td.high == NUM_RECORDS-1);
  122. /* Short traverse. */
  123. td.calls = 0;
  124. td.call_limit = NUM_RECORDS / 2;
  125. td.low = INT_MAX;
  126. td.high = INT_MIN;
  127. td.mismatch = false;
  128. td.delete = false;
  129. num = ntdb_traverse(ntdb, trav, &td);
  130. ok1(num == NUM_RECORDS / 2);
  131. ok1(!td.mismatch);
  132. ok1(td.calls == NUM_RECORDS / 2);
  133. ok1(td.low <= NUM_RECORDS / 2);
  134. ok1(td.high > NUM_RECORDS / 2);
  135. ok1(ntdb_check(ntdb, NULL, NULL) == 0);
  136. ok1(tap_log_messages == 0);
  137. /* Deleting traverse (delete everything). */
  138. td.calls = 0;
  139. td.call_limit = UINT_MAX;
  140. td.low = INT_MAX;
  141. td.high = INT_MIN;
  142. td.mismatch = false;
  143. td.delete = true;
  144. td.delete_error = NTDB_SUCCESS;
  145. num = ntdb_traverse(ntdb, trav, &td);
  146. ok1(num == NUM_RECORDS);
  147. ok1(td.delete_error == NTDB_SUCCESS);
  148. ok1(!td.mismatch);
  149. ok1(td.calls == NUM_RECORDS);
  150. ok1(td.low == 0);
  151. ok1(td.high == NUM_RECORDS - 1);
  152. ok1(ntdb_check(ntdb, NULL, NULL) == 0);
  153. /* Now it's empty! */
  154. ok1(ntdb_traverse(ntdb, NULL, NULL) == 0);
  155. /* Re-add. */
  156. ok1(store_records(ntdb));
  157. ok1(ntdb_traverse(ntdb, NULL, NULL) == NUM_RECORDS);
  158. ok1(ntdb_check(ntdb, NULL, NULL) == 0);
  159. /* Grow. This will cause us to be reshuffled. */
  160. tgd.calls = 0;
  161. tgd.num_large = 0;
  162. tgd.mismatch = false;
  163. tgd.error = NTDB_SUCCESS;
  164. ok1(ntdb_traverse(ntdb, trav_grow, &tgd) > 1);
  165. ok1(tgd.error == 0);
  166. ok1(!tgd.mismatch);
  167. ok1(ntdb_check(ntdb, NULL, NULL) == 0);
  168. ok1(tgd.num_large < tgd.calls);
  169. diag("growing db: %u calls, %u repeats",
  170. tgd.calls, tgd.num_large);
  171. ntdb_close(ntdb);
  172. }
  173. ok1(tap_log_messages == 0);
  174. return exit_status();
  175. }