Simplify parameter flow on VanillaChronicleHash initialization

This commit is contained in:
leventov
2016-02-09 15:03:34 +07:00
parent e2ec9db371
commit d7ed5b0153
3 changed files with 21 additions and 27 deletions
@@ -382,16 +382,19 @@ public abstract class VanillaChronicleHash<K,
}
}
public final void initBeforeMapping(File file, FileChannel ch, long headerEnd, boolean recover)
throws IOException {
public final void initBeforeMapping(
File file, RandomAccessFile raf, long headerEnd, boolean recover) throws IOException {
this.file = file;
this.raf = raf;
this.headerSize = roundUpMapHeaderSize(headerEnd);
if (!createdOrInMemory) {
// This block is for reading segmentHeadersOffset before main mapping
// After the mapping globalMutableState value's bytes are reassigned
ByteBuffer globalMutableStateBuffer =
ByteBuffer.allocate((int) globalMutableState.maxSize());
FileChannel fileChannel = raf.getChannel();
while (globalMutableStateBuffer.remaining() > 0) {
if (ch.read(globalMutableStateBuffer,
if (fileChannel.read(globalMutableStateBuffer,
this.headerSize + GLOBAL_MUTABLE_STATE_VALUE_OFFSET +
globalMutableStateBuffer.position()) == -1) {
throw throwRecoveryOrReturnIOException(file + " truncated", recover);
@@ -456,11 +459,6 @@ public abstract class VanillaChronicleHash<K,
onHeaderCreated();
}
private void setFile(File file, RandomAccessFile raf) {
this.file = file;
this.raf = raf;
}
public void registerRafReleaser() {
this.rafCleaner = Cleaner.create(this, new RafReleaser(file));
}
@@ -482,17 +480,14 @@ public abstract class VanillaChronicleHash<K,
}
}
public final void createMappedStoreAndSegments(File file, RandomAccessFile raf)
throws IOException {
public final void createMappedStoreAndSegments() throws IOException {
// TODO this method had been moved -- not clear where
//OS.warnOnWindows(sizeInBytesWithoutTiers());
setFile(file, raf);
long mapSize = expectedFileSize();
createMappedStoreAndSegments(map(mapSize, 0));
}
public final void basicRecover(File file, RandomAccessFile raf) throws IOException {
setFile(file, raf);
public final void basicRecover() throws IOException {
long segmentHeadersOffset = computeSegmentHeadersOffset();
long sizeInBytesWithoutTiers = computeSizeInBytesWithoutTiers(segmentHeadersOffset);
long sizeBeyondSegments = Math.max(raf.length() - sizeInBytesWithoutTiers, 0);
@@ -501,13 +496,13 @@ public abstract class VanillaChronicleHash<K,
allocatedExtraTierBulks * tierBulkSizeInBytes);
initBytesStoreAndHeadersViews(map(mapSize, 0));
resetGlobalMutableStateLock(file);
recoverAllocatedExtraTierBulks(file, allocatedExtraTierBulks);
recoverSegmentHeadersOffset(file, segmentHeadersOffset);
resetGlobalMutableStateLock();
recoverAllocatedExtraTierBulks(allocatedExtraTierBulks);
recoverSegmentHeadersOffset(segmentHeadersOffset);
initOffsetsAndBulks();
}
private void resetGlobalMutableStateLock(File file) {
private void resetGlobalMutableStateLock() {
long lockAddr = globalMutableStateAddress() + GLOBAL_MUTABLE_STATE_LOCK_OFFSET;
LockingStrategy lockingStrategy = globalMutableStateLockingStrategy;
long lockState = lockingStrategy.getState(nativeAccess(), null, lockAddr);
@@ -518,7 +513,7 @@ public abstract class VanillaChronicleHash<K,
}
}
private void recoverAllocatedExtraTierBulks(File file, int allocatedExtraTierBulks) {
private void recoverAllocatedExtraTierBulks(int allocatedExtraTierBulks) {
if (globalMutableState.getAllocatedExtraTierBulks() != allocatedExtraTierBulks) {
LOG.error("allocated extra tier bulks counter corrupted, or the map file {} " +
"is truncated. stored: {}, should be: {}", file,
@@ -527,7 +522,7 @@ public abstract class VanillaChronicleHash<K,
}
}
private void recoverSegmentHeadersOffset(File file, long segmentHeadersOffset) {
private void recoverSegmentHeadersOffset(long segmentHeadersOffset) {
if (globalMutableState.getSegmentHeadersOffset() != segmentHeadersOffset) {
LOG.error("segment headers offset of map at {} corrupted. stored: {}, should be: {}",
file, globalMutableState.getSegmentHeadersOffset(), segmentHeadersOffset);
@@ -1564,9 +1564,8 @@ public final class ChronicleMapBuilder<K, V> implements
private VanillaChronicleMap<K, V, ?> createWithNewFile(
VanillaChronicleMap<K, V, ?> map, File file, RandomAccessFile raf,
ByteBuffer headerBuffer, int headerSize) throws IOException {
FileChannel fileChannel = raf.getChannel();
map.initBeforeMapping(file, fileChannel, headerBuffer.limit(), false);
map.createMappedStoreAndSegments(file, raf);
map.initBeforeMapping(file, raf, headerBuffer.limit(), false);
map.createMappedStoreAndSegments();
commitChronicleMapReady(map, raf, headerBuffer, headerSize);
establishReplication(map);
return map;
@@ -1593,7 +1592,7 @@ public final class ChronicleMapBuilder<K, V> implements
Wire wire = new TextWire(headerBytes);
VanillaChronicleMap<K, V, ?> map = wire.getValueIn().typedMarshallable();
assert map != null;
map.initBeforeMapping(file, fileChannel, headerBuffer.limit(), recover);
map.initBeforeMapping(file, raf, headerBuffer.limit(), recover);
long expectedFileLength = map.expectedFileSize();
if (!recover && expectedFileLength != file.length()) {
throw new IOException("The file " + file + " the map is serialized from " +
@@ -1602,13 +1601,13 @@ public final class ChronicleMapBuilder<K, V> implements
}
map.initTransientsFromBuilder(this);
if (!recover) {
map.createMappedStoreAndSegments(file, raf);
map.createMappedStoreAndSegments();
} else {
if (!overrideBuilderConfig)
writeNotReady(fileChannel, headerBuffer, headerSize);
// if overrideBuilderConfig = true, readiness bit is already set
// in writeHeader() call
map.recover(file, raf);
map.recover();
commitChronicleMapReady(map, raf, headerBuffer, headerSize);
}
establishReplication(map);
@@ -147,8 +147,8 @@ public class VanillaChronicleMap<K, V, R>
initOwnTransients();
}
public void recover(File file, RandomAccessFile raf) throws IOException {
basicRecover(file, raf);
public void recover() throws IOException {
basicRecover();
iterationContext().recoverSegments();
}