You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by ak...@apache.org on 2007/01/26 07:20:19 UTC

svn commit: r500151 [1/2] - in /incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree: ./ jdbm/

Author: akarasulu
Date: Thu Jan 25 22:20:18 2007
New Revision: 500151

URL: http://svn.apache.org/viewvc?view=rev&rev=500151
Log:
adding basic jdbm based property store

Added:
    incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/
    incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/ArrayIterator.java
    incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/BTreeEnumeration.java
    incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/BTreeIterator.java
    incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/BTreeRedirect.java
    incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/BTreeTupleEnumeration.java
    incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/DupsEnumeration.java
    incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/EmptyEnumeration.java
    incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/EmptyTupleCursor.java
    incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/JdbmBTreeCursor.java
    incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/JdbmIndex.java
    incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/JdbmMasterTable.java
    incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/JdbmStore.java
    incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/JdbmStoreConfiguration.java
    incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/JdbmTable.java
    incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/JdbmTupleBrowser.java
    incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/SingletonEnumeration.java
    incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/SingletonTupleCursor.java
Modified:
    incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/IndexConfiguration.java
    incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/Table.java

Modified: incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/IndexConfiguration.java
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/IndexConfiguration.java?view=diff&rev=500151&r1=500150&r2=500151
==============================================================================
--- incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/IndexConfiguration.java (original)
+++ incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/IndexConfiguration.java Thu Jan 25 22:20:18 2007
@@ -20,6 +20,9 @@
 package org.apache.felix.bundledb.btree;
 
 
+import java.util.Comparator;
+
+
 /**
  * An immutable configuration object for partition indices on entry attributes.
  *
@@ -34,6 +37,7 @@
     private String propertyId;
     private int cacheSize = DEFAULT_INDEX_CACHE_SIZE;
     private int duplicateLimit = DEFAULT_DUPLICATE_LIMIT;
+    private Comparator comparator;
     
     
     protected void setPropertyId( String attributeId )
@@ -81,5 +85,17 @@
     public String toString()
     {
         return this.propertyId;
+    }
+
+    
+    public void setComparator( Comparator comparator )
+    {
+        this.comparator = comparator;
+    }
+    
+
+    public Comparator getComparator()
+    {
+        return comparator;
     }
 }

Modified: incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/Table.java
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/Table.java?view=diff&rev=500151&r1=500150&r2=500151
==============================================================================
--- incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/Table.java (original)
+++ incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/Table.java Thu Jan 25 22:20:18 2007
@@ -248,7 +248,7 @@
      * @param key the key to iterate over
      * @throws IOException if the underlying browser could not be set
      */
-    TupleCursor listValues( Object key ) throws IOException;
+    Enumeration listValues( Object key ) throws IOException;
 
 
     // ------------------------------------------------------------------------

Added: incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/ArrayIterator.java
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/ArrayIterator.java?view=auto&rev=500151
==============================================================================
--- incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/ArrayIterator.java (added)
+++ incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/ArrayIterator.java Thu Jan 25 22:20:18 2007
@@ -0,0 +1,246 @@
+/*
+ *  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.felix.bundledb.btree.jdbm;
+
+
+import java.lang.reflect.Array;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+
+/** 
+ * Implements an {@link java.util.Iterator Iterator} over any array.
+ * <p>
+ * The array can be either an array of object or of primitives. If you know 
+ * that you have an object array, the 
+ * {@link org.apache.commons.collections.iterators.ObjectArrayIterator ObjectArrayIterator}
+ * class is a better choice, as it will perform better.
+ * <p>
+ * The iterator implements a {@link #reset} method, allowing the reset of 
+ * the iterator back to the start if required.
+ *
+ * @since Commons Collections 1.0
+ * @version $Revision$ $Date$
+ *
+ * @author James Strachan
+ * @author Mauricio S. Moura
+ * @author Michael A. Smith
+ * @author Neil O'Toole
+ * @author Stephen Colebourne
+ */
+public class ArrayIterator implements Iterator
+{
+
+    /** The array to iterate over */
+    protected Object array;
+    /** The start index to loop from */
+    protected int startIndex = 0;
+    /** The end index to loop to */
+    protected int endIndex = 0;
+    /** The current iterator index */
+    protected int index = 0;
+
+
+    // Constructors
+    // ----------------------------------------------------------------------
+    /**
+     * Constructor for use with <code>setArray</code>.
+     * <p>
+     * Using this constructor, the iterator is equivalent to an empty iterator
+     * until {@link #setArray(Object)} is  called to establish the array to iterate over.
+     */
+    public ArrayIterator()
+    {
+        super();
+    }
+
+
+    /**
+     * Constructs an ArrayIterator that will iterate over the values in the
+     * specified array.
+     *
+     * @param array the array to iterate over.
+     * @throws IllegalArgumentException if <code>array</code> is not an array.
+     * @throws NullPointerException if <code>array</code> is <code>null</code>
+     */
+    public ArrayIterator( final Object array )
+    {
+        super();
+        setArray( array );
+    }
+
+
+    /**
+     * Constructs an ArrayIterator that will iterate over the values in the
+     * specified array from a specific start index.
+     *
+     * @param array  the array to iterate over.
+     * @param startIndex  the index to start iterating at.
+     * @throws IllegalArgumentException if <code>array</code> is not an array.
+     * @throws NullPointerException if <code>array</code> is <code>null</code>
+     * @throws IndexOutOfBoundsException if the index is invalid
+     */
+    public ArrayIterator( final Object array, final int startIndex )
+    {
+        super();
+        setArray( array );
+        checkBound( startIndex, "start" );
+        this.startIndex = startIndex;
+        this.index = startIndex;
+    }
+
+
+    /**
+     * Construct an ArrayIterator that will iterate over a range of values 
+     * in the specified array.
+     *
+     * @param array  the array to iterate over.
+     * @param startIndex  the index to start iterating at.
+     * @param endIndex  the index to finish iterating at.
+     * @throws IllegalArgumentException if <code>array</code> is not an array.
+     * @throws NullPointerException if <code>array</code> is <code>null</code>
+     * @throws IndexOutOfBoundsException if either index is invalid
+     */
+    public ArrayIterator( final Object array, final int startIndex, final int endIndex )
+    {
+        super();
+        setArray( array );
+        checkBound( startIndex, "start" );
+        checkBound( endIndex, "end" );
+        if ( endIndex < startIndex )
+        {
+            throw new IllegalArgumentException( "End index must not be less than start index." );
+        }
+        this.startIndex = startIndex;
+        this.endIndex = endIndex;
+        this.index = startIndex;
+    }
+
+
+    /**
+     * Checks whether the index is valid or not.
+     * 
+     * @param bound  the index to check
+     * @param type  the index type (for error messages)
+     * @throws IndexOutOfBoundsException if the index is invalid
+     */
+    protected void checkBound( final int bound, final String type )
+    {
+        if ( bound > this.endIndex )
+        {
+            throw new ArrayIndexOutOfBoundsException( "Attempt to make an ArrayIterator that " + type
+                + "s beyond the end of the array. " );
+        }
+        if ( bound < 0 )
+        {
+            throw new ArrayIndexOutOfBoundsException( "Attempt to make an ArrayIterator that " + type
+                + "s before the start of the array. " );
+        }
+    }
+
+
+    // Iterator interface
+    //-----------------------------------------------------------------------
+    /**
+     * Returns true if there are more elements to return from the array.
+     *
+     * @return true if there is a next element to return
+     */
+    public boolean hasNext()
+    {
+        return ( index < endIndex );
+    }
+
+
+    /**
+     * Returns the next element in the array.
+     *
+     * @return the next element in the array
+     * @throws NoSuchElementException if all the elements in the array
+     *  have already been returned
+     */
+    public Object next()
+    {
+        if ( hasNext() == false )
+        {
+            throw new NoSuchElementException();
+        }
+        return Array.get( array, index++ );
+    }
+
+
+    /**
+     * Throws {@link UnsupportedOperationException}.
+     *
+     * @throws UnsupportedOperationException always
+     */
+    public void remove()
+    {
+        throw new UnsupportedOperationException( "remove() method is not supported" );
+    }
+
+
+    // Properties
+    //-----------------------------------------------------------------------
+    /**
+     * Gets the array that this iterator is iterating over. 
+     *
+     * @return the array this iterator iterates over, or <code>null</code> if
+     *  the no-arg constructor was used and {@link #setArray(Object)} has never
+     *  been called with a valid array.
+     */
+    public Object getArray()
+    {
+        return array;
+    }
+
+
+    /**
+     * Sets the array that the ArrayIterator should iterate over.
+     * <p>
+     * If an array has previously been set (using the single-arg constructor
+     * or this method) then that array is discarded in favour of this one.
+     * Iteration is restarted at the start of the new array.
+     * Although this can be used to reset iteration, the {@link #reset()} method
+     * is a more effective choice.
+     *
+     * @param array the array that the iterator should iterate over.
+     * @throws IllegalArgumentException if <code>array</code> is not an array.
+     * @throws NullPointerException if <code>array</code> is <code>null</code>
+     */
+    public void setArray( final Object array )
+    {
+        // Array.getLength throws IllegalArgumentException if the object is not
+        // an array or NullPointerException if the object is null.  This call
+        // is made before saving the array and resetting the index so that the
+        // array iterator remains in a consistent state if the argument is not
+        // an array or is null.
+        this.endIndex = Array.getLength( array );
+        this.startIndex = 0;
+        this.array = array;
+        this.index = 0;
+    }
+
+
+    /**
+     * Resets the iterator back to the start index.
+     */
+    public void reset()
+    {
+        this.index = this.startIndex;
+    }
+}

