is = new BufferedInputStream(new FileInputStream(file));
InputArchive ia = BinaryInputArchive.getArchive(is);
FileHeader hdr = new FileHeader();
hdr.deserialize(ia, "fileheader"); // 反序列化
return hdr;
} finally {
// is.close();
}
}
复制代码
FileTxnIterator类
this class implements the txnlog iterator interface which is used for reading the transaction logs.
内部使用List保存着比指定zxid大或者含有指定zxid数据的log文件,初始化阶段会定位到参数zxid指定的位置,这样在后续访问时就可以从参数指定的zxid开始读取数据了。
public FileTxnIterator(File logDir, long zxid, boolean fastForward) throws IOException {
this.logDir = logDir;
this.zxid = zxid;
init();
if (fastForward && hdr != null) {
while (hdr.getZxid() < zxid) { // 这里将数据移动到zxid位置
if (!next()) {
break;
}
}
}
}
void init() throws IOException {
storedFiles = new ArrayList<>();
// 倒序查找log文件
List<File> files = Util.sortDataDir(
FileTxnLog.getLogFiles(logDir.listFiles(), 0),
LOG_FILE_PREFIX,
false);
for (File f : files) {
if (Util.getZxidFromName(f.getName(), LOG_FILE_PREFIX) >= zxid) {
storedFiles.add(f);
} else if (Util.getZxidFromName(f.getName(), LOG_FILE_PREFIX) < zxid) {
// add the last logfile that is less than the zxid
storedFiles.add(f);
break;
}
}
goToNextLog(); // 定位到下一个文件
next(); // 定位到下一个log数据
}
复制代码
SnapShot接口和FileSnap实现类
SnapShot接口
snapshot interface for the persistence layer. implement this interface for implementing snapshots.
public interface SnapShot {
// deserialize a data tree from the last valid snapshot and return the last zxid that was deserialized
long deserialize(DataTree dt, Map<Long, Integer> sessions) throws IOException;
// persist the datatree and the sessions into a persistence storage