Create an Evaluator for the given query sources and projection

@param <T>
@param metadata query metadata
@param sources sources of the query
@param projection projection of the query
@return evaluator
166005
public <T> Evaluator<T> create(QueryMetadata metadata, List<? extends Expression<?>> sources,
            Expression<T> projection) {
        final CollQuerySerializer serializer = new CollQuerySerializer(templates);
        serializer.append("return ");
        if (projection instanceof FactoryExpression<?>) {
            serializer.append("(");
            serializer.append(ClassUtils.getName(projection.getType()));
            serializer.append(")(");
            serializer.handle(projection);
            serializer.append(")");
        } else {
            serializer.handle(projection);
        }
        serializer.append(";");

        Map<Object,String> constantToLabel = serializer.getConstantToLabel();
        Map<String, Object> constants = getConstants(metadata, constantToLabel);
        Class<?>[] types = new Class<?>[sources.size()];
        String[] names = new String[sources.size()];
        for (int i = 0; i < sources.size(); i++) {
            types[i] = sources.get(i).getType();
            names[i] = sources.get(i).toString();
        }

        // normalize types
        for (int i = 0; i < types.length; i++) {
            if (Primitives.isWrapperType(types[i])) {
                types[i] = Primitives.unwrap(types[i]);
            }
        }

        return factory.createEvaluator(serializer.toString(), projection.getType(), names,
                types, constants);
    }
150252
public static SourceMapGenerator fromSourceMap(SourceMapConsumer aSourceMapConsumer) {
        String sourceRoot = aSourceMapConsumer.sourceRoot;
        SourceMapGenerator generator = new SourceMapGenerator(aSourceMapConsumer.file, sourceRoot);
        aSourceMapConsumer.eachMapping().forEach(mapping -> {
            Mapping newMapping = new Mapping(mapping.generated);
            if (mapping.source != null) {
                newMapping.source = mapping.source;
                if (sourceRoot != null) {
                    newMapping.source = Util.relative(sourceRoot, newMapping.source);
                }
                newMapping.original = mapping.original;
                if (mapping.name != null) {
                    newMapping.name = mapping.name;
                }
            }
            generator.addMapping(newMapping);
        });
        aSourceMapConsumer.sources().stream().forEach(sourceFile -> {
            String content = aSourceMapConsumer.sourceContentFor(sourceFile, null);
            if (content != null) {
                generator.setSourceContent(sourceFile, content);
            }
        });
        return generator;
    }
982656
private void addNamedNativeQueryMetadata(Class clazz)
    {
        ApplicationMetadata appMetadata = kunderaMetadata.getApplicationMetadata();
        String name, query = null;
        if (clazz.isAnnotationPresent(NamedQuery.class))
        {
            NamedQuery ann = (NamedQuery) clazz.getAnnotation(NamedQuery.class);
            appMetadata.addQueryToCollection(ann.name(), ann.query(), false, clazz);
        }

        if (clazz.isAnnotationPresent(NamedQueries.class))
        {
            NamedQueries ann = (NamedQueries) clazz.getAnnotation(NamedQueries.class);

            NamedQuery[] anns = ann.value();
            for (NamedQuery a : anns)
            {
                appMetadata.addQueryToCollection(a.name(), a.query(), false, clazz);
            }
        }

        if (clazz.isAnnotationPresent(NamedNativeQuery.class))
        {
            NamedNativeQuery ann = (NamedNativeQuery) clazz.getAnnotation(NamedNativeQuery.class);
            appMetadata.addQueryToCollection(ann.name(), ann.query(), true, clazz);
        }

        if (clazz.isAnnotationPresent(NamedNativeQueries.class))
        {
            NamedNativeQueries ann = (NamedNativeQueries) clazz.getAnnotation(NamedNativeQueries.class);

            NamedNativeQuery[] anns = ann.value();
            for (NamedNativeQuery a : anns)
            {
                appMetadata.addQueryToCollection(a.name(), a.query(), true, clazz);
            }
        }
    }
