Commit f70c06a2 authored by Matija Obreza's avatar Matija Obreza
Browse files

TaxonomyDatabase allows fetching speciesRow and genus by Id, lists speciesRows

parent 11265e63
......@@ -41,11 +41,17 @@ public class InMemoryTaxonomyDatabase implements TaxonomyDatabase {
private final static Logger LOG = LoggerFactory.getLogger(InMemoryTaxonomyDatabase.class);
/** The genus id lookup. */
private Map<String, List<Long>> genusIdLookup = new HashMap<>();
private Map<Long, String> genusIdLookup = new HashMap<>();
/** The genus id lookup. */
private Map<String, List<Long>> genusIdSpecies = new HashMap<>();
/** The species lookup. */
private Map<Long, List<SpeciesRow>> speciesLookup = new HashMap<>();
/** The species lookup. */
private Map<Long, SpeciesRow> speciesIdLookup = new HashMap<>();
/** The species rows. */
private int speciesRows;
......@@ -56,10 +62,11 @@ public class InMemoryTaxonomyDatabase implements TaxonomyDatabase {
* @param genus the genus
*/
public void registerGenus(Long genusId, String genus) {
if (!genusIdLookup.containsKey(genus)) {
genusIdLookup.put(genus, new ArrayList<>(1));
if (!genusIdSpecies.containsKey(genus)) {
genusIdSpecies.put(genus, new ArrayList<>(1));
}
genusIdLookup.get(genus).add(genusId);
genusIdLookup.put(genusId, genus);
genusIdSpecies.get(genus).add(genusId);
speciesLookup.put(genusId, new ArrayList<>(1));
}
......@@ -75,6 +82,10 @@ public class InMemoryTaxonomyDatabase implements TaxonomyDatabase {
throw new TaxonomyException("No genus with specified genusId");
genusSpecies.add(speciesRow);
if (speciesIdLookup.containsKey(speciesRow.getSpeciesId())) {
throw new TaxonomyException("Duplicate speciesId not allowed");
}
speciesIdLookup.put(speciesRow.getSpeciesId(), speciesRow);
speciesRows++;
}
......@@ -85,7 +96,7 @@ public class InMemoryTaxonomyDatabase implements TaxonomyDatabase {
*/
@Override
public String toString() {
return "InMemory Taxonomy Database: " + genusIdLookup.size() + " genera" + " and " + speciesRows + " species";
return "InMemory Taxonomy Database: " + genusIdSpecies.size() + " genera" + " and " + speciesRows + " species";
}
/**
......@@ -96,7 +107,7 @@ public class InMemoryTaxonomyDatabase implements TaxonomyDatabase {
*/
@Override
public boolean containsGenus(String genus) {
return genusIdLookup.containsKey(genus);
return genusIdSpecies.containsKey(genus);
// .keySet().stream().anyMatch(g -> g.equalsIgnoreCase(genus));
}
......@@ -114,7 +125,7 @@ public class InMemoryTaxonomyDatabase implements TaxonomyDatabase {
BestScore bestScore = new BestScore();
return genusIdLookup.keySet().parallelStream()
return genusIdSpecies.keySet().parallelStream()
// convert each genus to Suggestion
.map(candidate -> new Suggestion<String>(candidate, similarityScore(genus, candidate)))
// debug print
......@@ -171,11 +182,11 @@ public class InMemoryTaxonomyDatabase implements TaxonomyDatabase {
* @return the all genus species
*/
protected List<SpeciesRow> getAllGenusSpecies(String genus) {
if (!genusIdLookup.containsKey(genus)) {
if (!genusIdSpecies.containsKey(genus)) {
return Collections.emptyList();
}
return genusIdLookup.get(genus).stream()
return genusIdSpecies.get(genus).stream()
// .peek(InMemoryTaxonomyDatabase::print)
.map(genusId -> speciesLookup.get(genusId)).reduce(new ArrayList<SpeciesRow>(1), (all, genusSpecies) -> {
all.addAll(genusSpecies);
......@@ -192,7 +203,7 @@ public class InMemoryTaxonomyDatabase implements TaxonomyDatabase {
public boolean containsSpecies(String genus, String species) {
LOG.trace("Does database contain genus={} species={}", genus, species);
if (!genusIdLookup.containsKey(genus)) {
if (!genusIdSpecies.containsKey(genus)) {
return false;
}
......@@ -211,7 +222,7 @@ public class InMemoryTaxonomyDatabase implements TaxonomyDatabase {
// Don't make suggestions
return Collections.emptyList();
}
List<Long> genusId = genusIdLookup.get(genus);
List<Long> genusId = genusIdSpecies.get(genus);
if (genusId == null) {
throw new UnsupportedOperationException("Genus does not exist in database. Genus=" + genus);
}
......@@ -268,7 +279,7 @@ public class InMemoryTaxonomyDatabase implements TaxonomyDatabase {
*/
@Override
public String getSpeciesAuthority(String genus, String species) {
List<Long> genusId = genusIdLookup.get(genus);
List<Long> genusId = genusIdSpecies.get(genus);
if (genusId == null) {
return null;
}
......@@ -284,14 +295,16 @@ public class InMemoryTaxonomyDatabase implements TaxonomyDatabase {
.map(speciesRow -> speciesRow.getSpeciesAuthority()).orElse(null);
}
/* (non-Javadoc)
/*
* (non-Javadoc)
*
* @see org.genesys2.gringlobal.taxonomy.component.TaxonomyDatabase#containsSubtaxa(java.lang.String, java.lang.String, java.lang.String)
*/
@Override
public boolean containsSubtaxa(String genus, String species, String subtaxa) {
LOG.trace("Does database contain genus={} species={}", genus, species);
if (!genusIdLookup.containsKey(genus)) {
if (!genusIdSpecies.containsKey(genus)) {
return false;
}
......@@ -302,14 +315,16 @@ public class InMemoryTaxonomyDatabase implements TaxonomyDatabase {
.anyMatch(speciesRow -> subtaxa.equals(speciesRow.getSubtaxa()));
}
/* (non-Javadoc)
/*
* (non-Javadoc)
*
* @see org.genesys2.gringlobal.taxonomy.component.TaxonomyDatabase#findSimilarSubtaxa(java.lang.String, java.lang.String, java.lang.String, int)
*/
@Override
public List<String> findSimilarSubtaxa(String genus, String species, String subtaxa, int maxSize) {
LOG.debug("Searching similar subtaxa for genus={} species={} subtaxa={}", genus, species, subtaxa);
List<Long> genusId = genusIdLookup.get(genus);
List<Long> genusId = genusIdSpecies.get(genus);
if (genusId == null) {
throw new UnsupportedOperationException("Genus does not exist in database. Genus=" + genus);
}
......@@ -347,12 +362,14 @@ public class InMemoryTaxonomyDatabase implements TaxonomyDatabase {
.limit(maxSize).collect(Collectors.toList());
}
/* (non-Javadoc)
/*
* (non-Javadoc)
*
* @see org.genesys2.gringlobal.taxonomy.component.TaxonomyDatabase#getSubtaxaAuthority(java.lang.String, java.lang.String, java.lang.String)
*/
@Override
public String getSubtaxaAuthority(String genus, String species, String subtaxa) {
List<Long> genusId = genusIdLookup.get(genus);
List<Long> genusId = genusIdSpecies.get(genus);
if (genusId == null) {
return null;
}
......@@ -370,7 +387,9 @@ public class InMemoryTaxonomyDatabase implements TaxonomyDatabase {
.map(speciesRow -> speciesRow.getSubtaxaAuthority()).orElse(null);
}
/* (non-Javadoc)
/*
* (non-Javadoc)
*
* @see org.genesys2.gringlobal.taxonomy.component.TaxonomyDatabase#listSpecies(java.lang.String, java.lang.String, int)
*/
@Override
......@@ -381,4 +400,25 @@ public class InMemoryTaxonomyDatabase implements TaxonomyDatabase {
// limit results to maxSize
.limit(maxSize).collect(Collectors.toList());
}
@Override
public List<SpeciesRow> findSpeciesRow(String genus, String species, String subtaxa) {
return getAllGenusSpecies(genus).stream()
// keep rows with matching species
.filter(speciesRow -> species.equals(speciesRow.getSpeciesName()))
// match subtaxa
.filter(speciesRow -> subtaxa.equals(speciesRow.getSubtaxa()))
// to list
.collect(Collectors.toList());
}
@Override
public SpeciesRow getSpeciesRow(long speciesId) {
return speciesIdLookup.get(speciesId);
}
@Override
public String getGenus(long genusId) {
return genusIdLookup.get(genusId);
}
}
......@@ -103,6 +103,7 @@ public interface TaxonomyDatabase {
/**
* Return all subtaxa for genus and species
*
* @param genus valid genus
* @param species valid species
* @param maxSize maximum number of rows to return
......@@ -110,4 +111,30 @@ public interface TaxonomyDatabase {
*/
List<SpeciesRow> listSpecies(String genus, String species, int maxSize);
/**
* Find SpeciesRows matching the definition
*
* @param genus the genus
* @param species the species
* @param subtaxa the MCPD subtaxa
* @return list of matching SpeciesRows
*/
List<SpeciesRow> findSpeciesRow(String genus, String species, String subtaxa);
/**
* Get species by speciesId
*
* @param speciesId duh
* @return SpeciesRow or null if not found
*/
SpeciesRow getSpeciesRow(long speciesId);
/**
* Get genus by genusId
*
* @param genusId duh
* @return genus name or null
*/
String getGenus(long genusId);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment