001    /*
002     * CDDL HEADER START
003     *
004     * The contents of this file are subject to the terms of the
005     * Common Development and Distribution License, Version 1.0 only
006     * (the "License").  You may not use this file except in compliance
007     * with the License.
008     *
009     * You can obtain a copy of the license at
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2006-2008 Sun Microsystems, Inc.
026     */
027    package org.opends.server.backends.jeb;
028    
029    import org.opends.server.types.DN;
030    
031    import java.util.ArrayList;
032    
033    /**
034     * Configuration for the indexType rebuild process.
035     */
036    public class RebuildConfig
037    {
038      /**
039       * The base DN to rebuild.
040       */
041      private DN baseDN;
042    
043      /**
044       * The names of indexes to rebuild.
045       */
046      private ArrayList<String> rebuildList;
047    
048      /**
049       * The maximum number of rebuild threads to use at one time. An negative
050       * number indicates unlimited max number of threads.
051       */
052      private int maxRebuildThreads = -1;
053    
054      /**
055       * Create a new rebuild configuraiton.
056       */
057      public RebuildConfig()
058      {
059        rebuildList = new ArrayList<String>();
060      }
061    
062      /**
063       * Get the base DN to rebuild.
064       * @return The base DN to rebuild.
065       */
066      public DN getBaseDN()
067      {
068        return baseDN;
069      }
070    
071      /**
072       * Set the base DN to rebuild.
073       * @param baseDN The base DN to rebuild.
074       */
075      public void setBaseDN(DN baseDN)
076      {
077        this.baseDN = baseDN;
078      }
079    
080      /**
081       * Get the list of indexes to rebuild in this configuration.
082       *
083       * @return The list of indexes to rebuild.
084       */
085      public ArrayList<String> getRebuildList()
086      {
087        return rebuildList;
088      }
089    
090      /**
091       * Add an index to be rebuilt into the configuration. Duplicate index names
092       * will be ignored. Adding an index that causes a mix of complete and partial
093       * rebuild for the same attribute index in the configuration will remove
094       * the partial and just keep the complete attribute index name.
095       * (ie. uid and uid.presence).
096       *
097       * @param index The index to add.
098       */
099      public void addRebuildIndex(String index)
100      {
101        String[] newIndexParts = index.split("\\.");
102    
103        for(String s : new ArrayList<String>(rebuildList))
104        {
105          String[] existingIndexParts = s.split("\\.");
106          if(existingIndexParts[0].equalsIgnoreCase(newIndexParts[0]))
107          {
108            if(newIndexParts.length == 1 && existingIndexParts.length == 1)
109            {
110              return;
111            }
112            else if(newIndexParts.length > 1 && existingIndexParts.length == 1)
113            {
114              return;
115            }
116            else if(newIndexParts.length == 1 && existingIndexParts.length > 1)
117            {
118              rebuildList.remove(s);
119            }
120            else if(newIndexParts[1].equalsIgnoreCase(existingIndexParts[1]))
121            {
122              return;
123            }
124          }
125        }
126    
127        this.rebuildList.add(index);
128      }
129    
130      /**
131       * Check the given config for conflicts with this config. A conflict is
132       * detected if both configs specify the same indexType/database to be rebuilt.
133       *
134       * @param config The rebuild config to check against.
135       * @return the name of the indexType causing the conflict or null if no
136       *         conflict is detected.
137       */
138      public String checkConflicts(RebuildConfig config)
139      {
140        //If they specify different base DNs, no conflicts can occur.
141        if(this.baseDN.equals(config.baseDN))
142        {
143          for(String thisIndex : this.rebuildList)
144          {
145            for(String thatIndex : config.rebuildList)
146            {
147              String[] existingIndexParts = thisIndex.split("\\.");
148              String[] newIndexParts = thatIndex.split("\\.");
149              if(existingIndexParts[0].equalsIgnoreCase(newIndexParts[0]))
150              {
151                if(newIndexParts.length == 1 && existingIndexParts.length == 1)
152                {
153                  return thatIndex;
154                }
155                else if(newIndexParts.length > 1 && existingIndexParts.length == 1)
156                {
157                  return thatIndex;
158                }
159                else if(newIndexParts.length == 1 && existingIndexParts.length > 1)
160                {
161                  return thatIndex;
162                }
163                else if(newIndexParts[1].equalsIgnoreCase(existingIndexParts[1]))
164                {
165                  return thatIndex;
166                }
167              }
168            }
169          }
170        }
171    
172        return null;
173      }
174    
175      /**
176       * Get the maximum number of rebuild threads to use for the rebuild job
177       * at one time.
178       *
179       * @return The maximum number of rebuild threads.
180       */
181      public int getMaxRebuildThreads()
182      {
183        return maxRebuildThreads;
184      }
185    
186      /**
187       * Set the maximum number of rebuild threads to use for the rebuild
188       * job at one time.
189       *
190       * @param maxRebuildThreads The maximum number of rebuild threads.
191       */
192      public void setMaxRebuildThreads(int maxRebuildThreads)
193      {
194        this.maxRebuildThreads = maxRebuildThreads;
195      }
196    
197      /**
198       * Test if this rebuild config includes any system indexes to rebuild.
199       *
200       * @return True if rebuilding of system indexes are included. False otherwise.
201       */
202      public boolean includesSystemIndex()
203      {
204        for(String index : rebuildList)
205        {
206          if(index.equalsIgnoreCase("id2entry"))
207          {
208            return true;
209          }
210          if(index.equalsIgnoreCase("dn2id"))
211          {
212            return true;
213          }
214          if(index.equalsIgnoreCase("dn2uri"))
215          {
216            return true;
217          }
218          if(index.equalsIgnoreCase("id2children"))
219          {
220            return true;
221          }
222          if(index.equalsIgnoreCase("id2subtree"))
223          {
224            return true;
225          }
226        }
227    
228        return false;
229      }
230    
231    }