From 34db89706c3737478293e3f49ee32c2567688126 Mon Sep 17 00:00:00 2001
From: Konstantin Osipov <kostja@tarantool.org>
Date: Fri, 19 Dec 2014 22:02:34 +0300
Subject: [PATCH] Implement space:format(). Set format for system spaces.

---
 extra/schema_fill.lua        |  63 +++++++++++++++++++++++++++--------
 src/box/bootstrap.snap       | Bin 2076 -> 2589 bytes
 src/box/lua/schema.lua       |  19 ++++++++++-
 test/box/access_misc.result  |  26 +++++++++++----
 test/box/alter_limits.result |   4 +--
 test/box/bootstrap.result    |  23 +++++++++----
 test/box/temp_spaces.result  |  14 ++++----
 7 files changed, 112 insertions(+), 37 deletions(-)

diff --git a/extra/schema_fill.lua b/extra/schema_fill.lua
index b884748334..4f1e661966 100644
--- a/extra/schema_fill.lua
+++ b/extra/schema_fill.lua
@@ -20,42 +20,79 @@ _schema:insert{'version', 1, 6}
 --
 -- _schema
 --
-_space:insert{_schema.id, ADMIN, '_schema', 'memtx', 0, ''}
+_space:insert{_schema.id, ADMIN, '_schema', 'memtx', 0, '', {}}
 --
 -- _space
 --
-_space:insert{_space.id, ADMIN, '_space', 'memtx', 0, ''}
+_space:insert{_space.id, ADMIN, '_space', 'memtx', 0, '', {}}
 --
 -- _index
 --
-_space:insert{_index.id, ADMIN, '_index', 'memtx', 0, ''}
+_space:insert{_index.id, ADMIN, '_index', 'memtx', 0, '', {}}
 --
 -- _func
 --
-_space:insert{_func.id, ADMIN, '_func', 'memtx', 0, ''}
+_space:insert{_func.id, ADMIN, '_func', 'memtx', 0, '', {}}
 --
 -- _user
 --
-_space:insert{_user.id, ADMIN, '_user', 'memtx', 0, ''}
+_space:insert{_user.id, ADMIN, '_user', 'memtx', 0, '', {}}
 --
 -- _priv
 --
-_space:insert{_priv.id, ADMIN, '_priv', 'memtx', 0, ''}
+_space:insert{_priv.id, ADMIN, '_priv', 'memtx', 0, '', {}}
 --
 -- _cluster
 --
 _space:insert{_cluster.id, ADMIN, '_cluster', 'memtx', 0, ''}
 
+-- define formats.
+-- stick to the following convention:
+-- prefer user id (owner id) in field #2 (base-1)
+-- prefer object name in field #3 (base-1)
+-- 
+format={}
+format[1] = {type='str', name='key'}
+_schema:format(format)
+format={}
+format[1] = {name='id', type='num'}
+format[2] = {name='owner', type='num'}
+format[3] = {name='name', type='str'}
+format[4] = {name='engine', type='str'}
+format[5] = {name='field_count', type='num'}
+format[6] = {name='flags', type='str'}
+format[7] = {name='format', type='*'}
+_space:format(format)
+format={}
+format[1] = {name='id', type='num'}
+format[2] = {name='owner', type='num'}
+format[3] = {name='name', type='str'}
+format[4] = {name='setuid', type='num'}
+_func:format(format)
+format={}
+format[1] = {name='id', type='num'}
+format[2] = {name='owner', type='num'}
+format[3] = {name='name', type='str'}
+format[4] = {name='type', type='str'}
+format[5] = {name='auth', type='*'}
+_user:format(format)
+format={}
+format[1] = {name='grantor', type='num'}
+format[2] = {name='grantee', type='num'}
+format[3] = {name='object_type', type='str'}
+format[4] = {name='object_id', type='num'}
+format[5] = {name='privilege', type='num'}
+_priv:format(format)
+format = {}
+format[1] = {name='id', type='num'}
+format[2] = {name='uuid', type='str'}
+_cluster:format(format)
 -- define indexes
