From 71d60c2db03def788c464506afe1e087cf131db8 Mon Sep 17 00:00:00 2001
From: neil_a_wilson <neil_a_wilson@localhost>
Date: Fri, 23 Feb 2007 20:16:28 +0000
Subject: [PATCH] Revert the changes in revision #1237 (for issue #740) because they have introduced a large number of test failures.

---
 opendj-sdk/opends/src/server/org/opends/server/backends/jeb/ID2Entry.java                              |   62 ++++++++++++++
 opendj-sdk/opends/src/server/org/opends/server/backends/jeb/EntryContainer.java                        |   21 +----
 opendj-sdk/opends/src/server/org/opends/server/backends/jeb/VerifyJob.java                             |   58 +++++++++-----
 opendj-sdk/opends/src/server/org/opends/server/backends/jeb/ImportJob.java                             |    8 +
 opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/backends/jeb/TestVerifyJob.java |   33 +++++++
 5 files changed, 138 insertions(+), 44 deletions(-)

diff --git a/opendj-sdk/opends/src/server/org/opends/server/backends/jeb/EntryContainer.java b/opendj-sdk/opends/src/server/org/opends/server/backends/jeb/EntryContainer.java
index e60ae62..b8a5a52 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/backends/jeb/EntryContainer.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/backends/jeb/EntryContainer.java
@@ -1521,6 +1521,8 @@
         }
       }
 
+      // Increment the entry count.
+      id2entry.adjustRecordCount(txn, 1);
     }
 
     /**
@@ -2019,6 +2021,8 @@
       id2cBuffered.flush();
       id2sBuffered.flush();
 
+      // Decrement the entry count.
+      id2entry.adjustRecordCount(txn, -getDeletedEntryCount());
     }
 
     /**
@@ -3249,7 +3253,7 @@
    */
   public long getEntryCount() throws DatabaseException
   {
-    return id2entry.getRecordCount();
+    return id2entry.getRecordCount(null);
   }
 
   /**
@@ -3733,21 +3737,6 @@
   }
 
   /**
-   * Get the count of key/data pairs in the database in a JE database.
-   * This is a simple wrapper around the JE Database.count method.
-   * @param database the JE database handle.
-   * @return The count of key/data pairs in the database.
-   * @throws DatabaseException If an error occurs in the JE operation.
-   */
-  public static long count(Database database) throws DatabaseException
-  {
-    long count = database.count();
-    assert debugAccess("count", DATABASE_READ, null, database,
-        null, null, null);
-    return count;
-  }
-
-  /**
    * Remove a database from disk.
    *
    * @param name The short database name, to which the entryContainer name will
diff --git a/opendj-sdk/opends/src/server/org/opends/server/backends/jeb/ID2Entry.java b/opendj-sdk/opends/src/server/org/opends/server/backends/jeb/ID2Entry.java
index 5a9a956..f2439af 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/backends/jeb/ID2Entry.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/backends/jeb/ID2Entry.java
@@ -22,7 +22,7 @@
  * CDDL HEADER END
  *
  *
- *      Portions Copyright 2006 - 2007 Sun Microsystems, Inc.
+ *      Portions Copyright 2006 Sun Microsystems, Inc.
  */
 package org.opends.server.backends.jeb;
 
@@ -296,14 +296,70 @@
   /**
    * Get the count of the number of entries stored.
    *
+   * @param txn A JE database transaction to be used for database access, or
+   * null if none is required.
    * @return The number of entries stored.
    *
    * @throws DatabaseException If an error occurs in the JE database.
    */
-  public long getRecordCount() throws DatabaseException
+  public long getRecordCount(Transaction txn) throws DatabaseException
   {
+    DatabaseEntry key;
+    DatabaseEntry data = new DatabaseEntry();
+
+    // The count is stored in a special record whose key is entry ID zero.
+    EntryID id = new EntryID(0);
+    key = id.getDatabaseEntry();
+
     // Read the current count, if any.
-    return EntryContainer.count(getDatabase());
+    OperationStatus status = EntryContainer.read(getDatabase(), txn,
+                                            key, data, LockMode.DEFAULT);
+
+    // Parse the current count.
+    long count = 0;
+    if (status == OperationStatus.SUCCESS)
+    {
+      count = JebFormat.entryIDFromDatabase(data.getData());
+    }
+
+    return count;
   }
 
