directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r1239581 [3/9] - in /directory/apacheds/trunk/jdbm2: ./ src/ src/etc/ src/examples/ src/main/ src/main/java/ src/main/java/jdbm/ src/main/java/jdbm/btree/ src/main/java/jdbm/helper/ src/main/java/jdbm/htree/ src/main/java/jdbm/recman/ src/s...
Date Thu, 02 Feb 2012 12:38:42 GMT
Added: directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/ActionVersioning.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/ActionVersioning.java?rev=1239581&view=auto
==============================================================================
--- directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/ActionVersioning.java (added)
+++ directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/ActionVersioning.java Thu Feb  2 12:38:39 2012
@@ -0,0 +1,236 @@
+/*
+ *  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 jdbm.helper;
+
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+
+/**
+ * This is a helper class to keep track of versions assigned to actions. As client begin
+ * read only and read write actions, they call into this class and get the version they
+ * can use. As the clients end their actions, minimum read version any action is using is
+ * advanced and piggybacked to the client
+ * 
+ * This class assumes that there is one readWrite action at a time. 
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class ActionVersioning
+{
+    /** Current write version */
+    private Version nextVersion;
+    
+    /** Current read version reference */
+    private AtomicReference<Version> readReference;
+    
+    /** List to put versions on */
+    private ExplicitList<Version> versions = new ExplicitList<Version>();
+    
+    /** Lock to protect the list */
+    private Lock listLock = new ReentrantLock();
+    
+    
+    public ActionVersioning()
+    {
+        Version readVersion = new Version( 0 );
+        nextVersion = new Version( 1 ); 
+        readReference = new AtomicReference<Version>( readVersion );
+
+        versions.addFirst( nextVersion.getVersionsLink() );
+        versions.addFirst( readVersion.getVersionsLink() );
+    }
+    
+    
+    /**
+     * Returns back the new version to be used with the read/write action.
+     * Assume one read/write action at a time.
+     *
+     * @return new version for the action system
+     */
+    public Version beginWriteAction()
+    {
+        return nextVersion;
+    }
+    
+    
+    /**
+     * Called when the read/write action completes. Advances the version of action subsystem 
+     * and publishes a new version for the readers. Assume one read/write action at a time.
+     *
+     * @return minimum read version for the action subsystem
+     */
+    public Version endWriteAction()
+    {
+        Version minVersion;
+        
+        // Allocate the new nextVersion
+        Version newNextVersion = new Version( nextVersion.getVersion() + 1 );
+        
+        // Post the commited version as the new read version
+        Version oldReadVersion = readReference.getAndSet( nextVersion );
+        
+        // add the newnextversion to the versions list
+        listLock.lock();
+        versions.addLast( newNextVersion.getVersionsLink() );
+        
+        if ( ( oldReadVersion.getNumActions().get() == 0 ) && 
+            oldReadVersion.getVersionsLink().isLinked() )
+        {
+            versions.remove( oldReadVersion.getVersionsLink() );
+            oldReadVersion.getVersionsLink().uninit();
+        }
+        
+        minVersion = versions.begin().getElement();
+        listLock.unlock();
+        
+        nextVersion = newNextVersion;
+        return minVersion;
+    }
+    
+    
+    /**
+     * Returns a version that can be used by the read only action
+     *
+     * @return version to be used by the action
+     */
+    public Version beginReadAction()
+    {
+        Version readVersion = readReference.get();
+        
+        readVersion.getNumActions().incrementAndGet();
+        
+        /*
+         * If the write txn just finished and published
+         * a new version to read, check if we can still
+         * use our version for reading
+         */
+        if ( readVersion != readReference.get() )
+        {
+            listLock.lock();
+            
+            if ( readVersion.getVersionsLink().isUnLinked() )
+            {
+                readVersion = readReference.get();
+                readVersion.getNumActions().incrementAndGet();
+            }
+            
+            listLock.unlock();
+        }
+        
+        return readVersion;
+    }
+    
+    
+    /**
+     * Called when the read action with the given action is ended.
+     * Checks whether the minimum read version advanced
+     *
+     * @param version version of the read only action
+     * @return returns the miminum read version. Might return null if read version did not 
+     * advance for sure.
+     */
+    public Version endReadAction( Version version )
+    {
+        long numActions = version.getNumActions().decrementAndGet();
+        
+        if ( numActions < 0 )
+        {
+            throw new IllegalStateException( "NumActions zero when read action is ended : " + version );
+        }
+
+        
+        if ( ( numActions > 0 ) || ( version == readReference.get() ) )
+        {
+            // minimum read version did not change for sure
+            return null;
+        }
+        
+        Version minVersion = null;
+        listLock.lock();
+        
+        if ( ( version.getNumActions().get() == 0 ) && 
+            version.getVersionsLink().isLinked() )
+        {
+            version.getVersionsLink().remove();
+            version.getVersionsLink().uninit();
+        }
+        
+        minVersion = versions.begin().getElement();
+        listLock.unlock();
+        
+        return minVersion;
+    }
+    
+    
+    public static class Version
+    {
+        /** Represented version */
+        private long version;
+        
+        /** Used to put on versions chain */
+        private ExplicitList.Link<Version> versionsLink;
+        
+        /** Number of txns running with this version */
+        private  AtomicInteger numActions;
+        
+        
+        public Version ( long version )
+        {
+            this.version = version;
+            
+            versionsLink = new ExplicitList.Link<ActionVersioning.Version>( this );
+            
+            numActions = new AtomicInteger( 0 );
+        }
+        
+        
+        private ExplicitList.Link<Version> getVersionsLink()
+        {
+            return versionsLink;
+        }
+        
+        
+        private AtomicInteger getNumActions()
+        {
+            return numActions;
+        }
+        
+        
+        public long getVersion()
+        {
+            return version;
+        }
+        
+        @Override
+        public String toString()
+        {
+            StringBuilder sb = new StringBuilder();
+            sb.append( "Version: ");
+            sb.append( "(vesion: " ).append( version );
+            sb.append( ", numActions: " ).append( numActions );
+            sb.append( ")\n" );
+            
+            return sb.toString();
+        }
+    }
+}
\ No newline at end of file

