-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsplinter_cli_cmd_lua.c
More file actions
420 lines (357 loc) · 11.8 KB
/
Copy pathsplinter_cli_cmd_lua.c
File metadata and controls
420 lines (357 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/**
* @file splinter_cli_cmd_label.c
* @brief Implements the CLI 'label' command to tag keys via Bloom filter.
*/
#ifdef HAVE_LUA
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>
#ifdef HAVE_EMBEDDINGS
#include <math.h>
#endif
#include <lua5.4/lua.h>
#include <lua5.4/lualib.h>
#include <lua5.4/lauxlib.h>
#include "splinter_cli.h"
/**
* @brief Register a Lua script's interest in a Signal Group.
*/
static int lua_splinter_watch(lua_State *L) {
const char *key = luaL_checkstring(L, 1);
uint8_t group_id = (uint8_t)luaL_checkinteger(L, 2);
if (splinter_watch_register(key, group_id) == 0) {
lua_pushboolean(L, 1);
return 1;
}
lua_pushboolean(L, 0);
return 1;
}
/**
* @brief Unregister from a Signal Group.
*/
static int lua_splinter_unwatch(lua_State *L) {
const char *key = luaL_checkstring(L, 1);
uint8_t group_id = (uint8_t)luaL_checkinteger(L, 2);
if (splinter_watch_unregister(key, group_id) == 0) {
lua_pushboolean(L, 1);
return 1;
}
lua_pushboolean(L, 0);
return 1;
}
/**
* @brief Batch-retrieves a tandem set (key, key.1, key.2, etc) into a Lua table.
*/
static int lua_splinter_get_tandem(lua_State *L) {
const char *base_key = luaL_checkstring(L, 1);
int max_orders = (int)luaL_optinteger(L, 2, 64); // Safety cap
char tandem_name[SPLINTER_KEY_MAX];
char buf[4096]; // Use a larger buffer or H->max_val_sz if accessible
size_t received = 0;
lua_newtable(L);
for (int i = 0; i < max_orders; i++) {
if (i == 0) {
strncpy(tandem_name, base_key, SPLINTER_KEY_MAX - 1);
} else {
snprintf(tandem_name, sizeof(tandem_name), "%s.%d", base_key, i);
}
if (splinter_get(tandem_name, buf, sizeof(buf), &received) == 0) {
// Push the index (1-based for Lua) and the value
lua_pushinteger(L, i + 1);
lua_pushlstring(L, buf, received);
lua_settable(L, -3);
} else {
// Stop at the first missing order
break;
}
}
return 1;
}
/**
* @brief Batch-pushes a Lua table of values as a tandem set.
* Expects a table where index 1 is the base key, index 2 is order 1, etc.
*/
static int lua_splinter_set_tandem(lua_State *L) {
const char *base_key = luaL_checkstring(L, 1);
luaL_checktype(L, 2, LUA_TTABLE);
char tandem_name[SPLINTER_KEY_MAX];
int n = (int)lua_rawlen(L, 2);
for (int i = 1; i <= n; i++) {
lua_rawgeti(L, 2, i);
size_t len;
const char *val = lua_tolstring(L, -1, &len);
if (i == 1) {
strncpy(tandem_name, base_key, SPLINTER_KEY_MAX - 1);
} else {
snprintf(tandem_name, sizeof(tandem_name), "%s.%d", base_key, i - 1);
}
if (val && splinter_set(tandem_name, val, len) != 0) {
lua_pushboolean(L, 0);
return 1;
}
lua_pop(L, 1);
}
lua_pushboolean(L, 1);
return 1;
}
static int lua_splinter_math(lua_State *L) {
const char *key = luaL_checkstring(L, 1);
const char *op_str = luaL_checkstring(L, 2);
uint64_t val = (uint64_t)luaL_optinteger(L, 3, 0);
splinter_integer_op_t op;
if (strcasecmp(op_str, "inc") == 0) op = SPL_OP_INC;
else if (strcasecmp(op_str, "dec") == 0) op = SPL_OP_DEC;
else if (strcasecmp(op_str, "and") == 0) op = SPL_OP_AND;
else if (strcasecmp(op_str, "or") == 0) op = SPL_OP_OR;
else if (strcasecmp(op_str, "xor") == 0) op = SPL_OP_XOR;
else if (strcasecmp(op_str, "not") == 0) op = SPL_OP_NOT;
else return luaL_error(L, "invalid math operation: %s", op_str);
if (splinter_integer_op(key, op, &val) == 0) {
lua_pushboolean(L, 1);
return 1;
}
if (errno == EPROTOTYPE) {
return luaL_error(L, "key '%s' is not a BIGUINT slot", key);
}
lua_pushboolean(L, 0);
return 1;
}
static int lua_splinter_get(lua_State *L) {
const char *key = luaL_checkstring(L, 1);
char buf[SPLINTER_KEY_MAX + 8]; // Support key + alignment pad
size_t received = 0;
splinter_slot_snapshot_t snap = {0};
// 1. Fetch data from SHM
if (splinter_get(key, buf, sizeof(buf), &received) != 0) {
lua_pushnil(L);
return 1;
}
// 2. Determine semantic type via snapshot
if (splinter_get_slot_snapshot(key, &snap) == 0) {
if (snap.type_flag & SPL_SLOT_TYPE_BIGUINT) {
// Return as a Lua integer for math
uint64_t val = *(uint64_t *)buf;
lua_pushinteger(L, (lua_Integer)val);
return 1;
}
}
// 3. Fallback to string (VARTEXT/JSON/VOID)
lua_pushlstring(L, buf, received);
return 1;
}
static int lua_splinter_set(lua_State *L) {
const char *key = luaL_checkstring(L, 1);
int type = lua_type(L, 2);
if (type == LUA_TNUMBER) {
// 1. Auto-promote to BIGUINT and expand if needed
uint64_t val = (uint64_t)lua_tointeger(L, 2);
// Prepare the slot for 8-byte integer operations
if (splinter_set_named_type(key, SPL_SLOT_TYPE_BIGUINT) != 0) {
// If key doesn't exist, we create it first as VOID then upgrade
// This maintains the "silent" experience
splinter_set(key, &val, 8);
splinter_set_named_type(key, SPL_SLOT_TYPE_BIGUINT);
} else {
// Key existed, was expanded, now store the integer
splinter_set(key, &val, 8);
}
} else {
// 2. Standard string/binary set
size_t len;
const char *val = luaL_checklstring(L, 2, &len);
if (splinter_set(key, val, len) != 0) {
lua_pushboolean(L, 0);
return 1;
}
}
lua_pushboolean(L, 1);
return 1;
}
static int lua_splinter_unset(lua_State *L) {
const char *key = luaL_checkstring(L, 1);
int deleted_len = splinter_unset(key);
if (deleted_len >= 0) {
lua_pushinteger(L, deleted_len);
return 1;
}
lua_pushboolean(L, 0);
return 1;
}
static int lua_splinter_label(lua_State *L) {
const char *key = luaL_checkstring(L, 1);
uint64_t mask = 0;
if (lua_isnumber(L, 2)) {
mask = (uint64_t)lua_tointeger(L, 2);
} else {
// Here you could link into your label resolution logic if desired
return luaL_error(L, "Label must be a numeric mask");
}
if (splinter_set_label(key, mask) == 0) {
lua_pushboolean(L, 1);
return 1;
}
lua_pushboolean(L, 0);
return 1;
}
/**
* @brief Pulse the IPC bus for a slot without doing any other work.
*
* Wraps splinter_bump_slot() so a script can wake splinference (or any
* watcher) after applying an EMBED_LABEL / writing an embedding. Returns
* true on success, false if the key was not found.
*/
static int lua_splinter_bump(lua_State *L) {
const char *key = luaL_checkstring(L, 1);
if (splinter_bump_slot(key) == 0) {
lua_pushboolean(L, 1);
return 1;
}
lua_pushboolean(L, 0);
return 1;
}
/**
* @brief Sleep for a number of milliseconds, yielding the CPU.
*
* Lets a polling script (e.g. one waiting on splinference to populate a
* slot) back off instead of spinning. Negative values are clamped to 0.
*/
static int lua_splinter_sleep(lua_State *L) {
lua_Integer ms = luaL_checkinteger(L, 1);
if (ms < 0) ms = 0;
struct timespec req = {
.tv_sec = (time_t)(ms / 1000),
.tv_nsec = (long)((ms % 1000) * 1000000L)
};
/* Resume across signal interruptions so the full interval elapses. */
while (nanosleep(&req, &req) != 0 && errno == EINTR)
;
return 0;
}
#ifdef HAVE_EMBEDDINGS
/**
* @brief Retrieve a slot's embedding snapshot as a Lua array table.
*
* Returns a table of SPLINTER_EMBED_DIM floats (1-based) when the vector
* carries signal (magnitude > 1e-6), otherwise nil. An unpopulated slot
* reads back as a zero vector, which we treat as "no embedding".
*/
static int lua_splinter_get_embedding(lua_State *L) {
const char *key = luaL_checkstring(L, 1);
float vec[SPLINTER_EMBED_DIM];
if (splinter_get_embedding(key, vec) != 0) {
lua_pushnil(L);
return 1;
}
double sum = 0.0;
for (int i = 0; i < SPLINTER_EMBED_DIM; i++) {
sum += (double)vec[i] * (double)vec[i];
}
if (sqrt(sum) <= 1e-6) {
lua_pushnil(L);
return 1;
}
lua_createtable(L, SPLINTER_EMBED_DIM, 0);
for (int i = 0; i < SPLINTER_EMBED_DIM; i++) {
lua_pushnumber(L, (lua_Number)vec[i]);
lua_rawseti(L, -2, i + 1);
}
return 1;
}
/**
* @brief Write a Lua array table of SPLINTER_EMBED_DIM floats into a slot.
*
* The table must hold exactly SPLINTER_EMBED_DIM numeric entries (1-based);
* anything shorter is rejected so we never write a partial vector. Returns
* true on success, false if the underlying write fails.
*/
static int lua_splinter_set_embedding(lua_State *L) {
const char *key = luaL_checkstring(L, 1);
luaL_checktype(L, 2, LUA_TTABLE);
int n = (int)lua_rawlen(L, 2);
if (n != SPLINTER_EMBED_DIM) {
return luaL_error(L, "embedding table must hold exactly %d floats (got %d)",
SPLINTER_EMBED_DIM, n);
}
float vec[SPLINTER_EMBED_DIM];
for (int i = 0; i < SPLINTER_EMBED_DIM; i++) {
lua_rawgeti(L, 2, i + 1);
if (!lua_isnumber(L, -1)) {
lua_pop(L, 1);
return luaL_error(L, "embedding element %d is not a number", i + 1);
}
vec[i] = (float)lua_tonumber(L, -1);
lua_pop(L, 1);
}
if (splinter_set_embedding(key, vec) == 0) {
lua_pushboolean(L, 1);
return 1;
}
lua_pushboolean(L, 0);
return 1;
}
#endif // HAVE_EMBEDDINGS
static const char *modname = "lua";
void help_cmd_lua(unsigned int level) {
(void) level;
printf("Usage: %s <script.lua> [args...]\n", modname);
puts("");
puts("Any args after the script are exposed to it via the standard Lua");
puts("'arg' table: arg[0] is the script path, arg[1..n] are the args.");
puts("");
puts(" lua tag.lua mykey 42 --> arg[1]=\"mykey\", arg[2]=\"42\"");
puts("");
}
// This may end up being shared across modules
// Hence, not static. But not prototyped publicly either, yet.
int luaopen_splinter(lua_State *L) {
static const struct luaL_Reg bus_funcs[] = {
{"get", lua_splinter_get},
{"get_tandem", lua_splinter_get_tandem},
{"set", lua_splinter_set},
{"set_tandem", lua_splinter_set_tandem},
{"math", lua_splinter_math},
{"watch", lua_splinter_watch},
{"unwatch", lua_splinter_unwatch},
{"label", lua_splinter_label},
{"unset", lua_splinter_unset},
{"bump", lua_splinter_bump},
{"sleep", lua_splinter_sleep},
#ifdef HAVE_EMBEDDINGS
{"get_embedding", lua_splinter_get_embedding},
{"set_embedding", lua_splinter_set_embedding},
#endif
{NULL, NULL}
};
luaL_newlib(L, bus_funcs);
return 1;
}
int cmd_lua(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: lua <script.lua>\n");
return 1;
}
lua_State *L = luaL_newstate();
luaL_openlibs(L);
luaL_requiref(L, "splinter", luaopen_splinter, 1);
lua_pop(L, 1);
/* Expose invocation args the standard Lua way: arg[0] is the script,
* arg[1..n] are whatever followed it (e.g. a key name to operate on). */
lua_createtable(L, argc - 2, 1);
lua_pushstring(L, argv[1]);
lua_rawseti(L, -2, 0);
for (int i = 2; i < argc; i++) {
lua_pushstring(L, argv[i]);
lua_rawseti(L, -2, i - 1);
}
lua_setglobal(L, "arg");
if (luaL_dofile(L, argv[1]) != LUA_OK) {
fprintf(stderr, "Lua Error: %s\n", lua_tostring(L, -1));
lua_close(L);
return 1;
}
lua_close(L);
return 0;
}
#endif // HAVE_LUA