873852
private static GraphicalModelProto.MetaData.Builder getProtoMetaDataBuilder(Map<String, String> metaData) {
    GraphicalModelProto.MetaData.Builder builder = GraphicalModelProto.MetaData.newBuilder();
    for (String key : metaData.keySet()) {
      builder.addKey(key);
      builder.addValue(metaData.get(key));
    }
    return builder;
  }
719682
private Object readMetadataFromXML(InputSource source, Class target)
            throws MalformedURLException, ParserConfigurationException, SAXException, IOException
    {
        // TODO: make this configurable
        boolean validate = false;
        
        // get a xml reader instance:
        SAXParserFactory factory = SAXParserFactory.newInstance();
        log.info("RepositoryPersistor using SAXParserFactory : " + factory.getClass().getName());
        if (validate)
        {
            factory.setValidating(true);
        }
        SAXParser p = factory.newSAXParser();
        XMLReader reader = p.getXMLReader();
        if (validate)
        {
            reader.setErrorHandler(new OJBErrorHandler());
        }

        Object result;
        if (DescriptorRepository.class.equals(target))
        {
            // create an empty repository:
            DescriptorRepository repository = new DescriptorRepository();
            // create handler for building the repository structure
            ContentHandler handler = new RepositoryXmlHandler(repository);
            // tell parser to use our handler:
            reader.setContentHandler(handler);
            reader.parse(source);
            result = repository;
        }
        else if (ConnectionRepository.class.equals(target))
        {
            // create an empty repository:
            ConnectionRepository repository = new ConnectionRepository();
            // create handler for building the repository structure
            ContentHandler handler = new ConnectionDescriptorXmlHandler(repository);
            // tell parser to use our handler:
            reader.setContentHandler(handler);
            reader.parse(source);
            //LoggerFactory.getBootLogger().info("loading XML took " + (stop - start) + " msecs");
            result = repository;
        }
        else
            throw new MetadataException("Could not build a repository instance for '" + target +
                    "', using source " + source);
        return result;
    }
1288863
function createMeta(source, options) {
  return {
    tagName: null,
    fragments: null,
    options: {
      ...DEFAULT_OPTIONS,
      ...options
    },
    source
  }
}
1335300
public ServiceResult<T> addMetadata(String key, Metadata metadata) {
        this.getMetadata().put(key, metadata);
        return this;
    }
480151
@Synchronized
    @SneakyThrows
    public void buildMetadataResolverAggregate(final String entityId) {
        LOGGER.trace("Building metadata resolver aggregate");
        this.metadataResolver = new ChainingMetadataResolver();
        val resolvers = new ArrayList<MetadataResolver>();
        val entries = this.metadataResources.entrySet();
        entries.forEach(entry -> {
            val resource = entry.getKey();
            LOGGER.debug("Loading [{}]", resource.getFilename());
            resolvers.addAll(loadMetadataFromResource(entry.getValue(), resource, entityId));
        });
        this.metadataResolver.setId(ChainingMetadataResolver.class.getCanonicalName());
        this.metadataResolver.setResolvers(resolvers);
        LOGGER.debug("Collected metadata from [{}] resolvers(s). Initializing aggregate resolver...", resolvers.size());
        this.metadataResolver.initialize();
        LOGGER.info("Metadata aggregate initialized successfully.");
    }
29141
public static <T_Result, T_Source> List<T_Result> map(Collection<T_Source> source,
            Mapper<T_Result, T_Source> mapper) {
        List<T_Result> result = new LinkedList<T_Result>();
        for (T_Source element : source) {
            result.add(mapper.map(element));
        }
        return result;
    }
