directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: rev 10126 - in incubator/directory/rms/trunk/je/src/java/org/apache/rms/je: . sequence
Date Tue, 20 Apr 2004 17:45:58 GMT
Author: akarasulu
Date: Tue Apr 20 10:45:56 2004
New Revision: 10126

Added:
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/JeUtils.java   (contents, props
changed)
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/sequence/
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/sequence/DefaultSequence.java
      - copied, changed from rev 10044, incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/DefaultSequence.java
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/sequence/JeLoggingSequenceDaoMonitor.java
      - copied, changed from rev 10044, incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/JeLoggingSequenceDaoMonitor.java
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/sequence/JeSequenceDao.java
      - copied, changed from rev 10044, incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/JeSequenceDao.java
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/sequence/JeSequenceFactory.java
      - copied, changed from rev 10044, incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/JeSequenceFactory.java
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/sequence/LoggingSequenceDaoMonitor.java
      - copied, changed from rev 10044, incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/LoggingSequenceDaoMonitor.java
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/sequence/Sequence.java
      - copied, changed from rev 10044, incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/Sequence.java
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/sequence/SequenceDao.java
      - copied, changed from rev 10044, incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/SequenceDao.java
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/sequence/SequenceDaoMonitor.java
      - copied, changed from rev 10044, incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/SequenceDaoMonitor.java
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/sequence/SequenceFactory.java
      - copied, changed from rev 10044, incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/SequenceFactory.java
Removed:
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/DefaultSequence.java
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/JeLoggingSequenceDaoMonitor.java
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/JeSequenceDao.java
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/JeSequenceFactory.java
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/LoggingSequenceDaoMonitor.java
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/Sequence.java
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/SequenceDao.java
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/SequenceDaoMonitor.java
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/SequenceFactory.java
Modified:
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/AbstractJeTest.java
Log:
Commit changes ...

o moved all sequence related classes into the sequence package
o added JeUtils for various primitive encode decode ops and other ops w/ je
o completed the BitPermissionIndexDAO

TODOs

o eventually some of the sequence interfaces should move out into the xdbm
  project packages
o need to unit test BitPermissionIndexDAO
o check in test cases and move other classes into their respective packages



Modified: incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/AbstractJeTest.java
==============================================================================
--- incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/AbstractJeTest.java	(original)
+++ incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/AbstractJeTest.java	Tue Apr
20 10:45:56 2004
@@ -19,10 +19,8 @@
 
 import junit.framework.*;
 import com.sleepycat.je.*;
-import com.sleepycat.bind.serial.StoredClassCatalog;
 
-import java.io.File;
-import java.io.IOException;
+import java.io.File ;
 
 
 /**
@@ -38,6 +36,12 @@
     protected Environment env = null ;
     /** the test JE Database */
     protected Database db = null ;
+    /** common key entry created for convenience */
+    protected DatabaseEntry key = new DatabaseEntry() ;
+    /** common key entry created for convenience */
+    protected DatabaseEntry value = new DatabaseEntry() ;
+    /** the database configuration */
+    protected DatabaseConfig dbConfig = new DatabaseConfig() ;
 
 
     protected void setUp() throws Exception
@@ -53,7 +57,6 @@
         }
 
         env = new Environment( f, envConfig ) ;
-        DatabaseConfig dbConfig = new DatabaseConfig() ;
         dbConfig.setAllowCreate( true ) ;
         db = env.openDatabase( null, "testDb", dbConfig ) ;
     }

