diff --git a/pom.xml b/pom.xml index 6c17abe1d..906178988 100644 --- a/pom.xml +++ b/pom.xml @@ -28,7 +28,7 @@ 4.0.0 chronicle-map - 2.0.17b-SNAPSHOT + 2.0.21b-SNAPSHOT OpenHFT/Chronicle-Map Chronicle-Map diff --git a/src/main/java/net/openhft/chronicle/map/ChroncileMapJsonSerializer.java b/src/main/java/net/openhft/chronicle/map/ChroncileMapJsonSerializer.java new file mode 100644 index 000000000..04cd4e486 --- /dev/null +++ b/src/main/java/net/openhft/chronicle/map/ChroncileMapJsonSerializer.java @@ -0,0 +1,68 @@ +/* + * Copyright 2014 Higher Frequency Trading + * + * http://www.higherfrequencytrading.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package net.openhft.chronicle.map; + +import com.thoughtworks.xstream.XStream; +import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver; +import net.openhft.xstreem.convertors.ByteBufferConverter; +import net.openhft.xstreem.convertors.ChronicleMapConverter; +import net.openhft.xstreem.convertors.DataValueConverter; + +import java.io.*; +import java.util.Map; +import java.util.zip.GZIPInputStream; +import java.util.zip.GZIPOutputStream; + +/** + * @author Rob Austin. + */ +class JsonSerializer { + + static synchronized void getAll(File toFile, Map map) throws IOException { + final XStream xstream = new XStream(new JettisonMappedXmlDriver()); + xstream.setMode(XStream.NO_REFERENCES); + xstream.alias("cmap", VanillaChronicleMap.EntrySet.class); + + xstream.registerConverter(new ChronicleMapConverter(map)); + xstream.registerConverter(new ByteBufferConverter()); + xstream.registerConverter(new DataValueConverter()); + OutputStream outputStream = new FileOutputStream(toFile); + if (toFile.getName().toLowerCase().endsWith(".gz")) + outputStream = new GZIPOutputStream(outputStream); + try (OutputStream out = outputStream) { + xstream.toXML(map.entrySet(), out); + } + } + + + static synchronized void putAll(File fromFile, Map map) throws IOException { + final XStream xstream = new XStream(new JettisonMappedXmlDriver()); + xstream.setMode(XStream.NO_REFERENCES); + xstream.alias("cmap", VanillaChronicleMap.EntrySet.class); + xstream.registerConverter(new ChronicleMapConverter(map)); + xstream.registerConverter(new ByteBufferConverter()); + xstream.registerConverter(new DataValueConverter()); + + 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/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java b/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java index 8b7bd8f48..c63512eca 100755 --- a/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java @@ -18,8 +18,6 @@ package net.openhft.chronicle.map; -import com.thoughtworks.xstream.XStream; -import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver; import net.openhft.chronicle.hash.ChronicleHashErrorListener; import net.openhft.chronicle.hash.hashing.Hasher; import net.openhft.chronicle.hash.serialization.BytesInterop; @@ -36,22 +34,20 @@ import net.openhft.lang.model.DataValueClasses; import net.openhft.lang.threadlocal.Provider; import net.openhft.lang.threadlocal.StatefulCopyable; import net.openhft.lang.threadlocal.ThreadLocalCopies; -import net.openhft.xstreem.convertors.ByteBufferConverter; -import net.openhft.xstreem.convertors.ChronicleMapConverter; -import net.openhft.xstreem.convertors.DataValueConverter; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.*; +import java.io.File; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.Serializable; import java.lang.reflect.Array; import java.nio.channels.FileChannel; import java.util.*; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.ReentrantReadWriteLock; -import java.util.zip.GZIPInputStream; -import java.util.zip.GZIPOutputStream; import static java.lang.Long.numberOfTrailingZeros; import static java.lang.Math.max; @@ -667,36 +663,12 @@ class VanillaChronicleMap, @Override public synchronized void getAll(File toFile) throws IOException { - final XStream xstream = new XStream(new JettisonMappedXmlDriver()); - xstream.setMode(XStream.NO_REFERENCES); - xstream.alias("cmap", VanillaChronicleMap.EntrySet.class); - - xstream.registerConverter(new ChronicleMapConverter(this)); - xstream.registerConverter(new ByteBufferConverter()); - xstream.registerConverter(new DataValueConverter()); - OutputStream outputStream = new FileOutputStream(toFile); - if (toFile.getName().toLowerCase().endsWith(".gz")) - outputStream = new GZIPOutputStream(outputStream); - try (OutputStream out = outputStream) { - xstream.toXML(entrySet(), out); - } + JsonSerializer.getAll(toFile, this); } @Override public synchronized void putAll(File fromFile) throws IOException { - final XStream xstream = new XStream(new JettisonMappedXmlDriver()); - xstream.setMode(XStream.NO_REFERENCES); - xstream.alias("cmap", VanillaChronicleMap.EntrySet.class); - xstream.registerConverter(new ChronicleMapConverter(this)); - xstream.registerConverter(new ByteBufferConverter()); - xstream.registerConverter(new DataValueConverter()); - - InputStream inputStream = new FileInputStream(fromFile); - if (fromFile.getName().toLowerCase().endsWith(".gz")) - inputStream = new GZIPInputStream(inputStream); - try (InputStream out = inputStream) { - xstream.fromXML(out); - } + JsonSerializer.putAll(fromFile, this); } @Override diff --git a/src/main/java/net/openhft/xstreem/convertors/ByteBufferConverter.java b/src/main/java/net/openhft/xstreem/convertors/ByteBufferConverter.java index 8b76a66d0..c003cde8f 100644 --- a/src/main/java/net/openhft/xstreem/convertors/ByteBufferConverter.java +++ b/src/main/java/net/openhft/xstreem/convertors/ByteBufferConverter.java @@ -1,3 +1,20 @@ +/* + * Copyright 2014 Higher Frequency Trading + * + * http://www.higherfrequencytrading.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package net.openhft.xstreem.convertors; import com.thoughtworks.xstream.converters.ConversionException; @@ -12,7 +29,7 @@ import java.nio.CharBuffer; import java.nio.charset.*; /** - * Created by Rob on 22/12/14. + * Created by Rob Austin */ public class ByteBufferConverter implements Converter { diff --git a/src/main/java/net/openhft/xstreem/convertors/ChronicleMapConverter.java b/src/main/java/net/openhft/xstreem/convertors/ChronicleMapConverter.java index 8b0d44b61..8c7bff28b 100644 --- a/src/main/java/net/openhft/xstreem/convertors/ChronicleMapConverter.java +++ b/src/main/java/net/openhft/xstreem/convertors/ChronicleMapConverter.java @@ -1,3 +1,20 @@ +/* + * Copyright 2014 Higher Frequency Trading + * + * http://www.higherfrequencytrading.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package net.openhft.xstreem.convertors; import com.thoughtworks.xstream.converters.ConversionException; diff --git a/src/main/java/net/openhft/xstreem/convertors/DataValueConverter.java b/src/main/java/net/openhft/xstreem/convertors/DataValueConverter.java index c743b7900..b470b1e0d 100644 --- a/src/main/java/net/openhft/xstreem/convertors/DataValueConverter.java +++ b/src/main/java/net/openhft/xstreem/convertors/DataValueConverter.java @@ -1,3 +1,20 @@ +/* + * Copyright 2014 Higher Frequency Trading + * + * http://www.higherfrequencytrading.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package net.openhft.xstreem.convertors; import com.thoughtworks.xstream.converters.ConversionException; diff --git a/src/test/java/net/openhft/chronicle/map/CHMUseCasesTest.java b/src/test/java/net/openhft/chronicle/map/CHMUseCasesTest.java index 62526c0e9..f86c2f955 100644 --- a/src/test/java/net/openhft/chronicle/map/CHMUseCasesTest.java +++ b/src/test/java/net/openhft/chronicle/map/CHMUseCasesTest.java @@ -315,8 +315,6 @@ public class CHMUseCasesTest { try (ChronicleMap map = newInstance(builder)) { map.put("Key", expected); - char[] actual = map.get("Key"); - System.out.println(actual); assertEquals(Chars.asList(expected), Chars.asList(map.get("Key"))); mapChecks(); }