_hashindex.c 6.5 KB

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