Example usage for java.util ArrayList trimToSize

List of usage examples for java.util ArrayList trimToSize

Introduction

In this page you can find the example usage for java.util ArrayList trimToSize.

Prototype

public void trimToSize() 

Source Link

Document

Trims the capacity of this ArrayList instance to be the list's current size.

Usage

From source file:org.parosproxy.paros.network.ConnectionParam.java

private void loadProxyExcludedDomains() {
    List<HierarchicalConfiguration> fields = ((HierarchicalConfiguration) getConfig())
            .configurationsAt(ALL_PROXY_EXCLUDED_DOMAINS_KEY);
    this.proxyExcludedDomains = new ArrayList<>(fields.size());
    ArrayList<ProxyExcludedDomainMatcher> excludedDomainsEnabled = new ArrayList<>(fields.size());
    for (HierarchicalConfiguration sub : fields) {
        String value = sub.getString(PROXY_EXCLUDED_DOMAIN_VALUE_KEY, "");
        if (value.isEmpty()) {
            log.warn("Failed to read an outgoing proxy excluded domain entry, required value is empty.");
            continue;
        }//w w w .  j a  v  a  2  s .  c om

        ProxyExcludedDomainMatcher excludedDomain = null;
        boolean regex = sub.getBoolean(PROXY_EXCLUDED_DOMAIN_REGEX_KEY, false);
        if (regex) {
            try {
                Pattern pattern = ProxyExcludedDomainMatcher.createPattern(value);
                excludedDomain = new ProxyExcludedDomainMatcher(pattern);
            } catch (IllegalArgumentException e) {
                log.error("Failed to read an outgoing proxy excluded domain entry with regex: " + value, e);
            }
        } else {
            excludedDomain = new ProxyExcludedDomainMatcher(value);
        }

        if (excludedDomain != null) {
            excludedDomain.setEnabled(sub.getBoolean(PROXY_EXCLUDED_DOMAIN_ENABLED_KEY, true));

            proxyExcludedDomains.add(excludedDomain);

            if (excludedDomain.isEnabled()) {
                excludedDomainsEnabled.add(excludedDomain);
            }
        }
    }

    excludedDomainsEnabled.trimToSize();
    this.proxyExcludedDomainsEnabled = excludedDomainsEnabled;
}

From source file:org.zaproxy.zap.spider.SpiderParam.java

private void loadDomainsAlwaysInScope() {
    List<HierarchicalConfiguration> fields = ((HierarchicalConfiguration) getConfig())
            .configurationsAt(ALL_DOMAINS_ALWAYS_IN_SCOPE_KEY);
    this.domainsAlwaysInScope = new ArrayList<>(fields.size());
    ArrayList<DomainAlwaysInScopeMatcher> domainsInScopeEnabled = new ArrayList<>(fields.size());
    for (HierarchicalConfiguration sub : fields) {
        String value = sub.getString(DOMAIN_ALWAYS_IN_SCOPE_VALUE_KEY, "");
        if ("".equals(value)) {
            log.warn("Failed to read an spider domain in scope entry, required value is empty.");
        }/*from   w w  w.  j  a  v a 2 s .co  m*/

        DomainAlwaysInScopeMatcher excludedDomain = null;
        boolean regex = sub.getBoolean(DOMAIN_ALWAYS_IN_SCOPE_REGEX_KEY, false);
        if (regex) {
            try {
                Pattern pattern = DomainAlwaysInScopeMatcher.createPattern(value);
                excludedDomain = new DomainAlwaysInScopeMatcher(pattern);
            } catch (IllegalArgumentException e) {
                log.error("Failed to read an spider domain in scope entry with regex: " + value, e);
            }
        } else {
            excludedDomain = new DomainAlwaysInScopeMatcher(value);
        }

        if (excludedDomain != null) {
            excludedDomain.setEnabled(sub.getBoolean(DOMAIN_ALWAYS_IN_SCOPE_ENABLED_KEY, true));

            domainsAlwaysInScope.add(excludedDomain);

            if (excludedDomain.isEnabled()) {
                domainsInScopeEnabled.add(excludedDomain);
            }
        }
    }

    domainsInScopeEnabled.trimToSize();
    this.domainsAlwaysInScopeEnabled = domainsInScopeEnabled;
}

From source file:outlineDescriptor.FieldAnalyzer.java

