dict-txn-lua.c (dovecot-2.3.16) | : | dict-txn-lua.c (dovecot-2.3.17) | ||
---|---|---|---|---|
skipping to change at line 25 | skipping to change at line 25 | |||
STATE_COMMITTED, | STATE_COMMITTED, | |||
STATE_ABORTED, | STATE_ABORTED, | |||
} state; | } state; | |||
lua_State *L; | lua_State *L; | |||
}; | }; | |||
static int lua_dict_transaction_rollback(lua_State *L); | static int lua_dict_transaction_rollback(lua_State *L); | |||
static int lua_dict_transaction_commit(lua_State *L); | static int lua_dict_transaction_commit(lua_State *L); | |||
static int lua_dict_set(lua_State *L); | static int lua_dict_set(lua_State *L); | |||
static int lua_dict_unset(lua_State *L); | ||||
static int lua_dict_set_timestamp(lua_State *L); | ||||
static luaL_Reg lua_dict_txn_methods[] = { | static luaL_Reg lua_dict_txn_methods[] = { | |||
{ "rollback", lua_dict_transaction_rollback }, | { "rollback", lua_dict_transaction_rollback }, | |||
{ "commit", lua_dict_transaction_commit }, | { "commit", lua_dict_transaction_commit }, | |||
{ "set", lua_dict_set }, | { "set", lua_dict_set }, | |||
{ "unset", lua_dict_unset }, | ||||
{ "set_timestamp", lua_dict_set_timestamp }, | ||||
{ NULL, NULL }, | { NULL, NULL }, | |||
}; | }; | |||
static void sanity_check_txn(lua_State *L, struct lua_dict_txn *txn) | static void sanity_check_txn(lua_State *L, struct lua_dict_txn *txn) | |||
{ | { | |||
switch (txn->state) { | switch (txn->state) { | |||
case STATE_OPEN: | case STATE_OPEN: | |||
return; | return; | |||
case STATE_COMMITTED: | case STATE_COMMITTED: | |||
luaL_error(L, "dict transaction already committed"); | luaL_error(L, "dict transaction already committed"); | |||
skipping to change at line 167 | skipping to change at line 171 | |||
txn = xlua_dict_txn_getptr(L, 1, NULL); | txn = xlua_dict_txn_getptr(L, 1, NULL); | |||
key = luaL_checkstring(L, 2); | key = luaL_checkstring(L, 2); | |||
value = luaL_checkstring(L, 3); | value = luaL_checkstring(L, 3); | |||
dict_set(txn->txn, key, value); | dict_set(txn->txn, key, value); | |||
return 0; | return 0; | |||
} | } | |||
/* | /* | |||
* Start a dict transaction [-1,+1,e] | * Unset key [-2,+0,e] | |||
* | ||||
* Args: | ||||
* 1) userdata: struct lua_dict_txn * | ||||
* 2) string: key | ||||
*/ | ||||
static int lua_dict_unset(lua_State *L) | ||||
{ | ||||
struct lua_dict_txn *txn; | ||||
const char *key; | ||||
DLUA_REQUIRE_ARGS(L, 2); | ||||
txn = xlua_dict_txn_getptr(L, 1, NULL); | ||||
key = luaL_checkstring(L, 2); | ||||
dict_unset(txn->txn, key); | ||||
return 0; | ||||
} | ||||
/* | ||||
* Start a dict transaction [-(1|2),+1,e] | ||||
* | * | |||
* Args: | * Args: | |||
* 1) userdata: struct dict * | * 1) userdata: struct dict * | |||
* 2*) string: username | ||||
* | * | |||
* Returns: | * Returns: | |||
* Returns a new transaction object. | * Returns a new transaction object. | |||
* Username will be NULL if not provided in args. | ||||
*/ | */ | |||
int lua_dict_transaction_begin(lua_State *L) | int lua_dict_transaction_begin(lua_State *L) | |||
{ | { | |||
struct lua_dict_txn *txn; | struct lua_dict_txn *txn; | |||
struct dict *dict; | struct dict *dict; | |||
const char *username = NULL; | ||||
pool_t pool; | pool_t pool; | |||
DLUA_REQUIRE_ARGS(L, 1); | DLUA_REQUIRE_ARGS_IN(L, 1, 2); | |||
dict = dlua_check_dict(L, 1); | dict = dlua_check_dict(L, 1); | |||
if (lua_gettop(L) >= 2) | ||||
username = luaL_checkstring(L, 2); | ||||
pool = pool_alloconly_create("lua dict txn", 128); | pool = pool_alloconly_create("lua dict txn", 128); | |||
txn = p_new(pool, struct lua_dict_txn, 1); | txn = p_new(pool, struct lua_dict_txn, 1); | |||
txn->pool = pool; | txn->pool = pool; | |||
txn->txn = dict_transaction_begin(dict); | ||||
struct dict_op_settings set = { | ||||
.username = username, | ||||
}; | ||||
txn->txn = dict_transaction_begin(dict, &set); | ||||
txn->state = STATE_OPEN; | txn->state = STATE_OPEN; | |||
txn->L = L; | txn->L = L; | |||
xlua_pushdict_txn(L, txn, FALSE); | xlua_pushdict_txn(L, txn, FALSE); | |||
return 1; | return 1; | |||
} | } | |||
/* | ||||
* Set timestamp to the transaction [-2,+0,e] | ||||
* | ||||
* Args: | ||||
* 1) userdata: struct lua_dict_txn * | ||||
* 2) PosixTimespec : { tv_sec, tv_nsec } | ||||
*/ | ||||
static int lua_dict_set_timestamp(lua_State *L) | ||||
{ | ||||
struct lua_dict_txn *txn; | ||||
lua_Number tv_sec, tv_nsec; | ||||
DLUA_REQUIRE_ARGS(L, 2); | ||||
txn = xlua_dict_txn_getptr(L, 1, NULL); | ||||
if (dlua_table_get_number_by_str(L, 2, "tv_sec", &tv_sec) <= 0) | ||||
luaL_error(L, "tv_sec missing from table"); | ||||
if (dlua_table_get_number_by_str(L, 2, "tv_nsec", &tv_nsec) <= 0) | ||||
luaL_error(L, "tv_nsec missing from table"); | ||||
struct timespec ts = { | ||||
.tv_sec = tv_sec, | ||||
.tv_nsec = tv_nsec | ||||
}; | ||||
dict_transaction_set_timestamp(txn->txn, &ts); | ||||
return 0; | ||||
} | ||||
End of changes. 10 change blocks. | ||||
3 lines changed or deleted | 38 lines changed or added |