PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms 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
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  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. package org.apache.hadoop.hive.ql.lib;
  19. import java.util.Map;
  20. import java.util.Stack;
  21. import org.apache.hadoop.hive.ql.parse.SemanticException;
  22. /**
  23. * Dispatches calls to relevant method in processor. The user registers various
  24. * rules with the dispatcher, and the processor corresponding to closest
  25. * matching rule is fired.
  26. */
  27. public class DefaultRuleDispatcher implements Dispatcher {
  28. private final Map<Rule, NodeProcessor> procRules;
  29. private final NodeProcessorCtx procCtx;
  30. private final NodeProcessor defaultProc;
  31. /**
  32. * Constructor.
  33. *
  34. * @param defaultProc
  35. * default processor to be fired if no rule matches
  36. * @param rules
  37. * operator processor that handles actual processing of the node
  38. * @param procCtx
  39. * operator processor context, which is opaque to the dispatcher
  40. */
  41. public DefaultRuleDispatcher(NodeProcessor defaultProc,
  42. Map<Rule, NodeProcessor> rules, NodeProcessorCtx procCtx) {
  43. this.defaultProc = defaultProc;
  44. procRules = rules;
  45. this.procCtx = procCtx;
  46. }
  47. /**
  48. * Dispatcher function.
  49. *
  50. * @param nd
  51. * operator to process
  52. * @param ndStack
  53. * the operators encountered so far
  54. * @throws SemanticException
  55. */
  56. public Object dispatch(Node nd, Stack<Node> ndStack, Object... nodeOutputs)
  57. throws SemanticException {
  58. // find the firing rule
  59. // find the rule from the stack specified
  60. Rule rule = null;
  61. int minCost = Integer.MAX_VALUE;
  62. for (Rule r : procRules.keySet()) {
  63. int cost = r.cost(ndStack);
  64. if ((cost >= 0) && (cost <= minCost)) {
  65. minCost = cost;
  66. rule = r;
  67. }
  68. }
  69. NodeProcessor proc;
  70. if (rule == null) {
  71. proc = defaultProc;
  72. } else {
  73. proc = procRules.get(rule);
  74. }
  75. // Do nothing in case proc is null
  76. if (proc != null) {
  77. // Call the process function
  78. return proc.process(nd, ndStack, procCtx, nodeOutputs);
  79. } else {
  80. return null;
  81. }
  82. }
  83. }