Skip to content

Commit

Permalink
Correct logic to protect against NoSuchElementException (#2069)
Browse files Browse the repository at this point in the history
  • Loading branch information
apmoriarty committed Aug 17, 2023
1 parent 9ee7757 commit ceec11c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Tuple3<Key,Document,Map<String,Object>> apply(Tuple2<Key,Document> from)

logStart();
Map<String,Object> map = new HashMap<>(tfPopulator.getContextMap(from.first(), docKeys, fields));
logStop(docKeys.iterator().next());
logStop(docKeys);

Document merged = from.second();
merged.putAll(tfPopulator.document(), false);
Expand Down Expand Up @@ -96,14 +96,19 @@ private void logStart() {
aggregationStart = System.currentTimeMillis();
}

private void logStop(Key k) {
private void logStop(Set<Key> keys) {
if (aggregationThreshold == -1) {
return;
}

long elapsed = System.currentTimeMillis() - aggregationStart;
if (elapsed > aggregationThreshold) {
log.warn("time to aggregate offsets " + k.getRow() + " " + k.getColumnFamily().toString().replace("\0", "0x00") + " was " + elapsed);
if (keys == null || keys.isEmpty()) {
log.warn("time to aggregate offsets was " + elapsed);
} else {
Key k = keys.iterator().next();
log.warn("time to aggregate offsets " + k.getRow() + " " + k.getColumnFamily().toString().replace("\0", "0x00") + " was " + elapsed);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package datawave.query.postprocessing.tf;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.accumulo.core.data.Key;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;

import datawave.query.attributes.Document;
import datawave.query.jexl.JexlASTHelper;
import datawave.query.util.Tuple2;
import datawave.query.util.Tuple3;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class TermOffsetFunctionTest {

@Test
public void testApplyWithLogLogAggregationPruneToZero() throws Exception {
TermFrequencyConfig config = new TermFrequencyConfig();
config.setScript(JexlASTHelper.parseAndFlattenJexlQuery("content:phrase(FOO, termOffsetMap, 'bar', 'baz')"));
DocumentKeysFunction docKeyFunction = new DocumentKeysFunction(config);

MyTermOffsetPopulator termOffsetPopulator = new MyTermOffsetPopulator(null, config);

TermOffsetFunction termOffsetFunction = new TermOffsetFunction(termOffsetPopulator, Collections.emptySet(), docKeyFunction);
Tuple3<Key,Document,Map<String,Object>> tuple = termOffsetFunction.apply(new Tuple2<>(new Key(), new Document()));
assertEquals(new Key(), tuple.first());
assertEquals(new Document(), tuple.second());
assertEquals(new HashMap<String,Object>(), tuple.third());
}

// no-op
private static class MyTermOffsetPopulator extends TermOffsetPopulator {

public MyTermOffsetPopulator(Multimap<String,String> termFrequencyFieldValues, TermFrequencyConfig config) {
super(termFrequencyFieldValues, config);
}

public Multimap<String,String> getTermFrequencyFieldValues() {
return HashMultimap.create();
}

public Map<String,Object> getContextMap(Key docKey, Set<Key> keys, Set<String> fields) {
return new HashMap<>();
}

public Document document() {
return new Document();
}
}

}

0 comments on commit ceec11c

Please sign in to comment.