Added: directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/ByteArrayComparator.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/ByteArrayComparator.java?rev=1239581&view=auto
==============================================================================
--- directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/ByteArrayComparator.java (added)
+++ directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/ByteArrayComparator.java Thu Feb  2 12:38:39 2012
@@ -0,0 +1,135 @@
+/**
+ * JDBM LICENSE v1.00
+ *
+ * Redistribution and use of this software and associated documentation
+ * ("Software"), with or without modification, are permitted provided
+ * that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain copyright
+ *    statements and notices.  Redistributions must also contain a
+ *    copy of this document.
+ *
+ * 2. Redistributions in binary form must reproduce the
+ *    above copyright notice, this list of conditions and the
+ *    following disclaimer in the documentation and/or other
+ *    materials provided with the distribution.
+ *
+ * 3. The name "JDBM" must not be used to endorse or promote
+ *    products derived from this Software without prior written
+ *    permission of Cees de Groot.  For written permission,
+ *    please contact cg@cdegroot.com.
+ *
+ * 4. Products derived from this Software may not be called "JDBM"
+ *    nor may "JDBM" appear in their names without prior written
+ *    permission of Cees de Groot.
+ *
+ * 5. Due credit should be given to the JDBM Project
+ *    (http://jdbm.sourceforge.net/).
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE JDBM PROJECT AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
+ * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
+ * CEES DE GROOT OR ANY CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Copyright 2001 (C) Alex Boisvert. All Rights Reserved.
+ * Contributions are Copyright (C) 2001 by their associated contributors.
+ *
+ */
+
+package jdbm.helper;
+
+import java.util.Comparator;
+import java.io.Serializable;
+
+import org.apache.directory.server.i18n.I18n;
+
+/**
+ * Comparator for byte arrays.
+ *
+ * @author <a href="mailto:boisvert@intalio.com">Alex Boisvert</a>
+ */
+public final class ByteArrayComparator
+    implements Comparator<byte[]>, Serializable
+{
+
+    /**
+     * Version id for serialization.
+     */
+    final static long serialVersionUID = 1L;
+
+
+    /**
+     * Compare two objects.
+     *
+     * @param obj1 First object
+     * @param obj2 Second object
+     * @return a positive integer if obj1 > obj2, 0 if obj1 == obj2,
+     *         and a negative integer if obj1 < obj2
+     */
+     public int compare( byte[] obj1, byte[] obj2 )
+     {
+        if ( obj1 == null ) {
+            throw new IllegalArgumentException( I18n.err( I18n.ERR_525 ) );
+        }
+
+        if ( obj2 == null ) {
+            throw new IllegalArgumentException( I18n.err( I18n.ERR_526 ) );
+        }
+
+        return compareByteArray( obj1, obj2 );
+     }
+
+
+    /**
+     * Compare two byte arrays.
+     */
+    public static int compareByteArray( byte[] thisKey, byte[] otherKey )
+    {
+        int len = Math.min( thisKey.length, otherKey.length );
+
+        // compare the byte arrays
+        for ( int i=0; i<len; i++ ) {
+            if ( thisKey[i] >= 0 ) {
+                if ( otherKey[i] >= 0 ) {
+                    // both positive
+                    if ( thisKey[i] < otherKey[i] ) {
+                        return -1;
+                    } else if ( thisKey[i] > otherKey[i] ) {
+                        return 1;
+                    }
+                } else {
+                    // otherKey is negative => greater (because MSB is 1)
+                    return -1;
+                }
+            } else {
+                if ( otherKey[i] >= 0 ) {
+                    // thisKey is negative => greater (because MSB is 1)
+                    return 1;
+                } else {
+                    // both negative
+                    if ( thisKey[i] < otherKey[i] ) {
+                        return -1;
+                    } else if ( thisKey[i] > otherKey[i] ) {
+                        return 1;
+                    }
+                }
+            }
+        }
+        if ( thisKey.length == otherKey.length) {
+            return 0;
+        }
+        if ( thisKey.length < otherKey.length ) {
+            return -1;
+        }
+        return 1;
+    }
+
+}

Added: directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/ByteArraySerializer.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/ByteArraySerializer.java?rev=1239581&view=auto
==============================================================================
--- directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/ByteArraySerializer.java (added)
+++ directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/ByteArraySerializer.java Thu Feb  2 12:38:39 2012
@@ -0,0 +1,101 @@
+/**
+ * JDBM LICENSE v1.00
+ *
+ * Redistribution and use of this software and associated documentation
+ * ("Software"), with or without modification, are permitted provided
+ * that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain copyright
+ *    statements and notices.  Redistributions must also contain a
+ *    copy of this document.
+ *
+ * 2. Redistributions in binary form must reproduce the
+ *    above copyright notice, this list of conditions and the
+ *    following disclaimer in the documentation and/or other
+ *    materials provided with the distribution.
+ *
+ * 3. The name "JDBM" must not be used to endorse or promote
+ *    products derived from this Software without prior written
+ *    permission of Cees de Groot.  For written permission,
+ *    please contact cg@cdegroot.com.
+ *
+ * 4. Products derived from this Software may not be called "JDBM"
+ *    nor may "JDBM" appear in their names without prior written
+ *    permission of Cees de Groot.
+ *
+ * 5. Due credit should be given to the JDBM Project
+ *    (http://jdbm.sourceforge.net/).
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE JDBM PROJECT AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
+ * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
+ * CEES DE GROOT OR ANY CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Copyright 2001 (C) Alex Boisvert. All Rights Reserved.
+ * Contributions are Copyright (C) 2001 by their associated contributors.
+ *
+ */
+
+package jdbm.helper;
+
+import java.io.IOException;
+
+
+/**
+ * Serializer for byte arrays -- simple returns the byte array itself.  No actual
+ * serialization is performed.
+ *
+ * @author <a href="mailto:boisvert@intalio.com">Alex Boisvert</a>
+ */
+public final class ByteArraySerializer
+    implements Serializer
+{
+
+    /**
+     * Version id for serialization.
+     */
+    final static long serialVersionUID = 1L;
+
+
+    /**
+     * Static instance.
+     */
+    public static final ByteArraySerializer INSTANCE = new ByteArraySerializer();
+    
+    
+    /** 
+     * Serialize the content of an object into a byte array.
+     *
+     * @param obj Object to serialize
+     * @return a byte array representing the object's state
+     *
+     */
+    public byte[] serialize( Object obj ) 
+        throws IOException
+    {
+        return (byte[]) obj;
+    }
+
+    
+    /**
+     * Deserialize the content of an object from a byte array.
+     *
+     * @param serialized Byte array representation of the object
+     * @return deserialized object
+     *
+     */
+    public Object deserialize( byte[] serialized ) 
+        throws IOException
+    {
+        return serialized;
+    }    
+
+}