Added: incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/BTreeEnumeration.java
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/BTreeEnumeration.java?view=auto&rev=500151
==============================================================================
--- incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/BTreeEnumeration.java (added)
+++ incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/BTreeEnumeration.java Thu Jan 25 22:20:18 2007
@@ -0,0 +1,91 @@
+/*
+ *  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.felix.bundledb.btree.jdbm;
+
+
+import java.io.IOException;
+import java.util.NoSuchElementException;
+
+import jdbm.btree.BTree;
+import jdbm.helper.Tuple;
+import jdbm.helper.TupleBrowser;
+
+
+/**
+ * A Cursor that returns keys in a BTree.  This is used specifically 
+ * for situations when tables support duplicate keys and a BTree is used for 
+ * storing the values for that key.  This cursor thus advances a browser forwards 
+ * returning keys from a BTree as values.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class BTreeEnumeration implements JdbmBTreeCursor
+{
+    private final Tuple jdbmTuple = new jdbm.helper.Tuple();
+    private TupleBrowser browser;
+    private boolean success = false;
+    
+    
+    BTreeEnumeration( BTree tree ) throws IOException 
+    {
+        browser = tree.browse();
+        prefetch();
+    }
+
+    
+    private void prefetch() throws IOException
+    {
+        success = browser.getNext( jdbmTuple );
+    }
+    
+    
+    public void close()
+    {
+        success = false;
+    }
+
+
+    public boolean hasMoreElements()
+    {
+        return success;
+    }
+
+
+    public Object nextElement()
+    {
+        if ( ! success )
+        {
+            throw new NoSuchElementException();
+        }
+        
+        Object next = jdbmTuple.getKey();
+        
+        try
+        {
+            prefetch();
+        }
+        catch( IOException ioe ) 
+        {
+            throw new NoSuchElementException( "Failed due to underlying IOException: " + ioe.getMessage() );
+        }
+        return next;
+    }
+}

Added: incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/BTreeIterator.java
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/BTreeIterator.java?view=auto&rev=500151
==============================================================================
--- incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/BTreeIterator.java (added)
+++ incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/BTreeIterator.java Thu Jan 25 22:20:18 2007
@@ -0,0 +1,108 @@
+/*
+ *  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.felix.bundledb.btree.jdbm;
+
+
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+import jdbm.btree.BTree;
+import jdbm.helper.TupleBrowser;
+
+
+/**
+ * A NamingEnumeration that returns keys in a BTree.  This is used specifically 
+ * for situations when tables support duplicate keys and a BTree is used for 
+ * storing the values for that key.  This enumeration thus advances a browser forwards 
+ * returning keys from a BTree as values.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class BTreeIterator implements Iterator
+{
+    private final jdbm.helper.Tuple jdbmTuple = new jdbm.helper.Tuple();
+    private TupleBrowser browser;
+    private boolean success = false;
+    private boolean doAscending = true;
+    
+    
+    BTreeIterator( BTree tree, boolean doAscending ) throws IOException 
+    {
+        this.doAscending = doAscending;
+        
+        if ( doAscending )
+        {
+            browser = tree.browse();
+        }
+        else
+        {
+            browser = tree.browse( null );
+        }
+        
+        prefetch();
+    }
+
+    
+    private void prefetch() throws IOException
+    {
+        if ( doAscending )
+        {
+            success = browser.getNext( jdbmTuple );
+        }
+        else
+        {
+            success = browser.getPrevious( jdbmTuple );
+        }
+    }
+    
+    
+    public boolean hasNext()
+    {
+        return success;
+    }
+
+
+    public Object next()
+    {
+        if ( ! success )
+        {
+            throw new NoSuchElementException();
+        }
+        
+        Object next = jdbmTuple.getKey();
+        try
+        {
+            prefetch();
+        }
+        catch ( IOException e )
+        {
+            throw new NoSuchElementException( "Failure on btree: " + e.getMessage() );
+        }
+        return next;
+    }
+
+
+    public void remove()
+    {
+        throw new UnsupportedOperationException();
+    }
+}

Added: incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/BTreeRedirect.java
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/BTreeRedirect.java?view=auto&rev=500151
==============================================================================
--- incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/BTreeRedirect.java (added)
+++ incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/BTreeRedirect.java Thu Jan 25 22:20:18 2007
@@ -0,0 +1,53 @@
+/*
+ *  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.felix.bundledb.btree.jdbm;
+
+import java.io.Serializable;
+
+
+/**
+ * A redirection pointer to another BTree.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class BTreeRedirect implements Serializable
+{
+    private static final long serialVersionUID = -4289810071005184834L;
+
+    private Long recId;
+
+    
+    public BTreeRedirect()
+    {
+    }
+
+    
+    public BTreeRedirect( long recId )
+    {
+        this.recId = new Long( recId );
+    }
+    
+    
+    public Long getRecId()
+    {
+        return recId;
+    }
+}

Added: incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/BTreeTupleEnumeration.java
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/BTreeTupleEnumeration.java?view=auto&rev=500151
==============================================================================
--- incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/BTreeTupleEnumeration.java (added)
+++ incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/BTreeTupleEnumeration.java Thu Jan 25 22:20:18 2007
@@ -0,0 +1,132 @@
+/*
+ *  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.felix.bundledb.btree.jdbm;
+
+
+import java.io.IOException;
+import java.util.Comparator;
+import java.util.NoSuchElementException;
+
+import org.apache.felix.bundledb.btree.Tuple;
+import org.apache.felix.bundledb.btree.TupleCursor;
+
+import jdbm.btree.BTree;
+import jdbm.helper.TupleBrowser;
+
+
+/**
+ * A NamingEnumeration that returns underlying keys in a BTree as values for a 
+ * single key within a Tuple.  This is used specifically for situations when tables
+ * support duplicate keys and a BTree is used for storing the values for that
+ * key.  This enumeration thus advances a browser forwards or reverse returning
+ * keys from a BTree as Tuple values.  The key provided in the constructor is always
+ * returned as the key of the Tuple.
+ * 
+ * <p>
+ * WARNING: The tuple returned is reused every time for efficiency and populated
+ * a over and over again with the new value.  The key never changes.
+ * </p>
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class BTreeTupleEnumeration implements TupleCursor
+{
+    private final Object key;
+    private final Tuple tuple = new Tuple();
+    private final jdbm.helper.Tuple jdbmTuple = new jdbm.helper.Tuple();
+    private final boolean stepForward;
+    private TupleBrowser browser;
+    private boolean success = false;
+    
+    
+    @SuppressWarnings("unchecked")
+    BTreeTupleEnumeration( BTree tree, Comparator comparator, Object key, Object val, boolean isGreaterThan ) 
+        throws IOException 
+    {
+        this.key = key;
+        stepForward = isGreaterThan;
+        browser = tree.browse( val );
+        
+        if ( ! isGreaterThan )
+        {
+            boolean gotNextOk = browser.getNext( jdbmTuple );
+            if ( gotNextOk )
+            {
+                Object nextVal = jdbmTuple.getKey();
+                int compared = comparator.compare( val, nextVal );
+                if ( compared != 0 )
+                {
+                    browser.getPrevious( jdbmTuple );
+                }
+            }
+        }
+        
+        prefetch();
+    }
+
+    
+    BTreeTupleEnumeration( BTree tree, Object key ) throws IOException 
+    {
+        this.key = key;
+        stepForward = true;
+        browser = tree.browse();
+        prefetch();
+    }
+
+    
+    private void prefetch() throws IOException
+    {
+        if ( stepForward )
+        {
+            success = browser.getNext( jdbmTuple );
+        }
+        else
+        {
+            success = browser.getPrevious( jdbmTuple );
+        }
+    }
+    
+    
+    public void close() 
+    {
+        success = false;
+    }
+
+
+    public boolean hasMore()
+    {
+        return success;
+    }
+
+
+    public Tuple next() throws IOException
+    {
+        if ( ! success )
+        {
+            throw new NoSuchElementException();
+        }
+        
+        tuple.setKey( key );
+        tuple.setValue( jdbmTuple.getKey() );
+        prefetch();
+        return tuple;
+    }
+}

Added: incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/DupsEnumeration.java
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/DupsEnumeration.java?view=auto&rev=500151
==============================================================================
--- incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/DupsEnumeration.java (added)
+++ incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/DupsEnumeration.java Thu Jan 25 22:20:18 2007
@@ -0,0 +1,206 @@
+/*
+ *  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.felix.bundledb.btree.jdbm;
+
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.TreeSet;
+
+import jdbm.btree.BTree;
+
+import org.apache.felix.bundledb.btree.NoDupsTupleCursor;
+import org.apache.felix.bundledb.btree.Tuple;
+import org.apache.felix.bundledb.btree.TupleCursor;
+
+
+/**
+ * NamingEnumeration that enumerates over duplicate values nested into a value 
+ * using a TreeSet.
+ *
+ * @warning The Tuple returned by this listing is always the same instance 
+ * object returned every time. It is reused to for the sake of efficency rather 
+ * than creating a new tuple for each next() call.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev: 491471 $
+ */
+public class DupsEnumeration implements TupleCursor
+{
+    /** Marker for whether or not next() will return successfully */
+    private boolean hasMore = true;
+    /** The Tuple to return */
+    private final Tuple returned = new Tuple();
+    /** The Tuple to store prefetched values with */
+    private final Tuple prefetched = new Tuple();
+    /** The underlying no duplicates enumeration this enum expands out */
+    private final NoDupsTupleCursor underlying;
+
+    /** 
+     * The current Tuple returned from the underlying NoDupsEnumeration which
+     * contains TreeSets for Tuple values.  A NoDupsEnumeration on a Table that
+     * allows duplicates essentially returns Strings for keys and TreeSets for 
+     * their values.
+     */
+    private Tuple duplicates;
+    /** 
+     * The iterator over a set of Tuple values with the same key.  Basically
+     * iterates over the TreeSet values in the duplicates Tuple. 
+     */
+    private Iterator dupIterator;
+
+    private JdbmTable table;
+
+    
+    // ------------------------------------------------------------------------
+    // Constructor
+    // ------------------------------------------------------------------------
+
+    /**
+     * Creates a DupsEnumeration over a enumeration of Tuples holding TreeSets
+     * for values that have the same key.
+     *
+     * @param list the underlying enumeration
+     * @throws NamingException if there is a problem
+     */
+    public DupsEnumeration( JdbmTable table, NoDupsTupleCursor list ) throws IOException
+    {
+        this.table = table;
+        underlying = list;
+
+        // Protect against closed cursors
+        if ( !underlying.hasMore() )
+        {
+            close();
+            return;
+        }
+
+        prefetch();
+    }
+
+
+    public Tuple next() throws IOException
+    {
+        returned.setKey( prefetched.getKey() );
+        returned.setValue( prefetched.getValue() );
+        prefetch();
+        return returned;
+    }
+
+
+    public boolean hasMore()
+    {
+        return hasMore;
+    }
+
+
+    public void close()
+    {
+        hasMore = false;
+        underlying.close();
+    }
+
+
+    // ------------------------------------------------------------------------
+    // Private/Package Friendly Methods
+    // ------------------------------------------------------------------------
+
+    /**
+     * Prefetches values into the prefetched Tuple taking into account that 
+     * the returned Tuple values of the underlying enumeration list are really
+     * TreeSets that hold multiple sorted values for the same key.  
+     * 
+     * <p> The values prefetched into the prefetched Tuple are actual values 
+     * taken from the TreeSet.  So this NamingEnumeration simply expands out 
+     * duplicate keyed Tuples which it returns.  iterator is an iteration over
+     * the values held in the TreeSet returned by the underlying enumeration.  
+     * The values pulled off of this iterator are put into prefetched. 
+     * </p>
+     */
+    @SuppressWarnings("unchecked")
+    private void prefetch() throws IOException
+    {
+        /*
+         * If the iterator over the values of the current key is null or is 
+         * extinguished then we need to advance to the next key.
+         */
+        while ( null == dupIterator || !dupIterator.hasNext() )
+        {
+            /*
+             * If the underlying enumeration has more elements we get the next
+             * key/TreeSet Tuple to work with and get an iterator over it. 
+             */
+            if ( underlying.hasMore() )
+            {
+                duplicates = ( Tuple ) underlying.next();
+                
+                Object values = duplicates.getValue();
+                
+                if ( values instanceof TreeSet )
+                {
+                    TreeSet set = ( TreeSet ) duplicates.getValue();
+    
+                    if ( underlying.doAscendingScan() )
+                    {
+                        dupIterator = set.iterator();
+                    }
+                    else
+                    {
+                        /*
+                         * Need to reverse the list and iterate over the reversed
+                         * list.  
+                         * 
+                         * TODO This can be optimized by using a ReverseIterator 
+                         * over the array list.  I don't think there is a way to 
+                         * do this on the TreeSet.
+                         */
+                        List list = new ArrayList( set.size() );
+                        list.addAll( set );
+                        Collections.reverse( list );
+                        dupIterator = list.iterator();
+                    }
+                }
+                else if ( values instanceof BTreeRedirect )
+                {
+                    BTree tree = table.getBTree( ( BTreeRedirect ) values );
+                    dupIterator = new BTreeIterator( tree, underlying.doAscendingScan() );
+                }
+            }
+            else
+            {
+                close();
+                return;
+            }
+        }
+
+        /*
+         * If we get to this point then iterator has more elements and 
+         * duplicates holds the Tuple containing the key and TreeSet of 
+         * values for that key which the iterator iterates over.  All we
+         * need to do is populate the prefetched Tuple with the key and the
+         * next value in the iterator.
+         */
+        prefetched.setKey( duplicates.getKey() );
+        prefetched.setValue( dupIterator.next() );
+    }
+}