private CellReference[] identifyMaxima(CellReference[] store, Array2DRowRealMatrix kernel) {

    ArrayList<CellReference> out = new ArrayList<CellReference>();
    int maxCount = 0;

    for (CellReference pt : store) {

        //this point has already been processed thus it is not a separate maximum
        if ((flags[getListOffset(pt.x, pt.y)] & (DISCARDED | PROCESSED)) != 0) {
            continue;
        }//from w w w . j av  a2 s . c  om

        //boolean equalInRange = (flags[getListOffset(pt.x, pt.y)] & EQUAL) != 0;

        //checking the neighboring area
        for (int i = 0; i < kernel.getRowDimension(); i++) {

            double[] bounds = kernel.getRow(i);

            int upperLimit = (int) Math.max(bounds[1], bounds[2]);
            int bottom = (int) Math.min(bounds[1], bounds[2]);

            while (bottom <= upperLimit) {

                int yPos = pt.y + bottom;
                int xPos = (int) bounds[0] + pt.x;

                if (xPos >= 0 && xPos < coherenceKernel.length && yPos >= 0
                        && yPos < coherenceKernel[0].length) {

                    if ((flags[getListOffset(xPos, yPos)] & CANDIDATE) == 0) {
                        //this point isnt being considered for a maximum, continue
                        flags[getListOffset(xPos, yPos)] |= PROCESSED;

                    }
                    //checking for equality within tolerance
                    else if (pt.coh - FPERROR <= coherenceKernel[xPos][yPos]) {
                        flags[getListOffset(xPos, yPos)] |= (EQUAL);
                    } else {

                        flags[getListOffset(xPos, yPos)] |= (PROCESSED | DISCARDED);
                    }
                }
                flags[getListOffset(pt.x, pt.y)] |= MAX;
                bottom++;
            }
        }

        out.add(pt);
        maxCount++;

        if (maxCount >= count) {
            break;
        }
    }
    out.trimToSize();
    return out.toArray(new CellReference[out.size()]);

}

From source file:com.opengamma.language.external.ExternalFunctionHandler.java

/**
 * Creates a handler wrapper for a given class.
 * //from   w  ww. ja va2s  .co  m
 * @param clazz the class containing external function methods
 */
public ExternalFunctionHandler(final Class<?> clazz) {
    final Constructor<?>[] constructors = clazz.getConstructors();
    final Method[] methods = clazz.getMethods();
    final ArrayList<PublishedFunction> functions = new ArrayList<PublishedFunction>(
            constructors.length + methods.length);
    // Only need an instance of the class if one or more annotated methods are not static. In this case, the same
    // instance will be re-used for each non-static method. If instantiation fails (e.g. no default constructor), just
    // skip instance methods and log warnings.
    Object sharedInstance = null;
    boolean instantiateFailed = false;
    for (Constructor<?> constructor : constructors) {
        if (!constructor.isAnnotationPresent(ExternalFunction.class)) {
            continue;
        }
        s_logger.debug("Found constructor {}", constructor);
        // If there is a constructor method, can only have static declarations
        instantiateFailed = true;
        functions.add(new ConstructorWrapper(constructor));
    }
    for (Method method : methods) {
        if (!method.isAnnotationPresent(ExternalFunction.class)) {
            continue;
        }
        s_logger.debug("Found method {}", method);
        final Object instance;
        if (Modifier.isStatic(method.getModifiers())) {
            instance = null;
        } else {
            if (instantiateFailed) {
                s_logger.warn("Skipping method {}", method);
                continue;
            } else if (sharedInstance == null) {
                sharedInstance = tryGetInstance(clazz);
                if (sharedInstance == null) {
                    s_logger.warn("Default instantiation failed for {}", clazz);
                    s_logger.warn("Skipping method {}", method);
                    instantiateFailed = true;
                    continue;
                }
            }
            instance = sharedInstance;
        }
        functions.add(new MethodWrapper(method, instance));
    }
    functions.trimToSize();
    _functions = functions;
}

From source file:org.apache.hadoop.hbase.replication.regionserver.ReplicationSource.java

/**
 * We only want KVs that are scoped other than local
 * @param entry The entry to check for replication
 *//*from w  ww.  ja va 2  s  .  com*/
protected void removeNonReplicableEdits(HLog.Entry entry) {
    String tabName = entry.getKey().getTablename().getNameAsString();
    ArrayList<KeyValue> kvs = entry.getEdit().getKeyValues();
    Map<String, List<String>> tableCFs = null;
    try {
        tableCFs = this.replicationPeers.getTableCFs(peerId);
    } catch (IllegalArgumentException e) {
        LOG.error("should not happen: can't get tableCFs for peer " + peerId
                + ", degenerate as if it's not configured by keeping tableCFs==null");
    }
    int size = kvs.size();

    // clear kvs(prevent replicating) if logKey's table isn't in this peer's
    // replicable table list (empty tableCFs means all table are replicable)
    if (tableCFs != null && !tableCFs.containsKey(tabName)) {
        kvs.clear();
    } else {
        NavigableMap<byte[], Integer> scopes = entry.getKey().getScopes();
        List<String> cfs = (tableCFs == null) ? null : tableCFs.get(tabName);
        for (int i = size - 1; i >= 0; i--) {
            KeyValue kv = kvs.get(i);
            // The scope will be null or empty if
            // there's nothing to replicate in that WALEdit
            // ignore(remove) kv if its cf isn't in the replicable cf list
            // (empty cfs means all cfs of this table are replicable)
            if (scopes == null || !scopes.containsKey(kv.getFamily())
                    || (cfs != null && !cfs.contains(Bytes.toString(kv.getFamily())))) {
                kvs.remove(i);
            }
        }
    }

    if (kvs.size() < size / 2) {
        kvs.trimToSize();
    }
}

From source file:org.apache.accumulo.examples.wikisearch.logic.AbstractQueryLogic.java

