directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kayyag...@apache.org
Subject svn commit: r950662 - /directory/apacheds/trunk/jdbm-store/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmStore.java
Date Wed, 02 Jun 2010 17:11:01 GMT
Author: kayyagari
Date: Wed Jun  2 17:11:01 2010
New Revision: 950662

URL: http://svn.apache.org/viewvc?rev=950662&view=rev
Log:
o added a method to build any newly added userindex after the server was already loaded with
some data (look at DIRSERVER-1327 for the complete details)

Modified:
    directory/apacheds/trunk/jdbm-store/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmStore.java

Modified: directory/apacheds/trunk/jdbm-store/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmStore.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/jdbm-store/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmStore.java?rev=950662&r1=950661&r2=950662&view=diff
==============================================================================
--- directory/apacheds/trunk/jdbm-store/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmStore.java
(original)
+++ directory/apacheds/trunk/jdbm-store/src/main/java/org/apache/directory/server/core/partition/impl/btree/jdbm/JdbmStore.java
Wed Jun  2 17:11:01 2010
@@ -23,6 +23,7 @@ package org.apache.directory.server.core
 import java.io.File;
 import java.io.FilenameFilter;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 
 import jdbm.RecordManager;
@@ -36,7 +37,12 @@ import org.apache.directory.server.xdbm.
 import org.apache.directory.server.xdbm.Index;
 import org.apache.directory.shared.ldap.MultiException;
 import org.apache.directory.shared.ldap.constants.SchemaConstants;
+import org.apache.directory.shared.ldap.cursor.Cursor;
+import org.apache.directory.shared.ldap.cursor.Tuple;
 import org.apache.directory.shared.ldap.entry.Entry;
+import org.apache.directory.shared.ldap.entry.EntryAttribute;
+import org.apache.directory.shared.ldap.entry.Value;
+import org.apache.directory.shared.ldap.schema.AttributeType;
 import org.apache.directory.shared.ldap.schema.SchemaManager;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -56,6 +62,17 @@ public class JdbmStore<E> extends Abstra
     /** the JDBM record manager used by this database */
     private RecordManager recMan;
 
+    private static final String JDBM_DB_FILE_EXTN = ".db";
+    
+    private static final FilenameFilter DB_FILTER = new FilenameFilter()
+    {
+        
+        public boolean accept( File dir, String name )
+        {
+            // really important to filter master.db and master.lg files
+            return ( name.endsWith( JDBM_DB_FILE_EXTN ) && !name.startsWith( "master."
) );
+        }
+    };
 
     // ------------------------------------------------------------------------
     // C O N S T R U C T O R S
@@ -126,10 +143,43 @@ public class JdbmStore<E> extends Abstra
         // -------------------------------------------------------------------
         // Initializes the user and system indices
         // -------------------------------------------------------------------
+
+        // get all index db files first
+        File[] allIndexDbFiles = getPartitionDir().listFiles( DB_FILTER );
+        
+        // get the names of the db files also
+        List<String> indexDbFileNameList = Arrays.asList( getPartitionDir().list( DB_FILTER
) );
+        
         setupSystemIndices();
         setupUserIndices();
 
-        deleteUnusedIndexFiles();
+        // then add all index objects to a list
+        List<String> allIndices = new ArrayList<String>();
+        for( Index i : systemIndices.values() )
+        {
+            allIndices.addAll( i.getAttribute().getNames() );
+        }
+
+        // this loop is used for two purposes
+        // one for collecting all user indices
+        // two for finding a new index to be built
+        // just to avoid another iteration for determining which is the new index
+        for( Index i : userIndices.values() )
+        {
+            allIndices.addAll( i.getAttribute().getNames() );
+
+            // take the part after removing .db from the  
+            String name = i.getAttribute().getName() + JDBM_DB_FILE_EXTN;
+
+            // if the name doesn't exist in the list of index DB files
+            // this is a new index and we need to build it
+            if( !indexDbFileNameList.contains( name ) )
+            {
+                buildUserIndex( i );
+            }
+        }
+
+        deleteUnusedIndexFiles( allIndices, allIndexDbFiles );
         
         // We are done !
         initialized = true;
@@ -282,38 +332,13 @@ public class JdbmStore<E> extends Abstra
      * removes any unused/removed attribute index files present under the partition's
      * working directory
      */
-    private void deleteUnusedIndexFiles()
+    private void deleteUnusedIndexFiles( List<String> allIndices, File[] dbFiles )
     {
-        final String jdbmDbFileExtn = ".db";
-        
-        FilenameFilter filter = new FilenameFilter()
-        {
-            
-            public boolean accept( File dir, String name )
-            {
-                // really important to filter master.db and master.lg files
-                return ( name.endsWith( jdbmDbFileExtn ) && !name.startsWith( "master."
) );
-            }
-        };
-        
-        File[] dbFiles = getPartitionDir().listFiles( filter );
-        
-        List<String> allIndices = new ArrayList<String>();
-        for( Index i : systemIndices.values() )
-        {
-            allIndices.addAll( i.getAttribute().getNames() );
-        }
-        
-        for( Index i : userIndices.values() )
-        {
-            allIndices.addAll( i.getAttribute().getNames() );
-        }
-        
         for( File file : dbFiles )
         {
             String name = file.getName();
             // take the part after removing .db from the  
-            name = name.substring( 0, name.lastIndexOf( jdbmDbFileExtn ) );
+            name = name.substring( 0, name.lastIndexOf( JDBM_DB_FILE_EXTN ) );
             
             // remove the file if not found in the list of names of indices
             if( !allIndices.contains( name ) )
@@ -330,4 +355,45 @@ public class JdbmStore<E> extends Abstra
             }
         }
     }
+    
+    
+    /**
+     * builds a user defined index on a attribute by browsing all the entries present in
master db
+     * 
+     * @param userIdx then user defined index
+     * @throws Exception in case of any problems while building the index
+     */
+    public void buildUserIndex( Index userIdx ) throws Exception
+    {
+        AttributeType atType = userIdx.getAttribute();
+
+        LOG.info( "building the index for attribute type {}", atType );
+        
+        Cursor<Tuple<Long,Entry>> cursor = master.cursor();
+        cursor.beforeFirst();
+        
+        String attributeOid = userIdx.getAttribute().getOid();
+        
+        while( cursor.next() )
+        {
+            Tuple<Long,Entry> tuple = cursor.get();
+            
+            Long id = tuple.getKey();
+            Entry entry = tuple.getValue();
+            
+            EntryAttribute entryAttr = entry.get( atType );
+            if( entryAttr != null )
+            {
+                for ( Value<?> value : entryAttr )
+                {
+                    userIdx.add( value.get(), id );
+                }
+                
+                // Adds only those attributes that are indexed
+                presenceIdx.add( attributeOid, id );
+            }
+        }
+        
+        cursor.close();
+    }
 }



Mime
View raw message