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 java.util.Iterator; 028import java.util.Map.Entry; 029import java.util.concurrent.ConcurrentSkipListMap; 030 031import org.opends.server.replication.common.ServerState; 032import org.opends.server.replication.protocol.UpdateMsg; 033import org.opends.server.replication.server.changelog.api.ChangelogException; 034import org.opends.server.replication.server.changelog.api.DBCursor; 035import org.opends.server.replication.server.changelog.api.ReplicationDomainDB; 036import org.opends.server.types.DN; 037 038/** 039 * Cursor iterating over a all the replication domain known to the changelog DB. 040 * 041 * \@NotThreadSafe 042 */ 043public class MultiDomainDBCursor extends CompositeDBCursor<DN> 044{ 045 private final ReplicationDomainDB domainDB; 046 private final ConcurrentSkipListMap<DN, ServerState> newDomains = new ConcurrentSkipListMap<>(); 047 private final CursorOptions options; 048 049 /** 050 * Builds a MultiDomainDBCursor instance. 051 * 052 * @param domainDB 053 * the replication domain management DB 054 * @param options The cursor options 055 */ 056 public MultiDomainDBCursor(final ReplicationDomainDB domainDB, CursorOptions options) 057 { 058 this.domainDB = domainDB; 059 this.options = options; 060 } 061 062 /** 063 * Adds a replication domain for this cursor to iterate over. Added cursors 064 * will be created and iterated over on the next call to {@link #next()}. 065 * 066 * @param baseDN 067 * the replication domain's baseDN 068 * @param startAfterState 069 * the {@link ServerState} after which to start iterating 070 */ 071 public void addDomain(DN baseDN, ServerState startAfterState) 072 { 073 newDomains.put(baseDN, startAfterState != null ? startAfterState : new ServerState()); 074 } 075 076 /** {@inheritDoc} */ 077 @Override 078 protected void incorporateNewCursors() throws ChangelogException 079 { 080 for (Iterator<Entry<DN, ServerState>> iter = newDomains.entrySet().iterator(); 081 iter.hasNext();) 082 { 083 final Entry<DN, ServerState> entry = iter.next(); 084 final DN baseDN = entry.getKey(); 085 final ServerState serverState = entry.getValue(); 086 final DBCursor<UpdateMsg> domainDBCursor = domainDB.getCursorFrom(baseDN, serverState, options); 087 addCursor(domainDBCursor, baseDN); 088 iter.remove(); 089 } 090 } 091 092 /** 093 * Removes a replication domain from this cursor and stops iterating over it. 094 * Removed cursors will be effectively removed on the next call to 095 * {@link #next()}. 096 * 097 * @param baseDN 098 * the replication domain's baseDN 099 */ 100 public void removeDomain(DN baseDN) 101 { 102 removeCursor(baseDN); 103 } 104 105 /** {@inheritDoc} */ 106 @Override 107 public void close() 108 { 109 super.close(); 110 domainDB.unregisterCursor(this); 111 newDomains.clear(); 112 } 113 114}