corned_beef.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <fcntl.h>
  2. #include <stddef.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <sys/mman.h>
  6. #include <unistd.h>
  7. /* We're going to have three kinds of pages in our hash table: the
  8. * first is the 'index', which we'll have exactly one of. It will
  9. * contain the roots to a bunch of linked lists, each one corresponding
  10. * to a hash bucket, and a field to keep track of where the next
  11. * free page is. I'm not including any operations to delete keys or
  12. * values, so that number will increase strictly. */
  13. typedef struct {
  14. size_t list_head[0xff];
  15. size_t next_free_page;
  16. } cb_index;
  17. /* An intermediate node contains a key, a pointer to the next node in
  18. * the list, and a pointer to the corresponding value page. The 'next'
  19. * field will be zero to indicate that we're at the end of a list, and
  20. * otherwise it'll be an index into the on-disk file, pointing to a
  21. * particular page.
  22. */
  23. typedef struct {
  24. size_t next;
  25. size_t val_page;
  26. char key[];
  27. } cb_node;
  28. /* The thirst kind of page is a 'value', which needs no pointers, and
  29. * will be entirely given to a single string. This is all ridiculously
  30. * inefficient: pages are quite large, and we're not doing anything to
  31. * pack pages. It's entirely possible we'll have a hash table like
  32. * {'a': 'x', 'b': 'y'}
  33. * which would involve a total of five pages with mostly empty space.
  34. */
  35. /* Our 'handle' just contains the file descriptor and the index page
  36. * that we've mapped from the underlying file. */
  37. typedef struct {
  38. int file;
  39. cb_index* idx;
  40. } cb_handle;
  41. /* Our hash function is bad. No, really. */
  42. unsigned char very_bad_hash(char* input);
  43. /* Functions for opening/initializing a handle and closing one. */
  44. cb_handle corned_beef_open(char* filename);
  45. void corned_beef_close(cb_handle handle);
  46. /* Functions for initializing a hash table, looking up a key, and
  47. * inserting a value with a key. */
  48. int corned_beef_init(cb_handle handle);
  49. int corned_beef_lookup(cb_handle handle, char* key);
  50. int corned_beef_insert(cb_handle handle, char* key, char* val);