[Groonga-commit] groonga/grnxx at e61a4ea [master] Add classes Order* and Sorter.

Back to archive index

susumu.yata null+****@clear*****
Thu Jul 24 09:57:42 JST 2014


susumu.yata	2014-07-24 09:57:42 +0900 (Thu, 24 Jul 2014)

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

  Message:
    Add classes Order* and Sorter.

  Added files:
    include/grnxx/order.hpp
    include/grnxx/sorter.hpp
    lib/grnxx/order.cpp
    lib/grnxx/sorter.cpp
  Modified files:
    include/grnxx/Makefile.am
    include/grnxx/types.hpp
    lib/grnxx/Makefile.am

  Modified: include/grnxx/Makefile.am (+2 -0)
===================================================================
--- include/grnxx/Makefile.am    2014-07-24 09:11:28 +0900 (d414c61)
+++ include/grnxx/Makefile.am    2014-07-24 09:57:42 +0900 (86ddd8e)
@@ -7,6 +7,8 @@ pkginclude_HEADERS =	\
 	expression.hpp	\
 	index.hpp	\
 	library.hpp	\
+	order.hpp	\
 	record.hpp	\
+	sorter.hpp	\
 	table.hpp	\
 	types.hpp

  Added: include/grnxx/order.hpp (+103 -0) 100644
===================================================================
--- /dev/null
+++ include/grnxx/order.hpp    2014-07-24 09:57:42 +0900 (f5c7f1b)
@@ -0,0 +1,103 @@
+#ifndef GRNXX_ORDER_HPP
+#define GRNXX_ORDER_HPP
+
+#include <vector>
+
+#include "grnxx/types.hpp"
+
+namespace grnxx {
+
+// TODO: 無駄に複雑になっているので,単純化したい.
+
+struct Order {
+  unique_ptr<Expression> expression;
+  OrderType type;
+
+  Order() = default;
+  Order(unique_ptr<Expression> &&expression, OrderType type)
+      : expression(std::move(expression)),
+        type(type) {}
+};
+
+class OrderSet {
+ public:
+  ~OrderSet();
+
+  // Return the number of sort conditions.
+  Int size() const {
+    return static_cast<Int>(orders_.size());
+  }
+
+  // Return the "i"-th sort condition.
+  //
+  // If "i" is invalid, the result is undefined.
+  const Order &get(Int i) const {
+    return orders_[i];
+  }
+
+ private:
+  std::vector<Order> orders_;
+
+  OrderSet();
+
+  // Append a sort condition.
+  //
+  // On success, returns true.
+  // On failure, returns false and stores error information into "*error" if
+  // "error" != nullptr.
+  bool append(Error *error, Order &&order);
+
+  friend OrderSetBuilder;
+};
+
+class OrderSetBuilder {
+ public:
+  // Create an object for building order sets.
+  //
+  // On success, returns a poitner to the builder.
+  // On failure, returns nullptr and stores error information into "*error" if
+  // "error" != nullptr.
+  static unique_ptr<OrderSetBuilder> create(Error *error, const Table *table);
+
+  ~OrderSetBuilder();
+
+  // Return the associated table.
+  const Table *table() const {
+    return table_;
+  }
+
+  // Append a sort condition.
+  //
+  // The sort condition is added as the last sort condition.
+  // Sort conditions must be appended in order of priority.
+  // "_id" is useful to perform stable sorting.
+  //
+  // On success, returns true.
+  // On failure, returns false and stores error information into "*error" if
+  // "error" != nullptr.
+  bool append(Error *error,
+              unique_ptr<Expression> &&expression,
+              OrderType type = REGULAR_ORDER);
+
+  // Clear the building order set.
+  void clear();
+
+  // Complete building an order set and clear the builder.
+  //
+  // Fails if the order set is empty.
+  //
+  // On success, returns a poitner to the order set.
+  // On failure, returns nullptr and stores error information into "*error" if
+  // "error" != nullptr.
+  unique_ptr<OrderSet> release(Error *error);
+
+ private:
+  const Table *table_;
+  std::vector<Order> orders_;
+
+  OrderSetBuilder();
+};
+
+}  // namespace grnxx
+
+#endif  // GRNXX_ORDER_HPP

  Added: include/grnxx/sorter.hpp (+63 -0) 100644
===================================================================
--- /dev/null
+++ include/grnxx/sorter.hpp    2014-07-24 09:57:42 +0900 (10611c0)
@@ -0,0 +1,63 @@
+#ifndef GRNXX_SORTER_HPP
+#define GRNXX_SORTER_HPP
+
+#include "grnxx/types.hpp"
+
+namespace grnxx {
+
+class Sorter {
+ public:
+  ~Sorter();
+
+  // Create an object for sorting records.
+  //
+  // On success, returns a poitner to the sorter.
+  // On failure, returns nullptr and stores error information into "*error" if
+  // "error" != nullptr.
+  static std::unique_ptr<Sorter> create(
+      Error *error,
+      std::unique_ptr<OrderSet> &&order_set,
+      const SorterOptions &options);
+
+  // Set the target record set.
+  //
+  // Aborts sorting the old record set and starts sorting the new record set.
+  //
+  // On success, returns true.
+  // On failure, returns false and stores error information into "*error" if
+  // "error" != nullptr.
+  bool reset(Error *error, RecordSet *record_set);
+
+  // Progress sorting.
+  //
+  // On success, returns true.
+  // On failure, returns false and stores error information into "*error" if
+  // "error" != nullptr.
+  bool progress(Error *error);
+
+  // Finish sorting.
+  //
+  // Assumes that all the records are ready.
+  // Leaves only the result records if offset and limit are specified.
+  //
+  // On success, returns true.
+  // On failure, returns false and stores error information into "*error" if
+  // "error" != nullptr.
+  bool finish(Error *error);
+
+  // Sort records.
+  //
+  // Calls reset() and finish() to sort records.
+  //
+  // On success, returns true.
+  // On failure, returns false and stores error information into "*error" if
+  // "error" != nullptr.
+  bool sort(Error *error, RecordSet *record_set);
+
+ private:
+  Sorter();
+};
+
+}  // namespace grnxx
+
+#endif  // GRNXX_SORTER_HPP

  Modified: include/grnxx/types.hpp (+13 -3)
