nrk / redis-lua

A Lua client library for the redis key value storage system.

This URL has Read+Write access

redis-lua / redis.lua
100644 433 lines (358 sloc) 12.798 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
421
422
423
424
425
426
427
428
429
430
431
432
433
module('Redis', package.seeall)
 
local socket = require('socket') -- requires LuaSocket as a dependency
local uri = require('socket.url')
 
local redis_commands = {}
local network, request, response = {}, {}, {}, {}
 
local defaults = { host = '127.0.0.1', port = 6379 }
local protocol = { newline = '\r\n', ok = 'OK', err = 'ERR', null = 'nil' }
 
local function toboolean(value) return value == 1 end
 
local function fire_and_forget(client, command)
    -- let's fire and forget! the connection is closed as soon
    -- as the SHUTDOWN command is received by the server.
    network.write(client, command .. protocol.newline)
end
 
local function load_methods(proto, methods)
    local redis = setmetatable ({}, getmetatable(proto))
    for i, v in pairs(proto) do redis[i] = v end
 
    for i, v in pairs(methods) do redis[i] = v end
    return redis
end
 
-- ############################################################################
 
function network.write(client, buffer)
    local _, err = client.socket:send(buffer)
    if err then error(err) end
end
 
function network.read(client, len)
    if len == nil then len = '*l' end
    local line, err = client.socket:receive(len)
    if not err then return line else error('Connection error: ' .. err) end
end
 
-- ############################################################################
 
