Search in sources :

Example 1 with FunctionTemplate

use of com.srotya.sidewinder.core.functions.iterative.FunctionIteratorFactory.FunctionTemplate in project sidewinder by srotya.

the class MiscUtils method createIteratorChain.

public static FunctionIteratorFactory createIteratorChain(String[] parts, int startIndex) throws Exception {
    List<FunctionTemplate> templates = new ArrayList<>();
    for (int k = startIndex; k < parts.length; k++) {
        String[] args = parts[k].split(",");
        Class<? extends FunctionIterator> lookupFunction = FunctionIteratorTable.get().lookupFunction(args[0]);
        if (lookupFunction == null) {
            throw new BadRequestException("Unknown function:" + args[0]);
        }
        FunctionIterator instance = FunctionIterator.getDummyInstance(lookupFunction);
        if (args.length - 1 < instance.getNumberOfArgs()) {
            throw new BadRequestException("Insufficient arguments for aggregation function, needed:" + instance.getNumberOfArgs() + ", found:" + (args.length - 1));
        }
        Object[] ary = new Object[args.length - 1];
        for (int i = 1; i < args.length; i++) {
            Matcher matcher = NUMBER.matcher(args[i]);
            if (matcher.matches()) {
                if (matcher.group(1) != null) {
                    ary[i - 1] = Double.parseDouble(args[i]);
                } else {
                    ary[i - 1] = Integer.parseInt(args[i]);
                }
            } else {
                ary[i - 1] = args[i];
            }
        }
        instance.init(ary);
        templates.add(new FunctionTemplate(lookupFunction, ary));
    }
    return new FunctionIteratorFactory(templates);
}
Also used : FunctionIterator(com.srotya.sidewinder.core.functions.iterative.FunctionIterator) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) ByteString(com.srotya.sidewinder.core.storage.ByteString) DataPoint(com.srotya.sidewinder.core.storage.DataPoint) Point(com.srotya.sidewinder.core.rpc.Point) FunctionTemplate(com.srotya.sidewinder.core.functions.iterative.FunctionIteratorFactory.FunctionTemplate) BadRequestException(javax.ws.rs.BadRequestException) FunctionIteratorFactory(com.srotya.sidewinder.core.functions.iterative.FunctionIteratorFactory)

Aggregations

FunctionIterator (com.srotya.sidewinder.core.functions.iterative.FunctionIterator)1 FunctionIteratorFactory (com.srotya.sidewinder.core.functions.iterative.FunctionIteratorFactory)1 FunctionTemplate (com.srotya.sidewinder.core.functions.iterative.FunctionIteratorFactory.FunctionTemplate)1 Point (com.srotya.sidewinder.core.rpc.Point)1 ByteString (com.srotya.sidewinder.core.storage.ByteString)1 DataPoint (com.srotya.sidewinder.core.storage.DataPoint)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 BadRequestException (javax.ws.rs.BadRequestException)1