Tarantool development patches archive
 help / color / mirror / Atom feed
From: Mergen Imeev via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: s.ostanevich@corp.mail.ru, tsafin@tarantool.org
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH v1 01/10] sql: introduce mem_set_*() functions
Date: Mon,  1 Feb 2021 11:14:50 +0300	[thread overview]
Message-ID: <53e58dd59bc6eed361dbb11a3fcdc7260777499c.1612166870.git.imeevma@gmail.com> (raw)
In-Reply-To: <cover.1612166870.git.imeevma@gmail.com>

This patch add some new mem_set_*() functions. These functions will be
used to change value of a MEM. Also, function mem_init() is introduced.
This function is used to initialize a newly created MEM.

Part of #4470
---
 src/box/sql/vdbeInt.h | 101 ++++++++++++++++++++++++++++++++++++++++++
 src/box/sql/vdbemem.c | 101 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 202 insertions(+)

diff --git a/src/box/sql/vdbeInt.h b/src/box/sql/vdbeInt.h
index 7205f1af3..a24fbfc0e 100644
--- a/src/box/sql/vdbeInt.h
+++ b/src/box/sql/vdbeInt.h
@@ -484,6 +484,30 @@ void sqlVdbeMemMove(Mem *, Mem *);
 int sqlVdbeMemNulTerminate(Mem *);
 int sqlVdbeMemSetStr(Mem *, const char *, int, u8, void (*)(void *));
 
+/**
+ * Initialize a new mem. After initializing the mem will hold a NULL value.
+ *
+ * @param mem VDBE memory register to initialize.
+ */
+void
+mem_init(struct Mem *mem);
+
+/**
+ * Set VDBE memory register as NULL.
+ *
+ * @param mem VDBE memory register to update.
+ */
+void
+mem_set_null(struct Mem *mem);
+
+/**
+ * Set VDBE memory register as Undefined. MEM is not cleared prior to that.
+ *
+ * @param mem VDBE memory register to update.
+ */
+void
+mem_set_undefined(struct Mem *mem);
+
 void
 mem_set_bool(struct Mem *mem, bool value);
 
@@ -495,6 +519,15 @@ mem_set_bool(struct Mem *mem, bool value);
 void
 mem_set_ptr(struct Mem *mem, void *ptr);
 
+/**
+ * Set VDBE memory register as frame.
+ *
+ * @param mem VDBE memory register to update.
+ * @param frame Frame to set.
+ */
+void
+mem_set_frame(struct Mem *mem, struct VdbeFrame *frame);
+
 /**
  * Set integer value. Depending on its sign MEM_Int (in case
  * of negative value) or MEM_UInt flag is set.
@@ -517,6 +550,74 @@ mem_set_int(struct Mem *mem, int64_t value, bool is_neg);
 void
 mem_set_double(struct Mem *mem, double value);
 
+/**
+ * Set VDBE memory register as string. Length and way of allocation is also
+ * given. If is_null_terminated is true then sizeof(value) should be len + 1,
+ * otherwise sizeof(value) == len. If alloc_type is either MEM_Static or
+ * MEM_Ephem, then no more allocation, deallocation, or copying is required.
+ * In case it is MEM_Dyn, no need to allocate and copy, but MEM should be freed
+ * each time MEM changes or destroyed. If it is 0, the entire string should be
+ * copied into newly allocated memory, which should be freed when the memory is
+ * destroyed. However, in this case, there is no need to free memory every time
+ * the MEM changes. We only need to free memory if this MEM is given a new
+ * string or binary string with alloc_type 0.
+ *
+ * @param mem VDBE memory register to update.
+ * @param value String to set.
+ * @param len Length of the string.
+ * @param alloc_type Type of allocation of binary string.
+ * @param is_null_terminated True if string is NULL-terminated.
+ */
+int
+mem_set_str(struct Mem *mem, char *value, uint32_t len, int alloc_type,
+	    bool is_null_terminated);
+
+/**
+ * Set VDBE memory register as binary. Length and way of allocation is also
+ * given. If is_zero is true then binary received from this MEM may have length
+ * more than given size, however this is done outside of this MEM. If alloc_type
+ * is either MEM_Static or MEM_Ephem, then no more allocation, deallocation, or
+ * copying is required. In case it is MEM_Dyn, no need to allocate and copy, but
+ * MEM should be freed each time MEM changes or destroyed. If it is 0, the
+ * entire binary string should be copied into newly allocated memory, which
+ * should be freed when the memory is destroyed. However, in this case, there is
+ * no need to free memory every time the MEM changes. We only need to free
+ * memory if this MEM is given a new string or binary string with alloc_type 0.
+ *
+ * @param mem VDBE memory register to update.
+ * @param value Binary string to set.
+ * @param len Length of the string.
+ * @param alloc_type Type of allocation of binary string.
+ * @param is_zero True if binary string may be expanded with zeroes at the end.
+ */
+int
+mem_set_bin(struct Mem *mem, char *value, uint32_t size, int alloc_type,
+	    bool is_zero);
+
+/**
+ * Set VDBE memory register as MAP. See @a mem_set_bin with is_zero = 0 for
+ * more.
+ *
+ * @param mem VDBE memory register to update.
+ * @param value Binary string that contains msgpack with type MP_MAP to set.
+ * @param len Length of the binary string.
+ * @param alloc_type Type of allocation of binary string.
+ */
+int
+mem_set_map(struct Mem *mem, char *value, uint32_t size, int alloc_type);
+
+/**
+ * Set VDBE memory register as ARRAY. See @a mem_set_bin with is_zero = 0 for
+ * more.
+ *
+ * @param mem VDBE memory register to update.
+ * @param value Binary string that contains msgpack with type MP_ARRAY to set.
+ * @param len Length of the binary string.
+ * @param alloc_type Type of allocation of binary string.
+ */
+int
+mem_set_array(struct Mem *mem, char *value, uint32_t size, int alloc_type);
+
 void sqlVdbeMemInit(Mem *, sql *, u32);
 void sqlVdbeMemSetNull(Mem *);
 void sqlVdbeMemSetZeroBlob(Mem *, int);
