From 225aaf491fb097f9ab165de1c4d24aaa68670784 Mon Sep 17 00:00:00 2001
From: Jean-Noel Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Tue, 24 Sep 2013 09:19:20 +0000
Subject: [PATCH] OPENDJ-1116 Introduce abstraction for the changelog DB

---
 opends/src/server/org/opends/server/replication/server/ReplicationServer.java          |   10 ++--
 opends/src/server/org/opends/server/replication/server/changelog/je/JEChangelogDB.java |   75 ++++++++++++++++++-------------------
 opends/src/server/org/opends/server/replication/server/changelog/api/ChangelogDB.java  |   25 +++++-------
 3 files changed, 52 insertions(+), 58 deletions(-)

diff --git a/opends/src/server/org/opends/server/replication/server/ReplicationServer.java b/opends/src/server/org/opends/server/replication/server/ReplicationServer.java
index be985b3..ba9708a 100644
--- a/opends/src/server/org/opends/server/replication/server/ReplicationServer.java
+++ b/opends/src/server/org/opends/server/replication/server/ReplicationServer.java
@@ -101,7 +101,7 @@
   private volatile boolean shutdown = false;
   private int rcvWindow;
   private int queueSize;
-  private final ChangelogDB changelogDB = new JEChangelogDB(this);
+  private final ChangelogDB changelogDB;
 
   /**
    * The delay (in sec) after which the changes must be deleted from the
@@ -221,8 +221,8 @@
     purgeDelay = configuration.getReplicationPurgeDelay();
     rcvWindow = configuration.getWindowSize();
 
-    this.changelogDB.setReplicationDBDirectory(configuration
-        .getReplicationDBDirectory());
+    this.changelogDB =
+        new JEChangelogDB(this, configuration.getReplicationDBDirectory());
 
     groupId = (byte)configuration.getGroupId();
     weight = configuration.getWeight();
@@ -973,7 +973,7 @@
     }
 
     final String newDir = configuration.getReplicationDBDirectory();
-    if (newDir != null && !this.changelogDB.getDBDirName().equals(newDir))
+    if (newDir != null && !this.changelogDB.getDBDirectoryName().equals(newDir))
     {
       return new ConfigChangeResult(ResultCode.SUCCESS, true);
     }
@@ -1744,7 +1744,7 @@
    */
   public String getDbDirName()
   {
-    return this.changelogDB.getDBDirName();
+    return this.changelogDB.getDBDirectoryName();
   }
 
   /**
diff --git a/opends/src/server/org/opends/server/replication/server/changelog/api/ChangelogDB.java b/opends/src/server/org/opends/server/replication/server/changelog/api/ChangelogDB.java
index 0ff0187..f8bd02d 100644
--- a/opends/src/server/org/opends/server/replication/server/changelog/api/ChangelogDB.java
+++ b/opends/src/server/org/opends/server/replication/server/changelog/api/ChangelogDB.java
@@ -29,7 +29,6 @@
 import java.util.Map;
 import java.util.Set;
 
-import org.opends.server.config.ConfigException;
 import org.opends.server.replication.common.CSN;
 import org.opends.server.replication.protocol.UpdateMsg;
 import org.opends.server.types.DN;
@@ -52,31 +51,27 @@
   // DB control methods
 
   /**
-   * Set the directory to be used by the replication database.
-   *
-   * @param dbDirName
-   *          the directory for use by the replication database
-   * @throws ConfigException
-   *           if a problem occurs opening the directory
-   */
-  void setReplicationDBDirectory(String dbDirName) throws ConfigException;
-
-  /**
    * Get the replication server database directory. This is used by tests to do
    * some cleanup.
    *
    * @return the database directory name
    */
