Author: vgritsenko
Date: Thu Mar 15 18:44:50 2007
New Revision: 518830

URL: http://svn.apache.org/viewvc?view=rev&rev=518830
Log:
fix possible threading issue in key hash code; other cleanup

Modified:
    xml/xindice/trunk/java/src/org/apache/xindice/core/data/Key.java
    xml/xindice/trunk/java/src/org/apache/xindice/core/filer/HashFiler.java
    xml/xindice/trunk/java/src/org/apache/xindice/core/filer/Paged.java

Modified: xml/xindice/trunk/java/src/org/apache/xindice/core/data/Key.java
URL: 
http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/core/data/Key.java?view=diff&rev=518830&r1=518829&r2=518830
==============================================================================
--- xml/xindice/trunk/java/src/org/apache/xindice/core/data/Key.java (original)
+++ xml/xindice/trunk/java/src/org/apache/xindice/core/data/Key.java Thu Mar 15 
18:44:50 2007
@@ -25,7 +25,9 @@
  * @version $Revision$, $Date$
  */
 public final class Key extends Value {
-    private int hash = 0;
+
+    private int hash;
+
 
     public Key(Value value) {
         super(value);
@@ -43,20 +45,16 @@
         super(data);
     }
 
-    // TODO: This has to be revisited
-    private void calculateHash() {
-        hash = 0;
-        int pl = pos + len;
-        for (int i = pos; i < pl; i++) {
-            hash = (hash << 5) ^ data[i];
-            hash = hash % 1234567891;
-        }
-        hash = Math.abs(hash);
-    }
-
     public int getHash() {
+        int hash = this.hash;
         if (hash == 0) {
-            calculateHash();
+            // TODO: This has to be revisited
+            int end = pos + len;
+            for (int i = pos; i < end; i++) {
+                hash = (hash << 5) ^ data[i];
+                hash = hash % 1234567891;
+            }
+            this.hash = Math.abs(hash);
         }
         return hash;
     }
@@ -75,11 +73,11 @@
     }
 
     public boolean equals(Object obj) {
-        if (obj instanceof Key) {
-            return equals((Key) obj);
-        } else {
-            return super.equals(obj);
+        if (obj instanceof Value) {
+            return equals((Value) obj);
         }
+
+        return super.equals(obj);
     }
 }
 

Modified: 
xml/xindice/trunk/java/src/org/apache/xindice/core/filer/HashFiler.java
URL: 
http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/core/filer/HashFiler.java?view=diff&rev=518830&r1=518829&r2=518830
==============================================================================
--- xml/xindice/trunk/java/src/org/apache/xindice/core/filer/HashFiler.java 
(original)
+++ xml/xindice/trunk/java/src/org/apache/xindice/core/filer/HashFiler.java Thu 
Mar 15 18:44:50 2007
@@ -111,12 +111,12 @@
         synchronized (p) {
             while (true) {
                 HashPageHeader ph = (HashPageHeader) p.getPageHeader();
-                if (ph.getStatus() == RECORD && ph.getKeyHash() == 
key.getHash() && p.getKey().equals(key)) {
+                if (ph.getStatus() == RECORD && ph.getKeyHash() == hash && 
p.getKey().equals(key)) {
                     return p;
                 }
 
                 pageNum = ph.getNextCollision();
-                if (pageNum == -1) {
+                if (pageNum == NO_PAGE) {
                     return null;
                 }
                 p = getPage(pageNum);
@@ -135,7 +135,7 @@
                 Value v = readValue(startPage);
                 HashPageHeader sph = (HashPageHeader) 
startPage.getPageHeader();
 
-                HashMap meta = new HashMap(2);
+                HashMap meta = new HashMap(3);
                 meta.put(Record.CREATED, new Long(sph.getCreated()));
                 meta.put(Record.MODIFIED, new Long(sph.getModified()));
 
@@ -160,13 +160,13 @@
             while (true) {
                 ph = (HashPageHeader) p.getPageHeader();
                 if (ph.getStatus() == UNUSED || ph.getStatus() == DELETED
-                        || (ph.getStatus() == RECORD && ph.getKeyHash() == 
key.getHash() && p.getKey().equals(key))) {
+                        || (ph.getStatus() == RECORD && ph.getKeyHash() == 
hash && p.getKey().equals(key))) {
                     // Found free page
                     break;
                 }
 
                 pageNum = ph.getNextCollision();
-                if (pageNum == -1) {
+                if (pageNum == NO_PAGE) {
                     // Reached end of chain, add new page
                     Page np = getFreePage();
 
@@ -193,7 +193,7 @@
             ph.setModified(t);
             ph.setStatus(RECORD);
 
-            // Write modifications to the page header before existing 
synchronization block
+            // Write modifications to the page header before exiting 
synchronization block
             // This will prevent other threads from getting this same page
             p.write();
         }
@@ -219,6 +219,7 @@
         } catch (Exception e) {
             throw new FilerException(FaultCodes.DBE_CANNOT_CREATE, "Exception: 
" + e);
         } finally {
+            // FIXME It's not enough. At this point, new record could have 
been added to the chain
             if (p != null) {
                 p.getPageHeader().setStatus(DELETED);
                 try {
@@ -275,7 +276,7 @@
                 Page prev = null;
                 while (true) {
                     pageHead = (HashPageHeader) page.getPageHeader();
-                    if (pageHead.getStatus() == RECORD && 
pageHead.getKeyHash() == key.getHash() && page.getKey().equals(key)) {
+                    if (pageHead.getStatus() == RECORD && 
pageHead.getKeyHash() == hash && page.getKey().equals(key)) {
                         break;
                     }
 

Modified: xml/xindice/trunk/java/src/org/apache/xindice/core/filer/Paged.java
URL: 
http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/core/filer/Paged.java?view=diff&rev=518830&r1=518829&r2=518830
==============================================================================
--- xml/xindice/trunk/java/src/org/apache/xindice/core/filer/Paged.java 
(original)
+++ xml/xindice/trunk/java/src/org/apache/xindice/core/filer/Paged.java Thu Mar 
15 18:44:50 2007
@@ -341,7 +341,7 @@
 
             // if not check if it's already loaded in the page cache
             if (p == null) {
-                WeakReference ref = (WeakReference)pages.get(lp);
+                WeakReference ref = (WeakReference) pages.get(lp);
                 if (ref != null) {
                     p = (Page) ref.get();
                 }
@@ -1395,9 +1395,9 @@
         public synchronized Key getKey() {
             if (header.keyLen > 0) {
                 return new Key(this.data, this.keyPos, header.keyLen);
-            } else {
-                return null;
             }
+
+            return null;
         }
 
         public synchronized void streamTo(OutputStream os) throws IOException {


Reply via email to