// build an input taxonomy index
Directory src = newDirectory();
DirectoryTaxonomyWriter tw = new DirectoryTaxonomyWriter(src);
for (int i = 0; i < numCategories; i++) {
tw.addCategory(new FacetLabel("a", Integer.toString(i)));
}
tw.close();
// now add the taxonomy to an empty taxonomy, while adding the categories
// again, in parallel -- in the end, no duplicate categories should exist.
Directory dest = newDirectory();
final DirectoryTaxonomyWriter destTW = new DirectoryTaxonomyWriter(dest);
Thread t = new Thread() {
@Override
public void run() {
for (int i = 0; i < numCategories; i++) {
try {
destTW.addCategory(new FacetLabel("a", Integer.toString(i)));
} catch (IOException e) {
// shouldn't happen - if it does, let the test fail on uncaught exception.
throw new RuntimeException(e);
}
}
}
};
t.start();
OrdinalMap map = new MemoryOrdinalMap();
destTW.addTaxonomy(src, map);
t.join();
destTW.close();
// now validate
DirectoryTaxonomyReader dtr = new DirectoryTaxonomyReader(dest);
// +2 to account for the root category + "a"
assertEquals(numCategories + 2, dtr.getSize());
HashSet<FacetLabel> categories = new HashSet<>();
for (int i = 1; i < dtr.getSize(); i++) {
FacetLabel cat = dtr.getPath(i);
assertTrue("category " + cat + " already existed", categories.add(cat));
}
dtr.close();
IOUtils.close(src, dest);