From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id CE7C123B99 for ; Sat, 5 May 2018 08:47:37 -0400 (EDT) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id t_B-r9GNsYfh for ; Sat, 5 May 2018 08:47:37 -0400 (EDT) Received: from smtp62.i.mail.ru (smtp62.i.mail.ru [217.69.128.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id 10DA823B97 for ; Sat, 5 May 2018 08:47:36 -0400 (EDT) Subject: [tarantool-patches] Re: [PATCH v2 3/3] schema: expose space_mt and index_mt on box.schema table References: <20180402112843.k5szaedwomi4gtl3@esperanza> <8053011f0e2036fdb811dd7ad6097ccc150b76d0.1522774214.git.v.shpilevoy@tarantool.org> From: Vladislav Shpilevoy Message-ID: Date: Sat, 5 May 2018 15:47:34 +0300 MIME-Version: 1.0 In-Reply-To: <8053011f0e2036fdb811dd7ad6097ccc150b76d0.1522774214.git.v.shpilevoy@tarantool.org> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: tarantool-patches-bounce@freelists.org Errors-to: tarantool-patches-bounce@freelists.org Reply-To: tarantool-patches@freelists.org List-help: List-unsubscribe: List-software: Ecartis version 1.0.0 List-Id: tarantool-patches List-subscribe: List-owner: List-post: List-archive: To: tarantool-patches@freelists.org Cc: Konstantin Osipov Users requested to be able to extend spaces individually. The patch was reworked to allow this. On 03/04/2018 19:50, Vladislav Shpilevoy wrote: > 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 > schema: expose space_mt and index_mt on box.schema table 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. On the other hand local space/index metatables still can be extended individually to save compatibility with existing modules. Routinely space/index metatable is just a proxy for a global mt. When a user attempts to extend a space or index methods via local space/index metatable instead of from box.schema mt, the local metatable is transformed. Its __index metamethod starts looking up at first in self, and only then into the global mt. Closes #3204 diff --git a/src/box/lua/schema.lua b/src/box/lua/schema.lua index 8a1d23b00..6a3da4897 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 -- @@ -1118,7 +1114,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 +1340,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') @@ -1441,14 +1435,41 @@ end space_mt.frommap = box.internal.space.frommap 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 + +-- +-- Wrap a global space/index metatable into a space/index local +-- one. Routinely this metatable just indexes the global one. When +-- a user attempts to extend a space or index methods via local +-- space/index metatable instead of from box.schema mt, the local +-- metatable is transformed. Its __index metamethod starts looking +-- up at first in self, and only then into the global mt. +-- +local function wrap_schema_object_mt(name) + local mt = { __index = box.schema[name] } + local mt_mt = {} + mt_mt.__newindex = function(t, k, v) + mt_mt.__newindex = nil + mt.__index = function(t, k) + return mt[k] or box.schema[name][k] + end + rawset(mt, k, v) + end + setmetatable(mt, mt_mt) + return mt +end + 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 = wrap_schema_object_mt('vinyl_index_mt') else - index_mt = table.deepcopy(memtx_index_mt) + index_mt = wrap_schema_object_mt('memtx_index_mt') end + local space_mt = wrap_schema_object_mt('space_mt') setmetatable(space, space_mt) if type(space.index) == 'table' and space.enabled then diff --git a/test/box-tap/schema_mt.test.lua b/test/box-tap/schema_mt.test.lua new file mode 100755 index 000000000..5a635262f --- /dev/null +++ b/test/box-tap/schema_mt.test.lua @@ -0,0 +1,106 @@ +#!/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(26) + +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).__index, getmetatable(sp2).__index, 'spaces share metatables __index') + +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).__index, getmetatable(sp1_sk).__index, 'memtx indexes share metatables __index') +test:is(getmetatable(sp2_pk).__index, getmetatable(sp2_sk).__index, 'vinyl indexes share metatables __index') +test:isnt(getmetatable(sp1_pk).__index, getmetatable(sp2_pk).__index, 'engines do not share metatables __index') + +-- +-- 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') + +-- +-- Test space/index-local methods. +-- A space local metatable can extended so it does not affect +-- other spaces. Same about index. +-- +sp3 = box.schema.create_space('test3', {engine = 'memtx'}) +sp3_pk = sp3:create_index('pk') +sp3_sk = sp3:create_index('sk') +mt1 = getmetatable(sp1) +mt2 = getmetatable(sp2) +test:isnt(mt1, mt2, 'spaces do not share metatables') +index_mt1 = getmetatable(sp3_pk) +index_mt2 = getmetatable(sp3_sk) +test:isnt(index_mt1, index_mt2, 'indexes do not share metatables') + +mt1.my_func = function(a) return a end +test:isnil(mt2.my_func, 'extend local space metatable') +test:is(sp1.my_func(100), 100, 'extend local space metatable') +test:isnil(sp2.my_func, 'extend local space metatable') + +index_mt1.my_func = function(a) return a + 100 end +test:isnil(index_mt2.my_func, 'extend local index metatable') +test:is(sp3_pk.my_func(100), 200, 'extend local index metatable') +test:isnil(sp3_sk.my_func, 'extend local index metatable') + +sp1:drop() +sp2:drop() +sp3:drop() + +test:check() + +os.exit(0)