-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
harmonize db.get* methods; multithreaded usage search for units
- Loading branch information
Showing
19 changed files
with
187 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
olca-core/src/main/java/org/openlca/core/database/usage/UnitUsageSearch.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package org.openlca.core.database.usage; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.Executors; | ||
|
||
import org.openlca.core.database.IDatabase; | ||
import org.openlca.core.database.NativeSql; | ||
import org.openlca.core.model.Epd; | ||
import org.openlca.core.model.ImpactCategory; | ||
import org.openlca.core.model.Process; | ||
import org.openlca.core.model.Project; | ||
import org.openlca.core.model.Result; | ||
import org.openlca.core.model.RootEntity; | ||
import org.openlca.core.model.SocialIndicator; | ||
import org.openlca.core.model.Unit; | ||
import org.openlca.core.model.descriptors.RootDescriptor; | ||
|
||
/** | ||
* Searches for data sets where a unit is used. This does not include the unit | ||
* group where the unit is defined. | ||
*/ | ||
public record UnitUsageSearch(IDatabase db, Unit unit) { | ||
|
||
public List<RootDescriptor> run() { | ||
if (unit == null || db == null) | ||
return Collections.emptyList(); | ||
try { | ||
var exec = Executors.newFixedThreadPool(5); | ||
var results = exec.invokeAll(List.of( | ||
q(Process.class, | ||
"tbl_exchanges", "f_owner", "f_unit"), | ||
q(ImpactCategory.class, | ||
"tbl_impact_factors", "f_impact_category", "f_unit"), | ||
q(Project.class, | ||
"tbl_project_variants", "f_project", "f_unit"), | ||
q(Result.class, | ||
"tbl_flow_results", "f_result", "f_unit"), | ||
q(SocialIndicator.class, | ||
"tbl_social_indicators", "id", "f_activity_unit"), | ||
q(Epd.class, | ||
"tbl_epds", "id", "f_unit") | ||
)); | ||
exec.shutdown(); | ||
var descriptors = new ArrayList<RootDescriptor>(); | ||
for (var result : results) { | ||
descriptors.addAll(result.get()); | ||
} | ||
return descriptors; | ||
} catch (Exception e) { | ||
throw new RuntimeException("failed to query for usages of " + unit, e); | ||
} | ||
} | ||
|
||
private <T extends RootEntity> Callable<List<? extends RootDescriptor>> q( | ||
Class<T> type, String table, String entityField, String unitField) { | ||
return () -> { | ||
var query = "select distinct " + entityField | ||
+ " from " + table + " where " + unitField + " = " + unit.id; | ||
var ids = new HashSet<Long>(); | ||
NativeSql.on(db).query(query, r -> { | ||
ids.add(r.getLong(1)); | ||
return true; | ||
}); | ||
return ids.isEmpty() | ||
? Collections.emptyList() | ||
: db.getDescriptors(type, ids); | ||
}; | ||
} | ||
} |
42 changes: 0 additions & 42 deletions
42
olca-core/src/main/java/org/openlca/core/database/usage/UnitUseSearch.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
olca-core/src/test/java/org/openlca/core/database/usage/UnitUsageSearchTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package org.openlca.core.database.usage; | ||
|
||
|
||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.openlca.core.Tests; | ||
import org.openlca.core.database.IDatabase; | ||
import org.openlca.core.model.Exchange; | ||
import org.openlca.core.model.ImpactCategory; | ||
import org.openlca.core.model.ImpactFactor; | ||
import org.openlca.core.model.Process; | ||
import org.openlca.core.model.RootEntity; | ||
import org.openlca.core.model.SocialIndicator; | ||
import org.openlca.core.model.Unit; | ||
import org.openlca.core.model.UnitGroup; | ||
import org.openlca.core.model.descriptors.Descriptor; | ||
|
||
public class UnitUsageSearchTest { | ||
|
||
private final IDatabase db = Tests.getDb(); | ||
private UnitUsageSearch search; | ||
private UnitGroup group; | ||
private Unit unit; | ||
|
||
@Before | ||
public void setup() { | ||
group = db.insert(UnitGroup.of("Units of mass", "kg")); | ||
unit = group.referenceUnit; | ||
search = new UnitUsageSearch(db, unit); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
db.delete(group); | ||
} | ||
|
||
@Test | ||
public void testFindNoUsage() { | ||
var models = search.run(); | ||
Assert.assertNotNull(models); | ||
Assert.assertTrue(models.isEmpty()); | ||
} | ||
|
||
@Test | ||
public void testFindInImpactCategories() { | ||
var impact = ImpactCategory.of("my impact"); | ||
var factor = new ImpactFactor(); | ||
factor.unit = unit; | ||
impact.impactFactors.add(factor); | ||
check(impact); | ||
} | ||
|
||
@Test | ||
public void testFindInProcesses() { | ||
var process = new Process(); | ||
var exchange = new Exchange(); | ||
exchange.unit = unit; | ||
process.exchanges.add(exchange); | ||
check(process); | ||
} | ||
|
||
@Test | ||
public void testFindInSocialIndicator() { | ||
var indicator = new SocialIndicator(); | ||
indicator.name = "indicator"; | ||
indicator.activityUnit = unit; | ||
check(indicator); | ||
} | ||
|
||
private void check(RootEntity e) { | ||
db.insert(e); | ||
var results = search.run(); | ||
db.delete(e); | ||
var expected = Descriptor.of(e); | ||
Assert.assertEquals(1, results.size()); | ||
Assert.assertEquals(expected, results.get(0)); | ||
} | ||
|
||
} |
105 changes: 0 additions & 105 deletions
105
olca-core/src/test/java/org/openlca/core/database/usage/UnitUseSearchTest.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.