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 *      Copyright 2014-2015 ForgeRock AS
024 */
025package org.opends.server.replication.server.changelog.file;
026
027import org.opends.server.replication.protocol.UpdateMsg;
028import org.opends.server.replication.server.changelog.api.ChangelogException;
029import org.opends.server.replication.server.changelog.api.DBCursor;
030import org.opends.server.types.DN;
031
032/**
033 * Multi domain DB cursor that only returns updates for the domains which have
034 * been enabled for the external changelog.
035 */
036public final class ECLMultiDomainDBCursor implements DBCursor<UpdateMsg>
037{
038  private final ECLEnabledDomainPredicate predicate;
039  private final MultiDomainDBCursor cursor;
040
041  /**
042   * Builds an instance of this class filtering updates from the provided cursor.
043   *
044   * @param predicate
045   *          tells whether a domain is enabled for the external changelog
046   * @param cursor
047   *          the cursor whose updates will be filtered
048   */
049  public ECLMultiDomainDBCursor(ECLEnabledDomainPredicate predicate, MultiDomainDBCursor cursor)
050  {
051    this.predicate = predicate;
052    this.cursor = cursor;
053  }
054
055  /** {@inheritDoc} */
056  @Override
057  public UpdateMsg getRecord()
058  {
059    return cursor.getRecord();
060  }
061
062  /**
063   * Returns the data associated to the cursor that returned the current record.
064   *
065   * @return the data associated to the cursor that returned the current record.
066   */
067  public DN getData()
068  {
069    return cursor.getData();
070  }
071
072  /**
073   * Removes a replication domain from this cursor and stops iterating over it.
074   * Removed cursors will be effectively removed on the next call to
075   * {@link #next()}.
076   *
077   * @param baseDN
078   *          the replication domain's baseDN
079   */
080  public void removeDomain(DN baseDN)
081  {
082    cursor.removeDomain(baseDN);
083  }
084
085  @Override
086  public boolean next() throws ChangelogException
087  {
088    if (!cursor.next())
089    {
090      return false;
091    }
092    // discard updates from non ECL enabled domains by removing the disabled domains from the cursor
093    DN domain = cursor.getData();
094    while (domain != null && !predicate.isECLEnabledDomain(domain))
095    {
096      cursor.removeDomain(domain);
097      domain = cursor.getData();
098    }
099    return domain != null;
100  }
101
102  @Override
103  public void close()
104  {
105    cursor.close();
106  }
107
108  @Override
109  public String toString()
110  {
111    return getClass().getSimpleName() + " cursor=[" + cursor + ']';
112  }
113}