Added: incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/JeUtils.java
==============================================================================
--- (empty file)
+++ incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/JeUtils.java	Tue Apr 20 10:45:56
2004
@@ -0,0 +1,249 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed 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.rms.je ;
+
+
+import com.sleepycat.je.* ;
+
+
+/**
+ * Utility functions to use with JE.
+ * 
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class JeUtils
+{
+    /**
+     * Gets the last duplicate value of a key within a database.  It's really
+     * tough to get the last value of a database key.  There are basically 3
+     * steps required to do this successfully:
+     *
+     * <pre>
+     * Looking for last value of key '2' which should be 'c':
+     *
+     * 1). Advance to key '2'
+     *
+     *      -----
+     *      |1-a|
+     *   -->|2-b|
+     *      |2-c|
+     *      |3-d|
+     *      |3-e|
+     *      -----
+     *
+     * 2). Step forward one step w/o dups but check that we're not the last
+     *     key in the db.  If we are then we just advance to the very last
+     *     value in the db using Cursor.getLast() to take us there.
+     *      -----
+     *      |1-a|
+     *      |2-b|
+     *      |2-c|
+     *   -->|3-d|
+     *      |3-e|
+     *      -----
+     *
+     * 3). Step back one step with dups
+     *      -----
+     *      |1-a|
+     *      |2-b|
+     *   -->|2-c|
+     *      |3-d|
+     *      |3-e|
+     *      -----
+     * </pre>
+     *
+     * @param cursor a database cursor to positon at the last value of the key
+     * @param key the pre-populated key to find the last value for
+     * @param value the value to populate with the last value
+     * @param mode the locking mode to use when making calls
+     * @return the cursor positioned on the last duplicate of the key
+     * @throws DatabaseException any failures while accessing the db
+     */
+    public static OperationStatus
+            getSearchLastDuplicate( Cursor cursor,
+                                    DatabaseEntry key,
+                                    DatabaseEntry value,
+                                    LockMode mode ) throws DatabaseException
+    {
+        OperationStatus status = null ;
+
+        /*
+         *  1). goto the first value of the key
+         */
+        status = cursor.getSearchKey( key, value, mode ) ;
+        if ( status != OperationStatus.SUCCESS )
+        {
+            return status ;
+        }
+
+        /*
+         *  2). attempt to goto the next key - if the next key does not exist
+         *      because we're the last key in the db then we advance the
+         *      cursor to the very last record of the db and return status.
+         */
+        status = cursor.getNextNoDup( key, value, mode ) ;
+        if ( status == OperationStatus.NOTFOUND )
+        {
+            status = cursor.getLast( key, value, mode ) ;
+            return status ;
+        }
+
+        /*
+         *  3). take a step backwords to the previous duplicate which is the
+         *      last value for the key with duplicates and return status.
+         */
+        status = cursor.getPrev( key, value, mode ) ;
+        return status ;
+    }
+
+    /**
+     * Encodes a integer value into a byte array of at least length 4 if not
+     * a new array is allocated.
+     *
+     * @param value the int value to encode
+     * @param encoded the byte array to populate with encoded data
+     * @return the encoded data within the byte array argument or a new array
+     */
+    public static byte[] encodeInt( int value, byte[] encoded )
+    {
+        if ( encoded == null )
+        {
+            encoded = new byte[4] ;
+        }
+
+        if ( value < 0 )
+        {
+            value &= ~0x80000000 ;
+        }
+        else
+        {
+            value |= 0x80000000 ;
+        }
+
+        encoded[0] = ( byte ) ( value >>> 24 ) ;
+        encoded[1] = ( byte ) ( value >>> 16 ) ;
+        encoded[2] = ( byte ) ( value >>>  8 ) ;
+        encoded[3] = ( byte )   value ;
+
+        return encoded ;
+    }
+
+    /**
+     * Conveinence method that calls encodeInt(int, byte[]) with a null byte
+     * buffer so it must allocate the byte[] to return instead of reusing one.
+     *
+     * @param value the int value be encoded into a byte[]
+     * @return the encoded bytes
+     */
+    public static byte[] encodeInt( int value )
+    {
+        return encodeInt( value, ( byte[] ) null ) ;
+    }
+
+    /**
+     * Encodes a value into a byte array and stuffs it into a supplied
+     * database entry if the entry is not null and has enough space.
+     *
+     * @param value the integer to encode
+     * @param entry the entry to stuff the encoded bytes into
+     * @return the entry argument or a new DatabaseEntry if entry is null
+     */
+    public static DatabaseEntry encodeInt( int value, DatabaseEntry entry )
+    {
+        // null entry require new one to be created
+        if ( entry == null )
+        {
+            entry = new DatabaseEntry( encodeInt( value ) ) ;
+        }
+        // entry's data has space for a full integer so decode into it
+        else if ( entry.getData() != null && entry.getData().length >= 4 )
+        {
+            encodeInt( value, entry.getData() ) ;
+        }
+        // entry's data does not have enough space so allocate and set
+        else
+        {
+            entry.setData( encodeInt( value ) ) ;
+        }
+
+        return entry ;
+    }
+
+    /**
+     * Decodes an integer from a byte[] using the first four bytes with the
+     * most significant byte first to the least significant byte last.  Extra
+     * bytes are ignored and missing bytes are interpreted as 0x00 and a null
+     * array or zero length array returns 0.
+     *
+     * @param value the encoded integer bytes
+     * @return the decoded integer
+     */
+    public static int decodeInt( byte[] value )
+    {
+        long c1 = 0, c2 = 0, c3 = 0, c4 = 0 ;
+
+        if ( value == null || value.length == 0 )
+        {
+            return 0 ;
+        }
+
+        if ( value.length >= 1 )
+        {
+            c1 = value[0] ;
+        }
+
+        if ( value.length >= 2 )
+        {
+            c2 = value[1] ;
+        }
+
+        if ( value.length >= 3 )
+        {
+            c3 = value[2] ;
+        }
+
+        if ( value.length >= 4 )
+        {
+            c4 = value[3] ;
+        }
+
+        int val = ( int ) ( (c1 << 24) | (c2 << 16) | (c3 << 8) | c4 )
;
+        if ( val < 0 )
+            val &= ~0x80000000 ;
+        else
+            val |= 0x80000000 ;
+        return val ;
+    }
+
+    /**
+     * Decoded an encoded integer within a database entry.  Null entries
+     * will return zero.
+     *
+     * @param entry the entry carrying the serialized bytes to decode
+     * @return the decoded integer
+     */
+    public static int decodeInt( DatabaseEntry entry )
+    {
+        if ( entry == null )
+        {
+            return 0 ;
+        }
+
+        return decodeInt( entry.getData() ) ;
+    }
+}

