From 63fe5e6d4f5742d9d3bc45b95ecb290d9bd07ac0 Mon Sep 17 00:00:00 2001
From: Vladimir Davydov <vdavydov.dev@gmail.com>
Date: Sat, 1 Jul 2017 15:31:04 +0300
Subject: [PATCH] Move iterator_type to its own source file

Including index.h just for the sake of iterator_type, as we do in
vy_run.h and vy_mem.h, is a bit of overkill. Let's move its definition
to a separate source file, iterator_type.h.
---
 src/box/CMakeLists.txt  |   1 +
 src/box/index.cc        |  18 -------
 src/box/index.h         |  59 +----------------------
 src/box/iterator_type.c |  50 ++++++++++++++++++++
 src/box/iterator_type.h | 101 ++++++++++++++++++++++++++++++++++++++++
 src/box/vy_cache.h      |   2 +-
 src/box/vy_mem.h        |   2 +-
 src/box/vy_run.h        |   2 +-
 8 files changed, 156 insertions(+), 79 deletions(-)
 create mode 100644 src/box/iterator_type.c
 create mode 100644 src/box/iterator_type.h

diff --git a/src/box/CMakeLists.txt b/src/box/CMakeLists.txt
index e36a112be0..6f61c5b3bf 100644
--- a/src/box/CMakeLists.txt
+++ b/src/box/CMakeLists.txt
@@ -35,6 +35,7 @@ add_library(box STATIC
     tuple_hash.cc
     key_def.cc
     index.cc
+    iterator_type.c
     memtx_index.cc
     memtx_hash.cc
     memtx_tree.cc
diff --git a/src/box/index.cc b/src/box/index.cc
index 425b7eaac8..596a72730a 100644
--- a/src/box/index.cc
+++ b/src/box/index.cc
@@ -39,24 +39,6 @@
 #include "rmean.h"
 #include "info.h"
 
-const char *iterator_type_strs[] = {
-	/* [ITER_EQ]  = */ "EQ",
-	/* [ITER_REQ]  = */ "REQ",
-	/* [ITER_ALL] = */ "ALL",
-	/* [ITER_LT]  = */ "LT",
-	/* [ITER_LE]  = */ "LE",
-	/* [ITER_GE]  = */ "GE",
-	/* [ITER_GT]  = */ "GT",
-	/* [ITER_BITS_ALL_SET] = */ "BITS_ALL_SET",
-	/* [ITER_BITS_ANY_SET] = */ "BITS_ANY_SET",
-	/* [ITER_BITS_ALL_NOT_SET] = */ "BITS_ALL_NOT_SET",
-	/* [ITER_OVERLAPS] = */ "OVERLAPS",
-	/* [ITER_NEIGHBOR] = */ "NEIGHBOR",
-};
-
-static_assert(sizeof(iterator_type_strs) / sizeof(const char *) ==
-	iterator_type_MAX, "iterator_type_str constants");
-
 /* {{{ Utilities. **********************************************/
 
 UnsupportedIndexFeature::UnsupportedIndexFeature(const char *file,
diff --git a/src/box/index.h b/src/box/index.h
index 381c6aae70..96007b4957 100644
--- a/src/box/index.h
+++ b/src/box/index.h
@@ -32,6 +32,7 @@
  */
 #include <stdbool.h>
 #include "trivia/util.h"
+#include "iterator_type.h"
 
 #if defined(__cplusplus)
 extern "C" {
@@ -43,56 +44,6 @@ typedef struct tuple box_tuple_t;
 /** A space iterator */
 typedef struct iterator box_iterator_t;
 
-/**
- * Controls how to iterate over tuples in an index.
- * Different index types support different iterator types.
- * For example, one can start iteration from a particular value
- * (request key) and then retrieve all tuples where keys are
- * greater or equal (= GE) to this key.
- *
- * If iterator type is not supported by the selected index type,
- * iterator constructor must fail with ER_UNSUPPORTED. To be
- * selectable for primary key, an index must support at least
- * ITER_EQ and ITER_GE types.
- *
- * NULL value of request key corresponds to the first or last
- * key in the index, depending on iteration direction.
- * (first key for GE and GT types, and last key for LE and LT).
- * Therefore, to iterate over all tuples in an index, one can
- * use ITER_GE or ITER_LE iteration types with start key equal
- * to NULL.
- * For ITER_EQ, the key must not be NULL.
- */
-
-enum iterator_type {
-	/* ITER_EQ must be the first member for request_create  */
-	ITER_EQ               =  0, /* key == x ASC order                  */
-	ITER_REQ              =  1, /* key == x DESC order                 */
-	ITER_ALL              =  2, /* all tuples                          */
-	ITER_LT               =  3, /* key <  x                            */
-	ITER_LE               =  4, /* key <= x                            */
-	ITER_GE               =  5, /* key >= x                            */
-	ITER_GT               =  6, /* key >  x                            */
-	ITER_BITS_ALL_SET     =  7, /* all bits from x are set in key      */
-	ITER_BITS_ANY_SET     =  8, /* at least one x's bit is set         */
-	ITER_BITS_ALL_NOT_SET =  9, /* all bits are not set                */
-	ITER_OVERLAPS         = 10, /* key overlaps x                      */
-	ITER_NEIGHBOR         = 11, /* tuples in distance ascending order from specified point */
-	iterator_type_MAX     = ITER_NEIGHBOR + 1
-};
-
-/**
- * Determine a direction of the given iterator type.
- * That is -1 for REQ, LT and LE and +1 for all others.
- */
-static inline int
-iterator_direction(enum iterator_type type)
-{
-	const uint32_t reverse =
-		(1u << ITER_REQ) | (1u << ITER_LT) | (1u << ITER_LE);
-	return (reverse & (1u << type)) ? -1 : 1;
-}
-
 /**
  * Allocate and initialize iterator for space_id, index_id.
  *
@@ -274,8 +225,6 @@ int
 box_index_info(uint32_t space_id, uint32_t index_id,
 	       struct info_handler *info);
 
-extern const char *iterator_type_strs[];
-
 #if defined(__cplusplus)
 } /* extern "C" */
 #include "key_def.h"
@@ -290,12 +239,6 @@ struct iterator {
 	struct Index *index;
 };
 
-static inline bool
-iterator_type_is_reverse(enum iterator_type type)
-{
-	return type == ITER_REQ || type == ITER_LT || type == ITER_LE;
-}
-
 /**
  * Check that the key has correct part count and correct part size
  * for use in an index iterator.
diff --git a/src/box/iterator_type.c b/src/box/iterator_type.c
new file mode 100644
index 0000000000..5d6b55f21c
--- /dev/null
+++ b/src/box/iterator_type.c
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2010-2017, Tarantool AUTHORS, please see AUTHORS file.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above
+ *    copyright notice, this list of conditions and the
+ *    following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above
+ *    copyright notice, this list of conditions and the following
+ *    disclaimer in the documentation and/or other materials
+ *    provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+ * <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+ * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+#include "iterator_type.h"
+#include "trivia/util.h"
+
+const char *iterator_type_strs[] = {
+	/* [ITER_EQ]  = */ "EQ",
+	/* [ITER_REQ]  = */ "REQ",
+	/* [ITER_ALL] = */ "ALL",
+	/* [ITER_LT]  = */ "LT",
+	/* [ITER_LE]  = */ "LE",
+	/* [ITER_GE]  = */ "GE",
+	/* [ITER_GT]  = */ "GT",
+	/* [ITER_BITS_ALL_SET] = */ "BITS_ALL_SET",
+	/* [ITER_BITS_ANY_SET] = */ "BITS_ANY_SET",
+	/* [ITER_BITS_ALL_NOT_SET] = */ "BITS_ALL_NOT_SET",
+	/* [ITER_OVERLAPS] = */ "OVERLAPS",
+	/* [ITER_NEIGHBOR] = */ "NEIGHBOR",
+};
+
+static_assert(sizeof(iterator_type_strs) / sizeof(const char *) ==
+	iterator_type_MAX, "iterator_type_str constants");
diff --git a/src/box/iterator_type.h b/src/box/iterator_type.h
new file mode 100644
index 0000000000..445c1110d8
--- /dev/null
+++ b/src/box/iterator_type.h
@@ -0,0 +1,101 @@
+#ifndef TARANTOOL_BOX_ITERATOR_TYPE_H_INCLUDED
+#define TARANTOOL_BOX_ITERATOR_TYPE_H_INCLUDED
+/*
+ * Copyright 2010-2017, Tarantool AUTHORS, please see AUTHORS file.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above
+ *    copyright notice, this list of conditions and the
+ *    following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above
+ *    copyright notice, this list of conditions and the following
+ *    disclaimer in the documentation and/or other materials
+ *    provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+ * <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+ * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <stdbool.h>
+
+#if defined(__cplusplus)
+extern "C" {
+#endif /* defined(__cplusplus) */
+
+/**
+ * Controls how to iterate over tuples in an index.
+ * Different index types support different iterator types.
+ * For example, one can start iteration from a particular value
+ * (request key) and then retrieve all tuples where keys are
+ * greater or equal (= GE) to this key.
+ *
+ * If iterator type is not supported by the selected index type,
+ * iterator constructor must fail with ER_UNSUPPORTED. To be
+ * selectable for primary key, an index must support at least
+ * ITER_EQ and ITER_GE types.
+ *
+ * NULL value of request key corresponds to the first or last
+ * key in the index, depending on iteration direction.
+ * (first key for GE and GT types, and last key for LE and LT).
+ * Therefore, to iterate over all tuples in an index, one can
+ * use ITER_GE or ITER_LE iteration types with start key equal
+ * to NULL.
+ * For ITER_EQ, the key must not be NULL.
+ */
+enum iterator_type {
+	/* ITER_EQ must be the first member for request_create  */
+	ITER_EQ               =  0, /* key == x ASC order                  */
+	ITER_REQ              =  1, /* key == x DESC order                 */
+	ITER_ALL              =  2, /* all tuples                          */
+	ITER_LT               =  3, /* key <  x                            */
+	ITER_LE               =  4, /* key <= x                            */
+	ITER_GE               =  5, /* key >= x                            */
+	ITER_GT               =  6, /* key >  x                            */
+	ITER_BITS_ALL_SET     =  7, /* all bits from x are set in key      */
+	ITER_BITS_ANY_SET     =  8, /* at least one x's bit is set         */
+	ITER_BITS_ALL_NOT_SET =  9, /* all bits are not set                */
+	ITER_OVERLAPS         = 10, /* key overlaps x                      */
+	ITER_NEIGHBOR         = 11, /* tuples in distance ascending order from specified point */
+	iterator_type_MAX
+};
+
+extern const char *iterator_type_strs[];
+
+/**
+ * Determine a direction of the given iterator type.
+ * That is -1 for REQ, LT and LE and +1 for all others.
+ */
+static inline int
+iterator_direction(enum iterator_type type)
+{
+	const unsigned reverse =
+		(1u << ITER_REQ) | (1u << ITER_LT) | (1u << ITER_LE);
+	return (reverse & (1u << type)) ? -1 : 1;
+}
+
+static inline bool
+iterator_type_is_reverse(enum iterator_type type)
+{
+	return type == ITER_REQ || type == ITER_LT || type == ITER_LE;
+}
+
+#if defined(__cplusplus)
+} /* extern "C" */
+#endif /* defined(__cplusplus) */
+
+#endif /* TARANTOOL_BOX_ITERATOR_TYPE_H_INCLUDED */
diff --git a/src/box/vy_cache.h b/src/box/vy_cache.h
index ed4453e5f4..4714fb715e 100644
--- a/src/box/vy_cache.h
+++ b/src/box/vy_cache.h
@@ -36,7 +36,7 @@
 
 #include <small/rlist.h>
 
-#include "index.h" /* enum iterator_type */
+#include "iterator_type.h"
 #include "vy_stmt.h" /* for comparators */
 #include "vy_stmt_iterator.h" /* struct vy_stmt_iterator */
 #include "vy_stat.h"
diff --git a/src/box/vy_mem.h b/src/box/vy_mem.h
index 4e8df5b66d..c305ad8004 100644
--- a/src/box/vy_mem.h
+++ b/src/box/vy_mem.h
@@ -37,7 +37,7 @@
 #include <small/rlist.h>
 
 #include "ipc.h"
-#include "index.h" /* enum iterator_type */
+#include "iterator_type.h"
 #include "vy_stmt.h" /* for comparators */
 #include "vy_stmt_iterator.h" /* struct vy_stmt_iterator */
 #include "vy_stat.h"
diff --git a/src/box/vy_run.h b/src/box/vy_run.h
index 92556e77d7..3958d7bb38 100644
--- a/src/box/vy_run.h
+++ b/src/box/vy_run.h
@@ -35,7 +35,7 @@
 #include <stdbool.h>
 
 #include "ipc.h"
-#include "index.h" /* enum iterator_type */
+#include "iterator_type.h"
 #include "vy_stmt.h" /* for comparators */
 #include "vy_stmt_iterator.h" /* struct vy_stmt_iterator */
 #include "vy_stat.h"
-- 
GitLab