OSDN Git Service

Add connection throttling to try and reduce indexing & synchronizing impact.
authorRandy Baumgarte <randy@fbn.cx>
Tue, 18 Jan 2011 14:36:48 +0000 (09:36 -0500)
committerRandy Baumgarte <randy@fbn.cx>
Sun, 30 Jan 2011 13:36:55 +0000 (08:36 -0500)
src/cx/fbn/nevernote/NeverNote.java
src/cx/fbn/nevernote/sql/DatabaseConnection.java
src/cx/fbn/nevernote/threads/CounterRunner.java
src/cx/fbn/nevernote/threads/IndexRunner.java
src/cx/fbn/nevernote/threads/SaveRunner.java
src/cx/fbn/nevernote/threads/SyncRunner.java
src/cx/fbn/nevernote/threads/ThumbnailRunner.java

index a594d60..0eb192a 100644 (file)
@@ -774,7 +774,7 @@ public class NeverNote extends QMainWindow{
                         Global.getDatabaseUserPassword(), Global.cipherPassword);
             }
         }
-               DatabaseConnection dbConn = new DatabaseConnection(logger,Global.getDatabaseUrl(), Global.getDatabaseUserid(), Global.getDatabaseUserPassword(), Global.cipherPassword);
+               DatabaseConnection dbConn = new DatabaseConnection(logger,Global.getDatabaseUrl(), Global.getDatabaseUserid(), Global.getDatabaseUserPassword(), Global.cipherPassword, 0);
        return dbConn;
     }
     
