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 2009 Sun Microsystems, Inc. 025 * Portions Copyright 2011-2015 ForgeRock AS 026 */ 027package org.opends.server.replication.server.changelog.je; 028 029import org.forgerock.i18n.slf4j.LocalizedLogger; 030import org.opends.server.replication.server.changelog.api.*; 031import org.opends.server.replication.server.changelog.je.DraftCNDB.*; 032 033/** 034 * This class allows to iterate through the changes comming from the change number index DB. 035 * 036 * \@NotThreadSafe 037 */ 038public class JEChangeNumberIndexDBCursor implements 039 DBCursor<ChangeNumberIndexRecord> 040{ 041 private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass(); 042 private DraftCNDBCursor draftCNDbCursor; 043 044 /** 045 * As underlying cursor is already pointing to a record at start, this 046 * indicator allow to shift the pointed record at initialization time. 047 */ 048 private boolean isInitialized; 049 050 /** 051 * Creates a new DB Cursor. All created iterator must be released by 052 * the caller using the {@link #close()} method. 053 * 054 * @param db 055 * The db where the iterator must be created. 056 * @param startChangeNumber 057 * The change number after which the iterator must start. 058 * @throws ChangelogException 059 * If a database problem happened. 060 */ 061 public JEChangeNumberIndexDBCursor(DraftCNDB db, long startChangeNumber) 062 throws ChangelogException 063 { 064 draftCNDbCursor = db.openReadCursor(startChangeNumber); 065 } 066 067 /** {@inheritDoc} */ 068 @Override 069 public ChangeNumberIndexRecord getRecord() 070 { 071 try 072 { 073 return isInitialized ? draftCNDbCursor.currentRecord() : null; 074 } 075 catch (Exception e) 076 { 077 logger.traceException(e); 078 return null; 079 } 080 } 081 082 /** {@inheritDoc} */ 083 @Override 084 public boolean next() throws ChangelogException 085 { 086 if (draftCNDbCursor != null) 087 { 088 if (!isInitialized) 089 { 090 isInitialized = true; 091 return draftCNDbCursor.currentRecord() != null; 092 } 093 else 094 { 095 return draftCNDbCursor.next(); 096 } 097 } 098 return false; 099 } 100 101 /** {@inheritDoc} */ 102 @Override 103 public void close() 104 { 105 synchronized (this) 106 { 107 if (draftCNDbCursor != null) 108 { 109 draftCNDbCursor.close(); 110 draftCNDbCursor = null; 111 } 112 } 113 } 114 115 /** 116 * Called by the Gc when the object is garbage collected 117 * Release the cursor in case the iterator was badly used and releaseCursor 118 * was never called. 119 */ 120 @Override 121 protected void finalize() 122 { 123 close(); 124 } 125 126}