(final CompileContext cc)
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public final Expr optimize(final CompileContext cc) throws QueryException { |
| 155 | // no root, no nesting: assign context value (can be null) |
| 156 | if(root == null && !cc.nestedFocus()) root = cc.qc.focus.value; |
| 157 | |
| 158 | // remove redundant steps, find empty steps |
| 159 | Expr expr = simplify(cc); |
| 160 | if(expr != this) return expr; |
| 161 | |
| 162 | // flatten nested path expressions |
| 163 | expr = flatten(cc); |
| 164 | // rewrite list to union expressions |
| 165 | if(expr == this) expr = toUnion(cc); |
| 166 | // merge adjacent steps |
| 167 | if(expr == this) expr = mergeSteps(cc); |
| 168 | // move predicates downward |
| 169 | if(expr == this) expr = movePredicates(cc); |
| 170 | // return optimized expression |
| 171 | if(expr != this) return expr.optimize(cc); |
| 172 | |
| 173 | // assign sequence type, compute result size |
| 174 | final Expr rt = root != null ? root : cc.qc.focus.value; |
| 175 | seqType(rt); |
| 176 | |
| 177 | // remove paths that will yield no result |
| 178 | expr = removeEmpty(cc, rt); |
| 179 | // rewrite to simple map |
| 180 | if(expr == this) expr = toMap(cc); |
| 181 | // check index access |
| 182 | if(expr == this) expr = index(cc, rt); |
| 183 | /* rewrite descendant to child steps. this optimization is called after the index rewritings, |
| 184 | * as it is cheaper to invert a descendant step. examples: |
| 185 | * - //B [. = '...'] → IA('...', B) |
| 186 | * - /A/B[. = '...'] → IA('...', B)/parent::A *[parent::document-node()] */ |
| 187 | if(expr == this) expr = children(cc, rt); |
| 188 | // return optimized expression |
| 189 | if(expr != this) return expr; |
| 190 | |
| 191 | // choose the best path implementation (dummy will be used for type checking) |
| 192 | return copyType(get(info, root == null && rt instanceof Dummy ? rt : root, steps)); |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public final Expr simplifyFor(final Simplify mode, final CompileContext cc) |
no test coverage detected