1/*
2 * shash.h
3 *
4 * Copyright (C) 2017 Aerospike, Inc.
5 *
6 * Portions may be licensed to Aerospike, Inc. under one or more contributor
7 * license agreements.
8 *
9 * This program is free software: you can redistribute it and/or modify it under
10 * the terms of the GNU Affero General Public License as published by the Free
11 * Software Foundation, either version 3 of the License, or (at your option) any
12 * later version.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
17 * details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see http://www.gnu.org/licenses/
21 */
22
23#pragma once
24
25//==========================================================
26// Includes.
27//
28
29#include <stdint.h>
30
31#include <citrusleaf/cf_atomic.h>
32
33#include "cf_mutex.h"
34
35
36//==========================================================
37// Typedefs & constants.
38//
39
40// Return codes.
41#define CF_SHASH_ERR_FOUND -4
42#define CF_SHASH_ERR_NOT_FOUND -3
43#define CF_SHASH_ERR -1
44#define CF_SHASH_OK 0
45#define CF_SHASH_REDUCE_DELETE 1
46
47// Bit-values for 'flags' parameter.
48#define CF_SHASH_BIG_LOCK 0x01 // thread-safe with single big lock
49#define CF_SHASH_MANY_LOCK 0x02 // thread-safe with lock per bucket
50
51// User must provide the hash function at create time.
52typedef uint32_t (*cf_shash_hash_fn)(const void *key);
53
54// FIXME - explain or replace.
55typedef void (*cf_shash_update_fn)(const void *key, void *value_old, void *value_new, void *udata);
56
57// The "reduce" function called for every element. Returned value governs
58// behavior during reduce as follows:
59// - CF_SHASH_OK - continue iterating
60// - CF_SHASH_REDUCE_DELETE - delete the current element, continue iterating
61// - anything else (e.g. CF_SHASH_ERR) - stop iterating and return reduce_fn's
62// returned value
63typedef int (*cf_shash_reduce_fn)(const void *key, void *value, void *udata);
64
65// Private data.
66typedef struct cf_shash_s {
67 cf_shash_hash_fn h_fn;
68 uint32_t key_size;
69 uint32_t value_size;
70 uint32_t ele_size;
71 uint32_t n_buckets;
72 uint32_t flags;
73 cf_atomic32 n_elements;
74 void *table;
75 cf_mutex *bucket_locks;
76 cf_mutex big_lock;
77} cf_shash;
78
79
80//==========================================================
81// Public API - useful hash functions.
82//
83
84// TODO - hash function signature may change.
85uint32_t cf_shash_fn_u32(const void *key);
86uint32_t cf_shash_fn_ptr(const void *key);
87uint32_t cf_shash_fn_zstr(const void *key);
88
89
90//==========================================================
91// Public API.
92//
93
94cf_shash *cf_shash_create(cf_shash_hash_fn h_fn, uint32_t key_size, uint32_t value_size, uint32_t n_buckets, uint32_t flags);
95void cf_shash_destroy(cf_shash *h);
96uint32_t cf_shash_get_size(cf_shash *h);
97
98void cf_shash_put(cf_shash *h, const void *key, const void *value);
99int cf_shash_put_unique(cf_shash *h, const void *key, const void *value);
100
101void cf_shash_update(cf_shash *h, const void *key, void *value_old, void *value_new, cf_shash_update_fn update_fn, void *udata);
102
103int cf_shash_get(cf_shash *h, const void *key, void *value);
104int cf_shash_get_vlock(cf_shash *h, const void *key, void **value_r, cf_mutex **vlock_r);
105
106int cf_shash_delete(cf_shash *h, const void *key);
107int cf_shash_delete_lockfree(cf_shash *h, const void *key);
108int cf_shash_get_and_delete(cf_shash *h, const void *key, void *value);
109void cf_shash_delete_all(cf_shash *h);
110
111int cf_shash_reduce(cf_shash *h, cf_shash_reduce_fn reduce_fn, void *udata);
112