From 2b7a597a7d5f49f5b98c024b177083da3785dad6 Mon Sep 17 00:00:00 2001
From: jvergara <jvergara@localhost>
Date: Wed, 22 Apr 2009 15:26:34 +0000
Subject: [PATCH] Fix for issue 3935 (ldapsearch --countEntries and debugsearchindex : incorrect behavior) Parse the result sent by the server when the debugsearchindex attribute is requested.

---
 opends/src/server/org/opends/server/tools/LDAPSearch.java |   66 ++++++++++++++++++++++++++++++++
 1 files changed, 65 insertions(+), 1 deletions(-)

diff --git a/opends/src/server/org/opends/server/tools/LDAPSearch.java b/opends/src/server/org/opends/server/tools/LDAPSearch.java
index 65b6d7e..0db90e4 100644
--- a/opends/src/server/org/opends/server/tools/LDAPSearch.java
+++ b/opends/src/server/org/opends/server/tools/LDAPSearch.java
@@ -35,6 +35,7 @@
 import java.util.*;
 import java.util.concurrent.atomic.AtomicInteger;
 
+import org.opends.server.backends.jeb.EntryContainer;
 import org.opends.server.controls.*;
 import org.opends.server.util.Base64;
 import org.opends.server.util.EmbeddedUtils;
@@ -145,6 +146,9 @@
   {
     int matchingEntries = 0;
 
+    boolean hasDebugSearchIndex =
+      attributes.contains(EntryContainer.ATTR_DEBUG_SEARCH_INDEX);
+
     for (LDAPFilter filter: filters)
     {
       ByteString asn1OctetStr = ByteString.valueOf(baseDN);
@@ -281,7 +285,15 @@
                 StringBuilder sb = new StringBuilder();
                 toLDIF(searchEntryOp, sb, wrapColumn, typesOnly);
                 out.print(sb.toString());
-                matchingEntries++;
+                if (!hasDebugSearchIndex)
+                {
+                  matchingEntries++;
+                }
+                else
+                {
+                  matchingEntries +=
+                    getDebugSearchIndexEntries(searchEntryOp);
+                }
                 break;
 
               case OP_TYPE_SEARCH_RESULT_REFERENCE:
@@ -1849,5 +1861,57 @@
       }
     }
   }
+
+  /**
+   * Parses the contents of the provided entry and returning the number of
+   * entries found.  The method assumed that the entry is the generated entry
+   * sent by the server because the user asked to retrieve the attribute
+   * debugsearchindex.
+   * @param searchEntryOp the entry returned by the server.
+   * @return the number of total entries that matched the provided filter.
+   * @see org.opends.server.backends.jeb.EntryIDSet.toString(StringBuffer )
+   */
+  private int getDebugSearchIndexEntries(
+      SearchResultEntryProtocolOp searchEntryOp)
+  {
+    int matchingEntries = 0;
+    String attrValue = null;
+    LinkedList<LDAPAttribute> attributes = searchEntryOp.getAttributes();
+    // Add the attributes to the buffer.
+    for (LDAPAttribute a : attributes)
+    {
+      if (EntryContainer.ATTR_DEBUG_SEARCH_INDEX.equalsIgnoreCase(
+          a.getAttributeType()))
+      {
+        ArrayList<ByteString> values = a.getValues();
+        if (values != null && !values.isEmpty())
+        {
+          attrValue = values.get(0).toString();
+        }
+        break;
+      }
+    }
+
+    if (attrValue != null)
+    {
+      // If the user asked for the debug search index attribute, the resulting
+      // entry has an attribute of type:
+      // debugsearchindex: filter=(uid=*)[NOT-INDEXED]
+      // scope=wholeSubtree[COUNT:11] final=[COUNT:11]
+      // Which is generated in org.opends.server.backends.jeb.EntryIDSet
+      String sCount = "[COUNT:";
+      int index1 = attrValue.lastIndexOf(sCount);
+      if (index1 != -1)
+      {
+        int index2 = attrValue.lastIndexOf("]");
+        if (index2 != -1)
+        {
+          String s = attrValue.substring(index1 + sCount.length(), index2);
+          matchingEntries += Integer.parseInt(s);
+        }
+      }
+    }
+    return matchingEntries;
+  }
 }
 

--
Gitblit v1.10.0