run-30-exhaust-before-expand.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include "ntdb-source.h"
  2. #include "tap-interface.h"
  3. #include "logging.h"
  4. #include "helprun-external-agent.h"
  5. static bool empty_freetable(struct ntdb_context *ntdb)
  6. {
  7. struct ntdb_freetable ftab;
  8. unsigned int i;
  9. /* Now, free table should be completely exhausted in zone 0 */
  10. if (ntdb_read_convert(ntdb, ntdb->ftable_off, &ftab, sizeof(ftab)) != 0)
  11. abort();
  12. for (i = 0; i < sizeof(ftab.buckets)/sizeof(ftab.buckets[0]); i++) {
  13. if (ftab.buckets[i])
  14. return false;
  15. }
  16. return true;
  17. }
  18. int main(int argc, char *argv[])
  19. {
  20. unsigned int i, j;
  21. struct ntdb_context *ntdb;
  22. int flags[] = { NTDB_INTERNAL, NTDB_DEFAULT, NTDB_NOMMAP,
  23. NTDB_INTERNAL|NTDB_CONVERT, NTDB_CONVERT,
  24. NTDB_NOMMAP|NTDB_CONVERT };
  25. plan_tests(sizeof(flags) / sizeof(flags[0]) * 7 + 1);
  26. for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
  27. NTDB_DATA k, d;
  28. uint64_t size;
  29. bool was_empty = false;
  30. k.dptr = (void *)&j;
  31. k.dsize = sizeof(j);
  32. ntdb = ntdb_open("run-30-exhaust-before-expand.ntdb",
  33. flags[i]|MAYBE_NOSYNC,
  34. O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
  35. ok1(ntdb);
  36. if (!ntdb)
  37. continue;
  38. ok1(ntdb_check(ntdb, NULL, NULL) == 0);
  39. /* There's one empty record in initial db. */
  40. ok1(!empty_freetable(ntdb));
  41. size = ntdb->file->map_size;
  42. /* Create one record to chew up most space. */
  43. d.dsize = size - NEW_DATABASE_HDR_SIZE(ntdb->hash_bits) - 32;
  44. d.dptr = calloc(d.dsize, 1);
  45. j = 0;
  46. ok1(ntdb_store(ntdb, k, d, NTDB_INSERT) == 0);
  47. ok1(ntdb->file->map_size == size);
  48. free(d.dptr);
  49. /* Now insert minimal-length records until we expand. */
  50. for (j = 1; ntdb->file->map_size == size; j++) {
  51. was_empty = empty_freetable(ntdb);
  52. if (ntdb_store(ntdb, k, k, NTDB_INSERT) != 0)
  53. err(1, "Failed to store record %i", j);
  54. }
  55. /* Would have been empty before expansion, but no longer. */
  56. ok1(was_empty);
  57. ok1(!empty_freetable(ntdb));
  58. ntdb_close(ntdb);
  59. }
  60. ok1(tap_log_messages == 0);
  61. return exit_status();
  62. }