From 88b3513bbf0f7679c2d6fa44b10933a7fba32bc6 Mon Sep 17 00:00:00 2001
From: boli <boli@localhost>
Date: Fri, 23 Feb 2007 19:22:39 +0000
Subject: [PATCH] Changed ID2Entry#getRecordCount to use the Database#count method in JE 3.1.0. The JE backend no longer keeps a count of entries in entry ID 0. Modified TestVerifyJob unit test to no longer simulate a incorrect entry count.

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

diff --git a/opends/src/server/org/opends/server/backends/jeb/EntryContainer.java b/opends/src/server/org/opends/server/backends/jeb/EntryContainer.java
index b8a5a52..e60ae62 100644
--- a/opends/src/server/org/opends/server/backends/jeb/EntryContainer.java
+++ b/opends/src/server/org/opends/server/backends/jeb/EntryContainer.java
@@ -1521,8 +1521,6 @@
         }
       }
 
-      // Increment the entry count.
-      id2entry.adjustRecordCount(txn, 1);
     }
 
     /**
@@ -2021,8 +2019,6 @@
       id2cBuffered.flush();
       id2sBuffered.flush();
 
-      // Decrement the entry count.
-      id2entry.adjustRecordCount(txn, -getDeletedEntryCount());
     }
 
     /**
@@ -3253,7 +3249,7 @@
    */
   public long getEntryCount() throws DatabaseException
   {
-    return id2entry.getRecordCount(null);
+    return id2entry.getRecordCount();
   }
 
   /**
@@ -3737,6 +3733,21 @@
   }
 
   /**
+   * 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/opends/src/server/org/opends/server/backends/jeb/ID2Entry.java b/opends/src/server/org/opends/server/backends/jeb/ID2Entry.java
index f2439af..5a9a956 100644
--- a/opends/src/server/org/opends/server/backends/jeb/ID2Entry.java
+++ b/opends/src/server/org/opends/server/backends/jeb/ID2Entry.java
@@ -22,7 +22,7 @@
  * CDDL HEADER END
  *
  *
- *      Portions Copyright 2006 Sun Microsystems, Inc.
+ *      Portions Copyright 2006 - 2007 Sun Microsystems, Inc.
  */
 package org.opends.server.backends.jeb;
 
@@ -296,70 +296,14 @@
   /**
    * 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(Transaction txn) throws DatabaseException
+  public long getRecordCount() 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.DEFAULT);
-
-    // Parse the current count.
-    long count = 0;
-    if (status == OperationStatus.SUCCESS)
-    {
-      count = JebFormat.entryIDFromDatabase(data.getData());
-    }
-
-    return count;
+    return EntryContainer.count(getDatabase());
   }
 
-  /**
-   * 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/opends/src/server/org/opends/server/backends/jeb/ImportJob.java b/opends/src/server/org/opends/server/backends/jeb/ImportJob.java
index c8ae959..07c0989 100644
--- a/opends/src/server/org/opends/server/backends/jeb/ImportJob.java
+++ b/opends/src/server/org/opends/server/backends/jeb/ImportJob.java
@@ -22,7 +22,7 @@
  * CDDL HEADER END
  *
  *
- *      Portions Copyright 2006 Sun Microsystems, Inc.
+ *      Portions Copyright 2006 - 2007 Sun Microsystems, Inc.
  */
 package org.opends.server.backends.jeb;
 
@@ -581,12 +581,6 @@
       }
     }
 
-    // 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/opends/src/server/org/opends/server/backends/jeb/VerifyJob.java b/opends/src/server/org/opends/server/backends/jeb/VerifyJob.java
index 6a2ff16..68c53ac 100644
--- a/opends/src/server/org/opends/server/backends/jeb/VerifyJob.java
+++ b/opends/src/server/org/opends/server/backends/jeb/VerifyJob.java
@@ -22,7 +22,7 @@
  * CDDL HEADER END
  *
  *
- *      Portions Copyright 2006 Sun Microsystems, Inc.
+ *      Portions Copyright 2006 - 2007 Sun Microsystems, Inc.
  */
 package org.opends.server.backends.jeb;
 
