[Groonga-commit] groonga/grnxx at 540ba5a [master] Add Expression::evaluate().

Back to archive index

susumu.yata null+****@clear*****
Wed Jul 23 14:22:08 JST 2014


susumu.yata	2014-07-23 14:22:08 +0900 (Wed, 23 Jul 2014)

  New Revision: 540ba5a9752ad8a764700afcb4b1165a872d1fa5
  https://github.com/groonga/grnxx/commit/540ba5a9752ad8a764700afcb4b1165a872d1fa5

  Message:
    Add Expression::evaluate().

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

  Modified: include/grnxx/expression.hpp (+12 -0)
===================================================================
--- include/grnxx/expression.hpp    2014-07-22 18:10:37 +0900 (313c658)
+++ include/grnxx/expression.hpp    2014-07-23 14:22:08 +0900 (cb3f9f8)
@@ -102,6 +102,18 @@ class Expression {
   // "error" != nullptr.
   bool adjust(Error *error, RecordSet *record_set);
 
+  // Evaluate the expression.
+  //
+  // The result is stored into "*result_set".
+  //
+  // Returns true on success.
+  // On failure, returns false and stores error information into "*error" if
+  // "error" != nullptr.
+  template <typename T>
+  bool evaluate(Error *error,
+                const RecordSet &record_set,
+                std::vector<T> *result_set);
+
  private:
   const Table *table_;
   unique_ptr<ExpressionNode> root_;

  Modified: lib/grnxx/expression.cpp (+39 -0)
===================================================================
--- lib/grnxx/expression.cpp    2014-07-22 18:10:37 +0900 (02c7b70)
+++ lib/grnxx/expression.cpp    2014-07-23 14:22:08 +0900 (272221c)
@@ -688,6 +688,45 @@ bool Expression::adjust(Error *error, RecordSet *record_set) {
   return root_->adjust(error, record_set);
 }
 
+template <typename T>
+bool Expression::evaluate(Error *error,
+                          const RecordSet &record_set,
+                          std::vector<T> *result_set) {
+  Node<T> *node = static_cast<Node<T> *>(root_.get());
+  if (!node->evaluate(error, record_set)) {
+    return false;
+  }
+  try {
+    result_set->resize(record_set.size());
+  } catch (...) {
+    GRNXX_ERROR_SET(error, NO_MEMORY, "Memory allocation failed");
+    return false;
+  }
+  for (Int i = 0; i < result_set->size(); ++i) {
+    (*result_set)[i] = node->get(i);
+  }
+  return true;
+}
+
+template bool Expression::evaluate(Error *error,
+                                   const RecordSet &record_set,
+                                   std::vector<Bool> *result_set);
+template bool Expression::evaluate(Error *error,
+                                   const RecordSet &record_set,
+                                   std::vector<Int> *result_set);
+template bool Expression::evaluate(Error *error,
+                                   const RecordSet &record_set,
+                                   std::vector<Float> *result_set);
+template bool Expression::evaluate(Error *error,
+                                   const RecordSet &record_set,
+                                   std::vector<Time> *result_set);
+template bool Expression::evaluate(Error *error,
+                                   const RecordSet &record_set,
+                                   std::vector<GeoPoint> *result_set);
+template bool Expression::evaluate(Error *error,
+                                   const RecordSet &record_set,
+                                   std::vector<Text> *result_set);
+
 Expression::Expression(const Table *table, unique_ptr<ExpressionNode> &&root)
     : table_(table),
       root_(std::move(root)) {}

  Modified: test/test_grnxx.cpp (+19 -0)
===================================================================
--- test/test_grnxx.cpp    2014-07-22 18:10:37 +0900 (8a101e4)
+++ test/test_grnxx.cpp    2014-07-23 14:22:08 +0900 (e6199b0)
@@ -738,6 +738,25 @@ void test_expression() {
   assert(record_set.get(0).score == 1.0);
   assert(record_set.get(1).row_id == 2);
   assert(record_set.get(1).score == 1.0);
+
+  record_set.clear();
+  cursor = table->create_cursor(&error);
+  assert(cursor);
+  assert(cursor->read_all(&error, &record_set) == 2);
+
+  // 評価結果を取り出す.
+  assert(builder->push_column(&error, "IntColumn"));
+  assert(builder->push_datum(&error, grnxx::Int(100)));
+  assert(builder->push_operator(&error, grnxx::PLUS_OPERATOR));
+  expression = builder->release(&error);
+  assert(expression);
+
+  // 評価結果を確認する.
+  std::vector<grnxx::Int> result_set;
+  assert(expression->evaluate(&error, record_set, &result_set));
+  assert(result_set.size() == 2);
+  assert(result_set[0] == 223);
+  assert(result_set[1] == 556);
 }
 
 }  // namespace
-------------- next part --------------
HTML����������������������������...
Download 



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