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 legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2006-2008 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027package org.opends.server.backends.jeb;
028
029import org.forgerock.i18n.slf4j.LocalizedLogger;
030
031import java.util.Comparator;
032
033/**
034 * This comparator is used to sort databases in order of priority
035 * for preloading into the cache.
036 */
037public class DbPreloadComparator
038    implements Comparator<DatabaseContainer>
039{
040  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
041
042
043  /**
044   * Calculate the relative priority of a database for preloading.
045   *
046   * @param database A handle to the database.
047   * @return 1 for id2entry database, 2 for dn2id database, 3 for all others.
048   */
049  private static int priority(DatabaseContainer database)
050  {
051    String name = database.getName();
052    if (name.endsWith(EntryContainer.ID2ENTRY_DATABASE_NAME))
053    {
054      return 1;
055    }
056    else if (name.endsWith(EntryContainer.DN2ID_DATABASE_NAME))
057    {
058      return 2;
059    }
060    else
061    {
062      return 3;
063    }
064  }
065
066  /**
067   * Compares its two arguments for order.  Returns a negative integer,
068   * zero, or a positive integer as the first argument is less than, equal
069   * to, or greater than the second.
070   *
071   * @param database1 the first object to be compared.
072   * @param database2 the second object to be compared.
073   * @return a negative integer, zero, or a positive integer as the
074   *         first argument is less than, equal to, or greater than the
075   *         second.
076   **/
077  public int compare(DatabaseContainer database1, DatabaseContainer database2)
078  {
079    return priority(database1) - priority(database2);
080  }
081}