From cbe8a4ee37573d2c94a468b5973f0ab06d33ad6b Mon Sep 17 00:00:00 2001 From: Evgeny Kornev Date: Mon, 13 Jan 2025 11:07:18 +0200 Subject: [PATCH] lib: move impl of libr to src directory move impl of libr to src directory format code according to .clang-format rules --- CMakeLists.txt | 2 +- tidesdb-lua.c => src/tidesdb-lua.c | 176 +++++++++++++---------------- 2 files changed, 77 insertions(+), 101 deletions(-) rename tidesdb-lua.c => src/tidesdb-lua.c (58%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 38297c1..37f0b33 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,6 @@ find_library(LIBRARY_TIDEDB NAMES tidesdb REQUIRED) # require tidesdb library find_package(Lua 5.3 REQUIRED) -add_library(tidesdb_lua SHARED tidesdb-lua.c) +add_library(tidesdb_lua SHARED src/tidesdb-lua.c) target_include_directories(tidesdb_lua PRIVATE ${LUA_INCLUDE_DIR}) target_link_libraries(tidesdb_lua ${LUA_LIBRARIES} tidesdb) \ No newline at end of file diff --git a/tidesdb-lua.c b/src/tidesdb-lua.c similarity index 58% rename from tidesdb-lua.c rename to src/tidesdb-lua.c index 019176d..e9870e9 100644 --- a/tidesdb-lua.c +++ b/src/tidesdb-lua.c @@ -16,41 +16,44 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include #include +#include #include #include #include #include -#define LUA_RET_CODE() \ - if(ret) \ - { \ - lua_pushinteger(L, ret->code); \ - lua_pushstring(L, ret->message); \ - tidesdb_err_free(ret); \ - return 2; \ - } else { \ - lua_pushinteger(L, 0); \ - lua_pushstring(L, "OK"); \ - return 2; \ - } \ - -#define LUA_RET_CODE_AND_VALUE(_value, value_size) \ - if(ret) \ - { \ - lua_pushinteger(L, ret->code); \ - lua_pushstring(L, ret->message); \ - tidesdb_err_free(ret); \ - return 2; \ - } else { \ - lua_pushinteger(L, 0); \ - lua_pushstring(L, "OK"); \ - lua_pushlstring(L, _value, value_size); \ - free(_value); \ - return 3; \ - } \ +#define LUA_RET_CODE() \ + if (ret) \ + { \ + lua_pushinteger(L, ret->code); \ + lua_pushstring(L, ret->message); \ + tidesdb_err_free(ret); \ + return 2; \ + } \ + else \ + { \ + lua_pushinteger(L, 0); \ + lua_pushstring(L, "OK"); \ + return 2; \ + } +#define LUA_RET_CODE_AND_VALUE(_value, value_size) \ + if (ret) \ + { \ + lua_pushinteger(L, ret->code); \ + lua_pushstring(L, ret->message); \ + tidesdb_err_free(ret); \ + return 2; \ + } \ + else \ + { \ + lua_pushinteger(L, 0); \ + lua_pushstring(L, "OK"); \ + lua_pushlstring(L, _value, value_size); \ + free(_value); \ + return 3; \ + } static int db_open(lua_State *L); static int db_close(lua_State *L); @@ -58,7 +61,7 @@ static int create_column_family(lua_State *L); static int drop_column_family(lua_State *L); static int put(lua_State *L); static int get(lua_State *L); -static int delete(lua_State *L); +static int delete (lua_State *L); static int list_column_families(lua_State *L); static int compact_sstables(lua_State *L); @@ -89,26 +92,24 @@ static const luaL_Reg regs_tidesdb_lua[] = { }; static const luaL_Reg regs_tidesdb_txn_lua[] = { - {"put", txn_put}, - {"delete", txn_delete}, - {"commit", txn_commit}, - {"rollback", txn_rollback}, - {"free", txn_free}, - {NULL, NULL}, + {"put", txn_put}, {"delete", txn_delete}, {"commit", txn_commit}, + {"rollback", txn_rollback}, {"free", txn_free}, {NULL, NULL}, }; static int db_open(lua_State *L) { - const char* directory = luaL_checkstring(L, 1); + const char *directory = luaL_checkstring(L, 1); tidesdb_t *db = NULL; tidesdb_err_t *ret = tidesdb_open(directory, &db); - if(ret) { + if (ret) + { lua_pushinteger(L, ret->code); lua_pushstring(L, ret->message); tidesdb_err_free(ret); return 2; - } else { - + } + else + { lua_pushinteger(L, 0); lua_pushstring(L, "OK"); @@ -124,40 +125,33 @@ static int db_close(lua_State *L) lua_getfield(L, -1, "self_db"); tidesdb_t *db = lua_touserdata(L, -1); tidesdb_err_t *ret = tidesdb_close(db); - LUA_RET_CODE() + LUA_RET_CODE() } static int create_column_family(lua_State *L) { - lua_getfield(L, 1, "self_db"); tidesdb_t *db = lua_touserdata(L, -1); - - const char* column_family = luaL_checkstring(L, 2); + const char *column_family = luaL_checkstring(L, 2); const int flush_threshold = luaL_checkinteger(L, 3); const int max_skip_level = luaL_checkinteger(L, 4); const float prob_skip_level = luaL_checknumber(L, 5); const bool enable_compression = lua_toboolean(L, 6); - const tidesdb_compression_algo_t compression_algo = (tidesdb_compression_algo_t)luaL_checkinteger(L, 7); + const tidesdb_compression_algo_t compression_algo = + (tidesdb_compression_algo_t)luaL_checkinteger(L, 7); const bool enable_bloom_filter = lua_toboolean(L, 8); const tidesdb_memtable_ds_t db_data_struct = (tidesdb_memtable_ds_t)luaL_checkinteger(L, 9); - tidesdb_err_t *ret = tidesdb_create_column_family(db, - column_family, - flush_threshold, - max_skip_level, - prob_skip_level, - enable_compression, - compression_algo, - enable_bloom_filter, - db_data_struct); + tidesdb_err_t *ret = tidesdb_create_column_family( + db, column_family, flush_threshold, max_skip_level, prob_skip_level, enable_compression, + compression_algo, enable_bloom_filter, db_data_struct); LUA_RET_CODE() } static int drop_column_family(lua_State *L) { lua_getfield(L, 1, "self_db"); tidesdb_t *db = lua_touserdata(L, -1); - const char* column_family = luaL_checkstring(L, 2); + const char *column_family = luaL_checkstring(L, 2); tidesdb_err_t *ret = tidesdb_drop_column_family(db, column_family); LUA_RET_CODE() } @@ -167,18 +161,13 @@ static int put(lua_State *L) lua_getfield(L, 1, "self_db"); tidesdb_t *db = lua_touserdata(L, -1); - const char* column_family = luaL_checkstring(L, 2); - const uint8_t* key = (uint8_t*)luaL_checkstring(L, 3); + const char *column_family = luaL_checkstring(L, 2); + const uint8_t *key = (uint8_t *)luaL_checkstring(L, 3); const size_t key_size = (size_t)luaL_len(L, 3); - const uint8_t* value = (uint8_t*)luaL_checkstring(L, 4); + const uint8_t *value = (uint8_t *)luaL_checkstring(L, 4); const size_t value_size = (size_t)luaL_len(L, 4); const int ttl = luaL_checkinteger(L, 5); - tidesdb_err_t *ret = tidesdb_put(db, - column_family, - key, - key_size, - value, - value_size, + tidesdb_err_t *ret = tidesdb_put(db, column_family, key, key_size, value, value_size, ttl == -1 ? ttl : ttl + time(NULL)); LUA_RET_CODE() } @@ -187,32 +176,24 @@ static int get(lua_State *L) { lua_getfield(L, 1, "self_db"); tidesdb_t *db = lua_touserdata(L, -1); - const char* column_family = luaL_checkstring(L, 2); - const uint8_t* key = (uint8_t*)luaL_checkstring(L, 3); + const char *column_family = luaL_checkstring(L, 2); + const uint8_t *key = (uint8_t *)luaL_checkstring(L, 3); const size_t key_size = (size_t)luaL_len(L, 3); - uint8_t* value = NULL; + uint8_t *value = NULL; size_t value_size = 0; - tidesdb_err_t *ret = tidesdb_get(db, - column_family, - key, - key_size, - &value, - &value_size); - LUA_RET_CODE_AND_VALUE((char*)value, value_size) + tidesdb_err_t *ret = tidesdb_get(db, column_family, key, key_size, &value, &value_size); + LUA_RET_CODE_AND_VALUE((char *)value, value_size) } -static int delete(lua_State *L) +static int delete (lua_State *L) { lua_getfield(L, 1, "self_db"); tidesdb_t *db = lua_touserdata(L, -1); - const char* column_family = luaL_checkstring(L, 2); - const uint8_t* key = (uint8_t*)luaL_checkstring(L, 3); + const char *column_family = luaL_checkstring(L, 2); + const uint8_t *key = (uint8_t *)luaL_checkstring(L, 3); const size_t key_size = (size_t)luaL_len(L, 3); - tidesdb_err_t *ret = tidesdb_delete(db, - column_family, - key, - key_size); + tidesdb_err_t *ret = tidesdb_delete(db, column_family, key, key_size); LUA_RET_CODE() } @@ -220,18 +201,16 @@ static int compact_sstables(lua_State *L) { lua_getfield(L, 1, "self_db"); tidesdb_t *db = lua_touserdata(L, -1); - const char* column_family = luaL_checkstring(L, 2); + const char *column_family = luaL_checkstring(L, 2); const int max_threads = (uint32_t)luaL_checkinteger(L, 3); - tidesdb_err_t *ret = tidesdb_compact_sstables(db, - column_family, - max_threads); + tidesdb_err_t *ret = tidesdb_compact_sstables(db, column_family, max_threads); LUA_RET_CODE() } static int list_column_families(lua_State *L) { - char* list = NULL; + char *list = NULL; lua_getfield(L, -1, "self_db"); tidesdb_t *db = lua_touserdata(L, -1); tidesdb_err_t *ret = tidesdb_list_column_families(db, &list); @@ -240,18 +219,21 @@ static int list_column_families(lua_State *L) static int txn_begin(lua_State *L) { - const char* column_family = luaL_checkstring(L, 2); + const char *column_family = luaL_checkstring(L, 2); tidesdb_txn_t *txn = NULL; lua_getfield(L, -2, "self_db"); tidesdb_t *db = lua_touserdata(L, -1); tidesdb_err_t *ret = tidesdb_txn_begin(db, &txn, column_family); - if(ret) { + if (ret) + { lua_pushinteger(L, ret->code); lua_pushstring(L, ret->message); tidesdb_err_free(ret); return 2; - } else { + } + else + { lua_pushinteger(L, 0); lua_pushstring(L, "OK"); @@ -267,17 +249,13 @@ static int txn_put(lua_State *L) { lua_getfield(L, 1, "self_txn"); tidesdb_txn_t *txn = lua_touserdata(L, -1); - const uint8_t* key = (uint8_t*)luaL_checkstring(L, 2); + const uint8_t *key = (uint8_t *)luaL_checkstring(L, 2); const size_t key_size = (size_t)luaL_len(L, 2); - const uint8_t* value = (uint8_t*)luaL_checkstring(L, 3); + const uint8_t *value = (uint8_t *)luaL_checkstring(L, 3); const size_t value_size = (size_t)luaL_len(L, 3); const int ttl = luaL_checkinteger(L, 4); - tidesdb_err_t *ret = tidesdb_txn_put(txn, - key, - key_size, - value, - value_size, - ttl == -1 ? ttl : ttl + time(NULL)); + tidesdb_err_t *ret = + tidesdb_txn_put(txn, key, key_size, value, value_size, ttl == -1 ? ttl : ttl + time(NULL)); LUA_RET_CODE() } @@ -285,11 +263,9 @@ static int txn_delete(lua_State *L) { lua_getfield(L, 1, "self_txn"); tidesdb_txn_t *txn = lua_touserdata(L, -1); - const uint8_t* key = (uint8_t*)luaL_checkstring(L, 2); + const uint8_t *key = (uint8_t *)luaL_checkstring(L, 2); const size_t key_size = (size_t)luaL_len(L, 2); - tidesdb_err_t *ret = tidesdb_txn_delete(txn, - key, - key_size); + tidesdb_err_t *ret = tidesdb_txn_delete(txn, key, key_size); LUA_RET_CODE() }