void initialize()

in activeio-core/src/main/java/org/apache/activeio/journal/active/LogFileManager.java [107:187]


    void initialize(int onlineLogFileCount) throws IOException {
      try
      {
        LogFileNode logFiles[] = new LogFileNode[onlineLogFileCount];

        // Create the log directory if it does not exist.
        if (!logDirectory.exists()) {
            if (!logDirectory.mkdirs()) {
                throw new IOException("Could not create directory: " + logDirectory);
            }
        }

        // Open the control file.        
        int controlDataSize = SERIALIZED_SIZE + (LogFileNode.SERIALIZED_SIZE*onlineLogFileCount);
        controlFile = new ControlFile(new File(logDirectory, "control.dat"),  controlDataSize);
        // Make sure we are the only process using the control file.
        controlFile.lock();
        
        // Initialize the nodes.
        for (int i = 0; i < onlineLogFileCount; i++) {
            LogFile file = new LogFile(new File(logDirectory, "log-" + onlineLogNameFormat.format(i) + ".dat"),
                    initialLogFileSize);
            logFiles[i] = new LogFileNode(file);
        }

        // Link the nodes together.
        for (int i = 0; i < onlineLogFileCount; i++) {
            if (i == (onlineLogFileCount - 1)) {
                logFiles[i].setNext(logFiles[0]);
            } else {
                logFiles[i].setNext(logFiles[i + 1]);
            }
        }
        
        firstNode = logFiles[0];
        loadState();

        // Find the first active node
        for (int i = 0; i < onlineLogFileCount; i++) {
            if( logFiles[i].isActive() ) {
                activeLogFileCount.incrementAndGet();
                if( firstActiveNode == null || logFiles[i].getId() < firstActiveNode.getId() ) {
                    firstActiveNode = logFiles[i];
                }
            }
        }
        
        // None was active? activate one.
        if ( firstActiveNode == null ) {
            firstInactiveNode = logFiles[0];
            activateNextLogFile();
        } else {            
            // Find the append log and the first inactive node
            firstInactiveNode = null;
            LogFileNode log = firstActiveNode;
            do {
                if( !log.isActive() ) {
                    firstInactiveNode = log;
                    break;
                } else {
                    appendNode = log;
                }
                log = log.getNext();
            } while (log != firstActiveNode);
        }
        
        // If we did not have a clean shut down then we have to check the state 
        // of the append log.
        if( !this.loadedFromCleanShutDown ) {
            checkAppendLog();
        }
                    
        loadedFromCleanShutDown = false;
        storeState();
      }
      catch (JournalLockedException e)
      {
        controlFile.dispose();
        throw e;
      }
    }