实现leveldb-api的snapshot接口:
public class SnapshotImpl implements Snapshot
在leveldb快照中每次都是用一个序列号保存当前插入的这一条记录,因此当插入多条相同的记录时,通过序列号来确定那一条是最新的记录,在leveldb的快照中,在调用一个快照时,只要获取在当前快照序列号以下的记录,就可以读取到这个快照之前的数据。
private final AtomicBoolean closed = new AtomicBoolean(); private final Version version; private final long lastSequence;
SnapshotImpl(Version version, long lastSequence) { this.version = version; this.lastSequence = lastSequence; this.version.retain();
}
获取version和lastSequence
public long getLastSequence() { return lastSequence; } public Version getVersion() { return version; }
把lastSequence转化为String
@Override public String toString() { return Long.toString(lastSequence); }
判断snapshot和version是否一致:
public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } SnapshotImpl snapshot = (SnapshotImpl) o; if (lastSequence != snapshot.lastSequence) { return false; } if (!version.equals(snapshot.version)) { return false; } return true; }
转换impl为hash编码:
@Override public int hashCode() { int result = version.hashCode(); result = 31 * result + (int) (lastSequence ^ (lastSequence >>> 32)); return result; } }
ReadOption中保持了snapshot,在拿到快照后,取出快照中的sequence number,根据传入的key和sequence number进文件中查找记录,这样就能查找快照之前的数据。
参考:level Snapshot源码分析_叶长风的博客-CSDN博客
原创文章,作者:kirin,如若转载,请注明出处:https://blog.ytso.com/275595.html