[PATCH v2 10/14] vinyl: zap vy_write_iterator->format

Vladimir Davydov vdavydov.dev at gmail.com
Wed Mar 13 11:52:56 MSK 2019


It's actually only needed to initialize disk streams so let's pass it
to vy_write_iterator_new_slice() instead.
---
 src/box/vinyl.c               |  6 +++---
 src/box/vy_run.c              | 11 ++++++++++-
 src/box/vy_scheduler.c        | 13 ++++++-------
 src/box/vy_write_iterator.c   | 25 ++++++++++---------------
 src/box/vy_write_iterator.h   |  9 ++++-----
 test/unit/vy_point_lookup.c   | 11 +++++------
 test/unit/vy_write_iterator.c |  3 +--
 7 files changed, 39 insertions(+), 39 deletions(-)

diff --git a/src/box/vinyl.c b/src/box/vinyl.c
index 0e908cc5..ced0efe5 100644
--- a/src/box/vinyl.c
+++ b/src/box/vinyl.c
@@ -3004,14 +3004,14 @@ vy_send_range(struct vy_join_ctx *ctx,
 	/* Create a write iterator. */
 	struct rlist fake_read_views;
 	rlist_create(&fake_read_views);
-	ctx->wi = vy_write_iterator_new(ctx->key_def, ctx->format,
-					true, true, &fake_read_views, NULL);
+	ctx->wi = vy_write_iterator_new(ctx->key_def, true, true,
+					&fake_read_views, NULL);
 	if (ctx->wi == NULL) {
 		rc = -1;
 		goto out;
 	}
 	rlist_foreach_entry(slice, &ctx->slices, in_join) {
-		rc = vy_write_iterator_new_slice(ctx->wi, slice);
+		rc = vy_write_iterator_new_slice(ctx->wi, slice, ctx->format);
 		if (rc != 0)
 			goto out_delete_wi;
 	}
diff --git a/src/box/vy_run.c b/src/box/vy_run.c
index 59ae906a..76b2f94a 100644
--- a/src/box/vy_run.c
+++ b/src/box/vy_run.c
@@ -2668,11 +2668,19 @@ vy_slice_stream_stop(struct vy_stmt_stream *virt_stream)
 	}
 }
 
+static void
+vy_slice_stream_close(struct vy_stmt_stream *virt_stream)
+{
+	assert(virt_stream->iface->close == vy_slice_stream_close);
+	struct vy_slice_stream *stream = (struct vy_slice_stream *)virt_stream;
+	tuple_format_unref(stream->format);
+}
+
 static const struct vy_stmt_stream_iface vy_slice_stream_iface = {
 	.start = vy_slice_stream_search,
 	.next = vy_slice_stream_next,
 	.stop = vy_slice_stream_stop,
-	.close = NULL,
+	.close = vy_slice_stream_close,
 };
 
 void
@@ -2690,5 +2698,6 @@ vy_slice_stream_open(struct vy_slice_stream *stream, struct vy_slice *slice,
 	stream->slice = slice;
 	stream->cmp_def = cmp_def;
 	stream->format = format;
+	tuple_format_ref(format);
 	stream->is_primary = is_primary;
 }
