diff backend/src/main/java/org/dive4elements/river/importer/ImportCrossSection.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 9344aa0fb021
children
line wrap: on
line diff
--- a/backend/src/main/java/org/dive4elements/river/importer/ImportCrossSection.java	Fri Apr 06 14:13:14 2018 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/ImportCrossSection.java	Sun Apr 08 18:07:06 2018 +0200
@@ -8,16 +8,15 @@
 
 package org.dive4elements.river.importer;
 
-import org.dive4elements.river.model.River;
-import org.dive4elements.river.model.CrossSection;
-import org.dive4elements.river.model.TimeInterval;
-
-import org.hibernate.Session;
-import org.hibernate.Query;
-
 import java.util.List;
 
 import org.apache.log4j.Logger;
+import org.dive4elements.river.importer.common.StoreMode;
+import org.dive4elements.river.model.CrossSection;
+import org.dive4elements.river.model.River;
+import org.dive4elements.river.model.TimeInterval;
+import org.hibernate.Query;
+import org.hibernate.Session;
 
 /** CrossSection to be imported, holds list of ImportCrossSectionLines. */
 public class ImportCrossSection
@@ -28,6 +27,7 @@
     protected String                       description;
     protected ImportTimeInterval           timeInterval;
     protected List<ImportCrossSectionLine> lines;
+    protected StoreMode storeMode;
 
     protected CrossSection peer;
 
@@ -35,94 +35,97 @@
     }
 
     public ImportCrossSection(
-        ImportRiver                  river,
-        String                       description,
-        ImportTimeInterval           timeInterval,
-        List<ImportCrossSectionLine> lines
-    ) {
+            final ImportRiver                  river,
+            final String                       description,
+            final ImportTimeInterval           timeInterval,
+            final List<ImportCrossSectionLine> lines
+            ) {
         this.river        = river;
         this.description  = description;
         this.timeInterval = timeInterval;
         this.lines        = lines;
+        this.storeMode = StoreMode.NONE;
         wireWithLines();
     }
 
     public void wireWithLines() {
-        for (ImportCrossSectionLine line: lines) {
+        for (final ImportCrossSectionLine line: this.lines) {
             line.setCrossSection(this);
         }
     }
 
     public ImportRiver getRiver() {
-        return river;
+        return this.river;
     }
 
-    public void setRiver(ImportRiver river) {
+    public void setRiver(final ImportRiver river) {
         this.river = river;
     }
 
     public String getDescription() {
-        return description;
+        return this.description;
     }
 
-    public void setDescription(String description) {
+    public void setDescription(final String description) {
         this.description = description;
     }
 
     public ImportTimeInterval getTimeInterval() {
-        return timeInterval;
+        return this.timeInterval;
     }
 
-    public void setTimeInterval(ImportTimeInterval timeInterval) {
+    public void setTimeInterval(final ImportTimeInterval timeInterval) {
         this.timeInterval = timeInterval;
     }
 
     public void storeDependencies() {
 
-        log.info("store cross section '" + description + "'");
+        log.info("store cross section '" + this.description + "'");
 
         getPeer();
 
-        int i = 1, N = lines.size();
+        // int i = 1;
+        // final int N = this.lines.size();
 
-        for (ImportCrossSectionLine line: lines) {
+        for (final ImportCrossSectionLine line: this.lines) {
             line.storeDependencies();
-            log.info("  stored " + i + " lines. remaining: " + (N-i));
-            ++i;
+            // log.info(" stored " + i + " lines. remaining: " + (N-i));
+            // ++i;
         }
+        log.info(" lines stored: " + this.lines.size());
     }
 
     public CrossSection getPeer() {
 
-        if (peer == null) {
-            River r = river.getPeer();
-            TimeInterval t = timeInterval != null
-                ? timeInterval.getPeer()
-                : null;
+        if (this.peer == null) {
+            final River r = this.river.getPeer();
+            final TimeInterval t = (this.timeInterval != null) ? this.timeInterval.getPeer() : null;
 
-            Session session =
-                ImporterSession.getInstance().getDatabaseSession();
+            final Session session =
+                    ImporterSession.getInstance().getDatabaseSession();
 
-            Query query = session.createQuery(
-                "from CrossSection where " +
-                "river=:r and "            +
-                "timeInterval=:t and "     +
-                "description=:d");
+            final Query query = session.createQuery(
+                    "from CrossSection where " +
+                            "river=:r and "            +
+                            "timeInterval=:t and "     +
+                    "description=:d");
 
             query.setParameter("r", r);
             query.setParameter("t", t);
-            query.setParameter("d", description);
+            query.setParameter("d", this.description);
 
-            List<CrossSection> crossSections = query.list();
+            final List<CrossSection> crossSections = query.list();
             if (crossSections.isEmpty()) {
-                peer = new CrossSection(r, t, description);
-                session.save(peer);
+                this.peer = new CrossSection(r, t, this.description);
+                session.save(this.peer);
+                this.storeMode = StoreMode.INSERT;
             }
             else {
-                peer = crossSections.get(0);
+                this.peer = crossSections.get(0);
+                this.storeMode = StoreMode.UPDATE;
             }
         }
-        return peer;
+        return this.peer;
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org