_hashindex.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 "ATTICIDX"
  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. HashHeader header;
  125. HashIndex *index = NULL;
  126. if((fd = fopen(path, "r")) == NULL) {
  127. EPRINTF_PATH(path, "fopen failed");
  128. return NULL;
  129. }
  130. if(fread(&header, 1, sizeof(HashHeader), fd) != sizeof(HashHeader)) {
  131. EPRINTF_PATH(path, "fread failed");
  132. goto fail;
  133. }
  134. if(fseek(fd, 0, SEEK_END) < 0) {
  135. EPRINTF_PATH(path, "fseek failed");
  136. goto fail;
  137. }
  138. if((length = ftell(fd)) < 0) {
  139. EPRINTF_PATH(path, "ftell failed");
  140. goto fail;
  141. }
  142. if(fseek(fd, 0, SEEK_SET) < 0) {
  143. EPRINTF_PATH(path, "fseek failed");
  144. goto fail;
  145. }
  146. if(memcmp(header.magic, MAGIC, 8)) {
  147. EPRINTF_PATH(path, "Unknown file header");
  148. goto fail;
  149. }
  150. if(length != sizeof(HashHeader) + (off_t)_le32toh(header.num_buckets) * (header.key_size + header.value_size)) {
  151. EPRINTF_PATH(path, "Incorrect file length");
  152. goto fail;
  153. }
  154. if(!(index = malloc(sizeof(HashIndex)))) {
  155. EPRINTF_PATH(path, "malloc failed");
  156. goto fail;
  157. }
  158. if(!(index->data = malloc(length))) {
  159. EPRINTF_PATH(path, "malloc failed");
  160. free(index);
  161. index = NULL;
  162. goto fail;
  163. }
  164. if(fread(index->data, 1, length, fd) != length) {
  165. EPRINTF_PATH(path, "fread failed");
  166. free(index->data);
  167. free(index);
  168. index = NULL;
  169. goto fail;
  170. }
  171. index->data_len = length;
  172. index->num_entries = _le32toh(header.num_entries);
  173. index->num_buckets = _le32toh(header.num_buckets);
  174. index->key_size = header.key_size;
  175. index->value_size = header.value_size;
  176. index->bucket_size = index->key_size + index->value_size;
  177. index->buckets = index->data + sizeof(HashHeader);
  178. index->lower_limit = index->num_buckets > MIN_BUCKETS ? ((int)(index->num_buckets * BUCKET_LOWER_LIMIT)) : 0;
  179. index->upper_limit = (int)(index->num_buckets * BUCKET_UPPER_LIMIT);
  180. fail:
  181. if(fclose(fd) < 0) {
  182. EPRINTF_PATH(path, "fclose failed");
  183. }
  184. return index;
  185. }
  186. static HashIndex *
  187. hashindex_init(int capacity, int key_size, int value_size)
  188. {
  189. HashIndex *index;
  190. HashHeader header = {
  191. .magic = MAGIC, .num_entries = 0, .key_size = key_size, .value_size = value_size
  192. };
  193. int i;
  194. capacity = MAX(MIN_BUCKETS, capacity);
  195. if(!(index = malloc(sizeof(HashIndex)))) {
  196. EPRINTF("malloc failed");
  197. return NULL;
  198. }
  199. index->data_len = sizeof(HashHeader) + (off_t)capacity * (key_size + value_size);
  200. if(!(index->data = calloc(index->data_len, 1))) {
  201. EPRINTF("malloc failed");
  202. free(index);
  203. return NULL;
  204. }
  205. index->num_entries = 0;
  206. index->key_size = key_size;
  207. index->value_size = value_size;
  208. index->num_buckets = capacity;
  209. index->bucket_size = index->key_size + index->value_size;
  210. index->lower_limit = index->num_buckets > MIN_BUCKETS ? ((int)(index->num_buckets * BUCKET_LOWER_LIMIT)) : 0;
  211. index->upper_limit = (int)(index->num_buckets * BUCKET_UPPER_LIMIT);
  212. index->buckets = index->data + sizeof(HashHeader);
  213. memcpy(index->data, &header, sizeof(HashHeader));
  214. for(i = 0; i < capacity; i++) {
  215. BUCKET_MARK_EMPTY(index, i);
  216. }
  217. return index;
  218. }
  219. static void
  220. hashindex_free(HashIndex *index)
  221. {
  222. free(index->data);
  223. free(index);
  224. }
  225. static int
  226. hashindex_write(HashIndex *index, const char *path)
  227. {
  228. FILE *fd;
  229. int ret = 1;
  230. if((fd = fopen(path, "w")) == NULL) {
  231. EPRINTF_PATH(path, "open failed");
  232. fprintf(stderr, "Failed to open %s for writing\n", path);
  233. return 0;
  234. }
  235. *((uint32_t *)(index->data + 8)) = _htole32(index->num_entries);
  236. *((uint32_t *)(index->data + 12)) = _htole32(index->num_buckets);
  237. if(fwrite(index->data, 1, index->data_len, fd) != index->data_len) {
  238. EPRINTF_PATH(path, "fwrite failed");
  239. ret = 0;
  240. }
  241. if(fclose(fd) < 0) {
  242. EPRINTF_PATH(path, "fclose failed");
  243. }
  244. return ret;
  245. }
  246. static const void *
  247. hashindex_get(HashIndex *index, const void *key)
  248. {
  249. int idx = hashindex_lookup(index, key);
  250. if(idx < 0) {
  251. return NULL;
  252. }
  253. return BUCKET_ADDR(index, idx) + index->key_size;
  254. }
  255. static int
  256. hashindex_set(HashIndex *index, const void *key, const void *value)
  257. {
  258. int idx = hashindex_lookup(index, key);
  259. uint8_t *ptr;
  260. if(idx < 0)
  261. {
  262. if(index->num_entries > index->upper_limit) {
  263. if(!hashindex_resize(index, index->num_buckets * 2)) {
  264. return 0;
  265. }
  266. }
  267. idx = hashindex_index(index, key);
  268. while(!BUCKET_IS_EMPTY(index, idx) && !BUCKET_IS_DELETED(index, idx)) {
  269. idx = (idx + 1) % index->num_buckets;
  270. }
  271. ptr = BUCKET_ADDR(index, idx);
  272. memcpy(ptr, key, index->key_size);
  273. memcpy(ptr + index->key_size, value, index->value_size);
  274. index->num_entries += 1;
  275. }
  276. else
  277. {
  278. memcpy(BUCKET_ADDR(index, idx) + index->key_size, value, index->value_size);
  279. }
  280. return 1;
  281. }
  282. static int
  283. hashindex_delete(HashIndex *index, const void *key)
  284. {
  285. int idx = hashindex_lookup(index, key);
  286. if (idx < 0) {
  287. return 1;
  288. }
  289. BUCKET_MARK_DELETED(index, idx);
  290. index->num_entries -= 1;
  291. if(index->num_entries < index->lower_limit) {
  292. if(!hashindex_resize(index, index->num_buckets / 2)) {
  293. return 0;
  294. }
  295. }
  296. return 1;
  297. }
  298. static void *
  299. hashindex_next_key(HashIndex *index, const void *key)
  300. {
  301. int idx = 0;
  302. if(key) {
  303. idx = 1 + (key - index->buckets) / index->bucket_size;
  304. }
  305. if (idx == index->num_buckets) {
  306. return NULL;
  307. }
  308. while(BUCKET_IS_EMPTY(index, idx) || BUCKET_IS_DELETED(index, idx)) {
  309. idx ++;
  310. if (idx == index->num_buckets) {
  311. return NULL;
  312. }
  313. }
  314. return BUCKET_ADDR(index, idx);
  315. }
  316. static int
  317. hashindex_get_size(HashIndex *index)
  318. {
  319. return index->num_entries;
  320. }
  321. static void
  322. hashindex_summarize(HashIndex *index, long long *total_size, long long *total_csize, long long *total_unique_size, long long *total_unique_csize)
  323. {
  324. int64_t size = 0, csize = 0, unique_size = 0, unique_csize = 0;
  325. const int32_t *values;
  326. void *key = NULL;
  327. while((key = hashindex_next_key(index, key))) {
  328. values = key + 32;
  329. unique_size += values[1];
  330. unique_csize += values[2];
  331. size += values[0] * values[1];
  332. csize += values[0] * values[2];
  333. }
  334. *total_size = size;
  335. *total_csize = csize;
  336. *total_unique_size = unique_size;
  337. *total_unique_csize = unique_csize;
  338. }