package roart.thread;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.HashSet;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import roart.queue.Queues;
import roart.util.Constants;
import roart.database.IndexFilesDao;

import com.vaadin.ui.UI;

public class DbRunner implements Runnable {
	
    private static Logger log = LoggerFactory.getLogger(DbRunner.class);

    public static final Set<UI> uiset = new HashSet<UI>();
	
    final int update = 300;
    static long lastupdate = 0;

    public static volatile boolean doupdate = true;

    public void run() {
    	Set<Future<Object>> set = new HashSet<Future<Object>>();
	int nThreads = 4;
    	ThreadPoolExecutor /*ExecutorService*/ executorService = (ThreadPoolExecutor) Executors.newFixedThreadPool(nThreads);
    	while (true) {
	    long now = System.currentTimeMillis();
	    if ((now - lastupdate) >= update * 1000) {
		try {
		    if (doupdate) {
			IndexFilesDao.commit();
		    }
		} catch (Exception e) {
		    log.error(Constants.EXCEPTION, e);
		}
		lastupdate = now;
	    }
	    if (true) {
		try {
		    TimeUnit.SECONDS.sleep(update);
		} catch (InterruptedException e) {
		    // TODO Auto-generated catch block
		    log.error(Constants.EXCEPTION, e);
		    ClientRunner.notify("Db exception");
		}
	    }

     	}
    }

}

@@ -989,10 +989,9 @@
 e = 
-30
+6
 0;%0A 
@@ -1022,24 +1022,26 @@
 e = 0;%0A%0A    
+//
 public stati
@@ -1105,17 +1105,20 @@
 ) %7B%0A    
-%09
+    
 Set%3CFutu
@@ -1166,17 +1166,24 @@
 ct%3E%3E();%0A
-%09
+        
 int nThr
@@ -1196,17 +1196,20 @@
  4;%0A    
-%09
+    
 ThreadPo
@@ -1322,17 +1322,20 @@
 s);%0A    
-%09
+    
 while (t
@@ -1341,17 +1341,24 @@
 true) %7B%0A
-%09
+        
     long
@@ -1392,25 +1392,111 @@
 llis();%0A
-%09    if (
+            log.info(%22updatetime %22 + (int) ((now - lastupdate)/1000));%0A            if (true %7C%7C 
 (now - l
@@ -1530,26 +1530,54 @@
 ) %7B%0A
-%09%09try %7B%0A%09%09
+                try %7B%0A                
     if (
 doup
@@ -1572,24 +1572,31 @@
     if (
+true /*
 doupdate
 ) %7B%0A%09%09%09I
@@ -1591,49 +1591,100 @@
 date
+*/
 ) %7B%0A
-%09%09%09IndexFilesDao.commit();%0A%09%09    %7D%0A%09%09
+                        IndexFilesDao.commit();%0A                    %7D%0A                
 %7D ca
@@ -1699,26 +1699,40 @@
 eption e) %7B%0A
-%09%09
+                
     log.erro
@@ -1762,66 +1762,338 @@
 e);%0A
-%09%09%7D%0A%09%09lastupdate = now;%0A%09    %7D%0A%09    if (true) %7B%0A%09%09try %7B%0A%09%09
+                %7D%0A                lastupdate = System.currentTimeMillis();%0A            %7D%0A            try %7B%0A                int sleepsec = 60 - (int) (((lastupdate - now)/1000));  %0A                if (sleepsec %3C 1) %7B%0A                    sleepsec = 1;%0A                %7D%0A                log.info(%22sleepsec %22 + sleepsec);%0A            
     
@@ -2119,19 +2119,31 @@
 eep(
-update);%0A%09%09
+sleepsec);%0A            
 %7D ca
@@ -2173,18 +2173,28 @@
 on e) %7B%0A
-%09%09
+            
     // T
@@ -2224,18 +2224,28 @@
 h block%0A
-%09%09
+            
     log.
@@ -2279,14 +2279,24 @@
 e);%0A
-%09%09
+          
     
+  
 Clie
@@ -2332,26 +2332,30 @@
 %22);%0A
-%09%09%7D%0A%09
+        
     %7D%0A
-%0A
+   
      
-%09
 %7D%0A  

change save algorithm and intervals