From 641e89ef0e15c9edde69f3b8cf82c7dd5f68687a Mon Sep 17 00:00:00 2001
From: Yannick Lecaillez <ylecaillez@forgerock.com>
Date: Wed, 30 Sep 2015 14:28:07 +0000
Subject: [PATCH] OPENDJ-2016: New on disk merge import strategy based on storage engine.

---
 opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/EntryContainer.java |  135 +++++++++++++++------------------------------
 1 files changed, 45 insertions(+), 90 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/EntryContainer.java b/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/EntryContainer.java
index cfb72bc..d527a45 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/EntryContainer.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/EntryContainer.java
@@ -1474,7 +1474,7 @@
 
     // Insert into the indexes, in index configuration order.
     final IndexBuffer indexBuffer = new IndexBuffer();
-    indexInsertEntry(indexBuffer, entry, entryID);
+    insertEntryIntoIndexes(indexBuffer, entry, entryID);
 
     final ByteString encodedEntry = id2entry.encode(entry);
 
@@ -1490,33 +1490,10 @@
             // Check whether the entry already exists.
             if (dn2id.get(txn, entry.getName()) != null)
             {
-              throw new DirectoryException(ResultCode.ENTRY_ALREADY_EXISTS, ERR_ADD_ENTRY_ALREADY_EXISTS.get(
-                  entry.getName()));
+              throw new DirectoryException(ResultCode.ENTRY_ALREADY_EXISTS,
+                  ERR_ADD_ENTRY_ALREADY_EXISTS.get(entry.getName()));
             }
-
-            // Check that the parent entry exists.
-            EntryID parentID = null;
-            if (parentDN != null)
-            {
-              // Check for referral entries above the target.
-              dn2uri.targetEntryReferrals(txn, entry.getName(), null);
-
-              // Read the parent ID from dn2id.
-              parentID = dn2id.get(txn, parentDN);
-              if (parentID == null)
-              {
-                throw new DirectoryException(ResultCode.NO_SUCH_OBJECT,
-                    ERR_ADD_NO_SUCH_OBJECT.get(entry.getName()), getMatchedDN(txn, baseDN), null);
-              }
-              id2childrenCount.addDelta(txn, parentID, 1);
-            }
-
-            dn2id.put(txn, entry.getName(), entryID);
-            dn2uri.addEntry(txn, entry);
-            id2entry.put(txn, entryID, encodedEntry);
-
-            indexBuffer.flush(txn);
-
+            addEntry0(entry, parentDN, entryID, indexBuffer, encodedEntry, txn);
             if (addOperation != null)
             {
               // One last check before committing
@@ -1552,6 +1529,39 @@
     }
   }
 
+  void addEntry0(final Entry entry, final DN parentDN, final EntryID entryID, final IndexBuffer indexBuffer,
+      final ByteString encodedEntry, WriteableTransaction txn) throws DirectoryException
+  {
+    // Check that the parent entry exists.
+    if (parentDN != null)
+    {
+      // Check for referral entries above the target.
+      dn2uri.targetEntryReferrals(txn, entry.getName(), null);
+
+      final EntryID parentID = dn2id.get(txn, parentDN);
+      if (parentID == null)
+      {
+        throw new DirectoryException(ResultCode.NO_SUCH_OBJECT,
+            ERR_ADD_NO_SUCH_OBJECT.get(entry.getName()), getMatchedDN(txn, baseDN), null);
+      }
+      id2childrenCount.addDelta(txn, parentID, 1);
+    }
+
+    dn2id.put(txn, entry.getName(), entryID);
+    dn2uri.addEntry(txn, entry);
+    id2entry.put(txn, entryID, encodedEntry);
+
+    indexBuffer.flush(txn);
+  }
+
+  void importEntry(WriteableTransaction txn, EntryID entryID, Entry entry) throws DirectoryException,
+      StorageRuntimeException
+  {
+    final IndexBuffer indexBuffer = IndexBuffer.newImportIndexBuffer(txn, entryID);
+    insertEntryIntoIndexes(indexBuffer, entry, entryID);
+    addEntry0(entry, null, entryID, indexBuffer, id2entry.encode(entry), txn);
+  }
+
   /**
    * Removes the specified entry from this tree.  This method must ensure
    * that the entry exists and that it does not have any subordinate entries
@@ -1765,7 +1775,7 @@
     }
 
     // Remove from the indexes, in index config order.
-    indexRemoveEntry(indexBuffer, entry, leafID);
+    removeEntryFromIndexes(indexBuffer, entry, leafID);
 
     // Remove the children counter for this entry.
     id2childrenCount.deleteCount(txn, leafID);
@@ -1964,8 +1974,8 @@
             else
             {
               // The most optimal would be to figure out what the modifications were.
-              indexRemoveEntry(indexBuffer, oldEntry, entryID);
-              indexInsertEntry(indexBuffer, newEntry, entryID);
+              removeEntryFromIndexes(indexBuffer, oldEntry, entryID);
+              insertEntryIntoIndexes(indexBuffer, newEntry, entryID);
             }
 
             indexBuffer.flush(txn);
@@ -2256,7 +2266,7 @@
     if (renumbered || modifyDNOperation == null)
     {
       // Reindex the entry with the new ID.
-      indexInsertEntry(buffer, newEntry, newID);
+      insertEntryIntoIndexes(buffer, newEntry, newID);
     }
 
     if(isApexEntryMoved)
@@ -2309,7 +2319,7 @@
     if (!newID.equals(oldID) || modifyDNOperation == null)
     {
       // Reindex the entry with the new ID.
-      indexRemoveEntry(buffer, oldEntry, oldID);
+      removeEntryFromIndexes(buffer, oldEntry, oldID);
     }
     else
     {
@@ -2391,7 +2401,7 @@
       id2childrenCount.deleteCount(txn, oldID);
 
       // Reindex the entry with the new ID.
-      indexRemoveEntry(buffer, oldEntry, oldID);
+      removeEntryFromIndexes(buffer, oldEntry, oldID);
     }
     else if (!modifications.isEmpty())
     {
@@ -2444,7 +2454,7 @@
    * @throws StorageRuntimeException If an error occurs in the storage.
    * @throws DirectoryException If a Directory Server error occurs.
    */