index 25b5d3e..993f743 100644 (file)
@@ -47,11 +47,13 @@ public class DatabaseConnection {
        private SystemIconTable                         systemIconTable;
        private final ApplicationLogger         logger;
        private Connection                                      conn;
+       int throttle=0;
        int id;
 
        
-       public DatabaseConnection(ApplicationLogger l, String url, String userid, String password, String cypherPassword) {
+       public DatabaseConnection(ApplicationLogger l, String url, String userid, String password, String cypherPassword, int throttle) {
                logger = l;
+               this.throttle = throttle;
                dbSetup(url, userid, password, cypherPassword);
        }
        
@@ -107,7 +109,10 @@ public class DatabaseConnection {
                                passwordString = cypherPassword+" "+userPassword;
 //                     conn = DriverManager.getConnection(url,userid,passwordString);
 //                     conn = DriverManager.getConnection(url+";CACHE_SIZE=4096",userid,passwordString);
-                       conn = DriverManager.getConnection(url+";CACHE_SIZE="+Global.databaseCache,userid,passwordString);
+                       if (throttle == 0)
+                               conn = DriverManager.getConnection(url+";CACHE_SIZE="+Global.databaseCache,userid,passwordString);
+                       else
+                               conn = DriverManager.getConnection(url+";THROTTLE=" +new Integer(throttle).toString()+";CACHE_SIZE="+Global.databaseCache,userid,passwordString);
 //                     conn = DriverManager.getConnection(url+";AUTO_SERVER=TRUE",userid,passwordString);
                } catch (SQLException e) {
                        e.printStackTrace();
index 6e1b81c..3062256 100644 (file)
@@ -77,7 +77,7 @@ public class CounterRunner extends QObject implements Runnable {
                threadLock = new QMutex();\r
                logger = new ApplicationLogger(logname);\r
 //             setAutoDelete(false);   \r
-               conn = new DatabaseConnection(logger, u, uid, pswd, cpswd);\r
+               conn = new DatabaseConnection(logger, u, uid, pswd, cpswd, 300);\r
                keepRunning = true;\r
                notebookSignal = new NotebookSignal();\r
                tagSignal = new TagSignal();\r
index f9ff768..4ea69c5 100644 (file)
@@ -27,6 +27,7 @@ import java.io.InputStream;
 import java.util.List;\r
 import java.util.TreeSet;\r
 import java.util.concurrent.LinkedBlockingQueue;\r
+import java.util.concurrent.locks.LockSupport;\r
 \r
 import org.apache.commons.lang.StringEscapeUtils;\r
 import org.apache.tika.exception.TikaException;\r
@@ -86,7 +87,7 @@ public class IndexRunner extends QObject implements Runnable {
        public IndexRunner(String logname, String u, String uid, String pswd, String cpswd) {\r
                foundWords = new TreeSet<String>();\r
                logger = new ApplicationLogger(logname);\r
-               conn = new DatabaseConnection(logger, u, uid, pswd, cpswd);\r
+               conn = new DatabaseConnection(logger, u, uid, pswd, cpswd, 300);\r
                indexType = SCAN;\r
                guid = null;\r
                keepRunning = true;\r
@@ -109,7 +110,7 @@ public class IndexRunner extends QObject implements Runnable {
                while (keepRunning) {\r
                        idle=true;\r
                        try {\r
-                               //waitSeconds(1);\r
+                               waitSeconds(1);\r
                                String work = workQueue.take();\r
                                idle=false;\r
                                if (work.startsWith("SCAN")) {\r
@@ -570,7 +571,7 @@ public class IndexRunner extends QObject implements Runnable {
                for (int i=0; i<notes.size() && !interrupt && keepRunning; i++) {\r
                        guid = notes.get(i);\r
                        if (guid != null && keepRunning) {\r
-                               //waitSeconds(1);\r
+                               waitSeconds(1);\r
                                indexNoteContent();\r
                        }\r
                }\r
@@ -583,7 +584,7 @@ public class IndexRunner extends QObject implements Runnable {
                for (int i=0; i<unindexedResources.size()&& !interrupt && keepRunning; i++) {\r
                        guid = unindexedResources.get(i);\r
                        if (keepRunning) {\r
-                               //waitSeconds(1);\r
+                               waitSeconds(1);\r
                                indexResource();\r
                        }\r
                }\r
@@ -602,13 +603,16 @@ public class IndexRunner extends QObject implements Runnable {
                conn.getNoteTable().noteResourceTable.reindexAll(); \r
        }\r
 \r
-//     private void waitSeconds(int len) {\r
-//             QDateTime currentdate = new QDateTime(QDateTime.currentDateTime());\r
-//             QDateTime futuredate = new QDateTime(QDateTime.currentDateTime());\r
-//             \r
-//             while (keepRunning && (futuredate.toTime_t() - currentdate.toTime_t() >=len) ) {\r
-//                     Thread.yield();\r
-//                     futuredate = new QDateTime(QDateTime.currentDateTime());\r
-//             }\r
-//     }\r
+       private void waitSeconds(int len) {\r
+               long starttime = 0; // variable declared\r
+               //...\r
+               // for the first time, remember the timestamp\r
+           starttime = System.currentTimeMillis();\r
+               // the next timestamp we want to wake up\r
+               starttime += (1000.0);\r
+               // Wait until the desired next time arrives using nanosecond\r
+               // accuracy timer (wait(time) isn't accurate enough on most platforms) \r
+               LockSupport.parkNanos((Math.max(0, \r
+                   starttime - System.currentTimeMillis()) * 1000000));\r
+       }\r
 }\r
index 4569e04..a93fd17 100644 (file)
@@ -51,7 +51,7 @@ public class SaveRunner extends QObject implements Runnable {
        //*********************************************\r
        public SaveRunner(String logname, String u, String uid, String pswd, String cpswd) {\r
                logger = new ApplicationLogger(logname);\r
-               conn = new DatabaseConnection(logger, u, uid, pswd, cpswd);\r
+               conn = new DatabaseConnection(logger, u, uid, pswd, cpswd, 0);\r
                threadLock = new QMutex();\r
                keepRunning = true;\r
                noteSignals = new NoteSignal();\r
index e46270c..8ad6119 100644 (file)
@@ -182,7 +182,7 @@ public class SyncRunner extends QObject implements Runnable {
        public void run() {\r
                try {\r
                        logger.log(logger.EXTREME, "Starting thread");\r
-                       conn = new DatabaseConnection(logger, dburl, dbuid, dbpswd, dbcpswd);\r
+                       conn = new DatabaseConnection(logger, dburl, dbuid, dbpswd, dbcpswd, 200);\r
                        while(keepRunning) {\r
                                String work = workQueue.take();\r
                                logger.log(logger.EXTREME, "Work found: " +work);\r
index fccbeb5..024ce6f 100644 (file)
@@ -81,7 +81,7 @@ public class ThumbnailRunner extends QObject implements Runnable {
 \r
        public ThumbnailRunner(String logname, String u, String uid, String pswd, String cpswd) {\r
                logger = new ApplicationLogger(logname);\r
-               conn = new DatabaseConnection(logger, u, uid, pswd, cpswd);\r
+               conn = new DatabaseConnection(logger, u, uid, pswd, cpswd, 300);\r
                noteSignal = new NoteSignal();\r
                guid = null;\r
                keepRunning = true;\r