Added: incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/EmptyEnumeration.java
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/EmptyEnumeration.java?view=auto&rev=500151
==============================================================================
--- incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/EmptyEnumeration.java (added)
+++ incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/EmptyEnumeration.java Thu Jan 25 22:20:18 2007
@@ -0,0 +1,58 @@
+/*
+ *  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.felix.bundledb.btree.jdbm;
+
+
+import java.util.Enumeration;
+import java.util.NoSuchElementException;
+
+
+/**
+ * An empty Enumeration without any values: meaning
+ * hasMoreElements() always returns false, and nextElement() always
+ * throws a NoSuchElementException.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Revision: 437007 $
+ */
+public class EmptyEnumeration implements Enumeration
+{
+    /**
+     * Always return false.
+     * 
+     * @see java.util.Enumeration#hasMoreElements()
+     */
+    public boolean hasMoreElements()
+    {
+        return false;
+    }
+
+
+    /**
+     * Always throws NoSuchElementException.
+     * 
+     * @see java.util.Enumeration#nextElement()
+     */
+    public Object nextElement()
+    {
+        throw new NoSuchElementException();
+    }
+
+}

Added: incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/EmptyTupleCursor.java
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/EmptyTupleCursor.java?view=auto&rev=500151
==============================================================================
--- incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/EmptyTupleCursor.java (added)
+++ incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/EmptyTupleCursor.java Thu Jan 25 22:20:18 2007
@@ -0,0 +1,64 @@
+/*
+ *  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.felix.bundledb.btree.jdbm;
+
+
+import java.util.NoSuchElementException;
+
+import org.apache.felix.bundledb.btree.Tuple;
+import org.apache.felix.bundledb.btree.TupleCursor;
+
+
+/**
+ * An empty NamingEnumeration without any values: meaning
+ * hasMore/hasMoreElements() always returns false, and next/nextElement() always
+ * throws a NoSuchElementException.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Revision: 437007 $
+ */
+public class EmptyTupleCursor implements TupleCursor
+{
+
+    /**
+     * @see javax.naming.NamingEnumeration#close()
+     */
+    public void close()
+    {
+    }
+
+
+    /**
+     * Always returns false.
+     */
+    public boolean hasMore()
+    {
+        return false;
+    }
+
+
+    /**
+     * Always throws NoSuchElementException.
+     */
+    public Tuple next()
+    {
+        throw new NoSuchElementException();
+    }
+}