diff --git a/src/box/vy_scheduler.c b/src/box/vy_scheduler.c
index 91cf502b..437b95ac 100644
--- a/src/box/vy_scheduler.c
+++ b/src/box/vy_scheduler.c
@@ -1395,9 +1395,8 @@ vy_task_dump_new(struct vy_scheduler *scheduler, struct vy_worker *worker,
 	 */
 	struct vy_stmt_stream *wi;
 	bool is_last_level = (lsm->run_count == 0);
-	wi = vy_write_iterator_new(task->cmp_def, lsm->disk_format,
-				   lsm->index_id == 0, is_last_level,
-				   scheduler->read_views, NULL);
+	wi = vy_write_iterator_new(task->cmp_def, lsm->index_id == 0,
+				   is_last_level, scheduler->read_views, NULL);
 	if (wi == NULL)
 		goto err_wi;
 	rlist_foreach_entry(mem, &lsm->sealed, in_sealed) {
@@ -1670,9 +1669,8 @@ vy_task_compaction_new(struct vy_scheduler *scheduler, struct vy_worker *worker,
 
 	struct vy_stmt_stream *wi;
 	bool is_last_level = (range->compaction_priority == range->slice_count);
-	wi = vy_write_iterator_new(task->cmp_def, lsm->disk_format,
-				   lsm->index_id == 0, is_last_level,
-				   scheduler->read_views,
+	wi = vy_write_iterator_new(task->cmp_def, lsm->index_id == 0,
+				   is_last_level, scheduler->read_views,
 				   lsm->index_id > 0 ? NULL :
 				   &task->deferred_delete_handler);
 	if (wi == NULL)
@@ -1682,7 +1680,8 @@ vy_task_compaction_new(struct vy_scheduler *scheduler, struct vy_worker *worker,
 	int32_t dump_count = 0;
 	int n = range->compaction_priority;
 	rlist_foreach_entry(slice, &range->slices, in_range) {
-		if (vy_write_iterator_new_slice(wi, slice) != 0)
+		if (vy_write_iterator_new_slice(wi, slice,
+						lsm->disk_format) != 0)
 			goto err_wi_sub;
 		new_run->dump_lsn = MAX(new_run->dump_lsn,
 					slice->run->dump_lsn);
diff --git a/src/box/vy_write_iterator.c b/src/box/vy_write_iterator.c
index 8988daa3..36e43bc9 100644
--- a/src/box/vy_write_iterator.c
+++ b/src/box/vy_write_iterator.c
@@ -168,8 +168,6 @@ struct vy_write_iterator {
 	heap_t src_heap;
 	/** Index key definition used to store statements on disk. */
 	struct key_def *cmp_def;
-	/** Format to allocate new REPLACE and DELETE tuples from vy_run */
-	struct tuple_format *format;
 	/* There is no LSM tree level older than the one we're writing to. */
 	bool is_last_level;
 	/**
@@ -339,9 +337,8 @@ static const struct vy_stmt_stream_iface vy_slice_stream_iface;
  * @return the iterator or NULL on error (diag is set).
  */
 struct vy_stmt_stream *
-vy_write_iterator_new(struct key_def *cmp_def, struct tuple_format *format,
-		      bool is_primary, bool is_last_level,
-		      struct rlist *read_views,
+vy_write_iterator_new(struct key_def *cmp_def, bool is_primary,
+		      bool is_last_level, struct rlist *read_views,
 		      struct vy_deferred_delete_handler *handler)
 {
 	/*
@@ -378,8 +375,6 @@ vy_write_iterator_new(struct key_def *cmp_def, struct tuple_format *format,
 	vy_source_heap_create(&stream->src_heap);
 	rlist_create(&stream->src_list);
 	stream->cmp_def = cmp_def;
-	stream->format = format;
-	tuple_format_ref(stream->format);
 	stream->is_primary = is_primary;
 	stream->is_last_level = is_last_level;
 	stream->deferred_delete_handler = handler;
@@ -449,7 +444,6 @@ vy_write_iterator_close(struct vy_stmt_stream *vstream)
 	rlist_foreach_entry_safe(src, &stream->src_list, in_src_list, tmp)
 		vy_write_iterator_delete_src(stream, src);
 	vy_source_heap_destroy(&stream->src_heap);
-	tuple_format_unref(stream->format);
 	free(stream);
 }
 
@@ -474,14 +468,15 @@ vy_write_iterator_new_mem(struct vy_stmt_stream *vstream, struct vy_mem *mem)
  */
 NODISCARD int
 vy_write_iterator_new_slice(struct vy_stmt_stream *vstream,
-			    struct vy_slice *slice)
+			    struct vy_slice *slice,
+			    struct tuple_format *disk_format)
 {
 	struct vy_write_iterator *stream = (struct vy_write_iterator *)vstream;
 	struct vy_write_src *src = vy_write_iterator_new_src(stream);
 	if (src == NULL)
 		return -1;
 	vy_slice_stream_open(&src->slice_stream, slice, stream->cmp_def,
-			     stream->format, stream->is_primary);
+			     disk_format, stream->is_primary);
 	return 0;
 }
 
@@ -927,11 +922,11 @@ vy_read_view_merge(struct vy_write_iterator *stream, struct tuple *hint,
 		 * so as not to trigger optimization #5 on the next
 		 * compaction.
 		 */
-		uint32_t size;
-		const char *data = tuple_data_range(rv->tuple, &size);
-		struct tuple *copy = is_first_insert ?
-			vy_stmt_new_insert(stream->format, data, data + size) :
-			vy_stmt_new_replace(stream->format, data, data + size);
+		struct tuple *copy = vy_stmt_dup(rv->tuple);
+		if (is_first_insert)
+			vy_stmt_set_type(copy, IPROTO_INSERT);
+		else
+			vy_stmt_set_type(copy, IPROTO_REPLACE);
 		if (copy == NULL)
 			return -1;
 		vy_stmt_set_lsn(copy, vy_stmt_lsn(rv->tuple));
diff --git a/src/box/vy_write_iterator.h b/src/box/vy_write_iterator.h
index 4ae3815d..e217160e 100644
--- a/src/box/vy_write_iterator.h
+++ b/src/box/vy_write_iterator.h
@@ -239,7 +239,6 @@ struct vy_deferred_delete_handler {
  * Open an empty write iterator. To add sources to the iterator
  * use vy_write_iterator_add_* functions.
  * @param cmp_def - key definition for tuple compare.
- * @param format - dormat to allocate new REPLACE and DELETE tuples from vy_run.
  * @param LSM tree is_primary - set if this iterator is for a primary index.
  * @param is_last_level - there is no older level than the one we're writing to.
  * @param read_views - Opened read views.
@@ -249,9 +248,8 @@ struct vy_deferred_delete_handler {
  * @return the iterator or NULL on error (diag is set).
  */
 struct vy_stmt_stream *
-vy_write_iterator_new(struct key_def *cmp_def, struct tuple_format *format,
-		      bool is_primary, bool is_last_level,
-		      struct rlist *read_views,
+vy_write_iterator_new(struct key_def *cmp_def, bool is_primary,
+		      bool is_last_level, struct rlist *read_views,
 		      struct vy_deferred_delete_handler *handler);
 
 /**
@@ -267,7 +265,8 @@ vy_write_iterator_new_mem(struct vy_stmt_stream *stream, struct vy_mem *mem);
  */
 NODISCARD int
 vy_write_iterator_new_slice(struct vy_stmt_stream *stream,
-			    struct vy_slice *slice);
+			    struct vy_slice *slice,
+			    struct tuple_format *disk_format);
 
 #endif /* INCLUDES_TARANTOOL_BOX_VY_WRITE_STREAM_H */
 
diff --git a/test/unit/vy_point_lookup.c b/test/unit/vy_point_lookup.c
index 5f20d09b..38e9f403 100644
--- a/test/unit/vy_point_lookup.c
+++ b/test/unit/vy_point_lookup.c
@@ -191,9 +191,9 @@ test_basic()
 		tmpl_val.upsert_value = 8;
 		vy_mem_insert_template(run_mem, &tmpl_val);
 	}
-	struct vy_stmt_stream *write_stream
-		= vy_write_iterator_new(pk->cmp_def, pk->disk_format,
-					true, true, &read_views, NULL);
+	struct vy_stmt_stream *write_stream;
+	write_stream = vy_write_iterator_new(pk->cmp_def, true, true,
+					     &read_views, NULL);
 	vy_write_iterator_new_mem(write_stream, run_mem);
 	struct vy_run *run = vy_run_new(&run_env, 1);
 	isnt(run, NULL, "vy_run_new");
@@ -222,9 +222,8 @@ test_basic()
 		tmpl_val.upsert_value = 4;
 		vy_mem_insert_template(run_mem, &tmpl_val);
 	}
-	write_stream
-		= vy_write_iterator_new(pk->cmp_def, pk->disk_format,
-					true, true, &read_views, NULL);
+	write_stream = vy_write_iterator_new(pk->cmp_def, true, true,
+					     &read_views, NULL);
 	vy_write_iterator_new_mem(write_stream, run_mem);
 	run = vy_run_new(&run_env, 2);
 	isnt(run, NULL, "vy_run_new");
diff --git a/test/unit/vy_write_iterator.c b/test/unit/vy_write_iterator.c
index bb0eb8d3..ecbc6281 100644
--- a/test/unit/vy_write_iterator.c
+++ b/test/unit/vy_write_iterator.c
@@ -105,8 +105,7 @@ compare_write_iterator_results(const struct vy_stmt_template *content,
 	test_handler_create(&handler, mem->format);
 
 	struct vy_stmt_stream *wi;
-	wi = vy_write_iterator_new(key_def, mem->format, is_primary,
-				   is_last_level, &rv_list,
+	wi = vy_write_iterator_new(key_def, is_primary, is_last_level, &rv_list,
 				   is_primary ? &handler.base : NULL);
 	fail_if(wi == NULL);
 	fail_if(vy_write_iterator_new_mem(wi, mem) != 0);
-- 
2.11.0




More information about the Tarantool-patches mailing list