[Groonga-commit] groonga/grnxx at e84b775 [master] Add Expression::adjust().

Back to archive index

susumu.yata null+****@clear*****
Tue Jul 22 17:16:44 JST 2014


susumu.yata	2014-07-22 17:16:44 +0900 (Tue, 22 Jul 2014)

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

  Message:
    Add Expression::adjust().

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

  Modified: include/grnxx/expression.hpp (+10 -0)
===================================================================
--- include/grnxx/expression.hpp    2014-07-22 15:40:28 +0900 (852bbd2)
+++ include/grnxx/expression.hpp    2014-07-22 17:16:44 +0900 (313c658)
@@ -92,6 +92,16 @@ class Expression {
   // "error" != nullptr.
   bool filter(Error *error, RecordSet *record_set);
 
+  // Adjust scores of records.
+  //
+  // Evaluates the expression for the given record set and replaces their
+  // scores with the evaluation results.
+  //
+  // Returns true on success.
+  // On failure, returns false and stores error information into "*error" if
+  // "error" != nullptr.
+  bool adjust(Error *error, RecordSet *record_set);
+
  private:
   const Table *table_;
   unique_ptr<ExpressionNode> root_;

  Modified: lib/grnxx/expression.cpp (+35 -0)
===================================================================
--- lib/grnxx/expression.cpp    2014-07-22 15:40:28 +0900 (1f100e5)
+++ lib/grnxx/expression.cpp    2014-07-22 17:16:44 +0900 (02c7b70)
@@ -41,6 +41,16 @@ class ExpressionNode {
   // "error" != nullptr.
   virtual bool filter(Error *error, RecordSet *record_set) = 0;
 
+  // Adjust scores of records.
+  //
+  // Evaluates the expression for the given record set and replaces their
+  // scores with the evaluation results.
+  //
+  // Returns true on success.
+  // On failure, returns false and stores error information into "*error" if
+  // "error" != nullptr.
+  virtual bool adjust(Error *error, RecordSet *record_set) = 0;
+
   // Evaluate the expression subtree.
   //
   // The evaluation results are stored into each expression node.
@@ -64,6 +74,7 @@ class Node : public ExpressionNode {
   }
 
   virtual bool filter(Error *error, RecordSet *record_set);
+  virtual bool adjust(Error *error, RecordSet *record_set);
 
   virtual bool evaluate(Error *error, const RecordSet &record_set);
 
@@ -98,6 +109,26 @@ bool Node<Bool>::filter(Error *error, RecordSet *record_set) {
 }
 
 template <typename T>
+bool Node<T>::adjust(Error *error, RecordSet *record_set) {
+  // TODO: Define "This type is not supported" error.
+  GRNXX_ERROR_SET(error, NOT_SUPPORTED_YET, "Not supported yet");
+  return false;
+}
+
+template <>
+bool Node<Float>::adjust(Error *error, RecordSet *record_set) {
+  if (!evaluate(error, *record_set)) {
+    return false;
+  }
+  for (Int i = 0; i < record_set->size(); ++i) {
+    Record record = record_set->get(i);
+    record.score = values_[i];
+    record_set->set(i, record);
+  }
+  return true;
+}
+
+template <typename T>
 bool Node<T>::evaluate(Error *error, const RecordSet &record_set) {
   // TODO: This should be a pure virtual function.
   GRNXX_ERROR_SET(error, NOT_SUPPORTED_YET, "Not supported yet");
@@ -653,6 +684,10 @@ bool Expression::filter(Error *error, RecordSet *record_set) {
   return root_->filter(error, record_set);
 }
 
+bool Expression::adjust(Error *error, RecordSet *record_set) {
+  return root_->adjust(error, record_set);
+}
+
 Expression::Expression(const Table *table, unique_ptr<ExpressionNode> &&root)
     : table_(table),
       root_(std::move(root)) {}

  Modified: test/test_grnxx.cpp (+20 -0)
===================================================================
--- test/test_grnxx.cpp    2014-07-22 15:40:28 +0900 (b82c2ba)
+++ test/test_grnxx.cpp    2014-07-22 17:16:44 +0900 (8a101e4)
@@ -718,6 +718,26 @@ void test_expression() {
   assert(expression->filter(&error, &record_set));
   assert(record_set.size() == 1);
   assert(record_set.get(0).row_id == 2);
+
+  record_set.clear();
+  cursor = table->create_cursor(&error);
+  assert(cursor);
+  assert(cursor->read_all(&error, &record_set) == 2);
+
+  // スコア計算を試す.
+  assert(builder->push_column(&error, "_score"));
+  assert(builder->push_datum(&error, grnxx::Float(1.0)));
+  assert(builder->push_operator(&error, grnxx::PLUS_OPERATOR));
+  expression = builder->release(&error);
+  assert(expression);
+
+  // スコア調整に使ったときの結果を確認する.
+  assert(expression->adjust(&error, &record_set));
+  assert(record_set.size() == 2);
+  assert(record_set.get(0).row_id == 1);
+  assert(record_set.get(0).score == 1.0);
+  assert(record_set.get(1).row_id == 2);
+  assert(record_set.get(1).score == 1.0);
 }
 
 }  // namespace
-------------- next part --------------
HTML����������������������������...
Download 



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