Author: jflesch
Date: 2006-10-29 22:08:27 +0000 (Sun, 29 Oct 2006)
New Revision: 10743

Modified:
   trunk/apps/Thaw/src/thaw/core/ConfigWindow.java
   trunk/apps/Thaw/src/thaw/core/Main.java
   trunk/apps/Thaw/src/thaw/plugins/Hsqldb.java
   trunk/apps/Thaw/src/thaw/plugins/index/File.java
   trunk/apps/Thaw/src/thaw/plugins/index/Index.java
   trunk/apps/Thaw/src/thaw/plugins/index/Link.java
Log:
Add lock on index update mechanism

Modified: trunk/apps/Thaw/src/thaw/core/ConfigWindow.java
===================================================================
--- trunk/apps/Thaw/src/thaw/core/ConfigWindow.java     2006-10-29 21:37:32 UTC 
(rev 10742)
+++ trunk/apps/Thaw/src/thaw/core/ConfigWindow.java     2006-10-29 22:08:27 UTC 
(rev 10743)
@@ -211,7 +211,7 @@
        }

        public void windowDeactivated(WindowEvent e) {
-               // C'est pas comme si on en avait quelque chose ? foutre :p
+               // C'est pas comme si on en avait quelque chose a foutre :p
        }

        public void windowDeiconified(WindowEvent e) {

Modified: trunk/apps/Thaw/src/thaw/core/Main.java
===================================================================
--- trunk/apps/Thaw/src/thaw/core/Main.java     2006-10-29 21:37:32 UTC (rev 
10742)
+++ trunk/apps/Thaw/src/thaw/core/Main.java     2006-10-29 22:08:27 UTC (rev 
10743)
@@ -13,7 +13,7 @@
 public class Main {

        public final static String VERSION="0.6 WIP r at custom@";
-       //public final static String VERSION="0.5 Beta";
+       //public final static String VERSION="0.5.9 Beta";

        /**
         * Look & feel use by GUI front end

Modified: trunk/apps/Thaw/src/thaw/plugins/Hsqldb.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/Hsqldb.java        2006-10-29 21:37:32 UTC 
(rev 10742)
+++ trunk/apps/Thaw/src/thaw/plugins/Hsqldb.java        2006-10-29 22:08:27 UTC 
(rev 10743)
@@ -13,6 +13,8 @@

        private Connection connection;

+       private int writeLock;
+
        public Hsqldb() {

        }
@@ -32,6 +34,22 @@
                return true;
        }

+
+       public void lockWriting() {
+               while(writeLock > 0) {
+                       try {
+                               Thread.sleep(100);
+                       } catch(java.lang.InterruptedException e) {
+                               /* \_o< */
+                       }
+               }
+               writeLock++;
+       }
+
+       public synchronized void unlockWriting() {
+               writeLock = 0;
+       }
+
        public void realStart() {
                Logger.info(this, "Connecting to the database ...");


Modified: trunk/apps/Thaw/src/thaw/plugins/index/File.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/File.java    2006-10-29 21:37:32 UTC 
(rev 10742)
+++ trunk/apps/Thaw/src/thaw/plugins/index/File.java    2006-10-29 22:08:27 UTC 
(rev 10743)
@@ -229,12 +229,14 @@
                this.notifyObservers();
        }

