summaryrefslogtreecommitdiffstats
path: root/src/libutil/hash.h
blob: 3882ce5c7ba1d81b2f293bb106656396d22d77c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
 * @file hash.h
 * Hash table implementation that allows using memory pools for storage as well as using
 * shared memory for this purpose
 */

#ifndef RSPAMD_HASH_H
#define RSPAMD_HASH_H

#include "config.h"

#ifdef __cplusplus
extern "C" {
#endif

struct rspamd_lru_hash_s;
typedef struct rspamd_lru_hash_s rspamd_lru_hash_t;
struct rspamd_lru_element_s;
typedef struct rspamd_lru_element_s rspamd_lru_element_t;


/**
 * Create new lru hash
 * @param maxsize maximum elements in a hash
 * @param maxage maximum age of element
 * @param hash_func pointer to hash function
 * @param key_equal_func pointer to function for comparing keys
 * @return new rspamd_hash object
 */
rspamd_lru_hash_t *rspamd_lru_hash_new(gint maxsize,
									   GDestroyNotify key_destroy,
									   GDestroyNotify value_destroy);


/**
 * Create new lru hash
 * @param maxsize maximum elements in a hash
 * @param maxage maximum age of element
 * @param hash_func pointer to hash function
 * @param key_equal_func pointer to function for comparing keys
 * @return new rspamd_hash object
 */
rspamd_lru_hash_t *rspamd_lru_hash_new_full(gint maxsize,
											GDestroyNotify key_destroy,
											GDestroyNotify value_destroy,
											GHashFunc hfunc,
											GEqualFunc eqfunc);

/**
 * Lookup item from hash
 * @param hash hash object
 * @param key key to find
 * @return value of key or NULL if key is not found
 */
gpointer rspamd_lru_hash_lookup(rspamd_lru_hash_t *hash,
								gconstpointer key,
								time_t now);

/**
 * Removes key from LRU cache
 * @param hash
 * @param key
 * @return TRUE if key has been found and removed
 */
gboolean rspamd_lru_hash_remove(rspamd_lru_hash_t *hash,
								gconstpointer key);

/**
 * Insert item in hash
 * @param hash hash object
 * @param key key to insert
 * @param value value of key
 */
void rspamd_lru_hash_insert(rspamd_lru_hash_t *hash,
							gpointer key,
							gpointer value,
							time_t now,
							guint ttl);

/**
 * Remove lru hash
 * @param hash hash object
 */

void rspamd_lru_hash_destroy(rspamd_lru_hash_t *hash);

/**
 * Iterate over lru hash. Iterations must start from it=0 and are done when it==-1
 * @param hash
 * @param it
 * @param k
 * @param v
 * @return new it or -1 if iteration has been reached over
 */
int rspamd_lru_hash_foreach(rspamd_lru_hash_t *hash, int it, gpointer *k,
							gpointer *v);

/**
 * Returns number of elements in a hash
 * @param hash hash object
 */
guint rspamd_lru_hash_size(rspamd_lru_hash_t *hash);

/**
 * Returns hash capacity
 * @param hash hash object
 */
guint rspamd_lru_hash_capacity(rspamd_lru_hash_t *hash);

#ifdef __cplusplus
}
#endif

#endif