From: imeevma@tarantool.org To: tarantool-patches@freelists.org Subject: [tarantool-patches] [PATCH 2/5] Move some decode functions from alter.cc Date: Thu, 12 Jul 2018 14:16:11 +0300 [thread overview] Message-ID: <b8bb5caad710e629ee5b2b4a99e543d17f5706b1.1531393821.git.imeevma@gmail.com> (raw) In-Reply-To: <cover.1531393821.git.imeevma@gmail.com> Allow to use some format and options decode functions outside of alter.cc. Part of #3375. --- src/box/alter.cc | 203 ++-------------------------------------------------- src/box/index_def.c | 60 ++++++++++++++++ src/box/index_def.h | 8 +++ src/box/space_def.c | 150 ++++++++++++++++++++++++++++++++++++++ src/box/space_def.h | 15 ++++ 5 files changed, 237 insertions(+), 199 deletions(-) diff --git a/src/box/alter.cc b/src/box/alter.cc index 7b6bd1a..15240c0 100644 --- a/src/box/alter.cc +++ b/src/box/alter.cc @@ -167,61 +167,6 @@ err: } /** - * Fill index_opts structure from opts field in tuple of space _index - * Throw an error is unrecognized option. - */ -static void -index_opts_decode(struct index_opts *opts, const char *map, - struct region *region) -{ - index_opts_create(opts); - if (opts_decode(opts, index_opts_reg, &map, ER_WRONG_INDEX_OPTIONS, - BOX_INDEX_FIELD_OPTS, region) != 0) - diag_raise(); - if (opts->distance == rtree_index_distance_type_MAX) { - tnt_raise(ClientError, ER_WRONG_INDEX_OPTIONS, - BOX_INDEX_FIELD_OPTS, "distance must be either "\ - "'euclid' or 'manhattan'"); - } - if (opts->sql != NULL) { - char *sql = strdup(opts->sql); - if (sql == NULL) { - opts->sql = NULL; - tnt_raise(OutOfMemory, strlen(opts->sql) + 1, "strdup", - "sql"); - } - opts->sql = sql; - } - if (opts->range_size <= 0) { - tnt_raise(ClientError, ER_WRONG_INDEX_OPTIONS, - BOX_INDEX_FIELD_OPTS, - "range_size must be greater than 0"); - } - if (opts->page_size <= 0 || opts->page_size > opts->range_size) { - tnt_raise(ClientError, ER_WRONG_INDEX_OPTIONS, - BOX_INDEX_FIELD_OPTS, - "page_size must be greater than 0 and " - "less than or equal to range_size"); - } - if (opts->run_count_per_level <= 0) { - tnt_raise(ClientError, ER_WRONG_INDEX_OPTIONS, - BOX_INDEX_FIELD_OPTS, - "run_count_per_level must be greater than 0"); - } - if (opts->run_size_ratio <= 1) { - tnt_raise(ClientError, ER_WRONG_INDEX_OPTIONS, - BOX_INDEX_FIELD_OPTS, - "run_size_ratio must be greater than 1"); - } - if (opts->bloom_fpr <= 0 || opts->bloom_fpr > 1) { - tnt_raise(ClientError, ER_WRONG_INDEX_OPTIONS, - BOX_INDEX_FIELD_OPTS, - "bloom_fpr must be greater than 0 and " - "less than or equal to 1"); - } -} - -/** * Create a index_def object from a record in _index * system space. * @@ -250,7 +195,8 @@ index_def_new_from_tuple(struct tuple *tuple, struct space *space) const char *opts_field = tuple_field_with_type_xc(tuple, BOX_INDEX_FIELD_OPTS, MP_MAP); - index_opts_decode(&opts, opts_field, &fiber()->gc); + if(index_opts_decode(&opts, opts_field, &fiber()->gc) != 0) + diag_raise(); const char *parts = tuple_field(tuple, BOX_INDEX_FIELD_PARTS); uint32_t part_count = mp_decode_array(&parts); if (name_len > BOX_NAME_MAX) { @@ -316,149 +262,6 @@ space_opts_decode(struct space_opts *opts, const char *map, } /** - * Decode field definition from MessagePack map. Format: - * {name: <string>, type: <string>}. Type is optional. - * @param[out] field Field to decode to. - * @param data MessagePack map to decode. - * @param space_name Name of a space, from which the field is got. - * Used in error messages. - * @param name_len Length of @a space_name. - * @param errcode Error code to use for client errors. Either - * create or modify space errors. - * @param fieldno Field number to decode. Used in error messages. - * @param region Region to allocate field name. - */ -static void -field_def_decode(struct field_def *field, const char **data, - const char *space_name, uint32_t name_len, - uint32_t errcode, uint32_t fieldno, struct region *region) -{ - if (mp_typeof(**data) != MP_MAP) { - tnt_raise(ClientError, errcode, tt_cstr(space_name, name_len), - tt_sprintf("field %d is not map", - fieldno + TUPLE_INDEX_BASE)); - } - int count = mp_decode_map(data); - *field = field_def_default; - bool is_action_missing = true; - uint32_t action_literal_len = strlen("nullable_action"); - for (int i = 0; i < count; ++i) { - if (mp_typeof(**data) != MP_STR) { - tnt_raise(ClientError, errcode, - tt_cstr(space_name, name_len), - tt_sprintf("field %d format is not map"\ - " with string keys", - fieldno + TUPLE_INDEX_BASE)); - } - uint32_t key_len; - const char *key = mp_decode_str(data, &key_len); - if (opts_parse_key(field, field_def_reg, key, key_len, data, - ER_WRONG_SPACE_FORMAT, - fieldno + TUPLE_INDEX_BASE, region, - true) != 0) - diag_raise(); - if (is_action_missing && - key_len == action_literal_len && - memcmp(key, "nullable_action", action_literal_len) == 0) - is_action_missing = false; - } - if (is_action_missing) { - field->nullable_action = field->is_nullable ? - ON_CONFLICT_ACTION_NONE - : ON_CONFLICT_ACTION_DEFAULT; - } - if (field->name == NULL) { - tnt_raise(ClientError, errcode, tt_cstr(space_name, name_len), - tt_sprintf("field %d name is not specified", - fieldno + TUPLE_INDEX_BASE)); - } - size_t field_name_len = strlen(field->name); - if (field_name_len > BOX_NAME_MAX) { - tnt_raise(ClientError, errcode, tt_cstr(space_name, name_len), - tt_sprintf("field %d name is too long", - fieldno + TUPLE_INDEX_BASE)); - } - identifier_check_xc(field->name, field_name_len); - if (field->type == field_type_MAX) { - tnt_raise(ClientError, errcode, tt_cstr(space_name, name_len), - tt_sprintf("field %d has unknown field type", - fieldno + TUPLE_INDEX_BASE)); - } - if (field->nullable_action == on_conflict_action_MAX) { - tnt_raise(ClientError, errcode, tt_cstr(space_name, name_len), - tt_sprintf("field %d has unknown field on conflict " - "nullable action", - fieldno + TUPLE_INDEX_BASE)); - } - if (!((field->is_nullable && field->nullable_action == - ON_CONFLICT_ACTION_NONE) - || (!field->is_nullable - && field->nullable_action != ON_CONFLICT_ACTION_NONE))) { - tnt_raise(ClientError, errcode, tt_cstr(space_name, name_len), - tt_sprintf("field %d has conflicting nullability and " - "nullable action properties", fieldno + - TUPLE_INDEX_BASE)); - } - if (field->coll_id != COLL_NONE && - field->type != FIELD_TYPE_STRING && - field->type != FIELD_TYPE_SCALAR && - field->type != FIELD_TYPE_ANY) { - tnt_raise(ClientError, errcode, tt_cstr(space_name, name_len), - tt_sprintf("collation is reasonable only for " - "string, scalar and any fields")); - } - - const char *dv = field->default_value; - if (dv != NULL) { - field->default_value_expr = sql_expr_compile(sql_get(), dv, - strlen(dv)); - if (field->default_value_expr == NULL) - diag_raise(); - } -} - -/** - * Decode MessagePack array of fields. - * @param data MessagePack array of fields. - * @param[out] out_count Length of a result array. - * @param space_name Space name to use in error messages. - * @param errcode Errcode for client errors. - * @param region Region to allocate result array. - * - * @retval Array of fields. - */ -static struct field_def * -space_format_decode(const char *data, uint32_t *out_count, - const char *space_name, uint32_t name_len, - uint32_t errcode, struct region *region) -{ - /* Type is checked by _space format. */ - assert(mp_typeof(*data) == MP_ARRAY); - uint32_t count = mp_decode_array(&data); - *out_count = count; - if (count == 0) - return NULL; - size_t size = count * sizeof(struct field_def); - struct field_def *region_defs = - (struct field_def *) region_alloc_xc(region, size); - /* - * Nullify to prevent a case when decoding will fail in - * the middle and space_def_destroy_fields() below will - * work with garbage pointers. - */ - memset(region_defs, 0, size); - auto fields_guard = make_scoped_guard([=] { - space_def_destroy_fields(region_defs, count); - }); - for (uint32_t i = 0; i < count; ++i) { - field_def_decode(®ion_defs[i], &data, space_name, name_len, - errcode, i, region); - } - fields_guard.is_active = false; - return region_defs; -} - -/** * Fill space_def structure from struct tuple. */ static struct space_def * @@ -510,6 +313,8 @@ space_def_new_from_tuple(struct tuple *tuple, uint32_t errcode, MP_ARRAY); fields = space_format_decode(format, &field_count, name, name_len, errcode, region); + if (field_count != 0 && fields == NULL) + diag_raise(); auto fields_guard = make_scoped_guard([=] { space_def_destroy_fields(fields, field_count); }); diff --git a/src/box/index_def.c b/src/box/index_def.c index 45c74d9..e0d54c0 100644 --- a/src/box/index_def.c +++ b/src/box/index_def.c @@ -309,3 +309,63 @@ index_def_is_valid(struct index_def *index_def, const char *space_name) } return true; } + +int +index_opts_decode(struct index_opts *opts, const char *map, + struct region *region) +{ + index_opts_create(opts); + if (opts_decode(opts, index_opts_reg, &map, ER_WRONG_INDEX_OPTIONS, + BOX_INDEX_FIELD_OPTS, region) != 0) { + return -1; + } + if (opts->distance == rtree_index_distance_type_MAX) { + diag_set(ClientError, ER_WRONG_INDEX_OPTIONS, + BOX_INDEX_FIELD_OPTS, "distance must be either "\ + "'euclid' or 'manhattan'"); + return -1; + } + if (opts->sql != NULL) { + char *sql = strdup(opts->sql); + if (sql == NULL) { + opts->sql = NULL; + diag_set(OutOfMemory, strlen(opts->sql) + 1, "strdup", + "sql"); + return -1; + } + opts->sql = sql; + } + if (opts->range_size <= 0) { + diag_set(ClientError, ER_WRONG_INDEX_OPTIONS, + BOX_INDEX_FIELD_OPTS, + "range_size must be greater than 0"); + return -1; + } + if (opts->page_size <= 0 || opts->page_size > opts->range_size) { + diag_set(ClientError, ER_WRONG_INDEX_OPTIONS, + BOX_INDEX_FIELD_OPTS, + "page_size must be greater than 0 and " + "less than or equal to range_size"); + return -1; + } + if (opts->run_count_per_level <= 0) { + diag_set(ClientError, ER_WRONG_INDEX_OPTIONS, + BOX_INDEX_FIELD_OPTS, + "run_count_per_level must be greater than 0"); + return -1; + } + if (opts->run_size_ratio <= 1) { + diag_set(ClientError, ER_WRONG_INDEX_OPTIONS, + BOX_INDEX_FIELD_OPTS, + "run_size_ratio must be greater than 1"); + return -1; + } + if (opts->bloom_fpr <= 0 || opts->bloom_fpr > 1) { + diag_set(ClientError, ER_WRONG_INDEX_OPTIONS, + BOX_INDEX_FIELD_OPTS, + "bloom_fpr must be greater than 0 and " + "less than or equal to 1"); + return -1; + } + return 0; +} diff --git a/src/box/index_def.h b/src/box/index_def.h index 48a7820..4c3625b 100644 --- a/src/box/index_def.h +++ b/src/box/index_def.h @@ -358,6 +358,14 @@ index_def_cmp(const struct index_def *key1, const struct index_def *key2); bool index_def_is_valid(struct index_def *index_def, const char *space_name); +/** + * Fill index_opts structure from opts field in tuple of space _index + * Throw an error is unrecognized option. + */ +int +index_opts_decode(struct index_opts *opts, const char *map, + struct region *region); + #if defined(__cplusplus) } /* extern "C" */ diff --git a/src/box/space_def.c b/src/box/space_def.c index f5ca0b5..3b31727 100644 --- a/src/box/space_def.c +++ b/src/box/space_def.c @@ -34,6 +34,10 @@ #include "error.h" #include "sql.h" #include "msgpuck.h" +#include "tuple_format.h" +#include "schema_def.h" +#include "identifier.h" +#include "small/region.h" /** * Make checks from msgpack. @@ -351,3 +355,149 @@ error: sql_expr_list_delete(db, checks); return -1; } + +/** + * Decode field definition from MessagePack map. Format: + * {name: <string>, type: <string>}. Type is optional. + * @param[out] field Field to decode to. + * @param data MessagePack map to decode. + * @param space_name Name of a space, from which the field is got. + * Used in error messages. + * @param name_len Length of @a space_name. + * @param errcode Error code to use for client errors. Either + * create or modify space errors. + * @param fieldno Field number to decode. Used in error messages. + * @param region Region to allocate field name. + */ +static inline int +field_def_decode(struct field_def *field, const char **data, + const char *space_name, uint32_t name_len, + uint32_t errcode, uint32_t fieldno, struct region *region) +{ + if (mp_typeof(**data) != MP_MAP) { + diag_set(ClientError, errcode, tt_cstr(space_name, name_len), + tt_sprintf("field %d is not map", + fieldno + TUPLE_INDEX_BASE)); + return -1; + } + int count = mp_decode_map(data); + *field = field_def_default; + bool is_action_missing = true; + uint32_t action_literal_len = strlen("nullable_action"); + for (int i = 0; i < count; ++i) { + if (mp_typeof(**data) != MP_STR) { + diag_set(ClientError, errcode, + tt_cstr(space_name, name_len), + tt_sprintf("field %d format is not map"\ + " with string keys", + fieldno + TUPLE_INDEX_BASE)); + return -1; + } + uint32_t key_len; + const char *key = mp_decode_str(data, &key_len); + if (opts_parse_key(field, field_def_reg, key, key_len, data, + ER_WRONG_SPACE_FORMAT, + fieldno + TUPLE_INDEX_BASE, region, + true) != 0) + return -1; + if (is_action_missing && + key_len == action_literal_len && + memcmp(key, "nullable_action", action_literal_len) == 0) + is_action_missing = false; + } + if (is_action_missing) { + field->nullable_action = field->is_nullable ? + ON_CONFLICT_ACTION_NONE + : ON_CONFLICT_ACTION_DEFAULT; + } + if (field->name == NULL) { + diag_set(ClientError, errcode, tt_cstr(space_name, name_len), + tt_sprintf("field %d name is not specified", + fieldno + TUPLE_INDEX_BASE)); + return -1; + } + size_t field_name_len = strlen(field->name); + if (field_name_len > BOX_NAME_MAX) { + diag_set(ClientError, errcode, tt_cstr(space_name, name_len), + tt_sprintf("field %d name is too long", + fieldno + TUPLE_INDEX_BASE)); + return -1; + } + if(identifier_check(field->name, field_name_len)) + return -1; + if (field->type == field_type_MAX) { + diag_set(ClientError, errcode, tt_cstr(space_name, name_len), + tt_sprintf("field %d has unknown field type", + fieldno + TUPLE_INDEX_BASE)); + return -1; + } + if (field->nullable_action == on_conflict_action_MAX) { + diag_set(ClientError, errcode, tt_cstr(space_name, name_len), + tt_sprintf("field %d has unknown field on conflict " + "nullable action", + fieldno + TUPLE_INDEX_BASE)); + return -1; + } + if (!((field->is_nullable && field->nullable_action == + ON_CONFLICT_ACTION_NONE) + || (!field->is_nullable + && field->nullable_action != ON_CONFLICT_ACTION_NONE))) { + diag_set(ClientError, errcode, tt_cstr(space_name, name_len), + tt_sprintf("field %d has conflicting nullability and " + "nullable action properties", fieldno + + TUPLE_INDEX_BASE)); + return -1; + } + if (field->coll_id != COLL_NONE && + field->type != FIELD_TYPE_STRING && + field->type != FIELD_TYPE_SCALAR && + field->type != FIELD_TYPE_ANY) { + diag_set(ClientError, errcode, tt_cstr(space_name, name_len), + tt_sprintf("collation is reasonable only for " + "string, scalar and any fields")); + return -1; + } + + const char *dv = field->default_value; + if (dv != NULL) { + field->default_value_expr = sql_expr_compile(sql_get(), dv, + strlen(dv)); + if (field->default_value_expr == NULL) + return -1; + } + return 0; +} + +struct field_def * +space_format_decode(const char *data, uint32_t *out_count, + const char *space_name, uint32_t name_len, + uint32_t errcode, struct region *region) +{ + /* Type is checked by _space format. */ + assert(mp_typeof(*data) == MP_ARRAY); + uint32_t count = mp_decode_array(&data); + *out_count = count; + if (count == 0) + return NULL; + size_t size = count * sizeof(struct field_def); + struct field_def *region_defs = + (struct field_def *) region_alloc(region, size); + if(region_defs == NULL) { + diag_set(OutOfMemory, size, "region", "new slab"); + return NULL; + } + /* + * Nullify to prevent a case when decoding will fail in + * the middle and space_def_destroy_fields() below will + * work with garbage pointers. + */ + memset(region_defs, 0, size); + for (uint32_t i = 0; i < count; ++i) { + if(field_def_decode(®ion_defs[i], &data, space_name, + name_len, errcode, i, region) != 0) { + space_def_destroy_fields(region_defs, count); + return NULL; + } + } + return region_defs; +} diff --git a/src/box/space_def.h b/src/box/space_def.h index 0d1e902..16188c6 100644 --- a/src/box/space_def.h +++ b/src/box/space_def.h @@ -182,6 +182,21 @@ space_def_sizeof(uint32_t name_len, const struct field_def *fields, uint32_t field_count, uint32_t *names_offset, uint32_t *fields_offset, uint32_t *def_expr_offset); +/** + * Decode MessagePack array of fields. + * @param data MessagePack array of fields. + * @param[out] out_count Length of a result array. + * @param space_name Space name to use in error messages. + * @param errcode Errcode for client errors. + * @param region Region to allocate result array. + * + * @retval Array of fields. + */ +struct field_def * +space_format_decode(const char *data, uint32_t *out_count, + const char *space_name, uint32_t name_len, + uint32_t errcode, struct region *region); + #if defined(__cplusplus) } /* extern "C" */ -- 2.7.4
next prev parent reply other threads:[~2018-07-12 11:16 UTC|newest] Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top 2018-07-12 11:16 [tarantool-patches] [PATCH 0/5] Expose ephemeral spaces into Lua imeevma 2018-07-12 11:16 ` [tarantool-patches] [PATCH 1/5] Create new methods for ephemeral spaces imeevma 2018-07-13 16:32 ` [tarantool-patches] " Vladislav Shpilevoy 2018-07-12 11:16 ` imeevma [this message] 2018-07-13 16:32 ` [tarantool-patches] Re: [PATCH 2/5] Move some decode functions from alter.cc Vladislav Shpilevoy 2018-07-12 11:16 ` [tarantool-patches] [PATCH 3/5] Ephemeral space creation and deletion in Lua imeevma 2018-07-13 16:32 ` [tarantool-patches] " Vladislav Shpilevoy 2018-07-12 11:16 ` [tarantool-patches] [PATCH 4/5] Primary index for ephemeral spaces imeevma 2018-07-13 16:32 ` [tarantool-patches] " Vladislav Shpilevoy 2018-07-12 11:16 ` [tarantool-patches] [PATCH 5/5] Methods for ephemeral space and its index imeevma 2018-07-13 16:32 ` [tarantool-patches] " Vladislav Shpilevoy 2018-07-12 11:30 ` [tarantool-patches] Re: [PATCH 0/5] Expose ephemeral spaces into Lua Imeev Mergen
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=b8bb5caad710e629ee5b2b4a99e543d17f5706b1.1531393821.git.imeevma@gmail.com \ --to=imeevma@tarantool.org \ --cc=tarantool-patches@freelists.org \ --subject='Re: [tarantool-patches] [PATCH 2/5] Move some decode functions from alter.cc' \ /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