[Groonga-commit] groonga/grnxx at ecb924a [master] Add a stub for incremental sorting. (#42)

Back to archive index

susumu.yata null+****@clear*****
Wed Dec 10 13:37:28 JST 2014


susumu.yata	2014-12-10 13:37:28 +0900 (Wed, 10 Dec 2014)

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

  Message:
    Add a stub for incremental sorting. (#42)

  Modified files:
    lib/grnxx/impl/sorter.cpp
    lib/grnxx/impl/sorter.hpp

  Modified: lib/grnxx/impl/sorter.cpp (+72 -7)
===================================================================
--- lib/grnxx/impl/sorter.cpp    2014-12-08 11:20:16 +0900 (7a1026c)
+++ lib/grnxx/impl/sorter.cpp    2014-12-10 13:37:28 +0900 (a1d8dbb)
@@ -18,6 +18,12 @@ class Node {
     next_ = next;
   }
 
+  // Sort new records.
+  virtual void progress(Array<Record> *records,
+                        size_t offset,
+                        size_t limit,
+                        size_t progress);
+
   // Sort records in [begin, end).
   //
   // On failure, throws an exception.
@@ -28,6 +34,10 @@ class Node {
   Node *next_;
 };
 
+void Node::progress(Array<Record> *, size_t, size_t, size_t) {
+  // Not supported.
+}
+
 // --- RowIDNode ---
 
 // NOTE: The following implementation assumes that there are no duplicates.
@@ -1063,6 +1073,42 @@ void TextNode<T>::move_pivot_first(ArrayRef<Record> records, Text *values) {
   }
 }
 
+// --- RowIDNodeS ---
+
+template <typename T>
+class RowIDNodeS : public Node {
+ public:
+  using Comparer = T;
+
+  explicit RowIDNodeS(SorterOrder &&order)
+      : Node(std::move(order)),
+        comparer_() {}
+  ~RowIDNodeS() = default;
+
+  void progress(Array<Record> *records,
+                size_t offset,
+                size_t limit,
+                size_t progress);
+
+  void sort(ArrayRef<Record> ref, size_t begin, size_t end);
+
+ private:
+  Comparer comparer_;
+};
+
+template <typename T>
+void RowIDNodeS<T>::progress(Array<Record> *records,
+                             size_t offset,
+                             size_t limit,
+                             size_t progress) {
+  // TODO
+}
+
+template <typename T>
+void RowIDNodeS<T>::sort(ArrayRef<Record>, size_t, size_t) {
+  // Nothing to do because there are no duplicates.
+}
+
 }  // namespace sorter
 
 using namespace sorter;
@@ -1073,7 +1119,8 @@ Sorter::Sorter(Array<SorterOrder> &&orders, const SorterOptions &options)
       nodes_(),
       records_(nullptr),
       offset_(options.offset),
-      limit_(options.limit) {
+      limit_(options.limit),
+      progress_(0) {
   // A sorter requires one or more orders.
   // Also, expressions must be valid and associated tables must be the same.
   if (orders.size() == 0) {
@@ -1091,6 +1138,10 @@ Sorter::Sorter(Array<SorterOrder> &&orders, const SorterOptions &options)
     }
   }
 
+  if (limit_ > (std::numeric_limits<size_t>::max() - offset_)) {
+    limit_ = std::numeric_limits<size_t>::max() - offset_;
+  }
+
   nodes_.resize(orders.size());
   for (size_t i = 0; i < orders.size(); ++i) {
     nodes_[i].reset(create_node(std::move(orders[i])));
@@ -1108,13 +1159,19 @@ void Sorter::reset(Array<Record> *records) {
 }
 
 void Sorter::progress() {
-  // TODO: Incremental sorting is not supported yet.
+  if (!records_) {
+    throw "No target";  // TODO
+  }
+  nodes_[0]->progress(records_, offset_, limit_, progress_);
+  progress_ = records_->size();
 }
 
 void Sorter::finish() {
   if (!records_) {
     throw "No target";  // TODO
   }
+  nodes_[0]->progress(records_, offset_, limit_, progress_);
+  progress_ = records_->size();
   if ((offset_ >= records_->size()) || (limit_ <= 0)) {
     records_->clear();
     return;
@@ -1143,11 +1200,19 @@ void Sorter::sort(Array<Record> *records) {
 
 Node *Sorter::create_node(SorterOrder &&order) try {
   if (order.expression->is_row_id()) {
-    if (order.type == SORTER_REGULAR_ORDER) {
-      return new RowIDNode<RegularRowIDComparer>(std::move(order));
-    } else {
-      return new RowIDNode<ReverseRowIDComparer>(std::move(order));
-    }
+//    if ((offset_ + limit_) < 1000) {
+//      if (order.type == SORTER_REGULAR_ORDER) {
+//        return new RowIDNodeS<RegularRowIDComparer>(std::move(order));
+//      } else {
+//        return new RowIDNodeS<ReverseRowIDComparer>(std::move(order));
+//      }
+//    } else {
+      if (order.type == SORTER_REGULAR_ORDER) {
+        return new RowIDNode<RegularRowIDComparer>(std::move(order));
+      } else {
+        return new RowIDNode<ReverseRowIDComparer>(std::move(order));
+      }
+//    }
   } else if (order.expression->is_score()) {
     // NOTE: Specialization for Score is disabled because the implementation
     //       showed poor performance.

  Modified: lib/grnxx/impl/sorter.hpp (+2 -1)
===================================================================
--- lib/grnxx/impl/sorter.hpp    2014-12-08 11:20:16 +0900 (b445fd3)
+++ lib/grnxx/impl/sorter.hpp    2014-12-10 13:37:28 +0900 (9939bc0)
@@ -37,12 +37,13 @@ class Sorter : public SorterInterface {
   Array<Record> *records_;
   size_t offset_;
   size_t limit_;
+  size_t progress_;
 
   // Create a node for sorting records in "order".
   //
   // On success, returns the node.
   // On failure, throws an exception.
-  static Node *create_node(SorterOrder &&order);
+  Node *create_node(SorterOrder &&order);
 };
 
 }  // namespace impl
-------------- next part --------------
HTML����������������������������...
下載 



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