31552
private void handleTSMeta(final TSDB tsdb, final HttpQuery query) {

    final HttpMethod method = query.getAPIMethod();
    // GET
    if (method == HttpMethod.GET) {
      
      String tsuid = null;
      if (query.hasQueryStringParam("tsuid")) {
        tsuid = query.getQueryStringParam("tsuid");
        try {
          final TSMeta meta = TSMeta.getTSMeta(tsdb, tsuid).joinUninterruptibly();
          if (meta != null) {
            query.sendReply(query.serializer().formatTSMetaV1(meta));
          } else {
            throw new BadRequestException(HttpResponseStatus.NOT_FOUND,
                "Could not find Timeseries meta data");
          } 
        } catch (NoSuchUniqueName e) {
          // this would only happen if someone deleted a UID but left the 
          // the timeseries meta data
          throw new BadRequestException(HttpResponseStatus.NOT_FOUND, 
              "Unable to find one of the UIDs", e);
        } catch (BadRequestException e) {
          throw e;
        } catch (Exception e) {
          throw new RuntimeException(e);
        }
      } else {
        String mquery = query.getRequiredQueryStringParam("m");
        // m is of the following forms:
        // metric[{tag=value,...}]
        // where the parts in square brackets `[' .. `]' are optional.
        final HashMap<String, String> tags = new HashMap<String, String>();
        String metric = null;
        try {
          metric = Tags.parseWithMetric(mquery, tags);
        } catch (IllegalArgumentException e) {
          throw new BadRequestException(e);
        }
        final TSUIDQuery tsuid_query = new TSUIDQuery(tsdb, metric, tags);
        try {
          final List<TSMeta> tsmetas = tsuid_query.getTSMetas()
              .joinUninterruptibly();
          query.sendReply(query.serializer().formatTSMetaListV1(tsmetas));
        } catch (NoSuchUniqueName e) {
          throw new BadRequestException(HttpResponseStatus.NOT_FOUND, 
              "Unable to find one of the UIDs", e);
        } catch (BadRequestException e) {
          throw e;
        } catch (RuntimeException e) {
          throw new BadRequestException(e);
        } catch (Exception e) {
          throw new RuntimeException(e);
        }
      }
    // POST / PUT
    } else if (method == HttpMethod.POST || method == HttpMethod.PUT) {

      final TSMeta meta;
      if (query.hasContent()) {
        meta = query.serializer().parseTSMetaV1();
      } else {
        meta = this.parseTSMetaQS(query);
      }
      
      /**
       * Storage callback used to determine if the storage call was successful
       * or not. Also returns the updated object from storage.
       */
      class SyncCB implements Callback<Deferred<TSMeta>, Boolean> {

        @Override
        public Deferred<TSMeta> call(Boolean success) throws Exception {
          if (!success) {
            throw new BadRequestException(
                HttpResponseStatus.INTERNAL_SERVER_ERROR,
                "Failed to save the TSMeta to storage",
                "This may be caused by another process modifying storage data");
          }

          return TSMeta.getTSMeta(tsdb, meta.getTSUID());
        }

      }

      if (meta.getTSUID() == null || meta.getTSUID().isEmpty()) {
        // we got a JSON object without TSUID. Try to find a timeseries spec of
        // the form "m": "metric{tagk=tagv,...}"
        final String metric = query.getRequiredQueryStringParam("m");
        final boolean create = query.getQueryStringParam("create") != null
            && query.getQueryStringParam("create").equals("true");
        final String tsuid = getTSUIDForMetric(metric, tsdb);
        
        class WriteCounterIfNotPresentCB implements Callback<Boolean, Boolean> {

          @Override
          public Boolean call(Boolean exists) throws Exception {
            if (!exists && create) {
              final PutRequest put = new PutRequest(tsdb.metaTable(), 
                  UniqueId.stringToUid(tsuid), TSMeta.FAMILY(), 
                  TSMeta.COUNTER_QUALIFIER(), Bytes.fromLong(0));    
              tsdb.getClient().put(put);
            }

            return exists;
          }

        }

        try {
          // Check whether we have a TSMeta stored already
          final boolean exists = TSMeta
              .metaExistsInStorage(tsdb, tsuid)
              .joinUninterruptibly();
          // set TSUID
          meta.setTSUID(tsuid);
          
          if (!exists && create) {
            // Write 0 to counter column if not present
            TSMeta.counterExistsInStorage(tsdb, UniqueId.stringToUid(tsuid))
                .addCallback(new WriteCounterIfNotPresentCB())
                .joinUninterruptibly();
            // set TSUID
            final Deferred<TSMeta> process_meta = meta.storeNew(tsdb)
                .addCallbackDeferring(new SyncCB());
            final TSMeta updated_meta = process_meta.joinUninterruptibly();
            tsdb.indexTSMeta(updated_meta);
            tsdb.processTSMetaThroughTrees(updated_meta);
            query.sendReply(query.serializer().formatTSMetaV1(updated_meta));
          } else if (exists) {
            final Deferred<TSMeta> process_meta = meta.syncToStorage(tsdb,
                method == HttpMethod.PUT).addCallbackDeferring(new SyncCB());
            final TSMeta updated_meta = process_meta.joinUninterruptibly();
            tsdb.indexTSMeta(updated_meta);
            query.sendReply(query.serializer().formatTSMetaV1(updated_meta));
          } else {
            throw new BadRequestException(
                "Could not find TSMeta, specify \"create=true\" to create a new one.");
          }
        } catch (IllegalStateException e) {
          query.sendStatusOnly(HttpResponseStatus.NOT_MODIFIED);
        } catch (IllegalArgumentException e) {
          throw new BadRequestException(e);
        } catch (BadRequestException e) {
          throw e;
        } catch (NoSuchUniqueName e) {
          // this would only happen if someone deleted a UID but left the
          // the timeseries meta data
          throw new BadRequestException(HttpResponseStatus.NOT_FOUND,
              "Unable to find one or more UIDs", e);
        } catch (Exception e) {
          throw new RuntimeException(e);
        }
      } else {
        try {
          final Deferred<TSMeta> process_meta = meta.syncToStorage(tsdb,
              method == HttpMethod.PUT).addCallbackDeferring(new SyncCB());
          final TSMeta updated_meta = process_meta.joinUninterruptibly();
          tsdb.indexTSMeta(updated_meta);
          query.sendReply(query.serializer().formatTSMetaV1(updated_meta));
        } catch (IllegalStateException e) {
          query.sendStatusOnly(HttpResponseStatus.NOT_MODIFIED);
        } catch (IllegalArgumentException e) {
          throw new BadRequestException(e);
        } catch (NoSuchUniqueName e) {
          // this would only happen if someone deleted a UID but left the
          // the timeseries meta data
          throw new BadRequestException(HttpResponseStatus.NOT_FOUND,
              "Unable to find one or more UIDs", e);
        } catch (Exception e) {
          throw new RuntimeException(e);
        }
      }
    // DELETE  
    } else if (method == HttpMethod.DELETE) {
      
      final TSMeta meta;
      if (query.hasContent()) {
        meta = query.serializer().parseTSMetaV1();
      } else {
        meta = this.parseTSMetaQS(query);
      }
      try{
        meta.delete(tsdb);
        tsdb.deleteTSMeta(meta.getTSUID());
      } catch (IllegalArgumentException e) {
        throw new BadRequestException("Unable to delete TSMeta information", e);
      }
      query.sendStatusOnly(HttpResponseStatus.NO_CONTENT);
    } else {
      throw new BadRequestException(HttpResponseStatus.METHOD_NOT_ALLOWED, 
          "Method not allowed", "The HTTP method [" + method.getName() +
          "] is not permitted for this endpoint");
    }
  }
