use of com.alipay.sofa.jraft.rhea.storage.MemoryKVStoreSnapshotFile.SequenceDB in project sofa-jraft by sofastack.
the class MemoryRawKVStore method doSnapshotLoad.
void doSnapshotLoad(final MemoryKVStoreSnapshotFile snapshotFile, final String snapshotPath) throws Exception {
final Timer.Context timeCtx = getTimeContext("SNAPSHOT_LOAD");
try {
final SequenceDB sequenceDB = snapshotFile.readFromFile(snapshotPath, SEQUENCE_DB, SequenceDB.class);
final FencingKeyDB fencingKeyDB = snapshotFile.readFromFile(snapshotPath, FENCING_KEY_DB, FencingKeyDB.class);
final LockerDB lockerDB = snapshotFile.readFromFile(snapshotPath, LOCKER_DB, LockerDB.class);
this.sequenceDB.putAll(sequenceDB.data());
this.fencingKeyDB.putAll(fencingKeyDB.data());
this.lockerDB.putAll(lockerDB.data());
final TailIndex tailIndex = snapshotFile.readFromFile(snapshotPath, TAIL_INDEX, TailIndex.class);
final int tail = tailIndex.data();
final List<Segment> segments = Lists.newArrayListWithCapacity(tail + 1);
for (int i = 0; i <= tail; i++) {
final Segment segment = snapshotFile.readFromFile(snapshotPath, SEGMENT + i, Segment.class);
segments.add(segment);
}
for (final Segment segment : segments) {
for (final Pair<byte[], byte[]> p : segment.data()) {
this.defaultDB.put(p.getKey(), p.getValue());
}
}
} finally {
timeCtx.stop();
}
}
use of com.alipay.sofa.jraft.rhea.storage.MemoryKVStoreSnapshotFile.SequenceDB in project sofa-jraft by sofastack.
the class MemoryRawKVStore method doSnapshotSave.
void doSnapshotSave(final MemoryKVStoreSnapshotFile snapshotFile, final String snapshotPath, final Region region) throws Exception {
final Timer.Context timeCtx = getTimeContext("SNAPSHOT_SAVE");
try {
final String tempPath = snapshotPath + "_temp";
final File tempFile = new File(tempPath);
FileUtils.deleteDirectory(tempFile);
FileUtils.forceMkdir(tempFile);
snapshotFile.writeToFile(tempPath, SEQUENCE_DB, new SequenceDB(subRangeMap(this.sequenceDB, region)));
snapshotFile.writeToFile(tempPath, FENCING_KEY_DB, new FencingKeyDB(subRangeMap(this.fencingKeyDB, region)));
snapshotFile.writeToFile(tempPath, LOCKER_DB, new LockerDB(subRangeMap(this.lockerDB, region)));
final int size = this.opts.getKeysPerSegment();
final List<Pair<byte[], byte[]>> segment = Lists.newArrayListWithCapacity(size);
int index = 0;
final byte[] realStartKey = BytesUtil.nullToEmpty(region.getStartKey());
final byte[] endKey = region.getEndKey();
final NavigableMap<byte[], byte[]> subMap;
if (endKey == null) {
subMap = this.defaultDB.tailMap(realStartKey);
} else {
subMap = this.defaultDB.subMap(realStartKey, endKey);
}
for (final Map.Entry<byte[], byte[]> entry : subMap.entrySet()) {
segment.add(Pair.of(entry.getKey(), entry.getValue()));
if (segment.size() >= size) {
snapshotFile.writeToFile(tempPath, SEGMENT + index++, new Segment(segment));
segment.clear();
}
}
if (!segment.isEmpty()) {
snapshotFile.writeToFile(tempPath, SEGMENT + index++, new Segment(segment));
segment.clear();
}
snapshotFile.writeToFile(tempPath, TAIL_INDEX, new TailIndex(--index));
final File destinationPath = new File(snapshotPath);
FileUtils.deleteDirectory(destinationPath);
FileUtils.moveDirectory(tempFile, destinationPath);
} finally {
timeCtx.stop();
}
}
Aggregations