Copied: incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/sequence/DefaultSequence.java
(from rev 10044, incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/DefaultSequence.java)
==============================================================================
--- incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/DefaultSequence.java	(original)
+++ incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/sequence/DefaultSequence.java
Tue Apr 20 10:45:56 2004
@@ -14,9 +14,9 @@
  *   limitations under the License.
  *
  */
-package org.apache.rms.je;
+package org.apache.rms.je.sequence ;
 
-import org.apache.rms.RmsException;
+import org.apache.rms.RmsException ;
 
 
 /**

Copied: incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/sequence/JeLoggingSequenceDaoMonitor.java
(from rev 10044, incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/JeLoggingSequenceDaoMonitor.java)
==============================================================================
--- incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/JeLoggingSequenceDaoMonitor.java
(original)
+++ incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/sequence/JeLoggingSequenceDaoMonitor.java
Tue Apr 20 10:45:56 2004
@@ -14,11 +14,12 @@
  *   limitations under the License.
  *
  */
-package org.apache.rms.je ;
+package org.apache.rms.je.sequence ;
 
 
 import org.apache.commons.logging.Log ;
 import org.apache.commons.logging.LogFactory ;
+import org.apache.rms.je.sequence.LoggingSequenceDaoMonitor;
 import com.sleepycat.je.OperationStatus;
 
 

Copied: incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/sequence/JeSequenceDao.java
(from rev 10044, incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/JeSequenceDao.java)
==============================================================================
--- incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/JeSequenceDao.java	(original)
+++ incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/sequence/JeSequenceDao.java
Tue Apr 20 10:45:56 2004
@@ -14,15 +14,13 @@
  *   limitations under the License.
  *
  */
-package org.apache.rms.je ;
+package org.apache.rms.je.sequence ;
 
 import org.apache.rms.RmsException ;
+import org.apache.rms.je.JeUtils;
 
 import com.sleepycat.je.* ;
-import com.sleepycat.bind.EntryBinding ;
-import com.sleepycat.bind.tuple.TupleBinding ;
 
-import java.io.IOException ;
 import java.io.UnsupportedEncodingException ;
 
 
@@ -41,8 +39,6 @@
     private Database db ;
     /** the monitor used to track activities */
     private SequenceDaoMonitor monitor = new JeLoggingSequenceDaoMonitor() ;
-    /** the primitive Integer binding */
-    EntryBinding binding = TupleBinding.getPrimitiveBinding( Integer.class ) ;
 
 
     /**
@@ -63,7 +59,8 @@
     public void create( String name ) throws RmsException
     {
         DatabaseEntry key = buildKey( name ) ;
-        DatabaseEntry value = buildValue( new Integer( 0 ) ) ;
+        DatabaseEntry value = JeUtils
+                .encodeInt( 0, ( DatabaseEntry ) null ) ;
         OperationStatus status = null ;
 
         try
@@ -117,7 +114,6 @@
         OperationStatus status = null ;
         DatabaseEntry key = buildKey( name ) ;
         DatabaseEntry value = new DatabaseEntry() ;
-        Integer intObj = null ;
 
         try
         {
@@ -129,18 +125,8 @@
             throw new RmsException( e ) ;
         }
 
-        try
-        {
-            intObj = ( Integer ) binding.entryToObject( value ) ;
-        }
-        catch ( IOException e )
-        {
-            monitor.failedOnNextValue( name, e ) ;
-            throw new RmsException( e ) ;
-        }
-
-        intObj = new Integer( intObj.intValue() + 1 ) ;
-        value = buildValue( intObj ) ;
+        int intValue = JeUtils.decodeInt( value ) + 1 ;
+        value = JeUtils.encodeInt( intValue, value ) ;
 
         try
         {
@@ -152,7 +138,7 @@
             throw new RmsException( e ) ;
         }
 
-        return intObj.intValue() ;
+        return intValue ;
     }
 
 
@@ -166,7 +152,6 @@
         OperationStatus status = null ;
         DatabaseEntry key = buildKey( name ) ;
         DatabaseEntry value = new DatabaseEntry() ;
-        Integer intObj = null ;
 
         try
         {
@@ -178,17 +163,7 @@
             throw new RmsException( e ) ;
         }
 
-        try
-        {
-            intObj = ( Integer ) binding.entryToObject( value ) ;
-        }
-        catch ( IOException e )
-        {
-            monitor.failedOnCurrentValue( name, e ) ;
-            throw new RmsException( e ) ;
-        }
-
-        return intObj.intValue() ;
+        return JeUtils.decodeInt( value ) ;
     }
 
 
@@ -215,31 +190,5 @@
         }
 
         return new DatabaseEntry( keyData ) ;
-    }
-
-
-    /**
-     * Builds the DatabaseEntry value from an Integer.
-     *
-     * @param value the integer object value
-     * @return the serialized database entry
-     * @throws RmsException if there are failures while binding the key
-     */
-    private DatabaseEntry buildValue( Integer value )
-        throws RmsException
-    {
-        DatabaseEntry entry = new DatabaseEntry() ;
-
-        try
-        {
-            binding.objectToEntry( value, entry ) ;
-        }
-        catch ( IOException e )
-        {
-            monitor.failure( e ) ;
-            throw new RmsException( e ) ;
-        }
-
-        return entry ;
     }
 }