public Results runQuery(Connector connector, List<String> authorizations, String query, Date beginDate,
        Date endDate, Set<String> types) {

    if (StringUtils.isEmpty(query)) {
        throw new IllegalArgumentException(
                "NULL QueryNode reference passed to " + this.getClass().getSimpleName());
    }//  w  w w .  j a  va2s .  c  om

    Set<Range> ranges = new HashSet<Range>();
    Set<String> typeFilter = types;
    String array[] = authorizations.toArray(new String[0]);
    Authorizations auths = new Authorizations(array);
    Results results = new Results();

    // Get the query string
    String queryString = query;

    StopWatch abstractQueryLogic = new StopWatch();
    StopWatch optimizedQuery = new StopWatch();
    StopWatch queryGlobalIndex = new StopWatch();
    StopWatch optimizedEventQuery = new StopWatch();
    StopWatch fullScanQuery = new StopWatch();
    StopWatch processResults = new StopWatch();

    abstractQueryLogic.start();

    StopWatch parseQuery = new StopWatch();
    parseQuery.start();

    QueryParser parser;
    try {
        if (log.isDebugEnabled()) {
            log.debug("ShardQueryLogic calling QueryParser.execute");
        }
        parser = new QueryParser();
        parser.execute(queryString);
    } catch (org.apache.commons.jexl2.parser.ParseException e1) {
        throw new IllegalArgumentException("Error parsing query", e1);
    }
    int hash = parser.getHashValue();
    parseQuery.stop();
    if (log.isDebugEnabled()) {
        log.debug(hash + " Query: " + queryString);
    }

    Set<String> fields = new HashSet<String>();
    for (String f : parser.getQueryIdentifiers()) {
        fields.add(f);
    }
    if (log.isDebugEnabled()) {
        log.debug("getQueryIdentifiers: " + parser.getQueryIdentifiers().toString());
    }
    // Remove any negated fields from the fields list, we don't want to lookup negated fields
    // in the index.
    fields.removeAll(parser.getNegatedTermsForOptimizer());

    if (log.isDebugEnabled()) {
        log.debug("getQueryIdentifiers: " + parser.getQueryIdentifiers().toString());
    }
    // Get the mapping of field name to QueryTerm object from the query. The query term object
    // contains the operator, whether its negated or not, and the literal to test against.
    Multimap<String, QueryTerm> terms = parser.getQueryTerms();

    // Find out which terms are indexed
    // TODO: Should we cache indexed terms or does that not make sense since we are always
    // loading data.
    StopWatch queryMetadata = new StopWatch();
    queryMetadata.start();
    Map<String, Multimap<String, Class<? extends Normalizer>>> metadataResults;
    try {
        metadataResults = findIndexedTerms(connector, auths, fields, typeFilter);
    } catch (Exception e1) {
        throw new RuntimeException("Error in metadata lookup", e1);
    }

    // Create a map of indexed term to set of normalizers for it
    Multimap<String, Normalizer> indexedTerms = HashMultimap.create();
    for (Entry<String, Multimap<String, Class<? extends Normalizer>>> entry : metadataResults.entrySet()) {
        // Get the normalizer from the normalizer cache
        for (Class<? extends Normalizer> clazz : entry.getValue().values()) {
            indexedTerms.put(entry.getKey(), normalizerCacheMap.get(clazz));
        }
    }
    queryMetadata.stop();
    if (log.isDebugEnabled()) {
        log.debug(hash + " Indexed Terms: " + indexedTerms.toString());
    }

    Set<String> orTerms = parser.getOrTermsForOptimizer();

    // Iterate over the query terms to get the operators specified in the query.
    ArrayList<String> unevaluatedExpressions = new ArrayList<String>();
    boolean unsupportedOperatorSpecified = false;
    for (Entry<String, QueryTerm> entry : terms.entries()) {
        if (null == entry.getValue()) {
            continue;
        }

        if (null != this.unevaluatedFields && this.unevaluatedFields.contains(entry.getKey().trim())) {
            unevaluatedExpressions.add(entry.getKey().trim() + " " + entry.getValue().getOperator() + " "
                    + entry.getValue().getValue());
        }

        int operator = JexlOperatorConstants.getJJTNodeType(entry.getValue().getOperator());
        if (!(operator == ParserTreeConstants.JJTEQNODE || operator == ParserTreeConstants.JJTNENODE
                || operator == ParserTreeConstants.JJTLENODE || operator == ParserTreeConstants.JJTLTNODE
                || operator == ParserTreeConstants.JJTGENODE || operator == ParserTreeConstants.JJTGTNODE
                || operator == ParserTreeConstants.JJTERNODE)) {
            unsupportedOperatorSpecified = true;
            break;
        }
    }
    if (null != unevaluatedExpressions)
        unevaluatedExpressions.trimToSize();
    if (log.isDebugEnabled()) {
        log.debug(hash + " unsupportedOperators: " + unsupportedOperatorSpecified + " indexedTerms: "
                + indexedTerms.toString() + " orTerms: " + orTerms.toString() + " unevaluatedExpressions: "
                + unevaluatedExpressions.toString());
    }

    // We can use the intersecting iterator over the field index as an optimization under the
    // following conditions
    //
    // 1. No unsupported operators in the query.
    // 2. No 'or' operators and at least one term indexed
    // or
    // 1. No unsupported operators in the query.
    // 2. and all terms indexed
    // or
    // 1. All or'd terms are indexed. NOTE, this will potentially skip some queries and push to a full table scan
    // // WE should look into finding a better way to handle whether we do an optimized query or not.
    boolean optimizationSucceeded = false;
    boolean orsAllIndexed = false;
    if (orTerms.isEmpty()) {
        orsAllIndexed = false;
    } else {
        orsAllIndexed = indexedTerms.keySet().containsAll(orTerms);
    }

    if (log.isDebugEnabled()) {
        log.debug("All or terms are indexed");
    }

    if (!unsupportedOperatorSpecified && (((null == orTerms || orTerms.isEmpty()) && indexedTerms.size() > 0)
            || (fields.size() > 0 && indexedTerms.size() == fields.size()) || orsAllIndexed)) {
        optimizedQuery.start();
        // Set up intersecting iterator over field index.

        // Get information from the global index for the indexed terms. The results object will contain the term
        // mapped to an object that contains the total count, and partitions where this term is located.

        // TODO: Should we cache indexed term information or does that not make sense since we are always loading data
        queryGlobalIndex.start();
        IndexRanges termIndexInfo;
        try {
            // If fields is null or zero, then it's probably the case that the user entered a value
            // to search for with no fields. Check for the value in index.
            if (fields.isEmpty()) {
                termIndexInfo = this.getTermIndexInformation(connector, auths, queryString, typeFilter);
                if (null != termIndexInfo && termIndexInfo.getRanges().isEmpty()) {
                    // Then we didn't find anything in the index for this query. This may happen for an indexed term that has wildcards
                    // in unhandled locations.
                    // Break out of here by throwing a named exception and do full scan
                    throw new DoNotPerformOptimizedQueryException();
                }
                // We need to rewrite the query string here so that it's valid.
                if (termIndexInfo instanceof UnionIndexRanges) {
                    UnionIndexRanges union = (UnionIndexRanges) termIndexInfo;
                    StringBuilder buf = new StringBuilder();
                    String sep = "";
                    for (String fieldName : union.getFieldNamesAndValues().keySet()) {
                        buf.append(sep).append(fieldName).append(" == ");
                        if (!(queryString.startsWith("'") && queryString.endsWith("'"))) {
                            buf.append("'").append(queryString).append("'");
                        } else {
                            buf.append(queryString);
                        }
                        sep = " or ";
                    }
                    if (log.isDebugEnabled()) {
                        log.debug("Rewrote query for non-fielded single term query: " + queryString + " to "
                                + buf.toString());
                    }
                    queryString = buf.toString();
                } else {
                    throw new RuntimeException("Unexpected IndexRanges implementation");
                }
            } else {
                RangeCalculator calc = this.getTermIndexInformation(connector, auths, indexedTerms, terms,
                        this.getIndexTableName(), this.getReverseIndexTableName(), queryString,
                        this.queryThreads, typeFilter);
                if (null == calc.getResult() || calc.getResult().isEmpty()) {
                    // Then we didn't find anything in the index for this query. This may happen for an indexed term that has wildcards
                    // in unhandled locations.
                    // Break out of here by throwing a named exception and do full scan
                    throw new DoNotPerformOptimizedQueryException();
                }
                termIndexInfo = new UnionIndexRanges();
                termIndexInfo.setIndexValuesToOriginalValues(calc.getIndexValues());
                termIndexInfo.setFieldNamesAndValues(calc.getIndexEntries());
                termIndexInfo.getTermCardinality().putAll(calc.getTermCardinalities());
                for (Range r : calc.getResult()) {
                    // foo is a placeholder and is ignored.
                    termIndexInfo.add("foo", r);
                }
            }
        } catch (TableNotFoundException e) {
            log.error(this.getIndexTableName() + "not found", e);
            throw new RuntimeException(this.getIndexTableName() + "not found", e);
        } catch (org.apache.commons.jexl2.parser.ParseException e) {
            throw new RuntimeException("Error determining ranges for query: " + queryString, e);
        } catch (DoNotPerformOptimizedQueryException e) {
            log.info("Indexed fields not found in index, performing full scan");
            termIndexInfo = null;
        }
        queryGlobalIndex.stop();

        // Determine if we should proceed with optimized query based on results from the global index
        boolean proceed = false;
        if (null == termIndexInfo || termIndexInfo.getFieldNamesAndValues().values().size() == 0) {
            proceed = false;
        } else if (null != orTerms && orTerms.size() > 0
                && (termIndexInfo.getFieldNamesAndValues().values().size() == indexedTerms.size())) {
            proceed = true;
        } else if (termIndexInfo.getFieldNamesAndValues().values().size() > 0) {
            proceed = true;
        } else if (orsAllIndexed) {
            proceed = true;
        } else {
            proceed = false;
        }
        if (log.isDebugEnabled()) {
            log.debug("Proceed with optimized query: " + proceed);
            if (null != termIndexInfo)
                log.debug("termIndexInfo.getTermsFound().size(): "
                        + termIndexInfo.getFieldNamesAndValues().values().size() + " indexedTerms.size: "
                        + indexedTerms.size() + " fields.size: " + fields.size());
        }
        if (proceed) {

            if (log.isDebugEnabled()) {
                log.debug(hash + " Performing optimized query");
            }
            // Use the scan ranges from the GlobalIndexRanges object as the ranges for the batch scanner
            ranges = termIndexInfo.getRanges();
            if (log.isDebugEnabled()) {
                log.info(hash + " Ranges: count: " + ranges.size() + ", " + ranges.toString());
            }

            // Create BatchScanner, set the ranges, and setup the iterators.
            optimizedEventQuery.start();
            BatchScanner bs = null;
            try {
                bs = connector.createBatchScanner(this.getTableName(), auths, queryThreads);
                bs.setRanges(ranges);
                IteratorSetting si = new IteratorSetting(21, "eval", OptimizedQueryIterator.class);

                if (log.isDebugEnabled()) {
                    log.debug("Setting scan option: " + EvaluatingIterator.QUERY_OPTION + " to " + queryString);
                }
                // Set the query option
                si.addOption(EvaluatingIterator.QUERY_OPTION, queryString);
                // Set the Indexed Terms List option. This is the field name and normalized field value pair separated
                // by a comma.
                StringBuilder buf = new StringBuilder();
                String sep = "";
                for (Entry<String, String> entry : termIndexInfo.getFieldNamesAndValues().entries()) {
                    buf.append(sep);
                    buf.append(entry.getKey());
                    buf.append(":");
                    buf.append(termIndexInfo.getIndexValuesToOriginalValues().get(entry.getValue()));
                    buf.append(":");
                    buf.append(entry.getValue());
                    if (sep.equals("")) {
                        sep = ";";
                    }
                }
                if (log.isDebugEnabled()) {
                    log.debug("Setting scan option: " + FieldIndexQueryReWriter.INDEXED_TERMS_LIST + " to "
                            + buf.toString());
                }
                FieldIndexQueryReWriter rewriter = new FieldIndexQueryReWriter();
                String q = "";
                try {
                    q = queryString;
                    q = rewriter.applyCaseSensitivity(q, true, false);// Set upper/lower case for fieldname/fieldvalue
                    Map<String, String> opts = new HashMap<String, String>();
                    opts.put(FieldIndexQueryReWriter.INDEXED_TERMS_LIST, buf.toString());
                    q = rewriter.removeNonIndexedTermsAndInvalidRanges(q, opts);
                    q = rewriter.applyNormalizedTerms(q, opts);
                    if (log.isDebugEnabled()) {
                        log.debug("runServerQuery, FieldIndex Query: " + q);
                    }
                } catch (org.apache.commons.jexl2.parser.ParseException ex) {
                    log.error("Could not parse query, Jexl ParseException: " + ex);
                } catch (Exception ex) {
                    log.error("Problem rewriting query, Exception: " + ex.getMessage());
                }
                si.addOption(BooleanLogicIterator.FIELD_INDEX_QUERY, q);

                // Set the term cardinality option
                sep = "";
                buf.delete(0, buf.length());
                for (Entry<String, Long> entry : termIndexInfo.getTermCardinality().entrySet()) {
                    buf.append(sep);
                    buf.append(entry.getKey());
                    buf.append(":");
                    buf.append(entry.getValue());
                    sep = ",";
                }
                if (log.isDebugEnabled())
                    log.debug("Setting scan option: " + BooleanLogicIterator.TERM_CARDINALITIES + " to "
                            + buf.toString());
                si.addOption(BooleanLogicIterator.TERM_CARDINALITIES, buf.toString());
                if (this.useReadAheadIterator) {
                    if (log.isDebugEnabled()) {
                        log.debug("Enabling read ahead iterator with queue size: " + this.readAheadQueueSize
                                + " and timeout: " + this.readAheadTimeOut);
                    }
                    si.addOption(ReadAheadIterator.QUEUE_SIZE, this.readAheadQueueSize);
                    si.addOption(ReadAheadIterator.TIMEOUT, this.readAheadTimeOut);

                }

                if (null != unevaluatedExpressions) {
                    StringBuilder unevaluatedExpressionList = new StringBuilder();
                    String sep2 = "";
                    for (String exp : unevaluatedExpressions) {
                        unevaluatedExpressionList.append(sep2).append(exp);
                        sep2 = ",";
                    }
                    if (log.isDebugEnabled())
                        log.debug("Setting scan option: " + EvaluatingIterator.UNEVALUTED_EXPRESSIONS + " to "
                                + unevaluatedExpressionList.toString());
                    si.addOption(EvaluatingIterator.UNEVALUTED_EXPRESSIONS,
                            unevaluatedExpressionList.toString());
                }

                bs.addScanIterator(si);

                processResults.start();
                processResults.suspend();
                long count = 0;
                for (Entry<Key, Value> entry : bs) {
                    count++;
                    // The key that is returned by the EvaluatingIterator is not the same key that is in
                    // the table. The value that is returned by the EvaluatingIterator is a kryo
                    // serialized EventFields object.
                    processResults.resume();
                    Document d = this.createDocument(entry.getKey(), entry.getValue());
                    results.getResults().add(d);
                    processResults.suspend();
                }
                log.info(count + " matching entries found in optimized query.");
                optimizationSucceeded = true;
                processResults.stop();
            } catch (TableNotFoundException e) {
                log.error(this.getTableName() + "not found", e);
                throw new RuntimeException(this.getIndexTableName() + "not found", e);
            } finally {
                if (bs != null) {
                    bs.close();
                }
            }
            optimizedEventQuery.stop();
        }
        optimizedQuery.stop();
    }

    // WE should look into finding a better way to handle whether we do an optimized query or not.
    // We are not setting up an else condition here because we may have aborted the logic early in the if statement.
    if (!optimizationSucceeded || ((null != orTerms && orTerms.size() > 0)
            && (indexedTerms.size() != fields.size()) && !orsAllIndexed)) {
        // if (!optimizationSucceeded || ((null != orTerms && orTerms.size() > 0) && (indexedTerms.size() != fields.size()))) {
        fullScanQuery.start();
        if (log.isDebugEnabled()) {
            log.debug(hash + " Performing full scan query");
        }

        // Set up a full scan using the date ranges from the query
        // Create BatchScanner, set the ranges, and setup the iterators.
        BatchScanner bs = null;
        try {
            // The ranges are the start and end dates
            Collection<Range> r = getFullScanRange(beginDate, endDate, terms);
            ranges.addAll(r);

            if (log.isDebugEnabled()) {
                log.debug(hash + " Ranges: count: " + ranges.size() + ", " + ranges.toString());
            }

            bs = connector.createBatchScanner(this.getTableName(), auths, queryThreads);
            bs.setRanges(ranges);
            IteratorSetting si = new IteratorSetting(22, "eval", EvaluatingIterator.class);
            // Create datatype regex if needed
            if (null != typeFilter) {
                StringBuilder buf = new StringBuilder();
                String s = "";
                for (String type : typeFilter) {
                    buf.append(s).append(type).append(".*");
                    s = "|";
                }
                if (log.isDebugEnabled())
                    log.debug("Setting colf regex iterator to: " + buf.toString());
                IteratorSetting ri = new IteratorSetting(21, "typeFilter", RegExFilter.class);
                RegExFilter.setRegexs(ri, null, buf.toString(), null, null, false);
                bs.addScanIterator(ri);
            }
            if (log.isDebugEnabled()) {
                log.debug("Setting scan option: " + EvaluatingIterator.QUERY_OPTION + " to " + queryString);
            }
            si.addOption(EvaluatingIterator.QUERY_OPTION, queryString);
            if (null != unevaluatedExpressions) {
                StringBuilder unevaluatedExpressionList = new StringBuilder();
                String sep2 = "";
                for (String exp : unevaluatedExpressions) {
                    unevaluatedExpressionList.append(sep2).append(exp);
                    sep2 = ",";
                }
                if (log.isDebugEnabled())
                    log.debug("Setting scan option: " + EvaluatingIterator.UNEVALUTED_EXPRESSIONS + " to "
                            + unevaluatedExpressionList.toString());
                si.addOption(EvaluatingIterator.UNEVALUTED_EXPRESSIONS, unevaluatedExpressionList.toString());
            }
            bs.addScanIterator(si);
            long count = 0;
            processResults.start();
            processResults.suspend();
            for (Entry<Key, Value> entry : bs) {
                count++;
                // The key that is returned by the EvaluatingIterator is not the same key that is in
                // the partition table. The value that is returned by the EvaluatingIterator is a kryo
                // serialized EventFields object.
                processResults.resume();
                Document d = this.createDocument(entry.getKey(), entry.getValue());
                results.getResults().add(d);
                processResults.suspend();
            }
            processResults.stop();
            log.info(count + " matching entries found in full scan query.");
        } catch (TableNotFoundException e) {
            log.error(this.getTableName() + "not found", e);
        } finally {
            if (bs != null) {
                bs.close();
            }
        }
        fullScanQuery.stop();
    }

    log.info("AbstractQueryLogic: " + queryString + " " + timeString(abstractQueryLogic.getTime()));
    log.info("  1) parse query " + timeString(parseQuery.getTime()));
    log.info("  2) query metadata " + timeString(queryMetadata.getTime()));
    log.info("  3) full scan query " + timeString(fullScanQuery.getTime()));
    log.info("  3) optimized query " + timeString(optimizedQuery.getTime()));
    log.info("  1) process results " + timeString(processResults.getTime()));
    log.info("      1) query global index " + timeString(queryGlobalIndex.getTime()));
    log.info(hash + " Query completed.");

    return results;
}

