[PATCH v3 4/7] memtx: hide index implementation details from header

Kirill Shcherbatov kshcherbatov at tarantool.org
Fri Feb 22 18:42:29 MSK 2019


Refactored memtx_tree code so that memtx_tree.h, memtx_rtree.h,
memtx_bitset.h, memtx_hash.h contained only the signature of the
tree object constructor while all implementation details were in
corresponding *.c files.

Needed for #3961
---
 src/box/memtx_bitset.c | 17 +++++++++--
 src/box/memtx_bitset.h | 26 ++--------------
 src/box/memtx_hash.c   | 43 ++++++++++++++++++++++++--
 src/box/memtx_hash.h   | 44 +++------------------------
 src/box/memtx_rtree.c  | 12 ++++++--
 src/box/memtx_rtree.h  | 16 ++--------
 src/box/memtx_space.c  | 16 +++++-----
 src/box/memtx_tree.c   | 62 ++++++++++++++++++++++++++++++++++++--
 src/box/memtx_tree.h   | 68 ++----------------------------------------
 9 files changed, 147 insertions(+), 157 deletions(-)

diff --git a/src/box/memtx_bitset.c b/src/box/memtx_bitset.c
index 9dbc4141d..64259cf44 100644
--- a/src/box/memtx_bitset.c
+++ b/src/box/memtx_bitset.c
@@ -36,12 +36,25 @@
 
 #include "trivia/util.h"
 
+#include "bitset/index.h"
 #include "fiber.h"
+#include "index.h"
 #include "tuple.h"
 #include "memtx_engine.h"
 
+struct memtx_bitset_index {
+	struct index base;
+	struct tt_bitset_index index;
+#ifndef OLD_GOOD_BITSET
+	struct matras *id_to_tuple;
+	struct mh_bitset_index_t *tuple_to_id;
+	uint32_t spare_id;
+#endif /*#ifndef OLD_GOOD_BITSET*/
+};
+
 #ifndef OLD_GOOD_BITSET
 #include "small/matras.h"
