[tarantool-patches] [PATCH 3/3] tuple: account the whole array in field.data and size

Vladislav Shpilevoy v.shpilevoy at tarantool.org
Sat Sep 28 01:29:11 MSK 2019


Before the patch a struct update_field object didn't account
array header in its .size and .data members. Indeed, it was not
needed, because anyway updates could be only 'flat'.
For example, consider the tuple:

    [mp_array, mp_uint, mp_uint, mp_uint]
              ^                         ^
             pos1                      pos2

Struct update_field.size and .data accounted memory from pos1 to
pos2, without the array header. Number of fields was stored inside
a rope object. This is why it made no sense to keep array header
pointer.

But now updates are going to be not flat, and not only for array.
There will be an update tree. Each node of that tree will describe
update of some part of a tuple.

Some of the nodes will need to know exact border of their
children, including headers. It is going to be used for fast
copying of neighbours of such children. Consider an example.

Tuple with one field consisting of nested maps:

    tuple = {}
    tuple[1] = {
        a = {
            b = {
                c = {
                    d = {1, 2, 3}
                }
            }
        }
    }

Update:

    {{'+', '[1].a.b.c.d[1]', 1}, {'+', '[1].a.b.c.d[2]', 1}}

To update such a tuple a simple tree will be built:

            root: [ [1] ]
                     |
 isolated path: [ 'a.b.c' ]
                     |
      leaves: [ [1] [2] [3] ]
                +1  +1   -

Root node keeps the whole tuple borders. It is a rope with single
field.
This single field is a deeply updated map. Such deep multiple
updates with long common prefixes are stored as an isolated path +
map/array in the end. Here the isolated path is 'a.b.c'. It ends
with the terminal array update.

Assume, that operations are applied and it is time to save the
result. Save starts from the root.
Root rope will encode root array header, and will try to save the
single field. The single field is an isolated update. It needs to
save everything before old {1,2,3}, the new array {2,2,3}, and
everything after the old array. The simplest way to do it - know
exact borders of the old array {1,2,3} and memcpy all memory
before and after.

This is exactly what this patch allows to do. Everything before
update_field.data, and after update_field.data + .size can be
safely copied, and is not related to the field. To copy adjacent
memory it is not even needed to know field type.
Update_field.data and .size have the same meaning for all field
types.

Part of #1261
---
 src/box/update/update.c       | 28 ++++++++++++++++------------
 src/box/update/update_array.c |  9 +++++----
 src/box/update/update_field.h |  6 ++++--
 3 files changed, 25 insertions(+), 18 deletions(-)

diff --git a/src/box/update/update.c b/src/box/update/update.c
index 564f1b75c..a1ad8602b 100644
--- a/src/box/update/update.c
+++ b/src/box/update/update.c
@@ -264,11 +264,12 @@ update_read_ops(struct tuple_update *update, const char *expr,
  * @retval -1 Error.
  */
 static int
-update_do_ops(struct tuple_update *update, const char *old_data,
-	      const char *old_data_end, uint32_t part_count)
+update_do_ops(struct tuple_update *update, const char *header,
+	      const char *old_data, const char *old_data_end,
+	      uint32_t part_count)
 {
-	if (update_array_create(&update->root, old_data, old_data_end,
-				part_count) != 0)
+	if (update_array_create(&update->root, header, old_data,
+				old_data_end, part_count) != 0)
 		return -1;
 	struct update_op *op = update->ops;
 	struct update_op *ops_end = op + update->op_count;
@@ -285,12 +286,12 @@ update_do_ops(struct tuple_update *update, const char *old_data,
  *        and it is enough to simply write the error to the log.
  */
 static int
-upsert_do_ops(struct tuple_update *update, const char *old_data,
-	      const char *old_data_end, uint32_t part_count,
-	      bool suppress_error)
+upsert_do_ops(struct tuple_update *update, const char *header,
+	      const char *old_data, const char *old_data_end,
+	      uint32_t part_count, bool suppress_error)
 {
-	if (update_array_create(&update->root, old_data, old_data_end,
-				part_count) != 0)
+	if (update_array_create(&update->root, header, old_data,
+				old_data_end, part_count) != 0)
 		return -1;
 	struct update_op *op = update->ops;
 	struct update_op *ops_end = op + update->op_count;
@@ -347,11 +348,13 @@ tuple_update_execute(const char *expr,const char *expr_end,
 {
 	struct tuple_update update;
 	update_init(&update, index_base);
+	const char *header = old_data;
 	uint32_t field_count = mp_decode_array(&old_data);
 
 	if (update_read_ops(&update, expr, expr_end, dict, field_count) != 0)
 		return NULL;
-	if (update_do_ops(&update, old_data, old_data_end, field_count))
+	if (update_do_ops(&update, header, old_data, old_data_end,
+			  field_count) != 0)
 		return NULL;
 	if (column_mask)
 		*column_mask = update.column_mask;
@@ -367,12 +370,13 @@ tuple_upsert_execute(const char *expr,const char *expr_end,
 {
 	struct tuple_update update;
 	update_init(&update, index_base);
+	const char *header = old_data;
 	uint32_t field_count = mp_decode_array(&old_data);
 
 	if (update_read_ops(&update, expr, expr_end, dict, field_count) != 0)
 		return NULL;
-	if (upsert_do_ops(&update, old_data, old_data_end, field_count,
-			  suppress_error))
+	if (upsert_do_ops(&update, header, old_data, old_data_end, field_count,
+			  suppress_error) != 0)
 		return NULL;
 	if (column_mask)
 		*column_mask = update.column_mask;
diff --git a/src/box/update/update_array.c b/src/box/update/update_array.c
index 08cb223e8..b9a007c2b 100644
--- a/src/box/update/update_array.c
+++ b/src/box/update/update_array.c
@@ -139,12 +139,13 @@ update_array_extract_item(struct update_field *field, struct update_op *op)
 }
 
 int
-update_array_create(struct update_field *field, const char *data,
-		    const char *data_end, uint32_t field_count)
+update_array_create(struct update_field *field, const char *header,
+		    const char *data, const char *data_end,
+		    uint32_t field_count)
 {
 	field->type = UPDATE_ARRAY;
-	field->data = data;
-	field->size = data_end - data;
+	field->data = header;
+	field->size = data_end - header;
 	struct region *region = &fiber()->gc;
 	field->array.rope = rope_new(region);
 	if (field->array.rope == NULL)
diff --git a/src/box/update/update_field.h b/src/box/update/update_field.h
index 813e0761b..b8f903482 100644
--- a/src/box/update/update_field.h
+++ b/src/box/update/update_field.h
@@ -306,6 +306,7 @@ update_##type##_store(struct update_field *field, char *out, char *out_end);
 /**
  * Initialize @a field as an array to update.
  * @param[out] field Field to initialize.
+ * @param header Header of the MessagePack array @a data.
  * @param data MessagePack data of the array to update.
  * @param data_end End of @a data.
  * @param field_count Field count in @data.
@@ -314,8 +315,9 @@ update_##type##_store(struct update_field *field, char *out, char *out_end);
  * @retval -1 Error.
  */
 int
-update_array_create(struct update_field *field, const char *data,
-		    const char *data_end, uint32_t field_count);
+update_array_create(struct update_field *field, const char *header,
+		    const char *data, const char *data_end,
+		    uint32_t field_count);
 
 OP_DECL_GENERIC(array)
 
-- 
2.21.0 (Apple Git-122)





More information about the Tarantool-patches mailing list