Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: move impl of libr to src directory #17

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
176 changes: 76 additions & 100 deletions tidesdb-lua.c → src/tidesdb-lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,49 +16,52 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <lua.h>
#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>
#include <stdio.h>
#include <string.h>
#include <tidesdb/tidesdb.h>

#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);
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);

Expand Down Expand Up @@ -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");

Expand All @@ -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()
}
Expand All @@ -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()
}
Expand All @@ -187,51 +176,41 @@ 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()
}

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);
Expand All @@ -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");

Expand All @@ -267,29 +249,23 @@ 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()
}

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()
}

Expand Down
Loading