diff backend/src/main/java/org/dive4elements/river/importer/ImportSedimentLoadValue.java @ 8986:392bbcd8a88b

Database inserts accelerated by suppressing unnecessary database queries for new data series
author mschaefer
date Sun, 08 Apr 2018 18:07:06 +0200
parents 5e38e2924c07
children
line wrap: on
line diff
--- a/backend/src/main/java/org/dive4elements/river/importer/ImportSedimentLoadValue.java	Fri Apr 06 14:13:14 2018 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/ImportSedimentLoadValue.java	Sun Apr 08 18:07:06 2018 +0200
@@ -10,6 +10,7 @@
 
 import java.util.List;
 
+import org.dive4elements.river.importer.common.StoreMode;
 import org.dive4elements.river.model.MeasurementStation;
 import org.dive4elements.river.model.SedimentLoad;
 import org.dive4elements.river.model.SedimentLoadValue;
@@ -27,43 +28,48 @@
     }
 
     public ImportSedimentLoadValue(
-        MeasurementStation station,
-        Double             value
-    ) {
+            final MeasurementStation station,
+            final Double             value
+            ) {
         this.station      = station;
         this.value        = value;
     }
 
-    protected SedimentLoadValue getPeer(SedimentLoad sedimentLoad) {
+    protected SedimentLoadValue getPeer(final SedimentLoad sedimentLoad, final StoreMode parentStoreMode) {
 
-        if (peer == null) {
-            Session session = ImporterSession.getInstance()
-                .getDatabaseSession();
-            Query query = session.createQuery(
-                "from SedimentLoadValue where " +
-                "   measurementStation = :station and " +
-                "   sedimentLoad = :sedimentLoad and " +
-                "   value = :value");
+        if (this.peer == null) {
+            final Session session = ImporterSession.getInstance()
+                    .getDatabaseSession();
+            List<SedimentLoadValue> values;
+            if (parentStoreMode == StoreMode.INSERT)
+                values = null;
+            else {
+                final Query query = session.createQuery(
+                        "from SedimentLoadValue where " +
+                                "   measurementStation = :station and " +
+                                "   sedimentLoad = :sedimentLoad and " +
+                        "   value = :value");
 
-            query.setParameter("station", station);
-            query.setParameter("sedimentLoad", sedimentLoad);
-            query.setParameter("value", value);
+                query.setParameter("station", this.station);
+                query.setParameter("sedimentLoad", sedimentLoad);
+                query.setParameter("value", this.value);
 
-            List<SedimentLoadValue> values = query.list();
-            if (values.isEmpty()) {
-                peer = new SedimentLoadValue(sedimentLoad, station, value);
-                session.save(peer);
+                values = query.list();
+            }
+            if ((values == null) || values.isEmpty()) {
+                this.peer = new SedimentLoadValue(sedimentLoad, this.station, this.value);
+                session.save(this.peer);
             }
             else {
-                peer = values.get(0);
+                this.peer = values.get(0);
             }
         }
 
-        return peer;
+        return this.peer;
     }
 
-    public void storeDependencies(SedimentLoad sedimentLoad) {
-        getPeer(sedimentLoad);
+    public void storeDependencies(final SedimentLoad sedimentLoad, final StoreMode parentStoreMode) {
+        getPeer(sedimentLoad, parentStoreMode);
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org