xmlbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kkro...@apache.org
Subject svn commit: r156099 - in xmlbeans/trunk/src/store/org/apache/xmlbeans/impl/store: DomImpl.java Locale.java
Date Thu, 03 Mar 2005 22:58:46 GMT
Author: kkrouse
Date: Thu Mar  3 14:58:46 2005
New Revision: 156099

URL: http://svn.apache.org/viewcvs?view=rev&rev=156099
Log:
first cut at adding a DOM nth node cache

Modified:
    xmlbeans/trunk/src/store/org/apache/xmlbeans/impl/store/DomImpl.java
    xmlbeans/trunk/src/store/org/apache/xmlbeans/impl/store/Locale.java

Modified: xmlbeans/trunk/src/store/org/apache/xmlbeans/impl/store/DomImpl.java
URL: http://svn.apache.org/viewcvs/xmlbeans/trunk/src/store/org/apache/xmlbeans/impl/store/DomImpl.java?view=diff&r1=156098&r2=156099
==============================================================================
--- xmlbeans/trunk/src/store/org/apache/xmlbeans/impl/store/DomImpl.java (original)
+++ xmlbeans/trunk/src/store/org/apache/xmlbeans/impl/store/DomImpl.java Thu Mar  3 14:58:46
2005
@@ -98,6 +98,7 @@
     static Dom parent      ( Dom d ) { return node_getParentNode ( d ); }
     static Dom firstChild  ( Dom d ) { return node_getFirstChild ( d ); }
     static Dom nextSibling ( Dom d ) { return node_getNextSibling( d ); }
+    static Dom prevSibling ( Dom d ) { return node_getPreviousSibling( d ); }
 
     public static Dom append ( Dom n, Dom p )
     {
@@ -1644,6 +1645,8 @@
         while ( ! c.isAtEndOfLastPush() );
 
         c.release();
+        
+        n.locale().invalidateDomCaches(n);
     }
 
     //////////////////////////////////////////////////////////////////////////////////////
@@ -2526,11 +2529,15 @@
 
         // *Really* inefficient impl for now
 
-        for ( Dom c = node_getFirstChild( n ) ; c != null ; c = node_getNextSibling( c )
)
-            if (i-- == 0)
-                return c;
-
-        return null;
+        //for ( Dom c = node_getFirstChild( n ) ; c != null ; c = node_getNextSibling( c
) )
+        //    if (i-- == 0)
+        //        return c;
+        //        
+        //return null;
+        
+        // TODO - optimize for the 0 and 1 child case
+        
+        return n.locale().findDomNthChild(n, i);
     }
 
     //////////////////////////////////////////////////////////////////////////////////////
@@ -2576,12 +2583,17 @@
 
         // *Really* inefficient impl for now
 
-        int len = 0;
+        //int len = 0;
 
-        for ( Dom c = node_getFirstChild( n ) ; c != null ; c = node_getNextSibling( c )
)
-            len++;
+        //for ( Dom c = node_getFirstChild( n ) ; c != null ; c = node_getNextSibling( c
) )
+        //    len++;
 
-        return len;
+        //return len;
+        
+        
+        // TODO - optimize for the 0 and 1 child case
+        
+        return n.locale().domLength(n);
     }
 
     //////////////////////////////////////////////////////////////////////////////////////
@@ -3397,8 +3409,11 @@
         Dom p = (Dom) _node_getParentNode( t );
 
         if (p != null)
+        {
             _node_insertBefore( p, (Text) t2, _node_getNextSibling( t ) );
-
+            t.locale().invalidateDomCaches(p);
+        }
+        
         return (Text) t2;
     }
     
@@ -4825,4 +4840,4 @@
     }
     
 }
- 
\ No newline at end of file
+ 

Modified: xmlbeans/trunk/src/store/org/apache/xmlbeans/impl/store/Locale.java
URL: http://svn.apache.org/viewcvs/xmlbeans/trunk/src/store/org/apache/xmlbeans/impl/store/Locale.java?view=diff&r1=156098&r2=156099
==============================================================================
--- xmlbeans/trunk/src/store/org/apache/xmlbeans/impl/store/Locale.java (original)
+++ xmlbeans/trunk/src/store/org/apache/xmlbeans/impl/store/Locale.java Thu Mar  3 14:58:46
2005
@@ -2384,6 +2384,198 @@
     //
     //
     //
