[Tarantool-patches] [PATCH resend v2 07/11] lua, datetime: proper datetime encoding

Timur Safin tsafin at tarantool.org
Wed Jul 28 13:34:09 MSK 2021


* correct incorrect encoding of MP_EXT sizes for datetime
  messagepack values;
* export necessary symbols for datetime messagepack size calculations
  so they will be available for Lua consumption.

Part of #5941
---
 src/exports.h           |  2 ++
 src/lib/core/datetime.c | 15 ++++++++++-----
 src/lua/msgpackffi.lua  | 10 ++++++++++
 3 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/src/exports.h b/src/exports.h
index 586444b65..3a1e8854c 100644
--- a/src/exports.h
+++ b/src/exports.h
@@ -437,6 +437,7 @@ EXPORT(mp_encode_decimal)
 EXPORT(mp_encode_double)
 EXPORT(mp_encode_float)
 EXPORT(mp_encode_uuid)
+EXPORT(mp_encode_datetime)
 EXPORT(mp_ext_hint)
 EXPORT(mp_format)
 EXPORT(mp_fprint)
@@ -446,6 +447,7 @@ EXPORT(mp_fprint_recursion)
 EXPORT(mp_parser_hint)
 EXPORT(mp_sizeof_decimal)
 EXPORT(mp_sizeof_uuid)
+EXPORT(mp_sizeof_datetime)
 EXPORT(mp_snprint)
 EXPORT(mp_snprint_ext)
 EXPORT(mp_snprint_ext_default)
diff --git a/src/lib/core/datetime.c b/src/lib/core/datetime.c
index 26ba8a702..76b06eae5 100755
--- a/src/lib/core/datetime.c
+++ b/src/lib/core/datetime.c
@@ -83,8 +83,8 @@ mp_decode_Xint(const char **data)
 	return 0;
 }
 
-uint32_t
-mp_sizeof_datetime(const struct datetime_t *date)
+static inline uint32_t
+mp_sizeof_datetime_raw(const struct datetime_t *date)
 {
 	uint32_t sz = mp_sizeof_Xint(date->secs);
 
@@ -94,10 +94,15 @@ mp_sizeof_datetime(const struct datetime_t *date)
 		sz += mp_sizeof_Xint(date->nsec);
 	if (date->offset)
 		sz += mp_sizeof_Xint(date->offset);
-
 	return sz;
 }
 
+uint32_t
+mp_sizeof_datetime(const struct datetime_t *date)
+{
+	return mp_sizeof_ext(mp_sizeof_datetime_raw(date));
+}
+
 struct datetime_t *
 datetime_unpack(const char **data, uint32_t len, struct datetime_t *date)
 {
@@ -112,7 +117,7 @@ datetime_unpack(const char **data, uint32_t len, struct datetime_t *date)
 		return date;
 
 	svp = *data;
-	date->secs = mp_decode_Xint(data);
+	date->nsec = mp_decode_Xint(data);
 	len -= *data - svp;
 
 	if (len <= 0)
@@ -153,7 +158,7 @@ datetime_pack(char *data, const struct datetime_t *date)
 char *
 mp_encode_datetime(char *data, const struct datetime_t *date)
 {
-	uint32_t len = mp_sizeof_datetime(date);
+	uint32_t len = mp_sizeof_datetime_raw(date);
 
 	data = mp_encode_extl(data, MP_DATETIME, len);
 
diff --git a/src/lua/msgpackffi.lua b/src/lua/msgpackffi.lua
index 271be857a..c47d77acb 100644
--- a/src/lua/msgpackffi.lua
+++ b/src/lua/msgpackffi.lua
@@ -26,6 +26,10 @@ char *
 mp_encode_uuid(char *data, const struct tt_uuid *uuid);
 uint32_t
 mp_sizeof_uuid();
+uint32_t
+mp_sizeof_datetime(const struct t_datetime_tz *date);
+char *
+mp_encode_datetime(char *data, const struct t_datetime_tz *date);
 float
 mp_decode_float(const char **data);
 double
@@ -144,6 +148,11 @@ local function encode_uuid(buf, uuid)
     builtin.mp_encode_uuid(p, uuid)
 end
 
+local function encode_datetime(buf, date)
+    local p = buf:alloc(builtin.mp_sizeof_datetime(date))
+    builtin.mp_encode_datetime(p, date)
+end
+
 local function encode_int(buf, num)
     if num >= 0 then
         if num <= 0x7f then
@@ -322,6 +331,7 @@ on_encode(ffi.typeof('float'), encode_float)
 on_encode(ffi.typeof('double'), encode_double)
 on_encode(ffi.typeof('decimal_t'), encode_decimal)
 on_encode(ffi.typeof('struct tt_uuid'), encode_uuid)
+on_encode(ffi.typeof('struct datetime_t'), encode_datetime)
 
 --------------------------------------------------------------------------------
 -- Decoder
-- 
2.29.2



More information about the Tarantool-patches mailing list