[Groonga-commit] groonga/grnxx at 03f2081 [master] Add operators STARTS_WITH, ENDS_WITH, and CONTAINS. (#118)

Back to archive index

susumu.yata null+****@clear*****
Tue Dec 16 10:45:53 JST 2014


susumu.yata	2014-11-25 19:23:48 +0900 (Tue, 25 Nov 2014)

  New Revision: 03f20813dd24078785ec7400ffdc0a1a4aabe75e
  https://github.com/groonga/grnxx/commit/03f20813dd24078785ec7400ffdc0a1a4aabe75e

  Message:
    Add operators STARTS_WITH, ENDS_WITH, and CONTAINS. (#118)

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

  Modified: include/grnxx/expression.hpp (+7 -1)
===================================================================
--- include/grnxx/expression.hpp    2014-11-25 19:03:17 +0900 (0fef7f8)
+++ include/grnxx/expression.hpp    2014-11-25 19:23:48 +0900 (ba90ced)
@@ -55,7 +55,13 @@ enum OperatorType {
   DIVISION_OPERATOR,        // For Int, Float.
   MODULUS_OPERATOR,         // For Int.
 
-  // Array operators.
+  // TODO: The following operators can support Vector.
+  // Search operators.
+  STARTS_WITH_OPERATOR,  // For Text.
+  ENDS_WITH_OPERATOR,    // For Text.
+  CONTAINS_OPERATOR,     // For Text.
+
+  // Vector operators.
   SUBSCRIPT_OPERATOR,
 
   // -- Ternary operators --

  Modified: lib/grnxx/impl/expression.cpp (+82 -0)
===================================================================
--- lib/grnxx/impl/expression.cpp    2014-11-25 19:03:17 +0900 (95d82d9)
+++ lib/grnxx/impl/expression.cpp    2014-11-25 19:23:48 +0900 (be2c1ce)
@@ -1234,6 +1234,51 @@ struct ModulusOperator {
 template <typename T>
 using ModulusNode = GenericBinaryNode<ModulusOperator<T>>;
 
+// ----- StartsWithNode -----
+
+template <typename T>
+struct StartsWithOperator {
+  using Value = Bool;
+  using Arg1 = T;
+  using Arg2 = T;
+  Value operator()(const Arg1 &arg1, const Arg2 &arg2) const {
+    return arg1.starts_with(arg2);
+  }
+};
+
+template <typename T>
+using StartsWithNode = GenericBinaryNode<StartsWithOperator<T>>;
+
+// ----- EndsWithNode -----
+
+template <typename T>
+struct EndsWithOperator {
+  using Value = Bool;
+  using Arg1 = T;
+  using Arg2 = T;
+  Value operator()(const Arg1 &arg1, const Arg2 &arg2) const {
+    return arg1.ends_with(arg2);
+  }
+};
+
+template <typename T>
+using EndsWithNode = GenericBinaryNode<EndsWithOperator<T>>;
+
+// ----- ContainsNode -----
+
+template <typename T>
+struct ContainsOperator {
+  using Value = Bool;
+  using Arg1 = T;
+  using Arg2 = T;
+  Value operator()(const Arg1 &arg1, const Arg2 &arg2) const {
+    return arg1.contains(arg2);
+  }
+};
+
+template <typename T>
+using ContainsNode = GenericBinaryNode<ContainsOperator<T>>;
+
 // ---- SubscriptNode ----
 
 template <typename T>
@@ -2130,6 +2175,20 @@ Node *ExpressionBuilder::create_binary_node(
         }
       }
     }
+    case STARTS_WITH_OPERATOR:
+    case ENDS_WITH_OPERATOR:
+    case CONTAINS_OPERATOR: {
+      switch (arg1->data_type()) {
+        case TEXT_DATA: {
+          return create_search_node<Text>(
+              operator_type, std::move(arg1), std::move(arg2));
+        }
+        // TODO: Search nodes can support Vector.
+        default: {
+          throw "Invalid data type";  // TODO
+        }
+      }
+    }
     case SUBSCRIPT_OPERATOR: {
       return create_subscript_node(std::move(arg1), std::move(arg2));
     }
@@ -2244,6 +2303,29 @@ Node *ExpressionBuilder::create_arithmetic_node(
   }
 }
 
+template <typename T>
+Node *ExpressionBuilder::create_search_node(OperatorType operator_type,
+                                            std::unique_ptr<Node> &&arg1,
+                                            std::unique_ptr<Node> &&arg2) {
+  if (arg1->data_type() != arg2->data_type()) {
+    throw "Data type conflict";  // TODO
+  }
+  switch (operator_type) {
+    case STARTS_WITH_OPERATOR: {
+      return new StartsWithNode<T>(std::move(arg1), std::move(arg2));
+    }
+    case ENDS_WITH_OPERATOR: {
+      return new EndsWithNode<T>(std::move(arg1), std::move(arg2));
+    }
+    case CONTAINS_OPERATOR: {
+      return new ContainsNode<T>(std::move(arg1), std::move(arg2));
+    }
+    default: {
+      throw "Invalid operator";  // TODO
+    }
+  }
+}
+
 Node *ExpressionBuilder::create_subscript_node(std::unique_ptr<Node> &&arg1,
                                                std::unique_ptr<Node> &&arg2) {
   if (arg2->data_type() != INT_DATA) {

  Modified: lib/grnxx/impl/expression.hpp (+8 -0)
===================================================================
--- lib/grnxx/impl/expression.hpp    2014-11-25 19:03:17 +0900 (e57dba2)
+++ lib/grnxx/impl/expression.hpp    2014-11-25 19:23:48 +0900 (7fe7a1f)
@@ -180,6 +180,14 @@ class ExpressionBuilder : public ExpressionBuilderInterface {
                                std::unique_ptr<Node> &&arg1,
                                std::unique_ptr<Node> &&arg2);
 
+  // Create a node associated with a search operator.
+  //
+  // On failure, throws an exception.
+  template <typename T>
+  Node *create_search_node(OperatorType operator_type,
+                           std::unique_ptr<Node> &&arg1,
+                           std::unique_ptr<Node> &&arg2);
+
   // Create a node associated with a subscript operator.
   //
   // On failure, throws an exception.
-------------- next part --------------
HTML����������������������������...
Descargar 



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