-       public void insert() {
+       public synchronized void insert() {
                if (this.parent == null) {
                        Logger.notice(this, "insert(): No parent !");
                        return;
                }

+               db.lockWriting();
+
                try {
                        PreparedStatement st;

@@ -288,6 +290,7 @@
                        Logger.error(this, "Unable to insert file 
'"+this.fileName+"' because: "+e.toString());
                }

+               db.unlockWriting();
        }

        public void delete() {

Modified: trunk/apps/Thaw/src/thaw/plugins/index/Index.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/Index.java   2006-10-29 21:37:32 UTC 
(rev 10742)
+++ trunk/apps/Thaw/src/thaw/plugins/index/Index.java   2006-10-29 22:08:27 UTC 
(rev 10743)
@@ -143,6 +143,7 @@
        public boolean create() {
                try {
                        /* Rahh ! Hsqldb doesn't support getGeneratedKeys() ! 
8/ */
+                       db.lockWriting();

                        Connection c = this.db.getConnection();
                        PreparedStatement st;
@@ -177,10 +178,14 @@

                        st.execute();

+                       db.unlockWriting();
+
                        return true;
                } catch(SQLException e) {
                        Logger.error(this, "Unable to insert the new index in 
the db, because: "+e.toString());

+                       db.unlockWriting();
+
                        return false;
                }

@@ -327,6 +332,8 @@
                clientGet = new FCPClientGet(key, 2, 2, false, -1, null);
                this.transfer = clientGet;

+               clientGet.addObserver(this);
+
                /*
                 * These requests are usually quite fast, and don't consume a 
lot
                 * of bandwith / CPU. So we can skip the queue and start 
immediatly
@@ -357,20 +364,7 @@

                public void run() {
                        loadXML(clientGet.getInputStream());
-
-                       if (rewriteKey)
-                               publicKey = transfer.getFileKey();
-                       else
-                               revision = 
FreenetURIHelper.getUSKRevision(transfer.getFileKey());
-
-                       Logger.info(this, "Most up-to-date key found: " + 
publicKey);
-
                        save();
-
-                       transfer = null;
-
-                       setChanged();
-                       notifyObservers();
                }
        }

@@ -460,7 +454,7 @@
                }
        }

-       public void save() {
+       public synchronized void save() {
                try {
                        Connection c = this.db.getConnection();
                        PreparedStatement st = c.prepareStatement("UPDATE 
indexes SET originalName = ?, displayName = ?, publicKey = ?, privateKey = ?, 
positionInTree = ?, revision = ?, parent = ? , author = ? WHERE id = ?");
@@ -561,9 +555,30 @@
                                        return;
                                }

+                               if (this.transfer instanceof FCPClientGet) {
+                                       
((FCPClientGet)this.transfer).deleteObserver(this);
+
+                                       if (rewriteKey)
+                                               publicKey = 
transfer.getFileKey();
+                                       else
+                                               revision = 
FreenetURIHelper.getUSKRevision(transfer.getFileKey());
+
+                                       Logger.info(this, "Most up-to-date key 
found: " + publicKey);
+
+                                       /* Reminder: These requests are 
non-peristent */
+                                       //if 
(this.transfer.stop(this.queueManager))
+                                       //      
this.queueManager.remove(this.transfer);
+                                       this.queueManager.remove(this.transfer);
+
+                                       this.transfer = null;
+
+                                       this.setChanged();
+                                       this.notifyObservers();
+                               }
+
                        }

-                       if (this.transfer.isFinished() && 
!this.transfer.isSuccessful()) {
+                       if (this.transfer != null && this.transfer.isFinished() 
&& !this.transfer.isSuccessful()) {
                                Logger.info(this, "Unable to get new version of 
the index");
                                this.transfer = null;
                                this.setChanged();
@@ -988,7 +1003,7 @@
                return files;
        }

-       public void loadXML(java.io.InputStream input) {
+       public synchronized void loadXML(java.io.InputStream input) {
                DocumentBuilderFactory xmlFactory = 
DocumentBuilderFactory.newInstance();
                DocumentBuilder xmlBuilder;


Modified: trunk/apps/Thaw/src/thaw/plugins/index/Link.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/Link.java    2006-10-29 21:37:32 UTC 
(rev 10742)
+++ trunk/apps/Thaw/src/thaw/plugins/index/Link.java    2006-10-29 22:08:27 UTC 
(rev 10743)
@@ -88,6 +88,8 @@
                try {
                        PreparedStatement st;

+                       db.lockWriting();
+
                        st = this.db.getConnection().prepareStatement("SELECT 
id FROM links ORDER BY id DESC LIMIT 1");

                        try {
@@ -121,6 +123,8 @@
                        Logger.error(this, "Unable to insert link to 
'"+this.indexName+"' because: "+e.toString());
                }

+               db.unlockWriting();
+
        }




Reply via email to