[PATCH 2/2] schema: expose space_mt and index_mt on `box.schema` table
Vladimir Davydov
vdavydov.dev at gmail.com
Mon Apr 2 14:28:43 MSK 2018
On Sun, Apr 01, 2018 at 02:02:46PM +0300, Vladislav Shpilevoy wrote:
> function box.schema.space.bless(space)
> - local index_mt = table.deepcopy(index_mt)
> - local space_mt = table.deepcopy(space_mt)
> + -- At first, reference all global index functions. At second,
> + -- choose an implementation for read operations. They are not
> + -- in a global index_mt, because they are engine specific.
> + -- All common index_mt functions must be referenced here to
> + -- be able to get them using getmetatable(index_object) - if
> + -- they are only in index_mt, then getmetatable() returns only
> + -- read ops.
> + local local_index_mt = table.deepcopy(index_mt)
> + local_index_mt.__index = function(index, key)
> + return local_index_mt[key] or index_mt[key]
> + end
I don't really like that you use the same metatable for all spaces, but
a separate metatable for each index. Besides, because of this one can't
overwrite a built-in method (e.g. 'get') as it will be masked by
local_index_mt. AFAIU you do this, because vinyl and memtx use different
methods (luac vs ffi) for certain operations.
I see the following alternatives:
1. Leave as is. This isn't all so bad. After all, I doubt anybody's
actually willing to overwrite built-in methods.
2. Use two metatables, one for all memtx indexes, another for vinyl,
and push all changes from the public index_mt to both of these
metatables. Not quite sure it's technically possible in Lua.
3. Same as #2, but proscribe modification of built-in methods.
4. Add a separate engine-specific metatable for the method that differ
between memtx and vinyl, then define __index as:
local engine_index_mt = (engine == 'memtx' and
memtx_index_mt or vinyl_index_mt)
index_mt.__index = function(index, key)
return index_mt[key] or engine_index_mt[key]
Personally, I like #4 most, but it may affect performance.
Anyway, I guess this is up to Kostja to decide which one to choose.
I don't see any other technical problems in the patch, nor in the test.
> -- true if reading operations may yield
> local read_yields = space.engine == 'vinyl'
> local read_ops = {'select', 'get', 'min', 'max', 'count', 'random', 'pairs'}
> for _, op in ipairs(read_ops) do
> if read_yields then
> -- use Lua/C implmenetation
> - index_mt[op] = index_mt[op .. "_luac"]
> + local_index_mt[op] = index_mt[op .. "_luac"]
> else
> -- use FFI implementation
> - index_mt[op] = index_mt[op .. "_ffi"]
> + local_index_mt[op] = index_mt[op .. "_ffi"]
> end
> end
> index_mt.__pairs = index_mt.pairs -- Lua 5.2 compatibility
> @@ -1430,7 +1441,7 @@ function box.schema.space.bless(space)
> if type(space.index) == 'table' and space.enabled then
> for j, index in pairs(space.index) do
> if type(j) == 'number' then
> - setmetatable(index, index_mt)
> + setmetatable(index, local_index_mt)
> end
> end
> end
More information about the Tarantool-patches
mailing list