You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2013/12/13 13:46:15 UTC
svn commit: r1550714 - in /directory/mavibot/trunk/mavibot/src:
main/java/org/apache/directory/mavibot/btree/
main/java/org/apache/directory/mavibot/btree/memory/
main/java/org/apache/directory/mavibot/btree/persisted/
test/java/org/apache/directory/ma...
Author: elecharny
Date: Fri Dec 13 12:46:14 2013
New Revision: 1550714
URL: http://svn.apache.org/r1550714
Log:
o Added a ValueHolder common interface, and an AbstractValueHolder class
o Use an Array to store values in the InMemory Btree, instead of a simple value. A little bit more memory used, a little bit slower, but benefits from a common access with the Persisted Btree
o The ValueArrayCursor is now shared
Added:
directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractValueHolder.java
directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/ValueArrayCursor.java
directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/ValueHolder.java
directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/InMemoryValueHolder.java
- copied, changed from r1550676, directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/ValueHolder.java
directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/PersistedValueHolder.java
- copied, changed from r1550678, directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/ValueHolder.java
directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/persisted/PersistedBTreeBrowseTest.java
- copied, changed from r1550678, directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/persisted/ManagedBTreeBrowseTest.java
directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/persisted/PersistedBTreeBuilderTest.java
- copied, changed from r1550678, directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/persisted/ManagedBTreeBuilderTest.java
Removed:
directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/ValueHolder.java
directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/ValueHolder.java
directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/persisted/ManagedBTreeBrowseTest.java
directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/persisted/ManagedBTreeBuilderTest.java
Modified:
directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/BTreeFactory.java
directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/InMemoryBTreeBuilder.java
directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/Leaf.java
directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/TupleCursorImpl.java
directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/BTreeFactory.java
directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/Leaf.java
directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/PersistedBTreeBuilder.java
directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/RecordManager.java
directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/TupleCursorImpl.java
directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/memory/InMemoryBTreeTest.java
Added: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractValueHolder.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractValueHolder.java?rev=1550714&view=auto
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractValueHolder.java (added)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractValueHolder.java Fri Dec 13 12:46:14 2013
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.directory.mavibot.btree;
+
+
+/**
+ * A holder to store the Values
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @param <V> The value type
+ */
+public abstract class AbstractValueHolder<V> implements ValueHolder<V>
+{
+ /** The BTree storing multiple value, if we have more than one value */
+ protected BTree<V, V> valueBtree;
+
+ /** The array storing from 1 to N values */
+ protected V[] valueArray;
+
+ /** The parent BTree */
+ protected BTree<?, V> parentBtree;
+
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean isSubBtree()
+ {
+ return valueBtree != null;
+ }
+
+
+ /**
+ * Create a clone of this instance
+ */
+ public ValueHolder<V> clone() throws CloneNotSupportedException
+ {
+ ValueHolder<V> copy = ( ValueHolder<V> ) super.clone();
+
+ return copy;
+ }
+}
Added: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/ValueArrayCursor.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/ValueArrayCursor.java?rev=1550714&view=auto
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/ValueArrayCursor.java (added)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/ValueArrayCursor.java Fri Dec 13 12:46:14 2013
@@ -0,0 +1,151 @@
+package org.apache.directory.mavibot.btree;
+
+import java.io.IOException;
+
+import org.apache.directory.mavibot.btree.exception.EndOfFileExceededException;
+
+/**
+ * A class that encapsulate the values into an array
+ */
+public class ValueArrayCursor<V> implements ValueCursor<V>
+{
+ /** Store the current position in the array or in the BTree */
+ private int currentPos;
+
+ /** The array storing values (1 to N) */
+ private V[] valueArray;
+
+ /**
+ * Create an instance
+ */
+ public ValueArrayCursor( V[] valueArray )
+ {
+ // Start at -1 to be positioned before the first element
+ currentPos = BEFORE_FIRST;
+ this.valueArray = valueArray;
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean hasNext()
+ {
+ return ( currentPos < valueArray.length - 1 ) && ( currentPos != AFTER_LAST );
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ public V next()
+ {
+ if ( valueArray == null )
+ {
+ // Deserialize the array
+ return null;
+ }
+ else
+ {
+ currentPos++;
+
+ if ( currentPos == valueArray.length )
+ {
+ currentPos = AFTER_LAST;
+
+ // We have reached the end of the array
+ return null;
+ }
+ else
+ {
+ return valueArray[currentPos];
+ }
+ }
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean hasPrev() throws EndOfFileExceededException, IOException
+ {
+ return currentPos > 0 || currentPos == AFTER_LAST;
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void close()
+ {
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void beforeFirst() throws IOException
+ {
+ currentPos = BEFORE_FIRST;
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void afterLast() throws IOException
+ {
+ currentPos = AFTER_LAST;
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public V prev() throws EndOfFileExceededException, IOException
+ {
+ if ( valueArray == null )
+ {
+ // Deserialize the array
+ return null;
+ }
+ else
+ {
+ if ( currentPos == AFTER_LAST )
+ {
+ currentPos = valueArray.length - 1;
+ }
+ else
+ {
+ currentPos--;
+ }
+
+ if ( currentPos == BEFORE_FIRST )
+ {
+ // We have reached the end of the array
+ return null;
+ }
+ else
+ {
+ return valueArray[currentPos];
+ }
+ }
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int size()
+ {
+ return valueArray.length;
+ }
+}
+
Added: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/ValueHolder.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/ValueHolder.java?rev=1550714&view=auto
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/ValueHolder.java (added)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/ValueHolder.java Fri Dec 13 12:46:14 2013
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.directory.mavibot.btree;
+
+/**
+ * A holder to store the Values
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @param <V> The value type
+ */
+public interface ValueHolder<V> extends Cloneable
+{
+ /**
+ * Tells if a value is contained in this ValueHolder
+ *
+ * @param checkedValue The added to check
+ */
+ boolean contains( V checkedValue );
+
+
+ /**
+ * @return the number of stored values
+ */
+ int size();
+
+
+ /**
+ * @return a cursor on top of the values
+ */
+ ValueCursor<V> getCursor();
+
+
+ /**
+ * @return true if we store the values in a sub btree
+ */
+ boolean isSubBtree();
+
+
+ /**
+ * Add a new value in the ValueHolder
+ *
+ * @param newValue The added value
+ */
+ void add( V newValue );
+
+
+ /**
+ * Remove a value from the ValueHolder
+ *
+ * @param removedValue The value to remove
+ */
+ V remove( V removedValue );
+
+
+ /**
+ * Create a clone of this instance
+ *
+ * @return a new instance of a ValueHolder
+ * @throws CloneNotSupportedException If we can't clone this instance
+ */
+ ValueHolder<V> clone() throws CloneNotSupportedException;
+}
Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/BTreeFactory.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/BTreeFactory.java?rev=1550714&r1=1550713&r2=1550714&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/BTreeFactory.java (original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/BTreeFactory.java Fri Dec 13 12:46:14 2013
@@ -200,7 +200,7 @@ public class BTreeFactory
* @param pos The position in the values array
* @param value the value to inject
*/
- public static <K, V> void setValue( Leaf<K, V> page, int pos, ValueHolder<V> value )
+ public static <K, V> void setValue( Leaf<K, V> page, int pos, InMemoryValueHolder<V> value )
{
page.setValue( pos, value );
}
@@ -234,7 +234,7 @@ public class BTreeFactory
if ( btree.getRootPage() instanceof Leaf )
{
Leaf<K, V> leaf = ( Leaf<K, V> ) ( btree.getRootPage() );
- ValueHolder<V> valueHolder = leaf.values[last.pos];
+ InMemoryValueHolder<V> valueHolder = leaf.values[last.pos];
last.valueCursor = valueHolder.getCursor();
}
else
@@ -251,7 +251,7 @@ public class BTreeFactory
if ( p instanceof Leaf )
{
Leaf<K, V> leaf = ( Leaf<K, V> ) ( last.page );
- ValueHolder<V> valueHolder = leaf.values[last.pos];
+ InMemoryValueHolder<V> valueHolder = leaf.values[last.pos];
last.valueCursor = valueHolder.getCursor();
break;
}
Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/InMemoryBTreeBuilder.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/InMemoryBTreeBuilder.java?rev=1550714&r1=1550713&r2=1550714&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/InMemoryBTreeBuilder.java (original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/InMemoryBTreeBuilder.java Fri Dec 13 12:46:14 2013
@@ -86,7 +86,7 @@ public class InMemoryBTreeBuilder<K, V>
setKey( leaf1, leafIndex, tuple.getKey() );
- ValueHolder<V> eh = new ValueHolder<V>( btree, tuple.getValue() );
+ InMemoryValueHolder<V> eh = new InMemoryValueHolder<V>( btree, tuple.getValue() );
setValue( leaf1, leafIndex, eh );
@@ -118,8 +118,8 @@ public class InMemoryBTreeBuilder<K, V>
lastLeaf.setKeys( ( KeyHolder[] ) Array.newInstance( KeyHolder.class, n ) );
System.arraycopy( keys, 0, lastLeaf.getKeys(), 0, n );
- ValueHolder<V>[] values = lastLeaf.values;
- lastLeaf.values = ( ValueHolder<V>[] ) Array.newInstance( ValueHolder.class, n );
+ InMemoryValueHolder<V>[] values = lastLeaf.values;
+ lastLeaf.values = ( InMemoryValueHolder<V>[] ) Array.newInstance( InMemoryValueHolder.class, n );
System.arraycopy( values, 0, lastLeaf.values, 0, n );
break;
Copied: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/InMemoryValueHolder.java (from r1550676, directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/ValueHolder.java)
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/InMemoryValueHolder.java?p2=directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/InMemoryValueHolder.java&p1=directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/ValueHolder.java&r1=1550676&r2=1550714&rev=1550714&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/ValueHolder.java (original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/InMemoryValueHolder.java Fri Dec 13 12:46:14 2013
@@ -21,11 +21,14 @@ package org.apache.directory.mavibot.btr
import java.io.IOException;
+import java.lang.reflect.Array;
import java.util.Comparator;
import java.util.UUID;
+import org.apache.directory.mavibot.btree.AbstractValueHolder;
import org.apache.directory.mavibot.btree.BTree;
import org.apache.directory.mavibot.btree.Tuple;
+import org.apache.directory.mavibot.btree.ValueArrayCursor;
import org.apache.directory.mavibot.btree.ValueCursor;
import org.apache.directory.mavibot.btree.ValueBTreeCursor;
import org.apache.directory.mavibot.btree.exception.EndOfFileExceededException;
@@ -37,180 +40,20 @@ import org.apache.directory.mavibot.btre
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
* @param <V> The value type
*/
-public class ValueHolder<V> implements Cloneable
+public class InMemoryValueHolder<V> extends AbstractValueHolder<V>
{
- /** The deserialized value */
- private V value;
-
- /** The BTree storing multiple value, if we have more than one value */
- private BTree<V, V> valueBtree;
-
- /** The RecordManager */
- private BTree<?, V> btree;
-
-
- /**
- * A class that encapsulate one single value
- */
- private class ValueSingletonCursor implements ValueCursor<V>
- {
- /** Store the current position in the array or in the BTree */
- private int currentPos;
-
-
- /**
- * Create an instance
- */
- private ValueSingletonCursor()
- {
- // Start at -1 to be positioned before the first element
- currentPos = BEFORE_FIRST;
- }
-
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean hasNext()
- {
- return currentPos == BEFORE_FIRST;
- }
-
-
- /**
- * {@inheritDoc}
- */
- public V next()
- {
- switch ( currentPos )
- {
- case AFTER_LAST :
- return null;
-
- case BEFORE_FIRST :
- currentPos = 0;
- return value;
-
- default :
- currentPos = AFTER_LAST;
- return null;
- }
- }
-
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean hasPrev()
- {
- return currentPos > 0 || currentPos == AFTER_LAST;
- }
-
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void close()
- {
- }
-
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void beforeFirst() throws IOException
- {
- currentPos = BEFORE_FIRST;
- }
-
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void afterLast() throws IOException
- {
- currentPos = AFTER_LAST;
- }
-
-
- /**
- * {@inheritDoc}
- */
- @Override
- public V prev() throws EndOfFileExceededException, IOException
- {
- switch ( currentPos )
- {
- case AFTER_LAST :
- currentPos = 0;
- return value;
-
- case BEFORE_FIRST :
- return null;
-
- default :
- currentPos = BEFORE_FIRST;
- return null;
- }
- }
-
-
- /**
- * {@inheritDoc}
- */
- @Override
- public int size()
- {
- return 1;
- }
-
-
- /**
- * @see Object#toString()
- */
- public String toString()
- {
- StringBuilder sb = new StringBuilder();
-
- sb.append( "SingletonCursor , currentpos =" );
-
- switch ( currentPos )
- {
- case BEFORE_FIRST :
- sb.append( "BEFORE_FIRST" );
- break;
-
- case AFTER_LAST :
- sb.append( "AFTER_LAST" );
- break;
-
- default :
- sb.append( "0/0" );
- break;
- }
-
- return sb.toString();
- }
- }
-
-
/**
* Creates a new instance of a ValueHolder, containing the serialized values.
*
- * @param btree the container BTree
+ * @param parentBtree the parent BTree
* @param valueSerializer The Value's serializer
* @param raw The raw data containing the values
* @param nbValues the number of stored values
* @param raw the byte[] containing either the serialized array of values or the sub-btree offset
*/
- /* No qualifier */ValueHolder( BTree<?, V> btree, int nbValues )
+ InMemoryValueHolder( BTree<?, V> parentBtree, int nbValues )
{
- this.btree = btree;
+ this.parentBtree = parentBtree;
}
@@ -218,12 +61,12 @@ public class ValueHolder<V> implements C
* Creates a new instance of a ValueHolder, containing Values. This constructor is called
* whe we need to create a new ValueHolder with deserialized values.
*
- * @param valueSerializer The Value's serializer
+ * @param parentBtree The parent BTree
* @param values The Values stored in the ValueHolder
*/
- /* No qualifier */ValueHolder( BTree<?, V> btree, V... values )
+ InMemoryValueHolder( BTree<?, V> parentBtree, V... values )
{
- this.btree = btree;
+ this.parentBtree = parentBtree;
if ( ( values != null ) && ( values.length > 0 ) )
{
@@ -232,7 +75,7 @@ public class ValueHolder<V> implements C
if ( nbValues < 2 )
{
// Store the value
- value = values[0];
+ valueArray = values;
}
else
{
@@ -269,7 +112,7 @@ public class ValueHolder<V> implements C
}
else
{
- cursor = new ValueSingletonCursor();
+ cursor = new ValueArrayCursor<V>( valueArray );
}
return cursor;
@@ -277,16 +120,7 @@ public class ValueHolder<V> implements C
/**
- * @return the isSubBtree
- */
- public boolean isSubBtree()
- {
- return valueBtree != null;
- }
-
-
- /**
- * @return the number of stored values
+ * {@inheritDoc}
*/
public int size()
{
@@ -327,7 +161,7 @@ public class ValueHolder<V> implements C
/* No qualifier*/void setSubBtree( BTree<V, V> subBtree )
{
valueBtree = subBtree;
- value = null;
+ valueArray = null;
}
@@ -339,12 +173,12 @@ public class ValueHolder<V> implements C
// Ok, create a sub-btree
try
{
- valueBtree = new InMemoryBTree<V, V>( UUID.randomUUID().toString(), btree.getValueSerializer(),
- btree.getValueSerializer() );
+ valueBtree = new InMemoryBTree<V, V>( UUID.randomUUID().toString(), parentBtree.getValueSerializer(),
+ parentBtree.getValueSerializer() );
- valueBtree.insert( value, null, 0 );
+ valueBtree.insert( valueArray[0], null, 0 );
valueBtree.insert( newValue, null, 0 );
- value = null;
+ valueArray = null;
}
catch ( IOException e )
{
@@ -354,22 +188,20 @@ public class ValueHolder<V> implements C
/**
- * Add a new value in the ValueHolder
- *
- * @param newValue The added value
+ * {@inheritDoc}
*/
public void add( V newValue )
{
- if ( value != null )
+ if ( ( valueArray != null ) && ( valueArray.length > 0 ) )
{
try
{
- valueBtree = new InMemoryBTree<V, V>( UUID.randomUUID().toString(), btree.getValueSerializer(),
- btree.getValueSerializer() );
+ valueBtree = new InMemoryBTree<V, V>( UUID.randomUUID().toString(), parentBtree.getValueSerializer(),
+ parentBtree.getValueSerializer() );
- valueBtree.insert( value, null, 0 );
+ valueBtree.insert( valueArray[0], null, 0 );
valueBtree.insert( newValue, null, 0 );
- value = null;
+ valueArray = null;
}
catch ( IOException e )
{
@@ -389,87 +221,102 @@ public class ValueHolder<V> implements C
}
else
{
- this.value = newValue;
+ this.valueArray[0] = newValue;
}
}
/**
- * Remove a value from the ValueHolder
- *
- * @param removedValue The value to remove
+ * {@inheritDoc}
*/
- public V remove( V removedValue )
+ public V remove( V value )
{
- if ( valueBtree == null )
+ V removedValue = null;
+
+ if ( valueArray != null )
{
- if ( removedValue == null )
- {
- return null;
- }
-
- Comparator<V> comparator = btree.getValueSerializer().getComparator();
+ removedValue = removeFromArray( value );
+ }
+ else
+ {
+ removedValue = removeFromBtree( value );
+ }
- int result = comparator.compare( removedValue, value );
+ return removedValue;
+ }
- if ( result != 0 )
- {
- // The value does not exists : nothing to do
- return null;
- }
- else
+
+ /**
+ * Remove the value from a sub btree
+ */
+ private V removeFromBtree( V removedValue )
+ {
+ V returnedValue = null;
+
+ try
+ {
+ Tuple<V, V> removedTuple = valueBtree.delete( removedValue );
+
+ if ( removedTuple != null )
{
- V returnedValue = value;
- value = null;
-
- return returnedValue;
+ returnedValue = removedTuple.getKey();
}
}
- else
+ catch ( IOException e )
+ {
+ throw new RuntimeException( e );
+ }
+
+ if ( valueBtree.getNbElems() == 1 )
{
- V returnedValue = null;
-
try
{
- Tuple<V, V> removedTuple = valueBtree.delete( removedValue );
-
- if ( removedTuple != null )
- {
- returnedValue = removedTuple.getKey();
- }
+ valueArray = ( V[] ) Array.newInstance( parentBtree.getValueSerializer().getType(), 1 );
+ valueArray[0] = valueBtree.browse().next().getKey();
+ valueBtree.close();
+ valueBtree = null;
}
- catch ( IOException e )
+ catch ( EndOfFileExceededException e )
{
throw new RuntimeException( e );
}
-
- if ( valueBtree.getNbElems() == 1 )
+ catch ( IOException e )
{
- try
- {
- value = valueBtree.browse().next().getKey();
- valueBtree.close();
- valueBtree = null;
- }
- catch ( EndOfFileExceededException e )
- {
- throw new RuntimeException( e );
- }
- catch ( IOException e )
- {
- throw new RuntimeException( e );
- }
+ throw new RuntimeException( e );
}
-
- return returnedValue;
}
+
+ return returnedValue;
}
/**
- * Check that a value exists in the ValueHolder
- *
- * @param checkedValue The value to check
+ * Remove a value from an array
+ */
+ private V removeFromArray( V value )
+ {
+ // First check that the value is not already present in the ValueHolder
+ Comparator<V> comparator = parentBtree.getValueSerializer().getComparator();
+
+ int result = comparator.compare( valueArray[0], value );
+
+ if ( result != 0 )
+ {
+ // The value does not exists : nothing to do
+ return null;
+ }
+ else
+ {
+ V returnedValue = valueArray[0];
+ valueArray[0] = null;
+
+ return returnedValue;
+ }
+ }
+
+
+ /**
+ * {@inheritDoc}
*/
public boolean contains( V checkedValue )
{
@@ -488,9 +335,9 @@ public class ValueHolder<V> implements C
}
else
{
- Comparator<V> comparator = btree.getValueSerializer().getComparator();
+ Comparator<V> comparator = parentBtree.getValueSerializer().getComparator();
- int result = comparator.compare( checkedValue, value );
+ int result = comparator.compare( checkedValue, valueArray[0] );
return result == 0;
}
@@ -498,47 +345,13 @@ public class ValueHolder<V> implements C
/**
- * Find the position of a given value in the array, or the position where we
- * would insert the element (in this case, the position will be negative).
- * As we use a 0-based array, the negative position for 0 is -1.
- * -1 means the element can be added in position 0
- * -2 means the element can be added in position 1
- * ...
- */
- private int findPos1( V findValue )
- {
- if ( findValue == null )
- {
- return -1;
- }
-
- Comparator<V> comparator = btree.getValueSerializer().getComparator();
-
- int result = comparator.compare( findValue, value );
-
- return result;
- }
-
-
- /**
- * Create a clone of this instance
- */
- public ValueHolder<V> clone() throws CloneNotSupportedException
- {
- ValueHolder<V> copy = ( ValueHolder<V> ) super.clone();
-
- return copy;
- }
-
-
- /**
* @see Object#toString()
*/
public String toString()
{
StringBuilder sb = new StringBuilder();
- sb.append( "ValueHolder[" ).append( btree.getValueSerializer().getClass().getSimpleName() );
+ sb.append( "ValueHolder[" ).append( parentBtree.getValueSerializer().getClass().getSimpleName() );
if ( valueBtree != null )
{
@@ -547,7 +360,7 @@ public class ValueHolder<V> implements C
else
{
sb.append( ", {" );
- sb.append( value );
+ sb.append( valueArray[0] );
sb.append( "}" );
}
Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/Leaf.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/Leaf.java?rev=1550714&r1=1550713&r2=1550714&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/Leaf.java (original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/Leaf.java Fri Dec 13 12:46:14 2013
@@ -53,7 +53,7 @@ import org.apache.directory.mavibot.btre
/* No qualifier */class Leaf<K, V> extends AbstractInMemoryPage<K, V>
{
/** Values associated with keys */
- protected ValueHolder<V>[] values;
+ protected InMemoryValueHolder<V>[] values;
/**
@@ -80,7 +80,7 @@ import org.apache.directory.mavibot.btre
{
super( btree, revision, nbElems );
- this.values = ( ValueHolder<V>[] ) Array.newInstance( ValueHolder.class, nbElems );
+ this.values = ( InMemoryValueHolder<V>[] ) Array.newInstance( InMemoryValueHolder.class, nbElems );
}
@@ -159,7 +159,7 @@ import org.apache.directory.mavibot.btre
int index = -( pos + 1 );
- ValueHolder<V> valueHolder = values[index];
+ InMemoryValueHolder<V> valueHolder = values[index];
if ( value == null )
{
@@ -363,7 +363,7 @@ import org.apache.directory.mavibot.btre
{
// The sibling is on the left, borrow the rightmost element
K siblingKey = sibling.getKey( sibling.getNbElems() - 1 );
- ValueHolder<V> siblingValue = sibling.values[sibling.getNbElems() - 1];
+ InMemoryValueHolder<V> siblingValue = sibling.values[sibling.getNbElems() - 1];
// Create the new sibling, with one less element at the end
Leaf<K, V> newSibling = ( Leaf<K, V> ) sibling.copy( revision, sibling.getNbElems() - 1 );
@@ -410,7 +410,7 @@ import org.apache.directory.mavibot.btre
{
// The sibling is on the left, borrow the rightmost element
K siblingKey = sibling.getKey( 0 );
- ValueHolder<V> siblingHolder = sibling.values[0];
+ InMemoryValueHolder<V> siblingHolder = sibling.values[0];
// Create the new sibling
Leaf<K, V> newSibling = new Leaf<K, V>( btree, revision, sibling.getNbElems() - 1 );
@@ -490,7 +490,7 @@ import org.apache.directory.mavibot.btre
if ( pos < 0 )
{
- ValueHolder<V> valueHolder = ( ValueHolder<V> ) values[-( pos + 1 )];
+ InMemoryValueHolder<V> valueHolder = ( InMemoryValueHolder<V> ) values[-( pos + 1 )];
V value = valueHolder.getCursor().next();
@@ -518,7 +518,7 @@ import org.apache.directory.mavibot.btre
if ( pos < 0 )
{
- ValueHolder<V> valueHolder = ( ValueHolder<V> ) values[-( pos + 1 )];
+ InMemoryValueHolder<V> valueHolder = ( InMemoryValueHolder<V> ) values[-( pos + 1 )];
return valueHolder.getCursor();
}
@@ -552,7 +552,7 @@ import org.apache.directory.mavibot.btre
if ( pos < 0 )
{
- ValueHolder<V> valueHolder = values[-( pos + 1 )];
+ InMemoryValueHolder<V> valueHolder = values[-( pos + 1 )];
return valueHolder.contains( value );
}
@@ -566,7 +566,7 @@ import org.apache.directory.mavibot.btre
/**
* {@inheritDoc}
*/
- public ValueHolder<V> getValue( int pos )
+ public InMemoryValueHolder<V> getValue( int pos )
{
if ( pos < nbElems )
{
@@ -584,7 +584,7 @@ import org.apache.directory.mavibot.btre
* @param pos The position in the values array
* @param value the value to inject
*/
- public void setValue( int pos, ValueHolder<V> value )
+ public void setValue( int pos, InMemoryValueHolder<V> value )
{
values[pos] = value;
}
@@ -734,7 +734,7 @@ import org.apache.directory.mavibot.btre
}
// Get the previous value from the leaf (it's a copy)
- ValueHolder<V> valueHolder = newLeaf.values[pos];
+ InMemoryValueHolder<V> valueHolder = newLeaf.values[pos];
V replacedValue = null;
if ( !valueHolder.contains( value ) )
@@ -775,7 +775,7 @@ import org.apache.directory.mavibot.btre
Leaf<K, V> newLeaf = new Leaf<K, V>( btree, revision, nbElems + 1 );
// Atm, store the value in memory
- ValueHolder<V> valueHolder = new ValueHolder<V>( btree, value );
+ InMemoryValueHolder<V> valueHolder = new InMemoryValueHolder<V>( btree, value );
// Deal with the special case of an empty page
if ( nbElems == 0 )
@@ -822,7 +822,7 @@ import org.apache.directory.mavibot.btre
int middle = btree.getPageSize() >> 1;
Leaf<K, V> leftLeaf = null;
Leaf<K, V> rightLeaf = null;
- ValueHolder<V> valueHolder = new ValueHolder<V>( btree, value );
+ InMemoryValueHolder<V> valueHolder = new InMemoryValueHolder<V>( btree, value );
// Determinate where to store the new value
if ( pos <= middle )
Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/TupleCursorImpl.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/TupleCursorImpl.java?rev=1550714&r1=1550713&r2=1550714&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/TupleCursorImpl.java (original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/TupleCursorImpl.java Fri Dec 13 12:46:14 2013
@@ -260,7 +260,7 @@ public class TupleCursorImpl<K, V> exten
parentPos = stack[depth];
parentPos.pos = child.getNbElems() - 1;
parentPos.page = child;
- ValueHolder<V> valueHolder = ((Leaf<K, V>)parentPos.page).values[parentPos.pos];
+ InMemoryValueHolder<V> valueHolder = ((Leaf<K, V>)parentPos.page).values[parentPos.pos];
parentPos.valueCursor = valueHolder.getCursor();
parentPos.valueCursor.afterLast();
@@ -629,7 +629,7 @@ public class TupleCursorImpl<K, V> exten
try
{
- ValueHolder<V> valueHolder = ( ( Leaf<K, V> ) parentPos.page ).getValue( parentPos.pos );
+ InMemoryValueHolder<V> valueHolder = ( ( Leaf<K, V> ) parentPos.page ).getValue( parentPos.pos );
parentPos.valueCursor = valueHolder.getCursor();
@@ -681,7 +681,7 @@ public class TupleCursorImpl<K, V> exten
{
// This is the end : no more value
Leaf<K, V> leaf = ( Leaf<K, V> ) ( parentPos.page );
- ValueHolder<V> valueHolder = leaf.values[parentPos.pos];
+ InMemoryValueHolder<V> valueHolder = leaf.values[parentPos.pos];
parentPos.pos = AFTER_LAST;
parentPos.valueCursor = valueHolder.getCursor();
parentPos.valueCursor.afterLast();
@@ -704,7 +704,7 @@ public class TupleCursorImpl<K, V> exten
tuple.setKey( leaf.getKey( parentPos.pos ) );
// The value
- ValueHolder<V> valueHolder = leaf.values[parentPos.pos];
+ InMemoryValueHolder<V> valueHolder = leaf.values[parentPos.pos];
parentPos.valueCursor = valueHolder.getCursor();
V value = parentPos.valueCursor.next();
tuple.setValue( value );
@@ -777,7 +777,7 @@ public class TupleCursorImpl<K, V> exten
try
{
- ValueHolder<V> valueHolder = ( ( Leaf<K, V> ) parentPos.page ).getValue( parentPos.pos );
+ InMemoryValueHolder<V> valueHolder = ( ( Leaf<K, V> ) parentPos.page ).getValue( parentPos.pos );
parentPos.valueCursor = valueHolder.getCursor();
parentPos.valueCursor.afterLast();
@@ -851,7 +851,7 @@ public class TupleCursorImpl<K, V> exten
tuple.setKey( leaf.getKey( parentPos.pos ) );
// The value
- ValueHolder<V> valueHolder = leaf.values[parentPos.pos];
+ InMemoryValueHolder<V> valueHolder = leaf.values[parentPos.pos];
parentPos.valueCursor = valueHolder.getCursor();
V value = parentPos.valueCursor.next();
tuple.setValue( value );
Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/BTreeFactory.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/BTreeFactory.java?rev=1550714&r1=1550713&r2=1550714&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/BTreeFactory.java (original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/BTreeFactory.java Fri Dec 13 12:46:14 2013
@@ -26,6 +26,7 @@ import java.util.LinkedList;
import org.apache.directory.mavibot.btree.BTree;
import org.apache.directory.mavibot.btree.Page;
import org.apache.directory.mavibot.btree.ParentPos;
+import org.apache.directory.mavibot.btree.ValueHolder;
import org.apache.directory.mavibot.btree.serializer.ElementSerializer;
@@ -254,7 +255,7 @@ public class BTreeFactory
* @param pos The position in the values array
* @param value the value to inject
*/
- public static <K, V> void setValue( Leaf<K, V> page, int pos, ValueHolder<V> value )
+ public static <K, V> void setValue( Leaf<K, V> page, int pos, PersistedValueHolder<V> value )
{
page.setValue( pos, value );
}
Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/Leaf.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/Leaf.java?rev=1550714&r1=1550713&r2=1550714&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/Leaf.java (original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/Leaf.java Fri Dec 13 12:46:14 2013
@@ -39,6 +39,7 @@ import org.apache.directory.mavibot.btre
import org.apache.directory.mavibot.btree.Tuple;
import org.apache.directory.mavibot.btree.TupleCursor;
import org.apache.directory.mavibot.btree.ValueCursor;
+import org.apache.directory.mavibot.btree.ValueHolder;
import org.apache.directory.mavibot.btree.exception.EndOfFileExceededException;
import org.apache.directory.mavibot.btree.exception.KeyNotFoundException;
@@ -80,7 +81,7 @@ import org.apache.directory.mavibot.btre
/* No qualifier */Leaf( BTree<K, V> btree, long revision, int nbElems )
{
super( btree, revision, nbElems );
- values = ( ValueHolder<V>[] ) Array.newInstance( ValueHolder.class, nbElems );
+ values = ( ValueHolder<V>[] ) Array.newInstance( PersistedValueHolder.class, nbElems );
}
@@ -273,7 +274,7 @@ import org.apache.directory.mavibot.btre
// Replace the ValueHolder now
try
{
- ValueHolder<V> newValueHolder = (ValueHolder<V>)valueHolder.clone();
+ ValueHolder<V> newValueHolder = valueHolder.clone();
newValueHolder.remove( value );
newLeaf.values[pos] = newValueHolder;
@@ -488,7 +489,7 @@ import org.apache.directory.mavibot.btre
try
{
- ValueHolder<V> newValueHolder = (ValueHolder<V>)valueHolder.clone();
+ ValueHolder<V> newValueHolder = valueHolder.clone();
newValueHolder.remove( removedValue );
@@ -846,7 +847,7 @@ import org.apache.directory.mavibot.btre
Leaf<K, V> newLeaf = new Leaf<K, V>( btree, revision, nbElems + 1 );
// Create the value holder
- ValueHolder<V> valueHolder = new ValueHolder<V>( btree, value );
+ ValueHolder<V> valueHolder = new PersistedValueHolder<V>( btree, value );
// Deal with the special case of an empty page
if ( nbElems == 0 )
@@ -894,7 +895,7 @@ import org.apache.directory.mavibot.btre
int middle = btree.getPageSize() >> 1;
Leaf<K, V> leftLeaf = null;
Leaf<K, V> rightLeaf = null;
- ValueHolder<V> valueHolder = new ValueHolder<V>( btree, value );
+ ValueHolder<V> valueHolder = new PersistedValueHolder<V>( btree, value );
// Determinate where to store the new value
if ( pos <= middle )
Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/PersistedBTreeBuilder.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/PersistedBTreeBuilder.java?rev=1550714&r1=1550713&r2=1550714&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/PersistedBTreeBuilder.java (original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/PersistedBTreeBuilder.java Fri Dec 13 12:46:14 2013
@@ -37,6 +37,7 @@ import org.apache.directory.mavibot.btre
import org.apache.directory.mavibot.btree.KeyHolder;
import org.apache.directory.mavibot.btree.Page;
import org.apache.directory.mavibot.btree.Tuple;
+import org.apache.directory.mavibot.btree.ValueHolder;
import org.apache.directory.mavibot.btree.serializer.ElementSerializer;
@@ -91,7 +92,7 @@ public class PersistedBTreeBuilder<K, V>
setKey( leaf1, leafIndex, tuple.getKey() );
- ValueHolder<V> eh = new ValueHolder<V>( btree, tuple.getValue() );
+ PersistedValueHolder<V> eh = new PersistedValueHolder<V>( btree, tuple.getValue() );
setValue( leaf1, leafIndex, eh );
@@ -129,7 +130,7 @@ public class PersistedBTreeBuilder<K, V>
System.arraycopy( keys, 0, lastLeaf.getKeys(), 0, n );
ValueHolder<V>[] values = lastLeaf.values;
- lastLeaf.values = ( ValueHolder<V>[] ) Array.newInstance( ValueHolder.class, n );
+ lastLeaf.values = ( PersistedValueHolder<V>[] ) Array.newInstance( PersistedValueHolder.class, n );
System.arraycopy( values, 0, lastLeaf.values, 0, n );
PageHolder<K, V> pageHolder = ( PageHolder<K, V> ) rm.writePage( btree, lastLeaf, 1 );
Copied: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/PersistedValueHolder.java (from r1550678, directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/ValueHolder.java)
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/PersistedValueHolder.java?p2=directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/PersistedValueHolder.java&p1=directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/ValueHolder.java&r1=1550678&r2=1550714&rev=1550714&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/ValueHolder.java (original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/PersistedValueHolder.java Fri Dec 13 12:46:14 2013
@@ -25,13 +25,15 @@ import java.lang.reflect.Array;
import java.util.Comparator;
import java.util.UUID;
+import org.apache.directory.mavibot.btree.AbstractValueHolder;
import org.apache.directory.mavibot.btree.BTree;
import org.apache.directory.mavibot.btree.Tuple;
import org.apache.directory.mavibot.btree.TupleCursor;
+import org.apache.directory.mavibot.btree.ValueArrayCursor;
import org.apache.directory.mavibot.btree.ValueBTreeCursor;
import org.apache.directory.mavibot.btree.ValueCursor;
+import org.apache.directory.mavibot.btree.ValueHolder;
import org.apache.directory.mavibot.btree.exception.BTreeAlreadyManagedException;
-import org.apache.directory.mavibot.btree.exception.EndOfFileExceededException;
import org.apache.directory.mavibot.btree.serializer.ElementSerializer;
import org.apache.directory.mavibot.btree.serializer.IntSerializer;
import org.apache.directory.mavibot.btree.serializer.LongSerializer;
@@ -43,14 +45,8 @@ import org.apache.directory.mavibot.btre
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
* @param <V> The value type
*/
-public class ValueHolder<V> implements Cloneable
+public class PersistedValueHolder<V> extends AbstractValueHolder<V>
{
- /** The deserialized value */
- private V[] valueArray;
-
- /** The BTree storing multiple value, if we have more than a threshold values */
- private BTree<V, V> valueBtree;
-
/** The serialized value */
private byte[] raw;
@@ -60,9 +56,6 @@ public class ValueHolder<V> implements C
/** A flag to signal that the raw value represent the serialized values in their last state */
private boolean isRawUpToDate = false;
- /** The parent BTree */
- private BTree<?, V> btree;
-
/** The Value serializer */
private ElementSerializer<V> valueSerializer;
@@ -70,15 +63,15 @@ public class ValueHolder<V> implements C
/**
* Creates a new instance of a ValueHolder, containing the serialized values.
*
- * @param btree the container BTree
+ * @param parentBtree the parent BTree
* @param raw The raw data containing the values
* @param nbValues the number of stored values
* @param raw the byte[] containing either the serialized array of values or the sub-btree offset
*/
- /* No qualifier */ValueHolder( BTree<?, V> btree, int nbValues, byte[] raw )
+ PersistedValueHolder( BTree<?, V> parentBtree, int nbValues, byte[] raw )
{
- this.btree = btree;
- this.valueSerializer = btree.getValueSerializer();
+ this.parentBtree = parentBtree;
+ this.valueSerializer = parentBtree.getValueSerializer();
this.raw = raw;
isRawUpToDate = true;
@@ -96,13 +89,13 @@ public class ValueHolder<V> implements C
* Creates a new instance of a ValueHolder, containing Values. This constructor is called
* whe we need to create a new ValueHolder with deserialized values.
*
- * @param valueSerializer The Value's serializer
+ * @param parentBtree The parent BTree
* @param values The Values stored in the ValueHolder
*/
- /* No qualifier */ValueHolder( BTree<?, V> btree, V... values )
+ PersistedValueHolder( BTree<?, V> parentBtree, V... values )
{
- this.btree = btree;
- this.valueSerializer = btree.getValueSerializer();
+ this.parentBtree = parentBtree;
+ this.valueSerializer = parentBtree.getValueSerializer();
if ( values != null )
{
@@ -168,7 +161,7 @@ public class ValueHolder<V> implements C
}
else
{
- cursor = new ValueArrayCursor();
+ cursor = new ValueArrayCursor<V>( valueArray );
}
return cursor;
@@ -176,148 +169,6 @@ public class ValueHolder<V> implements C
/**
- * A class that encapsulate the values into an array
- */
- private class ValueArrayCursor implements ValueCursor<V>
- {
- /** Store the current position in the array or in the BTree */
- private int currentPos;
-
-
- /**
- * Create an instance
- */
- private ValueArrayCursor()
- {
- // Start at -1 to be positioned before the first element
- currentPos = BEFORE_FIRST;
- }
-
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean hasNext()
- {
- return ( currentPos < valueArray.length - 1 ) && ( currentPos != AFTER_LAST );
- }
-
-
- /**
- * {@inheritDoc}
- */
- public V next()
- {
- if ( valueArray == null )
- {
- // Deserialize the array
- return null;
- }
- else
- {
- currentPos++;
-
- if ( currentPos == valueArray.length )
- {
- currentPos = AFTER_LAST;
-
- // We have reached the end of the array
- return null;
- }
- else
- {
- return valueArray[currentPos];
- }
- }
- }
-
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean hasPrev() throws EndOfFileExceededException, IOException
- {
- return currentPos > 0 || currentPos == AFTER_LAST;
- }
-
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void close()
- {
- }
-
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void beforeFirst() throws IOException
- {
- currentPos = BEFORE_FIRST;
- }
-
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void afterLast() throws IOException
- {
- currentPos = AFTER_LAST;
- }
-
-
- /**
- * {@inheritDoc}
- */
- @Override
- public V prev() throws EndOfFileExceededException, IOException
- {
- if ( valueArray == null )
- {
- // Deserialize the array
- return null;
- }
- else
- {
- if ( currentPos == AFTER_LAST )
- {
- currentPos = valueArray.length - 1;
- }
- else
- {
- currentPos--;
- }
-
- if ( currentPos == BEFORE_FIRST )
- {
- // We have reached the end of the array
- return null;
- }
- else
- {
- return valueArray[currentPos];
- }
- }
- }
-
-
- /**
- * {@inheritDoc}
- */
- @Override
- public int size()
- {
- return valueArray.length;
- }
- }
-
- /**
* @return the raw representation of the value holder. The serialized value will not be the same
* if the values are stored in an array or in a btree. <br/>
* If they are stored in a BTree, the raw value will contain the offset of the btree, otherwise
@@ -381,16 +232,7 @@ public class ValueHolder<V> implements C
/**
- * @return the isSubBtree
- */
- public boolean isSubBtree()
- {
- return valueArray == null;
- }
-
-
- /**
- * @return the number of stored values
+ * {@inheritDoc}
*/
public int size()
{
@@ -419,14 +261,14 @@ public class ValueHolder<V> implements C
configuration.setKeySerializer( valueSerializer );
configuration.setName( UUID.randomUUID().toString() );
configuration.setValueSerializer( valueSerializer );
- configuration.setParentBTree( btree );
+ configuration.setParentBTree( parentBtree );
configuration.setSubBtree( true );
valueBtree = BTreeFactory.createBTree( configuration );
try
{
- ((PersistedBTree<V, V>)btree).getRecordManager().manage( valueBtree, RecordManager.INTERNAL_BTREE );
+ ((PersistedBTree<V, V>)parentBtree).getRecordManager().manage( valueBtree, RecordManager.INTERNAL_BTREE );
raw = null;
}
catch ( BTreeAlreadyManagedException e )
@@ -560,9 +402,7 @@ public class ValueHolder<V> implements C
/**
- * Add a new value in the ValueHolder
- *
- * @param value The added value
+ * {@inheritDoc}
*/
public void add( V value )
{
@@ -684,10 +524,9 @@ public class ValueHolder<V> implements C
}
}
+
/**
- * Add a new value in the ValueHolder
- *
- * @param value The added value
+ * {@inheritDoc}
*/
public V remove( V value )
{
@@ -750,19 +589,17 @@ public class ValueHolder<V> implements C
/**
- * Add a new value in the ValueHolder
- *
- * @param value The added value
+ * {@inheritDoc}
*/
- public boolean contains( V value )
+ public boolean contains( V checkedValue )
{
if ( valueArray == null )
{
- return btreeContains( value );
+ return btreeContains( checkedValue );
}
else
{
- return arrayContains( value );
+ return arrayContains( checkedValue );
}
}
@@ -899,7 +736,7 @@ public class ValueHolder<V> implements C
*/
public ValueHolder<V> clone() throws CloneNotSupportedException
{
- ValueHolder<V> copy = ( ValueHolder<V> ) super.clone();
+ PersistedValueHolder<V> copy = ( PersistedValueHolder<V> ) super.clone();
// copy the valueArray if it's not null
// We don't clone the BTree, as we will create new revisions when
@@ -959,7 +796,7 @@ public class ValueHolder<V> implements C
long offset = LongSerializer.deserialize( raw );
// and reload the sub btree
- valueBtree = ((PersistedBTree<V, V>)btree).getRecordManager().loadDupsBTree( offset );
+ valueBtree = ((PersistedBTree<V, V>)parentBtree).getRecordManager().loadDupsBTree( offset );
}
Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/RecordManager.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/RecordManager.java?rev=1550714&r1=1550713&r2=1550714&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/RecordManager.java (original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/RecordManager.java Fri Dec 13 12:46:14 2013
@@ -36,6 +36,7 @@ import java.util.concurrent.atomic.Atomi
import org.apache.directory.mavibot.btree.BTree;
import org.apache.directory.mavibot.btree.KeyHolder;
import org.apache.directory.mavibot.btree.Page;
+import org.apache.directory.mavibot.btree.ValueHolder;
import org.apache.directory.mavibot.btree.exception.BTreeAlreadyManagedException;
import org.apache.directory.mavibot.btree.exception.EndOfFileExceededException;
import org.apache.directory.mavibot.btree.exception.KeyNotFoundException;
@@ -636,7 +637,7 @@ public class RecordManager
{
// Read the number of values
int nbValues = byteBuffer.getInt();
- ValueHolder<V> valueHolder = null;
+ PersistedValueHolder<V> valueHolder = null;
if ( nbValues < 0 )
{
@@ -646,7 +647,7 @@ public class RecordManager
// Create the valueHolder. As the number of values is negative, we have to switch
// to a positive value but as we start at -1 for 0 value, add 1.
- valueHolder = new ValueHolder<V>( btree, 1 - nbValues, btreeOffsetBytes );
+ valueHolder = new PersistedValueHolder<V>( btree, 1 - nbValues, btreeOffsetBytes );
}
else
{
@@ -657,7 +658,7 @@ public class RecordManager
// This is an Array of values, read the byte[] associated with it
byte[] arrayBytes = new byte[valueLengths[i]];
byteBuffer.get( arrayBytes );
- valueHolder = new ValueHolder<V>( btree, nbValues, arrayBytes );
+ valueHolder = new PersistedValueHolder<V>( btree, nbValues, arrayBytes );
}
BTreeFactory.setValue( leaf, i, valueHolder );
@@ -1327,7 +1328,7 @@ public class RecordManager
dataSize = INT_SIZE;
// We have a serialized value. Just flush it
- byte[] data = valueHolder.getRaw();
+ byte[] data = ((PersistedValueHolder<V>)valueHolder).getRaw();
dataSize += data.length;
// Store the data size
@@ -1360,7 +1361,7 @@ public class RecordManager
dataSize += buffer.length;
// the BTree offset
- buffer = LongSerializer.serialize( valueHolder.getOffset() );
+ buffer = LongSerializer.serialize( ((PersistedValueHolder<V>)valueHolder).getOffset() );
serializedData.add( buffer );
dataSize += buffer.length;
}
@@ -1372,7 +1373,7 @@ public class RecordManager
dataSize += buffer.length;
// Now store each value
- byte[] data = valueHolder.getRaw();
+ byte[] data = ((PersistedValueHolder<V>)valueHolder).getRaw();
buffer = IntSerializer.serialize( data.length );
serializedData.add( buffer );
dataSize += buffer.length;
Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/TupleCursorImpl.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/TupleCursorImpl.java?rev=1550714&r1=1550713&r2=1550714&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/TupleCursorImpl.java (original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/TupleCursorImpl.java Fri Dec 13 12:46:14 2013
@@ -30,6 +30,7 @@ import org.apache.directory.mavibot.btre
import org.apache.directory.mavibot.btree.Transaction;
import org.apache.directory.mavibot.btree.Tuple;
import org.apache.directory.mavibot.btree.TupleCursor;
+import org.apache.directory.mavibot.btree.ValueHolder;
import org.apache.directory.mavibot.btree.exception.EndOfFileExceededException;
Modified: directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/memory/InMemoryBTreeTest.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/memory/InMemoryBTreeTest.java?rev=1550714&r1=1550713&r2=1550714&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/memory/InMemoryBTreeTest.java (original)
+++ directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/memory/InMemoryBTreeTest.java Fri Dec 13 12:46:14 2013
@@ -1049,13 +1049,13 @@ public class InMemoryBTreeTest
leaf.setRevision( revision );
leaf.setNbElems( tuples.length );
leaf.setKeys( new KeyHolder[leaf.getNbElems()] );
- leaf.values = ( ValueHolder<String>[] ) Array
- .newInstance( ValueHolder.class, leaf.getNbElems() );
+ leaf.values = ( InMemoryValueHolder<String>[] ) Array
+ .newInstance( InMemoryValueHolder.class, leaf.getNbElems() );
for ( Tuple<Integer, String> tuple : tuples )
{
leaf.setKey( pos, tuple.getKey() );
- leaf.values[pos] = new ValueHolder<String>( btree, tuple.getValue() );
+ leaf.values[pos] = new InMemoryValueHolder<String>( btree, tuple.getValue() );
pos++;
}
Copied: directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/persisted/PersistedBTreeBrowseTest.java (from r1550678, directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/persisted/ManagedBTreeBrowseTest.java)
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/persisted/PersistedBTreeBrowseTest.java?p2=directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/persisted/PersistedBTreeBrowseTest.java&p1=directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/persisted/ManagedBTreeBrowseTest.java&r1=1550678&r2=1550714&rev=1550714&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/persisted/ManagedBTreeBrowseTest.java (original)
+++ directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/persisted/PersistedBTreeBrowseTest.java Fri Dec 13 12:46:14 2013
@@ -50,7 +50,7 @@ import org.junit.rules.TemporaryFolder;
*
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
*/
-public class ManagedBTreeBrowseTest
+public class PersistedBTreeBrowseTest
{
private BTree<Long, String> btree = null;
Copied: directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/persisted/PersistedBTreeBuilderTest.java (from r1550678, directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/persisted/ManagedBTreeBuilderTest.java)
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/persisted/PersistedBTreeBuilderTest.java?p2=directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/persisted/PersistedBTreeBuilderTest.java&p1=directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/persisted/ManagedBTreeBuilderTest.java&r1=1550678&r2=1550714&rev=1550714&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/persisted/ManagedBTreeBuilderTest.java (original)
+++ directory/mavibot/trunk/mavibot/src/test/java/org/apache/directory/mavibot/btree/persisted/PersistedBTreeBuilderTest.java Fri Dec 13 12:46:14 2013
@@ -41,7 +41,7 @@ import org.junit.Test;
*
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
*/
-public class ManagedBTreeBuilderTest
+public class PersistedBTreeBuilderTest
{
@Test