Example usage for org.apache.zookeeper.server DataTree deserialize

List of usage examples for org.apache.zookeeper.server DataTree deserialize

Introduction

In this page you can find the example usage for org.apache.zookeeper.server DataTree deserialize.

Prototype

public void deserialize(InputArchive ia, String tag) throws IOException 

Source Link

Usage

From source file:com.zklogtool.reader.SnapshotFileReader.java

License:Apache License

/**
 * Reads snapshot file and returns <code>DataState</code> based on it.
 *
 * @return Returns <code>DataState</code> based only on snapshot file.
 * @throws CRCValidationException Thrown if CRC validation failed. Snapshot
 * is probably corrupted.// w ww  . j  a  v  a  2s.c  om
 * @throws IOException Thrown if there is a problem with reading snapshot
 * file.
 */
public DataState readFuzzySnapshot() throws CRCValidationException, IOException {

    DataTree dt = new DataTree();
    Map<Long, Integer> sessions = new HashMap<Long, Integer>();

    InputStream snapIS = null;
    CheckedInputStream crcIn = null;

    try {
        logger.info("Reading snapshot " + snapshotFile);
        snapIS = new BufferedInputStream(new FileInputStream(snapshotFile));
        crcIn = new CheckedInputStream(snapIS, new Adler32());
        InputArchive ia = getArchive(crcIn);

        FileHeader header = new FileHeader();
        header.deserialize(ia, "fileheader");
        if (header.getMagic() != SNAP_MAGIC) {
            throw new IOException("Mismatching magic headers " + header.getMagic() + " !=  " + SNAP_MAGIC);
        }

        int count = ia.readInt("count");

        while (count > 0) {
            long id = ia.readLong("id");
            int to = ia.readInt("timeout");
            sessions.put(id, to);
            count--;
        }

        dt.deserialize(ia, "tree");

        long checkSum = crcIn.getChecksum().getValue();
        long val = ia.readLong("val");
        if (val != checkSum) {
            throw new CRCValidationException("CRC corruption in snapshot");
        }

    } catch (IOException e) {
        logger.warn("Problem reading snap file " + snapshotFile, e);

        throw new IOException(e);

    } finally {
        if (snapIS != null) {
            snapIS.close();
        }
        if (crcIn != null) {
            crcIn.close();
        }
    }

    long lastZxid = -1;

    DataState dataState = new DataState(dt, sessions, lastZxid);

    Iterator<Entry<String, DataNode>> it = dataState.getNodes().entrySet().iterator();
    while (it.hasNext()) {

        Entry<String, DataNode> e = it.next();

        long tempZxid = e.getValue().stat.getCzxid();

        if (tempZxid > lastZxid) {
            lastZxid = tempZxid;
        }

        tempZxid = e.getValue().stat.getPzxid();
        if (tempZxid > lastZxid) {
            lastZxid = tempZxid;
        }

        tempZxid = e.getValue().stat.getMzxid();
        if (tempZxid > lastZxid) {
            lastZxid = tempZxid;
        }

    }

    dataState.setLastZxid(lastZxid);

    return dataState;

}