OSDN Git Service

Add connection throttling to try and reduce indexing & synchronizing impact.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / threads / IndexRunner.java
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