Added: directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/CacheEvictionException.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/CacheEvictionException.java?rev=1239581&view=auto
==============================================================================
--- directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/CacheEvictionException.java (added)
+++ directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/CacheEvictionException.java Thu Feb  2 12:38:39 2012
@@ -0,0 +1,74 @@
+/**
+ * JDBM LICENSE v1.00
+ *
+ * Redistribution and use of this software and associated documentation
+ * ("Software"), with or without modification, are permitted provided
+ * that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain copyright
+ *    statements and notices.  Redistributions must also contain a
+ *    copy of this document.
+ *
+ * 2. Redistributions in binary form must reproduce the
+ *    above copyright notice, this list of conditions and the
+ *    following disclaimer in the documentation and/or other
+ *    materials provided with the distribution.
+ *
+ * 3. The name "JDBM" must not be used to endorse or promote
+ *    products derived from this Software without prior written
+ *    permission of Cees de Groot.  For written permission,
+ *    please contact cg@cdegroot.com.
+ *
+ * 4. Products derived from this Software may not be called "JDBM"
+ *    nor may "JDBM" appear in their names without prior written
+ *    permission of Cees de Groot.
+ *
+ * 5. Due credit should be given to the JDBM Project
+ *    (http://jdbm.sourceforge.net/).
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE JDBM PROJECT AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
+ * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
+ * CEES DE GROOT OR ANY CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Copyright 2000 (C) Cees de Groot. All Rights Reserved.
+ * Contributions are Copyright (C) 2000 by their associated contributors.
+ *
+ * $Id: CacheEvictionException.java,v 1.4 2003/10/21 15:43:20 boisvert Exp $
+ */
+
+package jdbm.helper;
+
+/**
+ *  Exception that occurs during eviction of an object in the cache.
+ *
+ *  @author <a href="mailto:boisvert@intalio.com">Alex Boisvert</a>
+ */
+public class CacheEvictionException
+    extends Exception
+{
+
+    /**
+     * Nested exception -- the original exception that occured, if any.
+     */
+    protected Exception _nested;
+
+
+    public CacheEvictionException( Exception nested )
+    {
+        _nested = nested;
+    }
+
+    public Exception getNestedException()
+    {
+        return _nested;
+    }
+}

Added: directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/CachePolicy.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/CachePolicy.java?rev=1239581&view=auto
==============================================================================
--- directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/CachePolicy.java (added)
+++ directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/CachePolicy.java Thu Feb  2 12:38:39 2012
@@ -0,0 +1,139 @@
+/**
+ * JDBM LICENSE v1.00
+ *
+ * Redistribution and use of this software and associated documentation
+ * ("Software"), with or without modification, are permitted provided
+ * that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain copyright
+ *    statements and notices.  Redistributions must also contain a
+ *    copy of this document.
+ *
+ * 2. Redistributions in binary form must reproduce the
+ *    above copyright notice, this list of conditions and the
+ *    following disclaimer in the documentation and/or other
+ *    materials provided with the distribution.
+ *
+ * 3. The name "JDBM" must not be used to endorse or promote
+ *    products derived from this Software without prior written
+ *    permission of Cees de Groot.  For written permission,
+ *    please contact cg@cdegroot.com.
+ *
+ * 4. Products derived from this Software may not be called "JDBM"
+ *    nor may "JDBM" appear in their names without prior written
+ *    permission of Cees de Groot.
+ *
+ * 5. Due credit should be given to the JDBM Project
+ *    (http://jdbm.sourceforge.net/).
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE JDBM PROJECT AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
+ * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
+ * CEES DE GROOT OR ANY CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Copyright 2000 (C) Cees de Groot. All Rights Reserved.
+ * Contributions are Copyright (C) 2000 by their associated contributors.
+ *
+ * $Id: CachePolicy.java,v 1.5 2003/11/01 13:25:02 dranatunga Exp $
+ */
+package jdbm.helper;
+
+
+import java.util.Enumeration;
+
+
+/**
+ *  CachePolicity is an abstraction for different cache policies.
+ *  (ie. MRU, time-based, soft-refs, ...)
+ *
+ * @author <a href="mailto:boisvert@intalio.com">Alex Boisvert</a>
+ * @author <a href="mailto:dranatunga@users.sourceforge.net">Dilum Ranatunga</a>
+ */
+public interface CachePolicy<K,V>
+{
+    /**
+     * Place an object in the cache. If the cache does not currently contain
+     * an object for the key specified, this mapping is added. If an object
+     * currently exists under the specified key, the current object is
+     * replaced with the new object.
+     * <p>
+     * If the changes to the cache cause the eviction of any objects
+     * <strong>stored under other key(s)</strong>, events corresponding to
+     * the evictions are fired for each object. If an event listener is
+     * unable to handle the eviction, and throws a cache eviction exception,
+     * that exception is propagated to the caller. If such an exception is
+     * thrown, the cache itself should be left as it was before the
+     * <code>put()</code> operation was invoked: the the object whose
+     * eviction failed is still in the cache, and the new insertion or
+     * modification is reverted.
+     *
+     * @param key key for the cached object
+     * @param value the cached object
+     * @throws CacheEvictionException propagated if, while evicting objects
+     *     to make room for new object, an eviction listener encountered
+     *     this problem.
+     */
+    public void put( K key, V value ) throws CacheEvictionException;
+
+
+    /**
+     * Obtain the object stored under the key specified.
+     *
+     * @param key key the object was cached under
+     * @return the object if it is still in the cache, null otherwise.
+     */
+    public V get( K key );
+
+
+    /**
+     * Remove the object stored under the key specified. Note that since
+     * eviction notices are only fired when objects under <strong>different
+     * keys</strong> are evicted, no event is fired for any object stored
+     * under this key (see {@link #put(Object, Object) put( )}).
+     *
+     * @param key key the object was stored in the cache under.
+     */
+    public void remove( K key );
+
+
+    /**
+     * Remove all objects from the cache. Consistent with
+     * {@link #remove(Object) remove( )}, no eviction notices are fired.
+     */
+    public void removeAll();
+
+
+    /**
+     * Enumerate through the objects currently in the cache.
+     */
+    public Enumeration<V> elements();
+
+
+    /**
+     * Add a listener to this cache policy.
+     * <p>
+     * If this cache policy already contains a listener that is equal to
+     * the one being added, this call has no effect.
+     *
+     * @param listener the (non-null) listener to add to this policy
+     * @throws IllegalArgumentException if listener is null.
+     */
+    public void addListener( CachePolicyListener<V> listener ) throws IllegalArgumentException;
+
+    
+    /**
+     * Remove a listener from this cache policy. The listener is found
+     * using object equality, not identity.
+     *
+     * @param listener the listener to remove from this policy
+     */
+    public void removeListener( CachePolicyListener<V> listener );
+}