-  private void indexInsertEntry(IndexBuffer buffer, Entry entry, EntryID entryID)
+  private void insertEntryIntoIndexes(IndexBuffer buffer, Entry entry, EntryID entryID)
       throws StorageRuntimeException, DirectoryException
   {
     for (AttributeIndex index : attrIndexMap.values())
@@ -2467,7 +2477,7 @@
    * @throws StorageRuntimeException If an error occurs in the storage.
    * @throws DirectoryException If a Directory Server error occurs.
    */
-  private void indexRemoveEntry(IndexBuffer buffer, Entry entry, EntryID entryID)
+  private void removeEntryFromIndexes(IndexBuffer buffer, Entry entry, EntryID entryID)
       throws StorageRuntimeException, DirectoryException
   {
     for (AttributeIndex index : attrIndexMap.values())
@@ -2622,61 +2632,6 @@
     return treePrefix;
   }
 
-  /**
-   * Sets a new tree prefix for this entry container and rename all
-   * existing trees in use by this entry container.
-   *
-   * @param txn the transaction for renaming Trees
-   * @param newBaseDN The new tree prefix to use.
-   * @throws StorageRuntimeException If an error occurs in the storage.
-   */
-  void setTreePrefix(WriteableTransaction txn, final String newBaseDN) throws StorageRuntimeException
-  {
-    final List<Tree> allTrees = listTrees();
-    try
-    {
-      // Rename in transaction.
-      for(Tree tree : allTrees)
-      {
-        TreeName oldName = tree.getName();
-        TreeName newName = oldName.replaceBaseDN(newBaseDN);
-        txn.renameTree(oldName, newName);
-      }
-      // Only rename the containers if the txn succeeded.
-      for (Tree tree : allTrees)
-      {
-        TreeName oldName = tree.getName();
-        TreeName newName = oldName.replaceBaseDN(newBaseDN);
-        tree.setName(newName);
-      }
-    }
-    catch (Exception e)
-    {
-      String msg = e.getMessage();
-      if (msg == null)
-      {
-        msg = stackTraceToSingleLineString(e);
-      }
-      throw new StorageRuntimeException(ERR_UNCHECKED_EXCEPTION.get(msg).toString(), e);
-    }
-    try
-    {
-      for(Tree tree : allTrees)
-      {
-        tree.open(txn, false);
-      }
-    }
-    catch (Exception e)
-    {
-      String msg = e.getMessage();
-      if (msg == null)
-      {
-        msg = stackTraceToSingleLineString(e);
-      }
-      throw new StorageRuntimeException(ERR_UNCHECKED_EXCEPTION.get(msg).toString(), e);
-    }
-  }
-
   @Override
   public DN getBaseDN()
   {

--
Gitblit v1.10.0