16
16
* See the License for the specific language governing permissions and
17
17
* limitations under the License.
18
18
*/
19
- #include <lua.h>
20
19
#include <lauxlib.h>
20
+ #include <lua.h>
21
21
#include <lualib.h>
22
22
#include <stdio.h>
23
23
#include <string.h>
24
24
#include <tidesdb/tidesdb.h>
25
25
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
+ }
53
40
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
+ }
54
57
55
58
static int db_open (lua_State * L );
56
59
static int db_close (lua_State * L );
57
60
static int create_column_family (lua_State * L );
58
61
static int drop_column_family (lua_State * L );
59
62
static int put (lua_State * L );
60
63
static int get (lua_State * L );
61
- static int delete (lua_State * L );
64
+ static int delete (lua_State * L );
62
65
static int list_column_families (lua_State * L );
63
66
static int compact_sstables (lua_State * L );
64
67
@@ -89,26 +92,24 @@ static const luaL_Reg regs_tidesdb_lua[] = {
89
92
};
90
93
91
94
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 },
98
97
};
99
98
100
99
static int db_open (lua_State * L )
101
100
{
102
- const char * directory = luaL_checkstring (L , 1 );
101
+ const char * directory = luaL_checkstring (L , 1 );
103
102
tidesdb_t * db = NULL ;
104
103
tidesdb_err_t * ret = tidesdb_open (directory , & db );
105
- if (ret ) {
104
+ if (ret )
105
+ {
106
106
lua_pushinteger (L , ret -> code );
107
107
lua_pushstring (L , ret -> message );
108
108
tidesdb_err_free (ret );
109
109
return 2 ;
110
- } else {
111
-
110
+ }
111
+ else
112
+ {
112
113
lua_pushinteger (L , 0 );
113
114
lua_pushstring (L , "OK" );
114
115
@@ -124,40 +125,33 @@ static int db_close(lua_State *L)
124
125
lua_getfield (L , -1 , "self_db" );
125
126
tidesdb_t * db = lua_touserdata (L , -1 );
126
127
tidesdb_err_t * ret = tidesdb_close (db );
127
- LUA_RET_CODE ()
128
+ LUA_RET_CODE ()
128
129
}
129
130
130
131
static int create_column_family (lua_State * L )
131
132
{
132
-
133
133
lua_getfield (L , 1 , "self_db" );
134
134
tidesdb_t * db = lua_touserdata (L , -1 );
135
-
136
135
137
- const char * column_family = luaL_checkstring (L , 2 );
136
+ const char * column_family = luaL_checkstring (L , 2 );
138
137
const int flush_threshold = luaL_checkinteger (L , 3 );
139
138
const int max_skip_level = luaL_checkinteger (L , 4 );
140
139
const float prob_skip_level = luaL_checknumber (L , 5 );
141
140
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 );
143
143
const bool enable_bloom_filter = lua_toboolean (L , 8 );
144
144
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 );
154
148
LUA_RET_CODE ()
155
149
}
156
150
static int drop_column_family (lua_State * L )
157
151
{
158
152
lua_getfield (L , 1 , "self_db" );
159
153
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 );
161
155
tidesdb_err_t * ret = tidesdb_drop_column_family (db , column_family );
162
156
LUA_RET_CODE ()
163
157
}
@@ -167,18 +161,13 @@ static int put(lua_State *L)
167
161
lua_getfield (L , 1 , "self_db" );
168
162
tidesdb_t * db = lua_touserdata (L , -1 );
169
163
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 );
172
166
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 );
174
168
const size_t value_size = (size_t )luaL_len (L , 4 );
175
169
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 ,
182
171
ttl == -1 ? ttl : ttl + time (NULL ));
183
172
LUA_RET_CODE ()
184
173
}
@@ -187,51 +176,41 @@ static int get(lua_State *L)
187
176
{
188
177
lua_getfield (L , 1 , "self_db" );
189
178
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 );
192
181
const size_t key_size = (size_t )luaL_len (L , 3 );
193
- uint8_t * value = NULL ;
182
+ uint8_t * value = NULL ;
194
183
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 )
202
186
}
203
187
204
- static int delete (lua_State * L )
188
+ static int delete (lua_State * L )
205
189
{
206
190
lua_getfield (L , 1 , "self_db" );
207
191
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 );
210
194
const size_t key_size = (size_t )luaL_len (L , 3 );
211
195
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 );
216
197
LUA_RET_CODE ()
217
198
}
218
199
219
200
static int compact_sstables (lua_State * L )
220
201
{
221
202
lua_getfield (L , 1 , "self_db" );
222
203
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 );
224
205
const int max_threads = (uint32_t )luaL_checkinteger (L , 3 );
225
206
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 );
229
208
LUA_RET_CODE ()
230
209
}
231
210
232
211
static int list_column_families (lua_State * L )
233
212
{
234
- char * list = NULL ;
213
+ char * list = NULL ;
235
214
lua_getfield (L , -1 , "self_db" );
236
215
tidesdb_t * db = lua_touserdata (L , -1 );
237
216
tidesdb_err_t * ret = tidesdb_list_column_families (db , & list );
@@ -240,18 +219,21 @@ static int list_column_families(lua_State *L)
240
219
241
220
static int txn_begin (lua_State * L )
242
221
{
243
- const char * column_family = luaL_checkstring (L , 2 );
222
+ const char * column_family = luaL_checkstring (L , 2 );
244
223
tidesdb_txn_t * txn = NULL ;
245
224
246
225
lua_getfield (L , -2 , "self_db" );
247
226
tidesdb_t * db = lua_touserdata (L , -1 );
248
227
tidesdb_err_t * ret = tidesdb_txn_begin (db , & txn , column_family );
249
- if (ret ) {
228
+ if (ret )
229
+ {
250
230
lua_pushinteger (L , ret -> code );
251
231
lua_pushstring (L , ret -> message );
252
232
tidesdb_err_free (ret );
253
233
return 2 ;
254
- } else {
234
+ }
235
+ else
236
+ {
255
237
lua_pushinteger (L , 0 );
256
238
lua_pushstring (L , "OK" );
257
239
@@ -267,29 +249,23 @@ static int txn_put(lua_State *L)
267
249
{
268
250
lua_getfield (L , 1 , "self_txn" );
269
251
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 );
271
253
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 );
273
255
const size_t value_size = (size_t )luaL_len (L , 3 );
274
256
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 ));
281
259
LUA_RET_CODE ()
282
260
}
283
261
284
262
static int txn_delete (lua_State * L )
285
263
{
286
264
lua_getfield (L , 1 , "self_txn" );
287
265
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 );
289
267
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 );
293
269
LUA_RET_CODE ()
294
270
}
295
271
0 commit comments