@@ -434,7 +434,7 @@
       DatabaseEntry key = new DatabaseEntry();
       DatabaseEntry data = new DatabaseEntry();
 
-      Long storedEntryCount = null;
+      Long storedEntryCount = id2entry.getRecordCount();
 
       OperationStatus status;
       for (status = cursor.getFirst(key, data, LockMode.DEFAULT);
@@ -455,48 +455,32 @@
           continue;
         }
 
-        if (entryID.longValue() == 0)
-        {
-          // This is the stored entry count.
-          storedEntryCount = JebFormat.entryIDFromDatabase(data.getData());
-        }
-        else
-        {
-          keyCount++;
+        keyCount++;
 
-          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 (storedEntryCount != null)
-      {
-        if (keyCount != storedEntryCount)
+        Entry entry;
+        try
         {
+          entry = JebFormat.entryFromDatabase(data.getData());
+        }
+        catch (Exception e)
+        {
+          assert debugException(CLASS_NAME, "iterateID2Entry", e);
           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("Malformed id2entry record for ID %d:%n%s%n",
+              entryID.longValue(),
+              StaticUtils.bytesToHex(data.getData()));
+          continue;
         }
+
+        verifyEntry(entryID, entry);
       }
-      else
+      if (keyCount != storedEntryCount)
       {
         errorCount++;
-        System.err.printf("Missing record count in id2entry.%n");
+        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);
       }
     }
     finally
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/backends/jeb/TestVerifyJob.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/backends/jeb/TestVerifyJob.java
index b2540c0..ca3a8d5 100644
--- a/opends/tests/unit-tests-testng/src/server/org/opends/server/backends/jeb/TestVerifyJob.java
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/backends/jeb/TestVerifyJob.java
@@ -22,7 +22,7 @@
  * CDDL HEADER END
  *
  *
- *      Portions Copyright 2006 Sun Microsystems, Inc.
+ *      Portions Copyright 2006 - 2007 Sun Microsystems, Inc.
  */
 package org.opends.server.backends.jeb;
 
@@ -389,18 +389,6 @@
     	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);
-    }
 
     /**
      * 
@@ -425,7 +413,6 @@
     	testDN=DN.decode(noParentDN);
     	id=new EntryID(12);      
     	assertTrue(dn2id.insert(txn, testDN, id));
-    	setStoredCount(12);
     	performBECompleteVerify("dn2id", 3);
     }
     
@@ -452,8 +439,7 @@
     	byte[] idBytesp=new byte[16];
     	idBytesp[7]=(byte) 0xFF;
     	EntryIDSet idSetp=new EntryIDSet(null, idBytesp);
-    	id2child.writeKey(txn, keyp, idSetp);   	
-    	setStoredCount(12);  	
+    	id2child.writeKey(txn, keyp, idSetp);
     	performBECompleteVerify("id2children", 3);
     }
     
@@ -475,7 +461,6 @@
     	EntryIDSet idSetp=new EntryIDSet();   	
     	DatabaseEntry key= new EntryID(2).getDatabaseEntry();
     	id2child.writeKey(txn, key, idSetp);
-    	setStoredCount(3);  	
     	performBECompleteVerify("id2children", 0);
     }
   
@@ -490,8 +475,7 @@
     public void testVerifyID2Subtree() throws Exception {
     	preTest(2);
     	//Add entry with no parent
-    	addID2EntryReturnKey(noParentDN, 3, false);  	
-    	setStoredCount(3);
+    	addID2EntryReturnKey(noParentDN, 3, false);
     	performBECompleteVerify("id2subtree", 3);
     }
   
@@ -513,7 +497,6 @@
     	EntryIDSet idSet=new EntryIDSet();   	
     	DatabaseEntry key= new EntryID(2).getDatabaseEntry();
     	id2subtree.writeKey(txn, key, idSet);
-    	setStoredCount(3);
     	performBECompleteVerify("id2subtree", 1);
     }
 
@@ -679,16 +662,6 @@
     }
     
     
-    /**
-     * 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