Added: directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/CachePolicyListener.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/CachePolicyListener.java?rev=1239581&view=auto
==============================================================================
--- directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/CachePolicyListener.java (added)
+++ directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/CachePolicyListener.java Thu Feb  2 12:38:39 2012
@@ -0,0 +1,75 @@
+/**
+ * JDBM LICENSE v1.00
+ *
+ * Redistribution and use of this software and associated documentation
+ * ("Software"), with or without modification, are permitted provided
+ * that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain copyright
+ *    statements and notices.  Redistributions must also contain a
+ *    copy of this document.
+ *
+ * 2. Redistributions in binary form must reproduce the
+ *    above copyright notice, this list of conditions and the
+ *    following disclaimer in the documentation and/or other
+ *    materials provided with the distribution.
+ *
+ * 3. The name "JDBM" must not be used to endorse or promote
+ *    products derived from this Software without prior written
+ *    permission of Cees de Groot.  For written permission,
+ *    please contact cg@cdegroot.com.
+ *
+ * 4. Products derived from this Software may not be called "JDBM"
+ *    nor may "JDBM" appear in their names without prior written
+ *    permission of Cees de Groot.
+ *
+ * 5. Due credit should be given to the JDBM Project
+ *    (http://jdbm.sourceforge.net/).
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE JDBM PROJECT AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
+ * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
+ * CEES DE GROOT OR ANY CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Copyright 2000 (C) Cees de Groot. All Rights Reserved.
+ * Contributions are Copyright (C) 2000 by their associated contributors.
+ *
+ * $Id: CachePolicyListener.java,v 1.3 2003/11/01 13:25:41 dranatunga Exp $
+ */
+
+package jdbm.helper;
+
+/**
+ * Callback interface between {@link CachePolicy} and a Cache implementation
+ * to notify about cached object eviction.
+ * <p>
+ * Note that <code>CachePolicy</code> implementations typically use
+ * <em>object equality</em> when removing listeners, so concrete
+ * implementations of this interface should also pay attention to
+ * their {@link Object#equals(Object)} and {@link Object#hashCode()}
+ * methods.
+ *
+ * @author <a href="mailto:boisvert@intalio.com">Alex Boisvert</a>
+ */
+public interface CachePolicyListener<T> 
+{
+    /**
+     * Notification that the cache this listener is attached to is evicting
+     * the object indicated.
+     *
+     * @param obj object being evicted from cache
+     * @throws CacheEvictionException if this listener encountered problems
+     *     while preparing for the specified object's eviction. For example,
+     *     a listener may try to persist the object to disk, and encounter
+     *     an <code>IOException</code>.
+     */
+    public void cacheObjectEvicted( T obj ) throws CacheEvictionException;
+}

