[chronojump] on save curves All/Best/None, sqlite remains opened to not crash if many connections are opened/clos



commit 8c0af6262fbdac08e1d626fb07c55ff6e8729ab1
Author: Xavier de Blas <xaviblas gmail com>
Date:   Tue Mar 10 00:18:48 2015 +0100

    on save curves All/Best/None, sqlite remains opened to not crash if many connections are opened/closed

 src/gui/encoder.cs          |   38 +++++++++++++++++++-------------------
 src/gui/encoderTreeviews.cs |   23 +++++++++++++++--------
 2 files changed, 34 insertions(+), 27 deletions(-)
---
diff --git a/src/gui/encoder.cs b/src/gui/encoder.cs
index 5013389..c717345 100644
--- a/src/gui/encoder.cs
+++ b/src/gui/encoder.cs
@@ -998,7 +998,7 @@ public partial class ChronoJumpWindow
                }
 
                genericWin.ShowEditRow(false);
-               updateUserCurvesLabelsAndCombo();
+               updateUserCurvesLabelsAndCombo(false);
        }
        
        protected void on_encoder_show_curves_row_delete_pre (object o, EventArgs args) {
@@ -1015,24 +1015,24 @@ public partial class ChronoJumpWindow
 
                int uniqueID = genericWin.TreeviewSelectedUniqueID;
 
-               delete_encoder_curve(uniqueID);
+               delete_encoder_curve(false, uniqueID);
 
                genericWin.Delete_row_accepted();
        }
 
