use of org.apache.lucene.util.Counter in project languagetool by languagetool-org.
the class Searcher method getTopDocs.
private PossiblyLimitedTopDocs getTopDocs(Query query) throws IOException {
TopScoreDocCollector topCollector = TopScoreDocCollector.create(maxHits);
Counter clock = Counter.newCounter(true);
int waitMillis = 1000;
// TODO: if we interrupt the whole thread anyway, do we still need the TimeLimitingCollector?
TimeLimitingCollector collector = new TimeLimitingCollector(topCollector, clock, maxSearchTimeMillis / waitMillis);
collector.setBaseline(0);
Thread counterThread = new Thread() {
@Override
public void run() {
long startTime = System.currentTimeMillis();
while (true) {
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);
}
use of org.apache.lucene.util.Counter in project lucene-solr by apache.
the class TestIntBlockPool method testMultipleWriterReader.
public void testMultipleWriterReader() {
Counter bytesUsed = Counter.newCounter();
IntBlockPool pool = new IntBlockPool(new ByteTrackingAllocator(bytesUsed));
for (int j = 0; j < 2; j++) {
List<StartEndAndValues> holders = new ArrayList<>();
int num = atLeast(4);
for (int i = 0; i < num; i++) {
holders.add(new StartEndAndValues(random().nextInt(1000)));
}
IntBlockPool.SliceWriter writer = new IntBlockPool.SliceWriter(pool);
IntBlockPool.SliceReader reader = new IntBlockPool.SliceReader(pool);
int numValuesToWrite = atLeast(10000);
for (int i = 0; i < numValuesToWrite; i++) {
StartEndAndValues values = holders.get(random().nextInt(holders.size()));
if (values.valueCount == 0) {
values.start = writer.startNewSlice();
} else {
writer.reset(values.end);
}
writer.writeInt(values.nextValue());
values.end = writer.getCurrentOffset();
if (random().nextInt(5) == 0) {
// pick one and reader the ints
assertReader(reader, holders.get(random().nextInt(holders.size())));
}
}
while (!holders.isEmpty()) {
StartEndAndValues values = holders.remove(random().nextInt(holders.size()));
assertReader(reader, values);
}
if (random().nextBoolean()) {
pool.reset(true, false);
assertEquals(0, bytesUsed.get());
} else {
pool.reset(true, true);
assertEquals(IntBlockPool.INT_BLOCK_SIZE * Integer.BYTES, bytesUsed.get());
}
}
}
use of org.apache.lucene.util.Counter in project SearchServices by Alfresco.
the class MimetypeGroupingCollector method collect.
@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;
}
Counter counter = counters.get(group);
if (counter == null) {
counter = Counter.newCounter();
counters.put(group, counter);
}
counter.addAndGet(1);
}
}
leafDelegate.collect(doc);
}
use of org.apache.lucene.util.Counter in project SearchServices by Alfresco.
the class MimetypeGroupingCollector method finish.
public void finish() throws IOException {
NamedList<Object> analytics = new NamedList<>();
rb.rsp.add("analytics", analytics);
NamedList<Object> fieldCounts = new NamedList<>();
analytics.add("mimetype()", fieldCounts);
for (String key : counters.keySet()) {
Counter counter = counters.get(key);
fieldCounts.add(key, counter.get());
}
if (this.delegate instanceof DelegatingCollector) {
((DelegatingCollector) this.delegate).finish();
}
}
use of org.apache.lucene.util.Counter in project lucene-solr by apache.
the class TestIntBlockPool method testSingleWriterReader.
public void testSingleWriterReader() {
Counter bytesUsed = Counter.newCounter();
IntBlockPool pool = new IntBlockPool(new ByteTrackingAllocator(bytesUsed));
for (int j = 0; j < 2; j++) {
IntBlockPool.SliceWriter writer = new IntBlockPool.SliceWriter(pool);
int start = writer.startNewSlice();
int num = atLeast(100);
for (int i = 0; i < num; i++) {
writer.writeInt(i);
}
int upto = writer.getCurrentOffset();
IntBlockPool.SliceReader reader = new IntBlockPool.SliceReader(pool);
reader.reset(start, upto);
for (int i = 0; i < num; i++) {
assertEquals(i, reader.readInt());
}
assertTrue(reader.endOfSlice());
if (random().nextBoolean()) {
pool.reset(true, false);
assertEquals(0, bytesUsed.get());
} else {
pool.reset(true, true);
assertEquals(IntBlockPool.INT_BLOCK_SIZE * Integer.BYTES, bytesUsed.get());
}
}
}
Aggregations