fixed more recoveryMgr's bugs

git-svn-id: https://asterixdb.googlecode.com/svn/branches/asterix_lsm_stabilization@1413 eaa15691-b419-025a-1212-ee371bd00084
diff --git a/asterix-app/src/main/java/edu/uci/ics/asterix/hyracks/bootstrap/NCApplicationEntryPoint.java b/asterix-app/src/main/java/edu/uci/ics/asterix/hyracks/bootstrap/NCApplicationEntryPoint.java
index 7e910be..9468a68 100644
--- a/asterix-app/src/main/java/edu/uci/ics/asterix/hyracks/bootstrap/NCApplicationEntryPoint.java
+++ b/asterix-app/src/main/java/edu/uci/ics/asterix/hyracks/bootstrap/NCApplicationEntryPoint.java
@@ -16,7 +16,6 @@
 import edu.uci.ics.asterix.transaction.management.service.recovery.IRecoveryManager.SystemState;
 import edu.uci.ics.hyracks.api.application.INCApplicationContext;
 import edu.uci.ics.hyracks.api.application.INCApplicationEntryPoint;
-import edu.uci.ics.hyracks.storage.common.file.ILocalResourceRepository;
 
 public class NCApplicationEntryPoint implements INCApplicationEntryPoint {
     private static final Logger LOGGER = Logger.getLogger(NCApplicationEntryPoint.class.getName());
diff --git a/asterix-metadata/src/main/java/edu/uci/ics/asterix/metadata/bootstrap/MetadataBootstrap.java b/asterix-metadata/src/main/java/edu/uci/ics/asterix/metadata/bootstrap/MetadataBootstrap.java
index 55c3341..805a550 100644
--- a/asterix-metadata/src/main/java/edu/uci/ics/asterix/metadata/bootstrap/MetadataBootstrap.java
+++ b/asterix-metadata/src/main/java/edu/uci/ics/asterix/metadata/bootstrap/MetadataBootstrap.java
@@ -164,8 +164,6 @@
         ioManager = ncApplicationContext.getRootContext().getIOManager();
 
         if (isNewUniverse) {
-            //Do checkpoint only if it is new universe
-            //runtimeContext.getTransactionSubsystem().getRecoveryManager().checkpoint(false);
             MetadataTransactionContext mdTxnCtx = MetadataManager.INSTANCE.beginTransaction();
             try {
                 // Begin a transaction against the metadata.
diff --git a/asterix-transactions/src/main/java/edu/uci/ics/asterix/transaction/management/service/logging/IndexResourceManager.java b/asterix-transactions/src/main/java/edu/uci/ics/asterix/transaction/management/service/logging/IndexResourceManager.java
index 7e4b5bb..7ecfa69 100644
--- a/asterix-transactions/src/main/java/edu/uci/ics/asterix/transaction/management/service/logging/IndexResourceManager.java
+++ b/asterix-transactions/src/main/java/edu/uci/ics/asterix/transaction/management/service/logging/IndexResourceManager.java
@@ -50,15 +50,15 @@
                 .getIndex(resourceId);
 
         /* field count */
-        int fieldCount = logLocator.getBuffer().readInt(logLocator.getMemoryOffset() + offset);
+        int fieldCount = logLocator.getBuffer().readInt(offset);
         offset += 4;
 
         /* new operation */
-        byte newOperation = logLocator.getBuffer().getByte(logLocator.getMemoryOffset() + offset);
+        byte newOperation = logLocator.getBuffer().getByte(offset);
         offset += 1;
 
         /* new value size */
-        int newValueSize = logLocator.getBuffer().readInt(logLocator.getMemoryOffset() + offset);
+        int newValueSize = logLocator.getBuffer().readInt(offset);
         offset += 4;
 
         /* new value */
@@ -75,12 +75,12 @@
             if (resourceType == ResourceType.LSM_BTREE) {
 
                 /* old operation */
-                byte oldOperation = logLocator.getBuffer().getByte(logLocator.getMemoryOffset() + offset);
+                byte oldOperation = logLocator.getBuffer().getByte(offset);
                 offset += 1;
 
                 if (oldOperation != (byte) IndexOperation.NOOP.ordinal()) {
                     /* old value size */
-                    int oldValueSize = logLocator.getBuffer().readInt(logLocator.getMemoryOffset() + offset);
+                    int oldValueSize = logLocator.getBuffer().readInt(offset);
                     offset += 4;
 
                     /* old value */
@@ -117,15 +117,15 @@
                 .getIndex(resourceId);
 
         /* field count */
-        int fieldCount = logLocator.getBuffer().readInt(logLocator.getMemoryOffset() + offset);
+        int fieldCount = logLocator.getBuffer().readInt(offset);
         offset += 4;
 
         /* new operation */
-        byte newOperation = logLocator.getBuffer().getByte(logLocator.getMemoryOffset() + offset);
+        byte newOperation = logLocator.getBuffer().getByte(offset);
         offset += 1;
 
         /* new value size */
-        int newValueSize = logLocator.getBuffer().readInt(logLocator.getMemoryOffset() + offset);
+        int newValueSize = logLocator.getBuffer().readInt(offset);
         offset += 4;
 
         /* new value */
diff --git a/asterix-transactions/src/main/java/edu/uci/ics/asterix/transaction/management/service/recovery/RecoveryManager.java b/asterix-transactions/src/main/java/edu/uci/ics/asterix/transaction/management/service/recovery/RecoveryManager.java
index 77f9da5..ed88112 100644
--- a/asterix-transactions/src/main/java/edu/uci/ics/asterix/transaction/management/service/recovery/RecoveryManager.java
+++ b/asterix-transactions/src/main/java/edu/uci/ics/asterix/transaction/management/service/recovery/RecoveryManager.java
@@ -50,7 +50,6 @@
 import edu.uci.ics.asterix.transaction.management.service.logging.PhysicalLogLocator;
 import edu.uci.ics.asterix.transaction.management.service.transaction.IResourceManager;
 import edu.uci.ics.asterix.transaction.management.service.transaction.IResourceManager.ResourceType;
-import edu.uci.ics.asterix.transaction.management.service.transaction.JobIdFactory;
 import edu.uci.ics.asterix.transaction.management.service.transaction.TransactionContext;
 import edu.uci.ics.asterix.transaction.management.service.transaction.TransactionManagementConstants;
 import edu.uci.ics.asterix.transaction.management.service.transaction.TransactionManager;
@@ -136,6 +135,10 @@
 
     public void startRecovery(boolean synchronous) throws IOException, ACIDException {
 
+        int updateLogCount = 0;
+        int commitLogCount = 0;
+        int redoCount = 0;
+
         state = SystemState.RECOVERING;
 
         ILogManager logManager = txnSubsystem.getLogManager();
@@ -144,7 +147,7 @@
         if (LOGGER.isLoggable(Level.INFO)) {
             LOGGER.info("[RecoveryMgr] starting recovery ...");
         }
-        
+
         //winnerTxnTable is used to add pairs, <committed TxnId, the most recent commit LSN of the TxnId>
         Map<TxnId, Long> winnerTxnTable = new HashMap<TxnId, Long>();
         TxnId tempKeyTxnId = new TxnId(-1, -1, -1);
@@ -154,7 +157,7 @@
         CheckpointObject checkpointObject = readCheckpoint();
         long lowWaterMarkLSN = checkpointObject.getMinMCTFirstLSN();
         if (lowWaterMarkLSN == -1) {
-        	lowWaterMarkLSN = 0;
+            lowWaterMarkLSN = 0;
         }
         int maxJobId = checkpointObject.getMaxJobId();
         int currentJobId;
@@ -168,7 +171,7 @@
         if (LOGGER.isLoggable(Level.INFO)) {
             LOGGER.info("[RecoveryMgr] in analysis phase");
         }
-        
+
         //#. set log cursor to the lowWaterMarkLSN
         ILogCursor logCursor = logManager.readLog(new PhysicalLogLocator(lowWaterMarkLSN, logManager),
                 new ILogFilter() {
@@ -193,17 +196,23 @@
                 maxJobId = currentJobId;
             }
 
+            TxnId commitTxnId = null;
             switch (logType) {
                 case LogType.UPDATE:
-                    //do nothing                    
+                    if (IS_DEBUG_MODE) {
+                        updateLogCount++;
+                    }
                     break;
 
                 case LogType.COMMIT:
                 case LogType.ENTITY_COMMIT:
-                    tempKeyTxnId.setTxnId(logRecordHelper.getJobId(currentLogLocator),
+                    commitTxnId = new TxnId(logRecordHelper.getJobId(currentLogLocator),
                             logRecordHelper.getDatasetId(currentLogLocator),
                             logRecordHelper.getPKHashValue(currentLogLocator));
-                    winnerTxnTable.put(tempKeyTxnId, currentLogLocator.getLsn());
+                    winnerTxnTable.put(commitTxnId, currentLogLocator.getLsn());
+                    if (IS_DEBUG_MODE) {
+                        commitLogCount++;
+                    }
                     break;
 
                 default:
@@ -350,6 +359,9 @@
 
                             //redo finally.
                             resourceMgr.redo(logRecordHelper, currentLogLocator);
+                            if (IS_DEBUG_MODE) {
+                                redoCount++;
+                            }
                         }
                     }
                     break;
@@ -369,10 +381,14 @@
         for (long r : resourceIdList) {
             indexLifecycleManager.close(r);
         }
-        
+
         if (LOGGER.isLoggable(Level.INFO)) {
             LOGGER.info("[RecoveryMgr] recovery is over");
         }
+        if (IS_DEBUG_MODE) {
+            System.out.println("[RecoveryMgr] Count: Update/Commit/Redo = " + updateLogCount + "/" + commitLogCount
+                    + "/" + redoCount);
+        }
     }
 
     //TODO