[Groonga-commit] groonga/grnxx at ab41194 [master] Update Expression::evaluate() to use Subarray.

Back to archive index

susumu.yata null+****@clear*****
Tue Aug 5 17:57:36 JST 2014


susumu.yata	2014-08-05 17:57:36 +0900 (Tue, 05 Aug 2014)

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

  Message:
    Update Expression::evaluate() to use Subarray.

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

  Modified: include/grnxx/expression.hpp (+17 -2)
===================================================================
--- include/grnxx/expression.hpp    2014-08-05 17:56:11 +0900 (2ede2b6)
+++ include/grnxx/expression.hpp    2014-08-05 17:57:36 +0900 (9e543a3)
@@ -105,9 +105,9 @@ class Expression {
   //
   // The result is stored into "*results".
   //
-  // TODO: should append results to the end of "*results"?
+  // Fails if "T" is different from the result data type.
   //
-  // Returns true on success.
+  // On success, returns true.
   // On failure, returns false and stores error information into "*error" if
   // "error" != nullptr.
   template <typename T>
@@ -115,6 +115,21 @@ class Expression {
                 const RecordSubset &record_set,
                 Array<T> *results);
 
+  // Evaluate the expression.
+  //
+  // The result is stored into "*results".
+  //
+  // Fails if the number of records is different from the size of "*results".
+  // Fails if "T" is different from the result data type.
+  //
+  // On success, returns true.
+  // On failure, returns false and stores error information into "*error" if
+  // "error" != nullptr.
+  template <typename T>
+  bool evaluate(Error *error,
+                const RecordSubset &record_set,
+                Subarray<T> *results);
+
  private:
   const Table *table_;
   unique_ptr<ExpressionNode> root_;

  Modified: lib/grnxx/expression.cpp (+28 -6)
===================================================================
--- lib/grnxx/expression.cpp    2014-08-05 17:56:11 +0900 (0b844fe)
+++ lib/grnxx/expression.cpp    2014-08-05 17:57:36 +0900 (89435be)
@@ -739,17 +739,15 @@ template <typename T>
 bool Expression::evaluate(Error *error,
                           const RecordSubset &record_set,
                           Array<T> *results) {
-  Node<T> *node = static_cast<Node<T> *>(root_.get());
-  if (!node->evaluate(error, record_set)) {
+  if (TypeTraits<T>::data_type() != data_type()) {
+    GRNXX_ERROR_SET(error, INVALID_ARGUMENT, "Invalid data type");
     return false;
   }
   if (!results->resize(error, record_set.size())) {
     return false;
   }
-  for (Int i = 0; i < results->size(); ++i) {
-    (*results)[i] = node->get(i);
-  }
-  return true;
+  Subarray<T> subarray = results->subarray();
+  return evaluate(error, record_set, &subarray);
 }
 
 template bool Expression::evaluate(Error *error,
@@ -771,6 +769,30 @@ template bool Expression::evaluate(Error *error,
                                    const RecordSubset &record_set,
                                    Array<Text> *results);
 
+template <typename T>
+bool Expression::evaluate(Error *error,
+                          const RecordSubset &record_set,
+                          Subarray<T> *results) {
+  if (TypeTraits<T>::data_type() != data_type()) {
+    GRNXX_ERROR_SET(error, INVALID_ARGUMENT, "Invalid data type");
+    return false;
+  }
+  if (record_set.size() != results->size()) {
+    GRNXX_ERROR_SET(error, INVALID_ARGUMENT, "Size conflict: "
+                    "#records = %" PRIi64 ", #results = %" PRIi64,
+                    record_set.size(), results->size());
+    return false;
+  }
+  Node<T> *node = static_cast<Node<T> *>(root_.get());
+  if (!node->evaluate(error, record_set)) {
+    return false;
+  }
+  for (Int i = 0; i < results->size(); ++i) {
+    (*results)[i] = node->get(i);
+  }
+  return true;
+}
+
 Expression::Expression(const Table *table, unique_ptr<ExpressionNode> &&root)
     : table_(table),
       root_(std::move(root)) {}
-------------- next part --------------
HTML����������������������������...
下载 



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