+  /**
+   * Adjust the count of the number of entries stored.
+   *
+   * @param txn A database transaction, required to be non-null if multiple
+   *            threads are calling this method concurrently.
+   * @param deltaCount Amount to increment (or decrement if negative).
+   * @throws DatabaseException If an error occurs in the JE database.
+   */
+  public void adjustRecordCount(Transaction txn, long deltaCount)
+       throws DatabaseException
+  {
+    DatabaseEntry key;
+    DatabaseEntry data = new DatabaseEntry();
+
+    // The count is stored in a special record whose key is entry ID zero.
+    EntryID id = new EntryID(0);
+    key = id.getDatabaseEntry();
+
+    // Read the current count, if any.
+    OperationStatus status = EntryContainer.read(getDatabase(), txn,
+                                            key, data, LockMode.RMW);
+
+    // Parse the current count.
+    long count = 0;
+    if (status == OperationStatus.SUCCESS)
+    {
+      count = JebFormat.entryIDFromDatabase(data.getData());
+    }
+
+    // Adjust the count.
+    count += deltaCount;
+
+    // Write it.
+    byte[] bytes = JebFormat.entryIDToDatabase(count);
+    data.setData(bytes);
+    EntryContainer.put(getDatabase(), txn, key, data);
+  }
 }
diff --git a/opendj-sdk/opends/src/server/org/opends/server/backends/jeb/ImportJob.java b/opendj-sdk/opends/src/server/org/opends/server/backends/jeb/ImportJob.java
index 07c0989..c8ae959 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/backends/jeb/ImportJob.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/backends/jeb/ImportJob.java
@@ -22,7 +22,7 @@
  * CDDL HEADER END
  *
  *
- *      Portions Copyright 2006 - 2007 Sun Microsystems, Inc.
+ *      Portions Copyright 2006 Sun Microsystems, Inc.
  */
 package org.opends.server.backends.jeb;
 
@@ -581,6 +581,12 @@
       }
     }
 
+    // Record the entry count for each base DN.
+    for (ImportContext ic : importMap.values())
+    {
+      ID2Entry id2e = ic.getEntryContainer().getID2Entry();
+      id2e.adjustRecordCount(null, ic.getEntryInsertCount());
+    }
 
     return moreData;
   }
diff --git a/opendj-sdk/opends/src/server/org/opends/server/backends/jeb/VerifyJob.java b/opendj-sdk/opends/src/server/org/opends/server/backends/jeb/VerifyJob.java
index 68c53ac..6a2ff16 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/backends/jeb/VerifyJob.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/backends/jeb/VerifyJob.java
@@ -22,7 +22,7 @@
  * CDDL HEADER END
  *
  *
- *      Portions Copyright 2006 - 2007 Sun Microsystems, Inc.
+ *      Portions Copyright 2006 Sun Microsystems, Inc.
  */
 package org.opends.server.backends.jeb;
 
@@ -434,7 +434,7 @@
       DatabaseEntry key = new DatabaseEntry();
       DatabaseEntry data = new DatabaseEntry();
 
-      Long storedEntryCount = id2entry.getRecordCount();
+      Long storedEntryCount = null;
 
       OperationStatus status;
       for (status = cursor.getFirst(key, data, LockMode.DEFAULT);
@@ -455,32 +455,48 @@
           continue;
         }
 
-        keyCount++;
-
-        Entry entry;
-        try
+        if (entryID.longValue() == 0)
         {
-          entry = JebFormat.entryFromDatabase(data.getData());
+          // This is the stored entry count.
+          storedEntryCount = JebFormat.entryIDFromDatabase(data.getData());
         }
-        catch (Exception e)
+        else
         {
-          assert debugException(CLASS_NAME, "iterateID2Entry", e);
-          errorCount++;
-          System.err.printf("Malformed id2entry record for ID %d:%n%s%n",
-              entryID.longValue(),
-              StaticUtils.bytesToHex(data.getData()));
-          continue;
-        }
+          keyCount++;
 
-        verifyEntry(entryID, entry);
+          Entry entry;
+          try
+          {
+            entry = JebFormat.entryFromDatabase(data.getData());
+          }
+          catch (Exception e)
+          {
+            assert debugException(CLASS_NAME, "iterateID2Entry", e);
+            errorCount++;
+            System.err.printf("Malformed id2entry record for ID %d:%n%s%n",
+                              entryID.longValue(),
+                              StaticUtils.bytesToHex(data.getData()));
+            continue;
+          }
+
+          verifyEntry(entryID, entry);
+        }
       }