Added: directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/Conversion.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/Conversion.java?rev=1239581&view=auto
==============================================================================
--- directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/Conversion.java (added)
+++ directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/Conversion.java Thu Feb  2 12:38:39 2012
@@ -0,0 +1,224 @@
+/**
+ * JDBM LICENSE v1.00
+ *
+ * Redistribution and use of this software and associated documentation
+ * ("Software"), with or without modification, are permitted provided
+ * that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain copyright
+ *    statements and notices.  Redistributions must also contain a
+ *    copy of this document.
+ *
+ * 2. Redistributions in binary form must reproduce the
+ *    above copyright notice, this list of conditions and the
+ *    following disclaimer in the documentation and/or other
+ *    materials provided with the distribution.
+ *
+ * 3. The name "JDBM" must not be used to endorse or promote
+ *    products derived from this Software without prior written
+ *    permission of Cees de Groot.  For written permission,
+ *    please contact cg@cdegroot.com.
+ *
+ * 4. Products derived from this Software may not be called "JDBM"
+ *    nor may "JDBM" appear in their names without prior written
+ *    permission of Cees de Groot.
+ *
+ * 5. Due credit should be given to the JDBM Project
+ *    (http://jdbm.sourceforge.net/).
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE JDBM PROJECT AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
+ * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
+ * CEES DE GROOT OR ANY CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Copyright 2001 (C) Alex Boisvert. All Rights Reserved.
+ * Contributions are Copyright (C) 2001 by their associated contributors.
+ *
+ */
+
+package jdbm.helper;
+
+import org.apache.directory.server.i18n.I18n;
+
+
+/**
+ * Miscelaneous conversion utility methods.
+ *
+ * @author <a href="mailto:boisvert@intalio.com">Alex Boisvert</a>
+ */
+public class Conversion
+{
+
+    /**
+     * Convert a string into a byte array.
+     */
+    public static byte[] convertToByteArray( String s )
+    {
+        try {
+            // see the following page for character encoding
+            // http://java.sun.com/products/jdk/1.1/docs/guide/intl/encoding.doc.html
+            return s.getBytes( "UTF8" );
+        } catch ( java.io.UnsupportedEncodingException uee ) {
+            uee.printStackTrace();
+            throw new Error( I18n.err( I18n.ERR_527 ) );
+        }
+    }
+
+
+    /**
+     * Convert a byte into a byte array.
+     */
+    public static byte[] convertToByteArray( byte n )
+    {
+        n = (byte)( n ^ ( (byte) 0x80 ) ); // flip MSB because "byte" is signed
+        return new byte[] { n };
+    }
+
+
+    /**
+     * Convert a short into a byte array.
+     */
+    public static byte[] convertToByteArray( short n )
+    {
+        n = (short) ( n ^ ( (short) 0x8000 ) ); // flip MSB because "short" is signed
+        byte[] key = new byte[ 2 ];
+        pack2( key, 0, n );
+        return key;
+    }
+
+
+    /**
+     * Convert an int into a byte array.
+     */
+    public static byte[] convertToByteArray( int n )
+    {
+        n = (n ^ 0x80000000); // flip MSB because "int" is signed
+        byte[] key = new byte[4];
+        pack4(key, 0, n);
+        return key;
+    }
+
+
+    /**
+     * Convert a long into a byte array.
+     */
+    public static byte[] convertToByteArray( long n )
+    {
+        n = (n ^ 0x8000000000000000L); // flip MSB because "long" is signed
+        byte[] key = new byte[8];
+        pack8( key, 0, n );
+        return key;
+    }
+
+
+    /**
+     * Convert a byte array (encoded as UTF-8) into a String
+     */
+    public static String convertToString( byte[] buf )
+    {
+        try {
+            // see the following page for character encoding
+            // http://java.sun.com/products/jdk/1.1/docs/guide/intl/encoding.doc.html
+            return new String( buf, "UTF8" );
+        } catch ( java.io.UnsupportedEncodingException uee ) {
+            uee.printStackTrace();
+            throw new Error( I18n.err( I18n.ERR_527 ) );
+        }
+    }
+
+
+    /**
+     * Convert a byte array into an integer (signed 32-bit) value.
+     */
+    public static int convertToInt( byte[] buf )
+    {
+        int value = unpack4( buf, 0 );
+        value = ( value ^ 0x80000000 ); // flip MSB because "int" is signed
+        return value;
+    }
+
+
+    /**
+     * Convert a byte array into a long (signed 64-bit) value.
+     */
+    public static long convertToLong( byte[] buf )
+    {
+        long value = ( (long) unpack4( buf, 0 ) << 32  )
+                     + ( unpack4( buf, 4 ) & 0xFFFFFFFFL );
+        value = ( value ^ 0x8000000000000000L ); // flip MSB because "long" is signed
+        return value;
+    }
+
+
+
+
+    static int unpack4( byte[] buf, int offset )
+    {
+        int value = ( buf[ offset ] << 24 )
+            | ( ( buf[ offset+1 ] << 16 ) & 0x00FF0000 )
+            | ( ( buf[ offset+2 ] << 8 ) & 0x0000FF00 )
+            | ( ( buf[ offset+3 ] << 0 ) & 0x000000FF );
+
+        return value;
+    }
+
+
+    static final void pack2( byte[] data, int offs, int val )
+    {
+        data[offs++] = (byte) ( val >> 8 );
+        data[offs++] = (byte) val;
+    }
+
+
+    static final void pack4( byte[] data, int offs, int val )
+    {
+        data[offs++] = (byte) ( val >> 24 );
+        data[offs++] = (byte) ( val >> 16 );
+        data[offs++] = (byte) ( val >> 8 );
+        data[offs++] = (byte) val;
+    }
+
+
+    static final void pack8( byte[] data, int offs, long val )
+    {
+        pack4( data, 0, (int) ( val >> 32 ) );
+        pack4( data, 4, (int) val );
+    }
+
+
+    /**
+     * Test static methods
+     */
+    public static void main( String[] args )
+    {
+        byte[] buf;
+
+        buf = convertToByteArray( (int) 5 );
+        System.out.println( "int value of 5 is: " + convertToInt( buf ) );
+
+        buf = convertToByteArray( (int) -1 );
+        System.out.println( "int value of -1 is: " + convertToInt( buf ) );
+
+        buf = convertToByteArray( (int) 22111000 );
+        System.out.println( "int value of 22111000 is: " + convertToInt( buf ) );
+
+
+        buf = convertToByteArray( (long) 5L );
+        System.out.println( "long value of 5 is: " + convertToLong( buf ) );
+
+        buf = convertToByteArray( (long) -1L );
+        System.out.println( "long value of -1 is: " + convertToLong( buf ) );
+
+        buf = convertToByteArray( (long) 1112223334445556667L );
+        System.out.println( "long value of 1112223334445556667 is: " + convertToLong( buf ) );
+    }
+
+}

