[Groonga-commit] groonga/grnxx at e74bc21 [master] Implement common functions for Index.

Back to archive index

susumu.yata null+****@clear*****
Mon Sep 8 11:42:22 JST 2014


susumu.yata	2014-09-08 11:42:22 +0900 (Mon, 08 Sep 2014)

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

  Message:
    Implement common functions for Index.

  Modified files:
    include/grnxx/column.hpp
    lib/grnxx/column.cpp

  Modified: include/grnxx/column.hpp (+13 -4)
===================================================================
--- include/grnxx/column.hpp    2014-09-08 11:14:45 +0900 (486fa59)
+++ include/grnxx/column.hpp    2014-09-08 11:42:22 +0900 (3f3a10f)
@@ -1,6 +1,7 @@
 #ifndef GRNXX_COLUMN_HPP
 #define GRNXX_COLUMN_HPP
 
+#include "grnxx/array.hpp"
 #include "grnxx/name.hpp"
 #include "grnxx/types.hpp"
 
@@ -33,8 +34,7 @@ class Column {
   }
   // Return the number of indexes.
   Int num_indexes() const {
-    // TODO: Index is not supported yet.
-    return 0;
+    return indexes_.size();
   }
 
   // Create an index with "name", "index_type", and "index_options".
@@ -89,8 +89,7 @@ class Column {
   // On failure, returns nullptr and stores error information into "*error" if
   // "error" != nullptr.
   Index *get_index(Int index_id) const {
-    // TODO: Not supported yet.
-    return nullptr;
+    return indexes_[index_id].get();
   }
 
   // Find an index named "name".
@@ -131,6 +130,7 @@ class Column {
   DataType data_type_;
   Table *ref_table_;
   bool has_key_attribute_;
+  Array<unique_ptr<Index>> indexes_;
 
   Column();
 
@@ -185,6 +185,15 @@ class Column {
   // Unset the value.
   virtual void unset(Int row_id) = 0;
 
+  // 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,
+                            String name,
+                            Int *column_id) const;
+
   friend class Table;
 };
 

  Modified: lib/grnxx/column.cpp (+71 -11)
===================================================================
--- lib/grnxx/column.cpp    2014-09-08 11:14:45 +0900 (025b0d4)
+++ lib/grnxx/column.cpp    2014-09-08 11:42:22 +0900 (e408db7)
@@ -5,6 +5,7 @@
 #include "grnxx/datum.hpp"
 #include "grnxx/db.hpp"
 #include "grnxx/error.hpp"
+#include "grnxx/index.hpp"
 #include "grnxx/table.hpp"
 
 namespace grnxx {
@@ -21,30 +22,73 @@ Index *Column::create_index(Error *error,
 }
 
 bool Column::remove_index(Error *error, String name) {
-  // TODO: Index is not supported yet.
-  GRNXX_ERROR_SET(error, NOT_SUPPORTED_YET, "Not suported yet");
-  return false;
+  Int index_id;
+  if (!find_index_with_id(error, name, &index_id)) {
+    return false;
+  }
+  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;
+  }
+  indexes_.erase(index_id);
+  return true;
 }
 
 bool Column::rename_index(Error *error,
                           String name,
                           String new_name) {
-  // TODO: Index is not supported yet.
-  GRNXX_ERROR_SET(error, NOT_SUPPORTED_YET, "Not suported yet");
-  return false;
+  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);
 }
 
 bool Column::reorder_index(Error *error,
                            String name,
                            String prev_name) {
-  // TODO: Index is not supported yet.
-  GRNXX_ERROR_SET(error, NOT_SUPPORTED_YET, "Not suported yet");
-  return false;
+  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]);
+  }
+  for ( ; index_id > new_index_id; --index_id) {
+    std::swap(indexes_[index_id], indexes_[index_id - 1]);
+  }
+  return true;
 }
 
 Index *Column::find_index(Error *error, String name) const {
-  // TODO: Index is not supported yet.
-  GRNXX_ERROR_SET(error, NOT_SUPPORTED_YET, "Not suported yet");
+  for (Int index_id = 0; index_id < num_indexes(); ++index_id) {
+    if (name == indexes_[index_id]->name()) {
+      return indexes_[index_id].get();
+    }
+  }
   return nullptr;
 }
 
@@ -154,6 +198,22 @@ bool Column::set_initial_key(Error *error, Int row_id, const Datum &key) {
   return false;
 }
 
+Index *Column::find_index_with_id(Error *error,
+                                  String 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;
+}
+
 // -- ColumnImpl<T> --
 
 template <typename T>
-------------- next part --------------
HTML����������������������������...
다운로드 



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