1212382
SourceMetadata loadSourceMetadata(Class<?> clazz) {
        SourceMetadata metadata = metadataMap.get(clazz);
        if (metadata == null) {
            metadata = new SourceMetadata(clazz);
            SourceMetadata found = metadataMap.putIfAbsent(clazz, metadata);
            metadata = found == null ? metadata : found;
        }

        return metadata;
    }
1168310
public Metadata createMetadata(String typeName, Metadata metadata) {
        String scope = Metadata.scopeBasedOnType(typeName);
        return this.createMetadata(typeName, scope, metadata);
    }
423053
private DBObject toDBObject(final Projection projection) {
        String target;
        if (firstStage) {
            MappedField field = mapper.getMappedClass(source).getMappedField(projection.getTarget());
            target = field != null ? field.getNameToStore() : projection.getTarget();
        } else {
            target = projection.getTarget();
        }

        if (projection.getProjections() != null) {
            List<Projection> list = projection.getProjections();
            DBObject projections = new BasicDBObject();
            for (Projection subProjection : list) {
                projections.putAll(toDBObject(subProjection));
            }
            return new BasicDBObject(target, projections);
        } else if (projection.getSource() != null) {
            return new BasicDBObject(target, projection.getSource());
        } else if (projection.getArguments() != null) {
            DBObject args = toExpressionArgs(projection.getArguments());
            if (target == null) {
                // Unwrap for single-argument expressions
                if (args instanceof List<?> && ((List<?>) args).size() == 1) {
                    Object firstArg = ((List<?>) args).get(0);
                    if (firstArg instanceof DBObject) {
                        return (DBObject) firstArg;
                    }
                }
                return args;
            } else {
                // Unwrap for single-argument expressions
                if (args instanceof List<?> && ((List<?>) args).size() == 1) {
                    return new BasicDBObject(target, ((List<?>) args).get(0));
                }
                return new BasicDBObject(target, args);
            }
        } else {
            return new BasicDBObject(target, projection.isSuppressed() ? 0 : 1);
        }
    }