+    
+    Dom findDomNthChild ( Dom parent, int n )
+    {
+        assert n >= 0;
+        
+        if (parent == null)
+            return null;
+        
+        int da = _domNthCache_A.distance( parent, n );
+        int db = _domNthCache_B.distance( parent, n );
+        
+        Dom x =
+            da <= db
+            ? _domNthCache_A.fetch( parent, n )
+            : _domNthCache_B.fetch( parent, n );
+        
+        if (da == db)
+        {
+            domNthCache temp = _domNthCache_A;
+            _domNthCache_A = _domNthCache_B;
+            _domNthCache_B = temp;
+        }
+        
+        return x;
+    }
+    
+    int domLength ( Dom parent )
+    {
+        if (parent == null)
+            return 0;
+        
+        int da = _domNthCache_A.distance( parent, 0 );
+        int db = _domNthCache_B.distance( parent, 0 );
+        
+        int len =
+            da <= db
+            ? _domNthCache_A.length( parent )
+            : _domNthCache_B.length( parent );
+        
+        if (da == db)
+        {
+            domNthCache temp = _domNthCache_A;
+            _domNthCache_A = _domNthCache_B;
+            _domNthCache_B = temp;
+        }
+        
+        return len;
+    }
+    
+    void invalidateDomCaches ( Dom d )
+    {
+        if (_domNthCache_A._parent == d)
+            _domNthCache_A._version = -1;
+        if (_domNthCache_B._parent == d)
+            _domNthCache_B._version = -1;
+    }
+    
+    boolean isDomCached ( Dom d )
+    {
+        return _domNthCache_A._parent == d || _domNthCache_B._parent == d;
+    }
+    
+    class domNthCache
+    {
+        
+        int distance ( Dom parent, int n )
+        {
+            assert n >= 0;
+            
+            if (_version != Locale.this.version())
+                return Integer.MAX_VALUE - 1;
+            
+            if (parent != _parent)
+                return Integer.MAX_VALUE;
+            
+            return n > _n ? n - _n : _n - n;
+        }
+        
+        int length ( Dom parent )
+        {
+            if (_version != Locale.this.version() || _parent != parent)
+            {
+                _parent = parent;
+                _version = Locale.this.version();
+                _child = null;
+                _n = -1;
+                _len = -1;
+            }
+            
+            if (_len == -1)
+            {
+                Dom x = null;
+                
+                if (_child != null && _n != -1)
+                {
+                    x = _child;
+                    _len = _n;
+                }
+                else
+                {
+                    x = DomImpl.firstChild(_parent);
+                    _len = 0;
+                    
+                    // cache the 0th child
+                    _child = x;
+                    _n = 0;
+                }
+                
+                for (; x != null; x = DomImpl.nextSibling(x) )
+                {
+                    _len++;
+                }
+            }
+            
+            
+            return _len;
+        }
+        
+        Dom fetch ( Dom parent, int n )
+        {
+            assert n >= 0;
+            
+            if (_version != Locale.this.version() || _parent != parent)
+            {
+                _parent = parent;
+                _version = Locale.this.version();
+                _child = null;
+                _n = -1;
+                _len = -1;
+                
+                for (Dom x = DomImpl.firstChild(_parent); x != null; x = DomImpl.nextSibling(x)
)
+                {
+                    _n++;
+                    if (_child == null && n == _n )
+                    {
+                        _child = x;
+                        break;
+                    }
+                }
+                
+                return _child;
+            }
+            
+            if (_n < 0)
+                return null;
+            
+            if (n > _n)
+            {
+                while ( n > _n )
+                {
+                    for (Dom x = DomImpl.nextSibling(_child); ; x = DomImpl.nextSibling(x)
)
+                    {
+                        if (x == null)
+                            return null;
+                        
+                        _child = x;
+                        _n++;
+                        
+                        break;
+                    }
+                }
+            }
+            else if (n < _n)
+            {
+                while ( n < _n )
+                {
+                    for (Dom x = DomImpl.prevSibling(_child); ; x = DomImpl.prevSibling(x)
)
+                    {
+                        if (x == null)
+                            return null;
+                        
+                        _child = x;
+                        _n--;
+                        
+                        break;
+                    }
+                }
+            }
+            
+            return _child;
+        }
+        
+        private long  _version;
+        private Dom   _parent;
+        private Dom   _child;
+        private int   _n;
+        private int   _len;
+    }
+    
+    //
+    // 
+    //
 
     CharUtil getCharUtil()
     {
@@ -3412,4 +3604,7 @@
 
     nthCache _nthCache_A = new nthCache();
     nthCache _nthCache_B = new nthCache();
-}
\ No newline at end of file
+
+    domNthCache _domNthCache_A = new domNthCache();
+    domNthCache _domNthCache_B = new domNthCache();
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: commits-help@xmlbeans.apache.org


Mime
View raw message