From b5cf817b2af2e0ce075043c9dae54fb2a319426e Mon Sep 17 00:00:00 2001 From: leventov Date: Thu, 6 Aug 2015 03:46:54 +0300 Subject: [PATCH] Zero out contents of a newly-created persisted ChronicleHash --- .../chronicle/hash/impl/VanillaChronicleHash.java | 14 +++++++++++++- .../openhft/chronicle/map/ChronicleMapBuilder.java | 11 +++++++++-- .../chronicle/map/ReplicatedChronicleMap.java | 4 ++-- ...eplicateProxyMapUpdatedViaSharedMemoryTest.java | 3 +++ 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/openhft/chronicle/hash/impl/VanillaChronicleHash.java b/src/main/java/net/openhft/chronicle/hash/impl/VanillaChronicleHash.java index ed9b58e00..f3dac08ed 100644 --- a/src/main/java/net/openhft/chronicle/hash/impl/VanillaChronicleHash.java +++ b/src/main/java/net/openhft/chronicle/hash/impl/VanillaChronicleHash.java @@ -56,6 +56,10 @@ public abstract class VanillaChronicleHash kClass; @@ -110,10 +114,15 @@ public abstract class VanillaChronicleHash builder, boolean replicated) { + public VanillaChronicleHash( + ChronicleMapBuilder builder, boolean replicated) { // Version dataFileVersion = BuildVersion.version(); + // Because we are in constructor. If the Hash is not created, deserialization bypasses + // the constructor and created = false + this.created = true; + @SuppressWarnings("deprecation") ChronicleHashBuilderPrivateAPI privateAPI = builder.privateAPI(); @@ -205,6 +214,9 @@ public abstract class VanillaChronicleHash implements File file, SingleChronicleHashReplication singleHashReplication, ReplicationChannel channel) throws IOException { for (int i = 0; i < 10; i++) { - if (file.exists() && file.length() > 0) { + long fileLength = file.length(); + if (fileLength > 0) { try (FileInputStream fis = new FileInputStream(file); ObjectInputStream ois = new ObjectInputStream(fis)) { Object m; @@ -1379,6 +1380,12 @@ public final class ChronicleMapBuilder implements (VanillaChronicleMap) m; map.initTransientsFromBuilder(this); map.headerSize = roundUpMapHeaderSize(fis.getChannel().position()); + long sizeInBytes = map.sizeInBytes(); + if (sizeInBytes != fileLength) { + throw new IOException("The file " + file + "the map is serialized from " + + "has unexpected length " + fileLength + ", probably corrupted. " + + "Expected length is " + sizeInBytes); + } map.createMappedStoreAndSegments(file); // This is needed to property initialize key and value serialization builders, // which are later used in replication @@ -1388,7 +1395,7 @@ public final class ChronicleMapBuilder implements return establishReplication(map, singleHashReplication, channel); } } - if (file.createNewFile() || file.length() == 0) { + if (file.createNewFile() || fileLength == 0) { break; } try { diff --git a/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java b/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java index 772d71b8f..699333dc5 100644 --- a/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java @@ -208,10 +208,10 @@ public class ReplicatedChronicleMap