Copied: incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/sequence/JeSequenceFactory.java
(from rev 10044, incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/JeSequenceFactory.java)
==============================================================================
--- incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/JeSequenceFactory.java	(original)
+++ incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/sequence/JeSequenceFactory.java
Tue Apr 20 10:45:56 2004
@@ -14,10 +14,12 @@
  *   limitations under the License.
  *
  */
-package org.apache.rms.je ;
+package org.apache.rms.je.sequence ;
 
 
 import org.apache.rms.RmsException ;
+import org.apache.rms.je.sequence.DefaultSequence;
+import org.apache.rms.je.sequence.JeSequenceDao;
 
 
 /**

Copied: incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/sequence/LoggingSequenceDaoMonitor.java
(from rev 10044, incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/LoggingSequenceDaoMonitor.java)
==============================================================================
--- incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/LoggingSequenceDaoMonitor.java
(original)
+++ incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/sequence/LoggingSequenceDaoMonitor.java
Tue Apr 20 10:45:56 2004
@@ -14,11 +14,12 @@
  *   limitations under the License.
  *
  */
-package org.apache.rms.je ;
+package org.apache.rms.je.sequence ;
 
 
 import org.apache.commons.logging.Log ;
 import org.apache.commons.logging.LogFactory ;
+import org.apache.rms.je.sequence.SequenceDaoMonitor;
 
 
 /**

Copied: incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/sequence/Sequence.java
(from rev 10044, incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/Sequence.java)
==============================================================================
--- incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/Sequence.java	(original)
+++ incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/sequence/Sequence.java	Tue
Apr 20 10:45:56 2004
@@ -14,7 +14,7 @@
  *   limitations under the License.
  *
  */
-package org.apache.rms.je ;
+package org.apache.rms.je.sequence ;
 
 import org.apache.rms.RmsException;
 

Copied: incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/sequence/SequenceDao.java
(from rev 10044, incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/SequenceDao.java)
==============================================================================
--- incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/SequenceDao.java	(original)
+++ incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/sequence/SequenceDao.java
Tue Apr 20 10:45:56 2004
@@ -14,7 +14,7 @@
  *   limitations under the License.
  *
  */
-package org.apache.rms.je ;
+package org.apache.rms.je.sequence ;
 
 
 import org.apache.rms.RmsException ;

Copied: incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/sequence/SequenceDaoMonitor.java
(from rev 10044, incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/SequenceDaoMonitor.java)
==============================================================================
--- incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/SequenceDaoMonitor.java	(original)
+++ incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/sequence/SequenceDaoMonitor.java
Tue Apr 20 10:45:56 2004
@@ -14,7 +14,7 @@
  *   limitations under the License.
  *
  */
-package org.apache.rms.je ;
+package org.apache.rms.je.sequence ;
 
 
 /**

Copied: incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/sequence/SequenceFactory.java
(from rev 10044, incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/SequenceFactory.java)
==============================================================================
--- incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/SequenceFactory.java	(original)
+++ incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/sequence/SequenceFactory.java
Tue Apr 20 10:45:56 2004
@@ -14,9 +14,10 @@
  *   limitations under the License.
  *
  */
-package org.apache.rms.je;
+package org.apache.rms.je.sequence;
 
 import org.apache.rms.RmsException;
+import org.apache.rms.je.sequence.Sequence;
 
 
 /**

Mime
View raw message