[Groonga-commit] groonga/groonga at b49bee5 [master] mrb: support building tree from stack based expression

Back to archive index

Kouhei Sutou null+****@clear*****
Thu Apr 7 10:30:58 JST 2016


Kouhei Sutou	2016-04-07 10:30:58 +0900 (Thu, 07 Apr 2016)

  New Revision: b49bee5ec1ec0a7e6a45854eb301d93e47af31fa
  https://github.com/groonga/groonga/commit/b49bee5ec1ec0a7e6a45854eb301d93e47af31fa

  Message:
    mrb: support building tree from stack based expression

  Added files:
    lib/mrb/scripts/expression_tree.rb
    lib/mrb/scripts/expression_tree/Makefile.am
    lib/mrb/scripts/expression_tree/binary_operation.rb
    lib/mrb/scripts/expression_tree/logical_operation.rb
    lib/mrb/scripts/expression_tree/sources.am
    lib/mrb/scripts/expression_tree/value.rb
    lib/mrb/scripts/expression_tree_builder.rb
  Modified files:
    configure.ac
    lib/mrb/scripts/Makefile.am
    lib/mrb/scripts/expression.rb
    lib/mrb/scripts/sources.am

  Modified: configure.ac (+1 -0)
===================================================================
--- configure.ac    2016-04-07 00:05:13 +0900 (c7d688a)
+++ configure.ac    2016-04-07 10:30:58 +0900 (5c297df)
@@ -243,6 +243,7 @@ AC_CONFIG_FILES([
   lib/mrb/scripts/Makefile
   lib/mrb/scripts/command_line/Makefile
   lib/mrb/scripts/context/Makefile
+  lib/mrb/scripts/expression_tree/Makefile
   lib/mrb/scripts/initialize/Makefile
   lib/mrb/scripts/logger/Makefile
   lib/mrb/scripts/query_logger/Makefile

  Modified: lib/mrb/scripts/Makefile.am (+1 -0)
===================================================================
--- lib/mrb/scripts/Makefile.am    2016-04-07 00:05:13 +0900 (6f57ef9)
+++ lib/mrb/scripts/Makefile.am    2016-04-07 10:30:58 +0900 (835d881)
@@ -1,6 +1,7 @@
 SUBDIRS =					\
 	command_line				\
 	context					\
+	expression_tree				\
 	initialize				\
 	logger					\
 	query_logger

  Modified: lib/mrb/scripts/expression.rb (+2 -0)
===================================================================
--- lib/mrb/scripts/expression.rb    2016-04-07 00:05:13 +0900 (69a0fee)
+++ lib/mrb/scripts/expression.rb    2016-04-07 10:30:58 +0900 (04f29ac)
@@ -1,6 +1,8 @@
 require "expression_rewriter"
 require "expression_rewriters"
 
+require "expression_tree_builder"
+
 require "scan_info"
 require "scan_info_builder"
 

  Added: lib/mrb/scripts/expression_tree.rb (+3 -0) 100644
===================================================================
--- /dev/null
+++ lib/mrb/scripts/expression_tree.rb    2016-04-07 10:30:58 +0900 (d22baa5)
@@ -0,0 +1,3 @@
+require "expression_tree/binary_operation"
+require "expression_tree/logical_operation"
+require "expression_tree/value"

  Added: lib/mrb/scripts/expression_tree/Makefile.am (+9 -0) 100644
===================================================================
--- /dev/null
+++ lib/mrb/scripts/expression_tree/Makefile.am    2016-04-07 10:30:58 +0900 (1235932)
@@ -0,0 +1,9 @@
+include sources.am
+
+EXTRA_DIST =					\
+	$(RUBY_SCRIPT_FILES)
+
+if WITH_MRUBY
+ruby_scripts_expression_treedir = $(ruby_scriptsdir)/expression_tree
+ruby_scripts_expression_tree_DATA = $(RUBY_SCRIPT_FILES)
+endif

  Added: lib/mrb/scripts/expression_tree/binary_operation.rb (+14 -0) 100644
===================================================================
--- /dev/null
+++ lib/mrb/scripts/expression_tree/binary_operation.rb    2016-04-07 10:30:58 +0900 (d725d69)
@@ -0,0 +1,14 @@
+module Groonga
+  module ExpressionTree
+    class BinaryOperation
+      attr_reader :operator
+      attr_reader :left
+      attr_reader :right
+      def initialize(operator, left, right)
+        @operator = operator
+        @left = left
+        @right = right
+      end
+    end
+  end
+end

  Added: lib/mrb/scripts/expression_tree/logical_operation.rb (+12 -0) 100644
===================================================================
--- /dev/null
+++ lib/mrb/scripts/expression_tree/logical_operation.rb    2016-04-07 10:30:58 +0900 (eeacdb0)
@@ -0,0 +1,12 @@
+module Groonga
+  module ExpressionTree
+    class LogicalOperation
+      attr_reader :operator
+      attr_reader :nodes
+      def initialize(operator, nodes)
+        @operator = operator
+        @nodes = nodes
+      end
+    end
+  end
+end

  Added: lib/mrb/scripts/expression_tree/sources.am (+4 -0) 100644
===================================================================
--- /dev/null
+++ lib/mrb/scripts/expression_tree/sources.am    2016-04-07 10:30:58 +0900 (184be7c)
@@ -0,0 +1,4 @@
+RUBY_SCRIPT_FILES =				\
+	binary_operation.rb			\
+	logical_operation.rb			\
+	value.rb

  Added: lib/mrb/scripts/expression_tree/value.rb (+10 -0) 100644
===================================================================
--- /dev/null
+++ lib/mrb/scripts/expression_tree/value.rb    2016-04-07 10:30:58 +0900 (df0e211)
@@ -0,0 +1,10 @@
+module Groonga
+  module ExpressionTree
+    class Value
+      attr_reader :code
+      def initialize(code)
+        @code = code
+      end
+    end
+  end
+end

  Added: lib/mrb/scripts/expression_tree_builder.rb (+89 -0) 100644
===================================================================
--- /dev/null
+++ lib/mrb/scripts/expression_tree_builder.rb    2016-04-07 10:30:58 +0900 (1cf5da5)
@@ -0,0 +1,89 @@
+require "expression_tree"
+
+module Groonga
+  class ExpressionTreeBuilder
+    RELATION_OPERATORS = [
+      Operator::MATCH,
+      Operator::NEAR,
+      Operator::NEAR2,
+      Operator::SIMILAR,
+      Operator::PREFIX,
+      Operator::SUFFIX,
+      Operator::EQUAL,
+      Operator::NOT_EQUAL,
+      Operator::LESS,
+      Operator::GREATER,
+      Operator::LESS_EQUAL,
+      Operator::GREATER_EQUAL,
+      Operator::GEO_WITHINP5,
+      Operator::GEO_WITHINP6,
+      Operator::GEO_WITHINP8,
+      Operator::TERM_EXTRACT,
+      Operator::REGEXP,
+      Operator::FUZZY,
+    ]
+
+    ARITHMETIC_OPERATORS = [
+      Operator::BITWISE_OR,
+      Operator::BITWISE_XOR,
+      Operator::BITWISE_AND,
+      Operator::BITWISE_NOT,
+      Operator::SHIFTL,
+      Operator::SHIFTR,
+      Operator::SHIFTRR,
+      Operator::PLUS,
+      Operator::MINUS,
+      Operator::STAR,
+      Operator::MOD,
+    ]
+
+    LOGICAL_OPERATORS = [
+      Operator::AND,
+      Operator::OR,
+      Operator::AND_NOT,
+      Operator::ADJUST,
+    ]
+
+    def initialize(expression)
+      @expression = expression
+    end
+
+    def build
+      stack = []
+      codes =****@expre*****
+      codes.each do |code|
+        case code.op
+        when *LOGICAL_OPERATORS
+          right = stack.pop
+          left = stack.pop
+          nodes = []
+          add_logical_operation_node(code.op, nodes, left)
+          add_logical_operation_node(code.op, nodes, right)
+          node = ExpressionTree::LogicalOperation.new(code.op, nodes)
+          stack.push(node)
+        when *RELATION_OPERATORS, *ARITHMETIC_OPERATORS
+          right = stack.pop
+          left = stack.pop
+          node = ExpressionTree::BinaryOperation.new(code.op, left, right)
+          stack.push(node)
+        when Operator::GET_VALUE, Operator::PUSH
+          node = ExpressionTree::Value.new(code)
+          stack.push(node)
+        else
+          raise "unknown operator: #{code.inspect}"
+        end
+      end
+      stack.pop
+    end
+
+    private
+    def add_logical_operation_node(operator, nodes, node)
+      if node.is_a?(ExpressionTree::LogicalOperation) and
+          node.operator == operator
+        nodes.concat(node.nodes)
+      else
+        nodes << node
+      end
+    end
+  end
+end

  Modified: lib/mrb/scripts/sources.am (+2 -0)
===================================================================
--- lib/mrb/scripts/sources.am    2016-04-07 00:05:13 +0900 (07cfa48)
+++ lib/mrb/scripts/sources.am    2016-04-07 10:30:58 +0900 (718c8d0)
@@ -11,6 +11,8 @@ RUBY_SCRIPT_FILES =				\
 	expression_rewriter.rb			\
 	expression_rewriters.rb			\
 	expression_size_estimator.rb		\
+	expression_tree.rb			\
+	expression_tree_builder.rb		\
 	fixed_size_column.rb			\
 	id.rb					\
 	index_column.rb				\
-------------- next part --------------
HTML����������������������������...
Télécharger 



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