===================================================================
--- include/grnxx/types.hpp    2014-07-24 09:11:28 +0900 (05d117c)
+++ include/grnxx/types.hpp    2014-07-24 09:57:42 +0900 (aada258)
@@ -390,6 +390,16 @@ struct CursorOptions {
   CursorOptions();
 };
 
+struct SorterOptions {
+  // The first "offset" records are skipped (default: 0).
+  Int offset;
+
+  // At most "limit" records are sorted (default: numeric_limits<Int>::max()).
+  Int limit;
+
+  SorterOptions();
+};
+
 // Database temporary object types.
 class Datum;
 class Cursor;
@@ -397,9 +407,9 @@ class RecordSet;
 class Expression;
 class ExpressionNode;
 class ExpressionBuilder;
-
-// Database temporary object option types.
-struct CursorOptions;
+class OrderSet;
+class OrderSetBuilder;
+class Sorter;
 
 }  // namespace grnxx
 

  Modified: lib/grnxx/Makefile.am (+2 -0)
===================================================================
--- lib/grnxx/Makefile.am    2014-07-24 09:11:28 +0900 (3e5d28a)
+++ lib/grnxx/Makefile.am    2014-07-24 09:57:42 +0900 (1d83fbc)
@@ -16,7 +16,9 @@ libgrnxx_la_SOURCES =			\
 	index.cpp			\
 	library.cpp			\
 	name.cpp			\
+	order.cpp			\
 	record.cpp			\
+	sorter.cpp			\
 	table.cpp			\
 	types.cpp
 

  Added: lib/grnxx/order.cpp (+70 -0) 100644
===================================================================
--- /dev/null
+++ lib/grnxx/order.cpp    2014-07-24 09:57:42 +0900 (1c66416)
@@ -0,0 +1,70 @@
+#include "grnxx/order.hpp"
+
+#include "grnxx/error.hpp"
+#include "grnxx/expression.hpp"
+
+namespace grnxx {
+
+OrderSet::~OrderSet() {}
+
+OrderSet::OrderSet() : orders_() {}
+
+bool OrderSet::append(Error *error, Order &&order) {
+  try {
+    orders_.push_back(std::move(order));
+    return true;
+  } catch (...) {
+    GRNXX_ERROR_SET(error, NO_MEMORY, "Memory allocation failed");
+    return false;
+  }
+}
+
+unique_ptr<OrderSetBuilder> OrderSetBuilder::create(Error *error,
+                                                    const Table *table) {
+  unique_ptr<OrderSetBuilder> builder(new (nothrow) OrderSetBuilder);
+  if (!builder) {
+    GRNXX_ERROR_SET(error, NO_MEMORY, "Memory allocation failed");
+    return nullptr;
+  }
+  builder->table_ = table;
+  return builder;
+}
+
+OrderSetBuilder::~OrderSetBuilder() {}
+
+bool OrderSetBuilder::append(Error *error,
+                             unique_ptr<Expression> &&expression,
+                             OrderType type) {
+  try {
+    orders_.push_back(Order(std::move(expression), type));
+    return true;
+  } catch (...) {
+    GRNXX_ERROR_SET(error, NO_MEMORY, "Memory allocation failed");
+    return false;
+  }
+}
+
+void OrderSetBuilder::clear() {
+  orders_.clear();
+}
+
+unique_ptr<OrderSet> OrderSetBuilder::release(Error *error) {
+  unique_ptr<OrderSet> order_set(new (nothrow) OrderSet);
+  if (!order_set) {
+    GRNXX_ERROR_SET(error, NO_MEMORY, "Memory allocation failed");
+    return nullptr;
+  }
+  // TODO: The current status should not be broken on failure.
+  for (size_t i = 0; i < orders_.size(); ++i) {
+    if (!order_set->append(error, std::move(orders_[i]))) {
+      orders_.clear();
+      return nullptr;
+    }
+  }
+  orders_.clear();
+  return order_set;
+}
+
+OrderSetBuilder::OrderSetBuilder() : table_(nullptr), orders_() {};
+
+}  // namespace grnxx

  Added: lib/grnxx/sorter.cpp (+7 -0) 100644
===================================================================
--- /dev/null
+++ lib/grnxx/sorter.cpp    2014-07-24 09:57:42 +0900 (0a63eef)
@@ -0,0 +1,7 @@
+#include "grnxx/sorter.hpp"
+
+namespace grnxx {
+
+// TODO
+
+}  // namespace grnxx
-------------- next part --------------
HTML����������������������������...
Download 



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