PageRenderTime 24ms CodeModel.GetById 17ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.0.0-rc0/hive/external/ql/src/java/org/apache/hadoop/hive/ql/lib/DefaultRuleDispatcher.java

#
Java | 94 lines | 38 code | 11 blank | 45 comment | 9 complexity | 0e65f7a760751f4878a14b61d463aa0d MD5 | raw file
 1/**
 2 * Licensed to the Apache Software Foundation (ASF) under one
 3 * or more contributor license agreements.  See the NOTICE file
 4 * distributed with this work for additional information
 5 * regarding copyright ownership.  The ASF licenses this file
 6 * to you under the Apache License, Version 2.0 (the
 7 * "License"); you may not use this file except in compliance
 8 * with the License.  You may obtain a copy of the License at
 9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19package org.apache.hadoop.hive.ql.lib;
20
21import java.util.Map;
22import java.util.Stack;
23
24import org.apache.hadoop.hive.ql.parse.SemanticException;
25
26/**
27 * Dispatches calls to relevant method in processor. The user registers various
28 * rules with the dispatcher, and the processor corresponding to closest
29 * matching rule is fired.
30 */
31public class DefaultRuleDispatcher implements Dispatcher {
32
33  private final Map<Rule, NodeProcessor> procRules;
34  private final NodeProcessorCtx procCtx;
35  private final NodeProcessor defaultProc;
36
37  /**
38   * Constructor.
39   * 
40   * @param defaultProc
41   *          default processor to be fired if no rule matches
42   * @param rules
43   *          operator processor that handles actual processing of the node
44   * @param procCtx
45   *          operator processor context, which is opaque to the dispatcher
46   */
47  public DefaultRuleDispatcher(NodeProcessor defaultProc,
48      Map<Rule, NodeProcessor> rules, NodeProcessorCtx procCtx) {
49    this.defaultProc = defaultProc;
50    procRules = rules;
51    this.procCtx = procCtx;
52  }
53
54  /**
55   * Dispatcher function.
56   * 
57   * @param nd
58   *          operator to process
59   * @param ndStack
60   *          the operators encountered so far
61   * @throws SemanticException
62   */
63  public Object dispatch(Node nd, Stack<Node> ndStack, Object... nodeOutputs)
64      throws SemanticException {
65
66    // find the firing rule
67    // find the rule from the stack specified
68    Rule rule = null;
69    int minCost = Integer.MAX_VALUE;
70    for (Rule r : procRules.keySet()) {
71      int cost = r.cost(ndStack);
72      if ((cost >= 0) && (cost <= minCost)) {
73        minCost = cost;
74        rule = r;
75      }
76    }
77
78    NodeProcessor proc;
79
80    if (rule == null) {
81      proc = defaultProc;
82    } else {
83      proc = procRules.get(rule);
84    }
85
86    // Do nothing in case proc is null
87    if (proc != null) {
88      // Call the process function
89      return proc.process(nd, ndStack, procCtx, nodeOutputs);
90    } else {
91      return null;
92    }
93  }
94}