directory-commits mailing list archives

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

Added:
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/permissions/
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/permissions/BitPermissionDAO.java
  (contents, props changed)
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/permissions/BitPermissionIndexDAO.java
  (contents, props changed)
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/permissions/BitPermissionIndexManager.java
  (contents, props changed)
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/permissions/BitPermissionMapTupleBinding.java
      - copied, changed from rev 10044, incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/PermMapTupleBinding.java
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/permissions/BitPermissionTupleBinding.java
      - copied, changed from rev 10044, incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/PermTupleBinding.java
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/permissions/JeBitPermission.java
      - copied, changed from rev 10044, incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/JeBitPermission.java
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/permissions/JeBitPermissionDAO.java
  (contents, props changed)
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/permissions/JeBitPermissionFactory.java
      - copied unchanged from rev 10127, incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/JeBitPermissionFactory.java
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/permissions/JeBitPermissionIndexDAO.java
  (contents, props changed)
Removed:
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/JeBitPermission.java
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/JeBitPermissionFactory.java
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/PermMapTupleBinding.java
   incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/PermTupleBinding.java
Log:
Commit changes ..

o added new interfaces for the index manager
o added interfaces for the appName:index DAO
o added some implementations for permission classes



Added: incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/permissions/BitPermissionDAO.java
==============================================================================
--- (empty file)
+++ incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/permissions/BitPermissionDAO.java
Tue Apr 20 10:56:48 2004
@@ -0,0 +1,39 @@
+/*
+ *   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.permissions ;
+
+import org.apache.rms.RmsException;
+
+
+/**
+ * Data access operations to be performed.
+ * 
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public interface BitPermissionDAO
+{
+    /**
+     * Gets the smallest available index to assign for new permissions.
+     *
+     * @param appName the name of the application for the new permission
+     * @return the smallest index that can be used for a bit permission
+     */
+    int getSmallestAvailableIndex( String appName ) throws RmsException ;
+
+//    void create( String name, int idx, String appName ) throws RmsException ;
+}

Added: incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/permissions/BitPermissionIndexDAO.java
==============================================================================
--- (empty file)
+++ incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/permissions/BitPermissionIndexDAO.java
Tue Apr 20 10:56:48 2004
@@ -0,0 +1,101 @@
+/*
+ *   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.permissions ;
+
+
+import org.apache.rms.RmsException ;
+import org.apache.commons.collections.primitives.IntIterator;
+
+
+/**
+ * A Data Access Object (DAO) used to manipulate entries within a tuple based
+ * database containing application names mapped to permission index values.
+ * 
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public interface BitPermissionIndexDAO
+{
+    /**
+     * Puts a new record of an index being used into the database.
+     *
+     * @param appName the name of the application
+     * @param index the index of the permission
+     * @throws RmsException if there are problems accessing the tuple database
+     * or the entry is already present.
+     */
+    void put( String appName, int index ) throws RmsException ;
+
+    /**
+     * Deletes the record of an index being used from the database.
+     *
+     * @param appName the name of the application
+     * @param index the index of the permission
+     * @throws RmsException if there are problems accessing the tuple database
+     * or the entry does not exist
+     */
+    void delete( String appName, int index ) throws RmsException ;
+
+    /**
+     * Checks to see if the record of an index being used exists within the
+     * tuple database.
+     *
+     * @param appName the name of the application
+     * @param index the index of the permission
+     * @throws RmsException if there are problems accessing the tuple database
+     * or the entry does not exist
+     */
+    boolean has( String appName, int index ) throws RmsException ;
+
+    /**
+     * Returns an primitive integer iterator over the set of occupied indices
+     * for an application.  If entries for the appName do not exist an empty
+     * iterator is returned.
+     *
+     * @param appName the name of the application to get the listing for
+     * @return a primitive int iterator over the set of used indices
+     * @throws RmsException if there are problems accessing the backing store
+     */
+    IntIterator list( String appName ) throws RmsException ;
+
+    /**
+     * Closes the underlying cursor for an iterator that has not been closed
+     * by consuming the iterator.
+     *
+     * @param list the iterator to close the cursor of
+     * @throws RmsException if the iterator was not created by this DAO
+     */ 
+    void close( IntIterator list ) throws RmsException ;
+
+    /**
+     * Gets the first occupied index for an application.
+     *
+     * @param appName the name of the application
+     * @return the first occupied index for the application
+     * @throws RmsException if there are problems accessing the backing store
+     */
+    int getFirst( String appName ) throws RmsException ;
+
+    /**
+     * Gets the last occupied index for an application.
+     *
+     * @param appName the name of the application
+     * @return the last occupied index for the application
+     * @throws RmsException if there are problems accessing the backing store
+     */
+    int getLast( String appName ) throws RmsException ;
+}

