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

---
 opends/src/server/org/opends/server/replication/server/changelog/je/DraftCNDB.java |   29 ++++++++++-------------------
 opends/src/server/org/opends/server/replication/server/changelog/api/DBCursor.java |   12 ++++++++++++
 2 files changed, 22 insertions(+), 19 deletions(-)

diff --git a/opends/src/server/org/opends/server/replication/server/changelog/api/DBCursor.java b/opends/src/server/org/opends/server/replication/server/changelog/api/DBCursor.java
index 8de00fb..3c1a43d 100644
--- a/opends/src/server/org/opends/server/replication/server/changelog/api/DBCursor.java
+++ b/opends/src/server/org/opends/server/replication/server/changelog/api/DBCursor.java
@@ -32,6 +32,18 @@
  * Generic cursor interface into the changelog database. Once it is not used
  * anymore, a cursor must be closed to release all the resources into the
  * database.
+ * <p>
+ * Here is a typical usage pattern:
+ *
+ * <pre>
+ * DBCursor&lt;T&gt; cursor = ...;         // obtain a new cursor,
+ *                                   // already initialized
+ * T record1 = cursor.getRecord();   // get the first record
+ * while (cursor.next()) {           // advance to the next record
+ *   T record = cursor.getRecord();  // get the next record
+ *   ...                             // etc.
+ * }
+ * </pre>
  *
  * @param <T>
  *          type of the record being returned
diff --git a/opends/src/server/org/opends/server/replication/server/changelog/je/DraftCNDB.java b/opends/src/server/org/opends/server/replication/server/changelog/je/DraftCNDB.java
index 9a1c9f8..0de4f51 100644
--- a/opends/src/server/org/opends/server/replication/server/changelog/je/DraftCNDB.java
+++ b/opends/src/server/org/opends/server/replication/server/changelog/je/DraftCNDB.java
@@ -278,7 +278,7 @@
 
       return db.count();
     }
-    catch (Exception e)
+    catch (DatabaseException e)
     {
       TRACER.debugCaught(DebugLogLevel.ERROR, e);
     }
@@ -571,19 +571,9 @@
       {
         return null;
       }
-
-      try
-      {
-        return record;
-      }
-      catch (Exception e)
-      {
-        TRACER.debugCaught(DebugLogLevel.ERROR, e);
-        return null;
-      }
+      return record;
     }
 
-
     /**
      * Go to the next record on the cursor.
      * @return the next record on this cursor.
@@ -591,6 +581,8 @@
      */
     public boolean next() throws ChangelogException
     {
+      // first wipe old entry
+      record = null;
       if (isClosed)
       {
         return false;
@@ -598,18 +590,17 @@
 
       try {
         OperationStatus status = cursor.getNext(key, entry, LockMode.DEFAULT);
-        if (status != OperationStatus.SUCCESS)
+        if (status == OperationStatus.SUCCESS)
         {
-          record = null;
-          return false;
+          record = newCNIndexRecord(this.key, entry);
+          return true;
         }
-        record = newCNIndexRecord(this.key, entry);
+        return false;
       }
-      catch(Exception e)
+      catch (DatabaseException e)
       {
-        TRACER.debugCaught(DebugLogLevel.ERROR, e);
+        throw new ChangelogException(e);
       }
-      return true;
     }
 
     /**

--
Gitblit v1.10.0