2
0

_hashindex.c 10 KB

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