diff backend/src/main/java/org/dive4elements/river/importer/ImportFlowVelocityModelValue.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/ImportFlowVelocityModelValue.java	Fri Apr 06 14:13:14 2018 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/ImportFlowVelocityModelValue.java	Sun Apr 08 18:07:06 2018 +0200
@@ -9,34 +9,33 @@
 package org.dive4elements.river.importer;
 
 import java.math.BigDecimal;
-
 import java.util.List;
 
-import org.hibernate.Session;
-import org.hibernate.Query;
-
+import org.dive4elements.river.importer.common.StoreMode;
 import org.dive4elements.river.model.FlowVelocityModel;
 import org.dive4elements.river.model.FlowVelocityModelValue;
+import org.hibernate.Query;
+import org.hibernate.Session;
 
 
 public class ImportFlowVelocityModelValue {
 
-    private BigDecimal station;
-    private BigDecimal q;
-    private BigDecimal totalChannel;
-    private BigDecimal mainChannel;
-    private BigDecimal shearStress;
+    private final BigDecimal station;
+    private final BigDecimal q;
+    private final BigDecimal totalChannel;
+    private final BigDecimal mainChannel;
+    private final BigDecimal shearStress;
 
     private FlowVelocityModelValue peer;
 
 
     public ImportFlowVelocityModelValue(
-        BigDecimal station,
-        BigDecimal q,
-        BigDecimal totalChannel,
-        BigDecimal mainChannel,
-        BigDecimal shearStress
-    ) {
+            final BigDecimal station,
+            final BigDecimal q,
+            final BigDecimal totalChannel,
+            final BigDecimal mainChannel,
+            final BigDecimal shearStress
+            ) {
         this.station      = station;
         this.q            = q;
         this.totalChannel = totalChannel;
@@ -45,39 +44,41 @@
     }
 
 
-    public void storeDependencies(FlowVelocityModel model) {
-        getPeer(model);
+    public void storeDependencies(final FlowVelocityModel model, final StoreMode parentStoreMode) {
+        getPeer(model, parentStoreMode);
     }
 
 
-    public FlowVelocityModelValue getPeer(FlowVelocityModel model) {
-        if (peer == null) {
-            Session session = ImporterSession.getInstance()
-                .getDatabaseSession();
+    public FlowVelocityModelValue getPeer(final FlowVelocityModel model, final StoreMode parentStoreMode) {
+        if (this.peer == null) {
+            List<FlowVelocityModelValue> values;
+            final Session session = ImporterSession.getInstance().getDatabaseSession();
+            if (parentStoreMode == StoreMode.INSERT)
+                values = null;
+            else {
+                final Query query = session.createQuery(
+                        "from FlowVelocityModelValue where " +
+                                "   flowVelocity=:model and " +
+                                "   station between :station - 0.00001 and :station + 0.00001"
+                        );
 
-            Query query = session.createQuery(
-                "from FlowVelocityModelValue where " +
-                "   flowVelocity=:model and " +
-                "   station between :station - 0.00001 and :station + 0.00001"
-            );
+                query.setParameter("model", model);
+                query.setParameter("station", this.station.doubleValue());
 
-            query.setParameter("model", model);
-            query.setParameter("station", station.doubleValue());
-
-            List<FlowVelocityModelValue> values = query.list();
+                values = query.list();
+            }
+            if ((values == null) || values.isEmpty()) {
+                this.peer = new FlowVelocityModelValue(
+                        model, this.station, this.q, this.totalChannel, this.mainChannel, this.shearStress);
 
-            if (values.isEmpty()) {
-                peer = new FlowVelocityModelValue(
-                    model, station, q, totalChannel, mainChannel, shearStress);
-
-                session.save(peer);
+                session.save(this.peer);
             }
             else {
-                peer = values.get(0);
+                this.peer = values.get(0);
             }
         }
 
-        return peer;
+        return this.peer;
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org