/*
 * Copyright (C) 2014  Ohm Data
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package c5db.replication;

import c5db.ConfigDirectory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * A small persister that writes files to a directory.
*/
class Persister implements ReplicatorInfoPersistence {

    private ConfigDirectory configDirectory;

    Persister(ConfigDirectory configDirectory) {
        this.configDirectory = configDirectory;
    }

    @Override
    public long readCurrentTerm(String quorumId) throws IOException {
        return getLongofFile(quorumId, 0);
    }

    @Override
    public long readVotedFor(String quorumId) throws IOException {
        return getLongofFile(quorumId, 1);
    }

    private long getLongofFile(String quorumId, int whichLine) throws IOException {
        List<String> datas = configDirectory.readFile(quorumId, "replication-data");
        if (datas.size() != 2)
            return 0; // corrupt file?

        try {
            return Long.parseLong(datas.get(whichLine));
        } catch (NumberFormatException e) {
            return 0; // corrupt file sucks?
        }
    }

    @Override
    public void writeCurrentTermAndVotedFor(String quorumId, long currentTerm, long votedFor) throws IOException {
        List<String> datas = new ArrayList<>(2);
        datas.add(Long.toString(currentTerm));
        datas.add(Long.toString(votedFor));
        configDirectory.writeFile(quorumId, "replication-data", datas);
    }
}

@@ -979,16 +979,22 @@
 private 
+final 
 ConfigDi
@@ -1018,16 +1018,16 @@
 ectory;%0A
-
 %0A    Per
@@ -1530,36 +1530,135 @@
 ile(
-quorumId, %22replication-data%22
+configDirectory.getQuorumRelPath(quorumId),%0A                                                      ConfigDirectory.persisterFile
 );%0A 
@@ -2207,24 +2207,57 @@
 iteFile(
+configDirectory.getQuorumRelPath(
 quorumId
 , %22repli
@@ -2252,28 +2252,40 @@
 umId
+)
 , 
-%22replication-data%22
+ConfigDirectory.persisterFile
 , da

Move replicator's quorum-specific data to a subdirectory, and add peer list and region info files (issue #49)