-      if (keyCount != storedEntryCount)
+      if (storedEntryCount != null)
+      {
+        if (keyCount != storedEntryCount)
+        {
+          errorCount++;
+          System.err.printf("The stored entry count in id2entry (%d) does " +
+                            "not agree with the actual number of entry " +
+                            "records found (%d).%n",
+                            storedEntryCount, keyCount);
+        }
+      }
+      else
       {
         errorCount++;
-        System.err.printf("The stored entry count in id2entry (%d) does " +
-            "not agree with the actual number of entry " +
-            "records found (%d).%n",
-            storedEntryCount, keyCount);
+        System.err.printf("Missing record count in id2entry.%n");
       }
     }
     finally
diff --git a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/backends/jeb/TestVerifyJob.java b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/backends/jeb/TestVerifyJob.java
index ca3a8d5..b2540c0 100644
--- a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/backends/jeb/TestVerifyJob.java
+++ b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/backends/jeb/TestVerifyJob.java
@@ -22,7 +22,7 @@
  * CDDL HEADER END
  *
  *
- *      Portions Copyright 2006 - 2007 Sun Microsystems, Inc.
+ *      Portions Copyright 2006 Sun Microsystems, Inc.
  */
 package org.opends.server.backends.jeb;
 
@@ -389,6 +389,18 @@
     	assertTrue(id2entry.putRaw(txn, key1, data1));
     	performBECompleteVerify("telephoneNumber", 3);
     }
+    
+    /**
+     * Change the stored count to invalid value in the telephoneNumber
+     * index.
+     * @throws Exception if the error count is not equal 1.
+     */
+    @Test() public void testBadStoredCount() throws Exception {
+    	preTest(2);
+    	//whack the count
+    	setStoredCount(100);
+    	performBECompleteVerify("telephoneNumber", 1);
+    }
 
     /**
      * 
@@ -413,6 +425,7 @@
     	testDN=DN.decode(noParentDN);
     	id=new EntryID(12);      
     	assertTrue(dn2id.insert(txn, testDN, id));
+    	setStoredCount(12);
     	performBECompleteVerify("dn2id", 3);
     }
     
@@ -439,7 +452,8 @@
     	byte[] idBytesp=new byte[16];
     	idBytesp[7]=(byte) 0xFF;
     	EntryIDSet idSetp=new EntryIDSet(null, idBytesp);
-    	id2child.writeKey(txn, keyp, idSetp);
+    	id2child.writeKey(txn, keyp, idSetp);   	
+    	setStoredCount(12);  	
     	performBECompleteVerify("id2children", 3);
     }
     
@@ -461,6 +475,7 @@
     	EntryIDSet idSetp=new EntryIDSet();   	
     	DatabaseEntry key= new EntryID(2).getDatabaseEntry();
     	id2child.writeKey(txn, key, idSetp);
+    	setStoredCount(3);  	
     	performBECompleteVerify("id2children", 0);
     }
   
@@ -475,7 +490,8 @@
     public void testVerifyID2Subtree() throws Exception {
     	preTest(2);
     	//Add entry with no parent
-    	addID2EntryReturnKey(noParentDN, 3, false);
+    	addID2EntryReturnKey(noParentDN, 3, false);  	
+    	setStoredCount(3);
     	performBECompleteVerify("id2subtree", 3);
     }
   
@@ -497,6 +513,7 @@
     	EntryIDSet idSet=new EntryIDSet();   	
     	DatabaseEntry key= new EntryID(2).getDatabaseEntry();
     	id2subtree.writeKey(txn, key, idSet);
+    	setStoredCount(3);
     	performBECompleteVerify("id2subtree", 1);
     }
 
@@ -662,6 +679,16 @@
     }
     
     
+    /**
+     * Adjust stored entry count in the id2entry file.
+     * @param c new count.
+     * @throws Exception if the putRaw method fails.
+     */
+    private void setStoredCount(long c) throws Exception {
+    	DatabaseEntry keyS= new EntryID(0).getDatabaseEntry();
+    	DatabaseEntry dataS= new EntryID(c).getDatabaseEntry();
+    	assertTrue(id2entry.putRaw(txn, keyS, dataS));
+    }
  
     /**
      * Does a pretest setup. Creates some number of entries, gets

--
Gitblit v1.10.0