Unverified Commit 885f6b5c authored by Meir Shpilraien (Spielrein)'s avatar Meir Shpilraien (Spielrein) Committed by GitHub
Browse files

Redis Function Libraries (#10004)

# Redis Function Libraries

This PR implements Redis Functions Libraries as describe on: https://github.com/redis/redis/issues/9906.

Libraries purpose is to provide a better code sharing between functions by allowing to create multiple
functions in a single command. Functions that were created together can safely share code between
each other without worrying about compatibility issues and versioning.

Creating a new library is done using 'FUNCTION LOAD' command (full API is described below)

This PR introduces a new struct called libraryInfo, libraryInfo holds information about a library:
* name - name of the library
* engine - engine used to create the library
* code - library code
* description - library description
* functions - the functions exposed by the library

When Redis gets the `FUNCTION LOAD` command it creates a new empty libraryInfo.
Redis passes the `CODE` to the relevant engine alongside the empty libraryInfo.
As a result, the engine w...
parent 568c2e03
This diff is collapsed.
...@@ -15,16 +15,16 @@ if {$is_eval == 1} { ...@@ -15,16 +15,16 @@ if {$is_eval == 1} {
} }
} else { } else {
proc run_script {args} { proc run_script {args} {
r function create LUA test replace [lindex $args 0] r function load LUA test replace [format "redis.register_function('test', function(KEYS, ARGV)\n %s \nend)" [lindex $args 0]]
r fcall test {*}[lrange $args 1 end] r fcall test {*}[lrange $args 1 end]
} }
proc run_script_ro {args} { proc run_script_ro {args} {
r function create LUA test replace [lindex $args 0] r function load LUA test replace [format "redis.register_function('test', function(KEYS, ARGV)\n %s \nend)" [lindex $args 0]]
r fcall_ro test {*}[lrange $args 1 end] r fcall_ro test {*}[lrange $args 1 end]
} }
proc run_script_on_connection {args} { proc run_script_on_connection {args} {
set rd [lindex $args 0] set rd [lindex $args 0]
$rd function create LUA test replace [lindex $args 1] $rd function load LUA test replace [format "redis.register_function('test', function(KEYS, ARGV)\n %s \nend)" [lindex $args 1]]
# read the ok reply of function create # read the ok reply of function create
$rd read $rd read
$rd fcall test {*}[lrange $args 2 end] $rd fcall test {*}[lrange $args 2 end]
...@@ -37,7 +37,7 @@ if {$is_eval == 1} { ...@@ -37,7 +37,7 @@ if {$is_eval == 1} {
start_server {tags {"scripting"}} { start_server {tags {"scripting"}} {
test {Script - disallow write on OOM} { test {Script - disallow write on OOM} {
r FUNCTION create lua f1 replace { return redis.call('set', 'x', '1') } r FUNCTION load lua f1 replace { redis.register_function('f1', function() return redis.call('set', 'x', '1') end) }
r config set maxmemory 1 r config set maxmemory 1
...@@ -737,7 +737,7 @@ start_server {tags {"scripting"}} { ...@@ -737,7 +737,7 @@ start_server {tags {"scripting"}} {
set buf "*3\r\n\$4\r\neval\r\n\$33\r\nwhile 1 do redis.call('ping') end\r\n\$1\r\n0\r\n" set buf "*3\r\n\$4\r\neval\r\n\$33\r\nwhile 1 do redis.call('ping') end\r\n\$1\r\n0\r\n"
append buf "*1\r\n\$4\r\nping\r\n" append buf "*1\r\n\$4\r\nping\r\n"
} else { } else {
set buf "*6\r\n\$8\r\nfunction\r\n\$6\r\ncreate\r\n\$3\r\nlua\r\n\$4\r\ntest\r\n\$7\r\nreplace\r\n\$33\r\nwhile 1 do redis.call('ping') end\r\n" set buf "*6\r\n\$8\r\nfunction\r\n\$4\r\nload\r\n\$3\r\nlua\r\n\$4\r\ntest\r\n\$7\r\nreplace\r\n\$81\r\nredis.register_function('test', function() while 1 do redis.call('ping') end end)\r\n"
append buf "*3\r\n\$5\r\nfcall\r\n\$4\r\ntest\r\n\$1\r\n0\r\n" append buf "*3\r\n\$5\r\nfcall\r\n\$4\r\ntest\r\n\$1\r\n0\r\n"
append buf "*1\r\n\$4\r\nping\r\n" append buf "*1\r\n\$4\r\nping\r\n"
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment