Example usage for org.apache.lucene.util Counter addAndGet

List of usage examples for org.apache.lucene.util Counter addAndGet

Introduction

In this page you can find the example usage for org.apache.lucene.util Counter addAndGet.

Prototype

public abstract long addAndGet(long delta);

Source Link

Document

Adds the given delta to the counters current value

Usage

From source file:org.alfresco.solr.query.MimetypeGroupingCollector.java

License:Open Source License

@Override
public void collect(int doc) throws IOException {
    if (sortedDocValues != null) {
        int ordinal = sortedDocValues.getOrd(doc);
        if (ordinal > -1) {
            String value = (String) schemaField.getType().toObject(schemaField,
                    sortedDocValues.lookupOrd(ordinal));
            String group = doGroup ? mappings.get(value) : value;
            if (group == null) {
                group = value;//from ww  w . j  ava  2s .  c o  m
            }

            Counter counter = counters.get(group);
            if (counter == null) {
                counter = Counter.newCounter();
                counters.put(group, counter);
            }
            counter.addAndGet(1);
        }
    }

    leafDelegate.collect(doc);
}

From source file:org.elasticsearch.index.translog.TranslogDeletionPolicy.java

License:Apache License

/**
 * releases a generation that was acquired by {@link #acquireTranslogGen(long)}
 *//*ww  w.  j a v  a2  s.c  om*/
private synchronized void releaseTranslogGen(long translogGen) {
    Counter current = translogRefCounts.get(translogGen);
    if (current == null || current.get() <= 0) {
        throw new IllegalArgumentException("translog gen [" + translogGen + "] wasn't acquired");
    }
    if (current.addAndGet(-1) == 0) {
        translogRefCounts.remove(translogGen);
    }
}

From source file:org.languagetool.dev.index.Searcher.java

License:Open Source License

private PossiblyLimitedTopDocs getTopDocs(Query query, Sort sort) throws IOException {
    final TopFieldCollector topCollector = TopFieldCollector.create(sort, maxHits, true, false, false, false);
    final Counter clock = Counter.newCounter(true);
    final int waitMillis = 1000;
    // TODO: if we interrupt the whole thread anyway, do we still need the TimeLimitingCollector?
    final TimeLimitingCollector collector = new TimeLimitingCollector(topCollector, clock,
            maxSearchTimeMillis / waitMillis);
    collector.setBaseline(0);//from  w  w  w.ja va  2s  . c om
    final Thread counterThread = new Thread() {
        @Override
        public void run() {
            final long startTime = System.currentTimeMillis();
            while (true) {
                final long runTimeMillis = System.currentTimeMillis() - startTime;
                if (runTimeMillis > maxSearchTimeMillis) {
                    // make sure there's no lingering thread for too long
                    return;
                }
                clock.addAndGet(1);
                try {
                    Thread.sleep(waitMillis);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    };
    counterThread.setName("LuceneSearchTimeoutThread");
    counterThread.start();

    boolean timeLimitActivated = false;
    try {
        indexSearcher.search(query, collector);
    } catch (TimeLimitingCollector.TimeExceededException e) {
        timeLimitActivated = true;
    }
    return new PossiblyLimitedTopDocs(topCollector.topDocs(), timeLimitActivated);
}