[Groonga-commit] groonga/grnxx at ade9b56 [master] Add Index::find_starts_with(). (#52)

Back to archive index

susumu.yata null+****@clear*****
Wed Sep 17 14:16:54 JST 2014


susumu.yata	2014-09-17 14:16:54 +0900 (Wed, 17 Sep 2014)

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

  Message:
    Add Index::find_starts_with(). (#52)

  Modified files:
    include/grnxx/index.hpp
    lib/grnxx/index.cpp
    lib/grnxx/tree_index.hpp

  Modified: include/grnxx/index.hpp (+10 -0)
===================================================================
--- include/grnxx/index.hpp    2014-09-16 19:52:36 +0900 (b34ea42)
+++ include/grnxx/index.hpp    2014-09-17 14:16:54 +0900 (32ff2e0)
@@ -102,6 +102,16 @@ class Index {
       const IndexRange &range = IndexRange(),
       const CursorOptions &options = CursorOptions()) const;
 
+  // Create a cursor to get records.
+  //
+  // Returns a pointer to the cursor on success.
+  // On failure, returns nullptr and stores error information into "*error" if
+  // "error" != nullptr.
+  virtual unique_ptr<Cursor> find_starts_with(
+      Error *error,
+      const EndPoint &prefix,
+      const CursorOptions &options = CursorOptions()) const;
+
   // Insert a new entry.
   //
   // On success, returns true.

  Modified: lib/grnxx/index.cpp (+53 -0)
===================================================================
--- lib/grnxx/index.cpp    2014-09-16 19:52:36 +0900 (049938c)
+++ lib/grnxx/index.cpp    2014-09-17 14:16:54 +0900 (ca9f249)
@@ -28,6 +28,14 @@ unique_ptr<Cursor> Index::find_in_range(
   return nullptr;
 }
 
+unique_ptr<Cursor> Index::find_starts_with(
+    Error *error,
+    const EndPoint &,
+    const CursorOptions &) const {
+  GRNXX_ERROR_SET(error, NOT_SUPPORTED_YET, "Not supoprted yet");
+  return nullptr;
+}
+
 Index::Index()
     : column_(nullptr),
       name_(),
@@ -963,6 +971,51 @@ unique_ptr<Cursor> TreeIndex<Text>::find_in_range(
   }
 }
 
+unique_ptr<Cursor> TreeIndex<Text>::find_starts_with(
+    Error *error,
+    const EndPoint &prefix,
+    const CursorOptions &options) const {
+  std::string lower_bound_value = "";
+  if (prefix.value.type() != TEXT_DATA) {
+    GRNXX_ERROR_SET(error, INVALID_ARGUMENT, "Data type conflict");
+    return nullptr;
+  }
+  Text prefix_text = prefix.value.force_text();
+  lower_bound_value.assign(prefix_text.data(), prefix_text.size());
+
+  std::string upper_bound_value = lower_bound_value;
+  while (!upper_bound_value.empty() &&
+         (static_cast<unsigned char>(upper_bound_value.back()) == 0xFF)) {
+    upper_bound_value.pop_back();
+  }
+  if (!upper_bound_value.empty()) {
+    ++upper_bound_value.back();
+  }
+
+  if (prefix.type == EXCLUSIVE_END_POINT) {
+    lower_bound_value += '\0';
+  }
+
+  auto begin = map_.lower_bound(lower_bound_value);
+  auto end = !upper_bound_value.empty() ?
+      map_.lower_bound(upper_bound_value) : map_.end();
+  if (options.order_type == REGULAR_ORDER) {
+    return create_map_set_cursor(error,
+                                 column_->table(),
+                                 begin,
+                                 end,
+                                 options.offset,
+                                 options.limit);
+  } else {
+    return create_reverse_map_set_cursor(error,
+                                         column_->table(),
+                                         begin,
+                                         end,
+                                         options.offset,
+                                         options.limit);
+  }
+}
+
 bool TreeIndex<Text>::insert(Error *, Int row_id, const Datum &value) {
   Text text = value.force_text();
   std::string string(text.data(), text.size());

  Modified: lib/grnxx/tree_index.hpp (+5 -0)
===================================================================
--- lib/grnxx/tree_index.hpp    2014-09-16 19:52:36 +0900 (34c8166)
+++ lib/grnxx/tree_index.hpp    2014-09-17 14:16:54 +0900 (d3e4c30)
@@ -151,6 +151,11 @@ class TreeIndex<Text> : public Index {
       const IndexRange &range,
       const CursorOptions &options) const;
 
+  unique_ptr<Cursor> find_starts_with(
+      Error *error,
+      const EndPoint &prefix,
+      const CursorOptions &options = CursorOptions()) const;
+
   bool insert(Error *error, Int row_id, const Datum &value);
   bool remove(Error *error, Int row_id, const Datum &value);
 
-------------- next part --------------
HTML����������������������������...
Download 



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