From 64178023aa69d2125aaf3bb838190060049d2f41 Mon Sep 17 00:00:00 2001 From: Peter Lawrey Date: Wed, 10 Dec 2014 18:09:56 +0000 Subject: [PATCH] HCOLL-228 Add support for .json.gz files. --- .../chronicle/map/VanillaChronicleMap.java | 12 ++++++-- .../chronicle/map/LargeEntriesTest.java | 29 +++++++++++++------ 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java b/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java index af5314f64..47a293e89 100755 --- a/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java @@ -52,6 +52,8 @@ import java.nio.ByteOrder; import java.nio.channels.FileChannel; import java.util.*; import java.util.concurrent.TimeUnit; +import java.util.zip.GZIPInputStream; +import java.util.zip.GZIPOutputStream; import static java.lang.Class.forName; import static java.lang.Long.numberOfTrailingZeros; @@ -612,7 +614,10 @@ class VanillaChronicleMap, // ideally we will use java serialization if we can lazyXStreamConverter().registerConverter(xstream); - try (FileOutputStream out = new FileOutputStream(toFile)) { + OutputStream outputStream = new FileOutputStream(toFile); + if (toFile.getName().toLowerCase().endsWith(".gz")) + outputStream = new GZIPOutputStream(outputStream); + try (OutputStream out = outputStream) { xstream.toXML(entrySet(), out); } } @@ -626,7 +631,10 @@ class VanillaChronicleMap, // ideally we will use java serialization if we can lazyXStreamConverter().registerConverter(xstream); - try (FileInputStream out = new FileInputStream(fromFile)) { + InputStream inputStream = new FileInputStream(fromFile); + if (fromFile.getName().toLowerCase().endsWith(".gz")) + inputStream = new GZIPInputStream(inputStream); + try (InputStream out = inputStream) { xstream.fromXML(out); } } diff --git a/src/test/java/net/openhft/chronicle/map/LargeEntriesTest.java b/src/test/java/net/openhft/chronicle/map/LargeEntriesTest.java index 12b73c8c2..e84939acf 100644 --- a/src/test/java/net/openhft/chronicle/map/LargeEntriesTest.java +++ b/src/test/java/net/openhft/chronicle/map/LargeEntriesTest.java @@ -32,16 +32,16 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.*; /** * Created by peter on 06/12/14. */ public class LargeEntriesTest { - static final int ENTRIES = 640; + static final int ENTRIES = 6400; static final int ENTRY_SIZE = 1024; + private static final boolean TESTING = Boolean.getBoolean("testing"); @Test @Ignore @@ -55,7 +55,7 @@ public class LargeEntriesTest { .putReturnsNull(true) .create(); { - int threads = 2; //Runtime.getRuntime().availableProcessors(); + int threads = 4; //Runtime.getRuntime().availableProcessors(); ExecutorService es = Executors.newFixedThreadPool(threads); final int block = ENTRIES / threads; for (int i = 0; i < 3; i++) { @@ -124,11 +124,20 @@ public class LargeEntriesTest { SnappyStringMarshaller.INSTANCE.read(bytes); bytes.release(); } + Object lock = TESTING ? Thread.currentThread() : map; + List keys = new ArrayList<>(); for (int i = start; i < finish; i++) { String key = "key-" + i; - map.put(key, value); - String object = map.get(key); + String object; + synchronized (lock) { + map.put(key, value); + keys.add(key); + for (String s : keys) + assertTrue(s, map.containsKey(s)); + object = map.get(key); + assertTrue(key, map.containsKey(key)); + } assertNotNull(key, object); assertEquals(key, entrySize, object.length()); @@ -138,10 +147,12 @@ public class LargeEntriesTest { for (int i = start; i < finish; i++) { // System.out.println(i); String key = "key-" + i; - String object = map.get(key); + synchronized (lock) { + String object = map.get(key); - assertNotNull(key, object); - assertEquals(key, entrySize, object.length()); + assertNotNull(key, object); + assertEquals(key, entrySize, object.length()); + } } } }