403317
public static PoliciesCache fromSources(final Iterable<CacheableYamlSource> sources) {
        return fromSourceProvider(
                new SourceProvider() {
                    @Override
                    public Iterator<CacheableYamlSource> getSourceIterator() {
                        return sources.iterator();
                    }
                }
        );
    }
658491
public static <T> Source<T> join(Source<T>... sources) {
        return join(Arrays.asList(sources));
    }
413387
public List executeQuery(QueryBuilder filter, AggregationBuilder aggregation, final EntityMetadata entityMetadata,
            KunderaQuery query, int firstResult, int maxResults)
    {
        String[] fieldsToSelect = query.getResult();
        Class clazz = entityMetadata.getEntityClazz();

        MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata()
                .getMetamodel(entityMetadata.getPersistenceUnit());

        FilteredQueryBuilder queryBuilder = QueryBuilders.filteredQuery(null, filter);
        SearchRequestBuilder builder = txClient.prepareSearch(entityMetadata.getSchema().toLowerCase())
                .setTypes(entityMetadata.getTableName());

        addFieldsToBuilder(fieldsToSelect, clazz, metaModel, builder);

        if (aggregation == null)
        {
            builder.setQuery(queryBuilder);
            builder.setFrom(firstResult);
            builder.setSize(maxResults);
            addSortOrder(builder, query, entityMetadata);
        }
        else
        {
            logger.debug("Aggregated query identified");
            builder.addAggregation(aggregation);

            if (fieldsToSelect.length == 1
                    || (query.isSelectStatement() && query.getSelectStatement().hasGroupByClause()))
            {
                builder.setSize(0);
            }
        }

        SearchResponse response = null;
        logger.debug("Query generated: " + builder);

        try
        {
            response = builder.execute().actionGet();
            logger.debug("Query execution response: " + response);
        }
        catch (ElasticsearchException e)
        {
            logger.error("Exception occured while executing query on Elasticsearch.", e);
            throw new KunderaException("Exception occured while executing query on Elasticsearch.", e);
        }

        return esResponseReader.parseResponse(response, aggregation, fieldsToSelect, metaModel, clazz, entityMetadata,
                query);
    }
