svn commit: r527709 - in /xml/xindice/trunk/java/src/org/apache/xindice/core/filer: BTree.java BTreeFiler.java HashFiler.java

[email protected]
Newsgroups gmane.text.xml.xindice.devel
Message-ID <[email protected]>
Author: vgritsenko
Date: Wed Apr 11 15:23:47 2007
New Revision: 527709

URL: http://svn.apache.org/viewvc?view=rev&rev=527709
Log:
concurrency fixes to filers from bug #42026

Modified:
    xml/xindice/trunk/java/src/org/apache/xindice/core/filer/BTree.java
    xml/xindice/trunk/java/src/org/apache/xindice/core/filer/BTreeFiler.java
    xml/xindice/trunk/java/src/org/apache/xindice/core/filer/HashFiler.java

Modified: xml/xindice/trunk/java/src/org/apache/xindice/core/filer/BTree.java
URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/core/filer/BTree.java?view=diff&rev=527709&r1=527708&r2=527709
==============================================================================
--- xml/xindice/trunk/java/src/org/apache/xindice/core/filer/BTree.java (original)
+++ xml/xindice/trunk/java/src/org/apache/xindice/core/filer/BTree.java Wed Apr 11 15:23:47 2007
@@ -63,7 +63,7 @@
  * to store as many values and pointers on one page as is possible.
  *
  * <br>
- * This implementation supports the notion of nested roots.  This means
+ * This implementation supports the notion of nested roots. This means
  * that you can create a btree where the pointers actually point to the
  * root of a separate btree being managed in the same file.
  *
@@ -148,6 +148,19 @@
     }
 
     /**
+     * addKey adds a Value to the BTree and associates a pointer with
+     * it.  The pointer can be used for referencing any type of data, it
+     * just so happens that Xindice uses it for referencing pages of
+     * associated data in the BTree file or other files.
+     *
+     * @param value The Value to add
+     * @return Pointer to the value
+     */
+    public long addKey(Value value) throws IOException, BTreeException {
+        return getRootNode().addKey(value);
+    }
+
+    /**
      * addValue adds a Value to the BTree and associates a pointer with
      * it.  The pointer can be used for referencing any type of data, it
      * just so happens that Xindice uses it for referencing pages of
@@ -625,6 +638,47 @@
                         }
                     }
                     return -1;
+
+                default :
+                    throw new BTreeCorruptException("Invalid Page Type In addValue");
+            }
+        }
+
+        public synchronized long addKey(Value value) throws IOException, BTreeException {
+            if (value == null) {
+                throw new BTreeException(FaultCodes.DBE_CANNOT_CREATE, "Can't add a null Value");
+            }
+
+            int idx = Arrays.binarySearch(values, value);
+
+            switch (ph.getStatus()) {
+                case BRANCH:
+                    idx = idx < 0 ? -(idx + 1) : idx + 1;
+                    return getChildNode(idx).addKey(value);
+
+                case LEAF:
+                    if (idx >= 0) {
+                        // Key already exists
+                        return ptrs[idx];
+                    } else {
+                        // Value was not found
+                        idx = -(idx + 1);
+
+                        // Check to see if we've exhausted the block
+                        boolean split = needSplit(value);
+
+                        long pointer = getFreePage().getPageNum();
+                        set(insertArrayValue(values, value, idx), insertArrayLong(ptrs, pointer, idx));
+
+                        if (split) {
+                            split();
+                        } else {
+                            write();
+                        }
+
+                        fileHeader.incRecordCount();
+                        return pointer;
+                    }
 
                 default :
                     throw new BTreeCorruptException("Invalid Page Type In addValue");

Modified: xml/xindice/trunk/java/src/org/apache/xindice/core/filer/BTreeFiler.java
URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/core/filer/BTreeFiler.java?view=diff&rev=527709&r1=527708&r2=527709
==============================================================================
--- xml/xindice/trunk/java/src/org/apache/xindice/core/filer/BTreeFiler.java (original)
+++ xml/xindice/trunk/java/src/org/apache/xindice/core/filer/BTreeFiler.java Wed Apr 11 15:23:47 2007
@@ -129,15 +129,8 @@
 
         checkOpened();
         try {
-            Page p;
-            try {
-                long pos = findValue(key);
-                p = getPage(pos);
-            } catch (BTreeNotFoundException e) {
-                p = getFreePage();
-                addValue(key, p.getPageNum());
-                fileHeader.incRecordCount();
-            }
+            long pos = addKey(key);
+            Page p = getPage(pos);
 
             BTreeFilerPageHeader ph = (BTreeFilerPageHeader) p.getPageHeader();
             long t = System.currentTimeMillis();
@@ -150,6 +143,7 @@
             writeValue(p, value);
             flush();
         } catch (IOException e) {
+            // FIXME: cleanup?
             throw new FilerException(FaultCodes.DBE_CANNOT_CREATE,
                                      "Can't write record '" + key + "': " + e.getMessage(), e);
         }
@@ -163,10 +157,8 @@
 
         checkOpened();
         try {
-            long pos = findValue(key);
+            long pos = removeValue(key);
             Page p = getPage(pos);
-
-            removeValue(key);
             unlinkPages(p);
 
             fileHeader.decRecordCount();

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=527709&r1=527708&r2=527709
==============================================================================
--- 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 Wed Apr 11 15:23:47 2007
@@ -212,10 +212,7 @@
             p = seekInsertionPage(key);
             p.setKey(key);
             writeValue(p, value);
-            p = null;
         } catch (Exception e) {
-            throw new FilerException(FaultCodes.DBE_CANNOT_CREATE, "Exception: " + e, 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);
@@ -225,6 +222,8 @@
                     // Double exception
                 }
             }
+
+            throw new FilerException(FaultCodes.DBE_CANNOT_CREATE, "Exception: " + e, e);
         }
 
         flush();
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.