Added: directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/DefaultSerializer.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/DefaultSerializer.java?rev=1239581&view=auto
==============================================================================
--- directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/DefaultSerializer.java (added)
+++ directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/DefaultSerializer.java Thu Feb  2 12:38:39 2012
@@ -0,0 +1,102 @@
+/**
+ * JDBM LICENSE v1.00
+ *
+ * Redistribution and use of this software and associated documentation
+ * ("Software"), with or without modification, are permitted provided
+ * that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain copyright
+ *    statements and notices.  Redistributions must also contain a
+ *    copy of this document.
+ *
+ * 2. Redistributions in binary form must reproduce the
+ *    above copyright notice, this list of conditions and the
+ *    following disclaimer in the documentation and/or other
+ *    materials provided with the distribution.
+ *
+ * 3. The name "JDBM" must not be used to endorse or promote
+ *    products derived from this Software without prior written
+ *    permission of Cees de Groot.  For written permission,
+ *    please contact cg@cdegroot.com.
+ *
+ * 4. Products derived from this Software may not be called "JDBM"
+ *    nor may "JDBM" appear in their names without prior written
+ *    permission of Cees de Groot.
+ *
+ * 5. Due credit should be given to the JDBM Project
+ *    (http://jdbm.sourceforge.net/).
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE JDBM PROJECT AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
+ * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
+ * CEES DE GROOT OR ANY CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Copyright 2001 (C) Alex Boisvert. All Rights Reserved.
+ * Contributions are Copyright (C) 2001 by their associated contributors.
+ *
+ */
+package jdbm.helper;
+
+
+import java.io.IOException;
+
+
+/**
+ * Default java serializer.
+ *
+ * @author <a href="mailto:boisvert@intalio.com">Alex Boisvert</a>
+ */
+public class DefaultSerializer implements Serializer
+{
+    private static final long serialVersionUID = -3818545055661017388L;
+
+    public static final DefaultSerializer INSTANCE = new DefaultSerializer();
+    
+    
+    /**
+     * Construct a DefaultSerializer.
+     */
+    public DefaultSerializer()
+    {
+        // no op
+    }
+
+    
+    /**
+     * Serialize the content of an object into a byte array.
+     *
+     * @param obj Object to serialize
+     * @return a byte array representing the object's state
+     */
+     public byte[] serialize( Object obj ) throws IOException
+     {
+         return Serialization.serialize( obj );
+     }
+        
+        
+    /**
+     * De-serialize the content of an object from a byte array.
+     *
+     * @param serialized Byte array representation of the object
+     * @return de-serialized object
+     */
+     public Object deserialize( byte[] serialized ) throws IOException
+     {
+         try 
+         {
+            return Serialization.deserialize( serialized );
+         } 
+         catch ( ClassNotFoundException except ) 
+         {
+            throw new WrappedRuntimeException( except );
+         }
+     }
+}

Added: directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/EntryIO.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/EntryIO.java?rev=1239581&view=auto
==============================================================================
--- directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/EntryIO.java (added)
+++ directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/EntryIO.java Thu Feb  2 12:38:39 2012
@@ -0,0 +1,34 @@
+/*
+ *  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 jdbm.helper;
+
+import java.io.IOException;
+
+/**
+ * 
+ * TODO EntryIO.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public interface EntryIO<K, V>
+{
+    public V read( K key, Serializer serializer) throws IOException;
+    public void write( K key, V value, Serializer serializer ) throws IOException;
+}
\ No newline at end of file

Added: directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/ExplicitList.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/ExplicitList.java?rev=1239581&view=auto
==============================================================================
--- directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/ExplicitList.java (added)
+++ directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/ExplicitList.java Thu Feb  2 12:38:39 2012
@@ -0,0 +1,234 @@
+/*
+ *  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 jdbm.helper;
+
+
+/**
+ * A simple doubly linked list implementation that can be used when fast remove operations are desired.
+ * Objects are inserted into the list through an anchor (Link). When object is to be removed from the
+ * list, this anchor is provided by the client again and this class can do the remove operation in O(1)
+ * using the given anchor.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class ExplicitList<T>
+{
+
+    private Link<T> head = new Link<T>( null );
+
+    private int listSize = 0;
+    
+    public static class Link<V>
+    {
+        private V element;
+        private Link<V> next;
+        private Link<V> prev;
+
+
+        public Link( V element )
+        {
+            this.element = element;
+            this.reset();
+        }
+
+
+        public Link<V> getNext()
+        {
+            return next;
+        }
+
+
+        public void setNext( Link<V> next )
+        {
+            this.next = next;
+        }
+
+
+        public Link<V> getPrev()
+        {
+            return prev;
+        }
+
+
+        public void setPrev( Link<V> prev )
+        {
+            this.prev = prev;
+        }
+
+
+        public void remove()
+        {
+            if ( isLinked() == false )
+            {
+                throw new IllegalStateException( "Trying to remove from list an unlinked link" );
+            }
+ 
+            this.getPrev().setNext( this.getNext() );
+            this.getNext().setPrev( this.getPrev() );
+            this.reset();
+        }
+
+
+        public void addAfter( Link<V> after )
+        {
+            if ( this.isUnLinked() == false )
+            {
+                throw new IllegalStateException( "Trying to add to list already linked link: " + this );
+            }
+            
+            after.getNext().setPrev( this );
+            this.setNext( after.getNext() );
+            after.setNext( this );
+            this.setPrev( after );
+        }
+
+
+        public void addBefore( Link<V> before )
+        {
+            if ( this.isUnLinked() == false )
+            {
+                throw new IllegalStateException( "Trying to add to list already linked link: " + this );
+            }
+  
+            before.getPrev().setNext( this );
+            this.setPrev( before.getPrev() );
+            before.setPrev( this );
+            this.setNext( before );
+        }
+
+
+        /**
+         * Splices the given list by making this link as the new head.
+         *
+         * @param listHead head of the existing list
+         */
+        public void splice( Link<V> listHead )
+        {
+            Link<V> prevLink = listHead.getPrev();
+            listHead.setPrev( this );
+            prevLink.setNext( this );
+            this.setNext( listHead );
+            this.setPrev( prevLink );
+        }
+
+
+        public boolean isUnLinked()
+        {
+            return ( ( prev == this ) && ( next == this ) );
+        }
+
+
+        public boolean isLinked()
+        {
+            return ( !this.isUnLinked() );
+        }
+
+
+        public void reset()
+        {
+            next = this;
+            prev = this;
+        }
+
+
+        public void uninit()
+        {
+            if ( this.isUnLinked() == false )
+            {
+                throw new IllegalStateException( " Unitializing a still linked entry" + this );
+            }
+   
+            element = null;
+        }
+
+
+        public V getElement()
+        {
+            return this.element;
+        }
+        
+        @Override
+        public String toString()
+        {
+            StringBuilder sb = new StringBuilder();
+            sb.append( "Link: " ).append( this ).append( " " );
+            sb.append( "(next: " ).append( next );
+            sb.append( ",prev: " ).append( prev ).append(")");            
+            sb.append( "\n" );
+            
+            return sb.toString();
+        }
+    }
+
+
+    public void remove( Link<T> link )
+    {
+        if ( listSize <= 0 )
+        {
+            throw new IllegalStateException( "Trying to remove link " + link + " from a list with no elements" );
+        }
+
+        listSize--;
+        link.remove();
+    }
+
+
+    public void addFirst( Link<T> link )
+    {
+        listSize++;
+        link.addAfter( head );
+    }
+
+
+    public void addLast( Link<T> link )
+    {
+        listSize++;
+        link.addBefore( head );
+    }
+
+
+    public Link<T> begin()
+    {
+        return ( head.getNext() );
+    }
+
+
+    public Link<T> end()
+    {
+        return head;
+    }
+    
+    public int size()
+    {
+        return listSize;
+    }
+    
+    @Override
+    public String toString()
+    {
+        StringBuilder sb = new StringBuilder();
+        sb.append( "List: " );
+        sb.append( "(size: " ).append( listSize ).append( ")" );
+        sb.append( "\n" );
+        
+        return sb.toString();
+    }
+
+}
\ No newline at end of file

