diff backend/src/main/java/org/dive4elements/river/importer/ImportPorosityValue.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/ImportPorosityValue.java	Fri Apr 06 14:13:14 2018 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/ImportPorosityValue.java	Sun Apr 08 18:07:06 2018 +0200
@@ -9,22 +9,20 @@
 package org.dive4elements.river.importer;
 
 import java.math.BigDecimal;
-
 import java.util.List;
 
 import org.apache.log4j.Logger;
-
-import org.hibernate.Session;
-import org.hibernate.Query;
-
+import org.dive4elements.river.importer.common.StoreMode;
 import org.dive4elements.river.model.Porosity;
 import org.dive4elements.river.model.PorosityValue;
+import org.hibernate.Query;
+import org.hibernate.Session;
 
 
 public class ImportPorosityValue {
 
     private static final Logger log =
-        Logger.getLogger(ImportPorosityValue.class);
+            Logger.getLogger(ImportPorosityValue.class);
 
 
     protected PorosityValue peer;
@@ -39,11 +37,11 @@
 
 
     public ImportPorosityValue(
-        BigDecimal station,
-        BigDecimal shoreOffset,
-        BigDecimal porosity,
-        String     description
-    ) {
+            final BigDecimal station,
+            final BigDecimal shoreOffset,
+            final BigDecimal porosity,
+            final String     description
+            ) {
         this.station     = station;
         this.shoreOffset = shoreOffset;
         this.porosity    = porosity;
@@ -51,53 +49,57 @@
     }
 
 
-    public void storeDependencies(Porosity porosity) {
-        log.info("store dependencies");
+    public void storeDependencies(final Porosity porosity, final StoreMode parentStoreMode) {
+        // log.info("store dependencies");
 
-        getPeer(porosity);
+        getPeer(porosity, parentStoreMode);
     }
 
 
-    public PorosityValue getPeer(Porosity porosity) {
+    public PorosityValue getPeer(final Porosity porosity, final StoreMode parentStoreMode) {
         log.info("get peer");
 
-        if (peer == null) {
-            Session session = ImporterSession.getInstance()
-                .getDatabaseSession();
-
-            Query query = session.createQuery(
-                "from PorosityValue "
-                + "where porosity=:porosity "
-                + "and station between :station - 0.0001f "
-                + "    and :station + 0.0001f "
-                + "and porosityValue between :poros -0.0001f "
-                + "    and :poros + 0.0001f "
-                + "and description=:description");
+        if (this.peer == null) {
+            final Session session = ImporterSession.getInstance()
+                    .getDatabaseSession();
+            List<PorosityValue> values;
+            if (parentStoreMode == StoreMode.INSERT)
+                values = null;
+            else {
+                final Query query = session.createQuery(
+                        "from PorosityValue "
+                                + "where porosity=:porosity "
+                                + "and station between :station - 0.0001f "
+                                + "    and :station + 0.0001f "
+                                + "and porosityValue between :poros -0.0001f "
+                                + "    and :poros + 0.0001f "
+                                + "and description=:description");
 
-            query.setParameter("porosity", porosity);
-            query.setParameter("station", station.floatValue());
-            query.setParameter("poros", this.porosity.floatValue());
-            query.setParameter("description", description);
+                query.setParameter("porosity", porosity);
+                query.setParameter("station", this.station.floatValue());
+                query.setParameter("poros", this.porosity.floatValue());
+                query.setParameter("description", this.description);
 
-            List<PorosityValue> values = query.list();
-            if (values.isEmpty()) {
-                log.debug("Create new PorosityValue DB instance.");
+                values = query.list();
+            }
+            if ((values == null) || values.isEmpty()) {
+                // log.debug("Create new PorosityValue DB instance.");
 
-                peer = new PorosityValue(
-                    porosity,
-                    station,
-                    shoreOffset,
-                    this.porosity,
-                    description);
+                this.peer = new PorosityValue(
+                        porosity,
+                        this.station,
+                        this.shoreOffset,
+                        this.porosity,
+                        this.description);
 
-                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