mem.c (3717B)
1 #include <hashmap.h> 2 #include <llog.h> 3 4 #include "lq/mem.h" 5 #include "lq/store.h" 6 #include "lq/err.h" 7 #include "lq/io.h" 8 #include "debug.h" 9 10 11 static const int store_typ_mem = 2; 12 13 struct pair_t { 14 char *key; 15 size_t key_len; 16 char *val; 17 size_t val_len; 18 }; 19 20 static int pair_cmp(const void *a, const void *b, void *userdata) { 21 int i; 22 struct pair_t *pa; 23 struct pair_t *pb; 24 const char *ka; 25 const char *kb; 26 27 pa = (struct pair_t*)a; 28 pb = (struct pair_t*)b; 29 ka = pa->key; 30 kb = pb->key; 31 for (i = 0; i < pa->key_len; i++) { 32 if (*ka == *kb) { 33 ka++; 34 kb++; 35 continue; 36 } 37 if (*ka < *kb) { 38 return -1; 39 } 40 return 1; 41 } 42 return 0; 43 44 } 45 46 static long unsigned int pair_hash(const void *item, long unsigned int s0, long unsigned int s1) { 47 unsigned int r; 48 struct pair_t *o; 49 50 o = (struct pair_t*)item; 51 r = (unsigned int)hashmap_sip(o->key, o->key_len, s0, s1); 52 return r; 53 } 54 55 static void free_item(void *o) { 56 struct pair_t *v; 57 58 v = (struct pair_t*)o; 59 debug_x(LLOG_DEBUG, "store.mem", "freeing key", 1, MORGEL_TYP_BIN, v->key_len, "key", v->key); 60 lq_free(v->key); 61 lq_free(v->val); 62 lq_free((void*)v); 63 } 64 65 struct hashmap* lq_mem_init(LQStore *store) { 66 if (store->userdata == NULL) { 67 store->userdata = (void*)hashmap_new(sizeof(struct pair_t) , 1024*1024, 0, 0, pair_hash, pair_cmp, free_item, NULL); 68 debug(LLOG_INFO, "store.mem", "created new hashmap for mem store"); 69 } 70 return (struct hashmap *)store->userdata; 71 } 72 73 int lq_mem_content_get(enum payload_e typ, LQStore *store, const char *key, size_t key_len, char *value, size_t *value_len) { 74 struct hashmap *o; 75 struct pair_t v; 76 const struct pair_t *p; 77 char path[LQ_PATH_MAX]; 78 79 o = lq_mem_init(store); 80 81 path[0] = (char)typ; 82 lq_cpy(path+1, key, key_len); 83 v.key = path; 84 v.key_len = key_len + 1; 85 v.val = value; 86 v.val_len = *value_len; 87 88 debug_x(LLOG_DEBUG, "store.mem", "store get req", 1, MORGEL_TYP_BIN, v.key_len, "key", v.key); 89 90 p = hashmap_get(o, &v); 91 if (p == NULL) { 92 return ERR_NOENT; 93 } 94 *value_len = p->val_len; 95 lq_cpy(value, p->val, *value_len); 96 97 debug_x(LLOG_DEBUG, "store.mem", "store get res", 2, MORGEL_TYP_BIN, v.key_len, "key", v.key, MORGEL_TYP_NUM, 0, "bytes", *value_len); 98 99 return ERR_OK; 100 } 101 102 int lq_mem_content_put(enum payload_e typ, LQStore *store, const char *key, size_t *key_len, char *value, size_t value_len) { 103 const char *r; 104 struct hashmap *o; 105 struct pair_t *v; 106 char path[LQ_PATH_MAX]; 107 108 o = lq_mem_init(store); 109 110 v = lq_alloc(sizeof(struct pair_t)); 111 112 path[0] = (char)typ; 113 lq_cpy(path+1, key, *key_len); 114 v->key = lq_alloc(LQ_STORE_KEY_MAX); 115 v->key_len = *key_len + 1; 116 lq_cpy(v->key, path, v->key_len); 117 v->val = lq_alloc(LQ_STORE_VAL_MAX); 118 v->val_len = value_len; 119 lq_cpy(v->val, value, value_len); 120 121 debug_x(LLOG_DEBUG, "store.mem", "store put req", 2, MORGEL_TYP_BIN, v->key_len, "key", v->key, MORGEL_TYP_NUM, 0, "bytes", value_len); 122 123 r = hashmap_set(o, v); 124 if (r != NULL) { 125 if (hashmap_oom(o)) { 126 return ERR_WRITE; 127 } 128 } 129 130 debug_x(LLOG_DEBUG, "store.mem", "store put res", 2, MORGEL_TYP_BIN, v->key_len, "key", v->key, MORGEL_TYP_NUM, 0, "bytes", value_len); 131 132 return ERR_OK; 133 } 134 135 void lq_mem_content_free(LQStore *store) { 136 if (store->userdata != NULL) { 137 hashmap_free((struct hashmap*)store->userdata); 138 store->userdata = NULL; 139 } 140 lq_free(store); 141 } 142 143 struct lq_store_t LQMemContent = { 144 .store_typ = store_typ_mem, 145 .userdata = NULL, 146 .get = lq_mem_content_get, 147 .put = lq_mem_content_put, 148 .free = lq_mem_content_free, 149 }; 150 151 LQStore* lq_store_new(const char *spec) { 152 LQStore *store; 153 154 debug(LLOG_DEBUG, "store.mem", "ignoring spec in mem store init"); 155 store = lq_alloc(sizeof(LQStore)); 156 lq_cpy(store, &LQMemContent, sizeof(LQMemContent)); 157 store->userdata = NULL; 158 return store; 159 }