1 10 package mondrian.olap.fun; 11 12 import mondrian.olap.*; 13 import mondrian.olap.type.Type; 14 import mondrian.calc.*; 15 import mondrian.calc.impl.GenericCalc; 16 import mondrian.mdx.ResolvedFunCall; 17 18 import java.io.PrintWriter ; 19 20 28 public class CacheFunDef extends FunDefBase { 29 private final Type type; 30 private static final String NAME = "$Cache"; 31 private static final String SIGNATURE = "$Cache(<<Exp>>)"; 32 private static final String DESCRIPTION = "Evaluates and returns its sole argument, applying statement-level caching"; 33 private static final Syntax SYNTAX = Syntax.Internal; 34 private ExpCacheDescriptor cacheDescriptor; 35 static final CacheFunResolver Resolver = new CacheFunResolver(); 36 37 CacheFunDef( 38 String name, 39 String signature, 40 String description, 41 Syntax syntax, 42 int category, 43 Type type) { 44 super(name, signature, description, syntax, 45 category, new int[] {category}); 46 this.type = type; 47 } 48 49 public void unparse(Exp[] args, PrintWriter pw) { 50 args[0].unparse(pw); 51 } 52 53 public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) { 54 final Exp exp = call.getArg(0); 55 final ExpCacheDescriptor cacheDescriptor = 56 new ExpCacheDescriptor(exp, compiler); 57 return new GenericCalc(call) { 58 public Object evaluate(Evaluator evaluator) { 59 return evaluator.getCachedResult(cacheDescriptor); 60 } 61 62 public Calc[] getCalcs() { 63 return new Calc[] {cacheDescriptor.getCalc()}; 64 } 65 }; 66 } 67 68 public static class CacheFunResolver extends ResolverBase { 69 CacheFunResolver() { 70 super(NAME, SIGNATURE, DESCRIPTION, SYNTAX); 71 } 72 73 public FunDef resolve( 74 Exp[] args, Validator validator, int[] conversionCount) { 75 if (args.length != 1) { 76 return null; 77 } 78 final Exp exp = args[0]; 79 final int category = exp.getCategory(); 80 final Type type = exp.getType(); 81 return new CacheFunDef(NAME, SIGNATURE, DESCRIPTION, SYNTAX, 82 category, type); 83 } 84 85 public boolean requiresExpression(int k) { 86 return false; 87 } 88 } 89 } 90 91 | Popular Tags |