diff options
author | Vsevolod Stakhov <vsevolod@rambler-co.ru> | 2011-12-01 18:32:42 +0300 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@rambler-co.ru> | 2011-12-01 18:32:42 +0300 |
commit | dfc4c4b7b555ef549d913c067faea119211e2487 (patch) | |
tree | a2f4637f1e1d98c81d394c949e42b94ca6138ed4 /src/kvstorage.c | |
parent | a82935802cfcb329ff0ed70a34cc3557d69779ca (diff) | |
download | rspamd-dfc4c4b7b555ef549d913c067faea119211e2487.tar.gz rspamd-dfc4c4b7b555ef549d913c067faea119211e2487.zip |
* Add <no_overwrite> flag for keystorage preventing from overwriting keys (works for cryptographic keys).
* Add <ref> flag for file backend that enables reference count for items in this backend.
Diffstat (limited to 'src/kvstorage.c')
-rw-r--r-- | src/kvstorage.c | 43 |
1 files changed, 29 insertions, 14 deletions
diff --git a/src/kvstorage.c b/src/kvstorage.c index 7c8b3ef80..4a4585094 100644 --- a/src/kvstorage.c +++ b/src/kvstorage.c @@ -34,7 +34,7 @@ /** Create new kv storage */ struct rspamd_kv_storage * rspamd_kv_storage_new (gint id, const gchar *name, struct rspamd_kv_cache *cache, struct rspamd_kv_backend *backend, struct rspamd_kv_expire *expire, - gsize max_elts, gsize max_memory) + gsize max_elts, gsize max_memory, gboolean no_overwrite) { struct rspamd_kv_storage *new; @@ -51,6 +51,8 @@ rspamd_kv_storage_new (gint id, const gchar *name, struct rspamd_kv_cache *cache new->id = id; + new->no_overwrite = no_overwrite; + if (name != NULL) { new->name = g_strdup (name); } @@ -114,10 +116,7 @@ rspamd_kv_storage_insert_cache (struct rspamd_kv_storage *storage, gpointer key, elt = storage->cache->insert_func (storage->cache, key, keylen, data, len); - if (elt == NULL) { - g_static_rw_lock_writer_unlock (&storage->rwlock); - return FALSE; - } + /* Copy data */ elt->flags = flags; elt->expire = expire; @@ -195,17 +194,33 @@ rspamd_kv_storage_insert (struct rspamd_kv_storage *storage, gpointer key, guint elt = storage->cache->lookup_func (storage->cache, key, keylen); if (elt) { - if (storage->expire) { - storage->expire->delete_func (storage->expire, elt); - } - storage->memory -= ELT_SIZE (elt); - storage->cache->steal_func (storage->cache, elt); - if (elt->flags & KV_ELT_DIRTY) { - /* Element is in backend storage queue */ - elt->flags |= KV_ELT_NEED_FREE; + if (!storage->no_overwrite) { + /* Remove old elt */ + if (storage->expire) { + storage->expire->delete_func (storage->expire, elt); + } + storage->memory -= ELT_SIZE (elt); + storage->cache->steal_func (storage->cache, elt); + if (elt->flags & KV_ELT_DIRTY) { + /* Element is in backend storage queue */ + elt->flags |= KV_ELT_NEED_FREE; + } + else { + g_slice_free1 (ELT_SIZE (elt), elt); + } } else { - g_slice_free1 (ELT_SIZE (elt), elt); + /* Just do incref and nothing more */ + if (storage->backend && storage->backend->incref_func) { + if (storage->backend->incref_func (storage->backend, key, keylen)) { + g_static_rw_lock_writer_unlock (&storage->rwlock); + return TRUE; + } + else { + g_static_rw_lock_writer_unlock (&storage->rwlock); + return FALSE; + } + } } } |