Author: batosai
Date: 2008-08-16 16:22:23 +0000 (Sat, 16 Aug 2008)
New Revision: 21933

Modified:
   trunk/apps/WoT/src/plugins/WoT/IdentityInserter.java
   trunk/apps/WoT/src/plugins/WoT/WoTplugin.java
Log:
Made identityInserter a thread. OwnIdentities that have been updated are 
inserted every 30 minutes. That will limit the number of editions.

Modified: trunk/apps/WoT/src/plugins/WoT/IdentityInserter.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/IdentityInserter.java        2008-08-16 
14:28:10 UTC (rev 21932)
+++ trunk/apps/WoT/src/plugins/WoT/IdentityInserter.java        2008-08-16 
16:22:23 UTC (rev 21933)
@@ -16,6 +16,7 @@
 import javax.xml.transform.TransformerException;

 import com.db4o.ObjectContainer;
+import com.db4o.ObjectSet;
 import com.db4o.ext.DatabaseClosedException;
 import com.db4o.ext.Db4oIOException;

@@ -35,19 +36,56 @@
  * @author Julien Cornuwel (batosai at freenetproject.org)
  *
  */
-public class IdentityInserter implements ClientCallback {
+public class IdentityInserter implements ClientCallback, Runnable {

+       WoT wot;
        ObjectContainer db;
        HighLevelSimpleClient client;
        OwnIdentity identity;
        String TEMP_DIR = ".";

-       public IdentityInserter(ObjectContainer db, HighLevelSimpleClient 
client) {
+       boolean isRunning;

+       public IdentityInserter(WoT wot, ObjectContainer db, 
HighLevelSimpleClient client) {
+       
+               this.wot = wot;
                this.db = db;
                this.client = client;
+               isRunning = true;
        }

+       @Override
+       public void run() {
+               try{
+                       Thread.sleep(30 * 1000); // Let the node start up (30 
seconds)
+               } catch (InterruptedException e){}
+               while(isRunning) {
+                       ObjectSet<OwnIdentity> identities = 
wot.getOwnIdentities();
+                       while(identities.hasNext()) {
+                               OwnIdentity identity = identities.next();
+                               if(identity.needsInsert()) {
+                                       try {
+                                               insert(identity);
+                                               // We set the date now, so if 
the identity is modified during the insert, we'll insert it again next time
+                                               identity.setLastInsert(new 
Date()); 
+                                               db.store(identity);
+                                       } catch (Exception e) {
+                                               // TODO Log this properly
+                                               e.printStackTrace();
+                                       }
+                               }
+                       }
+                       db.commit();
+                       try{
+                               Thread.sleep(30 * 60 * 1000); // 30 minutes
+                       } catch (InterruptedException e){}
+               }
+       }
+       
+       public void stop() {
+               isRunning = false;
+       }
+       
        public void insert(OwnIdentity identity) throws 
TransformerConfigurationException, FileNotFoundException, 
ParserConfigurationException, TransformerException, IOException, 
InsertException, Db4oIOException, DatabaseClosedException, 
InvalidParameterException {

                this.identity = identity;
@@ -106,4 +144,5 @@
                db.store(identity);
                db.commit();
        }
+
 }

Modified: trunk/apps/WoT/src/plugins/WoT/WoTplugin.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/WoTplugin.java       2008-08-16 14:28:10 UTC 
(rev 21932)
+++ trunk/apps/WoT/src/plugins/WoT/WoTplugin.java       2008-08-16 16:22:23 UTC 
(rev 21933)
@@ -50,6 +50,7 @@
        private ObjectContainer db;
        private WoT wot;
        private WebInterface web;
+       private IdentityInserter inserter;

        public static String SELF_URI = "/plugins/plugins.WoT.WoTplugin";

@@ -63,9 +64,13 @@
                client = pr.getHLSimpleClient();

                web = new WebInterface(pr, db, client, SELF_URI, wot);
+               
+               inserter = new IdentityInserter(wot, db, client);
+               pr.getNode().executor.execute(inserter, "WoTinserter");
        }

        public void terminate() {
+               inserter.stop();
                db.commit();
                db.close();
        }
@@ -203,7 +208,6 @@

                db.commit();    

-               new IdentityInserter(db, client).insert(identity);      
                return identity;
        }

@@ -246,7 +250,7 @@


        public String getVersion() {
-               return "0.0.1 r"+Version.getSvnRevision();
+               return "0.0.2 r"+Version.getSvnRevision();
        }

        @Override


Reply via email to