[PATCH v6 3/4] box: manage format fields with JSON tree class
Vladimir Davydov
vdavydov.dev at gmail.com
Tue Dec 11 12:52:48 MSK 2018
On Thu, Dec 06, 2018 at 11:42:30AM +0300, Kirill Shcherbatov wrote:
> As we going to work with format fields in a unified way, we
> started to use the tree JSON class for working with first-level
> format fields.
>
> Needed for #1012
> ---
> src/box/sql.c | 18 +++---
> src/box/sql/build.c | 5 +-
> src/box/tuple.c | 10 ++--
> src/box/tuple_format.c | 122 +++++++++++++++++++++++++++++------------
> src/box/tuple_format.h | 49 ++++++++++++++---
> src/box/vy_stmt.c | 4 +-
> 6 files changed, 148 insertions(+), 60 deletions(-)
Pushed to 2.1 with minor changes:
diff --git a/src/box/tuple_format.c b/src/box/tuple_format.c
index eeb68f5d..3af39a37 100644
--- a/src/box/tuple_format.c
+++ b/src/box/tuple_format.c
@@ -41,17 +41,18 @@ static uint32_t formats_size = 0, formats_capacity = 0;
static struct tuple_field *
tuple_field_new(void)
{
- struct tuple_field *ret = calloc(1, sizeof(struct tuple_field));
- if (ret == NULL) {
+ struct tuple_field *field = calloc(1, sizeof(struct tuple_field));
+ if (field == NULL) {
diag_set(OutOfMemory, sizeof(struct tuple_field), "malloc",
- "ret");
+ "tuple field");
return NULL;
}
- ret->type = FIELD_TYPE_ANY;
- ret->offset_slot = TUPLE_OFFSET_SLOT_NIL;
- ret->coll_id = COLL_NONE;
- ret->nullable_action = ON_CONFLICT_ACTION_NONE;
- return ret;
+ field->token.type = JSON_TOKEN_END;
+ field->type = FIELD_TYPE_ANY;
+ field->offset_slot = TUPLE_OFFSET_SLOT_NIL;
+ field->coll_id = COLL_NONE;
+ field->nullable_action = ON_CONFLICT_ACTION_NONE;
+ return field;
}
static void
@@ -198,7 +199,7 @@ tuple_format_create(struct tuple_format *format, struct key_def * const *keys,
}
assert(tuple_format_field(format, 0)->offset_slot ==
- TUPLE_OFFSET_SLOT_NIL);
+ TUPLE_OFFSET_SLOT_NIL);
size_t field_map_size = -current_slot * sizeof(uint32_t);
if (field_map_size > UINT16_MAX) {
/** tuple->data_offset is 16 bits */
@@ -256,12 +257,15 @@ tuple_format_deregister(struct tuple_format *format)
format->id = FORMAT_ID_NIL;
}
-/** Destroy field JSON tree and release allocated memory. */
-static inline void
-tuple_format_fields_destroy(struct tuple_format *format)
+/*
+ * Dismantle the tuple field tree attached to the format and free
+ * memory occupied by tuple fields.
+ */
+static void
+tuple_format_destroy_fields(struct tuple_format *format)
{
struct tuple_field *field, *tmp;
- json_tree_foreach_entry_safe(&format->fields.root, field,
+ json_tree_foreach_entry_safe(field, &format->fields.root,
struct tuple_field, token, tmp) {
json_tree_del(&format->fields, &field->token);
tuple_field_delete(field);
@@ -293,6 +297,8 @@ tuple_format_alloc(struct key_def * const *keys, uint16_t key_count,
return NULL;
}
if (json_tree_create(&format->fields) != 0) {
+ diag_set(OutOfMemory, 0, "json_lexer_create",
+ "tuple field tree");
free(format);
return NULL;
}
@@ -305,7 +311,7 @@ tuple_format_alloc(struct key_def * const *keys, uint16_t key_count,
if (json_tree_add(&format->fields, &format->fields.root,
&field->token) != 0) {
diag_set(OutOfMemory, 0, "json_tree_add",
- "&format->tree");
+ "tuple field tree entry");
tuple_field_delete(field);
goto error;
}
@@ -325,8 +331,8 @@ tuple_format_alloc(struct key_def * const *keys, uint16_t key_count,
format->exact_field_count = 0;
format->min_field_count = 0;
return format;
-error:;
- tuple_format_fields_destroy(format);
+error:
+ tuple_format_destroy_fields(format);
free(format);
return NULL;
}
@@ -335,7 +341,7 @@ error:;
static inline void
tuple_format_destroy(struct tuple_format *format)
{
- tuple_format_fields_destroy(format);
+ tuple_format_destroy_fields(format);
tuple_dictionary_unref(format->dict);
}
diff --git a/src/box/tuple_format.h b/src/box/tuple_format.h
index 68e9205b..94933780 100644
--- a/src/box/tuple_format.h
+++ b/src/box/tuple_format.h
@@ -184,8 +184,8 @@ struct tuple_format {
};
/**
- * Return a count of first level nodes correspond to tuple
- * fields.
+ * Return the number of top-level tuple fields defined by
+ * a given format.
*/
static inline uint32_t
tuple_format_field_count(const struct tuple_format *format)
@@ -195,8 +195,8 @@ tuple_format_field_count(const struct tuple_format *format)
}
/**
- * Get the first level node corresponding to tuple field by its
- * fieldno.
+ * Return meta information of a top-level tuple field given
+ * a format and a field index.
*/
static inline struct tuple_field *
tuple_format_field(struct tuple_format *format, uint32_t fieldno)
More information about the Tarantool-patches
mailing list