[Groonga-commit] groonga/grnxx at d009050 [master] Add an Index stub. (#121)

Back to archive index

susumu.yata null+****@clear*****
Tue Dec 16 10:47:01 JST 2014


susumu.yata	2014-11-27 21:10:32 +0900 (Thu, 27 Nov 2014)

  New Revision: d009050e5c2c640a2def2e96f48209be81c9350e
  https://github.com/groonga/grnxx/commit/d009050e5c2c640a2def2e96f48209be81c9350e

  Message:
    Add an Index stub. (#121)

  Added files:
    lib/grnxx/impl/index.cpp
  Modified files:
    include/grnxx/index.hpp
    lib/grnxx/impl/Makefile.am
    lib/grnxx/impl/column/base.cpp
    lib/grnxx/impl/column/base.hpp
    lib/grnxx/impl/index.hpp

  Modified: include/grnxx/index.hpp (+44 -3)
===================================================================
--- include/grnxx/index.hpp    2014-11-27 17:16:06 +0900 (97c99c3)
+++ include/grnxx/index.hpp    2014-11-27 21:10:32 +0900 (b18df4a)
@@ -83,7 +83,48 @@ struct IndexOptions {
 
 class Index {
  public:
+  Index() = default;
+
+  // Return the owner column.
+  virtual Column *column() const = 0;
+  // Return the name.
+  virtual String name() const = 0;
+  // Return the index type.
+  virtual IndexType type() const = 0;
+
+  // Insert a new entry.
+  //
+  // On failure, throws an exception.
+  virtual void insert(Int row_id, const Datum &value) = 0;
+  // Remove an entry.
+  //
+  // On failure, throws an exception.
+  virtual bool remove(Int row_id, const Datum &value) = 0;
+
+  // Return whether "value" is registered or not.
+  virtual bool contains(const Datum &value) const;
+
+  // Find "datum".
+  //
+  // If found, returns the row ID.
+  // If not found, returns N/A.
+  virtual Int find_one(const Datum &value) const;
+
+  // Create a cursor to get records.
+  //
+  // On success, returns the cursor.
+  // On failure, throws an exception.
+  virtual std::unique_ptr<Cursor> find(
+      const Datum &value,
+      const CursorOptions &options = CursorOptions()) const;
+
+ protected:
   virtual ~Index() = default;
+};
+
+//class Index {
+// public:
+//  virtual ~Index() = default;
 
 //  // Return the owner column.
 //  Column *column() const {
@@ -204,9 +245,9 @@ class Index {
 //  // Return whether the index is removable or not.
 //  bool is_removable();
 
- protected:
-  Index() = default;
-};
+// protected:
+//  Index() = default;
+//};
 
 }  // namespace grnxx
 

  Modified: lib/grnxx/impl/Makefile.am (+1 -0)
===================================================================
--- lib/grnxx/impl/Makefile.am    2014-11-27 17:16:06 +0900 (6cf8af6)
+++ lib/grnxx/impl/Makefile.am    2014-11-27 21:10:32 +0900 (7b24b56)
@@ -11,6 +11,7 @@ libgrnxx_impl_la_LDFLAGS = @AM_LTLDFLAGS@
 libgrnxx_impl_la_SOURCES =		\
 	db.cpp				\
 	expression.cpp			\
+	index.cpp			\
 	merger.cpp			\
 	pipeline.cpp			\
 	sorter.cpp			\

  Modified: lib/grnxx/impl/column/base.cpp (+69 -137)
===================================================================
--- lib/grnxx/impl/column/base.cpp    2014-11-27 17:16:06 +0900 (574d4c3)
+++ lib/grnxx/impl/column/base.cpp    2014-11-27 21:10:32 +0900 (b7e386e)
@@ -2,6 +2,7 @@
 
 #include "grnxx/impl/column/scalar.hpp"
 #include "grnxx/impl/column/vector.hpp"
+#include "grnxx/impl/index.hpp"
 #include "grnxx/impl/table.hpp"
 
 namespace grnxx {
@@ -33,144 +34,78 @@ Index *ColumnBase::create_index(
     IndexType type,
     const IndexOptions &options) {
   throw "Not supported yet";  // TODO
-}
-
-void ColumnBase::remove_index(const String &name) {
-  throw "Not supported yet";  // TODO
-}
-
-void ColumnBase::rename_index(const String &name, const String &new_name) {
-  throw "Not supported yet";  // TODO
-}
-
-void ColumnBase::reorder_index(const String &name, const String &prev_name) {
-  throw "Not supported yet";  // TODO
-}
-
-Index *ColumnBase::find_index(const String &name) const {
-  throw "Not supported yet";  // TODO
-}
 
-bool ColumnBase::contains(const Datum &datum) const {
-  throw "Not supported yet";  // TODO
+//  if (find_index(name)) {
+//    throw "Index already exists";  // TODO
+//  }
+//  indexes_.reserve(indexes_.size() + 1);
+//  std::unique_ptr<Index> new_index = Index::create(this, name, type, options);
+//  indexes_.push_back(std::move(new_index));
+//  return indexes_.back().get();
 }
 
-Int ColumnBase::find_one(const Datum &datum) const {
+void ColumnBase::remove_index(const String &name) {
   throw "Not supported yet";  // TODO
-}
 
-//Index *ColumnBase::create_index(Error *error,
-//                                const StringCRef &name,
-//                                IndexType type,
-//                                const IndexOptions &options) {
-//  if (find_index(nullptr, name)) {
-//    GRNXX_ERROR_SET(error, ALREADY_EXISTS,
-//                    "Index already exists: name = \"%.*s\"",
-//                    static_cast<int>(name.size()), name.data());
-//    return nullptr;
-//  }
-//  if (!indexes_.reserve(error, indexes_.size() + 1)) {
-//    return nullptr;
-//  }
-//  unique_ptr<Index> new_index =
-//      Index::create(error, this, name, type, options);
-//  if (!new_index) {
-//    return nullptr;
-//  }
-//  indexes_.push_back(error, std::move(new_index));
-//  return indexes_.back().get();
-//}
-
-//bool ColumnBase::remove_index(Error *error, const StringCRef &name) {
-//  Int index_id;
-//  if (!find_index_with_id(error, name, &index_id)) {
-//    return false;
+//  size_t index_id;
+//  if (!find_index_with_id(name, &index_id)) {
+//    throw "Index not found";  // TODO
 //  }
 //  if (!indexes_[index_id]->is_removable()) {
-//    GRNXX_ERROR_SET(error, NOT_REMOVABLE,
-//                    "Index is not removable: name = \"%.*s\"",
-//                    static_cast<int>(name.size()), name.data());
-//    return false;
+//    throw "Index not removable";  // TODO
 //  }
 //  indexes_.erase(index_id);
-//  return true;
-//}
+}
 
-//bool ColumnBase::rename_index(Error *error,
-//                              const StringCRef &name,
-//                              const StringCRef &new_name) {
-//  Int index_id;
-//  if (!find_index_with_id(error, name, &index_id)) {
-//    return false;
-//  }
-//  if (name == new_name) {
-//    return true;
-//  }
-//  if (find_index(nullptr, new_name)) {
-//    GRNXX_ERROR_SET(error, ALREADY_EXISTS,
-//                    "Index already exists: new_name = \"%.*s\"",
-//                    static_cast<int>(new_name.size()), new_name.data());
-//    return false;
-//  }
-//  return indexes_[index_id]->rename(error, new_name);
-//}
+void ColumnBase::rename_index(const String &name, const String &new_name) {
+  throw "Not supported yet";  // TODO
 
-//bool ColumnBase::reorder_index(Error *error,
-//                               const StringCRef &name,
-//                               const StringCRef &prev_name) {
-//  Int index_id;
-//  if (!find_index_with_id(error, name, &index_id)) {
-//    return false;
-//  }
-//  Int new_index_id = 0;
-//  if (prev_name.size() != 0) {
-//    Int prev_index_id;
-//    if (!find_index_with_id(error, prev_name, &prev_index_id)) {
-//      return false;
-//    }
-//    if (index_id <= prev_index_id) {
-//      new_index_id = prev_index_id;
-//    } else {
-//      new_index_id = prev_index_id + 1;
-//    }
-//  }
-//  for ( ; index_id < new_index_id; ++index_id) {
-//    std::swap(indexes_[index_id], indexes_[index_id + 1]);
+//  size_t index_id;
+//  if (!find_index_with_id(name, &index_id)) {
+//    throw "Index not found";  // TODO
 //  }
-//  for ( ; index_id > new_index_id; --index_id) {
-//    std::swap(indexes_[index_id], indexes_[index_id - 1]);
+//  if (name == new_name) {
+//    return;
 //  }
-//  return true;
-//}
-
-//Index *ColumnBase::find_index(Error *error, const StringCRef &name) const {
-//  for (Int index_id = 0; index_id < num_indexes(); ++index_id) {
-//    if (name == indexes_[index_id]->name()) {
-//      return indexes_[index_id].get();
-//    }
+//  if (find_index(new_name)) {
+//    throw "Index already exists";  // TODO
 //  }
-//  GRNXX_ERROR_SET(error, NOT_FOUND, "Index not found");
-//  return nullptr;
-//}
-
-//bool ColumnBase::set(Error *error, Int, const Datum &) {
-//  GRNXX_ERROR_SET(error, NOT_SUPPORTED_YET, "Not suported yet");
-//  return false;
-//}
-
-//bool ColumnBase::get(Error *error, Int, Datum *) const {
-//  GRNXX_ERROR_SET(error, NOT_SUPPORTED_YET, "Not suported yet");
-//  return false;
-//}
+//  indexes_[index_id]->rename(new_name);
+}
 
-//bool ColumnBase::contains(const Datum &datum) const {
-//  return find_one(datum) != NULL_ROW_ID;
-//}
+void ColumnBase::reorder_index(const String &name, const String &prev_name) {
+  size_t index_id;
+  if (!find_index_with_id(name, &index_id)) {
+    throw "Index not found";  // TODO
+  }
+  size_t new_index_id = 0;
+  if (prev_name.size() != 0) {
+    size_t prev_index_id;
+    if (!find_index_with_id(prev_name, &prev_index_id)) {
+      throw "Index not found";  // TODO
+    }
+    if (index_id <= prev_index_id) {
+      new_index_id = prev_index_id;
+    } else {
+      new_index_id = prev_index_id + 1;
+    }
+  }
+  for ( ; index_id < new_index_id; ++index_id) {
+    std::swap(indexes_[index_id], indexes_[index_id + 1]);
+  }
+  for ( ; index_id > new_index_id; --index_id) {
+    std::swap(indexes_[index_id], indexes_[index_id - 1]);
+  }
+}
 
-//Int ColumnBase::find_one(const Datum &) const {
-//  // TODO: This function should be pure virtual.
-//  return Int::na();
-//}
+Index *ColumnBase::find_index(const String &name) const {
+  for (size_t i = 0; i < num_indexes(); ++i) {
+    if (name == indexes_[i]->name()) {
+      return indexes_[i].get();
+    }
+  }
+  return nullptr;
+}
 
 std::unique_ptr<ColumnBase> ColumnBase::create(
     Table *table,
@@ -253,21 +188,18 @@ void ColumnBase::set_key(Int, const Datum &) {
 //  throw "Not supported";  // TODO
 //}
 
-//Index *ColumnBase::find_index_with_id(Error *error,
-//                                      const StringCRef &name,
-//                                      Int *index_id) const {
-//  for (Int i = 0; i < num_indexes(); ++i) {
-//    if (name == indexes_[i]->name()) {
-//      if (index_id != nullptr) {
-//        *index_id = i;
-//      }
-//      return indexes_[i].get();
-//    }
-//  }
-//  GRNXX_ERROR_SET(error, NOT_FOUND, "Index not found: name = \"%.*s\"",
-//                  static_cast<int>(name.size()), name.data());
-//  return nullptr;
-//}
+Index *ColumnBase::find_index_with_id(const String &name,
+                                      size_t *index_id) const {
+  for (size_t i = 0; i < num_indexes(); ++i) {
+    if (name == indexes_[i]->name()) {
+      if (index_id) {
+        *index_id = i;
+      }
+      return indexes_[i].get();
+    }
+  }
+  return nullptr;
+}
 
 }  // namespace impl
 }  // namespace grnxx

  Modified: lib/grnxx/impl/column/base.hpp (+6 -12)
===================================================================
--- lib/grnxx/impl/column/base.hpp    2014-11-27 17:16:06 +0900 (9071dfd)
+++ lib/grnxx/impl/column/base.hpp    2014-11-27 21:10:32 +0900 (8c0c009)
@@ -53,9 +53,6 @@ class ColumnBase : public ColumnInterface {
   virtual void set(Int row_id, const Datum &datum) = 0;
   virtual void get(Int row_id, Datum *datum) const = 0;
 
-  virtual bool contains(const Datum &datum) const;
-  virtual Int find_one(const Datum &datum) const;
-
   // -- Internal API --
 
   // Create a new column.
@@ -114,15 +111,12 @@ class ColumnBase : public ColumnInterface {
   bool is_key_;
   Array<std::unique_ptr<Index>> indexes_;
 
-// private:
-//  // Find an index with its ID.
-//  //
-//  // On success, returns a pointer to the index.
-//  // On failure, returns nullptr and stores error information into "*error" if
-//  // "error" != nullptr.
-//  Index *find_index_with_id(Error *error,
-//                            const String &name,
-//                            Int *column_id) const;
+ private:
+  // Find an index with its ID.
+  //
+  // If found, returns the index.
+  // If not found, returns nullptr;
+  Index *find_index_with_id(const String &name, size_t *index_id) const;
 };
 
 }  // namespace impl

  Added: lib/grnxx/impl/index.cpp (+7 -0) 100644
===================================================================
--- /dev/null
+++ lib/grnxx/impl/index.cpp    2014-11-27 21:10:32 +0900 (a9c5b36)
@@ -0,0 +1,7 @@
+#include "grnxx/impl/index.hpp"
+
+namespace grnxx {
+namespace impl {
+
+}  // namespace impl
+}  // namespace grnxx

  Modified: lib/grnxx/impl/index.hpp (+41 -0)
===================================================================
--- lib/grnxx/impl/index.hpp    2014-11-27 17:16:06 +0900 (d8dd3f5)
+++ lib/grnxx/impl/index.hpp    2014-11-27 21:10:32 +0900 (77edc0f)
@@ -13,6 +13,47 @@ using ColumnInterface = grnxx::Column;
 using IndexInterface = grnxx::Index;
 
 class Index : public IndexInterface {
+ public:
+  // -- Public API (grnxx/index.hpp) --
+
+  Index(Column *column, const String &name);
+  virtual ~Index();
+
+  // Return the owner column.
+  ColumnInterface *column() const;
+  // Return the name.
+  String name() const {
+    return name_;
+  }
+
+  // -- Internal API --
+
+  // Create a new index.
+  //
+  // On success, returns the column.
+  // On failure, throws an exception.
+  static std::unique_ptr<Index> create(
+      Column *column,
+      const String &name,
+      IndexType type,
+      const IndexOptions &options);
+
+  // Return the owner table.
+  Column *_column() const {
+    return column_;
+  }
+
+  // Change the column name.
+  //
+  // On failure, throws an exception.
+  void rename(const String &new_name);
+
+  // Return whether the column is removable or not.
+  bool is_removable() const;
+
+ private:
+  Column *column_;
+  String name_;
 };
 
 }  // namespace impl
-------------- next part --------------
HTML����������������������������...
下载 



More information about the Groonga-commit mailing list
Back to archive index