_hashindex.c 6.9 KB

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