aboutsummaryrefslogtreecommitdiffstats
path: root/src/hash.h
blob: fc336b6bb7dffdddea46bd8cc7f2656e51be6686 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
 * @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 "mem_pool.h"

struct rspamd_hash_node {
	gpointer                 key;
	gpointer                 value;
	guint                    key_hash;
	struct rspamd_hash_node *next;
};

typedef struct rspamd_hash_s {
	gint                      size;
	gint                      nnodes;
	struct rspamd_hash_node **nodes;

	GHashFunc                 hash_func;
	GEqualFunc                key_equal_func;
	gint                      shared;
	memory_pool_rwlock_t     *lock;
	memory_pool_t            *pool;
} rspamd_hash_t;

typedef void (*lru_cache_insert_func)(gpointer storage, gpointer key, gpointer value);
typedef gpointer (*lru_cache_lookup_func)(gpointer storage, gpointer key);
typedef gboolean (*lru_cache_delete_func)(gpointer storage, gpointer key);
typedef void (*lru_cache_destroy_func)(gpointer storage);

typedef struct rspamd_lru_hash_s {
	gint                      maxsize;
	gint                      maxage;
	GDestroyNotify            value_destroy;
	GDestroyNotify			  key_destroy;
	GQueue                   *q;
	gpointer                  storage;
	lru_cache_insert_func     insert_func;
	lru_cache_lookup_func     lookup_func;
	lru_cache_delete_func     delete_func;
	lru_cache_destroy_func    destroy_func;
} rspamd_lru_hash_t;

typedef struct rspamd_lru_element_s {
	gpointer                  data;
	gpointer                  key;
	time_t                    store_time;
	rspamd_lru_hash_t        *hash;
} rspamd_lru_element_t;


#define rspamd_hash_size(x) (x)->nnodes

/**
 * Create new hash in specified pool
 * @param pool memory pool object
 * @param hash_func pointer to hash function
 * @param key_equal_func pointer to function for comparing keys
 * @return new rspamd_hash object
 */
rspamd_hash_t* rspamd_hash_new (memory_pool_t *pool, GHashFunc hash_func, GEqualFunc key_equal_func);

/**
 * Create new hash in specified pool using shared memory
 * @param pool memory pool object
 * @param hash_func pointer to hash function
 * @param key_equal_func pointer to function for comparing keys
 * @return new rspamd_hash object
 */
rspamd_hash_t* rspamd_hash_new_shared (memory_pool_t *pool, GHashFunc hash_func, GEqualFunc key_equal_func, gint size);

/**
 * Insert item in hash
 * @param hash hash object
 * @param key key to insert
 * @param value value of key
 */
void rspamd_hash_insert (rspamd_hash_t *hash, gpointer key, gpointer value);

/**
 * Remove item from hash
 * @param hash hash object
 * @param key key to delete
 */
gboolean rspamd_hash_remove (rspamd_hash_t *hash, gpointer key);

/**
 * 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_hash_lookup (rspamd_hash_t *hash, gpointer key);

/** 
 * Iterate throught hash
 * @param hash hash object
 * @param func user's function that would be called for each key/value pair
 * @param user_data pointer to user's data that would be passed to user's function
 */
void rspamd_hash_foreach (rspamd_hash_t *hash, GHFunc func, gpointer user_data);

/**
 * Create new lru hash
 * @param maxsize maximum elements in a hash
 * @param maxage maximum age of elemnt
 * @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 (GHashFunc hash_func, GEqualFunc key_equal_func,
		gint maxsize, gint maxage, GDestroyNotify key_destroy, GDestroyNotify value_destroy);

/**
 * Create new lru hash with custom storage
 * @param maxsize maximum elements in a hash
 * @param maxage maximum age of elemnt
 * @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 (GHashFunc hash_func, GEqualFunc key_equal_func,
		gint maxsize, gint maxage, GDestroyNotify key_destroy, GDestroyNotify value_destroy,
		gpointer storage, lru_cache_insert_func insert_func, lru_cache_lookup_func lookup_func,
		lru_cache_delete_func delete_func);
/**
 * 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, gpointer key, time_t now);
/**
 * 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);

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

void rspamd_lru_hash_destroy (rspamd_lru_hash_t *hash);

#endif

/*
 * vi:ts=4
 */