[PATCH v2 3/3] schema: expose space_mt and index_mt on box.schema table

Vladislav Shpilevoy v.shpilevoy at tarantool.org
Tue Apr 3 19:50:47 MSK 2018


This commit allows userland to extend the space and index
metatables with their own functions or even metamethods. Reducing
barriers for this kind of experimentation is vital for user
contribution toward the improvement of Tarantool's API.

There are 4 metatables available for extending:
box.schema.space_mt - metatable of all spaces;
box.schema.index_mt - base metatable of all indexes - replicated
                      into the vinyl and memtx. See below how.
box.schema.vinyl_index_mt - metatable of all vinyl indexes;
box.schema.memtx_index_mt - metatable of all memtx indexes.

Closes #3204
---
 src/box/lua/schema.lua          | 17 +++++----
 test/box-tap/schema_mt.test.lua | 80 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 88 insertions(+), 9 deletions(-)
 create mode 100755 test/box-tap/schema_mt.test.lua

diff --git a/src/box/lua/schema.lua b/src/box/lua/schema.lua
index 4b5eeeda9..5d5b0dc49 100644
--- a/src/box/lua/schema.lua
+++ b/src/box/lua/schema.lua
@@ -1074,10 +1074,6 @@ end
 
 internal.check_iterator_type = check_iterator_type -- export for net.box
 
-local function forbid_new_index(t, k, v)
-    return error('Attempt to modify a read-only table')
-end
-
 local base_index_mt = {}
 base_index_mt.__index = base_index_mt
 --
@@ -1098,6 +1094,7 @@ setmetatable(base_index_mt, {
         rawset(t, k, v)
     end
 })
+
 -- __len and __index
 base_index_mt.len = function(index)
     check_index_arg(index, 'len')
@@ -1118,7 +1115,6 @@ base_index_mt.bsize = function(index)
 end
 -- Lua 5.2 compatibility
 base_index_mt.__len = base_index_mt.len
-base_index_mt.__newindex = forbid_new_index
 -- min and max
 base_index_mt.min_ffi = function(index, key)
     check_index_arg(index, 'min')
@@ -1345,7 +1341,6 @@ space_mt.bsize = function(space)
     end
     return builtin.space_bsize(s)
 end
-space_mt.__newindex = forbid_new_index
 
 space_mt.get = function(space, key)
     check_space_arg(space, 'get')
@@ -1440,13 +1435,17 @@ space_mt.run_triggers = function(space, yesno)
 end
 space_mt.__index = space_mt
 
+box.schema.index_mt = base_index_mt
+box.schema.memtx_index_mt = memtx_index_mt
+box.schema.vinyl_index_mt = vinyl_index_mt
+box.schema.space_mt = space_mt
+
 function box.schema.space.bless(space)
-    local space_mt = table.deepcopy(space_mt)
     local index_mt
     if space.engine == 'vinyl' then
-        index_mt = table.deepcopy(vinyl_index_mt)
+        index_mt = vinyl_index_mt
     else
-        index_mt = table.deepcopy(memtx_index_mt)
+        index_mt = memtx_index_mt
     end
 
     setmetatable(space, space_mt)
diff --git a/test/box-tap/schema_mt.test.lua b/test/box-tap/schema_mt.test.lua
new file mode 100755
index 000000000..b785b8e21
--- /dev/null
+++ b/test/box-tap/schema_mt.test.lua
@@ -0,0 +1,80 @@
+#!/usr/bin/env tarantool
+--
+-- pr-3204: expose space_mt, index_mt into box.schema.
+--
+
+local tap = require('tap')
+local test = tap.test('schema_mt')
+
+test:plan(18)
+
+box.cfg{
+  log="tarantool.log",
+}
+
+--
+-- Check that space metatable is shared between all spaces,
+-- regardless of engine.
+--
+local sp1 = box.schema.create_space('test1', {engine = 'memtx'})
+local sp2 = box.schema.create_space('test2', {engine = 'vinyl'})
+test:is(getmetatable(sp1), getmetatable(sp2), 'spaces share metatables')
+
+function box.schema.space_mt.myfunc(space, args)
+  return args
+end
+test:is(sp1:myfunc(123), 123, 'space_mt can be extended')
+test:is(sp1.myfunc, sp2.myfunc, 'space_mt can be extended')
+
+--
+-- Check that index metatable is shared in a scope of an engine.
+--
+local sp1_pk = sp1:create_index('pk')
+local sp1_sk = sp1:create_index('sk')
+local sp2_pk = sp2:create_index('pk')
+local sp2_sk = sp2:create_index('sk')
+test:is(getmetatable(sp1_pk), getmetatable(sp1_sk), 'memtx indexes share metatables')
+test:is(getmetatable(sp2_pk), getmetatable(sp2_sk), 'vinyl indexes share metatables')
+test:isnt(getmetatable(sp1_pk), getmetatable(sp2_pk), 'engines do not share metatables')
+
+--
+-- Check that there are two ways to extend index metatable:
+-- extend base index metatable, or extend engine specific.
+--
+function box.schema.index_mt.common_func(index, args)
+  return args
+end
+function box.schema.vinyl_index_mt.vinyl_func(index, args)
+  return args
+end
+function box.schema.memtx_index_mt.memtx_func(index, args)
+  return args
+end
+test:is(box.schema.index_mt.common_func, box.schema.vinyl_index_mt.common_func,
+        'base index_mt is replicated into vinyl index_mt')
+test:is(box.schema.index_mt.common_func, box.schema.memtx_index_mt.common_func,
+        'base index_mt is replicated into memtx index_mt')
+test:is(box.schema.index_mt.vinyl_func, nil, 'vinyl index_mt is not replicated')
+test:is(box.schema.index_mt.memtx_func, nil, 'memtx index_mt is not replicated')
+
+test:is(sp1_pk.common_func, box.schema.index_mt.common_func,
+        'new common methods are visible in memtx index')
+test:is(sp2_pk.common_func, box.schema.index_mt.common_func,
+        'new common methods are visible in vinyl index')
+
+test:is(sp1_pk.memtx_func, box.schema.memtx_index_mt.memtx_func,
+        'new memtx methods are visible in memtx index')
+test:is(sp2_pk.vinyl_func, box.schema.vinyl_index_mt.vinyl_func,
+        'new vinyl methods are visible in vinyl index')
+
+test:is(sp1_pk:memtx_func(100), 100, 'memtx local methods work')
+test:is(sp1_sk:common_func(200), 200, 'memtx common methods work')
+test:is(sp2_pk:vinyl_func(300), 300, 'vinyl local methods work')
+test:is(sp2_sk:common_func(400), 400, 'vinyl common methods work')
+
+sp1:drop()
+sp2:drop()
+
+test:check()
+
+os.exit(0)
-- 
2.14.3 (Apple Git-98)




More information about the Tarantool-patches mailing list