-
Notifications
You must be signed in to change notification settings - Fork 2
/
redismodule.lua
424 lines (387 loc) · 14.9 KB
/
redismodule.lua
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
421
422
423
424
-- redismodule.lua
-- Copyright (c) 2016 Evan Wies
--
-- extracted from:
-- https://github.com/antirez/redis/blob/unstable/src/redismodule.h
--
-- NOTE: the API is currently unstable
local ffi = require 'ffi'
ffi.cdef([[
typedef long long mstime_t;
typedef struct RedisModuleCtx RedisModuleCtx;
typedef struct RedisModuleKey RedisModuleKey;
typedef struct RedisModuleString RedisModuleString;
typedef struct RedisModuleCallReply RedisModuleCallReply;
typedef int (*RedisModuleCmdFunc) (RedisModuleCtx *ctx, RedisModuleString **argv, int argc);
int RM_CreateCommand(RedisModuleCtx *ctx, const char *name, RedisModuleCmdFunc cmdfunc, const char *strflags, int firstkey, int lastkey, int keystep);
int RM_WrongArity(RedisModuleCtx *ctx);
int RM_ReplyWithLongLong(RedisModuleCtx *ctx, long long ll);
int RM_GetSelectedDb(RedisModuleCtx *ctx);
int RM_SelectDb(RedisModuleCtx *ctx, int newid);
void *RM_OpenKey(RedisModuleCtx *ctx, RedisModuleString *keyname, int mode);
void RM_CloseKey(RedisModuleKey *kp);
int RM_KeyType(RedisModuleKey *kp);
size_t RM_ValueLength(RedisModuleKey *kp);
int RM_ListPush(RedisModuleKey *kp, int where, RedisModuleString *ele);
RedisModuleString *RM_ListPop(RedisModuleKey *key, int where);
RedisModuleCallReply *RM_Call(RedisModuleCtx *ctx, const char *cmdname, const char *fmt, ...);
const char *RM_CallReplyProto(RedisModuleCallReply *reply, size_t *len);
void RM_FreeCallReply(RedisModuleCallReply *reply);
int RM_CallReplyType(RedisModuleCallReply *reply);
long long RM_CallReplyInteger(RedisModuleCallReply *reply);
size_t RM_CallReplyLength(RedisModuleCallReply *reply);
RedisModuleCallReply *RM_CallReplyArrayElement(RedisModuleCallReply *reply, size_t idx);
RedisModuleString *RM_CreateString(RedisModuleCtx *ctx, const char *ptr, size_t len);
RedisModuleString *RM_CreateStringFromLongLong(RedisModuleCtx *ctx, long long ll);
void RM_FreeString(RedisModuleCtx *ctx, RedisModuleString *str);
const char *RM_StringPtrLen(RedisModuleString *str, size_t *len);
int RM_ReplyWithError(RedisModuleCtx *ctx, const char *err);
int RM_ReplyWithSimpleString(RedisModuleCtx *ctx, const char *msg);
int RM_ReplyWithArray(RedisModuleCtx *ctx, long len);
void RM_ReplySetArrayLength(RedisModuleCtx *ctx, long len);
int RM_ReplyWithStringBuffer(RedisModuleCtx *ctx, const char *buf, size_t len);
int RM_ReplyWithString(RedisModuleCtx *ctx, RedisModuleString *str);
int RM_ReplyWithNull(RedisModuleCtx *ctx);
int RM_ReplyWithDouble(RedisModuleCtx *ctx, double d);
int RM_ReplyWithCallReply(RedisModuleCtx *ctx, RedisModuleCallReply *reply);
int RM_StringToLongLong(RedisModuleString *str, long long *ll);
int RM_StringToDouble(RedisModuleString *str, double *d);
void RM_AutoMemory(RedisModuleCtx *ctx);
int RM_Replicate(RedisModuleCtx *ctx, const char *cmdname, const char *fmt, ...);
int RM_ReplicateVerbatim(RedisModuleCtx *ctx);
const char *RM_CallReplyStringPtr(RedisModuleCallReply *reply, size_t *len);
RedisModuleString *RM_CreateStringFromCallReply(RedisModuleCallReply *reply);
int RM_DeleteKey(RedisModuleKey *key);
int RM_StringSet(RedisModuleKey *key, RedisModuleString *str);
char *RM_StringDMA(RedisModuleKey *key, size_t *len, int mode);
int RM_StringTruncate(RedisModuleKey *key, size_t newlen);
mstime_t RM_GetExpire(RedisModuleKey *key);
int RM_SetExpire(RedisModuleKey *key, mstime_t expire);
int RM_ZsetAdd(RedisModuleKey *key, double score, RedisModuleString *ele, int *flagsptr);
int RM_ZsetIncrby(RedisModuleKey *key, double score, RedisModuleString *ele, int *flagsptr, double *newscore);
int RM_ZsetScore(RedisModuleKey *key, RedisModuleString *ele, double *score);
int RM_ZsetRem(RedisModuleKey *key, RedisModuleString *ele, int *deleted);
void RM_ZsetRangeStop(RedisModuleKey *key);
int RM_ZsetFirstInScoreRange(RedisModuleKey *key, double min, double max, int minex, int maxex);
int RM_ZsetLastInScoreRange(RedisModuleKey *key, double min, double max, int minex, int maxex);
int RM_ZsetFirstInLexRange(RedisModuleKey *key, RedisModuleString *min, RedisModuleString *max);
int RM_ZsetLastInLexRange(RedisModuleKey *key, RedisModuleString *min, RedisModuleString *max);
RedisModuleString *RM_ZsetRangeCurrentElement(RedisModuleKey *key, double *score);
int RM_ZsetRangeNext(RedisModuleKey *key);
int RM_ZsetRangePrev(RedisModuleKey *key);
int RM_ZsetRangeEndReached(RedisModuleKey *key);
int RM_HashSet(RedisModuleKey *key, int flags, ...);
int RM_HashGet(RedisModuleKey *key, int flags, ...);
int RM_IsKeysPositionRequest(RedisModuleCtx *ctx);
void RM_KeyAtPos(RedisModuleCtx *ctx, int pos);
unsigned long long RM_GetClientId(RedisModuleCtx *ctx);
RedisModuleCtx* RM_LJ_GetEvalContext(void);
]])
local C = ffi.C
local ffi_new, ffi_gc, ffi_string = ffi.new, ffi.gc, ffi.string
local select, type = select, type
local RM_LJ = ffi.load('redis-mod_luajit', true)
local RM_OK = 0
local RM_ERR = 1
local int_1_t = ffi.typeof('int[1]')
local size_1_t = ffi.typeof('size_t[1]')
local ll_1_t = ffi.typeof('long long[1]')
local double_1_t = ffi.typeof('double[1]')
-- RedisModule Lua API
local RM = {
-- Error status return values.
OK = 0,
ERR = 1,
-- API versions.
APIVER_1 = 1,
-- API flags and constants
READ = 1,
WRITE = 2,
LIST_HEAD = 0,
LIST_TAIL = 1,
-- Key types.
KEYTYPE_EMPTY = 0,
KEYTYPE_STRING = 1,
KEYTYPE_LIST = 2,
KEYTYPE_HASH = 3,
KEYTYPE_SET = 4,
KEYTYPE_ZSET = 5,
-- Reply types.
REPLY_UNKNOWN = -1,
REPLY_STRING = 0,
REPLY_ERROR = 1,
REPLY_INTEGER = 2,
REPLY_ARRAY = 3,
REPLY_NULL = 4,
-- Postponed array length.
POSTPONED_ARRAY_LEN = -1,
-- Expire
NO_EXPIRE = -1,
-- Sorted set API flags.
ZADD_XX = 1,
ZADD_NX = 2,
ZADD_ADDED = 4,
ZADD_UPDATED = 8,
ZADD_NOP = 16,
-- Hash API flags.
HASH_NONE = 0,
HASH_NX = 2,
HASH_XX = 4,
HASH_CFIELDS = 8,
HASH_EXISTS = 16,
-- A special pointer that we can use between the core and the module to signal
-- field deletion, and that is impossible to be a valid pointer.
HASH_DELETE = 1, -- ((RedisModuleString*)(long)1)
-- Error messages.
ERRORMSG_WRONGTYPE = "WRONGTYPE Operation against a key holding the wrong kind of value",
POSITIVE_INFINITE = (1.0/0.0),
NEGATIVE_INFINITE = (-1.0/0.0),
}
--- RedisModule Ctx class
RM.Ctx = ffi.metatype( 'RedisModuleCtx', {
-- methods
__index = {
CreateCommand = function(ctx, name, cmdfunc, strflags, firstkey, lastkey, keystep)
return C.RM_CreateCommand(ctx, name, cmdfunc, strflags, firstkey, lastkey, keystep)
end,
WrongArity = function(ctx)
return C.RM_WrongArity(ctx)
end,
ReplyWithLongLong = function(ctx, ll)
return C.RM_ReplyWithLongLong(ctx, ll)
end,
GetSelectedDb = function(ctx)
return C.RM_GetSelectedDb(ctx)
end,
SelectDb = function(ctx, newid)
return C.RM_SelectDb(ctx, newid)
end,
ReplyWithError = function(ctx, err)
return C.RM_ReplyWithError(ctx, err)
end,
ReplyWithSimpleString = function(ctx, msg)
return C.RM_ReplyWithSimpleString(ctx, msg)
end,
ReplyWithArray = function(ctx, len)
return C.RM_ReplyWithArray(ctx, len)
end,
ReplySetArrayLength = function(ctx, len)
return C.RM_ReplySetArrayLength(ctx, len)
end,
ReplyWithStringBuffer = function(ctx, buf, len)
return C.RM_ReplyWithStringBuffer(ctx, buf, len)
end,
ReplyWithString = function(ctx, str)
if type(str) == 'string' then
str = RM.String(str)
end
return C.RM_ReplyWithStringBuffer(ctx, str)
end,
ReplyWithNull = function(ctx)
return C.RM_ReplyWithNull(ctx)
end,
ReplyWithDouble = function(ctx, d)
return C.RM_ReplyWithNull(ctx, d)
end,
ReplyWithCallReply = function(ctx, reply)
return C.RM_ReplyWithCallReply(ctx, reply)
end,
AutoMemory = function(ctx)
C.RM_AutoMemory(ctx)
end,
-- TODO Replicate = function(ctx, cmdname, fmt) -- , ...)
--end,
ReplicateVerbatim = function(ctx)
return C.RM_ReplicateVerbatim(ctx)
end,
IsKeysPositionRequest = function(ctx)
return C.RM_IsKeysPositionRequest(ctx)
end,
KeyAtPos = function(ctx, pos)
C.RM_KeyAtPos(ctx, pos)
end,
GetClientId = function(ctx)
return C.RM_GetClientId(ctx)
end,
Call = function(ctx, cmdname, fmt, ...)
local narg = select("#", ...)
local reply = C.RM_Call(ctx, cmdname, fmt, ...)
return reply
end,
}
})
--- RedisModule Key class
RM.Key = ffi.metatype( 'struct RedisModuleKey', {
-- garbage collection destructor, not invoked by user
__gc = C.RM_CloseKey,
-- methods
__index = {
KeyType = function(key)
return C.RM_KeyType(key)
end,
ValueLength = function(key)
return C.RM_ValueLength(key)
end,
ListPush = function(key, where, elem)
if type(elem) == 'string' then
elem = RM.String(elem)
end
return C.RM_ListPush(key, where, elem)
end,
ListPop = function(key, where)
local elem = C.RM_ListPush(pop, where)
return elem and ffi_gc(elem, C.RM_FreeString) or nil
end,
DeleteKey = function(key)
return C.RM_DeleteKey(key)
end,
StringSet = function(key, str)
return C.RM_StringSet(key, str)
end,
-- returns char*, size_t (ptr, len) for DMA access
-- mode should be RM.READ, RM.WRITE, or RM.READ+RM.WRITE
StringDMA = function(key, mode)
local len_1 = size_1_t()
local ptr = C.RM_StringDMA(key, len_1, mode)
return ptr, len_1[0]
end,
StringTruncate = function(key, newlen)
return C.RM_StringTruncate(key, newlen)
end,
GetExpire = function(key)
return C.RM_GetExpire(key)
end,
SetExpire = function(key, expire)
return C.RM_SetExpire(expire)
end,
-- returns result, outflags
ZsetAdd = function(key, score, elem, flags)
local flags_1 = int_1_t(flags or 0)
local res = C.RM_ZsetAdd(key, score, elem, flags_1)
return res, flags_1[0]
end,
-- returns result, outflags, newscore
ZsetIncrby = function(key, score, elem, flags)
local flags_1 = int_1_t(flags or 0)
local newscore_1 = double_1_t()
local res = C.RM_ZsetIncrby(key, score, elem, flags_1, newscore_1)
return res, flags_1[0], newscore_1[0]
end,
-- returns result, score
ZsetScore = function(key, elem, score)
local score_1 = double_1_t()
local res = C.RM_ZsetIncrby(key, elem, score_1)
return res, score_1[0]
end,
ZsetRem = function(key, elem)
local deleted_1 = int_1_t()
local res = C.RM_ZsetRem(key, elem, deleted_1)
return res, deleted_1[0]
end,
ZsetRangeStop = function(key)
return C.RM_ZsetRangeStop(key)
end,
ZsetFirstInScoreRange = function(key, min, max, minex, maxex)
return C.RM_ZsetFirstInScoreRange(key, min, max, minex, maxex)
end,
ZsetLastInScoreRange = function(key, min, max, minex, maxex)
return C.RM_ZsetLastInScoreRange(key, min, max, minex, maxex)
end,
ZsetFirstInLexRange = function(key, min, max)
return C.RM_ZsetFirstInLexRange(key, min, max)
end,
ZsetLastInLexRange = function(key, min, max)
return C.RM_ZsetLastInLexRange(key, min, max)
end,
-- returns RM.String, score
ZsetRangeCurrentElement = function(key)
local score_1 = double_1_t()
local str = C.RM_ZsetRangeCurrentElement(key, score_1)
return str, score_1[0]
end,
ZsetRangeNext = function(key)
return C.RM_ZsetRangeNext(key)
end,
ZsetRangePrev = function(key)
return C.RM_ZsetRangePrev(key)
end,
ZsetRangeEndReached = function(key)
return C.RM_ZsetRangeEndReached(key)
end,
--HashSet = function(key, int flags, ...)
-- return C.RM_HashSet(key)
--end,
--HashGet = function(key, int flags, ...)
-- return C.RM_HashGet(key)
--end,
}
})
--- RedisModule String class
RM.String = ffi.metatype( 'struct RedisModuleString', {
-- methods
__index = {
-- Returns the string converted into a long long integer.
-- Returns `nil` if the string can't be parsed as a valid, strict long long (no spaces before/after).
ToLongLong = function(str)
local ll_1 = ll_1_t()
local res = C.RM_StringToLongLong(str, ll_1)
return (res == RM_OK) and ll_1[0] or nil
end,
-- Returns the string converted into a Lua number.
-- Returns `nil` if the string is not a valid string representation of a double value.
ToDouble = function(str)
local double_1 = double_1_t()
local res = C.RM_StringToDouble(str, double_1)
return (res == RM_OK) and double_1[0] or nil
end,
-- Returns the const char*, size_t (ptr, len) of the String.
PtrLen = function(str)
local len_1 = size_1_t()
local ptr = C.RM_StringPtrLen(str, len_1)
return ptr, len_1[0]
end,
},
-- Creates a Lua string from this String
__tostring = function(str)
local len_1 = size_1_t()
local ptr = C.RM_StringPtrLen(str, len_1)
return ffi_string(ptr, len_1[0])
end
})
--- RedisModule CallReply class
RM.CallReply = ffi.metatype( 'struct RedisModuleCallReply', {
-- methods
__index = {
Index = function(reply, idx)
return C.RM_CallReplyArrayElement(reply, idx)
end,
ArrayElement = function(reply, idx)
return C.RM_CallReplyArrayElement(reply, idx)
end,
Length = function(reply)
return C.RM_CallReplyLength(reply)
end,
Type = function(reply)
return C.RM_CallReplyType(reply)
end,
Integer = function(reply)
return C.RM_CallReplyInteger(reply)
end,
-- returns const char*, size_t of this CallReply
Proto = function(reply)
local len_1 = size_1_t()
local ptr = C.RM_CallReplyProto(reply, len_1)
return ptr, len_1[0]
end,
-- returns a RM.String from this CallReply
String = function(reply)
return C.RM_CreateStringFromCallReply(reply)
end,
}
})
-- Gets the current RM.Ctx
RM.EvalCtx = function()
return RM_LJ.RM_LJ_GetEvalContext()
end
-- Return RedisModule Lua API
return RM