dict.c revision 9844b5359f5cab77e4c31a7ac9e4a60a0073929e
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen/* Copyright (C) 2005 Timo Sirainen */
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen#include "lib.h"
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen#include "array.h"
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen#include "dict-sql.h"
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen#include "dict-private.h"
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainenstatic array_t ARRAY_DEFINE(dict_classes, struct dict *);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainenstatic struct dict *dict_class_lookup(const char *name)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen{
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen struct dict *const *dicts;
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen unsigned int i, count;
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen dicts = array_get(&dict_classes, &count);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen for (i = 0; i < count; i++) {
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen if (strcmp(dicts[i]->name, name) == 0)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen return dicts[i];
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen }
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen return NULL;
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen}
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainenvoid dict_class_register(struct dict *dict_class)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen{
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen if (!array_is_created(&dict_classes))
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen ARRAY_CREATE(&dict_classes, default_pool, struct dict *, 8);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen if (dict_class_lookup(dict_class->name) != NULL) {
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen i_fatal("dict_class_register(%s): Already registered",
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen dict_class->name);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen }
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen array_append(&dict_classes, &dict_class, 1);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen}
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainenvoid dict_class_unregister(struct dict *dict_class)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen{
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen struct dict *const *dicts;
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen unsigned int i, count;
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen dicts = array_get(&dict_classes, &count);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen for (i = 0; i < count; i++) {
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen if (dicts[i] == dict_class) {
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen array_delete(&dict_classes, i, 1);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen break;
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen }
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen }
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen i_assert(i < count);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen if (array_count(&dict_classes) == 0)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen array_free(&dict_classes);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen}
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
9844b5359f5cab77e4c31a7ac9e4a60a0073929eTimo Sirainenstruct dict *dict_init(const char *uri, const char *username)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen{
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen struct dict *dict;
9844b5359f5cab77e4c31a7ac9e4a60a0073929eTimo Sirainen const char *p, *name;
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen p = strchr(uri, ':');
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen if (p == NULL) {
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen i_error("URI is missing ':': %s", uri);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen return NULL;
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen }
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen t_push();
9844b5359f5cab77e4c31a7ac9e4a60a0073929eTimo Sirainen name = t_strdup_until(uri, p);
9844b5359f5cab77e4c31a7ac9e4a60a0073929eTimo Sirainen dict = dict_class_lookup(name);
9844b5359f5cab77e4c31a7ac9e4a60a0073929eTimo Sirainen if (dict == NULL) {
9844b5359f5cab77e4c31a7ac9e4a60a0073929eTimo Sirainen i_error("Unknown dict module: %s", name);
9844b5359f5cab77e4c31a7ac9e4a60a0073929eTimo Sirainen t_pop();
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen return NULL;
9844b5359f5cab77e4c31a7ac9e4a60a0073929eTimo Sirainen }
9844b5359f5cab77e4c31a7ac9e4a60a0073929eTimo Sirainen t_pop();
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
9844b5359f5cab77e4c31a7ac9e4a60a0073929eTimo Sirainen return dict->v.init(dict, p+1, username);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen}
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainenvoid dict_deinit(struct dict **_dict)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen{
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen struct dict *dict = *_dict;
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen *_dict = NULL;
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen dict->v.deinit(dict);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen}
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainenint dict_lookup(struct dict *dict, pool_t pool, const char *key,
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen const char **value_r)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen{
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen return dict->v.lookup(dict, pool, key, value_r);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen}
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainenstruct dict_iterate_context *
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainendict_iterate_init(struct dict *dict, const char *path, bool recurse)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen{
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen return dict->v.iterate_init(dict, path, recurse);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen}
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainenint dict_iterate(struct dict_iterate_context *ctx,
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen const char **key_r, const char **value_r)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen{
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen return ctx->dict->v.iterate(ctx, key_r, value_r);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen}
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainenvoid dict_iterate_deinit(struct dict_iterate_context *ctx)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen{
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen ctx->dict->v.iterate_deinit(ctx);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen}
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainenstruct dict_transaction_context *dict_transaction_begin(struct dict *dict)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen{
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen return dict->v.transaction_init(dict);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen}
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainenint dict_transaction_commit(struct dict_transaction_context *ctx)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen{
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen return ctx->dict->v.transaction_commit(ctx);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen}
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainenvoid dict_transaction_rollback(struct dict_transaction_context *ctx)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen{
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen ctx->dict->v.transaction_rollback(ctx);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen}
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainenvoid dict_set(struct dict_transaction_context *ctx,
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen const char *key, const char *value)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen{
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen ctx->dict->v.set(ctx, key, value);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen}
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainenvoid dict_atomic_inc(struct dict_transaction_context *ctx,
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen const char *key, long long diff)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen{
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen ctx->dict->v.atomic_inc(ctx, key, diff);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen}