Skip to content

Commit

Permalink
Merge pull request #4 from jihoonson/feature/avoid_null
Browse files Browse the repository at this point in the history
Avoid returning nulls when the result is not found
  • Loading branch information
shin285 authored Dec 19, 2016
2 parents 74c1fab + cb6aa9a commit 60fd6d6
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public Map<String,V> get(FindContext<V> context, char key) {
if (children == null) {
final AhoCorasickNode<V> currentNode = context.getCurrentFailNode();
if (currentNode == null) {
return null;
return new HashMap<>();
} else {
context.setCurrentNode(currentNode);
}
Expand Down Expand Up @@ -171,9 +171,7 @@ public Map<String,V> get(FindContext<V> context, char key) {
}
break;
}
if(resultMap.size() == 0){
return null;
}

return resultMap;
}

Expand Down Expand Up @@ -209,9 +207,6 @@ public Map<String,V> get(FindContext<V> context, char[] keys) {
for (int i = 0; i < keys.length; i++) {
resultMap.putAll(get(context, keys[i]));
}
if(resultMap.size() == 0){
return null;
}
return resultMap;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package kr.co.shineware.ds.aho_corasick;

import static org.junit.Assert.assertEquals;

import java.util.*;
import java.util.concurrent.*;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class AhoCorasickDictionaryConcurrencyTest {

private static ExecutorService service;
private static final int THREAD_NUM = 4;
private static final int DATA_NUM = 100000;

private static String[] KEYS;
private static Integer[] VALS;

private static final AhoCorasickDictionary<Integer> dic = new AhoCorasickDictionary<>();

@BeforeClass
public static void setup() {
KEYS = new String[DATA_NUM];
VALS = new Integer[DATA_NUM];

for (int i = 0; i < KEYS.length; i++) {
KEYS[i] = Integer.toString(i);
VALS[i] = i;
}

for (int i = 0; i < KEYS.length; i++) {
dic.put(KEYS[i], VALS[i]);
}
dic.buildFailLink();

service = Executors.newFixedThreadPool(THREAD_NUM);
}

@AfterClass
public static void teardown() {
if (service != null) {
service.shutdown();
}
}

@Test
public void testSequentialGet() {
for (int i = 0; i < KEYS.length; i++) {
final Map<String, Integer> result = dic.get(KEYS[i]);
final Set<Integer> expectedResult = expectedResultSet(KEYS[i]);

assertEquals(expectedResult, new HashSet<>(result.values()));
}
}

@Test
public void testConcurrentGet() throws ExecutionException, InterruptedException {
final List<Future> futures = new ArrayList<>();
for (int i = 0; i < KEYS.length; i++) {
final int index = i;
futures.add(service.submit(new Callable<Set<Integer>>() {
@Override
public Set<Integer> call() throws Exception {
return new HashSet<>(dic.get(KEYS[index]).values());
}
}));
}

for (int i = 0; i < futures.size(); i++) {
final Set<Integer> expectedResult = expectedResultSet(KEYS[i]);
assertEquals(expectedResult, futures.get(i).get());
}
}

private static Set<Integer> expectedResultSet(final String str) {
final Set<Integer> resultSet = new HashSet<>();

for (int subStrLen = 1; subStrLen < str.length() + 1; subStrLen++) {
for (int subStrBeginIdx = 0; subStrBeginIdx + subStrLen <= str.length(); subStrBeginIdx++) {
final String subStr = str.substring(subStrBeginIdx, subStrBeginIdx + subStrLen);
resultSet.add(Integer.parseInt(subStr));
}
}

return resultSet;
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
package kr.co.shineware.ds.aho_corasick;

import static org.junit.Assert.assertEquals;

import java.util.*;
import java.util.concurrent.*;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class AhoCorasickDictionaryTest {
import java.util.HashMap;
import java.util.Map;

private static ExecutorService service;
private static final int THREAD_NUM = 4;
private static final int DATA_NUM = 100000;
import static org.junit.Assert.assertEquals;

public class AhoCorasickDictionaryTest {

private static final int DATA_NUM = 100;
private static String[] KEYS;
private static Integer[] VALS;

Expand All @@ -34,15 +30,6 @@ public static void setup() {
dic.put(KEYS[i], VALS[i]);
}
dic.buildFailLink();

service = Executors.newFixedThreadPool(THREAD_NUM);
}

@AfterClass
public static void teardown() {
if (service != null) {
service.shutdown();
}
}

@Test
Expand Down Expand Up @@ -71,44 +58,7 @@ private static Map<String, Integer> expectedResultMapForGetWithSingleChar(final
}

@Test
public void testSequentialGet() {
for (int i = 0; i < KEYS.length; i++) {
final Map<String, Integer> result = dic.get(KEYS[i]);
final Set<Integer> expectedResult = expectedResultSet(KEYS[i]);

assertEquals(expectedResult, new HashSet<>(result.values()));
}
}

@Test
public void testConcurrentGet() throws ExecutionException, InterruptedException {
final List<Future> futures = new ArrayList<>();
for (int i = 0; i < KEYS.length; i++) {
final int index = i;
futures.add(service.submit(new Callable<Set<Integer>>() {
@Override
public Set<Integer> call() throws Exception {
return new HashSet<>(dic.get(KEYS[index]).values());
}
}));
}

for (int i = 0; i < futures.size(); i++) {
final Set<Integer> expectedResult = expectedResultSet(KEYS[i]);
assertEquals(expectedResult, futures.get(i).get());
}
}

private static Set<Integer> expectedResultSet(final String str) {
final Set<Integer> resultSet = new HashSet<>();

for (int subStrLen = 1; subStrLen < str.length() + 1; subStrLen++) {
for (int subStrBeginIdx = 0; subStrBeginIdx + subStrLen <= str.length(); subStrBeginIdx++) {
final String subStr = str.substring(subStrBeginIdx, subStrBeginIdx + subStrLen);
resultSet.add(Integer.parseInt(subStr));
}
}

return resultSet;
public void testNotFound() {
assertEquals(new HashMap<>(), dic.get(dic.newFindContext(), "가"));
}
}

0 comments on commit 60fd6d6

Please sign in to comment.