Added: incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/permissions/BitPermissionIndexManager.java
==============================================================================
--- (empty file)
+++ incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/permissions/BitPermissionIndexManager.java
Tue Apr 20 10:56:48 2004
@@ -0,0 +1,66 @@
+/*
+ *   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.permissions ;
+
+
+import org.apache.rms.RmsException ;
+
+
+/**
+ * Interface used to manage the set of bit permission indices.
+ * 
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public interface BitPermissionIndexManager
+{
+    /**
+     * Gets the largest permission index in use for an application.
+     *
+     * @param appName the name of the application to get the index for
+     * @return the maximum occupied index or -1 if permissions don't exist
+     *  for the application
+     * @throws RmsException if there are failures accessing the backing store
+     */
+    int getMaxIndex( String appName ) throws RmsException ;
+
+    /**
+     * Gets the smallest free index.
+     *
+     * @param appName the name of the application to get the index for
+     * @return the minimum free index
+     * @throws RmsException if there are failures accessing the backing store
+     */
+    int getMinFreeIndex( String appName ) throws RmsException ;
+
+    /**
+     * Releases a claimed index.
+     *
+     * @param appName the name of the application to release the index for
+     * @throws RmsException if there are failures accessing the backing store
+     */
+    void releaseIndex( String appName, int index ) throws RmsException ;
+
+    /**
+     * Claims a index for an application.
+     *
+     * @param appName the name of the application to claim the index for
+     * @param index the index to claim
+     * @throws RmsException if there are failures accessing the backing store
+     */
+    void claimIndex( String appName, int index ) throws RmsException ;
+}

Copied: incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/permissions/BitPermissionMapTupleBinding.java
(from rev 10044, incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/PermMapTupleBinding.java)
==============================================================================
--- incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/PermMapTupleBinding.java	(original)
+++ incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/permissions/BitPermissionMapTupleBinding.java
Tue Apr 20 10:56:48 2004
@@ -14,7 +14,7 @@
  *   limitations under the License.
  *
  */
-package org.apache.rms.je ;
+package org.apache.rms.je.permissions ;
 
 
 import com.sleepycat.bind.tuple.TupleInput ;
@@ -40,7 +40,7 @@
  * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
  * @version $Rev$
  */