-  String getDBDirName();
+  String getDBDirectoryName();
 
   /**
-   * Initializes the replication database.
+   * Initializes the replication database by reading its previous state and
+   * building the relevant ReplicaDBs according to the previous state. This
+   * method must be called once before using the ChangelogDB.
    */
   void initializeDB();
 
   /**
-   * Sets the purge delay for the replication database. This purge delay is a
-   * best effort.
+   * Sets the purge delay for the replication database. Can be called while the
+   * database is running.
+   * <p>
+   * Purging happens on a best effort basis, i.e. the purge delay is used by the
+   * replication database to know which data can be purged, but there are no
+   * guarantees on when the purging will actually happen.
    *
    * @param delayInMillis
    *          the purge delay in milliseconds
diff --git a/opends/src/server/org/opends/server/replication/server/changelog/je/JEChangelogDB.java b/opends/src/server/org/opends/server/replication/server/changelog/je/JEChangelogDB.java
index b5e51e5..e7981f3 100644
--- a/opends/src/server/org/opends/server/replication/server/changelog/je/JEChangelogDB.java
+++ b/opends/src/server/org/opends/server/replication/server/changelog/je/JEChangelogDB.java
@@ -66,8 +66,8 @@
   private final Map<DN, Map<Integer, DbHandler>> sourceDbHandlers =
       new ConcurrentHashMap<DN, Map<Integer, DbHandler>>();
   private ReplicationDbEnv dbEnv;
-  private String dbDirName = null;
-  private File dbDirectory;
+  private final String dbDirectoryName;
+  private final File dbDirectory;
 
   /** The local replication server. */
   private final ReplicationServer replicationServer;
@@ -77,10 +77,40 @@
    *
    * @param replicationServer
    *          the local replication server.
+   * @param dbDirName
+   *          the directory for use by the replication database
+   * @throws ConfigException
+   *           if a problem occurs opening the supplied directory
    */
-  public JEChangelogDB(ReplicationServer replicationServer)
+  public JEChangelogDB(ReplicationServer replicationServer, String dbDirName)
+      throws ConfigException
   {
     this.replicationServer = replicationServer;
+    this.dbDirectoryName = dbDirName != null ? dbDirName : "changelogDb";
+    this.dbDirectory = makeDir(this.dbDirectoryName);
+  }
+
+  private File makeDir(String dbDirName) throws ConfigException
+  {
+    // Check that this path exists or create it.
+    File dbDirectory = getFileForPath(dbDirName);
+    try
+    {
+      if (!dbDirectory.exists())
+      {
+        dbDirectory.mkdir();
+      }
+      return dbDirectory;
+    }
+    catch (Exception e)
+    {
+      MessageBuilder mb = new MessageBuilder();
+      mb.append(e.getLocalizedMessage());
+      mb.append(" ");
+      mb.append(String.valueOf(dbDirectory));
+      Message msg = ERR_FILE_CHECK_CREATE_FAILED.get(mb.toString());
+      throw new ConfigException(msg, e);
+    }
   }
 
   private Map<Integer, DbHandler> getDomainMap(DN baseDN)
@@ -146,8 +176,8 @@
   {
     try
     {
-      dbEnv = new ReplicationDbEnv(getFileForPath(dbDirName).getAbsolutePath(),
-          replicationServer);
+      dbEnv = new ReplicationDbEnv(
+          getFileForPath(dbDirectoryName).getAbsolutePath(), replicationServer);
       initializeChangelogState(dbEnv.readChangelogState());
     }
     catch (ChangelogException e)
@@ -369,40 +399,9 @@
 
   /** {@inheritDoc} */
   @Override
-  public void setReplicationDBDirectory(String dbDirName)
-      throws ConfigException
+  public String getDBDirectoryName()
   {
-    if (dbDirName == null)
-    {
-      dbDirName = "changelogDb";
-    }
-    this.dbDirName = dbDirName;
-
-    // Check that this path exists or create it.
-    dbDirectory = getFileForPath(this.dbDirName);
-    try
-    {
-      if (!dbDirectory.exists())
-      {
-        dbDirectory.mkdir();
-      }
-    }
-    catch (Exception e)
-    {
-      MessageBuilder mb = new MessageBuilder();
-      mb.append(e.getLocalizedMessage());
-      mb.append(" ");
-      mb.append(String.valueOf(dbDirectory));
-      Message msg = ERR_FILE_CHECK_CREATE_FAILED.get(mb.toString());
-      throw new ConfigException(msg, e);
-    }
-  }
-
-  /** {@inheritDoc} */
-  @Override
-  public String getDBDirName()
-  {
-    return this.dbDirName;
+    return this.dbDirectoryName;
   }
 
   /** {@inheritDoc} */

--
Gitblit v1.10.0