From 39cfb739395cca91589c958fda4790c87ce0c9c8 Mon Sep 17 00:00:00 2001
From: Nicolas Capponi <nicolas.capponi@forgerock.com>
Date: Fri, 02 Jan 2015 14:42:16 +0000
Subject: [PATCH] OPENDJ-1585 Re-implement DN normalization in server

---
 opendj3-server-dev/src/server/org/opends/server/backends/jeb/JebFormat.java |  112 ++++++-------------------------------------------------
 1 files changed, 13 insertions(+), 99 deletions(-)

diff --git a/opendj3-server-dev/src/server/org/opends/server/backends/jeb/JebFormat.java b/opendj3-server-dev/src/server/org/opends/server/backends/jeb/JebFormat.java
index a0a0d4b..1a14d7c 100644
--- a/opendj3-server-dev/src/server/org/opends/server/backends/jeb/JebFormat.java
+++ b/opendj3-server-dev/src/server/org/opends/server/backends/jeb/JebFormat.java
@@ -22,18 +22,12 @@
  *
  *
  *      Copyright 2006-2010 Sun Microsystems, Inc.
- *      Portions Copyright 2014 ForgeRock AS
+ *      Portions Copyright 2014-2015 ForgeRock AS
  */
 package org.opends.server.backends.jeb;
 
-import java.util.Iterator;
-import java.util.TreeSet;
-
 import org.forgerock.opendj.ldap.ByteStringBuilder;
 import org.opends.server.types.DN;
-import org.opends.server.types.DirectoryException;
-import org.opends.server.types.RDN;
-import org.opends.server.util.StaticUtils;
 
 /**
  * Handles the disk representation of LDAP data.
@@ -254,62 +248,6 @@
   }
 
   /**
-   * Decode a DN value from its database key representation.
-   *
-   * @param dnKey The database key value of the DN.
-   * @param prefix The DN to prefix the decoded DN value.
-   * @return The decoded DN value.
-   * @throws DirectoryException if an error occurs while decoding the DN value.
-   * @see #dnToDNKey(DN, int)
-   */
-  public static DN dnFromDNKey(byte[] dnKey, DN prefix)
-      throws DirectoryException
-  {
-    DN dn = prefix;
-    boolean escaped = false;
-    ByteStringBuilder buffer = new ByteStringBuilder();
-    for (byte b : dnKey)
-    {
-      if (b == 0x5C)
-      {
-        escaped = true;
-        continue;
-      }
-      else if (!escaped && b == 0x01)
-      {
-        buffer.append(0x01);
-        escaped = false;
-        continue;
-      }
-      else if (!escaped && b == 0x00)
-      {
-        if(buffer.length() > 0)
-        {
-          dn = dn.child(RDN.decode(buffer.toString()));
-          buffer.clear();
-        }
-      }
-      else
-      {
-        if(escaped)
-        {
-          buffer.append(0x5C);
-          escaped = false;
-        }
-        buffer.append(b);
-      }
-    }
-
-    if(buffer.length() > 0)
-    {
-      dn = dn.child(RDN.decode(buffer.toString()));
-    }
-
-    return dn;
-  }
-
-
-  /**
    * Find the length of bytes that represents the superior DN of the given
    * DN key. The superior DN is represented by the initial bytes of the DN key.
    *
@@ -334,17 +272,17 @@
    */
   public static int findDNKeyParent(byte[] dnKey, int offset, int length)
   {
-    if(length == 0)
+    if (length == 0)
     {
       // This is the root or base DN
       return -1;
     }
 
-    // We will walk backwords through the buffer and find the first
-    // unescaped comma
-    for(int i = offset+length - 1; i >= offset; i--)
+    // We will walk backwords through the buffer and
+    // find the first unescaped NORMALIZED_RDN_SEPARATOR
+    for (int i = offset+length - 1; i >= offset; i--)
     {
-      if(dnKey[i] == 0x00 && i-1 >= offset && dnKey[i-1] != 0x5C)
+      if (dnKey[i] == DN.NORMALIZED_RDN_SEPARATOR && i-1 >= offset && dnKey[i-1] != DN.NORMALIZED_ESC_BYTE)
       {
         return i;
       }
@@ -354,48 +292,24 @@
 
   /**
    * Create a DN database key from an entry DN.
+   *
    * @param dn The entry DN.
    * @param prefixRDNs The number of prefix RDNs to remove from the encoded
    *                   representation.
    * @return A DatabaseEntry containing the key.
-   * @see #dnFromDNKey(byte[], DN)
    */
   public static byte[] dnToDNKey(DN dn, int prefixRDNs)
   {
-    StringBuilder buffer = new StringBuilder();
-    for (int i = dn.size() - prefixRDNs - 1; i >= 0; i--)
+    ByteStringBuilder builder = new ByteStringBuilder();
+    int startSize = dn.size() - prefixRDNs - 1;
+    for (int i = startSize; i >= 0; i--)
     {
-      buffer.append('\u0000');
-      formatRDNKey(dn.getRDN(i), buffer);
+        builder.append(DN.NORMALIZED_RDN_SEPARATOR);
+        dn.getRDN(i).toNormalizedByteString(builder);
     }
 
-    return StaticUtils.getBytes(buffer.toString());
+    return builder.toByteArray();
   }
 
-  private static void formatRDNKey(RDN rdn, StringBuilder buffer)
-  {
-    if (!rdn.isMultiValued())
-    {
-      rdn.toNormalizedString(buffer);
-    }
-    else
-    {
-      TreeSet<String> rdnElementStrings = new TreeSet<String>();
 
-      for (int i=0; i < rdn.getNumValues(); i++)
-      {
-        StringBuilder b2 = new StringBuilder();
-        rdn.getNormalizedAVAString(i, b2);
-        rdnElementStrings.add(b2.toString());
-      }
-
-      Iterator<String> iterator = rdnElementStrings.iterator();
-      buffer.append(iterator.next().replace("\u0001", "\\\u0001"));
-      while (iterator.hasNext())
-      {
-        buffer.append('\u0001');
-        buffer.append(iterator.next().replace("\u0001", "\\\u0001"));
-      }
-    }
-  }
 }

--
Gitblit v1.10.0