Added: incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/JdbmBTreeCursor.java
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/JdbmBTreeCursor.java?view=auto&rev=500151
==============================================================================
--- incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/JdbmBTreeCursor.java (added)
+++ incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/JdbmBTreeCursor.java Thu Jan 25 22:20:18 2007
@@ -0,0 +1,35 @@
+/*
+ *  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.felix.bundledb.btree.jdbm;
+
+
+import java.io.IOException;
+import java.util.Enumeration;
+
+
+/**
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public interface JdbmBTreeCursor extends Enumeration
+{
+    void close() throws IOException;
+}

Added: incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/JdbmIndex.java
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/JdbmIndex.java?view=auto&rev=500151
==============================================================================
--- incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/JdbmIndex.java (added)
+++ incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/JdbmIndex.java Thu Jan 25 22:20:18 2007
@@ -0,0 +1,296 @@
+/*
+ *  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.felix.bundledb.btree.jdbm;
+
+
+import java.io.File;
+import java.io.IOException;
+import java.math.BigInteger;
+import java.util.Comparator;
+import java.util.Enumeration;
+import java.util.regex.Pattern;
+
+import javax.naming.NamingException;
+
+import jdbm.RecordManager;
+import jdbm.helper.MRU;
+import jdbm.recman.BaseRecordManager;
+import jdbm.recman.CacheRecordManager;
+
+import org.apache.felix.bundledb.btree.Index;
+import org.apache.felix.bundledb.btree.IndexComparator;
+import org.apache.felix.bundledb.btree.IndexConfiguration;
+import org.apache.felix.bundledb.btree.Tuple2IndexRecordCursor;
+
+
+/** 
+ * A Jdbm based index implementation.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev: 491471 $
+ */
+public class JdbmIndex implements Index
+{
+    /**  */
+    public static final String FORWARD_BTREE = "_forward";
+    /** */
+    public static final String REVERSE_BTREE = "_reverse";
+
+    /** */
+    private String property;
+    /** */
+    private JdbmTable forward = null;
+    /** */
+    private JdbmTable reverse = null;
+    /** */
+    private RecordManager recMan = null;
+    
+    private int numDupLimit = IndexConfiguration.DEFAULT_DUPLICATE_LIMIT;
+
+
+    // ------------------------------------------------------------------------
+    // C O N S T R U C T O R S
+    // ------------------------------------------------------------------------
+
+
+    public JdbmIndex( String property, Comparator comparator, File wkDirPath, int cacheSize, int numDupLimit ) 
+        throws IOException
+    {
+        this.numDupLimit = numDupLimit;
+        File file = new File( wkDirPath.getPath() + File.separator + property );
+        this.property = property;
+
+        String path = file.getAbsolutePath();
+        BaseRecordManager base = new BaseRecordManager( path );
+        base.disableTransactions();
+        recMan = new CacheRecordManager( base, new MRU( cacheSize ) );
+
+        initTables( comparator );
+    }
+
+
+    /**
+     * Initializes the forward and reverse tables used by this Index.
+     * 
+     * @throws NamingException if we cannot initialize the forward and reverse 
+     * tables
+     */
+    private void initTables( Comparator comp ) throws IOException
+    {
+        /*
+         * The forward key/value map stores property values to master table 
+         * primary keys.  A value for an property can occur several times in
+         * different entries so the forward map can have more than one value.
+         */
+        forward = new JdbmTable( property + FORWARD_BTREE, true, numDupLimit, recMan, 
+            new IndexComparator( comp, true ) );
+
+        /*
+         * Now the reverse map stores the primary key into the master table as
+         * the key and the values of properties as the value.  If a property
+         * is single valued then duplicate keys should not be allowed within the 
+         * reverse table.
+         */
+        reverse = new JdbmTable( property + REVERSE_BTREE, false, numDupLimit, recMan, 
+            new IndexComparator( comp, false ) );
+    }
+
+
+    public String getProperty()
+    {
+        return property;
+    }
+
+
+    // ------------------------------------------------------------------------
+    // Scan Count Methods
+    // ------------------------------------------------------------------------
+
+
+    public int count() throws IOException
+    {
+        return forward.count();
+    }
+
+
+    public int count( String propertyValue ) throws IOException
+    {
+        return forward.count( propertyValue );
+    }
+
+
+    public int count( String propertyValue, boolean isGreaterThan ) throws IOException
+    {
+        return forward.count( propertyValue, isGreaterThan );
+    }
+
+
+    // ------------------------------------------------------------------------
+    // Forward and Reverse Lookups
+    // ------------------------------------------------------------------------
+
+    /**
+     * @see Index#forwardLookup(java.lang.Object)
+     */
+    public BigInteger forwardLookup( String attrVal ) throws IOException
+    {
+        return ( BigInteger ) forward.get( attrVal );
+    }
+
+
+    /**
+     * @see Index#reverseLookup(java.math.BigInteger)
+     */
+    public String reverseLookup( BigInteger id ) throws IOException
+    {
+        return ( String ) reverse.get( id );
+    }
+
+
+    // ------------------------------------------------------------------------
+    // Add/Drop Methods
+    // ------------------------------------------------------------------------
+
+
+    public synchronized void add( String propertyValue, BigInteger id ) throws IOException
+    {
+        forward.put( propertyValue, id );
+        reverse.put( id, propertyValue );
+    }
+
+    
+    public void drop( String propertyValue ) throws IOException
+    {
+        Enumeration values = forward.listValues( propertyValue );
+        while ( values.hasMoreElements() )
+        {
+            BigInteger id = ( BigInteger ) values.nextElement();
+            reverse.remove( id );
+        }
+        forward.remove( propertyValue );
+    }
+    
+
+    public void drop( BigInteger entryId ) throws IOException
+    {
+        Enumeration values = reverse.listValues( entryId );
+
+        while ( values.hasMoreElements() )
+        {
+            forward.remove( values.nextElement(), entryId );
+        }
+
+        reverse.remove( entryId );
+    }
+
+
+    public void drop( String value, BigInteger id ) throws IOException
+    {
+        reverse.remove( id, value );
+        forward.remove( value, id );
+    }
+
+
+    // ------------------------------------------------------------------------
+    // Index Listing Operations
+    // ------------------------------------------------------------------------
+
+
+    public Tuple2IndexRecordCursor listReverseIndices( BigInteger id ) throws IOException
+    {
+        return new Tuple2IndexRecordCursor( reverse.listTuples( id ), true );
+    }
+
+
+    public Tuple2IndexRecordCursor listIndices() throws IOException
+    {
+        return new Tuple2IndexRecordCursor( forward.listTuples() );
+    }
+
+
+    public Tuple2IndexRecordCursor listIndices( String propertyValue ) throws IOException
+    {
+        return new Tuple2IndexRecordCursor( forward.listTuples( propertyValue ) );
+    }
+
+
+    public Tuple2IndexRecordCursor listIndices( String propertyValue, boolean isGreaterThan ) throws IOException
+    {
+        return new Tuple2IndexRecordCursor( forward.listTuples( propertyValue, isGreaterThan ) );
+    }
+
+
+    public Tuple2IndexRecordCursor listIndices( Pattern regex ) throws IOException
+    {
+        return new Tuple2IndexRecordCursor( forward.listTuples(), false, regex );
+    }
+
+
+    public Tuple2IndexRecordCursor listIndices( Pattern regex, String prefix ) throws IOException
+    {
+        return new Tuple2IndexRecordCursor( forward.listTuples( prefix, true ), false, regex );
+    }
+
+
+    // ------------------------------------------------------------------------
+    // Value Assertion (a.k.a Index Lookup) Methods //
+    // ------------------------------------------------------------------------
+
+
+    public boolean hasValue( String propertyValue, BigInteger id ) throws IOException
+    {
+        return forward.has( propertyValue, id );
+    }
+
+
+    public boolean hasValue( String propertyValue, BigInteger id, boolean isGreaterThan ) throws IOException
+    {
+        return forward.has( propertyValue, id, isGreaterThan );
+    }
+
+
+    public boolean hasValue( Pattern regex, BigInteger id ) throws IOException
+    {
+        Tuple2IndexRecordCursor list = new Tuple2IndexRecordCursor( reverse.listTuples( id ), true, regex );
+        boolean hasValue = list.hasMore();
+        list.close();
+        return hasValue;
+    }
+
+
+    // ------------------------------------------------------------------------
+    // Maintenance Methods 
+    // ------------------------------------------------------------------------
+
+
+    public synchronized void close() throws IOException
+    {
+        forward.close();
+        reverse.close();
+        recMan.commit();
+        recMan.close();
+    }
+
+
+    public synchronized void sync() throws IOException
+    {
+        recMan.commit();
+    }
+}