-       void delete_encoder_curve(int uniqueID) {
+       void delete_encoder_curve(bool dbconOpened, int uniqueID) {
                LogB.Information(uniqueID.ToString());
 
-               EncoderSQL eSQL = (EncoderSQL) SqliteEncoder.Select(false, uniqueID, 0, 0, -1, "", 
EncoderSQL.Eccons.ALL, false, true)[0];
+               EncoderSQL eSQL = (EncoderSQL) SqliteEncoder.Select(dbconOpened, uniqueID, 0, 0, -1, "", 
EncoderSQL.Eccons.ALL, false, true)[0];
                //remove the file
                bool deletedOk = Util.FileDelete(eSQL.GetFullURL(false));       //don't convertPathToR
                if(deletedOk)  {
-                       Sqlite.Delete(false, Constants.EncoderTable, Convert.ToInt32(uniqueID));
+                       Sqlite.Delete(dbconOpened, Constants.EncoderTable, Convert.ToInt32(uniqueID));
                        
-                       ArrayList escArray = SqliteEncoder.SelectSignalCurve(false, 
+                       ArrayList escArray = SqliteEncoder.SelectSignalCurve(dbconOpened, 
                                                -1, Convert.ToInt32(uniqueID),  //signal, curve
                                                -1, -1);                        //msStart, msEnd
-                       SqliteEncoder.DeleteSignalCurveWithCurveID(false, 
+                       SqliteEncoder.DeleteSignalCurveWithCurveID(dbconOpened, 
                                        Convert.ToInt32(eSQL.uniqueID)); //delete by curveID on SignalCurve 
table
                        //if deleted curve is from current signal, uncheck it in encoderCaptureCurves
                        if(escArray.Count > 0) {
@@ -1041,7 +1041,7 @@ public partial class ChronoJumpWindow
                                        encoderCaptureSelectBySavedCurves(esc.msCentral, false);
                        }
 
-                       updateUserCurvesLabelsAndCombo();
+                       updateUserCurvesLabelsAndCombo(dbconOpened);
                }
        }
        
@@ -1682,10 +1682,10 @@ public partial class ChronoJumpWindow
                return countActiveCurves;
        }
 
-       private void updateUserCurvesLabelsAndCombo() 
+       private void updateUserCurvesLabelsAndCombo(bool dbconOpened) 
        {
                ArrayList data = SqliteEncoder.Select(
-                               false, -1, currentPerson.UniqueID, currentSession.UniqueID, -1, 
+                               dbconOpened, -1, currentPerson.UniqueID, currentSession.UniqueID, -1, 
                                "curve", EncoderSQL.Eccons.ALL, 
                                false, true);
                int activeCurvesNum = getActiveCurvesNum(data);
@@ -1750,7 +1750,7 @@ public partial class ChronoJumpWindow
        }
 
 
-       string encoderSaveSignalOrCurve (string mode, int selectedID) 
+       string encoderSaveSignalOrCurve (bool dbconOpened, string mode, int selectedID) 
        {
                //mode is different than type. 
                //mode can be curve or signal
@@ -1829,11 +1829,11 @@ public partial class ChronoJumpWindow
                        LogB.Information(curveStart + "->" + duration);
                
                        int curveIDMax;
-                       int countCurveIDs = Sqlite.Count(Constants.EncoderTable, false);
+                       int countCurveIDs = Sqlite.Count(Constants.EncoderTable, dbconOpened);
                        if(countCurveIDs == 0)
                                curveIDMax = 0;
                        else
-                               curveIDMax = Sqlite.Max(Constants.EncoderTable, "uniqueID", false);
+                               curveIDMax = Sqlite.Max(Constants.EncoderTable, "uniqueID", dbconOpened);
                        
                        //save raw file to hard disk
                        fileSaved = UtilEncoder.EncoderSaveCurve(UtilEncoder.GetEncoderDataTempFileName(), 
@@ -1843,7 +1843,7 @@ public partial class ChronoJumpWindow
                                        currentPerson.Name, encoderTimeStamp, curveIDMax);
 
                        //save it to SQL (encoderSignalCurve table)
-                       SqliteEncoder.SignalCurveInsert(false, 
+                       SqliteEncoder.SignalCurveInsert(dbconOpened, 
                                        Convert.ToInt32(encoderSignalUniqueID), curveIDMax +1,
                                        Convert.ToInt32(curveStart + (duration /2)));
 
@@ -1883,7 +1883,7 @@ public partial class ChronoJumpWindow
                //we know it because encoderUniqueID is != than "-1" if we loaded something from database
                //This also saves curves
                if(myID == "-1") {
-                       myID = SqliteEncoder.Insert(false, eSQL).ToString(); //Adding on SQL
+                       myID = SqliteEncoder.Insert(dbconOpened, eSQL).ToString(); //Adding on SQL
                        if(mode == "signal") {
                                encoderSignalUniqueID = myID;
                                feedback = Catalog.GetString("Set saved");
@@ -1899,7 +1899,7 @@ public partial class ChronoJumpWindow
                                                                Convert.ToInt32(encoderSignalUniqueID));
                                                //need assign uniqueID to update and add the URL of video
                                                eSQL.uniqueID = encoderSignalUniqueID;
-                                               SqliteEncoder.Update(false, eSQL);
+                                               SqliteEncoder.Update(dbconOpened, eSQL);
                                        
                                                //notebook_video_encoder.CurrentPage = 1;
                                                radiobutton_video_encoder_play.Active  = true;
@@ -1916,7 +1916,7 @@ public partial class ChronoJumpWindow
                        LogB.Warning("TOSTRING1");
                        eSQL.ToString();
                        //only signal is updated
-                       SqliteEncoder.Update(false, eSQL); //Adding on SQL
+                       SqliteEncoder.Update(dbconOpened, eSQL); //Adding on SQL
                        LogB.Warning("TOSTRING2");
                        eSQL.ToString();
                        feedback = Catalog.GetString("Set updated");
@@ -5438,7 +5438,7 @@ LogB.Debug("D");
                                                        preferences.encoderAutoSaveCurve == 
Constants.EncoderAutoSaveCurve.BESTMEANPOWER) )
                                                needToAutoSaveCurve = true;
 
-                                       encoder_pulsebar_capture.Text = encoderSaveSignalOrCurve("signal", 
0); //this updates encoderSignalUniqueID
+                                       encoder_pulsebar_capture.Text = encoderSaveSignalOrCurve(false, 
"signal", 0); //this updates encoderSignalUniqueID
 
                                        if(needToAutoSaveCurve)
                                                
encoderCaptureSaveCurvesAllNoneBest(preferences.encoderAutoSaveCurve);
@@ -5488,7 +5488,7 @@ LogB.Debug("D");
                                                                }
                                        }
                                        if(deletedUserCurves)
-                                               updateUserCurvesLabelsAndCombo();               // (5)
+                                               updateUserCurvesLabelsAndCombo(false);          // (5)
 
 
                                        findAndMarkSavedCurves();
diff --git a/src/gui/encoderTreeviews.cs b/src/gui/encoderTreeviews.cs
index 1ebe588..2ac80e1 100644
--- a/src/gui/encoderTreeviews.cs
+++ b/src/gui/encoderTreeviews.cs
@@ -185,11 +185,11 @@ public partial class ChronoJumpWindow
        }
        
        //rowNum starts at zero