-public class PermMapTupleBinding extends TupleBinding
+public class BitPermissionMapTupleBinding extends TupleBinding
 {
     /** A factory that always returns null */
     private final static Factory NULL_FACTORY = new Factory()

Copied: incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/permissions/BitPermissionTupleBinding.java
(from rev 10044, incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/PermTupleBinding.java)
==============================================================================
--- incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/PermTupleBinding.java	(original)
+++ incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/permissions/BitPermissionTupleBinding.java
Tue Apr 20 10:56:48 2004
@@ -14,7 +14,7 @@
  *   limitations under the License.
  *
  */
-package org.apache.rms.je ;
+package org.apache.rms.je.permissions ;
 
 
 import com.sleepycat.bind.tuple.TupleInput ;
@@ -24,6 +24,7 @@
 import java.io.IOException ;
 
 import org.apache.rms.BitPermission ;
+import org.apache.rms.je.permissions.JeBitPermission;
 
 
 /**
@@ -32,7 +33,7 @@
  * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
  * @version $Rev$
  */
-public class PermTupleBinding extends TupleBinding
+public class BitPermissionTupleBinding extends TupleBinding
 {
     /**
      * Reconstructs a bit permission using a serialized entry.

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

Added: incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/permissions/JeBitPermissionDAO.java
==============================================================================
--- (empty file)
+++ incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/permissions/JeBitPermissionDAO.java
Tue Apr 20 10:56:48 2004
@@ -0,0 +1,134 @@
+/*
+ *   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.permissions ;
+
+
+import com.sleepycat.je.*;
+
+import org.apache.commons.lang.Validate ;
+import org.apache.commons.lang.NotImplementedException;
+
+import org.apache.rms.RmsException ;
+import org.apache.rms.je.permissions.BitPermissionDAO;
+import org.apache.rms.je.sequence.Sequence;
+import org.apache.rms.je.sequence.Sequence;
+
+import java.io.UnsupportedEncodingException;
+
+
+/**
+ * A JE Database BitPermission data access object.
+ * 
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class JeBitPermissionDAO implements BitPermissionDAO
+{
+    private final BitPermissionTupleBinding permBinding =
+            new BitPermissionTupleBinding() ;
+    /** the JE database to use for this DAP */
+    private final Database db ;
+    /** the sequence we use to create new permission row ids with */
+    private final Sequence seq ;
+    /** the secondary database used to index permissions by vector indices */
+    private final SecondaryDatabase byIdx ;
+    /** the secondary database used to index permission by name */
+    private final SecondaryDatabase byName ;
+    /** the secondary database used to index permission by app name */
+    private final SecondaryDatabase byApp ;
+
+
+    /**
+     * Creates a JE based permission DAO using a JE database.
+     *
+     * @param db the JE Database for the permission DAO
+     */
+    JeBitPermissionDAO( Database db, Sequence seq, SecondaryDatabase idx,
+                   SecondaryDatabase name, SecondaryDatabase app )
+    {
+        Validate.notNull( db ) ;
+        Validate.notNull( seq ) ;
+        Validate.notNull( app ) ;
+        Validate.notNull( idx ) ;
+        Validate.notNull( name ) ;
+
+        this.db = db ;
+        this.seq = seq ;
+        this.byApp = app ;
+        this.byIdx = idx ;
+        this.byName = name ;
+    }
+
+
+    /**
+     * Gets the smallest available index to assign for new permissions.
+     *
+     * @return the smallest index that can be used for a bit permission
+     */
+    public int getSmallestAvailableIndex( String appName ) throws RmsException
+    {
+        /*
+         * Need to create a cursor over the byName secondary index and
+         * perform lookups on the byIdx secondary to find holes.  The
+         * first lookup to fail on the byIdx secondary is the hole.
+         */
+        int idx = 0 ;
+        SecondaryCursor cursor = null ;
+        DatabaseEntry appKey = new DatabaseEntry() ;
+        DatabaseEntry rowId = new DatabaseEntry() ;
+        OperationStatus status = null ;
+
+        try
+        {
+            cursor = byApp.openSecondaryCursor( null, null ) ;
+            appKey.setData( appName.getBytes( "UTF-8" ) ) ;
+            status = cursor.getSearchKey( appKey, rowId, LockMode.DEFAULT ) ;
+
+            if ( status == OperationStatus.NOTFOUND )
+            {
+                return 0 ;
+            }
+            else if ( status != OperationStatus.SUCCESS )
+            {
+                throw new RmsException( "Failed to place cursor on " + appName ) ;
+            }
+
+            while ( cursor.getNextDup( appKey, rowId, LockMode.DEFAULT )
+                    == OperationStatus.SUCCESS )
+            {
+                // keep going until getNextDup is not successful
+            }
+        }
+        catch ( UnsupportedEncodingException e )
+        {
+            throw new RmsException( e ) ;
+        }
+        catch ( DatabaseException e )
+        {
+            throw new RmsException( e ) ;
+        }
+        finally
+        {
+            if ( cursor != null )
+            {
+                try { cursor.close() ; } catch ( DatabaseException e )  {}
+            }
+        }
+
+        throw new NotImplementedException( "STUB" ) ;
+    }
+}

Added: incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/permissions/JeBitPermissionIndexDAO.java
==============================================================================
--- (empty file)
+++ incubator/directory/rms/trunk/je/src/java/org/apache/rms/je/permissions/JeBitPermissionIndexDAO.java
Tue Apr 20 10:56:48 2004
@@ -0,0 +1,472 @@
+/*
+ *   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.permissions ;
+
+
+import org.apache.rms.je.JeUtils ;
+import org.apache.rms.RmsException ;
+
+import org.apache.commons.collections.primitives.IntIterator ;
+
+import com.sleepycat.je.* ;
+
+import java.util.NoSuchElementException ;
+import java.io.UnsupportedEncodingException ;
+
+
+/**
+ * A JE based BitPermissionIndex data access object.
+ * 
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class JeBitPermissionIndexDAO implements BitPermissionIndexDAO
+{
+    /** the JE database this DAO is designed for */
+    private final Database db ;
+
+
+    /**
+     * Creates a DAO wrapper around the permission index database.
+     *
+     * @param db the permission index JE database
+     * @throws RmsException if the db does not allow duplicates
+     */
+    public JeBitPermissionIndexDAO( Database db ) throws RmsException
+    {
+        try
+        {
+            if ( ! db.getConfig().getAllowDuplicates() )
+            {
+                throw new RmsException( "Duplicates must be enable for db" ) ;
+            }
+        }
+        catch ( DatabaseException e )
+        {
+            throw new RmsException( e ) ;
+        }
+
+        this.db = db ;
+    }
+
+
+    /**
+     * Puts a new record of an index being used into the database.
+     *
+     * @param appName the name of the application
+     * @param index   the index of the permission
+     * @throws org.apache.rms.RmsException if there are problems accessing
+     * the tuple database or the entry is already present.
+     */
+    public void put( String appName, int index ) throws RmsException
+    {
+        OperationStatus status = null ;
+        final DatabaseEntry key = new DatabaseEntry() ;
+        final DatabaseEntry value = new DatabaseEntry() ;
+
+        value.setData( JeUtils.encodeInt( index ) ) ;
+
+        try
+        {
+            key.setData( appName.getBytes( "UTF-8" ) ) ;
+            status = db.put( null, key, value ) ;
+
+            if ( status != OperationStatus.SUCCESS )
+            {
+                throw new RmsException( "Put of index entry " +
+                        index + " for application named " + appName
+                        + " failed with a status of " + status.toString() ) ;
+            }
+        }
+        catch ( UnsupportedEncodingException e )
+        {
+            throw new RmsException( e ) ;
+        }
+        catch ( DatabaseException e )
+        {
+            throw new RmsException( e ) ;
+        }
+    }
+
+
+    /**
+     * Deletes the record of an index being used from the database.
+     *
+     * @param appName the name of the application
+     * @param index   the index of the permission
+     * @throws org.apache.rms.RmsException if there are problems accessing
+     * the tuple database or the entry does not exist
+     */
+    public void delete( String appName, int index ) throws RmsException
+    {
+        Cursor cursor = null ;
+        OperationStatus status = null ;
+        final DatabaseEntry key = new DatabaseEntry() ;
+        final DatabaseEntry value = new DatabaseEntry() ;
+
+        value.setData( JeUtils.encodeInt( index ) ) ;
+        try
+        {
+            key.setData( appName.getBytes( "UTF-8" ) ) ;
+            cursor = db.openCursor( null, null ) ;
+            status = cursor.getSearchBoth( key, value, LockMode.DEFAULT ) ;
+
+            if ( status == OperationStatus.SUCCESS )
+            {
+                status = cursor.delete() ;
+
+                if ( status != OperationStatus.SUCCESS )
+                {
+                    throw new RmsException(
+                            "Delete operation failed with status "
+                            + status.toString() ) ;
+                }
+            }
+        }
+        catch ( UnsupportedEncodingException e )
+        {
+            throw new RmsException( e ) ;
+        }
+        catch ( DatabaseException e )
+        {
+            throw new RmsException( e ) ;
+        }
+        finally
+        {
+            if ( cursor != null )
+            {
+                try
+                {
+                    cursor.close() ;
+                }
+                catch ( DatabaseException e )
+                {
+                    throw new RmsException( e ) ;
+                }
+            }
+        }
+    }
+
+
+    /**
+     * Checks to see if the record of an index being used exists within the
+     * tuple database.
+     *
+     * @param appName the name of the application
+     * @param index   the index of the permission
+     * @throws org.apache.rms.RmsException if there are problems accessing
+     * the tuple database or the entry does not exist
+     */
+    public boolean has( String appName, int index ) throws RmsException
+    {
+        Cursor cursor = null ;
+        OperationStatus status = null ;
+        final DatabaseEntry key = new DatabaseEntry() ;
+        final DatabaseEntry value = new DatabaseEntry() ;
+
+        value.setData( JeUtils.encodeInt( index ) ) ;
+        try
+        {
+            key.setData( appName.getBytes( "UTF-8" ) ) ;
+            cursor = db.openCursor( null, null ) ;
+            status = cursor.getSearchBoth( key, value, LockMode.DEFAULT ) ;
+            cursor.close() ;
+        }
+        catch ( UnsupportedEncodingException e )
+        {
+            throw new RmsException( e ) ;
+        }
+        catch ( DatabaseException e )
+        {
+            throw new RmsException( e ) ;
+        }
+        finally
+        {
+            if ( cursor != null )
+            {
+                try
+                {
+                    cursor.close() ;
+                }
+                catch ( DatabaseException e )
+                {
+                    throw new RmsException( e ) ;
+                }
+            }
+        }
+
+        if ( status == OperationStatus.SUCCESS )
+        {
+            return true ;
+        }
+        else
+        {
+            return false ;
+        }
+    }
+
+
+    /**
+     * Closes the underlying cursor for an iterator that has not been closed
+     * by consuming the iterator.
+     *
+     * @param list the iterator to close the cursor of
+     * @throws org.apache.rms.RmsException if the iterator was not created by
+     * this DAO
+     */
+    public void close( IntIterator list ) throws RmsException
+    {
+        if ( list instanceof IndexIterator )
+        {
+            ( ( IndexIterator ) list ).close() ;
+            return ;
+        }
+
+        throw new RmsException( "Cannot close: Iterator != IndexIterator" ) ;
+    }
+
+
+    /**
+     * Returns an primitive integer iterator over the set of occupied indices
+     * for an application.  If entries for the appName do not exist an empty
+     * iterator is returned.
+     *
+     * @param appName the name of the application to get the listing for
+     * @return a primitive int iterator over the set of used indices
+     * @throws org.apache.rms.RmsException if there are problems accessing
+     * the backing store
+     */
+    public IntIterator list( String appName ) throws RmsException
+    {
+        try
+        {
+            return new IndexIterator( appName ) ;
+        }
+        catch ( DatabaseException e )
+        {
+            throw new RmsException( e ) ;
+        }
+        catch ( UnsupportedEncodingException e )
+        {
+            throw new RmsException( e ) ;
+        }
+    }
+
+
+    /**
+     * Gets the first occupied index for an application or -1 if there are no
+     * permissions for the application.
+     *
+     * @param appName the name of the application
+     * @return the first occupied index for the application
+     * @throws org.apache.rms.RmsException if there are problems accessing
+     * the backing store
+     */
+    public int getFirst( String appName ) throws RmsException
+    {
+        byte[] keyBytes = null ;
+        DatabaseEntry key = null ;
+        DatabaseEntry value = null ;
+        OperationStatus status = null ;
+
+        try
+        {
+            appName.getBytes( "UTF-8" ) ;
+        }
+        catch ( UnsupportedEncodingException e )
+        {
+            throw new RmsException( e ) ;
+        }
+
+        key = new DatabaseEntry( keyBytes ) ;
+        value = new DatabaseEntry() ;
+
+        try
+        {
+            status = db.get( null, key, value, LockMode.DEFAULT ) ;
+        }
+        catch ( DatabaseException e )
+        {
+            throw new RmsException( e ) ;
+        }
+
+        if ( status == OperationStatus.NOTFOUND )
+        {
+            return -1 ;
+        }
+
+        return JeUtils.decodeInt( value.getData() ) ;
+    }
+
+
+    /**
+     * Gets the last occupied index for an application or -1 if there
+     * are no permissions for the application.
+     *
+     * @param appName the name of the application
+     * @return the last occupied index for the application
+     * @throws org.apache.rms.RmsException if there are problems accessing the backing store
+     */
+    public int getLast( String appName ) throws RmsException
+    {
+        Cursor cursor = null ;
+        byte[] keyBytes = null ;
+        DatabaseEntry key = null ;
+        DatabaseEntry value = null ;
+        OperationStatus status = null ;
+
+        try
+        {
+            appName.getBytes( "UTF-8" ) ;
+        }
+        catch ( UnsupportedEncodingException e )
+        {
+            throw new RmsException( e ) ;
+        }
+
+        key = new DatabaseEntry( keyBytes ) ;
+        value = new DatabaseEntry() ;
+
+        try
+        {
+            cursor = db.openCursor( null, null ) ;
+            status = JeUtils.getSearchLastDuplicate(
+                    cursor, key, value, LockMode.DEFAULT ) ;
+        }
+        catch ( DatabaseException e )
+        {
+            throw new RmsException( e ) ;
+        }
+        finally
+        {
+            if ( cursor != null )
+            {
+                try
+                {
+                    cursor.close() ;
+                }
+                catch ( DatabaseException e )
+                {
+                    throw new RmsException( e ) ;
+                }
+            }
+        }
+
+        if ( status == OperationStatus.NOTFOUND )
+        {
+            return -1 ;
+        }
+
+        return JeUtils.decodeInt( value.getData() ) ;
+    }
+
+
+    class IndexIterator implements IntIterator
+    {
+        final Cursor cursor ;
+        final DatabaseEntry key ;
+        final DatabaseEntry value ;
+        boolean hasNext = true ;
+        int prefetched = -1 ;
+
+
+        IndexIterator( String appName )
+                throws DatabaseException, UnsupportedEncodingException
+        {
+            OperationStatus status = null ;
+            value = new DatabaseEntry() ;
+            key = new DatabaseEntry( appName.getBytes( "UTF-8" ) ) ;
+            cursor = db.openCursor( null, null ) ;
+
+            try
+            {
+                status = cursor.getSearchKey( key, value, LockMode.DEFAULT ) ;
+            }
+            finally
+            {
+                cursor.close() ;
+            }
+
+            if ( status == OperationStatus.NOTFOUND )
+            {
+                hasNext = false ;
+                cursor.close() ;
+                return ;
+            }
+        }
+
+
+        public boolean hasNext()
+        {
+            return hasNext ;
+        }
+
+
+        public int next()
+        {
+            int retVal = prefetched ;
+            OperationStatus status = null ;
+
+            try
+            {
+                status = cursor.getNextDup( key, value, LockMode.DEFAULT ) ;
+            }
+            catch ( DatabaseException e )
+            {
+                throw new NoSuchElementException( "database access failure" ) ;
+            }
+            finally
+            {
+                try { close() ;} catch( RmsException e ) {}
+            }
+
+            if ( status == OperationStatus.SUCCESS )
+            {
+                prefetched = JeUtils.decodeInt( value ) ;
+            }
+            else
+            {
+                prefetched = -1 ;
+                hasNext = false ;
+                try { cursor.close() ; } catch( DatabaseException e ) {}
+            }
+
+            return retVal ;
+        }
+
+
+        public void remove()
+        {
+            throw new UnsupportedOperationException() ;
+        }
+
+
+        void close() throws RmsException
+        {
+            try
+            {
+                prefetched = -1 ;
+                hasNext = false ;
+                cursor.close() ;
+            }
+            catch ( DatabaseException e )
+            {
+                throw new RmsException( e ) ;
+            }
+        }
+    }
+}

Mime
View raw message