Getting Started With Lua UDFs
Currently, User-Defined Functions are written in Lua. Lua is a powerful, fast, lightweight, embeddable scripting language. To learn more about Lua, see About Lua.
If this is your first foray into Lua programming, then we suggest the following material:
- Programming in Lua, Second Edition (Lua 5.1)
- Lua 5.1 Reference Manual
- Lua Tutorials
- Lua Unofficial FAQ (uFAQ)
Lua Version
Aerospike currently supports Lua 5.1.4.
Lua Restrictions
Aerospike supports the full Lua programming language, with a few exceptions.
Globals are restricted
- Global variables are not allowed.
- Global functions can only be called by Aerospike Server, and cannot be called by other Lua functions.
- To call a custom Lua function from another Lua function, the called function must be declared as a "local" function. For example, external function
sum()
can call local functionadd()
, provided it is defined as local.
local function add(a,b)
return a + b
end
function sum(a,b)
return add(a,b)
end
Restricted modules and functions
- coroutines - Lua functions can call each other provided they are "forward declared" before their actual use. In this example, we forward-declare
fun_B()
and then we can use it inside the body offun_B()
. We don't have to forward-declarefun_A()
in this example because it is declared first.
local fun_B
local fun_A( foo )
fun_B( foo.bar )
end
local fun_B( bar )
fun_A( bar.foo )
end
- debug module – Not enabled due to not being able to support the debugging features in Lua.
os.exit()
– Not enabled because it didn't make sense for a Lua script to cause the database to process to exit.