Skip to content

Commit 53b764c

Browse files
authored
Merge pull request #17 from tidesdb/kornev/move_imp_to_src
lib: move impl of libr to src directory
2 parents 68a96bf + cbe8a4e commit 53b764c

File tree

2 files changed

+77
-101
lines changed

2 files changed

+77
-101
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ find_library(LIBRARY_TIDEDB NAMES tidesdb REQUIRED) # require tidesdb library
1212

1313
find_package(Lua 5.3 REQUIRED)
1414

15-
add_library(tidesdb_lua SHARED tidesdb-lua.c)
15+
add_library(tidesdb_lua SHARED src/tidesdb-lua.c)
1616
target_include_directories(tidesdb_lua PRIVATE ${LUA_INCLUDE_DIR})
1717
target_link_libraries(tidesdb_lua ${LUA_LIBRARIES} tidesdb)

tidesdb-lua.c renamed to src/tidesdb-lua.c

Lines changed: 76 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -16,49 +16,52 @@
1616
* See the License for the specific language governing permissions and
1717
* limitations under the License.
1818
*/
19-
#include <lua.h>
2019
#include <lauxlib.h>
20+
#include <lua.h>
2121
#include <lualib.h>
2222
#include <stdio.h>
2323
#include <string.h>
2424
#include <tidesdb/tidesdb.h>
2525

26-
#define LUA_RET_CODE() \
27-
if(ret) \
28-
{ \
29-
lua_pushinteger(L, ret->code); \
30-
lua_pushstring(L, ret->message); \
31-
tidesdb_err_free(ret); \
32-
return 2; \
33-
} else { \
34-
lua_pushinteger(L, 0); \
35-
lua_pushstring(L, "OK"); \
36-
return 2; \
37-
} \
38-
39-
#define LUA_RET_CODE_AND_VALUE(_value, value_size) \
40-
if(ret) \
41-
{ \
42-
lua_pushinteger(L, ret->code); \
43-
lua_pushstring(L, ret->message); \
44-
tidesdb_err_free(ret); \
45-
return 2; \
46-
} else { \
47-
lua_pushinteger(L, 0); \
48-
lua_pushstring(L, "OK"); \
49-
lua_pushlstring(L, _value, value_size); \
50-
free(_value); \
51-
return 3; \
52-
} \
26+
#define LUA_RET_CODE() \
27+
if (ret) \
28+
{ \
29+
lua_pushinteger(L, ret->code); \
30+
lua_pushstring(L, ret->message); \
31+
tidesdb_err_free(ret); \
32+
return 2; \
33+
} \
34+
else \
35+
{ \
36+
lua_pushinteger(L, 0); \
37+
lua_pushstring(L, "OK"); \
38+
return 2; \
39+
}
5340

41+
#define LUA_RET_CODE_AND_VALUE(_value, value_size) \
42+
if (ret) \
43+
{ \
44+
lua_pushinteger(L, ret->code); \
45+
lua_pushstring(L, ret->message); \
46+
tidesdb_err_free(ret); \
47+
return 2; \
48+
} \
49+
else \
50+
{ \
51+
lua_pushinteger(L, 0); \
52+
lua_pushstring(L, "OK"); \
53+
lua_pushlstring(L, _value, value_size); \
54+
free(_value); \
55+
return 3; \
56+
}
5457

5558
static int db_open(lua_State *L);
5659
static int db_close(lua_State *L);
5760
static int create_column_family(lua_State *L);
5861
static int drop_column_family(lua_State *L);
5962
static int put(lua_State *L);
6063
static int get(lua_State *L);
61-
static int delete(lua_State *L);
64+
static int delete (lua_State *L);
6265
static int list_column_families(lua_State *L);
6366
static int compact_sstables(lua_State *L);
6467

@@ -89,26 +92,24 @@ static const luaL_Reg regs_tidesdb_lua[] = {
8992
};
9093

9194
static const luaL_Reg regs_tidesdb_txn_lua[] = {
92-
{"put", txn_put},
93-
{"delete", txn_delete},
94-
{"commit", txn_commit},
95-
{"rollback", txn_rollback},
96-
{"free", txn_free},
97-
{NULL, NULL},
95+
{"put", txn_put}, {"delete", txn_delete}, {"commit", txn_commit},
96+
{"rollback", txn_rollback}, {"free", txn_free}, {NULL, NULL},
9897
};
9998