Added: incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/JdbmMasterTable.java
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/JdbmMasterTable.java?view=auto&rev=500151
==============================================================================
--- incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/JdbmMasterTable.java (added)
+++ incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/JdbmMasterTable.java Thu Jan 25 22:20:18 2007
@@ -0,0 +1,204 @@
+/*
+ *  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.felix.bundledb.btree.jdbm;
+
+
+import java.io.IOException;
+import java.math.BigInteger;
+import java.util.Comparator;
+import java.util.Properties;
+
+import jdbm.RecordManager;
+import jdbm.helper.StringComparator;
+
+import org.apache.felix.bundledb.btree.BigIntegerComparator;
+import org.apache.felix.bundledb.btree.MasterTable;
+
+
+/**
+ * The master table used to store the Attributes of entries.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev: 491471 $
+ */
+public class JdbmMasterTable extends JdbmTable implements MasterTable
+{
+    private static final StringComparator STRCOMP = new StringComparator();
+    private static final Comparator BIG_INTEGER_COMPARATOR = BigIntegerComparator.INSTANCE;
+    private static final Comparator STRING_COMPARATOR = STRCOMP;
+
+    /**  */
+    private JdbmTable adminTbl = null;
+
+
+    /**
+     * Creates the master entry table using a Berkeley Db for the backing store.
+     *
+     * @param recMan the jdbm record manager
+     * @throws IOException if there is an error opening the Db file.
+     */
+    public JdbmMasterTable( RecordManager recMan ) throws IOException
+    {
+        super( DBF, recMan, BIG_INTEGER_COMPARATOR );
+        adminTbl = new JdbmTable( "admin", recMan, STRING_COMPARATOR );
+        String seqValue = ( String ) adminTbl.get( SEQPROP_KEY );
+
+        if ( null == seqValue )
+        {
+            adminTbl.put( SEQPROP_KEY, BigInteger.ZERO.toString() );
+        }
+    }
+
+
+    /**
+     * Gets the Properties of an entry from this MasterTable.
+     *
+     * @param id the BigInteger id of the entry to retrieve.
+     * @return the Properties of the entry.
+     * @throws IOException if there is a read error on the underlying Db.
+     */
+    public Properties get( BigInteger id ) throws IOException
+    {
+        return ( Properties ) super.get( id );
+    }
+
+
+    /**
+     * Puts the Attributes of an entry into this master table at an index 
+     * specified by id.  Used both to create new entries and update existing 
+     * ones.
+     *
+     * @param entry the Attributes of entry w/ operational attributes
+     * @param id the BigInteger id of the entry to put
+     * @return the Attributes of the entry put
+     * @throws IOException if there is a write error on the underlying Db.
+     */
+    public Properties put( Properties entry, BigInteger id ) throws IOException
+    {
+        return ( Properties ) super.put( id, entry );
+    }
+
+
+    /**
+     * Deletes a entry from the master table at an index specified by id.
+     *
+     * @param id the BigInteger id of the entry to delete
+     * @return the Attributes of the deleted entry
+     * @throws IOException if there is a write error on the underlying Db
+     */
+    public Properties delete( BigInteger id ) throws IOException
+    {
+        return ( Properties ) super.remove( id );
+    }
+
+
+    /**
+     * Get's the current id value from this master database's sequence without
+     * affecting the seq.
+     *
+     * @return the current value.
+     * @throws IOException if the admin table storing sequences cannot be
+     * read.
+     */
+    public BigInteger getCurrentId() throws IOException
+    {
+        BigInteger id = null;
+
+        synchronized ( adminTbl )
+        {
+            id = new BigInteger( ( String ) adminTbl.get( SEQPROP_KEY ) );
+
+            if ( null == id )
+            {
+                adminTbl.put( SEQPROP_KEY, BigInteger.ZERO.toString() );
+                id = BigInteger.ZERO;
+            }
+        }
+
+        return id;
+    }
+
+
+    /**
+     * Get's the next value from this SequenceBDb.  This has the side-effect of
+     * changing the current sequence values perminantly in memory and on disk.
+     * Master table sequence begins at BigInteger.ONE.  The BigInteger.ZERO is
+     * used for the fictitious parent of the suffix root entry.
+     *
+     * @return the current value incremented by one.
+     * @throws IOException if the admin table storing sequences cannot be
+     * read and writen to.
+     */
+    public BigInteger getNextId() throws IOException
+    {
+        BigInteger lastVal = null;
+        BigInteger nextVal = null;
+
+        synchronized ( adminTbl )
+        {
+            lastVal = new BigInteger( ( String ) adminTbl.get( SEQPROP_KEY ) );
+
+            if ( null == lastVal )
+            {
+                adminTbl.put( SEQPROP_KEY, BigInteger.ONE.toString() );
+                return BigInteger.ONE;
+            }
+            else
+            {
+                nextVal = lastVal.add( BigInteger.ONE );
+                adminTbl.put( SEQPROP_KEY, nextVal.toString() );
+            }
+        }
+
+        return nextVal;
+    }
+
+
+    /**
+     * Gets a persistant property stored in the admin table of this MasterTable.
+     *
+     * @param property the key of the property to get the value of
+     * @return the value of the property
+     * @throws IOException when the underlying admin table cannot be read
+     */
+    public String getProperty( String property ) throws IOException
+    {
+        synchronized ( adminTbl )
+        {
+            return ( String ) adminTbl.get( property );
+        }
+    }
+
+
+    /**
+     * Sets a persistant property stored in the admin table of this MasterTable.
+     *
+     * @param property the key of the property to set the value of
+     * @param value the value of the property
+     * @throws IOException when the underlying admin table cannot be writen
+     */
+    public void setProperty( String property, String value ) throws IOException
+    {
+        synchronized ( adminTbl )
+        {
+            adminTbl.put( property, value );
+        }
+    }
+}

