one more patch of DOMFile instead previos

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

one more patch of DOMFile instead previos

Евгений-8



Index: eXist-1.0/src/org/exist/storage/dom/DOMFile.java
===================================================================
RCS file: /cvsroot/exist/eXist-1.0/src/org/exist/storage/dom/DOMFile.java,v
retrieving revision 1.7
diff -u -r1.7 DOMFile.java
--- eXist-1.0/src/org/exist/storage/dom/DOMFile.java 30 Sep 2005 09:43:47 -0000 1.7
+++ eXist-1.0/src/org/exist/storage/dom/DOMFile.java 12 Oct 2005 14:51:57 -0000
@@ -60,9 +60,12 @@
 import org.exist.util.Lockable;
 import org.exist.util.ReadOnlyException;
 import org.exist.util.hashtable.Object2LongIdentityHashMap;
+import org.exist.util.hashtable.Long2ObjectHashMap;
 import org.exist.util.sanity.SanityCheck;
 import org.exist.xquery.TerminatedException;
 import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
 
 /**
  * This is the main storage for XML nodes. Nodes are stored in document order.
@@ -151,16 +154,61 @@
 
  public final static long DATA_SYNC_PERIOD = 4200;
 
+    private Long2ObjectHashMap values = new Long2ObjectHashMap(); // cach for values  of nodes
+
  private final Cache dataCache;
 
+    private long mask(long m){
+        return m & 0xFFFFFFFF0000FFFFL;
+    }
+    
+    private String getValuesCache(long p){
+        return (String)values.get(mask(p));
+    }
+
+    private void putValuesCache(long p, String value){
+        values.put(mask(p),value);
+    }
+    
+    /**
+     * Clear cash values for node with address p
+     *
+     * @param p
+     */
+    private void clearValuesCache(long p){
+         values.remove(mask(p));
+    }
+    
+    private void clearValuesCache(NodeImpl node){
+        clearValuesCache(node.getInternalAddress());
+    }
+    
+    private void clearAncestorValuesCache(NodeImpl node){
+        Node parent = node.getParentNode();
+        while (parent!=null){
+            clearValuesCache((NodeImpl)parent);
+            parent = parent.getParentNode();
+        }
+    }
+    
+    private void clearChildrenValuesCache(NodeImpl node){
+        if (node.hasChildNodes()){
+            NodeList children = node.getChildNodes();
+            for (int i = 0; i < children.getLength(); i++){
+                NodeImpl n = (NodeImpl) children.item(i);
+                clearChildrenValuesCache(n);
+                clearValuesCache(n);
+            }
+        }
+    }
+    
  private BTreeFileHeader fileHeader;
 
  private Object owner = null;
 
  private Lock lock = null;
 
- private final Object2LongIdentityHashMap pages = new Object2LongIdentityHashMap(
- 64);
+ private final Object2LongIdentityHashMap pages = new Object2LongIdentityHashMap(64);
 
  private DocumentImpl currentDocument = null;
 
@@ -1425,7 +1473,7 @@
  }
  }
 
- /**
+     /**
  * Physically remove a node. The data of the node will be removed from the
  * page and the occupied space is freed.
  *
@@ -1523,6 +1571,12 @@
  }
  }
 
+ public void remove(Txn transaction, Value key, NodeImpl node) {
+        clearValuesCache(node); //remove node's value from cahce
+        clearAncestorValuesCache(node);
+        remove(transaction, key, node.getInternalAddress());
+    }
+    
  /**
  * Remove the specified page. The page is added to the list of free pages.
  *
@@ -1603,6 +1657,12 @@
 // LOG.debug(debug.toString());
  }
 
+ public void removeAll(Txn transaction, NodeImpl node) {
+         clearValuesCache(node);
+         clearChildrenValuesCache(node);
+         removeAll(transaction, node.getInternalAddress());
+    }
+    
  public String debugPages(DocumentImpl doc, boolean showPageContents) {
  StringBuffer buf = new StringBuffer();
  buf.append("Pages used by ").append(doc.getName());
@@ -1699,6 +1759,7 @@
  */
  public void update(Txn transaction, long p, byte[] value)
  throws ReadOnlyException {
+                
  RecordPos rec = findRecord(p);
         
  short l = ByteConversion.byteToShort(rec.page.data, rec.offset);
@@ -1724,6 +1785,13 @@
  rec.page.setDirty(true);
  }
 
+ public void update(Txn transaction, NodeImpl node, byte[] value)
+ throws ReadOnlyException {
+        clearValuesCache(node);
+        clearAncestorValuesCache(node);
+        update(transaction, node.getInternalAddress(), value);
+    }
+    
  /**
  * Retrieve the string value of the specified node.
  *
@@ -1737,6 +1805,14 @@
  address = findValue(this, proxy);
  if (address == BTree.KEY_NOT_FOUND)
  return null;
+            
+ String value;
+
+            value = getValuesCache(address); //get value from cache, if cached ..
+            if (value!=null)
+                return value;
+            // ... otherwise calculate value
+            
  final RecordPos rec = findRecord(address);
  SanityCheck.THROW_ASSERT(rec != null,
  "Node data could not be found! Page: "
@@ -1746,12 +1822,15 @@
  final ByteArrayOutputStream os = new ByteArrayOutputStream();
  getNodeValue(os, rec, true, addWhitespace);
  final byte[] data = os.toByteArray();
- String value;
+            
  try {
  value = new String(data, "UTF-8");
  } catch (UnsupportedEncodingException e) {
  value = new String(data);
  }
+            
+            putValuesCache(address,value); //add value in cache
+            
  return value;
  } catch (BTreeException e) {
  LOG.warn("btree error while reading node value", e);