bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2005-2018 Dovecot authors, see the included COPYING file */
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen#include "lib.h"
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen#include "array.h"
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen#include "dict-transaction-memory.h"
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainenvoid dict_transaction_memory_init(struct dict_transaction_memory_context *ctx,
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen struct dict *dict, pool_t pool)
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen{
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen ctx->ctx.dict = dict;
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen ctx->pool = pool;
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen p_array_init(&ctx->changes, pool, 32);
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen}
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainenvoid dict_transaction_memory_rollback(struct dict_transaction_context *_ctx)
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen{
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen struct dict_transaction_memory_context *ctx =
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen (struct dict_transaction_memory_context *)_ctx;
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen pool_unref(&ctx->pool);
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen}
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainenvoid dict_transaction_memory_set(struct dict_transaction_context *_ctx,
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen const char *key, const char *value)
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen{
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen struct dict_transaction_memory_context *ctx =
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen (struct dict_transaction_memory_context *)_ctx;
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen struct dict_transaction_memory_change *change;
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen change = array_append_space(&ctx->changes);
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen change->type = DICT_CHANGE_TYPE_SET;
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen change->key = p_strdup(ctx->pool, key);
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen change->value.str = p_strdup(ctx->pool, value);
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen}
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainenvoid dict_transaction_memory_unset(struct dict_transaction_context *_ctx,
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen const char *key)
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen{
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen struct dict_transaction_memory_context *ctx =
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen (struct dict_transaction_memory_context *)_ctx;
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen struct dict_transaction_memory_change *change;
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen change = array_append_space(&ctx->changes);
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen change->type = DICT_CHANGE_TYPE_UNSET;
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen change->key = p_strdup(ctx->pool, key);
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen}
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainenvoid dict_transaction_memory_atomic_inc(struct dict_transaction_context *_ctx,
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen const char *key, long long diff)
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen{
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen struct dict_transaction_memory_context *ctx =
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen (struct dict_transaction_memory_context *)_ctx;
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen struct dict_transaction_memory_change *change;
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen change = array_append_space(&ctx->changes);
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen change->type = DICT_CHANGE_TYPE_INC;
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen change->key = p_strdup(ctx->pool, key);
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen change->value.diff = diff;
8bf42ce5ef783b96a2ded67524173e95e9b45adaTimo Sirainen}