+struct mh_bitset_index_t;
 
 struct bitset_hash_entry {
 	struct tuple *tuple;
@@ -491,7 +504,7 @@ static const struct index_vtab memtx_bitset_index_vtab = {
 	/* .end_build = */ generic_index_end_build,
 };
 
-struct memtx_bitset_index *
+struct index *
 memtx_bitset_index_new(struct memtx_engine *memtx, struct index_def *def)
 {
 	assert(def->iid > 0);
@@ -529,5 +542,5 @@ memtx_bitset_index_new(struct memtx_engine *memtx, struct index_def *def)
 #endif /* #ifndef OLD_GOOD_BITSET */
 
 	tt_bitset_index_create(&index->index, realloc);
-	return index;
+	return &index->base;
 }
diff --git a/src/box/memtx_bitset.h b/src/box/memtx_bitset.h
index 62971dd51..148d463d8 100644
--- a/src/box/memtx_bitset.h
+++ b/src/box/memtx_bitset.h
@@ -31,35 +31,15 @@
  * SUCH DAMAGE.
  */
 
-/**
- * @brief Index API wrapper for bitset_index
- * @see bitset/index.h
- */
-#include "index.h"
-#include "bitset/index.h"
-
 #if defined(__cplusplus)
 extern "C" {
 #endif /* defined(__cplusplus) */
 
+struct index;
+struct index_def;
 struct memtx_engine;
 
-#ifndef OLD_GOOD_BITSET
-struct matras;
-struct mh_bitset_index_t;
-#endif /*#ifndef OLD_GOOD_BITSET*/
-
-struct memtx_bitset_index {
-	struct index base;
-	struct tt_bitset_index index;
-#ifndef OLD_GOOD_BITSET
-	struct matras *id_to_tuple;
-	struct mh_bitset_index_t *tuple_to_id;
-	uint32_t spare_id;
-#endif /*#ifndef OLD_GOOD_BITSET*/
-};
-
-struct memtx_bitset_index *
+struct index *
 memtx_bitset_index_new(struct memtx_engine *memtx, struct index_def *def);
 
 #if defined(__cplusplus)
diff --git a/src/box/memtx_hash.c b/src/box/memtx_hash.c
index b35a52528..e7d64a9fb 100644
--- a/src/box/memtx_hash.c
+++ b/src/box/memtx_hash.c
@@ -31,6 +31,7 @@
 #include "memtx_hash.h"
 #include "say.h"
 #include "fiber.h"
+#include "index.h"
 #include "tuple.h"
 #include "memtx_engine.h"
 #include "space.h"
@@ -39,6 +40,44 @@
 
 #include <small/mempool.h>
 
+static inline bool
+memtx_hash_equal(struct tuple *tuple_a, struct tuple *tuple_b,
+		 struct key_def *key_def)
+{
+	return tuple_compare(tuple_a, tuple_b, key_def) == 0;
+}
+
+static inline bool
+memtx_hash_equal_key(struct tuple *tuple, const char *key,
+		     struct key_def *key_def)
+{
+	return tuple_compare_with_key(tuple, key, key_def->part_count,
+				      key_def) == 0;
+}
+
+#define LIGHT_NAME _index
+#define LIGHT_DATA_TYPE struct tuple *
+#define LIGHT_KEY_TYPE const char *
+#define LIGHT_CMP_ARG_TYPE struct key_def *
+#define LIGHT_EQUAL(a, b, c) memtx_hash_equal(a, b, c)
+#define LIGHT_EQUAL_KEY(a, b, c) memtx_hash_equal_key(a, b, c)
+
+#include "salad/light.h"
+
+#undef LIGHT_NAME
+#undef LIGHT_DATA_TYPE
+#undef LIGHT_KEY_TYPE
+#undef LIGHT_CMP_ARG_TYPE
+#undef LIGHT_EQUAL
+#undef LIGHT_EQUAL_KEY
+
+struct memtx_hash_index {
+	struct index base;
+	struct light_index_core hash_table;
+	struct memtx_gc_task gc_task;
+	struct light_index_iterator gc_iterator;
+};
+
 /* {{{ MemtxHash Iterators ****************************************/
 
 struct hash_iterator {
@@ -450,7 +489,7 @@ static const struct index_vtab memtx_hash_index_vtab = {
 	/* .end_build = */ generic_index_end_build,
 };
 
-struct memtx_hash_index *
+struct index *
 memtx_hash_index_new(struct memtx_engine *memtx, struct index_def *def)
 {
 	if (!mempool_is_initialized(&memtx->iterator_pool)) {
@@ -474,7 +513,7 @@ memtx_hash_index_new(struct memtx_engine *memtx, struct index_def *def)
 	light_index_create(&index->hash_table, MEMTX_EXTENT_SIZE,
 			   memtx_index_extent_alloc, memtx_index_extent_free,
 			   memtx, index->base.def->key_def);
-	return index;
+	return &index->base;
 }
 
 /* }}} */
diff --git a/src/box/memtx_hash.h b/src/box/memtx_hash.h
index 10663fcfc..2dba3f5c5 100644
--- a/src/box/memtx_hash.h
+++ b/src/box/memtx_hash.h
@@ -30,52 +30,16 @@
  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
-#include "index.h"
-#include "memtx_engine.h"
 
 #if defined(__cplusplus)
 extern "C" {
 #endif /* defined(__cplusplus) */
 
-static inline bool
-memtx_hash_equal(struct tuple *tuple_a, struct tuple *tuple_b,
-		 struct key_def *key_def)
-{
-	return tuple_compare(tuple_a, tuple_b, key_def) == 0;
-}
+struct index;
+struct index_def;
+struct memtx_engine;
 
-static inline bool
-memtx_hash_equal_key(struct tuple *tuple, const char *key,
-		     struct key_def *key_def)
-{
-	return tuple_compare_with_key(tuple, key, key_def->part_count,
-				      key_def) == 0;
-}
-
-#define LIGHT_NAME _index
-#define LIGHT_DATA_TYPE struct tuple *
-#define LIGHT_KEY_TYPE const char *
-#define LIGHT_CMP_ARG_TYPE struct key_def *
-#define LIGHT_EQUAL(a, b, c) memtx_hash_equal(a, b, c)
-#define LIGHT_EQUAL_KEY(a, b, c) memtx_hash_equal_key(a, b, c)
-
-#include "salad/light.h"
-
-#undef LIGHT_NAME
-#undef LIGHT_DATA_TYPE
-#undef LIGHT_KEY_TYPE
-#undef LIGHT_CMP_ARG_TYPE
-#undef LIGHT_EQUAL
-#undef LIGHT_EQUAL_KEY
-
-struct memtx_hash_index {
-	struct index base;
-	struct light_index_core hash_table;
-	struct memtx_gc_task gc_task;
-	struct light_index_iterator gc_iterator;
-};
-
-struct memtx_hash_index *
+struct index *
 memtx_hash_index_new(struct memtx_engine *memtx, struct index_def *def);
 
 #if defined(__cplusplus)
diff --git a/src/box/memtx_rtree.c b/src/box/memtx_rtree.c
index 9cb93f150..5d2901ad9 100644
--- a/src/box/memtx_rtree.c
+++ b/src/box/memtx_rtree.c
@@ -30,9 +30,11 @@
  */
 #include "memtx_rtree.h"
 
+#include <salad/rtree.h>
 #include <small/small.h>
 #include <small/mempool.h>
 
+#include "index.h"
 #include "errinj.h"
 #include "fiber.h"
 #include "trivia/util.h"
@@ -41,6 +43,12 @@
 #include "space.h"
 #include "memtx_engine.h"
 
+struct memtx_rtree_index {
+	struct index base;
+	unsigned dimension;
+	struct rtree tree;
+};
+
 /* {{{ Utilities. *************************************************/
 
 static inline int
@@ -333,7 +341,7 @@ static const struct index_vtab memtx_rtree_index_vtab = {
 	/* .end_build = */ generic_index_end_build,
 };
 
-struct memtx_rtree_index *
+struct index *
 memtx_rtree_index_new(struct memtx_engine *memtx, struct index_def *def)
 {
 	assert(def->iid > 0);
@@ -377,5 +385,5 @@ memtx_rtree_index_new(struct memtx_engine *memtx, struct index_def *def)
 	rtree_init(&index->tree, index->dimension, MEMTX_EXTENT_SIZE,
 		   memtx_index_extent_alloc, memtx_index_extent_free, memtx,
 		   distance_type);
-	return index;
+	return &index->base;
 }
diff --git a/src/box/memtx_rtree.h b/src/box/memtx_rtree.h
index 359aa898e..2941b805a 100644
--- a/src/box/memtx_rtree.h
+++ b/src/box/memtx_rtree.h
@@ -30,26 +30,16 @@
  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
-#include <stddef.h>
-#include <stdint.h>
-
-#include <salad/rtree.h>
-
-#include "index.h"
 
 #if defined(__cplusplus)
 extern "C" {
 #endif /* defined(__cplusplus) */
 
+struct index;
+struct index_def;
 struct memtx_engine;
 
-struct memtx_rtree_index {
-	struct index base;
-	unsigned dimension;
-	struct rtree tree;
-};
-
-struct memtx_rtree_index *
+struct index *
 memtx_rtree_index_new(struct memtx_engine *memtx, struct index_def *def);
 
 #if defined(__cplusplus)
diff --git a/src/box/memtx_space.c b/src/box/memtx_space.c
index 5f8cc98fd..92ec0b300 100644
--- a/src/box/memtx_space.c
+++ b/src/box/memtx_space.c
@@ -716,21 +716,21 @@ sequence_data_index_create_snapshot_iterator(struct index *index)
 static struct index *
 sequence_data_index_new(struct memtx_engine *memtx, struct index_def *def)
 {
-	struct memtx_hash_index *index = memtx_hash_index_new(memtx, def);
+	struct index *index = memtx_hash_index_new(memtx, def);
 	if (index == NULL)
 		return NULL;
 
 	static struct index_vtab vtab;
 	static bool vtab_initialized;
 	if (!vtab_initialized) {
-		vtab = *index->base.vtab;
+		vtab = *index->vtab;
 		vtab.create_snapshot_iterator =
 			sequence_data_index_create_snapshot_iterator;
 		vtab_initialized = true;
 	}
 
-	index->base.vtab = &vtab;
-	return &index->base;
+	index->vtab = &vtab;
+	return index;
 }
 
 static struct index *
@@ -751,13 +751,13 @@ memtx_space_create_index(struct space *space, struct index_def *index_def)
 
 	switch (index_def->type) {
 	case HASH:
-		return (struct index *)memtx_hash_index_new(memtx, index_def);
+		return memtx_hash_index_new(memtx, index_def);
 	case TREE:
-		return (struct index *)memtx_tree_index_new(memtx, index_def);
+		return memtx_tree_index_new(memtx, index_def);
 	case RTREE:
-		return (struct index *)memtx_rtree_index_new(memtx, index_def);
+		return memtx_rtree_index_new(memtx, index_def);
 	case BITSET:
-		return (struct index *)memtx_bitset_index_new(memtx, index_def);
+		return memtx_bitset_index_new(memtx, index_def);
 	default:
 		unreachable();
 		return NULL;
diff --git a/src/box/memtx_tree.c b/src/box/memtx_tree.c
index fe66427fc..07d20473f 100644
--- a/src/box/memtx_tree.c
+++ b/src/box/memtx_tree.c
@@ -39,6 +39,64 @@
 #include <third_party/qsort_arg.h>
 #include <small/mempool.h>
 
+/**
+ * Struct that is used as a key in BPS tree definition.
+ */
+struct memtx_tree_key_data {
+	/** Sequence of msgpacked search fields. */
+	const char *key;
+	/** Number of msgpacked search fields. */
+	uint32_t part_count;
+};
+
+/**
+ * BPS tree element vs key comparator.
+ * Defined in header in order to allow compiler to inline it.
+ * @param tuple - tuple to compare.
+ * @param key_data - key to compare with.
+ * @param def - key definition.
+ * @retval 0  if tuple == key in terms of def.
+ * @retval <0 if tuple < key in terms of def.
+ * @retval >0 if tuple > key in terms of def.
+ */
+static int
+memtx_tree_compare_key(const struct tuple *tuple,
+		       const struct memtx_tree_key_data *key_data,
+		       struct key_def *def)
+{
+	return tuple_compare_with_key(tuple, key_data->key,
+				      key_data->part_count, def);
+}
+
+#define BPS_TREE_NAME memtx_tree
+#define BPS_TREE_BLOCK_SIZE (512)
+#define BPS_TREE_EXTENT_SIZE MEMTX_EXTENT_SIZE
+#define BPS_TREE_COMPARE(a, b, arg) tuple_compare(a, b, arg)
+#define BPS_TREE_COMPARE_KEY(a, b, arg) memtx_tree_compare_key(a, b, arg)
+#define bps_tree_elem_t struct tuple *
+#define bps_tree_key_t struct memtx_tree_key_data *
+#define bps_tree_arg_t struct key_def *
+
+#include "salad/bps_tree.h"
+
+#undef BPS_TREE_NAME
+#undef BPS_TREE_BLOCK_SIZE
+#undef BPS_TREE_EXTENT_SIZE
+#undef BPS_TREE_COMPARE
+#undef BPS_TREE_COMPARE_KEY
+#undef bps_tree_elem_t
+#undef bps_tree_key_t
+#undef bps_tree_arg_t
+
+struct memtx_tree_index {
+	struct index base;
+	struct memtx_tree tree;
+	struct tuple **build_array;
+	size_t build_array_size, build_array_alloc_size;
+	struct memtx_gc_task gc_task;
+	struct memtx_tree_iterator gc_iterator;
+};
+
 /* {{{ Utilities. *************************************************/
 
 static int
@@ -686,7 +744,7 @@ static const struct index_vtab memtx_tree_index_vtab = {
 	/* .end_build = */ memtx_tree_index_end_build,
 };
 
-struct memtx_tree_index *
+struct index *
 memtx_tree_index_new(struct memtx_engine *memtx, struct index_def *def)
 {
 	if (!mempool_is_initialized(&memtx->iterator_pool)) {
@@ -710,5 +768,5 @@ memtx_tree_index_new(struct memtx_engine *memtx, struct index_def *def)
 	struct key_def *cmp_def = memtx_tree_index_cmp_def(index);
 	memtx_tree_create(&index->tree, cmp_def, memtx_index_extent_alloc,
 			  memtx_index_extent_free, memtx);
-	return index;
+	return &index->base;
 }
diff --git a/src/box/memtx_tree.h b/src/box/memtx_tree.h
index bbc8b2419..edeaebab9 100644
--- a/src/box/memtx_tree.h
+++ b/src/box/memtx_tree.h
@@ -30,78 +30,16 @@
  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
-#include <stddef.h>
-#include <stdint.h>
-
-#include "index.h"
-#include "memtx_engine.h"
 
 #if defined(__cplusplus)
 extern "C" {
 #endif /* defined(__cplusplus) */
 
+struct index;
+struct index_def;
 struct memtx_engine;
 
-/**
- * Struct that is used as a key in BPS tree definition.
- */
-struct memtx_tree_key_data
-{
-	/** Sequence of msgpacked search fields */
-	const char *key;
-	/** Number of msgpacked search fields */
-	uint32_t part_count;
-};
-
-/**
- * BPS tree element vs key comparator.
- * Defined in header in order to allow compiler to inline it.
- * @param tuple - tuple to compare.
- * @param key_data - key to compare with.
- * @param def - key definition.
- * @retval 0  if tuple == key in terms of def.
- * @retval <0 if tuple < key in terms of def.
- * @retval >0 if tuple > key in terms of def.
- */
-static inline int
-memtx_tree_compare_key(const struct tuple *tuple,
-		       const struct memtx_tree_key_data *key_data,
-		       struct key_def *def)
-{
-	return tuple_compare_with_key(tuple, key_data->key,
-				      key_data->part_count, def);
-}
-
-#define BPS_TREE_NAME memtx_tree
-#define BPS_TREE_BLOCK_SIZE (512)
-#define BPS_TREE_EXTENT_SIZE MEMTX_EXTENT_SIZE
-#define BPS_TREE_COMPARE(a, b, arg) tuple_compare(a, b, arg)
-#define BPS_TREE_COMPARE_KEY(a, b, arg) memtx_tree_compare_key(a, b, arg)
-#define bps_tree_elem_t struct tuple *
-#define bps_tree_key_t struct memtx_tree_key_data *
-#define bps_tree_arg_t struct key_def *
-
-#include "salad/bps_tree.h"
-
-#undef BPS_TREE_NAME
-#undef BPS_TREE_BLOCK_SIZE
-#undef BPS_TREE_EXTENT_SIZE
-#undef BPS_TREE_COMPARE
-#undef BPS_TREE_COMPARE_KEY
-#undef bps_tree_elem_t
-#undef bps_tree_key_t
-#undef bps_tree_arg_t
-
-struct memtx_tree_index {
-	struct index base;
-	struct memtx_tree tree;
-	struct tuple **build_array;
-	size_t build_array_size, build_array_alloc_size;
-	struct memtx_gc_task gc_task;
-	struct memtx_tree_iterator gc_iterator;
-};
-
-struct memtx_tree_index *
+struct index *
 memtx_tree_index_new(struct memtx_engine *memtx, struct index_def *def);
 
 #if defined(__cplusplus)
-- 
2.20.1




More information about the Tarantool-patches mailing list