1054202
public Evaluator<List<Object[]>> createEvaluator(QueryMetadata metadata,
            List<JoinExpression> joins, @Nullable Predicate filter) {
        List<String> sourceNames = new ArrayList<String>();
        List<Type> sourceTypes = new ArrayList<Type>();
        List<Class<?>> sourceClasses = new ArrayList<Class<?>>();
        StringBuilder vars = new StringBuilder();
        CollQuerySerializer ser = new CollQuerySerializer(templates);
        ser.append("java.util.List<Object[]> rv = new java.util.ArrayList<Object[]>();\n");

        List<String> anyJoinMatchers = new ArrayList<String>();

        // creating context
        for (JoinExpression join : joins) {
            Expression<?> target = join.getTarget();
            String typeName = com.mysema.codegen.support.ClassUtils.getName(target.getType());
            if (vars.length() > 0) {
                vars.append(",");
            }
            switch (join.getType()) {
            case DEFAULT:
                ser.append("for (" + typeName + " " + target + " : " + target + "_) {\n");
                vars.append(target);
                sourceNames.add(target + "_");
                sourceTypes.add(new SimpleType(Types.ITERABLE, new ClassType(TypeCategory.SIMPLE,target.getType())));
                sourceClasses.add(Iterable.class);
                break;

            case INNERJOIN:
            case LEFTJOIN:
                Operation<?> alias = (Operation<?>) join.getTarget();
                boolean colAnyJoin = join.getCondition() != null && join.getCondition().toString().equals("any");
                boolean leftJoin = join.getType() == JoinType.LEFTJOIN;
                String matcher = null;
                if (colAnyJoin) {
                    matcher = alias.getArg(1).toString() + "_matched";
                    ser.append("boolean " + matcher + " = false;\n");
                    anyJoinMatchers.add(matcher);
                }
                ser.append("for (" + typeName + " " + alias.getArg(1) + " : ");
                if (leftJoin) {
                    ser.append(CollQueryFunctions.class.getName() + ".leftJoin(");
                }
                if (colAnyJoin) {
                    Context context = new Context();
                    Expression<?> replacement = alias.getArg(0)
                            .accept(collectionAnyVisitor, context);
                    ser.handle(replacement);
                } else {
                    ser.handle(alias.getArg(0));
                }
                if (alias.getArg(0).getType().equals(Map.class)) {
                    ser.append(".values()");
                }
                if (leftJoin) {
                    ser.append(")");
                }
                ser.append(") {\n");
                if (matcher != null) {
                    ser.append("if (!" + matcher + ") {\n");
                }
                vars.append(alias.getArg(1));
                break;

            default:
                throw new IllegalArgumentException("Illegal join expression " + join);
            }
        }

        // filter
        if (filter != null) {
            ser.append("try {\n");
            ser.append("if (");
            ser.handle(filter).append(") {\n");
            for (String matcher : anyJoinMatchers) {
                ser.append("    " + matcher + " = true;\n");
            }
            ser.append("    rv.add(new Object[]{" + vars + "});\n");
            ser.append("}\n");
            ser.append("} catch (NullPointerException npe) { }\n");
        } else {
            ser.append("rv.add(new Object[]{" + vars + "});\n");
        }

        // closing context
        int amount = joins.size() + anyJoinMatchers.size();
        for (int i = 0; i < amount; i++) {
            ser.append("}\n");
        }
        ser.append("return rv;");

        Map<Object,String> constantToLabel = ser.getConstantToLabel();
        Map<String, Object> constants = getConstants(metadata, constantToLabel);

        ClassType projectionType = new ClassType(TypeCategory.LIST, List.class, Types.OBJECTS);
        return factory.createEvaluator(
                ser.toString(),
                projectionType,
                sourceNames.toArray(new String[sourceNames.size()]),
                sourceTypes.toArray(new Type[sourceTypes.size()]),
                sourceClasses.toArray(new Class<?>[sourceClasses.size()]),
                constants);
    }