function response.read(client)
    local res = network.read(client)
    local prefix = res:sub(1, -#res)
    local response_handler = protocol.prefixes[prefix]
 
    if not response_handler then
        error("Unknown response prefix: " .. prefix)
    else
        return response_handler(client, res)
    end
end
 
function response.status(client, data)
    local sub = data:sub(2)
    if sub == protocol.ok then return true else return sub end
end
 
function response.error(client, data)
    local err_line = data:sub(2)
 
    if err_line:sub(1, 3) == protocol.err then
        error("Redis error: " .. err_line:sub(5))
    else
        error("Redis error: " .. err_line)
    end
end
 
function response.bulk(client, data)
    local str = data:sub(2)
    local len = tonumber(str)
 
    if not len then
        error('Cannot parse ' .. str .. ' as data length.')
    else
        if len == -1 then return nil end
        local next_chunk = network.read(client, len + 2)
        return next_chunk:sub(1, -3);
    end
end
 
function response.multibulk(client, data)
    local str = data:sub(2)
 
    -- TODO: add a check if the returned value is indeed a number
    local list_count = tonumber(str)
 
    if list_count == -1 then
        return nil
    else
        local list = {}
 
        if list_count > 0 then
            for i = 1, list_count do
                table.insert(list, i, response.bulk(client, network.read(client)))
            end
        end
 
        return list
    end
end
 
function response.integer(client, data)
    local res = data:sub(2)
    local number = tonumber(res)
 
    if not number then
        if res == protocol.null then
            return nil
        else
            error('Cannot parse ' .. res .. ' as numeric response.')
        end
    end
 
    return number
end
 
protocol.prefixes = {
    ['+'] = response.status,
    ['-'] = response.error,
    ['$'] = response.bulk,
    ['*'] = response.multibulk,
    [':'] = response.integer,
}
 
-- ############################################################################
 
function request.raw(client, buffer)
    -- TODO: optimize
    local bufferType = type(buffer)
 
    if bufferType == 'string' then
        network.write(client, buffer)
    elseif bufferType == 'table' then
        network.write(client, table.concat(buffer))
    else
        error('Argument error: ' .. bufferType)
    end
 
    return response.read(client)
end
 
function request.inline(client, command, ...)
    if arg.n == 0 then
        network.write(client, command .. protocol.newline)
    else
        local arguments = arg
        arguments.n = nil
 
        if #arguments > 0 then
            arguments = table.concat(arguments, ' ')
        else
            arguments = ''
        end
 
        network.write(client, command .. ' ' .. arguments .. protocol.newline)
    end
 
    return response.read(client)
end
 
function request.bulk(client, command, ...)
    local arguments = arg
    local data = tostring(table.remove(arguments))
    arguments.n = nil
 
    -- TODO: optimize
    if #arguments > 0 then
        arguments = table.concat(arguments, ' ')
    else
        arguments = ''
    end
 
    return request.raw(client, {
        command, ' ', arguments, ' ', #data, protocol.newline, data, protocol.newline
    })
end
 
function request.multibulk(client, command, ...)
    local buffer = { }
    local arguments = { }
    local args_len = 1
 
    if arg.n == 1 and type(arg[1]) == 'table' then
        for k, v in pairs(arg[1]) do
            table.insert(arguments, k)
            table.insert(arguments, v)
            args_len = args_len + 2
        end
    else
        arguments = arg
        args_len = args_len + arg.n
        arguments.n = nil
    end
 
    table.insert(buffer, '*' .. tostring(args_len) .. protocol.newline)
    table.insert(buffer, '$' .. #command .. protocol.newline .. command .. protocol.newline)
 
    for _, argument in pairs(arguments) do
        s_argument = tostring(argument)
        table.insert(buffer, '$' .. #s_argument .. protocol.newline .. s_argument .. protocol.newline)
    end
 
    return request.raw(client, buffer)
end
 
-- ############################################################################
 
local function custom(command, send, parse)
    return function(self, ...)
        local reply = send(self, command, ...)
        if parse then
            return parse(reply, command, ...)
        else
            return reply
        end
    end
end
 
local function bulk(command, reader)
    return custom(command, request.bulk, reader)
end
 
local function multibulk(command, reader)
    return custom(command, request.multibulk, reader)
end
 
local function inline(command, reader)
    return custom(command, request.inline, reader)
end
 
-- ############################################################################
 
function connect(...)
    local host, port = defaults.host, defaults.port
 
    if arg.n == 1 then
        local server = uri.parse(arg[1])
        if server.scheme then
            if server.scheme ~= 'redis' then
                error('"' .. server.scheme .. '" is an invalid scheme')
            end
            host, port = server.host, server.port or defaults.port
        else
            host, port = server.path, defaults.port
        end
    elseif arg.n > 1 then
        host, port = arg[1], arg[2]
    end
 
    if host == nil then
        error('please specify the address of running redis instance')
    end
 
    local client_socket = socket.connect(host, tonumber(port))
    if not client_socket then
        error('Could not connect to ' .. host .. ':' .. port)
    end
 
    local redis_client = {
        socket = client_socket,
        raw_cmd = function(self, buffer)
            return request.raw(self, buffer .. protocol.newline)
        end,
    }
 
    return load_methods(redis_client, redis_commands)
end
 
-- ############################################################################
 
redis_commands = {
    -- miscellaneous commands
    ping = inline('PING',
        function(response)
            if response == 'PONG' then return true else return false end
        end
    ),
    echo = bulk('ECHO'),
    -- TODO: the server returns an empty -ERR on authentication failure
    auth = inline('AUTH'),
 
    -- connection handling
    quit = custom('QUIT', fire_and_forget),
 
    -- commands operating on string values
    set = bulk('SET'),
    set_preserve = bulk('SETNX', toboolean),
    set_multiple = multibulk('MSET'),
    set_multiple_preserve = multibulk('MSETNX', toboolean),
    get = inline('GET'),
    get_multiple = inline('MGET'),
    get_set = bulk('GETSET'),
    increment = inline('INCR'),
    increment_by = inline('INCRBY'),
    decrement = inline('DECR'),
    decrement_by = inline('DECRBY'),
    exists = inline('EXISTS', toboolean),
    delete = inline('DEL', toboolean),
    type = inline('TYPE'),
 
    -- commands operating on the key space
    keys = inline('KEYS',
        function(response)
            local keys = {}
            response:gsub('[^%s]+', function(key)
                table.insert(keys, key)
            end)
            return keys
        end
    ),
    random_key = inline('RANDOMKEY'),
    rename = inline('RENAME'),
    rename_preserve = inline('RENAMENX'),
    expire = inline('EXPIRE', toboolean),
    expire_at = inline('EXPIREAT', toboolean),
    database_size = inline('DBSIZE'),
    time_to_live = inline('TTL'),
 
    -- commands operating on lists
    push_tail = bulk('RPUSH'),
    push_head = bulk('LPUSH'),
    list_length = inline('LLEN'),
    list_range = inline('LRANGE'),
    list_trim = inline('LTRIM'),
    list_index = inline('LINDEX'),
    list_set = bulk('LSET'),
    list_remove = bulk('LREM'),
    pop_first = inline('LPOP'),
    pop_last = inline('RPOP'),
 
    -- commands operating on sets
    set_add = bulk('SADD'),
    set_remove = bulk('SREM'),
    set_pop = inline('SPOP'),
    set_move = bulk('SMOVE'),
    set_cardinality = inline('SCARD'),
    set_is_member = bulk('SISMEMBER'),
    set_intersection = inline('SINTER'),
    set_intersection_store = inline('SINTERSTORE'),
    set_union = inline('SUNION'),
    set_union_store = inline('SUNIONSTORE'),
    set_diff = inline('SDIFF'),
    set_diff_store = inline('SDIFFSTORE'),
    set_members = inline('SMEMBERS'),
    set_random_member = inline('SRANDMEMBER'),
 
    -- commands operating on sorted sets
    zset_add = bulk('ZADD'),
    zset_remove = bulk('ZREM'),
    zset_range = inline('ZRANGE'),
    zset_range_by_score = inline('ZRANGEBYSCORE'),
    zset_reverse_range = inline('ZREVRANGE'),
    zset_cardinality = inline('ZCARD'),
    zset_score = bulk('ZSCORE'),
    zset_remove_range_by_score = inline('ZREMRANGEBYSCORE'),
 
    -- multiple databases handling commands
    select_database = inline('SELECT'),
    move_key = inline('MOVE'),
    flush_database = inline('FLUSHDB'),
    flush_databases = inline('FLUSHALL'),
 
    -- sorting
    --[[ params = {
by = 'weight_*',
get = 'object_*',
limit = { 0, 10 },
sort = 'desc',
alpha = true,
}
--]]
    sort = custom('SORT',
        function(client, command, key, params)
            local query = { key }
 
            if params then
                if params.by then
                    table.insert(query, 'BY ' .. params.by)
                end
 
                if type(params.limit) == 'table' then
                    -- TODO: check for lower and upper limits
                    table.insert(query, 'LIMIT ' .. params.limit[1] .. ' ' .. params.limit[2])
                end
 
                if params.get then
                    table.insert(query, 'GET ' .. params.get)
                end
 
                if params.sort then
                    table.insert(query, params.sort)
                end
 
                if params.alpha == true then
                    table.insert(query, 'ALPHA')
                end
 
                if params.store then
                    table.insert(query, 'STORE ' .. params.store)
                end
            end
 
            return request.inline(client, command, table.concat(query, ' '))
        end
    ),
 
    -- persistence control commands
    save = inline('SAVE'),
    background_save = inline('BGSAVE'),
    last_save = inline('LASTSAVE'),
    shutdown = custom('SHUTDOWN', fire_and_forget),
 
    -- remote server control commands
    info = inline('INFO',
        function(response)
            local info = {}
            response:gsub('([^\r\n]*)\r\n', function(kv)
                local k,v = kv:match(('([^:]*):([^:]*)'):rep(1))
                info[k] = v
            end)
            return info
        end
    ),
    slave_of = inline('SLAVEOF'),
    slave_of_no_one = custom('SLAVEOF',
        function(client, command)
            return request.inline(client, command, 'NO ONE')
        end
    ),
}