-_index:insert{_schema.id, 0, 'primary', 'tree', 1, 1, 0, 'str'}
-
 -- stick to the following convention:
--- prefer user id (owner id) in field #1
--- prefer object name in field #2
--- index on owner id is index #1
--- index on object name is index #2
---
+-- index on owner id is index #1 (base-0)
+-- index on object name is index #2 (base-0)
 -- space name is unique
+_index:insert{_schema.id, 0, 'primary', 'tree', 1, 1, 0, 'str'}
 _index:insert{_space.id, 0, 'primary', 'tree', 1, 1, 0, 'num'}
 _index:insert{_space.id, 1, 'owner', 'tree', 0, 1, 1, 'num'}
 _index:insert{_space.id, 2, 'name', 'tree', 1, 1, 2, 'str'}
diff --git a/src/box/bootstrap.snap b/src/box/bootstrap.snap
index 2c0715d7f83f7d0ccf0dffc4361941fe70b46dd6..0ba0e769ecb83b49117c389e1b8454930de62b8e 100644
GIT binary patch
literal 2589
zcmbuBO>7%g5XawJ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6df1b9b413..eb61e5b8e9 100644
--- a/src/box/lua/schema.lua
+++ b/src/box/lua/schema.lua
@@ -189,6 +189,7 @@ box.schema.space.create = function(name, options)
         id = 'number',
         field_count = 'number',
         user = 'string, number',
