run-15-append.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "ntdb-source.h"
  2. #include "tap-interface.h"
  3. #include <ccan/ilog/ilog.h>
  4. #include "logging.h"
  5. #include "helprun-external-agent.h"
  6. #define MAX_SIZE 13100
  7. #define SIZE_STEP 131
  8. static ntdb_off_t ntdb_offset(struct ntdb_context *ntdb, NTDB_DATA key)
  9. {
  10. ntdb_off_t off;
  11. struct ntdb_used_record urec;
  12. struct hash_info h;
  13. off = find_and_lock(ntdb, key, F_RDLCK, &h, &urec, NULL);
  14. if (NTDB_OFF_IS_ERR(off))
  15. return 0;
  16. ntdb_unlock_hash(ntdb, h.h, F_RDLCK);
  17. return off;
  18. }
  19. int main(int argc, char *argv[])
  20. {
  21. unsigned int i, j, moves;
  22. struct ntdb_context *ntdb;
  23. unsigned char *buffer;
  24. ntdb_off_t oldoff = 0, newoff;
  25. int flags[] = { NTDB_INTERNAL, NTDB_DEFAULT, NTDB_NOMMAP,
  26. NTDB_INTERNAL|NTDB_CONVERT, NTDB_CONVERT,
  27. NTDB_NOMMAP|NTDB_CONVERT };
  28. NTDB_DATA key = ntdb_mkdata("key", 3);
  29. NTDB_DATA data;
  30. buffer = malloc(MAX_SIZE);
  31. for (i = 0; i < MAX_SIZE; i++)
  32. buffer[i] = i;
  33. plan_tests(sizeof(flags) / sizeof(flags[0])
  34. * ((3 + MAX_SIZE/SIZE_STEP * 5) * 2 + 7)
  35. + 1);
  36. /* Using ntdb_store. */
  37. for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
  38. ntdb = ntdb_open("run-append.ntdb", flags[i]|MAYBE_NOSYNC,
  39. O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
  40. ok1(ntdb);
  41. if (!ntdb)
  42. continue;
  43. moves = 0;
  44. for (j = 0; j < MAX_SIZE; j += SIZE_STEP) {
  45. data.dptr = buffer;
  46. data.dsize = j;
  47. ok1(ntdb_store(ntdb, key, data, NTDB_REPLACE) == 0);
  48. ok1(ntdb_check(ntdb, NULL, NULL) == 0);
  49. ok1(ntdb_fetch(ntdb, key, &data) == NTDB_SUCCESS);
  50. ok1(data.dsize == j);
  51. ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
  52. free(data.dptr);
  53. newoff = ntdb_offset(ntdb, key);
  54. if (newoff != oldoff)
  55. moves++;
  56. oldoff = newoff;
  57. }
  58. ok1(!ntdb->file || (ntdb->file->allrecord_lock.count == 0
  59. && ntdb->file->num_lockrecs == 0));
  60. /* We should increase by 50% each time... */
  61. ok(moves <= ilog64(j / SIZE_STEP)*2,
  62. "Moved %u times", moves);
  63. ntdb_close(ntdb);
  64. }
  65. /* Using ntdb_append. */
  66. for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
  67. size_t prev_len = 0;
  68. ntdb = ntdb_open("run-append.ntdb", flags[i]|MAYBE_NOSYNC,
  69. O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
  70. ok1(ntdb);
  71. if (!ntdb)
  72. continue;
  73. moves = 0;
  74. for (j = 0; j < MAX_SIZE; j += SIZE_STEP) {
  75. data.dptr = buffer + prev_len;
  76. data.dsize = j - prev_len;
  77. ok1(ntdb_append(ntdb, key, data) == 0);
  78. ok1(ntdb_check(ntdb, NULL, NULL) == 0);
  79. ok1(ntdb_fetch(ntdb, key, &data) == NTDB_SUCCESS);
  80. ok1(data.dsize == j);
  81. ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
  82. free(data.dptr);
  83. prev_len = data.dsize;
  84. newoff = ntdb_offset(ntdb, key);
  85. if (newoff != oldoff)
  86. moves++;
  87. oldoff = newoff;
  88. }
  89. ok1(!ntdb->file || (ntdb->file->allrecord_lock.count == 0
  90. && ntdb->file->num_lockrecs == 0));
  91. /* We should increase by 50% each time... */
  92. ok(moves <= ilog64(j / SIZE_STEP)*2,
  93. "Moved %u times", moves);
  94. ntdb_close(ntdb);
  95. }
  96. for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
  97. ntdb = ntdb_open("run-append.ntdb", flags[i]|MAYBE_NOSYNC,
  98. O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
  99. ok1(ntdb);
  100. if (!ntdb)
  101. continue;
  102. /* Huge initial store. */
  103. data.dptr = buffer;
  104. data.dsize = MAX_SIZE;
  105. ok1(ntdb_append(ntdb, key, data) == 0);
  106. ok1(ntdb_check(ntdb, NULL, NULL) == 0);
  107. ok1(ntdb_fetch(ntdb, key, &data) == NTDB_SUCCESS);
  108. ok1(data.dsize == MAX_SIZE);
  109. ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
  110. free(data.dptr);
  111. ok1(!ntdb->file || (ntdb->file->allrecord_lock.count == 0
  112. && ntdb->file->num_lockrecs == 0));
  113. ntdb_close(ntdb);
  114. }
  115. ok1(tap_log_messages == 0);
  116. free(buffer);
  117. return exit_status();
  118. }