_hashindex.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. typedef struct {
  12. char magic[8];
  13. int32_t num_entries;
  14. int32_t num_buckets;
  15. int8_t key_size;
  16. int8_t value_size;
  17. } __attribute__((__packed__)) HashHeader;
  18. typedef struct {
  19. char *path;
  20. void *map_addr;
  21. off_t map_length;
  22. void *buckets;
  23. int num_entries;
  24. int num_buckets;
  25. int key_size;
  26. int value_size;
  27. int bucket_size;
  28. int limit;
  29. } HashIndex;
  30. #define MAGIC "DARCHASH"
  31. #define EMPTY ((int32_t)-1)
  32. #define DELETED ((int32_t)-2)
  33. #define BUCKET_ADDR_READ(index, idx) (index->buckets + (idx * index->bucket_size))
  34. #define BUCKET_ADDR_WRITE(index, idx) (index->buckets + (idx * index->bucket_size))
  35. #define BUCKET_IS_DELETED(index, idx) (*((int32_t *)(BUCKET_ADDR_READ(index, idx) + index->key_size)) == DELETED)
  36. #define BUCKET_IS_EMPTY(index, idx) (*((int32_t *)(BUCKET_ADDR_READ(index, idx) + index->key_size)) == EMPTY)
  37. #define BUCKET_MATCHES_KEY(index, idx, key) (memcmp(key, BUCKET_ADDR_READ(index, idx), index->key_size) == 0)
  38. #define BUCKET_MARK_DELETED(index, idx) (*((int32_t *)(BUCKET_ADDR_WRITE(index, idx) + index->key_size)) = DELETED)
  39. static HashIndex *hashindex_open(const char *path);
  40. static void hashindex_close(HashIndex *index);
  41. static void hashindex_clear(HashIndex *index);
  42. static void hashindex_flush(HashIndex *index);
  43. static HashIndex *hashindex_create(const char *path, int capacity, int key_size, int value_size);
  44. static const void *hashindex_get(HashIndex *index, const void *key);
  45. static void hashindex_set(HashIndex *index, const void *key, const void *value);
  46. static void hashindex_delete(HashIndex *index, const void *key);
  47. static void *hashindex_next_key(HashIndex *index, const void *key);
  48. /* Private API */
  49. static int
  50. hashindex_index(HashIndex *index, const void *key)
  51. {
  52. return *((uint32_t *)key) % index->num_buckets;
  53. }
  54. static int
  55. hashindex_lookup(HashIndex *index, const void *key)
  56. {
  57. int didx = -1;
  58. int start = hashindex_index(index, key);
  59. int idx = start;
  60. for(;;) {
  61. if(BUCKET_IS_EMPTY(index, idx))
  62. {
  63. return -1;
  64. }
  65. if(BUCKET_IS_DELETED(index, idx)) {
  66. if(didx == -1) {
  67. didx = idx;
  68. }
  69. }
  70. else if(BUCKET_MATCHES_KEY(index, idx, key)) {
  71. if (didx != -1) {
  72. memcpy(BUCKET_ADDR_WRITE(index, didx), BUCKET_ADDR_READ(index, idx), index->bucket_size);
  73. BUCKET_MARK_DELETED(index, idx);
  74. idx = didx;
  75. }
  76. return idx;
  77. }
  78. idx = (idx + 1) % index->num_buckets;
  79. if(idx == start) {
  80. return -1;
  81. }
  82. }
  83. }
  84. static void
  85. hashindex_resize(HashIndex *index, int capacity)
  86. {
  87. char *new_path = malloc(strlen(index->path) + 5);
  88. strcpy(new_path, index->path);
  89. strcat(new_path, ".tmp");
  90. HashIndex *new = hashindex_create(new_path, capacity, index->key_size, index->value_size);
  91. void *key = NULL;
  92. while((key = hashindex_next_key(index, key))) {
  93. hashindex_set(new, key, hashindex_get(index, key));
  94. }
  95. munmap(index->map_addr, index->map_length);
  96. index->map_addr = new->map_addr;
  97. index->map_length = new->map_length;
  98. index->num_buckets = new->num_buckets;
  99. index->limit = new->limit;
  100. index->buckets = new->buckets;
  101. unlink(index->path);
  102. rename(new_path, index->path);
  103. free(new_path);
  104. free(new->path);
  105. free(new);
  106. }
  107. /* Public API */
  108. static HashIndex *
  109. hashindex_open(const char *path)
  110. {
  111. int fd = open(path, O_RDWR);
  112. if(fd < 0) {
  113. fprintf(stderr, "Failed to open %s\n", path);
  114. return NULL;
  115. }
  116. off_t length = lseek(fd, 0, SEEK_END);
  117. void *addr = mmap(0, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  118. close(fd);
  119. if(addr == MAP_FAILED) {
  120. fprintf(stderr, "Failed to mmap %s", path);
  121. }
  122. HashHeader *header = (HashHeader *)addr;
  123. HashIndex *index = malloc(sizeof(HashIndex));
  124. index->path = malloc(strlen(path) + 1);
  125. strcpy(index->path, path);
  126. index->map_addr = addr;
  127. index->map_length = length;
  128. index->num_entries = header->num_entries;
  129. index->num_buckets = header->num_buckets;
  130. index->key_size = header->key_size;
  131. index->value_size = header->value_size;
  132. index->bucket_size = index->key_size + index->value_size;
  133. index->buckets = (addr + sizeof(HashHeader));
  134. index->limit = (int)(index->num_buckets * .75);
  135. return index;
  136. }
  137. static HashIndex *
  138. hashindex_create(const char *path, int capacity, int key_size, int value_size)
  139. {
  140. FILE *fd;
  141. int i;
  142. if(!(fd = fopen(path, "w"))) {
  143. fprintf(stderr, "Failed to create %s\n", path);
  144. return NULL;
  145. }
  146. HashHeader header;
  147. memcpy(header.magic, MAGIC, sizeof(MAGIC) - 1);
  148. header.num_entries = 0;
  149. header.num_buckets = capacity;
  150. header.key_size = key_size;
  151. header.value_size = value_size;
  152. int bucket_size = key_size + value_size;
  153. char *bucket = calloc(bucket_size, 1);
  154. if(fwrite(&header, 1, sizeof(header), fd) != sizeof(header))
  155. goto error;
  156. *((int32_t *)(bucket + key_size)) = EMPTY;
  157. for(i = 0; i < capacity; i++) {
  158. if(fwrite(bucket, 1, bucket_size, fd) != bucket_size)
  159. goto error;
  160. }
  161. free(bucket);
  162. fclose(fd);
  163. return hashindex_open(path);
  164. error:
  165. fclose(fd);
  166. free(bucket);
  167. return NULL;
  168. }
  169. static void
  170. hashindex_clear(HashIndex *index)
  171. {
  172. int i;
  173. for(i = 0; i < index->num_buckets; i++) {
  174. BUCKET_MARK_DELETED(index, i);
  175. }
  176. index->num_entries = 0;
  177. hashindex_resize(index, 16);
  178. }
  179. static void
  180. hashindex_flush(HashIndex *index)
  181. {
  182. *((int32_t *)(index->map_addr + 8)) = index->num_entries;
  183. *((int32_t *)(index->map_addr + 12)) = index->num_buckets;
  184. msync(index->map_addr, index->map_length, MS_SYNC);
  185. }
  186. static void
  187. hashindex_close(HashIndex *index)
  188. {
  189. hashindex_flush(index);
  190. munmap(index->map_addr, index->map_length);
  191. free(index->path);
  192. free(index);
  193. }
  194. static const void *
  195. hashindex_get(HashIndex *index, const void *key)
  196. {
  197. int idx = hashindex_lookup(index, key);
  198. if(idx < 0) {
  199. return NULL;
  200. }
  201. return BUCKET_ADDR_READ(index, idx) + index->key_size;
  202. }
  203. static void
  204. hashindex_set(HashIndex *index, const void *key, const void *value)
  205. {
  206. int idx = hashindex_lookup(index, key);
  207. uint8_t *ptr;
  208. if(idx < 0)
  209. {
  210. if(index->num_entries > index->limit) {
  211. hashindex_resize(index, index->num_buckets * 2);
  212. }
  213. idx = hashindex_index(index, key);
  214. while(!BUCKET_IS_EMPTY(index, idx) && !BUCKET_IS_DELETED(index, idx)) {
  215. idx = (idx + 1) % index->num_buckets;
  216. }
  217. ptr = BUCKET_ADDR_WRITE(index, idx);
  218. memcpy(ptr, key, index->key_size);
  219. memcpy(ptr + index->key_size, value, index->value_size);
  220. index->num_entries += 1;
  221. }
  222. else
  223. {
  224. memcpy(BUCKET_ADDR_WRITE(index, idx) + index->key_size, value, index->value_size);
  225. }
  226. }
  227. static void
  228. hashindex_delete(HashIndex *index, const void *key)
  229. {
  230. int idx = hashindex_lookup(index, key);
  231. if (idx < 0) {
  232. return;
  233. }
  234. BUCKET_MARK_DELETED(index, idx);
  235. index->num_entries -= 1;
  236. }
  237. static void *
  238. hashindex_next_key(HashIndex *index, const void *key)
  239. {
  240. int idx = 0;
  241. if(key) {
  242. idx = 1 + (key - index->buckets) / index->bucket_size;
  243. }
  244. if (idx == index->num_buckets)
  245. return NULL;
  246. while(BUCKET_IS_EMPTY(index, idx) || BUCKET_IS_DELETED(index, idx)) {
  247. idx ++;
  248. if (idx == index->num_buckets)
  249. return NULL;
  250. }
  251. return BUCKET_ADDR_READ(index, idx);
  252. }
  253. static int
  254. hashindex_get_size(HashIndex *index)
  255. {
  256. return index->num_entries;
  257. }