_hashindex.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <unistd.h>
  10. #if defined(BYTE_ORDER)&&(BYTE_ORDER == BIG_ENDIAN)
  11. #define _le32toh(x) __builtin_bswap32(x)
  12. #define _htole32(x) __builtin_bswap32(x)
  13. #elif defined(BYTE_ORDER)&&(BYTE_ORDER == LITTLE_ENDIAN)
  14. #define _le32toh(x) (x)
  15. #define _htole32(x) (x)
  16. #else
  17. #error Unknown byte order
  18. #endif
  19. typedef struct {
  20. char magic[8];
  21. int32_t num_entries;
  22. int32_t num_buckets;
  23. int8_t key_size;
  24. int8_t value_size;
  25. } __attribute__((__packed__)) HashHeader;
  26. typedef struct {
  27. void *data;
  28. void *buckets;
  29. int num_entries;
  30. int num_buckets;
  31. int key_size;
  32. int value_size;
  33. off_t bucket_size;
  34. int lower_limit;
  35. int upper_limit;
  36. off_t data_len;
  37. } HashIndex;
  38. #define MAGIC "BORG_IDX"
  39. #define EMPTY _htole32(0xffffffff)
  40. #define DELETED _htole32(0xfffffffe)
  41. #define MAX_BUCKET_SIZE 512
  42. #define BUCKET_LOWER_LIMIT .25
  43. #define BUCKET_UPPER_LIMIT .90
  44. #define MIN_BUCKETS 1024
  45. #define MAX(x, y) ((x) > (y) ? (x): (y))
  46. #define BUCKET_ADDR(index, idx) (index->buckets + (idx * index->bucket_size))
  47. #define BUCKET_IS_DELETED(index, idx) (*((uint32_t *)(BUCKET_ADDR(index, idx) + index->key_size)) == DELETED)
  48. #define BUCKET_IS_EMPTY(index, idx) (*((uint32_t *)(BUCKET_ADDR(index, idx) + index->key_size)) == EMPTY)
  49. #define BUCKET_MATCHES_KEY(index, idx, key) (memcmp(key, BUCKET_ADDR(index, idx), index->key_size) == 0)
  50. #define BUCKET_MARK_DELETED(index, idx) (*((uint32_t *)(BUCKET_ADDR(index, idx) + index->key_size)) = DELETED)
  51. #define BUCKET_MARK_EMPTY(index, idx) (*((uint32_t *)(BUCKET_ADDR(index, idx) + index->key_size)) = EMPTY)
  52. #define EPRINTF(msg, ...) fprintf(stderr, "hashindex: " msg "\n", ##__VA_ARGS__)
  53. #define EPRINTF_PATH(path, msg, ...) fprintf(stderr, "hashindex: %s: " msg "\n", path, ##__VA_ARGS__)
  54. static HashIndex *hashindex_read(const char *path);
  55. static int hashindex_write(HashIndex *index, const char *path);
  56. static HashIndex *hashindex_init(int capacity, int key_size, int value_size);
  57. static const void *hashindex_get(HashIndex *index, const void *key);
  58. static int hashindex_set(HashIndex *index, const void *key, const void *value);
  59. static int hashindex_delete(HashIndex *index, const void *key);
  60. static void *hashindex_next_key(HashIndex *index, const void *key);
  61. /* Private API */
  62. static int
  63. hashindex_index(HashIndex *index, const void *key)
  64. {
  65. return _le32toh(*((uint32_t *)key)) % index->num_buckets;
  66. }
  67. static int
  68. hashindex_lookup(HashIndex *index, const void *key)
  69. {
  70. int didx = -1;
  71. int start = hashindex_index(index, key);
  72. int idx = start;
  73. for(;;) {
  74. if(BUCKET_IS_EMPTY(index, idx))
  75. {
  76. return -1;
  77. }
  78. if(BUCKET_IS_DELETED(index, idx)) {
  79. if(didx == -1) {
  80. didx = idx;
  81. }
  82. }
  83. else if(BUCKET_MATCHES_KEY(index, idx, key)) {
  84. if (didx != -1) {
  85. memcpy(BUCKET_ADDR(index, didx), BUCKET_ADDR(index, idx), index->bucket_size);
  86. BUCKET_MARK_DELETED(index, idx);
  87. idx = didx;
  88. }
  89. return idx;
  90. }
  91. idx = (idx + 1) % index->num_buckets;
  92. if(idx == start) {
  93. return -1;
  94. }
  95. }
  96. }
  97. static int
  98. hashindex_resize(HashIndex *index, int capacity)
  99. {
  100. HashIndex *new;
  101. void *key = NULL;
  102. if(!(new = hashindex_init(capacity, index->key_size, index->value_size))) {
  103. return 0;
  104. }
  105. while((key = hashindex_next_key(index, key))) {
  106. hashindex_set(new, key, hashindex_get(index, key));
  107. }
  108. free(index->data);
  109. index->data = new->data;
  110. index->data_len = new->data_len;
  111. index->num_buckets = new->num_buckets;
  112. index->lower_limit = new->lower_limit;
  113. index->upper_limit = new->upper_limit;
  114. index->buckets = new->buckets;
  115. free(new);
  116. return 1;
  117. }
  118. /* Public API */
  119. static HashIndex *
  120. hashindex_read(const char *path)
  121. {
  122. FILE *fd;
  123. off_t length;
  124. off_t bytes_read;
  125. HashHeader header;
  126. HashIndex *index = NULL;
  127. if((fd = fopen(path, "r")) == NULL) {
  128. EPRINTF_PATH(path, "fopen failed");
  129. return NULL;
  130. }
  131. bytes_read = fread(&header, 1, sizeof(HashHeader), fd);
  132. if(bytes_read != sizeof(HashHeader)) {
  133. EPRINTF_PATH(path, "fread header failed (expected %ld, got %ld)", sizeof(HashHeader), bytes_read);
  134. goto fail;
  135. }
  136. if(fseek(fd, 0, SEEK_END) < 0) {
  137. EPRINTF_PATH(path, "fseek failed");
  138. goto fail;
  139. }
  140. if((length = ftell(fd)) < 0) {
  141. EPRINTF_PATH(path, "ftell failed");
  142. goto fail;
  143. }
  144. if(fseek(fd, 0, SEEK_SET) < 0) {
  145. EPRINTF_PATH(path, "fseek failed");
  146. goto fail;
  147. }
  148. if(memcmp(header.magic, MAGIC, 8)) {
  149. EPRINTF_PATH(path, "Unknown file header");
  150. goto fail;
  151. }
  152. if(length != sizeof(HashHeader) + (off_t)_le32toh(header.num_buckets) * (header.key_size + header.value_size)) {
  153. EPRINTF_PATH(path, "Incorrect file length");
  154. goto fail;
  155. }
  156. if(!(index = malloc(sizeof(HashIndex)))) {
  157. EPRINTF_PATH(path, "malloc failed");
  158. goto fail;
  159. }
  160. if(!(index->data = malloc(length))) {
  161. EPRINTF_PATH(path, "malloc failed");
  162. free(index);
  163. index = NULL;
  164. goto fail;
  165. }
  166. bytes_read = fread(index->data, 1, length, fd);
  167. if(bytes_read != length) {
  168. EPRINTF_PATH(path, "fread hashindex failed (expected %ld, got %ld)", length, bytes_read);
  169. free(index->data);
  170. free(index);
  171. index = NULL;
  172. goto fail;
  173. }
  174. index->data_len = length;
  175. index->num_entries = _le32toh(header.num_entries);
  176. index->num_buckets = _le32toh(header.num_buckets);
  177. index->key_size = header.key_size;
  178. index->value_size = header.value_size;
  179. index->bucket_size = index->key_size + index->value_size;
  180. index->buckets = index->data + sizeof(HashHeader);
  181. index->lower_limit = index->num_buckets > MIN_BUCKETS ? ((int)(index->num_buckets * BUCKET_LOWER_LIMIT)) : 0;
  182. index->upper_limit = (int)(index->num_buckets * BUCKET_UPPER_LIMIT);
  183. fail:
  184. if(fclose(fd) < 0) {
  185. EPRINTF_PATH(path, "fclose failed");
  186. }
  187. return index;
  188. }
  189. static HashIndex *
  190. hashindex_init(int capacity, int key_size, int value_size)
  191. {
  192. HashIndex *index;
  193. HashHeader header = {
  194. .magic = MAGIC, .num_entries = 0, .key_size = key_size, .value_size = value_size
  195. };
  196. int i;
  197. capacity = MAX(MIN_BUCKETS, capacity);
  198. if(!(index = malloc(sizeof(HashIndex)))) {
  199. EPRINTF("malloc failed");
  200. return NULL;
  201. }
  202. index->data_len = sizeof(HashHeader) + (off_t)capacity * (key_size + value_size);
  203. if(!(index->data = calloc(index->data_len, 1))) {
  204. EPRINTF("malloc failed");
  205. free(index);
  206. return NULL;
  207. }
  208. index->num_entries = 0;
  209. index->key_size = key_size;
  210. index->value_size = value_size;
  211. index->num_buckets = capacity;
  212. index->bucket_size = index->key_size + index->value_size;
  213. index->lower_limit = index->num_buckets > MIN_BUCKETS ? ((int)(index->num_buckets * BUCKET_LOWER_LIMIT)) : 0;
  214. index->upper_limit = (int)(index->num_buckets * BUCKET_UPPER_LIMIT);
  215. index->buckets = index->data + sizeof(HashHeader);
  216. memcpy(index->data, &header, sizeof(HashHeader));
  217. for(i = 0; i < capacity; i++) {
  218. BUCKET_MARK_EMPTY(index, i);
  219. }
  220. return index;
  221. }
  222. static void
  223. hashindex_free(HashIndex *index)
  224. {
  225. free(index->data);
  226. free(index);
  227. }
  228. static int
  229. hashindex_write(HashIndex *index, const char *path)
  230. {
  231. FILE *fd;
  232. int ret = 1;
  233. if((fd = fopen(path, "w")) == NULL) {
  234. EPRINTF_PATH(path, "open failed");
  235. fprintf(stderr, "Failed to open %s for writing\n", path);
  236. return 0;
  237. }
  238. *((uint32_t *)(index->data + 8)) = _htole32(index->num_entries);
  239. *((uint32_t *)(index->data + 12)) = _htole32(index->num_buckets);
  240. if(fwrite(index->data, 1, index->data_len, fd) != index->data_len) {
  241. EPRINTF_PATH(path, "fwrite failed");
  242. ret = 0;
  243. }
  244. if(fclose(fd) < 0) {
  245. EPRINTF_PATH(path, "fclose failed");
  246. }
  247. return ret;
  248. }
  249. static const void *
  250. hashindex_get(HashIndex *index, const void *key)
  251. {
  252. int idx = hashindex_lookup(index, key);
  253. if(idx < 0) {
  254. return NULL;
  255. }
  256. return BUCKET_ADDR(index, idx) + index->key_size;
  257. }
  258. static int
  259. hashindex_set(HashIndex *index, const void *key, const void *value)
  260. {
  261. int idx = hashindex_lookup(index, key);
  262. uint8_t *ptr;
  263. if(idx < 0)
  264. {
  265. if(index->num_entries > index->upper_limit) {
  266. if(!hashindex_resize(index, index->num_buckets * 2)) {
  267. return 0;
  268. }
  269. }
  270. idx = hashindex_index(index, key);
  271. while(!BUCKET_IS_EMPTY(index, idx) && !BUCKET_IS_DELETED(index, idx)) {
  272. idx = (idx + 1) % index->num_buckets;
  273. }
  274. ptr = BUCKET_ADDR(index, idx);
  275. memcpy(ptr, key, index->key_size);
  276. memcpy(ptr + index->key_size, value, index->value_size);
  277. index->num_entries += 1;
  278. }
  279. else
  280. {
  281. memcpy(BUCKET_ADDR(index, idx) + index->key_size, value, index->value_size);
  282. }
  283. return 1;
  284. }
  285. static int
  286. hashindex_delete(HashIndex *index, const void *key)
  287. {
  288. int idx = hashindex_lookup(index, key);
  289. if (idx < 0) {
  290. return 1;
  291. }
  292. BUCKET_MARK_DELETED(index, idx);
  293. index->num_entries -= 1;
  294. if(index->num_entries < index->lower_limit) {
  295. if(!hashindex_resize(index, index->num_buckets / 2)) {
  296. return 0;
  297. }
  298. }
  299. return 1;
  300. }
  301. static void *
  302. hashindex_next_key(HashIndex *index, const void *key)
  303. {
  304. int idx = 0;
  305. if(key) {
  306. idx = 1 + (key - index->buckets) / index->bucket_size;
  307. }
  308. if (idx == index->num_buckets) {
  309. return NULL;
  310. }
  311. while(BUCKET_IS_EMPTY(index, idx) || BUCKET_IS_DELETED(index, idx)) {
  312. idx ++;
  313. if (idx == index->num_buckets) {
  314. return NULL;
  315. }
  316. }
  317. return BUCKET_ADDR(index, idx);
  318. }
  319. static int
  320. hashindex_get_size(HashIndex *index)
  321. {
  322. return index->num_entries;
  323. }
  324. static void
  325. hashindex_summarize(HashIndex *index, long long *total_size, long long *total_csize, long long *total_unique_size, long long *total_unique_csize)
  326. {
  327. int64_t size = 0, csize = 0, unique_size = 0, unique_csize = 0;
  328. const int32_t *values;
  329. void *key = NULL;
  330. while((key = hashindex_next_key(index, key))) {
  331. values = key + 32;
  332. unique_size += values[1];
  333. unique_csize += values[2];
  334. size += values[0] * values[1];
  335. csize += values[0] * values[2];
  336. }
  337. *total_size = size;
  338. *total_csize = csize;
  339. *total_unique_size = unique_size;
  340. *total_unique_csize = unique_csize;
  341. }