10099
static int db_open(lua_State *L)
101100
{
102-
const char* directory = luaL_checkstring(L, 1);
101+
const char *directory = luaL_checkstring(L, 1);
103102
tidesdb_t *db = NULL;
104103
tidesdb_err_t *ret = tidesdb_open(directory, &db);
105-
if(ret) {
104+
if (ret)
105+
{
106106
lua_pushinteger(L, ret->code);
107107
lua_pushstring(L, ret->message);
108108
tidesdb_err_free(ret);
109109
return 2;
110-
} else {
111-
110+
}
111+
else
112+
{
112113
lua_pushinteger(L, 0);
113114
lua_pushstring(L, "OK");
114115

@@ -124,40 +125,33 @@ static int db_close(lua_State *L)
124125
lua_getfield(L, -1, "self_db");
125126
tidesdb_t *db = lua_touserdata(L, -1);
126127
tidesdb_err_t *ret = tidesdb_close(db);
127-
LUA_RET_CODE()
128+
LUA_RET_CODE()
128129
}
129130

130131
static int create_column_family(lua_State *L)
131132
{
132-
133133
lua_getfield(L, 1, "self_db");
134134
tidesdb_t *db = lua_touserdata(L, -1);
135-
136135

137-
const char* column_family = luaL_checkstring(L, 2);
136+
const char *column_family = luaL_checkstring(L, 2);
138137
const int flush_threshold = luaL_checkinteger(L, 3);
139138
const int max_skip_level = luaL_checkinteger(L, 4);
140139
const float prob_skip_level = luaL_checknumber(L, 5);
141140
const bool enable_compression = lua_toboolean(L, 6);
142-
const tidesdb_compression_algo_t compression_algo = (tidesdb_compression_algo_t)luaL_checkinteger(L, 7);
141+
const tidesdb_compression_algo_t compression_algo =
142+
(tidesdb_compression_algo_t)luaL_checkinteger(L, 7);
143143
const bool enable_bloom_filter = lua_toboolean(L, 8);
144144
const tidesdb_memtable_ds_t db_data_struct = (tidesdb_memtable_ds_t)luaL_checkinteger(L, 9);
145-
tidesdb_err_t *ret = tidesdb_create_column_family(db,
146-
column_family,
147-
flush_threshold,
148-
max_skip_level,
149-
prob_skip_level,
150-
enable_compression,
151-
compression_algo,
152-
enable_bloom_filter,
153-
db_data_struct);
145+
tidesdb_err_t *ret = tidesdb_create_column_family(
146+
db, column_family, flush_threshold, max_skip_level, prob_skip_level, enable_compression,
147+
compression_algo, enable_bloom_filter, db_data_struct);
154148
LUA_RET_CODE()
155149
}
156150
static int drop_column_family(lua_State *L)
157151
{
158152
lua_getfield(L, 1, "self_db");
159153
tidesdb_t *db = lua_touserdata(L, -1);
160-
const char* column_family = luaL_checkstring(L, 2);
154+
const char *column_family = luaL_checkstring(L, 2);
161155
tidesdb_err_t *ret = tidesdb_drop_column_family(db, column_family);
162156
LUA_RET_CODE()
163157
}
@@ -167,18 +161,13 @@ static int put(lua_State *L)
167161
lua_getfield(L, 1, "self_db");
168162
tidesdb_t *db = lua_touserdata(L, -1);
169163

170-
const char* column_family = luaL_checkstring(L, 2);
171-
const uint8_t* key = (uint8_t*)luaL_checkstring(L, 3);
164+
const char *column_family = luaL_checkstring(L, 2);
165+
const uint8_t *key = (uint8_t *)luaL_checkstring(L, 3);
172166
const size_t key_size = (size_t)luaL_len(L, 3);
173-
const uint8_t* value = (uint8_t*)luaL_checkstring(L, 4);
167+
const uint8_t *value = (uint8_t *)luaL_checkstring(L, 4);
174168
const size_t value_size = (size_t)luaL_len(L, 4);
175169
const int ttl = luaL_checkinteger(L, 5);
176-
tidesdb_err_t *ret = tidesdb_put(db,
177-
column_family,
178-
key,
179-
key_size,
180-
value,
181-
value_size,
170+
tidesdb_err_t *ret = tidesdb_put(db, column_family, key, key_size, value, value_size,
182171
ttl == -1 ? ttl : ttl + time(NULL));
183172
LUA_RET_CODE()
184173
}
@@ -187,51 +176,41 @@ static int get(lua_State *L)
187176
{
188177
lua_getfield(L, 1, "self_db");
189178
tidesdb_t *db = lua_touserdata(L, -1);
190-
const char* column_family = luaL_checkstring(L, 2);
191-
const uint8_t* key = (uint8_t*)luaL_checkstring(L, 3);
179+
const char *column_family = luaL_checkstring(L, 2);
180+
const uint8_t *key = (uint8_t *)luaL_checkstring(L, 3);
192181
const size_t key_size = (size_t)luaL_len(L, 3);
193-
uint8_t* value = NULL;
182+
uint8_t *value = NULL;
194183
size_t value_size = 0;
195-
tidesdb_err_t *ret = tidesdb_get(db,
196-
column_family,
197-
key,
198-
key_size,
199-
&value,
200-
&value_size);
201-
LUA_RET_CODE_AND_VALUE((char*)value, value_size)
184+
tidesdb_err_t *ret = tidesdb_get(db, column_family, key, key_size, &value, &value_size);
185+
LUA_RET_CODE_AND_VALUE((char *)value, value_size)
202186
}
203187

204-
static int delete(lua_State *L)
188+
static int delete (lua_State *L)
205189
{
206190
lua_getfield(L, 1, "self_db");
207191
tidesdb_t *db = lua_touserdata(L, -1);
208-
const char* column_family = luaL_checkstring(L, 2);
209-
const uint8_t* key = (uint8_t*)luaL_checkstring(L, 3);
192+
const char *column_family = luaL_checkstring(L, 2);
193+
const uint8_t *key = (uint8_t *)luaL_checkstring(L, 3);
210194
const size_t key_size = (size_t)luaL_len(L, 3);
211195

212-
tidesdb_err_t *ret = tidesdb_delete(db,
213-
column_family,
214-
key,
215-
key_size);
196+
tidesdb_err_t *ret = tidesdb_delete(db, column_family, key, key_size);
216197
LUA_RET_CODE()
217198
}
218199

219200
static int compact_sstables(lua_State *L)
220201
{
221202
lua_getfield(L, 1, "self_db");
222203
tidesdb_t *db = lua_touserdata(L, -1);
223-
const char* column_family = luaL_checkstring(L, 2);
204+
const char *column_family = luaL_checkstring(L, 2);
224205
const int max_threads = (uint32_t)luaL_checkinteger(L, 3);
225206

226-
tidesdb_err_t *ret = tidesdb_compact_sstables(db,
227-
column_family,
228-
max_threads);
207+
tidesdb_err_t *ret = tidesdb_compact_sstables(db, column_family, max_threads);
229208
LUA_RET_CODE()
230209
}
231210

232211
static int list_column_families(lua_State *L)
233212
{
234-
char* list = NULL;
213+
char *list = NULL;
235214
lua_getfield(L, -1, "self_db");
236215
tidesdb_t *db = lua_touserdata(L, -1);
237216
tidesdb_err_t *ret = tidesdb_list_column_families(db, &list);
@@ -240,18 +219,21 @@ static int list_column_families(lua_State *L)
240219

241220
static int txn_begin(lua_State *L)
242221
{
243-
const char* column_family = luaL_checkstring(L, 2);
222+
const char *column_family = luaL_checkstring(L, 2);
244223
tidesdb_txn_t *txn = NULL;
245224

246225
lua_getfield(L, -2, "self_db");
247226
tidesdb_t *db = lua_touserdata(L, -1);
248227
tidesdb_err_t *ret = tidesdb_txn_begin(db, &txn, column_family);
249-
if(ret) {
228+
if (ret)
229+
{
250230
lua_pushinteger(L, ret->code);
251231
lua_pushstring(L, ret->message);
252232
tidesdb_err_free(ret);
253233
return 2;
254-
} else {
234+
}
235+
else
236+
{
255237
lua_pushinteger(L, 0);
256238
lua_pushstring(L, "OK");
257239

@@ -267,29 +249,23 @@ static int txn_put(lua_State *L)
267249
{
268250
lua_getfield(L, 1, "self_txn");
269251
tidesdb_txn_t *txn = lua_touserdata(L, -1);
270-
const uint8_t* key = (uint8_t*)luaL_checkstring(L, 2);
252+
const uint8_t *key = (uint8_t *)luaL_checkstring(L, 2);
271253
const size_t key_size = (size_t)luaL_len(L, 2);
272-
const uint8_t* value = (uint8_t*)luaL_checkstring(L, 3);
254+
const uint8_t *value = (uint8_t *)luaL_checkstring(L, 3);
273255
const size_t value_size = (size_t)luaL_len(L, 3);
274256
const int ttl = luaL_checkinteger(L, 4);
275-
tidesdb_err_t *ret = tidesdb_txn_put(txn,
276-
key,
277-
key_size,
278-
value,
279-
value_size,
280-
ttl == -1 ? ttl : ttl + time(NULL));
257+
tidesdb_err_t *ret =
258+
tidesdb_txn_put(txn, key, key_size, value, value_size, ttl == -1 ? ttl : ttl + time(NULL));
281259
LUA_RET_CODE()
282260
}
283261

284262
static int txn_delete(lua_State *L)
285263
{
286264
lua_getfield(L, 1, "self_txn");
287265
tidesdb_txn_t *txn = lua_touserdata(L, -1);
288-
const uint8_t* key = (uint8_t*)luaL_checkstring(L, 2);
266+
const uint8_t *key = (uint8_t *)luaL_checkstring(L, 2);
289267
const size_t key_size = (size_t)luaL_len(L, 2);
290-
tidesdb_err_t *ret = tidesdb_txn_delete(txn,
291-
key,
292-
key_size);
268+
tidesdb_err_t *ret = tidesdb_txn_delete(txn, key, key_size);
293269
LUA_RET_CODE()
294270
}
295271

0 commit comments

Comments
 (0)