From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id A3E5468F40; Tue, 23 Mar 2021 12:51:47 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org A3E5468F40 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1616493107; bh=LrQ93D2B9YHKGWUYu6Rov2jeEss40FrxJdevkE5EtjI=; h=To:Cc:Date:In-Reply-To:References:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=xCkPY11MpV66qGRaJE7jvYa6zEVHTZLtegqxM1lV6hutZYIBEevRrqv7xSS/8tXRG eLFaoHgrL4MUTzna4U3XenqeweSAc0T63lZPYK1A+BigMmnxipxx+uU7T1pzO00tAI 5DXnY2y5STPE6mK1MIxaHYtGDXbcNtVjDPAs+0dg= Received: from smtp16.mail.ru (smtp16.mail.ru [94.100.176.153]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 2117568F57 for ; Tue, 23 Mar 2021 12:36:09 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 2117568F57 Received: by smtp16.mail.ru with esmtpa (envelope-from ) id 1lOdSK-00035i-6n; Tue, 23 Mar 2021 12:36:08 +0300 To: v.shpilevoy@tarantool.org, tsafin@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Tue, 23 Mar 2021 12:36:07 +0300 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-7564579A: 78E4E2B564C1792B X-77F55803: 4F1203BC0FB41BD95D6E7CC48CB1F5F19D4B90E2C0E110A6B701D7D268243E19182A05F538085040A39FB548DDBA20054221549A340FC1F98B6B28EB1C5AA90500BABA40B3484B03 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7AB524098FB2F2222EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637D84D274D72FCF47F8638F802B75D45FF914D58D5BE9E6BC131B5C99E7648C95C686AF563A045C75E0D35645F605E7D5335008D1CE41587A9A471835C12D1D9774AD6D5ED66289B5278DA827A17800CE764603B5C71CE8B8F9FA2833FD35BB23D2EF20D2F80756B5F868A13BD56FB6657A471835C12D1D977725E5C173C3A84C3643C8550F8485502117882F4460429728AD0CFFFB425014E868A13BD56FB6657D81D268191BDAD3DC09775C1D3CA48CFF209DE081F923274BA3038C0950A5D36C8A9BA7A39EFB766EC990983EF5C0329BA3038C0950A5D36D5E8D9A59859A8B695372DBD645BC79B76E601842F6C81A1F004C906525384307823802FF610243DF43C7A68FF6260569E8FC8737B5C2249EC8D19AE6D49635B3BBE47FD9DD3FB59A8DF7F3B2552694A57739F23D657EF2B13377AFFFEAFD26923F8577A6DFFEA7CAA794D17B305146093EC92FD9297F6715571747095F342E857739F23D657EF2BD5E8D9A59859A8B627F5244B2F0CCF32089D37D7C0E48F6C5571747095F342E857739F23D657EF2B6825BDBE14D8E702E4EE3A04994FF497E5BFE6E7EFDEDCD789D4C264860C145E X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A2BBE337FB72E92315FF39D8DB89857825EFA8BF88FCBFD63CE0852D54D1EC5181386D740E071D760D03615D58E5D586DD X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C686AF563A045C75E0D35645F605E7D5335008D1CE41587A99C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF0417BEADF48D1460699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34E318F287A436F24CE64E4DB8266996D357A3BB14D733936B7E73F2F5AA28995ED355A36697D3BF2A1D7E09C32AA3244C8DA590F8A51D6DC86A91C3B5234B21C34DBEAD0ED6C55A80FACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojWJZv20R+6UhU7tp7X8k2Uw== X-Mailru-Sender: 5C3750E245F362008BC1685FEC6306ED6EE81EC32348A8BC4221549A340FC1F95F9045B74F665CC85105BD0848736F9966FEC6BF5C9C28D97E07721503EA2E00ED97202A5A4E92BF7402F9BA4338D657ED14614B50AE0675 X-Mras: Ok Subject: [Tarantool-patches] [PATCH v4 34/53] sql: introduce mem_set_*_map() and mem_set_*_array() X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Mergen Imeev via Tarantool-patches Reply-To: imeevma@tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" This patch introduces mem_set_*_map() and mem_set_*_array() functions. These function should be used to set MAP or ARRAY values to MEM. Part of #5818 --- src/box/sql/mem.c | 176 +++++++++++++++++++++------------------------ src/box/sql/mem.h | 26 ++++++- src/box/sql/vdbe.c | 8 ++- 3 files changed, 109 insertions(+), 101 deletions(-) diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c index 7885caaf5..583de00a2 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -508,6 +508,86 @@ mem_append_to_binary(struct Mem *mem, const char *value, uint32_t size) return 0; } +void +mem_set_ephemeral_map(struct Mem *mem, char *value, uint32_t size) +{ + assert(mp_typeof(*value) == MP_MAP); + mem_set_const_bin(mem, value, size, MEM_Ephem); + mem->flags |= MEM_Subtype; + mem->subtype = SQL_SUBTYPE_MSGPACK; + mem->field_type = FIELD_TYPE_MAP; +} + +void +mem_set_static_map(struct Mem *mem, char *value, uint32_t size) +{ + assert(mp_typeof(*value) == MP_MAP); + mem_set_const_bin(mem, value, size, MEM_Static); + mem->flags |= MEM_Subtype; + mem->subtype = SQL_SUBTYPE_MSGPACK; + mem->field_type = FIELD_TYPE_MAP; +} + +void +mem_set_dynamic_map(struct Mem *mem, char *value, uint32_t size) +{ + assert(mp_typeof(*value) == MP_MAP); + mem_set_dyn_bin(mem, value, size, MEM_Dyn); + mem->flags |= MEM_Subtype; + mem->subtype = SQL_SUBTYPE_MSGPACK; + mem->field_type = FIELD_TYPE_MAP; +} + +void +mem_set_allocated_map(struct Mem *mem, char *value, uint32_t size) +{ + assert(mp_typeof(*value) == MP_MAP); + mem_set_dyn_bin(mem, value, size, 0); + mem->flags |= MEM_Subtype; + mem->subtype = SQL_SUBTYPE_MSGPACK; + mem->field_type = FIELD_TYPE_MAP; +} + +void +mem_set_ephemeral_array(struct Mem *mem, char *value, uint32_t size) +{ + assert(mp_typeof(*value) == MP_ARRAY); + mem_set_const_bin(mem, value, size, MEM_Ephem); + mem->flags |= MEM_Subtype; + mem->subtype = SQL_SUBTYPE_MSGPACK; + mem->field_type = FIELD_TYPE_ARRAY; +} + +void +mem_set_static_array(struct Mem *mem, char *value, uint32_t size) +{ + assert(mp_typeof(*value) == MP_ARRAY); + mem_set_const_bin(mem, value, size, MEM_Static); + mem->flags |= MEM_Subtype; + mem->subtype = SQL_SUBTYPE_MSGPACK; + mem->field_type = FIELD_TYPE_ARRAY; +} + +void +mem_set_dynamic_array(struct Mem *mem, char *value, uint32_t size) +{ + assert(mp_typeof(*value) == MP_ARRAY); + mem_set_dyn_bin(mem, value, size, MEM_Dyn); + mem->flags |= MEM_Subtype; + mem->subtype = SQL_SUBTYPE_MSGPACK; + mem->field_type = FIELD_TYPE_ARRAY; +} + +void +mem_set_allocated_array(struct Mem *mem, char *value, uint32_t size) +{ + assert(mp_typeof(*value) == MP_ARRAY); + mem_set_dyn_bin(mem, value, size, 0); + mem->flags |= MEM_Subtype; + mem->subtype = SQL_SUBTYPE_MSGPACK; + mem->field_type = FIELD_TYPE_ARRAY; +} + int mem_copy(struct Mem *to, const struct Mem *from) { @@ -2134,102 +2214,6 @@ mem_set_ptr(struct Mem *mem, void *ptr) mem->u.p = ptr; } -/* - * Change the value of a Mem to be a string or a BLOB. - * - * The memory management strategy depends on the value of the xDel - * parameter. If the value passed is SQL_TRANSIENT, then the - * string is copied into a (possibly existing) buffer managed by the - * Mem structure. Otherwise, any existing buffer is freed and the - * pointer copied. - * - * If the string is too large (if it exceeds the SQL_LIMIT_LENGTH - * size limit) then no memory allocation occurs. If the string can be - * stored without allocating memory, then it is. If a memory allocation - * is required to store the string, then value of pMem is unchanged. In - * either case, error is returned. - */ -int -sqlVdbeMemSetStr(Mem * pMem, /* Memory cell to set to string value */ - const char *z, /* String pointer */ - int n, /* Bytes in string, or negative */ - u8 not_blob, /* Encoding of z. 0 for BLOBs */ - void (*xDel) (void *) /* Destructor function */ - ) -{ - int nByte = n; /* New value for pMem->n */ - int iLimit; /* Maximum allowed string or blob size */ - u16 flags = 0; /* New value for pMem->flags */ - - /* If z is a NULL pointer, set pMem to contain an SQL NULL. */ - if (!z) { - mem_clear(pMem); - return 0; - } - - if (pMem->db) { - iLimit = pMem->db->aLimit[SQL_LIMIT_LENGTH]; - } else { - iLimit = SQL_MAX_LENGTH; - } - flags = (not_blob == 0 ? MEM_Blob : MEM_Str); - if (nByte < 0) { - assert(not_blob != 0); - nByte = sqlStrlen30(z); - if (nByte > iLimit) - nByte = iLimit + 1; - flags |= MEM_Term; - } - - /* The following block sets the new values of Mem.z and Mem.xDel. It - * also sets a flag in local variable "flags" to indicate the memory - * management (one of MEM_Dyn or MEM_Static). - */ - if (xDel == SQL_TRANSIENT) { - int nAlloc = nByte; - if (flags & MEM_Term) { - nAlloc += 1; //SQL_UTF8 - } - if (nByte > iLimit) { - diag_set(ClientError, ER_SQL_EXECUTE, "string or binary"\ - "string is too big"); - return -1; - } - testcase(nAlloc == 0); - testcase(nAlloc == 31); - testcase(nAlloc == 32); - if (sqlVdbeMemClearAndResize(pMem, MAX(nAlloc, 32))) { - return -1; - } - memcpy(pMem->z, z, nAlloc); - } else if (xDel == SQL_DYNAMIC) { - mem_destroy(pMem); - pMem->zMalloc = pMem->z = (char *)z; - pMem->szMalloc = sqlDbMallocSize(pMem->db, pMem->zMalloc); - } else { - mem_destroy(pMem); - pMem->z = (char *)z; - pMem->xDel = xDel; - flags |= ((xDel == SQL_STATIC) ? MEM_Static : MEM_Dyn); - } - - pMem->n = nByte; - pMem->flags = flags; - assert((pMem->flags & (MEM_Str | MEM_Blob)) != 0); - if ((pMem->flags & MEM_Str) != 0) - pMem->field_type = FIELD_TYPE_STRING; - else - pMem->field_type = FIELD_TYPE_VARBINARY; - - if (nByte > iLimit) { - diag_set(ClientError, ER_SQL_EXECUTE, "string or binary string"\ - "is too big"); - return -1; - } - - return 0; -} - /* * Free an sql_value object */ diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h index 3bd04bbbd..b94495f89 100644 --- a/src/box/sql/mem.h +++ b/src/box/sql/mem.h @@ -238,6 +238,30 @@ mem_copy_binary(struct Mem *mem, const char *value, uint32_t size); int mem_append_to_binary(struct Mem *mem, const char *value, uint32_t size); +void +mem_set_ephemeral_map(struct Mem *mem, char *value, uint32_t size); + +void +mem_set_static_map(struct Mem *mem, char *value, uint32_t size); + +void +mem_set_dynamic_map(struct Mem *mem, char *value, uint32_t size); + +void +mem_set_allocated_map(struct Mem *mem, char *value, uint32_t size); + +void +mem_set_ephemeral_array(struct Mem *mem, char *value, uint32_t size); + +void +mem_set_static_array(struct Mem *mem, char *value, uint32_t size); + +void +mem_set_dynamic_array(struct Mem *mem, char *value, uint32_t size); + +void +mem_set_allocated_array(struct Mem *mem, char *value, uint32_t size); + int mem_copy(struct Mem *to, const struct Mem *from); @@ -473,8 +497,6 @@ int sqlVdbeMemClearAndResize(struct Mem * pMem, int n); void mem_set_ptr(struct Mem *mem, void *ptr); -int -sqlVdbeMemSetStr(struct Mem *, const char *, int, u8, void (*)(void *)); void sqlValueFree(struct Mem *); struct Mem *sqlValueNew(struct sql *); diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c index 9fc08e30c..c91aeede3 100644 --- a/src/box/sql/vdbe.c +++ b/src/box/sql/vdbe.c @@ -862,9 +862,11 @@ case OP_Blob: { /* out2 */ */ mem_set_static_binary(pOut, pOp->p4.z, pOp->p1); } else { - sqlVdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0); - pOut->flags |= MEM_Subtype; - pOut->subtype = pOp->p3; + assert(pOp->p3 == SQL_SUBTYPE_MSGPACK); + if (mp_typeof(*pOp->p4.z) == MP_MAP) + mem_set_static_map(pOut, pOp->p4.z, pOp->p1); + else + mem_set_static_array(pOut, pOp->p4.z, pOp->p1); } UPDATE_MAX_BLOBSIZE(pOut); break; -- 2.25.1