[PATCH 06/13] memtx: don't store pointers to index internals in iterator

Vladimir Davydov vdavydov.dev at gmail.com
Sat Aug 10 13:03:33 MSK 2019


It's pointless as we can always access the index via iterator->index.
---
 src/box/memtx_bitset.c | 10 ++-----
 src/box/memtx_hash.c   | 20 ++++++-------
 src/box/memtx_tree.c   | 67 +++++++++++++++++++++++-------------------
 3 files changed, 49 insertions(+), 48 deletions(-)

diff --git a/src/box/memtx_bitset.c b/src/box/memtx_bitset.c
index 59bc9642..67eaf6fd 100644
--- a/src/box/memtx_bitset.c
+++ b/src/box/memtx_bitset.c
@@ -168,9 +168,6 @@ value_to_tuple(size_t value)
 struct bitset_index_iterator {
 	struct iterator base; /* Must be the first member. */
 	struct tt_bitset_iterator bitset_it;
-#ifndef OLD_GOOD_BITSET
-	struct memtx_bitset_index *bitset_index;
-#endif /* #ifndef OLD_GOOD_BITSET */
 	/** Memory pool the iterator was allocated from. */
 	struct mempool *pool;
 };
@@ -208,7 +205,9 @@ bitset_index_iterator_next(struct iterator *iterator, struct tuple **ret)
 	}
 
 #ifndef OLD_GOOD_BITSET
-	*ret = memtx_bitset_index_value_to_tuple(it->bitset_index, value);
+	struct memtx_bitset_index *index =
+		(struct memtx_bitset_index *)iterator->index;
+	*ret = memtx_bitset_index_value_to_tuple(index, value);
 #else /* #ifndef OLD_GOOD_BITSET */
 	*ret = value_to_tuple(value);
 #endif /* #ifndef OLD_GOOD_BITSET */