diff --git a/src/box/sql/vdbemem.c b/src/box/sql/vdbemem.c
index 1101f7205..186aebe03 100644
--- a/src/box/sql/vdbemem.c
+++ b/src/box/sql/vdbemem.c
@@ -799,6 +799,29 @@ sqlValueSetNull(sql_value * p)
 	sqlVdbeMemSetNull((Mem *) p);
 }
 
+void
+mem_init(struct Mem *mem)
+{
+	memset(mem, 0, sizeof(*mem));
+	mem->db = sql_get();
+	mem->flags = MEM_Null;
+	mem->field_type = field_type_MAX;
+}
+
+void
+mem_set_null(struct Mem *mem)
+{
+	sqlVdbeMemSetNull(mem);
+	mem->field_type = field_type_MAX;
+}
+
+void
+mem_set_undefined(struct Mem *mem)
+{
+	mem->flags = MEM_Undefined;
+	mem->field_type = field_type_MAX;
+}
+
 void
 mem_set_ptr(struct Mem *mem, void *ptr)
 {
@@ -807,6 +830,15 @@ mem_set_ptr(struct Mem *mem, void *ptr)
 	mem->u.p = ptr;
 }
 
+void
+mem_set_frame(struct Mem *mem, struct VdbeFrame *frame)
+{
+	sqlVdbeMemSetNull(mem);
+	mem->u.pFrame = frame;
+	mem->flags = MEM_Frame;
+	mem->field_type = field_type_MAX;
+}
+
 /*
  * Delete any previous value and set the value to be a BLOB of length
  * n containing all zeros.
@@ -880,6 +912,75 @@ mem_set_double(struct Mem *mem, double value)
 	mem->field_type = FIELD_TYPE_DOUBLE;
 }
 
+int
+mem_set_str(struct Mem *mem, char *value, uint32_t len, int alloc_type,
+	    bool is_null_terminated)
+{
+	sqlVdbeMemSetNull(mem);
+	if (alloc_type != 0) {
+		mem->z = value;
+		if (alloc_type == MEM_Dyn)
+			mem->xDel = SQL_DYNAMIC;
+	} else {
+		uint32_t size = is_null_terminated ? len + 1 : len;
+		if (sqlVdbeMemClearAndResize(mem, size) != 0)
+			return -1;
+		memcpy(mem->z, value, size);
+	}
+	mem->n = len;
+	mem->flags = MEM_Str | alloc_type;
+	if (is_null_terminated)
+		mem->flags |= MEM_Term;
+	mem->field_type = FIELD_TYPE_STRING;
+	return 0;
+}
+
+int
+mem_set_bin(struct Mem *mem, char *value, uint32_t size, int alloc_type,
+	    bool is_zero)
+{
+	sqlVdbeMemSetNull(mem);
+	if (alloc_type != 0) {
+		mem->z = value;
+		if (alloc_type == MEM_Dyn)
+			mem->xDel = SQL_DYNAMIC;
+	} else {
+		if (sqlVdbeMemClearAndResize(mem, size) != 0)
+			return -1;
+		memcpy(mem->z, value, size);
+	}
+	mem->n = size;
+	mem->flags = MEM_Blob | alloc_type;
+	if (is_zero)
+		mem->flags |= MEM_Zero;
+	mem->field_type = FIELD_TYPE_VARBINARY;
+	return 0;
+}
+
+int
+mem_set_map(struct Mem *mem, char *value, uint32_t size, int alloc_type)
+{
+	assert(mp_typeof(*value) == MP_MAP);
+	if (mem_set_bin(mem, value, size, alloc_type, false) != 0)
+		return -1;
+	mem->subtype = SQL_SUBTYPE_MSGPACK;
+	mem->flags |= MEM_Subtype;
+	mem->field_type = FIELD_TYPE_MAP;
+	return 0;
+}
+
+int
+mem_set_array(struct Mem *mem, char *value, uint32_t size, int alloc_type)
+{
+	assert(mp_typeof(*value) == MP_ARRAY);
+	if (mem_set_bin(mem, value, size, alloc_type, false) != 0)
+		return -1;
+	mem->subtype = SQL_SUBTYPE_MSGPACK;
+	mem->flags |= MEM_Subtype;
+	mem->field_type = FIELD_TYPE_ARRAY;
+	return 0;
+}
+
 /*
  * Return true if the Mem object contains a TEXT or BLOB that is
  * too large - whose size exceeds SQL_MAX_LENGTH.
-- 
2.25.1


  reply	other threads:[~2021-02-01  8:15 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-01  8:14 [Tarantool-patches] [PATCH v1 00/10] Encapsulate MEM type changing and checking Mergen Imeev via Tarantool-patches
2021-02-01  8:14 ` Mergen Imeev via Tarantool-patches [this message]
2021-02-01  8:14 ` [Tarantool-patches] [PATCH v1 02/10] sql: Initialize MEM in sqlVdbeAllocUnpackedRecord() Mergen Imeev via Tarantool-patches
2021-02-01  8:14 ` [Tarantool-patches] [PATCH v1 03/10] sql: introduce mem_is_*() functions Mergen Imeev via Tarantool-patches
2021-02-01  8:14 ` [Tarantool-patches] [PATCH v1 04/10] sql: introduce mem_convert_to_binary() Mergen Imeev via Tarantool-patches
2021-02-01  8:14 ` [Tarantool-patches] [PATCH v1 05/10] sql: refactor vdbesort.c Mergen Imeev via Tarantool-patches
2021-02-01  8:15 ` [Tarantool-patches] [PATCH v1 06/10] sql: refactor sql/func.c Mergen Imeev via Tarantool-patches
2021-02-01  8:15 ` [Tarantool-patches] [PATCH v1 07/10] sql: refactor vdbetrace.c Mergen Imeev via Tarantool-patches
2021-02-01  8:15 ` [Tarantool-patches] [PATCH v1 08/10] sql: refactor vdbeapi.c Mergen Imeev via Tarantool-patches
2021-02-01  8:15 ` [Tarantool-patches] [PATCH v1 09/10] sql: refactor vdbeaux.c Mergen Imeev via Tarantool-patches
2021-02-09  9:51   ` [Tarantool-patches] FW: " Timur Safin via Tarantool-patches
2021-02-13 15:33     ` Mergen Imeev via Tarantool-patches
2021-02-28 17:35     ` Vladislav Shpilevoy via Tarantool-patches
2021-02-01  8:15 ` [Tarantool-patches] [PATCH v1 10/10] sql: refactor vdbe.c Mergen Imeev via Tarantool-patches
     [not found]   ` <047f01d6fec7$b5a90bb0$20fb2310$@tarantool.org>
2021-02-13 15:26     ` Mergen Imeev via Tarantool-patches
2021-02-09  9:36 ` [Tarantool-patches] [PATCH v1 00/10] Encapsulate MEM type changing and checking Timur Safin via Tarantool-patches
2021-02-13 15:13   ` Mergen Imeev via Tarantool-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=53e58dd59bc6eed361dbb11a3fcdc7260777499c.1612166870.git.imeevma@gmail.com \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=imeevma@tarantool.org \
    --cc=s.ostanevich@corp.mail.ru \
    --cc=tsafin@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH v1 01/10] sql: introduce mem_set_*() functions' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox