From 84cf626ebcae1b535abe9efd3eed5cdf78bdd319 Mon Sep 17 00:00:00 2001
From: Jean-Noel Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Thu, 05 Sep 2013 07:51:54 +0000
Subject: [PATCH] OPENDJ-1116 Introduce abstraction for the changelog DB

---
 opends/src/server/org/opends/server/replication/server/changelog/je/DraftCNData.java |  107 +++++++++++++++++++++--------------------------------
 1 files changed, 43 insertions(+), 64 deletions(-)

diff --git a/opends/src/server/org/opends/server/replication/server/changelog/je/DraftCNData.java b/opends/src/server/org/opends/server/replication/server/changelog/je/DraftCNData.java
index 205c5de..8335497 100644
--- a/opends/src/server/org/opends/server/replication/server/changelog/je/DraftCNData.java
+++ b/opends/src/server/org/opends/server/replication/server/changelog/je/DraftCNData.java
@@ -31,6 +31,7 @@
 
 import org.opends.messages.Message;
 import org.opends.server.replication.common.CSN;
+import org.opends.server.replication.server.changelog.api.CNIndexData;
 import org.opends.server.replication.server.changelog.api.ChangelogException;
 
 import com.sleepycat.je.DatabaseEntry;
@@ -46,18 +47,25 @@
 
   private static final long serialVersionUID = 1L;
 
-  private String value;
-  private String baseDN;
-  private CSN csn;
+  private long changeNumber;
+  private CNIndexData cnIndexData;
 
   /**
    * Creates a record to be stored in the DraftCNDB.
-   * @param previousCookie The previous cookie.
-   * @param baseDN The baseDN (domain DN).
-   * @param csn The replication CSN.
+   *
+   * @param changeNumber
+   *          the change number
+   * @param previousCookie
+   *          The previous cookie
+   * @param baseDN
+   *          The baseDN (domain DN)
+   * @param csn
+   *          The replication CSN
    */
-  public DraftCNData(String previousCookie, String baseDN, CSN csn)
+  public DraftCNData(long changeNumber, String previousCookie, String baseDN,
+      CSN csn)
   {
+    this.changeNumber = changeNumber;
     String record =
         previousCookie + FIELD_SEPARATOR + baseDN + FIELD_SEPARATOR + csn;
     setData(getBytes(record));
@@ -65,29 +73,38 @@
 
   /**
    * Creates a record to be stored in the DraftCNDB from the provided byte[].
-   * @param data the provided byte[].
-   * @throws ChangelogException a.
+   *
+   * @param changeNumber
+   *          the change number
+   * @param data
+   *          the provided byte[]
+   * @throws ChangelogException
+   *           if a database problem occurred
    */
-  public DraftCNData(byte[] data) throws ChangelogException
+  public DraftCNData(long changeNumber, byte[] data) throws ChangelogException
   {
-    decodeData(data);
+    this.changeNumber = changeNumber;
+    this.cnIndexData = decodeData(changeNumber, data);
   }
 
   /**
-   * Decode a record into fields.
-   * @param data the provided byte array.
-   * @throws ChangelogException when a problem occurs.
+   * Decode and returns a {@link CNIndexData} record.
+   *
+   * @param changeNumber
+   * @param data
+   *          the provided byte array.
+   * @return the decoded {@link CNIndexData} record
+   * @throws ChangelogException
+   *           when a problem occurs.
    */
-  public void decodeData(byte[] data) throws ChangelogException
+  private CNIndexData decodeData(long changeNumber, byte[] data)
+      throws ChangelogException
   {
     try
     {
       String stringData = new String(data, "UTF-8");
-
       String[] str = stringData.split(FIELD_SEPARATOR, 3);
-      value = str[0];
-      baseDN = str[1];
-      csn = new CSN(str[2]);
+      return new CNIndexData(changeNumber, str[0], str[1], new CSN(str[2]));
     }
     catch (UnsupportedEncodingException e)
     {
@@ -98,43 +115,17 @@
   }
 
   /**
-   * Getter for the value.
+   * Getter for the decoded {@link CNIndexData} record.
    *
-   * @return the value.
-   * @throws ChangelogException when a problem occurs.
-   */
-  public String getValue() throws ChangelogException
-  {
-    if (value == null)
-      decodeData(getData());
-    return this.value;
-  }
-
-  /**
-   * Getter for the service ID.
-   *
-   * @return The baseDN
-   * @throws ChangelogException when a problem occurs.
-   */
-  public String getBaseDN() throws ChangelogException
-  {
-    if (value == null)
-      decodeData(getData());
-    return this.baseDN;
-  }
-
-  /**
-   * Getter for the replication CSN.
-   *
-   * @return the replication CSN.
+   * @return the CNIndexData record.
    * @throws ChangelogException
    *           when a problem occurs.
    */
-  public CSN getCSN() throws ChangelogException
+  public CNIndexData getCNIndexData() throws ChangelogException
   {
-    if (value == null)
-      decodeData(getData());
-    return this.csn;
+    if (cnIndexData == null)
+      cnIndexData = decodeData(changeNumber, getData());
+    return cnIndexData;
   }
 
   /**
@@ -144,19 +135,7 @@
   @Override
   public String toString()
   {
-    StringBuilder buffer = new StringBuilder();
-    toString(buffer);
-    return buffer.toString();
+    return "DraftCNData : [" + cnIndexData + "]";
   }
 
-  /**
-   * Dump a string representation of these data into the provided buffer.
-   * @param buffer the provided buffer.
-   */
-  public void toString(StringBuilder buffer)
-  {
-    buffer.append("DraftCNData : [value=").append(value);
-    buffer.append("] [serviceID=").append(baseDN);
-    buffer.append("] [csn=").append(csn).append("]");
-  }
 }

--
Gitblit v1.10.0