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 static org.opends.server.loggers.debug.DebugLogger.*;
030    import org.opends.server.loggers.debug.DebugTracer;
031    
032    import java.util.Comparator;
033    
034    /**
035     * This comparator is used to sort databases in order of priority
036     * for preloading into the cache.
037     */
038    public class DbPreloadComparator
039        implements Comparator<DatabaseContainer>
040    {
041      /**
042       * The tracer object for the debug logger.
043       */
044      private static final DebugTracer TRACER = getTracer();
045    
046    
047      /**
048       * Calculate the relative priority of a database for preloading.
049       *
050       * @param database A handle to the database.
051       * @return 1 for id2entry database, 2 for dn2id database, 3 for all others.
052       */
053      static private int priority(DatabaseContainer database)
054      {
055        String name = database.getName();
056        if (name.endsWith(EntryContainer.ID2ENTRY_DATABASE_NAME))
057        {
058          return 1;
059        }
060        else if (name.endsWith(EntryContainer.DN2ID_DATABASE_NAME))
061        {
062          return 2;
063        }
064        else
065        {
066          return 3;
067        }
068      }
069    
070      /**
071       * Compares its two arguments for order.  Returns a negative integer,
072       * zero, or a positive integer as the first argument is less than, equal
073       * to, or greater than the second.
074       *
075       * @param database1 the first object to be compared.
076       * @param database2 the second object to be compared.
077       * @return a negative integer, zero, or a positive integer as the
078       *         first argument is less than, equal to, or greater than the
079       *         second.
080       **/
081      public int compare(DatabaseContainer database1, DatabaseContainer database2)
082      {
083        return priority(database1) - priority(database2);
084      }
085    }