From ab5010c6af0a5c8063c642a042c05ae513997dfb Mon Sep 17 00:00:00 2001
From: Vladimir Davydov <vdavydov@tarantool.org>
Date: Mon, 29 Aug 2022 13:57:54 +0300
Subject: [PATCH] tuple: add helper for creating runtime tuple format with
 field names

Currently, the helper is used only for creation of a tuple format for
Lua (needed for net.box schema). Later on, we will reuse this helper for
creating tuple formats for user read views.

Needed for https://github.com/tarantool/tarantool-ee/issues/207

NO_DOC=refactoring
NO_TEST=refactoring
NO_CHANGELOG=refactoring
---
 src/box/lua/misc.cc |  4 +---
 src/box/tuple.c     | 15 ++++++++++++---
 src/box/tuple.h     | 12 ++++++++++++
 3 files changed, 25 insertions(+), 6 deletions(-)

diff --git a/src/box/lua/misc.cc b/src/box/lua/misc.cc
index 2cc1c74caf..db566903be 100644
--- a/src/box/lua/misc.cc
+++ b/src/box/lua/misc.cc
@@ -313,9 +313,7 @@ lbox_tuple_format_new(struct lua_State *L)
 	region_truncate(region, region_svp);
 	if (dict == NULL)
 		return luaT_error(L);
-	struct tuple_format *format =
-		tuple_format_new(&tuple_format_runtime->vtab, NULL, NULL, 0,
-				 NULL, 0, 0, dict, false, true, NULL, 0);
+	struct tuple_format *format = runtime_tuple_format_new(dict);
 	/*
 	 * Since dictionary reference counter is 1 from the
 	 * beginning and after creation of the tuple_format
diff --git a/src/box/tuple.c b/src/box/tuple.c
index 39805bfc28..1932f67d7b 100644
--- a/src/box/tuple.c
+++ b/src/box/tuple.c
@@ -284,6 +284,17 @@ tuple_bigref_tuple_count()
 	return tuple_uploaded_refs->size;
 }
 
+struct tuple_format *
+runtime_tuple_format_new(struct tuple_dictionary *dict)
+{
+	return tuple_format_new(&tuple_format_runtime_vtab, /*engine=*/NULL,
+				/*keys=*/NULL, /*key_count=*/0,
+				/*space_field_count=*/NULL,
+				/*exact_field_count=*/0, 0, dict,
+				/*is_temporary=*/false, /*is_reusable=*/true,
+				/*contraint_def=*/NULL, /*constraint_count=*/0);
+}
+
 int
 tuple_init(field_name_hash_f hash)
 {
@@ -292,9 +303,7 @@ tuple_init(field_name_hash_f hash)
 	/*
 	 * Create a format for runtime tuples
 	 */
-	tuple_format_runtime =
-		simple_tuple_format_new(&tuple_format_runtime_vtab,
-					NULL, NULL, 0);
+	tuple_format_runtime = runtime_tuple_format_new(/*dict=*/NULL);
 	if (tuple_format_runtime == NULL)
 		return -1;
 
diff --git a/src/box/tuple.h b/src/box/tuple.h
index 1a203f92c0..a57c93763d 100644
--- a/src/box/tuple.h
+++ b/src/box/tuple.h
@@ -75,6 +75,18 @@ tuple_arena_create(struct slab_arena *arena, struct quota *quota,
 void
 tuple_arena_destroy(struct slab_arena *arena);
 
+/**
+ * Creates a new format for standalone tuples.
+ *
+ * Tuples created with the new format are allocated from the runtime arena.
+ * In contrast to the preallocated tuple_format_runtime, which has no field
+ * names, the new format uses the provided field name dictionary.
+ *
+ * On success, returns the new format. On error, returns NULL and sets diag.
+ */
+struct tuple_format *
+runtime_tuple_format_new(struct tuple_dictionary *dict);
+
 /** \cond public */
 
 typedef struct tuple_format box_tuple_format_t;
-- 
GitLab