-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This patch add `crud.schema` introspection handle, which allows to inspect which spaces are available and what format do they have. The feature covers two use cases: - when application users want to know what data they can manipulate, - when crud integration tools want to know data format for pre-processing (for example, CRUD HTTP API).
- Loading branch information
1 parent
6b20c8c
commit 10e2c80
Showing
11 changed files
with
464 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
local checks = require('checks') | ||
local errors = require('errors') | ||
|
||
local SchemaError = errors.new_class('SchemaError', {capture_stack = false}) | ||
|
||
local schema_module = require('crud.common.schema') | ||
local utils = require('crud.common.utils') | ||
|
||
local schema = {} | ||
|
||
local system_spaces = { | ||
-- https://github.com/tarantool/tarantool/blob/3240201a2f5bac3bddf8a74015db9b351954e0b5/src/box/schema_def.h#L77-L127 | ||
['_vinyl_deferred_delete'] = true, | ||
['_schema'] = true, | ||
['_collation'] = true, | ||
['_vcollation'] = true, | ||
['_space'] = true, | ||
['_vspace'] = true, | ||
['_sequence'] = true, | ||
['_sequence_data'] = true, | ||
['_vsequence'] = true, | ||
['_index'] = true, | ||
['_vindex'] = true, | ||
['_func'] = true, | ||
['_vfunc'] = true, | ||
['_user'] = true, | ||
['_vuser'] = true, | ||
['_priv'] = true, | ||
['_vpriv'] = true, | ||
['_cluster'] = true, | ||
['_trigger'] = true, | ||
['_truncate'] = true, | ||
['_space_sequence'] = true, | ||
['_vspace_sequence'] = true, | ||
['_fk_constraint'] = true, | ||
['_ck_constraint'] = true, | ||
['_func_index'] = true, | ||
['_session_settings'] = true, | ||
-- https://github.com/tarantool/vshard/blob/b3c27b32637863e9a03503e641bb7c8c69779a00/vshard/storage/init.lua#L752 | ||
['_bucket'] = true, | ||
-- https://github.com/tarantool/ddl/blob/b55d0ff7409f32e4d527e2d25444d883bce4163b/test/set_sharding_metadata_test.lua#L92-L98 | ||
['_ddl_sharding_key'] = true, | ||
['_ddl_sharding_func'] = true, | ||
} | ||
|
||
local function get_crud_schema(space) | ||
local sch = schema_module.get_normalized_space_schema(space) | ||
|
||
-- bucket_id is not nullable for a storage, yet | ||
-- it is optional for a crud user. | ||
for _, v in ipairs(sch.format) do | ||
if v.name == 'bucket_id' then | ||
v.is_nullable = true | ||
end | ||
end | ||
|
||
for id, v in pairs(sch.indexes) do | ||
-- There is no reason for a user to know about | ||
-- bucket_id index. | ||
if v.name == 'bucket_id' then | ||
sch.indexes[id] = nil | ||
end | ||
end | ||
|
||
return sch | ||
end | ||
|
||
schema.call = function(space_name, opts) | ||
checks('?string', { | ||
vshard_router = '?string|table', | ||
timeout = '?number', | ||
}) | ||
|
||
opts = opts or {} | ||
|
||
local vshard_router, err = utils.get_vshard_router_instance(opts.vshard_router) | ||
if err ~= nil then | ||
return nil, SchemaError:new(err) | ||
end | ||
|
||
local _, err = schema_module.reload_schema(vshard_router) | ||
if err ~= nil then | ||
return nil, SchemaError:new(err) | ||
end | ||
|
||
local spaces, err = utils.get_spaces(vshard_router, opts.timeout) | ||
if err ~= nil then | ||
return nil, SchemaError:new(err) | ||
end | ||
|
||
if space_name ~= nil then | ||
local space = spaces[space_name] | ||
if space == nil then | ||
return nil, SchemaError:new("Space %q doesn't exist", space_name) | ||
end | ||
return get_crud_schema(space) | ||
else | ||
local resp = {} | ||
|
||
for name, space in pairs(spaces) do | ||
-- Can be indexed by space id and space name, | ||
-- so we need to be careful with duplicates. | ||
if type(name) == 'string' and system_spaces[name] == nil then | ||
resp[name] = get_crud_schema(space) | ||
end | ||
end | ||
|
||
return resp | ||
end | ||
end | ||
|
||
return schema |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#!/usr/bin/env tarantool | ||
|
||
require('strict').on() | ||
_G.is_initialized = function() return false end | ||
|
||
local log = require('log') | ||
local errors = require('errors') | ||
local cartridge = require('cartridge') | ||
|
||
if package.setsearchroot ~= nil then | ||
package.setsearchroot() | ||
else | ||
package.path = package.path .. debug.sourcedir() .. "/?.lua;" | ||
end | ||
|
||
package.preload['customers-storage'] = function() | ||
return { | ||
role_name = 'customers-storage', | ||
init = require('storage_init') | ||
} | ||
end | ||
|
||
local ok, err = errors.pcall('CartridgeCfgError', cartridge.cfg, { | ||
advertise_uri = 'localhost:3301', | ||
http_port = 8081, | ||
bucket_count = 3000, | ||
roles = { | ||
'cartridge.roles.crud-router', | ||
'cartridge.roles.crud-storage', | ||
'customers-storage', | ||
}} | ||
) | ||
|
||
if not ok then | ||
log.error('%s', err) | ||
os.exit(1) | ||
end | ||
|
||
_G.is_initialized = cartridge.is_healthy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
return function() | ||
if box.info.ro == true then | ||
return | ||
end | ||
|
||
local engine = os.getenv('ENGINE') or 'memtx' | ||
|
||
local customers_space = box.schema.space.create('customers', { | ||
format = { | ||
{name = 'id', type = 'unsigned'}, | ||
{name = 'bucket_id', type = 'unsigned'}, | ||
{name = 'name', type = 'string'}, | ||
{name = 'age', type = 'number'}, | ||
}, | ||
if_not_exists = true, | ||
engine = engine, | ||
}) | ||
customers_space:create_index('id', { | ||
parts = { {field = 'id'} }, | ||
if_not_exists = true, | ||
}) | ||
customers_space:create_index('bucket_id', { | ||
parts = { {field = 'bucket_id'} }, | ||
unique = false, | ||
if_not_exists = true, | ||
}) | ||
|
||
local shops_space = box.schema.space.create('shops', { | ||
format = { | ||
{name = 'registry_id', type = 'unsigned'}, | ||
{name = 'bucket_id', type = 'unsigned'}, | ||
{name = 'name', type = 'string'}, | ||
{name = 'address', type = 'string'}, | ||
{name = 'owner', type = 'string', is_nullable = true}, | ||
}, | ||
if_not_exists = true, | ||
engine = engine, | ||
}) | ||
shops_space:create_index('registry', { | ||
parts = { {field = 'registry_id'} }, | ||
if_not_exists = true, | ||
}) | ||
shops_space:create_index('bucket_id', { | ||
parts = { {field = 'bucket_id'} }, | ||
unique = false, | ||
if_not_exists = true, | ||
}) | ||
shops_space:create_index('address', { | ||
parts = { {field = 'address'} }, | ||
unique = true, | ||
if_not_exists = true, | ||
}) | ||
end |
Oops, something went wrong.