Added: incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/JdbmStore.java
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/JdbmStore.java?view=auto&rev=500151
==============================================================================
--- incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/JdbmStore.java (added)
+++ incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/JdbmStore.java Thu Jan 25 22:20:18 2007
@@ -0,0 +1,349 @@
+/*
+ *  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.felix.bundledb.btree.jdbm;
+
+
+import java.io.File;
+import java.io.IOException;
+import java.math.BigInteger;
+import java.util.Comparator;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+
+import jdbm.RecordManager;
+import jdbm.helper.MRU;
+import jdbm.helper.StringComparator;
+import jdbm.recman.BaseRecordManager;
+import jdbm.recman.CacheRecordManager;
+
+import org.apache.felix.bundledb.btree.Index;
+import org.apache.felix.bundledb.btree.IndexConfiguration;
+import org.apache.felix.bundledb.btree.IndexNotFoundException;
+
+
+public class JdbmStore
+{
+    private static final int DEFAULT_CACHE_SIZE = 10000;
+
+    /** the JDBM record manager used by this database */
+    private RecordManager recMan;
+    /** the working directory to use for files */
+    private File workingDirectory;
+    /** the master table storing entries by primary key */
+    private JdbmMasterTable master;
+    /** a map of property names to user indices */
+    private Map<String, Index> indices;
+
+    /** true if open */
+    private boolean initialized;
+    /** true if we sync disks on every write operation */
+    private boolean isSyncOnWrite = true;
+    
+    private Index existanceIdx;
+    
+    // ------------------------------------------------------------------------
+    // C O N S T R U C T O R S
+    // ------------------------------------------------------------------------
+
+    /**
+     * Creates a store based on JDBM B+Trees.
+     */
+    public JdbmStore()
+    {
+    }
+
+    
+    /**
+     * Use this method to initialize the indices.  
+     */
+    protected void initIndices( Set indices ) throws IOException
+    {
+        existanceIdx = new JdbmIndex( "existence", new StringComparator(), workingDirectory, 
+            IndexConfiguration.DEFAULT_INDEX_CACHE_SIZE, IndexConfiguration.DEFAULT_DUPLICATE_LIMIT );
+        
+        for ( Iterator ii = indices.iterator(); ii.hasNext(); /**/ )
+        {
+            Object nextObject = ii.next();
+            String name = null;
+            int cacheSize = IndexConfiguration.DEFAULT_INDEX_CACHE_SIZE;
+            int numDupLimit = IndexConfiguration.DEFAULT_DUPLICATE_LIMIT;
+            
+            IndexConfiguration indexConfiguration = ( IndexConfiguration ) nextObject;
+            name = indexConfiguration.getPropertyId();
+            cacheSize = indexConfiguration.getCacheSize();
+            numDupLimit = indexConfiguration.getDuplicateLimit();
+            
+            if ( cacheSize <= 0 ) 
+            {
+                cacheSize = IndexConfiguration.DEFAULT_INDEX_CACHE_SIZE;
+            }
+            
+            addIndexOn( name, indexConfiguration.getComparator(), cacheSize, numDupLimit );
+        }
+    }
+
+    
+    public synchronized void init( JdbmStoreConfiguration config ) throws IOException
+    {
+        isSyncOnWrite = config.isSyncOnWrite();
+        workingDirectory = config.getWorkingDirectory();
+        workingDirectory.mkdirs();
+
+        String path = workingDirectory.getPath() + File.separator + "master";
+        BaseRecordManager base = new BaseRecordManager( path );
+        base.disableTransactions();
+        
+        int cacheSize = config.getCacheSize();
+        if ( cacheSize < 0 )
+        {
+            cacheSize = DEFAULT_CACHE_SIZE;
+        }
+        recMan = new CacheRecordManager( base, new MRU( cacheSize ) );
+        master = new JdbmMasterTable( recMan );
+        indices = new HashMap<String,Index>();
+        initIndices( config.getIndexedProperties() );
+        initialized = true;
+    }
+
+
+    public synchronized void destroy()
+    {
+        if ( !initialized )
+        {
+            return;
+        }
+
+        Iterator list = indices.values().iterator();
+        while ( list.hasNext() )
+        {
+            Index index = ( Index ) list.next();
+
+            try
+            {
+                index.close();
+            }
+            catch ( Throwable t )
+            {
+                t.printStackTrace();
+            }
+        }
+
+        try
+        {
+            existanceIdx.close();
+        }
+        catch ( Throwable t )
+        {
+            t.printStackTrace();
+        }
+        
+        try
+        {
+            master.close();
+        }
+        catch ( Throwable t )
+        {
+            t.printStackTrace();
+        }
+
+        try
+        {
+            recMan.close();
+        }
+        catch ( Throwable t )
+        {
+            t.printStackTrace();
+        }
+
+        initialized = false;
+    }
+
+
+    public boolean isInitialized()
+    {
+        return initialized;
+    }
+
+
+    public synchronized void sync() throws IOException
+    {
+        if ( !initialized )
+        {
+            return;
+        }
+
+        Iterator list = indices.values().iterator();
+        // Sync all user defined indices
+        while ( list.hasNext() )
+        {
+            Index idx = ( Index ) list.next();
+            idx.sync();
+        }
+
+        try
+        {
+            existanceIdx.sync();
+        }
+        catch ( Throwable t )
+        {
+            t.printStackTrace();
+        }
+
+        try
+        {
+            master.sync();
+        }
+        catch ( Throwable t )
+        {
+            t.printStackTrace();
+        }
+
+        recMan.commit();
+    }
+
+
+    // ------------------------------------------------------------------------
+    // I N D E X   M E T H O D S
+    // ------------------------------------------------------------------------
+
+    public void addIndexOn( String property, Comparator comparator, int cacheSize, int numDupLimit ) throws IOException
+    {
+        Index idx = new JdbmIndex( property, comparator, workingDirectory, cacheSize, numDupLimit );
+        indices.put( property, idx );
+    }
+
+
+    public Iterator getUserIndices()
+    {
+        return indices.keySet().iterator();
+    }
+
+
+    public boolean hasUserIndexOn( String id ) throws IOException
+    {
+        return indices.containsKey( id );
+    }
+
+
+    /**
+     * @see org.apache.directory.server.core.partition.impl.btree.BTreePartition#getUserIndex(String)
+     */
+    public Index getUserIndex( String id ) throws IndexNotFoundException
+    {
+        if ( indices.containsKey( id ) )
+        {
+            return ( Index ) indices.get( id );
+        }
+        else
+        {
+            throw new IndexNotFoundException( "A user index on property " + id + " does not exist!" );
+        }
+    }
+
+
+    public int count() throws IOException
+    {
+        return master.count();
+    }
+
+
+    public void add( Properties entry ) throws IOException
+    {
+        BigInteger id;
+        id = master.getNextId();
+
+        // Now work on the user defined indices
+        Enumeration list = entry.keys();
+        while ( list.hasMoreElements() )
+        {
+            String property = ( String ) list.nextElement();
+
+            if ( hasUserIndexOn( property ) )
+            {
+                Index idx = getUserIndex( property );
+                
+                idx.add( entry.getProperty( property ), id );
+
+                // Adds only those attributes that are indexed
+                existanceIdx.add( property, id );
+            }
+        }
+
+        master.put( entry, id );
+        
+        if ( isSyncOnWrite )
+        {
+            sync();
+        }
+    }
+    
+    
+    public Index getExistenceIndex()
+    {
+        return existanceIdx;
+    }
+
+
+    public Properties lookup( BigInteger id ) throws IOException
+    {
+        return master.get( id );
+    }
+
+
+    public void delete( BigInteger id ) throws IOException
+    {
+        Properties entry = lookup( id );
+        Enumeration keys = entry.keys();
+        while ( keys.hasMoreElements() )
+        {
+            String propertyId = ( ( String ) keys.nextElement() );
+
+            if ( hasUserIndexOn( propertyId ) )
+            {
+                Index index = getUserIndex( propertyId );
+                index.drop( propertyId, id );
+                existanceIdx.drop( propertyId, id );
+            }
+        }
+
+        master.delete( id );
+        
+        if ( isSyncOnWrite )
+        {
+            sync();
+        }
+    }
+
+
+    public void setProperty( String propertyName, String propertyValue ) throws IOException
+    {
+        master.setProperty( propertyName, propertyValue );
+    }
+
+
+    public String getProperty( String propertyName ) throws IOException
+    {
+        return master.getProperty( propertyName );
+    }
+}