+        format = 'table'
     }
     local options_defaults = {
         engine = 'memtx',
@@ -222,10 +223,23 @@ box.schema.space.create = function(name, options)
         uid = session.uid()
     end
     local temporary = options.temporary and "temporary" or ""
-    _space:insert{id, uid, name, options.engine, options.field_count, temporary}
+    local format = options.format and options.format or {}
+    _space:insert{id, uid, name, options.engine, options.field_count, temporary, format}
     return box.space[id], "created"
 end
 
+-- space format - the metadata about space fields
+function box.schema.space.format(id, format)
+    _space = box.space._space
+    check_param(id, 'id', 'number')
+    check_param(format, 'format', 'table')
+    if format == nil then
+        return _space:get(id)[7]
+    else
+        _space:update(id, {{'=', 7, format}})
+    end
+end
+
 box.schema.create_space = box.schema.space.create
 
 box.schema.space.drop = function(space_id)
@@ -809,6 +823,9 @@ function box.schema.space.bless(space)
             _index:insert(keys[i])
         end
     end
+    space_mt.format = function(space, format)
+        return box.schema.space.format(space.id, format)
+    end
     space_mt.drop = function(space)
         return box.schema.space.drop(space.id)
     end
diff --git a/test/box/access_misc.result b/test/box/access_misc.result
index 7318cc5abe..52e788ab82 100644
--- a/test/box/access_misc.result
+++ b/test/box/access_misc.result
@@ -253,7 +253,10 @@ box.space._user:select(1)
 ...
 box.space._space:select(280)
 ---
-- - [280, 1, '_space', 'memtx', 0, '']
+- - [280, 1, '_space', 'memtx', 0, '', [{'name': 'id', 'type': 'num'}, {'name': 'owner',
+        'type': 'num'}, {'name': 'name', 'type': 'str'}, {'name': 'engine', 'type': 'str'},
+      {'name': 'field_count', 'type': 'num'}, {'name': 'flags', 'type': 'str'}, {
+        'name': 'format', 'type': '*'}]]
 ...
 us = box.schema.create_space('uniuser_space')
 ---
@@ -577,13 +580,22 @@ box.space._user:select()
 ...
 box.space._space:select()
 ---
-- - [272, 1, '_schema', 'memtx', 0, '']
-  - [280, 1, '_space', 'memtx', 0, '']
+- - [272, 1, '_schema', 'memtx', 0, '', [{'type': 'str', 'name': 'key'}]]
+  - [280, 1, '_space', 'memtx', 0, '', [{'name': 'id', 'type': 'num'}, {'name': 'owner',
+        'type': 'num'}, {'name': 'name', 'type': 'str'}, {'name': 'engine', 'type': 'str'},
+      {'name': 'field_count', 'type': 'num'}, {'name': 'flags', 'type': 'str'}, {
+        'name': 'format', 'type': '*'}]]
   - [288, 1, '_index', 'memtx', 0, '']
-  - [296, 1, '_func', 'memtx', 0, '']
-  - [304, 1, '_user', 'memtx', 0, '']
-  - [312, 1, '_priv', 'memtx', 0, '']
-  - [320, 1, '_cluster', 'memtx', 0, '']
+  - [296, 1, '_func', 'memtx', 0, '', [{'name': 'id', 'type': 'num'}, {'name': 'owner',
+        'type': 'num'}, {'name': 'name', 'type': 'str'}, {'name': 'setuid', 'type': 'num'}]]
+  - [304, 1, '_user', 'memtx', 0, '', [{'name': 'id', 'type': 'num'}, {'name': 'owner',
+        'type': 'num'}, {'name': 'name', 'type': 'str'}, {'name': 'type', 'type': 'str'},
+      {'name': 'auth', 'type': '*'}]]
+  - [312, 1, '_priv', 'memtx', 0, '', [{'name': 'grantor', 'type': 'num'}, {'name': 'grantee',
+        'type': 'num'}, {'name': 'object_type', 'type': 'str'}, {'name': 'object_id',
+        'type': 'num'}, {'name': 'privilege', 'type': 'num'}]]
+  - [320, 1, '_cluster', 'memtx', 0, '', [{'name': 'id', 'type': 'num'}, {'name': 'uuid',
+        'type': 'str'}]]
 ...
 box.space._func:select()
 ---
diff --git a/test/box/alter_limits.result b/test/box/alter_limits.result
index be2884b36c..f89e5d14de 100644
--- a/test/box/alter_limits.result
+++ b/test/box/alter_limits.result
@@ -273,7 +273,7 @@ box.space['_space']:update(s.id, {{"=", FIELD_COUNT + 1, 1}})
 -- remove field_count - ok
 box.space['_space']:update(s.id, {{"=", FIELD_COUNT + 1, 0}})
 ---
-- [512, 1, 'test', 'memtx', 0, '']
+- [512, 1, 'test', 'memtx', 0, '', []]
 ...
 s:select{}
 ---
@@ -294,7 +294,7 @@ s:select{}
 -- set field_count of an empty space
 box.space['_space']:update(s.id, {{"=", FIELD_COUNT + 1, 3}})
 ---
-- [512, 1, 'test', 'memtx', 3, '']
+- [512, 1, 'test', 'memtx', 3, '', []]
 ...
 s:select{}
 ---
diff --git a/test/box/bootstrap.result b/test/box/bootstrap.result
index 60ff3b25bb..8e55683e96 100644
--- a/test/box/bootstrap.result
+++ b/test/box/bootstrap.result
@@ -47,13 +47,22 @@ box.space._cluster:select{}
 ...
 box.space._space:select{}
 ---
-- - [272, 1, '_schema', 'memtx', 0, '']
-  - [280, 1, '_space', 'memtx', 0, '']
-  - [288, 1, '_index', 'memtx', 0, '']
-  - [296, 1, '_func', 'memtx', 0, '']
-  - [304, 1, '_user', 'memtx', 0, '']
-  - [312, 1, '_priv', 'memtx', 0, '']
-  - [320, 1, '_cluster', 'memtx', 0, '']
+- - [272, 1, '_schema', 'memtx', 0, '', [{'type': 'str', 'name': 'key'}]]
+  - [280, 1, '_space', 'memtx', 0, '', [{'name': 'id', 'type': 'num'}, {'name': 'owner',
+        'type': 'num'}, {'name': 'name', 'type': 'str'}, {'name': 'engine', 'type': 'str'},
+      {'name': 'field_count', 'type': 'num'}, {'name': 'flags', 'type': 'str'}, {
+        'name': 'format', 'type': '*'}]]
+  - [288, 1, '_index', 'memtx', 0, '', []]
+  - [296, 1, '_func', 'memtx', 0, '', [{'name': 'id', 'type': 'num'}, {'name': 'owner',
+        'type': 'num'}, {'name': 'name', 'type': 'str'}, {'name': 'setuid', 'type': 'num'}]]
+  - [304, 1, '_user', 'memtx', 0, '', [{'name': 'id', 'type': 'num'}, {'name': 'owner',
+        'type': 'num'}, {'name': 'name', 'type': 'str'}, {'name': 'type', 'type': 'str'},
+      {'name': 'auth', 'type': '*'}]]
+  - [312, 1, '_priv', 'memtx', 0, '', [{'name': 'grantor', 'type': 'num'}, {'name': 'grantee',
+        'type': 'num'}, {'name': 'object_type', 'type': 'str'}, {'name': 'object_id',
+        'type': 'num'}, {'name': 'privilege', 'type': 'num'}]]
+  - [320, 1, '_cluster', 'memtx', 0, '', [{'name': 'id', 'type': 'num'}, {'name': 'uuid',
+        'type': 'str'}]]
 ...
 box.space._index:select{}
 ---
diff --git a/test/box/temp_spaces.result b/test/box/temp_spaces.result
index 16b1a4b867..548fb8cbdd 100644
--- a/test/box/temp_spaces.result
+++ b/test/box/temp_spaces.result
@@ -55,7 +55,7 @@ s:len()
 ...
 box.space[box.schema.SPACE_ID]:update(s.id, {{'=', FLAGS, 'temporary'}})
 ---
-- [512, 1, 't', 'memtx', 0, 'temporary']
+- [512, 1, 't', 'memtx', 0, 'temporary', []]
 ...
 box.space[box.schema.SPACE_ID]:update(s.id, {{'=', FLAGS, ''}})
 ---
@@ -79,7 +79,7 @@ s.temporary
 ...
 box.space[box.schema.SPACE_ID]:update(s.id, {{'=', FLAGS, 'no-temporary'}})
 ---
-- [512, 1, 't', 'memtx', 0, 'no-temporary']
+- [512, 1, 't', 'memtx', 0, 'no-temporary', []]
 ...
 s.temporary
 ---
@@ -87,7 +87,7 @@ s.temporary
 ...
 box.space[box.schema.SPACE_ID]:update(s.id, {{'=', FLAGS, ',:asfda:temporary'}})
 ---
-- [512, 1, 't', 'memtx', 0, ',:asfda:temporary']
+- [512, 1, 't', 'memtx', 0, ',:asfda:temporary', []]
 ...
 s.temporary
 ---
@@ -95,7 +95,7 @@ s.temporary
 ...
 box.space[box.schema.SPACE_ID]:update(s.id, {{'=', FLAGS, 'a,b,c,d,e'}})
 ---
-- [512, 1, 't', 'memtx', 0, 'a,b,c,d,e']
+- [512, 1, 't', 'memtx', 0, 'a,b,c,d,e', []]
 ...
 s.temporary
 ---
@@ -103,7 +103,7 @@ s.temporary
 ...
 box.space[box.schema.SPACE_ID]:update(s.id, {{'=', FLAGS, 'temporary'}})
 ---
-- [512, 1, 't', 'memtx', 0, 'temporary']
+- [512, 1, 't', 'memtx', 0, 'temporary', []]
 ...
 s.temporary
 ---
@@ -118,7 +118,7 @@ s:insert{1, 2, 3}
 ...
 box.space[box.schema.SPACE_ID]:update(s.id, {{'=', FLAGS, 'temporary'}})
 ---
-- [512, 1, 't', 'memtx', 0, 'temporary']
+- [512, 1, 't', 'memtx', 0, 'temporary', []]
 ...
 box.space[box.schema.SPACE_ID]:update(s.id, {{'=', FLAGS, 'no-temporary'}})
 ---
@@ -130,7 +130,7 @@ s:delete{1}
 ...
 box.space[box.schema.SPACE_ID]:update(s.id, {{'=', FLAGS, 'no-temporary'}})
 ---
-- [512, 1, 't', 'memtx', 0, 'no-temporary']
+- [512, 1, 't', 'memtx', 0, 'no-temporary', []]
 ...
 s:drop()
 ---
-- 
GitLab