_hashindex.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. #include <sys/mman.h>
  11. #if defined(__BYTE_ORDER)&&(__BYTE_ORDER == __BIG_ENDIAN)
  12. #error This code is not big endian safe yet
  13. #endif
  14. typedef struct {
  15. char magic[8];
  16. int32_t num_entries;
  17. int32_t num_buckets;
  18. int8_t key_size;
  19. int8_t value_size;
  20. } __attribute__((__packed__)) HashHeader;
  21. typedef struct {
  22. char *path;
  23. void *map_addr;
  24. off_t map_length;
  25. void *buckets;
  26. int num_entries;
  27. int num_buckets;
  28. int key_size;
  29. int value_size;
  30. int bucket_size;
  31. int lower_limit;
  32. int upper_limit;
  33. } HashIndex;
  34. #define MAGIC "ATTICIDX"
  35. #define EMPTY ((int32_t)-1)
  36. #define DELETED ((int32_t)-2)
  37. #define MAX_BUCKET_SIZE 512
  38. #define BUCKET_LOWER_LIMIT .25
  39. #define BUCKET_UPPER_LIMIT .90
  40. #define MIN_BUCKETS 1024
  41. #define MAX(x, y) ((x) > (y) ? (x): (y))
  42. #define BUCKET_ADDR_READ(index, idx) (index->buckets + (idx * index->bucket_size))
  43. #define BUCKET_ADDR_WRITE(index, idx) (index->buckets + (idx * index->bucket_size))
  44. #define BUCKET_IS_DELETED(index, idx) (*((int32_t *)(BUCKET_ADDR_READ(index, idx) + index->key_size)) == DELETED)
  45. #define BUCKET_IS_EMPTY(index, idx) (*((int32_t *)(BUCKET_ADDR_READ(index, idx) + index->key_size)) == EMPTY)
  46. #define BUCKET_MATCHES_KEY(index, idx, key) (memcmp(key, BUCKET_ADDR_READ(index, idx), index->key_size) == 0)
  47. #define BUCKET_MARK_DELETED(index, idx) (*((int32_t *)(BUCKET_ADDR_WRITE(index, idx) + index->key_size)) = DELETED)
  48. #define EPRINTF(msg, ...) EPRINTF_PATH(index->path, msg, ##__VA_ARGS__)
  49. #define EPRINTF_PATH(path, msg, ...) fprintf(stderr, "hashindex: %s: " msg "\n", path, ##__VA_ARGS__)
  50. static HashIndex *hashindex_open(const char *path);
  51. static int hashindex_close(HashIndex *index);
  52. static int hashindex_clear(HashIndex *index);
  53. static int hashindex_flush(HashIndex *index);
  54. static HashIndex *hashindex_create(const char *path, int capacity, int key_size, int value_size);
  55. static const void *hashindex_get(HashIndex *index, const void *key);
  56. static int hashindex_set(HashIndex *index, const void *key, const void *value);
  57. static int hashindex_delete(HashIndex *index, const void *key);
  58. static void *hashindex_next_key(HashIndex *index, const void *key);
  59. /* Private API */
  60. static int
  61. hashindex_index(HashIndex *index, const void *key)
  62. {
  63. return *((uint32_t *)key) % index->num_buckets;
  64. }
  65. static int
  66. hashindex_lookup(HashIndex *index, const void *key)
  67. {
  68. int didx = -1;
  69. int start = hashindex_index(index, key);
  70. int idx = start;
  71. for(;;) {
  72. if(BUCKET_IS_EMPTY(index, idx))
  73. {
  74. return -1;
  75. }
  76. if(BUCKET_IS_DELETED(index, idx)) {
  77. if(didx == -1) {
  78. didx = idx;
  79. }
  80. }
  81. else if(BUCKET_MATCHES_KEY(index, idx, key)) {
  82. if (didx != -1) {
  83. memcpy(BUCKET_ADDR_WRITE(index, didx), BUCKET_ADDR_READ(index, idx), index->bucket_size);
  84. BUCKET_MARK_DELETED(index, idx);
  85. idx = didx;
  86. }
  87. return idx;
  88. }
  89. idx = (idx + 1) % index->num_buckets;
  90. if(idx == start) {
  91. return -1;
  92. }
  93. }
  94. }
  95. static int
  96. hashindex_resize(HashIndex *index, int capacity)
  97. {
  98. char *new_path = malloc(strlen(index->path) + 5);
  99. strcpy(new_path, index->path);
  100. strcat(new_path, ".tmp");
  101. HashIndex *new;
  102. if(!(new = hashindex_create(new_path, capacity, index->key_size, index->value_size))) {
  103. free(new_path);
  104. return 0;
  105. }
  106. void *key = NULL;
  107. while((key = hashindex_next_key(index, key))) {
  108. hashindex_set(new, key, hashindex_get(index, key));
  109. }
  110. munmap(index->map_addr, index->map_length);
  111. index->map_addr = new->map_addr;
  112. index->map_length = new->map_length;
  113. index->num_buckets = new->num_buckets;
  114. index->lower_limit = new->lower_limit;
  115. index->upper_limit = new->upper_limit;
  116. index->buckets = new->buckets;
  117. unlink(index->path);
  118. rename(new_path, index->path);
  119. free(new_path);
  120. free(new->path);
  121. free(new);
  122. return 1;
  123. }
  124. /* Public API */
  125. static HashIndex *
  126. hashindex_open(const char *path)
  127. {
  128. void *addr;
  129. int fd;
  130. off_t length;
  131. HashHeader *header;
  132. HashIndex *index;
  133. if((fd = open(path, O_RDWR)) < 0) {
  134. EPRINTF_PATH(path, "open failed");
  135. fprintf(stderr, "Failed to open %s\n", path);
  136. return NULL;
  137. }
  138. if((length = lseek(fd, 0, SEEK_END)) < 0) {
  139. EPRINTF_PATH(path, "lseek failed");
  140. if(close(fd) < 0) {
  141. EPRINTF_PATH(path, "close failed");
  142. }
  143. return NULL;
  144. }
  145. addr = mmap(0, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  146. if(close(fd) < 0) {
  147. EPRINTF_PATH(path, "close failed");
  148. return NULL;
  149. }
  150. if(addr == MAP_FAILED) {
  151. EPRINTF_PATH(path, "mmap failed");
  152. return NULL;
  153. }
  154. header = (HashHeader *)addr;
  155. if(memcmp(header->magic, MAGIC, 8)) {
  156. EPRINTF_PATH(path, "Unknown file header");
  157. return NULL;
  158. }
  159. if(length != sizeof(HashHeader) + header->num_buckets * (header->key_size + header->value_size)) {
  160. EPRINTF_PATH(path, "Incorrect file length");
  161. return NULL;
  162. }
  163. if(!(index = malloc(sizeof(HashIndex)))) {
  164. EPRINTF_PATH(path, "malloc failed");
  165. return NULL;
  166. }
  167. index->map_addr = addr;
  168. index->map_length = length;
  169. index->num_entries = header->num_entries;
  170. index->num_buckets = header->num_buckets;
  171. index->key_size = header->key_size;
  172. index->value_size = header->value_size;
  173. index->bucket_size = index->key_size + index->value_size;
  174. index->buckets = (addr + sizeof(HashHeader));
  175. index->lower_limit = index->num_buckets > MIN_BUCKETS ? ((int)(index->num_buckets * BUCKET_LOWER_LIMIT)) : 0;
  176. index->upper_limit = (int)(index->num_buckets * BUCKET_UPPER_LIMIT);
  177. if(!(index->path = strdup(path))) {
  178. EPRINTF_PATH(path, "strdup failed");
  179. free(index);
  180. return NULL;
  181. }
  182. return index;
  183. }
  184. static HashIndex *
  185. hashindex_create(const char *path, int capacity, int key_size, int value_size)
  186. {
  187. FILE *fd;
  188. char bucket[MAX_BUCKET_SIZE] = {};
  189. int i, bucket_size;
  190. HashHeader header = {
  191. .magic = MAGIC, .num_entries = 0, .key_size = key_size, .value_size = value_size
  192. };
  193. capacity = MAX(MIN_BUCKETS, capacity);
  194. header.num_buckets = capacity;
  195. if(!(fd = fopen(path, "w"))) {
  196. EPRINTF_PATH(path, "fopen failed");
  197. return NULL;
  198. }
  199. bucket_size = key_size + value_size;
  200. if(fwrite(&header, 1, sizeof(header), fd) != sizeof(header)) {
  201. goto error;
  202. }
  203. *((int32_t *)(bucket + key_size)) = EMPTY;
  204. for(i = 0; i < capacity; i++) {
  205. if(fwrite(bucket, 1, bucket_size, fd) != bucket_size) {
  206. goto error;
  207. }
  208. }
  209. if(fclose(fd) < 0) {
  210. EPRINTF_PATH(path, "fclose failed");
  211. return NULL;
  212. }
  213. return hashindex_open(path);
  214. error:
  215. EPRINTF_PATH(path, "fwrite failed");
  216. if(fclose(fd) < 0) {
  217. EPRINTF_PATH(path, "fclose failed");
  218. }
  219. return NULL;
  220. }
  221. static int
  222. hashindex_clear(HashIndex *index)
  223. {
  224. int i;
  225. for(i = 0; i < index->num_buckets; i++) {
  226. BUCKET_MARK_DELETED(index, i);
  227. }
  228. index->num_entries = 0;
  229. return hashindex_resize(index, MIN_BUCKETS);
  230. }
  231. static int
  232. hashindex_flush(HashIndex *index)
  233. {
  234. *((int32_t *)(index->map_addr + 8)) = index->num_entries;
  235. *((int32_t *)(index->map_addr + 12)) = index->num_buckets;
  236. if(msync(index->map_addr, index->map_length, MS_SYNC) < 0) {
  237. EPRINTF("msync failed");
  238. return 0;
  239. }
  240. return 1;
  241. }
  242. static int
  243. hashindex_close(HashIndex *index)
  244. {
  245. int rv = 1;
  246. if(hashindex_flush(index) < 0) {
  247. rv = 0;
  248. }
  249. if(munmap(index->map_addr, index->map_length) < 0) {
  250. EPRINTF("munmap failed");
  251. rv = 0;
  252. }
  253. free(index->path);
  254. free(index);
  255. return rv;
  256. }
  257. static const void *
  258. hashindex_get(HashIndex *index, const void *key)
  259. {
  260. int idx = hashindex_lookup(index, key);
  261. if(idx < 0) {
  262. return NULL;
  263. }
  264. return BUCKET_ADDR_READ(index, idx) + index->key_size;
  265. }
  266. static int
  267. hashindex_set(HashIndex *index, const void *key, const void *value)
  268. {
  269. int idx = hashindex_lookup(index, key);
  270. uint8_t *ptr;
  271. if(idx < 0)
  272. {
  273. if(index->num_entries > index->upper_limit) {
  274. if(!hashindex_resize(index, index->num_buckets * 2)) {
  275. return 0;
  276. }
  277. }
  278. idx = hashindex_index(index, key);
  279. while(!BUCKET_IS_EMPTY(index, idx) && !BUCKET_IS_DELETED(index, idx)) {
  280. idx = (idx + 1) % index->num_buckets;
  281. }
  282. ptr = BUCKET_ADDR_WRITE(index, idx);
  283. memcpy(ptr, key, index->key_size);
  284. memcpy(ptr + index->key_size, value, index->value_size);
  285. index->num_entries += 1;
  286. }
  287. else
  288. {
  289. memcpy(BUCKET_ADDR_WRITE(index, idx) + index->key_size, value, index->value_size);
  290. }
  291. return 1;
  292. }
  293. static int
  294. hashindex_delete(HashIndex *index, const void *key)
  295. {
  296. int idx = hashindex_lookup(index, key);
  297. if (idx < 0) {
  298. return 1;
  299. }
  300. BUCKET_MARK_DELETED(index, idx);
  301. index->num_entries -= 1;
  302. if(index->num_entries < index->lower_limit) {
  303. if(!hashindex_resize(index, index->num_buckets * 2)) {
  304. return 0;
  305. }
  306. }
  307. return 1;
  308. }
  309. static void *
  310. hashindex_next_key(HashIndex *index, const void *key)
  311. {
  312. int idx = 0;
  313. if(key) {
  314. idx = 1 + (key - index->buckets) / index->bucket_size;
  315. }
  316. if (idx == index->num_buckets) {
  317. return NULL;
  318. }
  319. while(BUCKET_IS_EMPTY(index, idx) || BUCKET_IS_DELETED(index, idx)) {
  320. idx ++;
  321. if (idx == index->num_buckets) {
  322. return NULL;
  323. }
  324. }
  325. return BUCKET_ADDR_READ(index, idx);
  326. }
  327. static int
  328. hashindex_get_size(HashIndex *index)
  329. {
  330. return index->num_entries;
  331. }