static bool set_insert(const char *value) { unsigned long h; h = CK_HS_HASH(&hs, hs_hash, value); return ck_hs_put(&hs, h, value); }
static bool set_remove(const char *value) { unsigned long h; h = CK_HS_HASH(&hs, hs_hash, value); return ck_hs_remove(&hs, h, value) != NULL; }
static bool set_swap(const char *value) { unsigned long h; void *previous; h = CK_HS_HASH(&hs, hs_hash, value); return ck_hs_fas(&hs, h, value, &previous); }
static void * set_get(const char *value) { unsigned long h; void *v; h = CK_HS_HASH(&hs, hs_hash, value); v = ck_hs_get(&hs, h, value); return v; }
static bool set_replace(const char *value) { unsigned long h; void *previous; h = CK_HS_HASH(&hs, hs_hash, value); ck_hs_set(&hs, h, value, &previous); return previous != NULL; }
int mtev_hash_delete(mtev_hash_table *h, const char *k, int klen, NoitHashFreeFunc keyfree, NoitHashFreeFunc datafree) { long hashv; ck_hash_attr_t *data_struct; ck_key_t *retrieved_key; union { ck_key_t key; char pad[sizeof(ck_key_t) + ONSTACK_KEY_SIZE]; } onstack_key; ck_key_t *key = &onstack_key.key; if(!h) return 0; if(h->u.hs.hf == NULL) { mtevL(mtev_error, "warning: null hashtable in mtev_hash_delete... initializing\n"); mtev_stacktrace(mtev_error); mtev_hash_init(h); } if(klen > ONSTACK_KEY_SIZE) key = calloc(1, sizeof(ck_key_t) + klen + 1); memcpy(key->label, k, klen); key->label[klen] = 0; key->len = klen + sizeof(uint32_t); hashv = CK_HS_HASH(&h->u.hs, hs_hash, key); LOCK(h); retrieved_key = ck_hs_remove(&h->u.hs, hashv, key); UNLOCK(h); if (retrieved_key) { data_struct = index_attribute_container(retrieved_key); if (data_struct) { if (keyfree) keyfree(data_struct->key_ptr); if (datafree) datafree(data_struct->data); free(data_struct); if(key != &onstack_key.key) free(key); return 1; } } if(key != &onstack_key.key) free(key); return 0; }
int mtev_hash_replace(mtev_hash_table *h, const char *k, int klen, void *data, NoitHashFreeFunc keyfree, NoitHashFreeFunc datafree) { long hashv; int ret; void *retrieved_key = NULL; ck_hash_attr_t *data_struct; ck_hash_attr_t *attr = calloc(1, sizeof(ck_hash_attr_t) + klen + 1); if(h->u.hs.hf == NULL) { mtevL(mtev_error, "warning: null hashtable in mtev_hash_replace... initializing\n"); mtev_stacktrace(mtev_error); mtev_hash_init(h); } memcpy(attr->key.label, k, klen); attr->key.label[klen] = 0; attr->key.len = klen + sizeof(uint32_t); attr->data = data; attr->key_ptr = (char*)k; hashv = CK_HS_HASH(&h->u.hs, hs_hash, &attr->key); LOCK(h); ret = ck_hs_set(&h->u.hs, hashv, &attr->key, &retrieved_key); UNLOCK(h); if (ret) { if (retrieved_key) { data_struct = index_attribute_container(retrieved_key); if (data_struct) { if (keyfree) keyfree(data_struct->key_ptr); if (datafree) datafree(data_struct->data); } free(data_struct); } } else { free(attr); } return 1; }
int mtev_hash_store(mtev_hash_table *h, const char *k, int klen, void *data) { long hashv; int ret = 0; ck_hash_attr_t *attr = calloc(1, sizeof(ck_hash_attr_t) + klen + 1); if(h->u.hs.hf == NULL) { mtevL(mtev_error, "warning: null hashtable in mtev_hash_store... initializing\n"); mtev_stacktrace(mtev_error); mtev_hash_init(h); } memcpy(attr->key.label, k, klen); attr->key.label[klen] = 0; attr->key.len = klen + sizeof(uint32_t); attr->key_ptr = (char*)k; attr->data = data; hashv = CK_HS_HASH(&h->u.hs, hs_hash, &attr->key); LOCK(h); ret = ck_hs_put(&h->u.hs, hashv, &attr->key); UNLOCK(h); if (!ret) free(attr); return ret; }