_hashindex.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 (__SVR4) && defined (__sun)
  11. #include <sys/isa_defs.h>
  12. #endif
  13. #if (defined(BYTE_ORDER)&&(BYTE_ORDER == BIG_ENDIAN)) || \
  14. (defined(_BIG_ENDIAN)&&defined(__SVR4)&&defined(__sun))
  15. #define _le32toh(x) __builtin_bswap32(x)
  16. #define _htole32(x) __builtin_bswap32(x)
  17. #elif (defined(BYTE_ORDER)&&(BYTE_ORDER == LITTLE_ENDIAN)) || \
  18. (defined(_LITTLE_ENDIAN)&&defined(__SVR4)&&defined(__sun))
  19. #define _le32toh(x) (x)
  20. #define _htole32(x) (x)
  21. #else
  22. #error Unknown byte order
  23. #endif
  24. #define MAGIC "BORG_IDX"
  25. #define MAGIC_LEN 8
  26. typedef struct {
  27. char magic[MAGIC_LEN];
  28. int32_t num_entries;
  29. int32_t num_buckets;
  30. int8_t key_size;
  31. int8_t value_size;
  32. } __attribute__((__packed__)) HashHeader;
  33. typedef struct {
  34. void *buckets;
  35. int num_entries;
  36. int num_buckets;
  37. int key_size;
  38. int value_size;
  39. off_t bucket_size;
  40. int lower_limit;
  41. int upper_limit;
  42. } HashIndex;
  43. /* prime (or w/ big prime factors) hash table sizes
  44. * not sure we need primes for borg's usage (as we have a hash function based
  45. * on sha256, we can assume an even, seemingly random distribution of values),
  46. * but OTOH primes don't harm.
  47. * also, growth of the sizes starts with fast-growing 2x steps, but slows down
  48. * more and more down to 1.1x. this is to avoid huge jumps in memory allocation,
  49. * like e.g. 4G -> 8G.
  50. * these values are generated by hash_sizes.py.
  51. */
  52. static int hash_sizes[] = {
  53. 1031, 2053, 4099, 8209, 16411, 32771, 65537, 131101, 262147, 445649,
  54. 757607, 1287917, 2189459, 3065243, 4291319, 6007867, 8410991,
  55. 11775359, 16485527, 23079703, 27695653, 33234787, 39881729, 47858071,
  56. 57429683, 68915617, 82698751, 99238507, 119086189, 144378011, 157223263,
  57. 173476439, 190253911, 209915011, 230493629, 253169431, 278728861,
  58. 306647623, 337318939, 370742809, 408229973, 449387209, 493428073,
  59. 543105119, 596976533, 657794869, 722676499, 795815791, 874066969,
  60. 962279771, 1057701643, 1164002657, 1280003147, 1407800297, 1548442699,
  61. 1703765389, 1873768367, 2062383853, /* 32bit int ends about here */
  62. };
  63. #define HASH_MIN_LOAD .25
  64. #define HASH_MAX_LOAD .75 /* don't go higher than 0.75, otherwise performance severely suffers! */
  65. #define MAX(x, y) ((x) > (y) ? (x): (y))
  66. #define NELEMS(x) (sizeof(x) / sizeof((x)[0]))
  67. #define EMPTY _htole32(0xffffffff)
  68. #define DELETED _htole32(0xfffffffe)
  69. #define BUCKET_ADDR(index, idx) (index->buckets + (idx * index->bucket_size))
  70. #define BUCKET_MATCHES_KEY(index, idx, key) (memcmp(key, BUCKET_ADDR(index, idx), index->key_size) == 0)
  71. #define BUCKET_IS_DELETED(index, idx) (*((uint32_t *)(BUCKET_ADDR(index, idx) + index->key_size)) == DELETED)
  72. #define BUCKET_IS_EMPTY(index, idx) (*((uint32_t *)(BUCKET_ADDR(index, idx) + index->key_size)) == EMPTY)
  73. #define BUCKET_MARK_DELETED(index, idx) (*((uint32_t *)(BUCKET_ADDR(index, idx) + index->key_size)) = DELETED)
  74. #define BUCKET_MARK_EMPTY(index, idx) (*((uint32_t *)(BUCKET_ADDR(index, idx) + index->key_size)) = EMPTY)
  75. #define EPRINTF_MSG(msg, ...) fprintf(stderr, "hashindex: " msg "\n", ##__VA_ARGS__)
  76. #define EPRINTF_MSG_PATH(path, msg, ...) fprintf(stderr, "hashindex: %s: " msg "\n", path, ##__VA_ARGS__)
  77. #define EPRINTF(msg, ...) fprintf(stderr, "hashindex: " msg "(%s)\n", ##__VA_ARGS__, strerror(errno))
  78. #define EPRINTF_PATH(path, msg, ...) fprintf(stderr, "hashindex: %s: " msg " (%s)\n", path, ##__VA_ARGS__, strerror(errno))
  79. static HashIndex *hashindex_read(const char *path);
  80. static int hashindex_write(HashIndex *index, const char *path);
  81. static HashIndex *hashindex_init(int capacity, int key_size, int value_size);
  82. static const void *hashindex_get(HashIndex *index, const void *key);
  83. static int hashindex_set(HashIndex *index, const void *key, const void *value);
  84. static int hashindex_delete(HashIndex *index, const void *key);
  85. static void *hashindex_next_key(HashIndex *index, const void *key);
  86. /* Private API */
  87. static int
  88. hashindex_index(HashIndex *index, const void *key)
  89. {
  90. return _le32toh(*((uint32_t *)key)) % index->num_buckets;
  91. }
  92. static int
  93. hashindex_lookup(HashIndex *index, const void *key)
  94. {
  95. int didx = -1;
  96. int start = hashindex_index(index, key);
  97. int idx = start;
  98. for(;;) {
  99. if(BUCKET_IS_EMPTY(index, idx))
  100. {
  101. return -1;
  102. }
  103. if(BUCKET_IS_DELETED(index, idx)) {
  104. if(didx == -1) {
  105. didx = idx;
  106. }
  107. }
  108. else if(BUCKET_MATCHES_KEY(index, idx, key)) {
  109. if (didx != -1) {
  110. memcpy(BUCKET_ADDR(index, didx), BUCKET_ADDR(index, idx), index->bucket_size);
  111. BUCKET_MARK_DELETED(index, idx);
  112. idx = didx;
  113. }
  114. return idx;
  115. }
  116. idx = (idx + 1) % index->num_buckets;
  117. if(idx == start) {
  118. return -1;
  119. }
  120. }
  121. }
  122. static int
  123. hashindex_resize(HashIndex *index, int capacity)
  124. {
  125. HashIndex *new;
  126. void *key = NULL;
  127. int32_t key_size = index->key_size;
  128. if(!(new = hashindex_init(capacity, key_size, index->value_size))) {
  129. return 0;
  130. }
  131. while((key = hashindex_next_key(index, key))) {
  132. hashindex_set(new, key, key + key_size);
  133. }
  134. free(index->buckets);
  135. index->buckets = new->buckets;
  136. index->num_buckets = new->num_buckets;
  137. index->lower_limit = new->lower_limit;
  138. index->upper_limit = new->upper_limit;
  139. free(new);
  140. return 1;
  141. }
  142. int get_lower_limit(int num_buckets){
  143. int min_buckets = hash_sizes[0];
  144. if (num_buckets <= min_buckets)
  145. return 0;
  146. return (int)(num_buckets * HASH_MIN_LOAD);
  147. }
  148. int get_upper_limit(int num_buckets){
  149. int max_buckets = hash_sizes[NELEMS(hash_sizes) - 1];
  150. if (num_buckets >= max_buckets)
  151. return num_buckets;
  152. return (int)(num_buckets * HASH_MAX_LOAD);
  153. }
  154. int size_idx(int size){
  155. /* find the hash_sizes index with entry >= size */
  156. int elems = NELEMS(hash_sizes);
  157. int entry, i=0;
  158. do{
  159. entry = hash_sizes[i++];
  160. }while((entry < size) && (i < elems));
  161. if (i >= elems)
  162. return elems - 1;
  163. i--;
  164. return i;
  165. }
  166. int fit_size(int current){
  167. int i = size_idx(current);
  168. return hash_sizes[i];
  169. }
  170. int grow_size(int current){
  171. int i = size_idx(current) + 1;
  172. int elems = NELEMS(hash_sizes);
  173. if (i >= elems)
  174. return hash_sizes[elems - 1];
  175. return hash_sizes[i];
  176. }
  177. int shrink_size(int current){
  178. int i = size_idx(current) - 1;
  179. if (i < 0)
  180. return hash_sizes[0];
  181. return hash_sizes[i];
  182. }
  183. /* Public API */
  184. static HashIndex *
  185. hashindex_read(const char *path)
  186. {
  187. FILE *fd;
  188. off_t length, buckets_length, bytes_read;
  189. HashHeader header;
  190. HashIndex *index = NULL;
  191. if((fd = fopen(path, "rb")) == NULL) {
  192. EPRINTF_PATH(path, "fopen for reading failed");
  193. return NULL;
  194. }
  195. bytes_read = fread(&header, 1, sizeof(HashHeader), fd);
  196. if(bytes_read != sizeof(HashHeader)) {
  197. if(ferror(fd)) {
  198. EPRINTF_PATH(path, "fread header failed (expected %ju, got %ju)",
  199. (uintmax_t) sizeof(HashHeader), (uintmax_t) bytes_read);
  200. }
  201. else {
  202. EPRINTF_MSG_PATH(path, "fread header failed (expected %ju, got %ju)",
  203. (uintmax_t) sizeof(HashHeader), (uintmax_t) bytes_read);
  204. }
  205. goto fail;
  206. }
  207. if(fseek(fd, 0, SEEK_END) < 0) {
  208. EPRINTF_PATH(path, "fseek failed");
  209. goto fail;
  210. }
  211. if((length = ftell(fd)) < 0) {
  212. EPRINTF_PATH(path, "ftell failed");
  213. goto fail;
  214. }
  215. if(fseek(fd, sizeof(HashHeader), SEEK_SET) < 0) {
  216. EPRINTF_PATH(path, "fseek failed");
  217. goto fail;
  218. }
  219. if(memcmp(header.magic, MAGIC, MAGIC_LEN)) {
  220. EPRINTF_MSG_PATH(path, "Unknown MAGIC in header");
  221. goto fail;
  222. }
  223. buckets_length = (off_t)_le32toh(header.num_buckets) * (header.key_size + header.value_size);
  224. if((size_t) length != sizeof(HashHeader) + buckets_length) {
  225. EPRINTF_MSG_PATH(path, "Incorrect file length (expected %ju, got %ju)",
  226. (uintmax_t) sizeof(HashHeader) + buckets_length, (uintmax_t) length);
  227. goto fail;
  228. }
  229. if(!(index = malloc(sizeof(HashIndex)))) {
  230. EPRINTF_PATH(path, "malloc header failed");
  231. goto fail;
  232. }
  233. if(!(index->buckets = malloc(buckets_length))) {
  234. EPRINTF_PATH(path, "malloc buckets failed");
  235. free(index);
  236. index = NULL;
  237. goto fail;
  238. }
  239. bytes_read = fread(index->buckets, 1, buckets_length, fd);
  240. if(bytes_read != buckets_length) {
  241. if(ferror(fd)) {
  242. EPRINTF_PATH(path, "fread buckets failed (expected %ju, got %ju)",
  243. (uintmax_t) buckets_length, (uintmax_t) bytes_read);
  244. }
  245. else {
  246. EPRINTF_MSG_PATH(path, "fread buckets failed (expected %ju, got %ju)",
  247. (uintmax_t) buckets_length, (uintmax_t) bytes_read);
  248. }
  249. free(index->buckets);
  250. free(index);
  251. index = NULL;
  252. goto fail;
  253. }
  254. index->num_entries = _le32toh(header.num_entries);
  255. index->num_buckets = _le32toh(header.num_buckets);
  256. index->key_size = header.key_size;
  257. index->value_size = header.value_size;
  258. index->bucket_size = index->key_size + index->value_size;
  259. index->lower_limit = get_lower_limit(index->num_buckets);
  260. index->upper_limit = get_upper_limit(index->num_buckets);
  261. fail:
  262. if(fclose(fd) < 0) {
  263. EPRINTF_PATH(path, "fclose failed");
  264. }
  265. return index;
  266. }
  267. static HashIndex *
  268. hashindex_init(int capacity, int key_size, int value_size)
  269. {
  270. HashIndex *index;
  271. int i;
  272. capacity = fit_size(capacity);
  273. if(!(index = malloc(sizeof(HashIndex)))) {
  274. EPRINTF("malloc header failed");
  275. return NULL;
  276. }
  277. if(!(index->buckets = calloc(capacity, key_size + value_size))) {
  278. EPRINTF("malloc buckets failed");
  279. free(index);
  280. return NULL;
  281. }
  282. index->num_entries = 0;
  283. index->key_size = key_size;
  284. index->value_size = value_size;
  285. index->num_buckets = capacity;
  286. index->bucket_size = index->key_size + index->value_size;
  287. index->lower_limit = get_lower_limit(index->num_buckets);
  288. index->upper_limit = get_upper_limit(index->num_buckets);
  289. for(i = 0; i < capacity; i++) {
  290. BUCKET_MARK_EMPTY(index, i);
  291. }
  292. return index;
  293. }
  294. static void
  295. hashindex_free(HashIndex *index)
  296. {
  297. free(index->buckets);
  298. free(index);
  299. }
  300. static int
  301. hashindex_write(HashIndex *index, const char *path)
  302. {
  303. off_t buckets_length = (off_t)index->num_buckets * index->bucket_size;
  304. FILE *fd;
  305. HashHeader header = {
  306. .magic = MAGIC,
  307. .num_entries = _htole32(index->num_entries),
  308. .num_buckets = _htole32(index->num_buckets),
  309. .key_size = index->key_size,
  310. .value_size = index->value_size
  311. };
  312. int ret = 1;
  313. if((fd = fopen(path, "wb")) == NULL) {
  314. EPRINTF_PATH(path, "fopen for writing failed");
  315. return 0;
  316. }
  317. if(fwrite(&header, 1, sizeof(header), fd) != sizeof(header)) {
  318. EPRINTF_PATH(path, "fwrite header failed");
  319. ret = 0;
  320. }
  321. if(fwrite(index->buckets, 1, buckets_length, fd) != (size_t) buckets_length) {
  322. EPRINTF_PATH(path, "fwrite buckets failed");
  323. ret = 0;
  324. }
  325. if(fclose(fd) < 0) {
  326. EPRINTF_PATH(path, "fclose failed");
  327. }
  328. return ret;
  329. }
  330. static const void *
  331. hashindex_get(HashIndex *index, const void *key)
  332. {
  333. int idx = hashindex_lookup(index, key);
  334. if(idx < 0) {
  335. return NULL;
  336. }
  337. return BUCKET_ADDR(index, idx) + index->key_size;
  338. }
  339. static int
  340. hashindex_set(HashIndex *index, const void *key, const void *value)
  341. {
  342. int idx = hashindex_lookup(index, key);
  343. uint8_t *ptr;
  344. if(idx < 0)
  345. {
  346. if(index->num_entries > index->upper_limit) {
  347. if(!hashindex_resize(index, grow_size(index->num_buckets))) {
  348. return 0;
  349. }
  350. }
  351. idx = hashindex_index(index, key);
  352. while(!BUCKET_IS_EMPTY(index, idx) && !BUCKET_IS_DELETED(index, idx)) {
  353. idx = (idx + 1) % index->num_buckets;
  354. }
  355. ptr = BUCKET_ADDR(index, idx);
  356. memcpy(ptr, key, index->key_size);
  357. memcpy(ptr + index->key_size, value, index->value_size);
  358. index->num_entries += 1;
  359. }
  360. else
  361. {
  362. memcpy(BUCKET_ADDR(index, idx) + index->key_size, value, index->value_size);
  363. }
  364. return 1;
  365. }
  366. static int
  367. hashindex_delete(HashIndex *index, const void *key)
  368. {
  369. int idx = hashindex_lookup(index, key);
  370. if (idx < 0) {
  371. return 1;
  372. }
  373. BUCKET_MARK_DELETED(index, idx);
  374. index->num_entries -= 1;
  375. if(index->num_entries < index->lower_limit) {
  376. if(!hashindex_resize(index, shrink_size(index->num_buckets))) {
  377. return 0;
  378. }
  379. }
  380. return 1;
  381. }
  382. static void *
  383. hashindex_next_key(HashIndex *index, const void *key)
  384. {
  385. int idx = 0;
  386. if(key) {
  387. idx = 1 + (key - index->buckets) / index->bucket_size;
  388. }
  389. if (idx == index->num_buckets) {
  390. return NULL;
  391. }
  392. while(BUCKET_IS_EMPTY(index, idx) || BUCKET_IS_DELETED(index, idx)) {
  393. idx ++;
  394. if (idx == index->num_buckets) {
  395. return NULL;
  396. }
  397. }
  398. return BUCKET_ADDR(index, idx);
  399. }
  400. static int
  401. hashindex_get_size(HashIndex *index)
  402. {
  403. return index->num_entries;
  404. }