Added: incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/JdbmStoreConfiguration.java
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/JdbmStoreConfiguration.java?view=auto&rev=500151
==============================================================================
--- incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/JdbmStoreConfiguration.java (added)
+++ incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/btree/jdbm/JdbmStoreConfiguration.java Thu Jan 25 22:20:18 2007
@@ -0,0 +1,94 @@
+/*
+ *  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.felix.bundledb.btree.jdbm;
+
+
+import java.io.File;
+import java.util.Set;
+
+
+public class JdbmStoreConfiguration
+{
+    private File workingDirectory;
+    private Set indexedProperties;
+    private boolean isSyncOnWrite;
+    private boolean enableOptimizer;
+    private int cacheSize = 100;
+    
+
+    public void setWorkingDirectory( File workingDirectory )
+    {
+        this.workingDirectory = workingDirectory;
+    }
+    
+    
+    public File getWorkingDirectory()
+    {
+        return workingDirectory;
+    }
+    
+    
+    public void setIndexedProperties( Set indexedAttributes )
+    {
+        this.indexedProperties = indexedAttributes;
+    }
+    
+    
+    public Set getIndexedProperties()
+    {
+        return indexedProperties;
+    }
+    
+    
+    public void setSyncOnWrite( boolean isSyncOnWrite )
+    {
+        this.isSyncOnWrite = isSyncOnWrite;
+    }
+
+
+    public boolean isSyncOnWrite()
+    {
+        return isSyncOnWrite;
+    }
+
+
+    public void setEnableOptimizer( boolean enableOptimizer )
+    {
+        this.enableOptimizer = enableOptimizer;
+    }
+
+
+    public boolean isEnableOptimizer()
+    {
+        return enableOptimizer;
+    }
+
+
+    public void setCacheSize( int cacheSize )
+    {
+        this.cacheSize = cacheSize;
+    }
+
+
+    public int getCacheSize()
+    {
+        return cacheSize;
+    }
+}