directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r1540153 - in /directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed: BTree.java BTreeBuilder.java BTreeConfiguration.java RecordManager.java ValueHolder.java
Date Fri, 08 Nov 2013 18:52:30 GMT
Author: elecharny
Date: Fri Nov  8 18:52:30 2013
New Revision: 1540153

URL: http://svn.apache.org/r1540153
Log:
o Shared the cache with any sub-btree, to avoid creating a new one for each sub-btree

Modified:
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BTree.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BTreeBuilder.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BTreeConfiguration.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/RecordManager.java
    directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/ValueHolder.java

Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BTree.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BTree.java?rev=1540153&r1=1540152&r2=1540153&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BTree.java
(original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BTree.java
Fri Nov  8 18:52:30 2013
@@ -25,7 +25,6 @@ import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.nio.channels.FileChannel;
 import java.util.Comparator;
-import java.util.LinkedList;
 import java.util.concurrent.ConcurrentLinkedQueue;
 import java.util.concurrent.locks.ReentrantLock;
 
@@ -111,6 +110,9 @@ public class BTree<K, V> implements Clos
 
     /** The default number of pages to keep in memory */
     private static final int DEFAULT_CACHE_SIZE = 1000;
+    
+    /** A flag indicating if this BTree is a Sub BTree */
+    private boolean isSubBtree = false;
 
     /** The number of stored Values before we switch to a BTree */
     private static final int DEFAULT_VALUE_THRESHOLD_UP = 8;
@@ -221,6 +223,7 @@ public class BTree<K, V> implements Clos
         btreeHeader = new BTreeHeader();
         btreeHeader.setName( name );
         btreeHeader.setPageSize( configuration.getPageSize() );
+        isSubBtree = configuration.isSubBtree();
 
         keySerializer = configuration.getKeySerializer();
         btreeHeader.setKeySerializerFQCN( keySerializer.getClass().getName() );
@@ -241,6 +244,14 @@ public class BTree<K, V> implements Clos
         // Create the first root page, with revision 0L. It will be empty
         // and increment the revision at the same time
         rootPage = new Leaf<K, V>( this );
+        
+        if ( isSubBtree )
+        {
+            // The subBTree inherit its cache from its parent BTree
+            this.cache = configuration.getParentBTree().getCache();
+            this.writeLock = configuration.getParentBTree().getWriteLock();
+            readTransactions = new ConcurrentLinkedQueue<Transaction<K, V>>();
+        }
 
         // Now, initialize the BTree
         init();
@@ -359,22 +370,27 @@ public class BTree<K, V> implements Clos
      */
     public void init() throws IOException
     {
-        // Create the queue containing the pending read transactions
-        readTransactions = new ConcurrentLinkedQueue<Transaction<K, V>>();
-
-        writeLock = new ReentrantLock();
-
-        // Initialize the caches
-        CacheConfiguration cacheConfiguration = new CacheConfiguration();
-        cacheConfiguration.setName( "pages" );
-        cacheConfiguration.setEternal( true );
-        cacheConfiguration.setOverflowToDisk( false );
-        cacheConfiguration.setCacheLoaderTimeoutMillis( 0 );
-        cacheConfiguration.setMaxElementsInMemory( cacheSize );
-        cacheConfiguration.setMemoryStoreEvictionPolicy( "LRU" );
+        if ( !isSubBtree )
+        {
+            // This is not a subBtree, we have to initialize the cache
 
-        cache = new Cache( cacheConfiguration );
-        cache.initialise();
+            // Create the queue containing the pending read transactions
+            readTransactions = new ConcurrentLinkedQueue<Transaction<K, V>>();
+    
+            writeLock = new ReentrantLock();
+    
+            // Initialize the caches
+            CacheConfiguration cacheConfiguration = new CacheConfiguration();
+            cacheConfiguration.setName( "pages" );
+            cacheConfiguration.setEternal( true );
+            cacheConfiguration.setOverflowToDisk( false );
+            cacheConfiguration.setCacheLoaderTimeoutMillis( 0 );
+            cacheConfiguration.setMaxElementsInMemory( cacheSize );
+            cacheConfiguration.setMemoryStoreEvictionPolicy( "LRU" );
+    
+            cache = new Cache( cacheConfiguration );
+            cache.initialise();
+        }
 
         // Initialize the txnManager thread
         //FIXME we should NOT create a new transaction manager thread for each BTree
@@ -392,6 +408,24 @@ public class BTree<K, V> implements Clos
 
 
     /**
+     * Return the cache we use in this BTree
+     */
+    /* No qualifier */ReentrantLock getWriteLock()
+    {
+        return writeLock;
+    }
+
+
+    /**
+     * Return the cache we use in this BTree
+     */
+    /* No qualifier */ConcurrentLinkedQueue<Transaction<K, V>> getReadTransactions()
+    {
+        return readTransactions;
+    }
+
+
+    /**
      * Close the BTree, cleaning up all the data structure
      */
     public void close() throws IOException
@@ -1215,7 +1249,7 @@ public class BTree<K, V> implements Clos
     @SuppressWarnings("unchecked")
     /* no qualifier */ValueHolder<V> createValueHolder( V value )
     {
-        return new ValueHolder<V>( recordManager, valueSerializer, value );
+        return new ValueHolder<V>( this, valueSerializer, value );
     }
 
 

Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BTreeBuilder.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BTreeBuilder.java?rev=1540153&r1=1540152&r2=1540153&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BTreeBuilder.java
(original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BTreeBuilder.java
Fri Nov  8 18:52:30 2013
@@ -83,7 +83,7 @@ public class BTreeBuilder<K, V>
 
             setKey( leaf1, leafIndex, tuple.getKey() );
 
-            ValueHolder<V> eh = new ValueHolder<V>( btree.getRecordManager(),
btree.getValueSerializer(),
+            ValueHolder<V> eh = new ValueHolder<V>( btree, btree.getValueSerializer(),
                 tuple.getValue() );
 
             setValue( leaf1, leafIndex, eh );

Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BTreeConfiguration.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BTreeConfiguration.java?rev=1540153&r1=1540152&r2=1540153&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BTreeConfiguration.java
(original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/BTreeConfiguration.java
Fri Nov  8 18:52:30 2013
@@ -20,6 +20,8 @@
 package org.apache.directory.mavibot.btree.managed;
 
 
+import net.sf.ehcache.Cache;
+
 import org.apache.directory.mavibot.btree.serializer.ElementSerializer;
 
 
@@ -83,11 +85,16 @@ public class BTreeConfiguration<K, V>
 
     /** Flag to enable duplicate key support */
     private boolean allowDuplicates;
+    
+    /** A flag set when the BTree is a sub btree */
+    private boolean isSubBtree = false;
 
-    /** The cache size */
+    /** The cache size, if it's <= 0, we don't have cache */
     private int cacheSize;
-
-
+    
+    /** The inherited BTree if we create a sub BTree */
+    private BTree<?, V> parentBTree;
+    
     /**
      * @return the pageSize
      */
@@ -293,7 +300,7 @@ public class BTreeConfiguration<K, V>
      * 
      * @param allowDuplicates
      * @throws IllegalStateException if the btree was already initialized or when tried to
turn off duplicate suport on
-     *                               an existing btree containing duplicate keys
+     * an existing btree containing duplicate keys
      */
     public void setAllowDuplicates( boolean allowDuplicates )
     {
@@ -311,10 +318,46 @@ public class BTreeConfiguration<K, V>
 
 
     /**
-     * @param cacheSize the cacheSize to set
+     * @param cacheSize the cacheSize to set.
      */
     public void setCacheSize( int cacheSize )
     {
         this.cacheSize = cacheSize;
     }
+
+
+    /**
+     * @return the cache
+     */
+    public BTree<?, V> getParentBTree()
+    {
+        return parentBTree;
+    }
+
+
+    /**
+     * @param cache the cache to set.
+     */
+    public void setParentBTree( BTree<?, V> parentBTree )
+    {
+        this.parentBTree = parentBTree;
+    }
+
+
+    /**
+     * @return True if this is a sub btree, false otherwise
+     */
+    public boolean isSubBtree()
+    {
+        return isSubBtree;
+    }
+
+
+    /**
+     * @param isSubBtree True if the BTree will be a sub btree, false otherwise
+     */
+    public void setSubBtree( boolean isSubBtree )
+    {
+        this.isSubBtree = isSubBtree;
+    }
 }

Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/RecordManager.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/RecordManager.java?rev=1540153&r1=1540152&r2=1540153&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/RecordManager.java
(original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/RecordManager.java
Fri Nov  8 18:52:30 2013
@@ -652,7 +652,7 @@ public class RecordManager
                         throw new RuntimeException( e );
                     }
 
-                    valueHolder = new ValueHolder<V>( this, btree.getValueSerializer(),
subBtree );
+                    valueHolder = new ValueHolder<V>( btree, btree.getValueSerializer(),
subBtree );
 
                     valueHolder.setSubBtree( subBtree );
                 }
@@ -676,7 +676,7 @@ public class RecordManager
                 // This is an Array of values, read the byte[] associated with it
                 byte[] valueBytes = new byte[valueLengths[i]];
                 byteBuffer.get( valueBytes );
-                valueHolder = new ValueHolder<V>( this, btree.getValueSerializer(),
false, nbValues,
+                valueHolder = new ValueHolder<V>( btree, btree.getValueSerializer(),
false, nbValues,
                     valueBytes );
             }
 

Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/ValueHolder.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/ValueHolder.java?rev=1540153&r1=1540152&r2=1540153&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/ValueHolder.java
(original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/managed/ValueHolder.java
Fri Nov  8 18:52:30 2013
@@ -54,7 +54,7 @@ public class ValueHolder<V> implements C
     private boolean isSubBtree = false;
 
     /** The RecordManager */
-    private RecordManager recordManager;
+    private BTree<?, V> btree;
 
     /** The Value serializer */
     private ElementSerializer<V> valueSerializer;
@@ -69,14 +69,14 @@ public class ValueHolder<V> implements C
      * @param valueSerializer The Value's serializer
      * @param raw The raw data containing the values
      */
-    /* No qualifier */ValueHolder( RecordManager recordManager, ElementSerializer<V>
valueSerializer,
+    /* No qualifier */ValueHolder( BTree<?, V> btree, ElementSerializer<V> valueSerializer,
         boolean isSubBtree, int nbValues,
         byte[] raw )
     {
         this.valueSerializer = valueSerializer;
-        this.recordManager = recordManager;
         this.raw = raw;
         this.isSubBtree = isSubBtree;
+        this.btree = btree;
 
         if ( nbValues < BTree.valueThresholdUp )
         {
@@ -98,11 +98,11 @@ public class ValueHolder<V> implements C
      * @param valueSerializer The Value's serializer
      * @param raw The raw data containing the values
      */
-    /* No qualifier */ValueHolder( RecordManager recordManager, ElementSerializer<V>
valueSerializer,
+    /* No qualifier */ValueHolder( BTree<?, V> btree, ElementSerializer<V> valueSerializer,
         BTree<V, V> subBtree )
     {
         this.valueSerializer = valueSerializer;
-        this.recordManager = recordManager;
+        this.btree = btree;
         raw = null;
         isRaw = false;
         isSubBtree = true;
@@ -116,10 +116,10 @@ public class ValueHolder<V> implements C
      * @param valueSerializer The Value's serializer
      * @param values The Values stored in the ValueHolder
      */
-    /* No qualifier */ValueHolder( RecordManager recordManager, ElementSerializer<V>
valueSerializer, V... values )
+    /* No qualifier */ValueHolder( BTree<?, V> btree, ElementSerializer<V> valueSerializer,
V... values )
     {
         this.valueSerializer = valueSerializer;
-        this.recordManager = recordManager;
+        this.btree = btree;
 
         if ( values != null )
         {
@@ -648,11 +648,19 @@ public class ValueHolder<V> implements C
     {
         try
         {
-            valueBtree = new BTree<V, V>( UUID.randomUUID().toString(), valueSerializer,
valueSerializer );
+            BTreeConfiguration<V, V> configuration = new BTreeConfiguration<V, V>();
+            configuration.setAllowDuplicates( false );
+            configuration.setKeySerializer( valueSerializer );
+            configuration.setName( UUID.randomUUID().toString() );
+            configuration.setValueSerializer( valueSerializer );
+            configuration.setParentBTree( btree );
+            configuration.setSubBtree( true );
+            
+            valueBtree = new BTree<V, V>( configuration );
 
             try
             {
-                recordManager.manage( valueBtree, true );
+                btree.getRecordManager().manage( valueBtree, true );
                 isSubBtree = true;
                 isRaw = false;
                 raw = null;



Mime
View raw message