From source file:org.glite.ce.monitor.holder.NotificationHolder.java

private void processPolicy(SensorEventArrayList eventList, Policy policy,
        CEMonitorConsumerStub.Notification notification, SubscriptionPersistent subscription) throws Exception {

    Query query = policy.getQuery();
    QueryProcessor qp = null;/*ww  w.j  ava2 s. c om*/

    Action[] actions = policy.getAction();
    if (actions == null) {
        throw (new Exception("policy.actions not found!"));
    }

    if (query != null) {

        qp = queryProcessorHolder.getQueryProcessor(query.getQueryLanguage());

        if (qp == null) {
            logger.error("QueryProcessor \"" + query.getQueryLanguage() + "\" not found!");
            throw (new Exception("QueryProcessor \"" + query.getQueryLanguage() + "\" not found!"));
        }
    }

    Dialect[] dialectArray = subscription.getTopic().getDialect();
    String dataFormat = "default";

    if ((dialectArray != null) && dialectArray.length > 0 && dialectArray[0].getName() != null) {
        dataFormat = dialectArray[0].getName();
    }

    ArrayList<Event> notificationEventList = new ArrayList<Event>(0);
    ArrayList<QueryResult> queryResultList = new ArrayList<QueryResult>(0);

    for (int i = 0; i < eventList.size(); i++) {
        SensorEvent event = eventList.get(i);

        if (event == null) {
            continue;
        }

        if (event.isExpired()) {

            try {
                logger.debug("removing expired event [id=" + event.getID() + "] [name=" + event.getName()
                        + "] [expirationTime=" + event.getExpirationTime() + "]");
                topicHolder.removeEvent(event);
            } catch (Throwable t) {
                logger.error(
                        "cannot remove the event [id=" + event.getID() + "] [name=" + event.getName() + "]!",
                        t);
            }

            eventList.remove(i);

            continue;

        }

        SensorOutputDataFormat format = event.getSensorOutputDataFormatApplied();

        if (format == null || !format.getName().equals(dataFormat)) {
            event.applyFormat(dataFormat);
        }

        if (qp != null) {
            queryResultList.add(qp.evaluate(query, event));
        } else {
            QueryResult queryRes = new QueryResult(event.getMessage().length);
            for (int x = 0; x < queryRes.size(); x++) {
                queryRes.setResult(x, true);
            }

            queryResultList.add(queryRes);
        }

        if (event.getMessage() != null && event.getMessage().length > 0) {
            Event tmpEvent = new Event();
            tmpEvent.setID(event.getID());
            tmpEvent.setTimestamp(event.getTimestamp());
            tmpEvent.setMessage(event.getMessage());
            tmpEvent.setProducer(event.getProducer());
            notificationEventList.add(tmpEvent);
        }
    }

    notificationEventList.trimToSize();
    CEMonitorConsumerStub.Event[] eventArray = new CEMonitorConsumerStub.Event[notificationEventList.size()];
    for (int k = 0; k < eventArray.length; k++) {
        Event srcEvn = notificationEventList.get(k);
        CEMonitorConsumerStub.Event dstEvn = new CEMonitorConsumerStub.Event();
        dstEvn.setID(srcEvn.getID());
        dstEvn.setMessage(srcEvn.getMessage());
        dstEvn.setProducer(srcEvn.getProducer());
        dstEvn.setTimestamp(srcEvn.getTimestamp());
        eventArray[k] = dstEvn;
    }
    notificationEventList.clear();

    notification.setEvent(eventArray);

    QueryResult[] queryResult = new QueryResult[notificationEventList.size()];
    queryResult = (QueryResult[]) queryResultList.toArray(queryResult);

    for (int i = 0; i < actions.length; i++) {
        try {

            Action action = new Action();
            action.setCreationTime(actions[i].getCreationTime());
            action.setDoActionWhenQueryIs(actions[i].isDoActionWhenQueryIs());
            action.setId(actions[i].getId());
            action.setJarPath(actions[i].getJarPath());
            action.setName(actions[i].getName());
            action.setType(actions[i].getType());
            action.setParameter(actions[i].getParameter());
            action.setProperty(actions[i].getProperty());
            action.addParameter(new Parameter("notification", notification));
            action.addParameter(new Parameter("queryResult", queryResult));
            action.addParameter(new Parameter("subscriptionId", subscription.getId()));
            action.execute();

        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
    }
}

From source file:net.sf.ehcache.Cache.java

/**
 * Returns a list of all element keys in the cache. Only keys of non-expired
 * elements are returned./*from  w w w.  j  a  v a2s  .  c  o m*/
 * <p/>
 * The returned keys are unique and can be considered a set.
 * <p/>
 * The List returned is not live. It is a copy.
 * <p/>
 * The time taken is O(n), where n is the number of elements in the cache. On
 * a 1.8Ghz P4, the time taken is approximately 200ms per 1000 entries. This method
 * is not syncrhonized, because it relies on a non-live list returned from {@link #getKeys()}
 * , which is synchronised, and which takes 8ms per 1000 entries. This way
 * cache liveness is preserved, even if this method is very slow to return.
 * <p/>
 * Consider whether your usage requires checking for expired keys. Because
 * this method takes so long, depending on cache settings, the list could be
 * quite out of date by the time you get it.
 *
 * @return a list of {@link Object} keys
 * @throws IllegalStateException if the cache is not {@link Status#STATUS_ALIVE}
 */
public final List getKeysWithExpiryCheck() throws IllegalStateException, CacheException {
    List allKeyList = getKeys();
    //remove keys of expired elements
    ArrayList nonExpiredKeys = new ArrayList(allKeyList.size());
    int allKeyListSize = allKeyList.size();
    for (int i = 0; i < allKeyListSize; i++) {
        Object key = allKeyList.get(i);
        Element element = getQuiet(key);
        if (element != null) {
            nonExpiredKeys.add(key);
        }
    }
    nonExpiredKeys.trimToSize();
    return nonExpiredKeys;
}

From source file:org.zaproxy.zap.extension.websocket.db.TableWebSocket.java

private PreparedStatement buildMessageCriteriaStatement(String query, WebSocketMessageDTO criteria,
        List<Integer> opcodes, List<Integer> inScopeChannelIds) throws SQLException, DatabaseException {
    ArrayList<String> where = new ArrayList<>();
    ArrayList<Object> params = new ArrayList<>();

    if (criteria.channel.id != null) {
        where.add("m.channel_id = ?");
        params.add(criteria.channel.id);
    }/*from   www . j  a  v a 2s. c o m*/

    if (criteria.isOutgoing != null) {
        where.add("m.is_outgoing = ?");
        params.add(criteria.isOutgoing);
    }

    if (opcodes != null && !opcodes.isEmpty()) {
        StringBuilder opcodeExpr = new StringBuilder("(");
        int opcodesCount = opcodes.size();

        for (int i = 0; i < opcodesCount; i++) {
            params.add(opcodes.get(i));

            opcodeExpr.append("m.opcode = ?");
            if ((i + 1) < opcodesCount) {
                opcodeExpr.append(" OR ");
            }
        }

        opcodeExpr.append(")");
        where.add(opcodeExpr.toString());
    }
    if (inScopeChannelIds != null) {
        StringBuilder whereExpr = new StringBuilder("m.channel_id IN (");
        int inScopeChannelCount = inScopeChannelIds.size();

        if (inScopeChannelCount > 0) {
            for (int i = 0; i < inScopeChannelCount; i++) {
                params.add(inScopeChannelIds.get(i));

                whereExpr.append("?");
                if ((i + 1) < inScopeChannelCount) {
                    whereExpr.append(",");
                }
            }
        } else {
            whereExpr.append("null");
        }

        whereExpr.append(")");
        where.add(whereExpr.toString());
    }

    if (criteria instanceof WebSocketFuzzMessageDTO) {
        WebSocketFuzzMessageDTO fuzzCriteria = (WebSocketFuzzMessageDTO) criteria;
        if (fuzzCriteria.fuzzId != null) {
            params.add(fuzzCriteria.fuzzId);
            where.add("f.fuzz_id = ?");
        }
    }

    where.trimToSize();
    params.trimToSize();

    return buildCriteriaStatementHelper(query, where, params);
}

From source file:com.flexive.ejb.beans.BriefcaseEngineBean.java

/**
 * Gets a list of all briefcase for the calling user.
 *
 * @param idFilter      if set only the pricelist with the given id will be loaded
 * @param includeShared if enabled shared briefcases will be included, if disabled only
 *                      the briefcases created by the calling user will be returned
 * @return the briefcases//from w ww.  j ava  2  s  .  c om
 * @throws FxApplicationException if the function fails
 */
private List<Briefcase> getList(Long idFilter, boolean includeShared) throws FxApplicationException {
    Connection con = null;
    Statement stmt = null;
    String sql;
    final ArrayList<Briefcase> result = new ArrayList<Briefcase>(500);
    try {
        // Obtain a database connection
        con = Database.getDbConnection();
        sql = "SELECT " +
        //1,   2,  3        ,  4         , 5 ,    6        7         8              9      ,  10    , 11
                "ID,NAME,DESCRIPTION,SOURCE_QUERY,ACL,CREATED_BY,CREATED_AT,MODIFIED_BY,MODIFIED_AT,MANDATOR,ICON_ID, "
                +
                // 12
                "(SELECT COUNT(*) FROM " + TBL_BRIEFCASE_DATA + " bd WHERE bd.briefcase_id=b.id) AS \"size\""
                + "FROM " + DatabaseConst.TBL_BRIEFCASE + " b WHERE ";
        sql += getSqlAccessFilter(null, includeShared, ACLPermission.READ);
        if (idFilter != null) {
            sql += " and id=" + idFilter;
        }

        stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        while (rs != null && rs.next()) {
            final long id = rs.getLong(1);
            final String name = rs.getString(2);
            String desc = rs.getString(3);
            if (rs.wasNull())
                desc = "";
            String src = rs.getString(4);
            if (rs.wasNull())
                src = "";
            long acl = rs.getLong(5);
            if (rs.wasNull())
                acl = -1;
            final LifeCycleInfo lc = LifeCycleInfoImpl.load(rs, 6, 7, 8, 9);
            final long mandator = rs.getLong(10);
            final long iconId = rs.getLong(11);
            final int size = rs.getInt(12);
            result.add(new Briefcase(id, name, mandator, desc, src, acl, lc, iconId, size));
        }
        result.trimToSize();
        return result;
    } catch (SQLException exc) {
        throw new FxLoadException(LOG, exc, "ex.briefcase.failedToLoadList", exc.getMessage());
    } finally {
        closeObjects(BriefcaseEngineBean.class, con, stmt);
    }
}