From 700dffb800fea545d109919f59d168f09b37c05e Mon Sep 17 00:00:00 2001
From: Jean-Noel Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Thu, 10 Oct 2013 14:31:02 +0000
Subject: [PATCH] OPENDJ-1116 Introduce abstraction for the changelog DB

---
 opends/src/server/org/opends/server/replication/server/ECLServerHandler.java |   43 +++++++++++++++++++++++++++++--------------
 1 files changed, 29 insertions(+), 14 deletions(-)

diff --git a/opends/src/server/org/opends/server/replication/server/ECLServerHandler.java b/opends/src/server/org/opends/server/replication/server/ECLServerHandler.java
index 8acee72..c9c2a07 100644
--- a/opends/src/server/org/opends/server/replication/server/ECLServerHandler.java
+++ b/opends/src/server/org/opends/server/replication/server/ECLServerHandler.java
@@ -598,22 +598,23 @@
         return null;
       }
 
-      final String crossDomainStartState = oldestRecord.getPreviousCookie();
-      cnIndexDBCursor = cnIndexDB.getCursorFrom(oldestRecord.getChangeNumber());
-      return crossDomainStartState;
+      cnIndexDBCursor =
+          getCursorFrom(cnIndexDB, oldestRecord.getChangeNumber());
+      return oldestRecord.getPreviousCookie();
     }
 
     // Request filter DOES contain a startChangeNumber
 
     // Read the CNIndexDB to see whether it contains startChangeNumber
-    CNIndexRecord startRecord = cnIndexDB.getRecord(startChangeNumber);
+    DBCursor<CNIndexRecord> cursor = cnIndexDB.getCursorFrom(startChangeNumber);
+    final CNIndexRecord startRecord = cursor.getRecord();
     if (startRecord != null)
     {
       // found the provided startChangeNumber, let's return it
-      final String crossDomainStartState = startRecord.getPreviousCookie();
-      cnIndexDBCursor = cnIndexDB.getCursorFrom(startChangeNumber);
-      return crossDomainStartState;
+      cnIndexDBCursor = cursor;
+      return startRecord.getPreviousCookie();
     }
+    close(cursor);
 
     // startChangeNumber provided in the request IS NOT in the CNIndexDB
 
@@ -630,17 +631,18 @@
     // the DB, let's use the lower limit.
     if (startChangeNumber < oldestChangeNumber)
     {
-      CNIndexRecord oldestRecord = cnIndexDB.getRecord(oldestChangeNumber);
+      cursor = cnIndexDB.getCursorFrom(oldestChangeNumber);
+      final CNIndexRecord oldestRecord = cursor.getRecord();
       if (oldestRecord == null)
       {
         // This should not happen
+        close(cursor);
         isEndOfCNIndexDBReached = true;
         return null;
       }
 
-      final String crossDomainStartState = oldestRecord.getPreviousCookie();
-      cnIndexDBCursor = cnIndexDB.getCursorFrom(oldestChangeNumber);
-      return crossDomainStartState;
+      cnIndexDBCursor = cursor;
+      return oldestRecord.getPreviousCookie();
     }
     else if (startChangeNumber <= newestChangeNumber)
     {
@@ -653,9 +655,9 @@
         return null;
       }
 
-      final String crossDomainStartState = newestRecord.getPreviousCookie();
-      cnIndexDBCursor = cnIndexDB.getCursorFrom(newestRecord.getChangeNumber());
-      return crossDomainStartState;
+      cnIndexDBCursor =
+          getCursorFrom(cnIndexDB, newestRecord.getChangeNumber());
+      return newestRecord.getPreviousCookie();
 
       // TODO:ECL ... ok we'll start from the end of the CNIndexDB BUT ...
       // this may be very long. Work on perf improvement here.
@@ -665,6 +667,19 @@
     throw new DirectoryException(ResultCode.SUCCESS, Message.raw(""));
   }
 
+  private DBCursor<CNIndexRecord> getCursorFrom(ChangeNumberIndexDB cnIndexDB,
+      long startChangeNumber) throws ChangelogException
+  {
+    DBCursor<CNIndexRecord> cursor = cnIndexDB.getCursorFrom(startChangeNumber);
+    if (cursor.getRecord() == null)
+    {
+      close(cursor);
+      throw new ChangelogException(Message.raw("Change Number "
+          + startChangeNumber + " is not available in the Changelog"));
+    }
+    return cursor;
+  }
+
   /**
    * Initialize the context for each domain.
    * @param  providedCookie the provided generalized state

--
Gitblit v1.10.0