-       void saveOrDeleteCurveFromCaptureTreeView(int rowNum, EncoderCurve curve, bool save) 
+       void saveOrDeleteCurveFromCaptureTreeView(bool dbconOpened, int rowNum, EncoderCurve curve, bool 
save) 
        {
                LogB.Information("saving? " + save.ToString() + "; rownum:" + rowNum.ToString());
                if(save)
-                       encoderSaveSignalOrCurve("curve", rowNum +1);
+                       encoderSaveSignalOrCurve(dbconOpened, "curve", rowNum +1);
                else {
                        double msStart = Convert.ToDouble(curve.Start);
                        double msEnd = -1;
@@ -203,11 +203,11 @@ public partial class ChronoJumpWindow
                                        Convert.ToDouble(curveNext.Duration);
                        }
 
-                       ArrayList signalCurves = SqliteEncoder.SelectSignalCurve(false,
+                       ArrayList signalCurves = SqliteEncoder.SelectSignalCurve(dbconOpened,
                                        Convert.ToInt32(encoderSignalUniqueID), -1, 
                                        msStart, msEnd);
                        foreach(EncoderSignalCurve esc in signalCurves)
-                               delete_encoder_curve(esc.curveID);
+                               delete_encoder_curve(dbconOpened, esc.curveID);
                }
        }
 
@@ -244,7 +244,7 @@ public partial class ChronoJumpWindow
                        //this makes RenderRecord work on changed row without having to put mouse there
                        encoderCaptureListStore.EmitRowChanged(path,iter);
 
-                       saveOrDeleteCurveFromCaptureTreeView(rowNum, curve, ! val);
+                       saveOrDeleteCurveFromCaptureTreeView(false, rowNum, curve, ! val);
                        
                        /* temporarily removed message
                         *
@@ -272,7 +272,7 @@ public partial class ChronoJumpWindow
                                encoderCaptureListStore.EmitRowChanged(path,iter);
                        }
                        
-                       updateUserCurvesLabelsAndCombo();
+                       updateUserCurvesLabelsAndCombo(false);
 
                        callPlotCurvesGraphDoPlot();
                }
@@ -306,6 +306,10 @@ public partial class ChronoJumpWindow
                if(! iterOk)
                        return;
 
+               //need to open Sqlite because if more than 50 curves are saved/deleted, it will crash if 
open/close connnections all the time
+               //TODO: do as a transaction, but code need to be refactored
+               Sqlite.Open();
+
                bool changeTo;
                while(iterOk) {
                        TreePath path = encoderCaptureListStore.GetPath(iter);
@@ -326,7 +330,7 @@ public partial class ChronoJumpWindow
                                encoderCaptureListStore.EmitRowChanged(path,iter);
 
                                //on "ecS" don't pass the 2nd row, pass always the first
-                               saveOrDeleteCurveFromCaptureTreeView(i, curve, changeTo);
+                               saveOrDeleteCurveFromCaptureTreeView(true, i, curve, changeTo);
                                
                                if(ecconLast != "c") {
                                        path.Next();
@@ -357,6 +361,7 @@ public partial class ChronoJumpWindow
 
                        iterOk = encoderCaptureListStore.IterNext (ref iter);
                }
+               
                //combo_encoder_capture_show_save_curve_button();
                
                /* temporarily removed message
@@ -370,7 +375,9 @@ public partial class ChronoJumpWindow
                */
 
                        
-               updateUserCurvesLabelsAndCombo();
+               updateUserCurvesLabelsAndCombo(true);
+               
+               Sqlite.Close();
                        
                callPlotCurvesGraphDoPlot();
        }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]