[patches] [server 2/2] alter: introduce ModifySpaceFormat alter operation
Vladislav Shpilevoy
v.shpilevoy at tarantool.org
Tue Jan 23 14:38:44 MSK 2018
ModifySpaceFormat operation sets new names into old tuple formats.
Closes #3011
Signed-off-by: Vladislav Shpilevoy <v.shpilevoy at tarantool.org>
---
src/box/alter.cc | 56 ++++++++++++++++++++++++++++++++++++++
test/box/alter.result | 72 +++++++++++++++++++++++++++++++++++++++++++++++++
test/box/alter.test.lua | 24 +++++++++++++++++
3 files changed, 152 insertions(+)
diff --git a/src/box/alter.cc b/src/box/alter.cc
index 435febdd2..eb453d096 100644
--- a/src/box/alter.cc
+++ b/src/box/alter.cc
@@ -820,6 +820,61 @@ alter_space_do(struct txn *txn, struct alter_space *alter)
/* {{{ AlterSpaceOp descendants - alter operations, such as Add/Drop index */
+/**
+ * The operation is executed on each space format change.
+ * Now the single purpose is to update an old field names
+ * dictionary, used by old space formats, and use it in a new
+ * formats (vinyl creates many formats, not one).
+ */
+class ModifySpaceFormat: public AlterSpaceOp
+{
+ /**
+ * Newely created field dictionary. When new space_def is
+ * created, it allocates new dictionary. Alter moves new
+ * names into an old dictionary and deletes new one.
+ */
+ struct tuple_dictionary *new_dict;
+ /**
+ * New space definition. It can not be got from alter,
+ * because alter_def() is called before
+ * ModifySpace::alter_def().
+ */
+ struct space_def *new_def;
+public:
+ ModifySpaceFormat(struct alter_space *alter, struct space_def *new_def)
+ :AlterSpaceOp(alter), new_dict(NULL), new_def(new_def) {}
+ virtual void alter_def(struct alter_space *alter);
+ virtual void rollback(struct alter_space *alter);
+ virtual ~ModifySpaceFormat()
+ {
+ if (new_dict != NULL)
+ tuple_dictionary_unref(new_dict);
+ }
+};
+
+void
+ModifySpaceFormat::alter_def(struct alter_space *alter)
+{
+ /*
+ * Move new names into an old dictionary, which already is
+ * referenced by existing tuple formats. New dictionary
+ * object is deleted later, in destructor.
+ */
+ new_dict = new_def->dict;
+ struct tuple_dictionary *old_dict = alter->old_space->def->dict;
+ tuple_dictionary_swap(new_dict, old_dict);
+ new_def->dict = old_dict;
+ tuple_dictionary_ref(old_dict);
+}
+
+void
+ModifySpaceFormat::rollback(struct alter_space *alter)
+{
+ /* Return old names into the old dict. */
+ struct tuple_dictionary *old_dict = alter->old_space->def->dict;
+ tuple_dictionary_swap(new_dict, old_dict);
+}
+
/** Change non-essential properties of a space. */
class ModifySpace: public AlterSpaceOp
{
@@ -1437,6 +1492,7 @@ on_replace_dd_space(struct trigger * /* trigger */, void *event)
struct alter_space *alter = alter_space_new(old_space);
auto alter_guard =
make_scoped_guard([=] {alter_space_delete(alter);});
+ (void) new ModifySpaceFormat(alter, def);
(void) new ModifySpace(alter, def);
def_guard.is_active = false;
/* Create MoveIndex ops for all space indexes. */
diff --git a/test/box/alter.result b/test/box/alter.result
index 0a5c55889..fc6efde6c 100644
--- a/test/box/alter.result
+++ b/test/box/alter.result
@@ -1735,3 +1735,75 @@ s:select{}
s:drop()
---
...
+--
+-- gh-3011: add new names to old tuple formats.
+--
+s = box.schema.create_space('test')
+---
+...
+pk = s:create_index('pk')
+---
+...
+t1 = s:replace{1}
+---
+...
+t1.field1
+---
+- null
+...
+format = {}
+---
+...
+format[1] = {name = 'field1', type = 'unsigned'}
+---
+...
+s:format(format)
+---
+...
+t2 = s:replace{2}
+---
+...
+t2.field1
+---
+- 2
+...
+t1.field1
+---
+- 1
+...
+format[1].name = 'field_1'
+---
+...
+s:format(format)
+---
+...
+t3 = s:replace{3}
+---
+...
+t1.field1
+---
+- null
+...
+t1.field_1
+---
+- 1
+...
+t2.field1
+---
+- null
+...
+t2.field_1
+---
+- 2
+...
+t3.field1
+---
+- null
+...
+t3.field_1
+---
+- 3
+...
+s:drop()
+---
+...
diff --git a/test/box/alter.test.lua b/test/box/alter.test.lua
index 9219f6570..a74c84f09 100644
--- a/test/box/alter.test.lua
+++ b/test/box/alter.test.lua
@@ -661,3 +661,27 @@ pk:alter{parts = {{1, 'integer'}}}
s:replace{-2}
s:select{}
s:drop()
+
+--
+-- gh-3011: add new names to old tuple formats.
+--
+s = box.schema.create_space('test')
+pk = s:create_index('pk')
+t1 = s:replace{1}
+t1.field1
+format = {}
+format[1] = {name = 'field1', type = 'unsigned'}
+s:format(format)
+t2 = s:replace{2}
+t2.field1
+t1.field1
+format[1].name = 'field_1'
+s:format(format)
+t3 = s:replace{3}
+t1.field1
+t1.field_1
+t2.field1
+t2.field_1
+t3.field1
+t3.field_1
+s:drop()
--
2.11.0 (Apple Git-81)
More information about the Tarantool-patches
mailing list