@@ -348,9 +347,6 @@ memtx_bitset_index_create_iterator(struct index *base, enum iterator_type type,
 	it->base.free = bitset_index_iterator_free;
 
 	tt_bitset_iterator_create(&it->bitset_it, realloc);
-#ifndef OLD_GOOD_BITSET
-	it->bitset_index = index;
-#endif
 	const void *bitset_key = NULL;
 	uint32_t bitset_key_size = 0;
 
diff --git a/src/box/memtx_hash.c b/src/box/memtx_hash.c
index 3174ae2c..e30170d1 100644
--- a/src/box/memtx_hash.c
+++ b/src/box/memtx_hash.c
@@ -83,7 +83,6 @@ struct memtx_hash_index {
 
 struct hash_iterator {
 	struct iterator base; /* Must be the first member. */
-	struct light_index_core *hash_table;
 	struct light_index_iterator iterator;
 	/** Memory pool the iterator was allocated from. */
 	struct mempool *pool;
@@ -106,7 +105,8 @@ hash_iterator_ge(struct iterator *ptr, struct tuple **ret)
 {
 	assert(ptr->free == hash_iterator_free);
 	struct hash_iterator *it = (struct hash_iterator *) ptr;
-	struct tuple **res = light_index_iterator_get_and_next(it->hash_table,
+	struct memtx_hash_index *index = (struct memtx_hash_index *)ptr->index;
+	struct tuple **res = light_index_iterator_get_and_next(&index->hash_table,
 							       &it->iterator);
 	*ret = res != NULL ? *res : NULL;
 	return 0;
@@ -118,10 +118,11 @@ hash_iterator_gt(struct iterator *ptr, struct tuple **ret)
 	assert(ptr->free == hash_iterator_free);
 	ptr->next = hash_iterator_ge;
 	struct hash_iterator *it = (struct hash_iterator *) ptr;
-	struct tuple **res = light_index_iterator_get_and_next(it->hash_table,
+	struct memtx_hash_index *index = (struct memtx_hash_index *)ptr->index;
+	struct tuple **res = light_index_iterator_get_and_next(&index->hash_table,
 							       &it->iterator);
 	if (res != NULL)
-		res = light_index_iterator_get_and_next(it->hash_table,
+		res = light_index_iterator_get_and_next(&index->hash_table,
 							&it->iterator);
 	*ret = res != NULL ? *res : NULL;
 	return 0;
@@ -364,27 +365,26 @@ memtx_hash_index_create_iterator(struct index *base, enum iterator_type type,
 	iterator_create(&it->base, base);
 	it->pool = &memtx->iterator_pool;
 	it->base.free = hash_iterator_free;
-	it->hash_table = &index->hash_table;
-	light_index_iterator_begin(it->hash_table, &it->iterator);
+	light_index_iterator_begin(&index->hash_table, &it->iterator);
 
 	switch (type) {
 	case ITER_GT:
 		if (part_count != 0) {
-			light_index_iterator_key(it->hash_table, &it->iterator,
+			light_index_iterator_key(&index->hash_table, &it->iterator,
 					key_hash(key, base->def->key_def), key);
 			it->base.next = hash_iterator_gt;
 		} else {
-			light_index_iterator_begin(it->hash_table, &it->iterator);
+			light_index_iterator_begin(&index->hash_table, &it->iterator);
 			it->base.next = hash_iterator_ge;
 		}
 		break;
 	case ITER_ALL:
-		light_index_iterator_begin(it->hash_table, &it->iterator);
+		light_index_iterator_begin(&index->hash_table, &it->iterator);
 		it->base.next = hash_iterator_ge;
 		break;
 	case ITER_EQ:
 		assert(part_count > 0);
-		light_index_iterator_key(it->hash_table, &it->iterator,
+		light_index_iterator_key(&index->hash_table, &it->iterator,
 				key_hash(key, base->def->key_def), key);
 		it->base.next = hash_iterator_eq;
 		break;
diff --git a/src/box/memtx_tree.c b/src/box/memtx_tree.c
index 084b4aff..aeba2ba3 100644
--- a/src/box/memtx_tree.c
+++ b/src/box/memtx_tree.c
@@ -134,8 +134,6 @@ memtx_tree_qcompare(const void* a, const void *b, void *c)
 /* {{{ MemtxTree Iterators ****************************************/
 struct tree_iterator {
 	struct iterator base;
-	const struct memtx_tree *tree;
-	struct index_def *index_def;
 	struct memtx_tree_iterator tree_iterator;
 	enum iterator_type type;
 	struct memtx_tree_key_data key_data;
@@ -179,20 +177,21 @@ tree_iterator_dummie(struct iterator *iterator, struct tuple **ret)
 static int
 tree_iterator_next(struct iterator *iterator, struct tuple **ret)
 {
+	struct memtx_tree_index *index =
+		(struct memtx_tree_index *)iterator->index;
 	struct tree_iterator *it = tree_iterator(iterator);
 	assert(it->current.tuple != NULL);
 	struct memtx_tree_data *check =
-		memtx_tree_iterator_get_elem(it->tree, &it->tree_iterator);
+		memtx_tree_iterator_get_elem(&index->tree, &it->tree_iterator);
 	if (check == NULL || !memtx_tree_data_is_equal(check, &it->current)) {
-		it->tree_iterator =
-			memtx_tree_upper_bound_elem(it->tree, it->current,
-						    NULL);
+		it->tree_iterator = memtx_tree_upper_bound_elem(&index->tree,
+								it->current, NULL);
 	} else {
-		memtx_tree_iterator_next(it->tree, &it->tree_iterator);
+		memtx_tree_iterator_next(&index->tree, &it->tree_iterator);
 	}
 	tuple_unref(it->current.tuple);
 	struct memtx_tree_data *res =
-		memtx_tree_iterator_get_elem(it->tree, &it->tree_iterator);
+		memtx_tree_iterator_get_elem(&index->tree, &it->tree_iterator);
 	if (res == NULL) {
 		iterator->next = tree_iterator_dummie;
 		it->current.tuple = NULL;
@@ -208,18 +207,20 @@ tree_iterator_next(struct iterator *iterator, struct tuple **ret)
 static int
 tree_iterator_prev(struct iterator *iterator, struct tuple **ret)
 {
+	struct memtx_tree_index *index =
+		(struct memtx_tree_index *)iterator->index;
 	struct tree_iterator *it = tree_iterator(iterator);
 	assert(it->current.tuple != NULL);
 	struct memtx_tree_data *check =
-		memtx_tree_iterator_get_elem(it->tree, &it->tree_iterator);
+		memtx_tree_iterator_get_elem(&index->tree, &it->tree_iterator);
 	if (check == NULL || !memtx_tree_data_is_equal(check, &it->current)) {
-		it->tree_iterator =
-			memtx_tree_lower_bound_elem(it->tree, it->current, NULL);
+		it->tree_iterator = memtx_tree_lower_bound_elem(&index->tree,
+								it->current, NULL);
 	}
-	memtx_tree_iterator_prev(it->tree, &it->tree_iterator);
+	memtx_tree_iterator_prev(&index->tree, &it->tree_iterator);
 	tuple_unref(it->current.tuple);
 	struct memtx_tree_data *res =
-		memtx_tree_iterator_get_elem(it->tree, &it->tree_iterator);
+		memtx_tree_iterator_get_elem(&index->tree, &it->tree_iterator);
 	if (!res) {
 		iterator->next = tree_iterator_dummie;
 		it->current.tuple = NULL;
@@ -235,26 +236,28 @@ tree_iterator_prev(struct iterator *iterator, struct tuple **ret)
 static int
 tree_iterator_next_equal(struct iterator *iterator, struct tuple **ret)
 {
+	struct memtx_tree_index *index =
+		(struct memtx_tree_index *)iterator->index;
 	struct tree_iterator *it = tree_iterator(iterator);
 	assert(it->current.tuple != NULL);
 	struct memtx_tree_data *check =
-		memtx_tree_iterator_get_elem(it->tree, &it->tree_iterator);
+		memtx_tree_iterator_get_elem(&index->tree, &it->tree_iterator);
 	if (check == NULL || !memtx_tree_data_is_equal(check, &it->current)) {
-		it->tree_iterator =
-			memtx_tree_upper_bound_elem(it->tree, it->current, NULL);
+		it->tree_iterator = memtx_tree_upper_bound_elem(&index->tree,
+								it->current, NULL);
 	} else {
-		memtx_tree_iterator_next(it->tree, &it->tree_iterator);
+		memtx_tree_iterator_next(&index->tree, &it->tree_iterator);
 	}
 	tuple_unref(it->current.tuple);
 	struct memtx_tree_data *res =
-		memtx_tree_iterator_get_elem(it->tree, &it->tree_iterator);
+		memtx_tree_iterator_get_elem(&index->tree, &it->tree_iterator);
 	/* Use user key def to save a few loops. */
 	if (res == NULL ||
 	    tuple_compare_with_key(res->tuple, res->hint,
 				   it->key_data.key,
 				   it->key_data.part_count,
 				   it->key_data.hint,
-				   it->index_def->key_def) != 0) {
+				   index->base.def->key_def) != 0) {
 		iterator->next = tree_iterator_dummie;
 		it->current.tuple = NULL;
 		*ret = NULL;
@@ -269,25 +272,27 @@ tree_iterator_next_equal(struct iterator *iterator, struct tuple **ret)
 static int
 tree_iterator_prev_equal(struct iterator *iterator, struct tuple **ret)
 {
+	struct memtx_tree_index *index =
+		(struct memtx_tree_index *)iterator->index;
 	struct tree_iterator *it = tree_iterator(iterator);
 	assert(it->current.tuple != NULL);
 	struct memtx_tree_data *check =
-		memtx_tree_iterator_get_elem(it->tree, &it->tree_iterator);
+		memtx_tree_iterator_get_elem(&index->tree, &it->tree_iterator);
 	if (check == NULL || !memtx_tree_data_is_equal(check, &it->current)) {
-		it->tree_iterator =
-			memtx_tree_lower_bound_elem(it->tree, it->current, NULL);
+		it->tree_iterator = memtx_tree_lower_bound_elem(&index->tree,
+								it->current, NULL);
 	}
-	memtx_tree_iterator_prev(it->tree, &it->tree_iterator);
+	memtx_tree_iterator_prev(&index->tree, &it->tree_iterator);
 	tuple_unref(it->current.tuple);
 	struct memtx_tree_data *res =
-		memtx_tree_iterator_get_elem(it->tree, &it->tree_iterator);
+		memtx_tree_iterator_get_elem(&index->tree, &it->tree_iterator);
 	/* Use user key def to save a few loops. */
 	if (res == NULL ||
 	    tuple_compare_with_key(res->tuple, res->hint,
 				   it->key_data.key,
 				   it->key_data.part_count,
 				   it->key_data.hint,
-				   it->index_def->key_def) != 0) {
+				   index->base.def->key_def) != 0) {
 		iterator->next = tree_iterator_dummie;
 		it->current.tuple = NULL;
 		*ret = NULL;
@@ -331,9 +336,11 @@ static int
 tree_iterator_start(struct iterator *iterator, struct tuple **ret)
 {
 	*ret = NULL;
+	struct memtx_tree_index *index =
+		(struct memtx_tree_index *)iterator->index;
 	struct tree_iterator *it = tree_iterator(iterator);
 	it->base.next = tree_iterator_dummie;
-	const struct memtx_tree *tree = it->tree;
+	struct memtx_tree *tree = &index->tree;
 	enum iterator_type type = it->type;
 	bool exact = false;
 	assert(it->current.tuple == NULL);
@@ -369,12 +376,12 @@ tree_iterator_start(struct iterator *iterator, struct tuple **ret)
 			 * iterator_next call will convert the iterator to the
 			 * last position in the tree, that's what we need.
 			 */
-			memtx_tree_iterator_prev(it->tree, &it->tree_iterator);
+			memtx_tree_iterator_prev(tree, &it->tree_iterator);
 		}
 	}
 
-	struct memtx_tree_data *res =
-		memtx_tree_iterator_get_elem(it->tree, &it->tree_iterator);
+	struct memtx_tree_data *res = memtx_tree_iterator_get_elem(tree,
+							&it->tree_iterator);
 	if (!res)
 		return 0;
 	*ret = res->tuple;
@@ -1002,8 +1009,6 @@ memtx_tree_index_create_iterator(struct index *base, enum iterator_type type,
 	it->key_data.key = key;
 	it->key_data.part_count = part_count;
 	it->key_data.hint = key_hint(key, part_count, cmp_def);
-	it->index_def = base->def;
-	it->tree = &index->tree;
 	it->tree_iterator = memtx_tree_invalid_iterator();
 	it->current.tuple = NULL;
 	return (struct iterator *)it;
-- 
2.20.1




More information about the Tarantool-patches mailing list