Added: directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/FastIterator.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/FastIterator.java?rev=1239581&view=auto
==============================================================================
--- directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/FastIterator.java (added)
+++ directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/FastIterator.java Thu Feb  2 12:38:39 2012
@@ -0,0 +1,67 @@
+/**
+ * JDBM LICENSE v1.00
+ *
+ * Redistribution and use of this software and associated documentation
+ * ("Software"), with or without modification, are permitted provided
+ * that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain copyright
+ *    statements and notices.  Redistributions must also contain a
+ *    copy of this document.
+ *
+ * 2. Redistributions in binary form must reproduce the
+ *    above copyright notice, this list of conditions and the
+ *    following disclaimer in the documentation and/or other
+ *    materials provided with the distribution.
+ *
+ * 3. The name "JDBM" must not be used to endorse or promote
+ *    products derived from this Software without prior written
+ *    permission of Cees de Groot.  For written permission,
+ *    please contact cg@cdegroot.com.
+ *
+ * 4. Products derived from this Software may not be called "JDBM"
+ *    nor may "JDBM" appear in their names without prior written
+ *    permission of Cees de Groot.
+ *
+ * 5. Due credit should be given to the JDBM Project
+ *    (http://jdbm.sourceforge.net/).
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE JDBM PROJECT AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
+ * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
+ * CEES DE GROOT OR ANY CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Copyright 2000 (C) Cees de Groot. All Rights Reserved.
+ * Contributions are Copyright (C) 2000 by their associated contributors.
+ *
+ * $Id: FastIterator.java,v 1.2 2003/10/21 15:43:58 boisvert Exp $
+ */
+
+package jdbm.helper;
+
+
+/**
+ * Fast and simple iterator.
+ *
+ * @author <a href="boisvert@intalio.com">Alex Boisvert</a>
+ */
+public abstract class FastIterator
+{
+
+    /**
+     * Returns the next element in the interation.
+     *
+     * @return the next element in the iteration, or null if no more element.
+     */
+    public abstract Object next()
+        throws IterationException;
+
+}

Added: directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/IntegerComparator.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/IntegerComparator.java?rev=1239581&view=auto
==============================================================================
--- directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/IntegerComparator.java (added)
+++ directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/IntegerComparator.java Thu Feb  2 12:38:39 2012
@@ -0,0 +1,106 @@
+/**
+ * JDBM LICENSE v1.00
+ *
+ * Redistribution and use of this software and associated documentation
+ * ("Software"), with or without modification, are permitted provided
+ * that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain copyright
+ *    statements and notices.  Redistributions must also contain a
+ *    copy of this document.
+ *
+ * 2. Redistributions in binary form must reproduce the
+ *    above copyright notice, this list of conditions and the
+ *    following disclaimer in the documentation and/or other
+ *    materials provided with the distribution.
+ *
+ * 3. The name "JDBM" must not be used to endorse or promote
+ *    products derived from this Software without prior written
+ *    permission of Cees de Groot.  For written permission,
+ *    please contact cg@cdegroot.com.
+ *
+ * 4. Products derived from this Software may not be called "JDBM"
+ *    nor may "JDBM" appear in their names without prior written
+ *    permission of Cees de Groot.
+ *
+ * 5. Due credit should be given to the JDBM Project
+ *    (http://jdbm.sourceforge.net/).
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE JDBM PROJECT AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
+ * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
+ * CEES DE GROOT OR ANY CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Copyright 2001 (C) Alex Boisvert. All Rights Reserved.
+ * Contributions are Copyright (C) 2001 by their associated contributors.
+ *
+ */
+
+package jdbm.helper;
+
+import java.io.Serializable;
+import java.util.Comparator;
+
+import org.apache.directory.server.i18n.I18n;
+
+/**
+ * Comparator for Integer objects.
+ *
+ * @author <a href="mailto:cdaller@iicm.edu">Christof Dallermassl</a>
+ */
+public final class IntegerComparator
+    implements Comparator<Integer>, Serializable
+{
+
+    /**
+     * Version id for serialization.
+     */
+    final static long serialVersionUID = 1L;
+
+
+    /**
+     * Compare two objects.
+     *
+     * @param obj1 First object
+     * @param obj2 Second object
+     * @return a positive integer if obj1 > obj2, 0 if obj1 == obj2,
+     *         and a negative integer if obj1 < obj2
+     */
+    public int compare( Integer obj1, Integer obj2 )
+    {
+        if ( obj1 == obj2 ) {
+            return 0;
+        }
+
+        if ( obj1 == null ) {
+            throw new IllegalArgumentException( I18n.err( I18n.ERR_525 ) );
+        }
+
+        if ( obj2 == null ) {
+            throw new IllegalArgumentException( I18n.err( I18n.ERR_526 ) );
+        }
+
+        // complicated to avoid usage of Integer.compareTo, as this
+        // method is Java 1.2 only!
+        int int1 = obj1.intValue();
+        int int2 = obj2.intValue();
+        if ( int1 == int2 ) {
+            return 0;
+        }
+
+        if ( int1 < int2 ) {
+          return -1;
+        } else {
+          return 1;
+        }
+    }
+
+}

Added: directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/IntegerSerializer.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/IntegerSerializer.java?rev=1239581&view=auto
==============================================================================
--- directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/IntegerSerializer.java (added)
+++ directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/IntegerSerializer.java Thu Feb  2 12:38:39 2012
@@ -0,0 +1,100 @@
+/**
+ * JDBM LICENSE v1.00
+ *
+ * Redistribution and use of this software and associated documentation
+ * ("Software"), with or without modification, are permitted provided
+ * that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain copyright
+ *    statements and notices.  Redistributions must also contain a
+ *    copy of this document.
+ *
+ * 2. Redistributions in binary form must reproduce the
+ *    above copyright notice, this list of conditions and the
+ *    following disclaimer in the documentation and/or other
+ *    materials provided with the distribution.
+ *
+ * 3. The name "JDBM" must not be used to endorse or promote
+ *    products derived from this Software without prior written
+ *    permission of Cees de Groot.  For written permission,
+ *    please contact cg@cdegroot.com.
+ *
+ * 4. Products derived from this Software may not be called "JDBM"
+ *    nor may "JDBM" appear in their names without prior written
+ *    permission of Cees de Groot.
+ *
+ * 5. Due credit should be given to the JDBM Project
+ *    (http://jdbm.sourceforge.net/).
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE JDBM PROJECT AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
+ * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
+ * CEES DE GROOT OR ANY CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Copyright 2001 (C) Alex Boisvert. All Rights Reserved.
+ * Contributions are Copyright (C) 2001 by their associated contributors.
+ *
+ */
+
+package jdbm.helper;
+
+import java.io.IOException;
+
+/**
+ * Optimized serializer for integers.
+ *
+ * @author <a href="mailto:boisvert@intalio.com">Alex Boisvert</a>
+ */
+public class IntegerSerializer
+    implements Serializer
+{
+
+    
+    public static final IntegerSerializer INSTANCE = new IntegerSerializer();
+    
+    
+    /**
+     * Construct an IntegerSerializer.
+     */
+    public IntegerSerializer()
+    {
+        // no op
+    }
+
+    
+    /**
+     * Serialize the content of an object into a byte array.
+     *
+     * @param obj Object to serialize
+     * @return a byte array representing the object's state
+     */
+     public byte[] serialize( Object obj )
+        throws IOException
+     {
+         Integer number = (Integer) obj;
+         return Conversion.convertToByteArray( number.intValue() );
+     }
+        
+        
+    /**
+     * Deserialize the content of an object from a byte array.
+     *
+     * @param serialized Byte array representation of the object
+     * @return deserialized object
+     */
+     public Object deserialize( byte[] serialized )
+        throws IOException
+     {
+         int number = Conversion.convertToInt( serialized );
+         return Integer.valueOf( number );
+     }
+
+}

Added: directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/IterationException.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/IterationException.java?rev=1239581&view=auto
==============================================================================
--- directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/IterationException.java (added)
+++ directory/apacheds/trunk/jdbm2/src/main/java/jdbm/helper/IterationException.java Thu Feb  2 12:38:39 2012
@@ -0,0 +1,96 @@
+/**
+ * JDBM LICENSE v1.00
+ *
+ * Redistribution and use of this software and associated documentation
+ * ("Software"), with or without modification, are permitted provided
+ * that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain copyright
+ *    statements and notices.  Redistributions must also contain a
+ *    copy of this document.
+ *
+ * 2. Redistributions in binary form must reproduce the
+ *    above copyright notice, this list of conditions and the
+ *    following disclaimer in the documentation and/or other
+ *    materials provided with the distribution.
+ *
+ * 3. The name "JDBM" must not be used to endorse or promote
+ *    products derived from this Software without prior written
+ *    permission of Cees de Groot.  For written permission,
+ *    please contact cg@cdegroot.com.
+ *
+ * 4. Products derived from this Software may not be called "JDBM"
+ *    nor may "JDBM" appear in their names without prior written
+ *    permission of Cees de Groot.
+ *
+ * 5. Due credit should be given to the JDBM Project
+ *    (http://jdbm.sourceforge.net/).
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE JDBM PROJECT AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
+ * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
+ * CEES DE GROOT OR ANY CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Copyright 2000 (C) Cees de Groot. All Rights Reserved.
+ * Contributions are Copyright (C) 2000 by their associated contributors.
+ *
+ * $Id: IterationException.java,v 1.2 2003/09/21 15:47:00 boisvert Exp $
+ */
+
+package jdbm.helper;
+
+
+/**
+ * Iteration exception.
+ *
+ * @author <a href="boisvert@intalio.com">Alex Boisvert</a>
+ */
+public class IterationException
+    extends WrappedRuntimeException
+{
+
+    /**
+     * Construct a new iteration exception wrapping an underlying exception
+     * and providing a message.
+     *
+     * @param message The exception message
+     * @param except The underlying exception
+     */
+    public IterationException( String message, Exception except )
+    {
+        super( message, except );
+    }
+
+
+    /**
+     * Construct a new iteration exception with a message.
+     *
+     * @param message The exception message
+     */
+    public IterationException( String message )
+    {
+        super( message, null );
+    }
+
+
+    /**
+     * Construct a new iteration exception wrapping an underlying exception.
+     *
+     * @param except The underlying exception
+     */
+    public IterationException( Exception except )
+    {
+        super( except );
+    }
+
+}
+
+



Mime
View raw message