optimized serializers for nodes
Project: http://git-wip-us.apache.org/repos/asf/marmotta/repo Commit: http://git-wip-us.apache.org/repos/asf/marmotta/commit/27a9c265 Tree: http://git-wip-us.apache.org/repos/asf/marmotta/tree/27a9c265 Diff: http://git-wip-us.apache.org/repos/asf/marmotta/diff/27a9c265 Branch: refs/heads/develop Commit: 27a9c2652dae658fcf32cf46c0bffb5a5b7c1bfa Parents: 1698c6c Author: Sebastian Schaffert <[email protected]> Authored: Mon Mar 3 18:17:00 2014 +0100 Committer: Sebastian Schaffert <[email protected]> Committed: Mon Mar 3 18:17:00 2014 +0100 ---------------------------------------------------------------------- .../org/apache/marmotta/commons/io/DataIO.java | 67 ++++++ .../kiwi/externalizer/ExternalizerIds.java | 2 + .../externalizer/StringLiteralExternalizer.java | 32 +-- .../kiwi/externalizer/TripleExternalizer.java | 44 ++-- .../kiwi/externalizer/UriExternalizer.java | 78 ++++++- .../marmotta/kiwi/test/ExternalizerTest.java | 228 +++++++++++++++++++ .../test/externalizer/ExternalizerTest.java | 206 ----------------- 7 files changed, 406 insertions(+), 251 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/marmotta/blob/27a9c265/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/io/DataIO.java ---------------------------------------------------------------------- diff --git a/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/io/DataIO.java b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/io/DataIO.java new file mode 100644 index 0000000..d236ef7 --- /dev/null +++ b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/io/DataIO.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.marmotta.commons.io; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.util.Date; + +/** + * Add file description here! + * + * @author Sebastian Schaffert ([email protected]) + */ +public class DataIO { + + + public static void writeString(DataOutput out, String s) throws IOException { + if(s != null) { + out.writeInt(s.length()); + out.writeChars(s); + } else { + out.writeInt(-1); + } + } + + + public static String readString(DataInput in) throws IOException { + int len = in.readInt(); + + if(len >= 0) { + StringBuilder builder = new StringBuilder(); + for(int i=0; i<len; i++) { + builder.append(in.readChar()); + } + return builder.toString(); + } else { + return null; + } + } + + + public static void writeDate(DataOutput out, Date date) throws IOException { + out.writeLong(date.getTime()); + } + + + public static Date readDate(DataInput in) throws IOException { + long time = in.readLong(); + return new Date(time); + } +} http://git-wip-us.apache.org/repos/asf/marmotta/blob/27a9c265/libraries/kiwi/kiwi-caching-infinispan/src/main/java/org/apache/marmotta/kiwi/externalizer/ExternalizerIds.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-caching-infinispan/src/main/java/org/apache/marmotta/kiwi/externalizer/ExternalizerIds.java b/libraries/kiwi/kiwi-caching-infinispan/src/main/java/org/apache/marmotta/kiwi/externalizer/ExternalizerIds.java index 880303d..5be8cbe 100644 --- a/libraries/kiwi/kiwi-caching-infinispan/src/main/java/org/apache/marmotta/kiwi/externalizer/ExternalizerIds.java +++ b/libraries/kiwi/kiwi-caching-infinispan/src/main/java/org/apache/marmotta/kiwi/externalizer/ExternalizerIds.java @@ -24,6 +24,8 @@ package org.apache.marmotta.kiwi.externalizer; */ public class ExternalizerIds { + public static final int TRIPLE = 13; + public static final int URI = 17; public static final int BNODE = 23; http://git-wip-us.apache.org/repos/asf/marmotta/blob/27a9c265/libraries/kiwi/kiwi-caching-infinispan/src/main/java/org/apache/marmotta/kiwi/externalizer/StringLiteralExternalizer.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-caching-infinispan/src/main/java/org/apache/marmotta/kiwi/externalizer/StringLiteralExternalizer.java b/libraries/kiwi/kiwi-caching-infinispan/src/main/java/org/apache/marmotta/kiwi/externalizer/StringLiteralExternalizer.java index 4e47459..178c76a 100644 --- a/libraries/kiwi/kiwi-caching-infinispan/src/main/java/org/apache/marmotta/kiwi/externalizer/StringLiteralExternalizer.java +++ b/libraries/kiwi/kiwi-caching-infinispan/src/main/java/org/apache/marmotta/kiwi/externalizer/StringLiteralExternalizer.java @@ -17,6 +17,7 @@ package org.apache.marmotta.kiwi.externalizer; +import org.apache.marmotta.commons.io.DataIO; import org.apache.marmotta.kiwi.model.rdf.KiWiStringLiteral; import org.apache.marmotta.kiwi.model.rdf.KiWiUriResource; import org.infinispan.commons.marshall.AdvancedExternalizer; @@ -49,14 +50,9 @@ public class StringLiteralExternalizer implements AdvancedExternalizer<KiWiStrin @Override public void writeObject(ObjectOutput output, KiWiStringLiteral object) throws IOException { output.writeLong(object.getId()); - output.writeInt(object.getContent().length()); - output.writeChars(object.getContent()); - if(object.getLanguage() != null) { - output.writeInt(object.getLanguage().length()); - output.writeChars(object.getLanguage()); - } else { - output.writeInt(0); - } + + DataIO.writeString(output, object.getContent()); + DataIO.writeString(output, object.getLanguage()); output.writeObject(object.getDatatype()); @@ -67,27 +63,15 @@ public class StringLiteralExternalizer implements AdvancedExternalizer<KiWiStrin @Override public KiWiStringLiteral readObject(ObjectInput input) throws IOException, ClassNotFoundException { long id = input.readLong(); - int clen = input.readInt(); - char[] content = new char[clen]; - for(int i=0; i<clen; i++) { - content[i]=input.readChar(); - } - - int llen = input.readInt(); - String lang = null; - if(llen > 0) { - char[] lb = new char[llen]; - for(int i=0; i<llen; i++) { - lb[i] = input.readChar(); - } - lang = new String(lb); - } + + String content = DataIO.readString(input); + String lang = DataIO.readString(input); KiWiUriResource dtype = (KiWiUriResource) input.readObject(); Date created = new Date(input.readLong()); - KiWiStringLiteral r = new KiWiStringLiteral(new String(content), lang != null ? Locale.forLanguageTag(lang) : null, dtype, created); + KiWiStringLiteral r = new KiWiStringLiteral(content, lang != null ? Locale.forLanguageTag(lang) : null, dtype, created); r.setId(id); return r; http://git-wip-us.apache.org/repos/asf/marmotta/blob/27a9c265/libraries/kiwi/kiwi-caching-infinispan/src/main/java/org/apache/marmotta/kiwi/externalizer/TripleExternalizer.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-caching-infinispan/src/main/java/org/apache/marmotta/kiwi/externalizer/TripleExternalizer.java b/libraries/kiwi/kiwi-caching-infinispan/src/main/java/org/apache/marmotta/kiwi/externalizer/TripleExternalizer.java index 05e6933..60793c1 100644 --- a/libraries/kiwi/kiwi-caching-infinispan/src/main/java/org/apache/marmotta/kiwi/externalizer/TripleExternalizer.java +++ b/libraries/kiwi/kiwi-caching-infinispan/src/main/java/org/apache/marmotta/kiwi/externalizer/TripleExternalizer.java @@ -18,6 +18,7 @@ package org.apache.marmotta.kiwi.externalizer; import org.apache.commons.lang3.StringUtils; +import org.apache.marmotta.commons.io.DataIO; import org.apache.marmotta.kiwi.model.rdf.KiWiNode; import org.apache.marmotta.kiwi.model.rdf.KiWiResource; import org.apache.marmotta.kiwi.model.rdf.KiWiTriple; @@ -49,7 +50,7 @@ public class TripleExternalizer implements AdvancedExternalizer<KiWiTriple> { @Override public Integer getId() { - return 13; + return ExternalizerIds.TRIPLE; } @Override @@ -64,19 +65,16 @@ public class TripleExternalizer implements AdvancedExternalizer<KiWiTriple> { String prefix = StringUtils.getCommonPrefix(sUri,oUri); output.writeByte(MODE_PREFIX); - output.writeInt(prefix.length()); - output.writeChars(prefix); + DataIO.writeString(output,prefix); output.writeLong(object.getSubject().getId()); - output.writeInt(sUri.length() - prefix.length()); - output.writeChars(sUri.substring(prefix.length())); + DataIO.writeString(output, sUri.substring(prefix.length())); output.writeLong(object.getSubject().getCreated().getTime()); output.writeObject(object.getPredicate()); output.writeLong(object.getObject().getId()); - output.writeInt(oUri.length() - prefix.length()); - output.writeChars(oUri.substring(prefix.length())); + DataIO.writeString(output, oUri.substring(prefix.length())); output.writeLong(object.getObject().getCreated().getTime()); } else { output.writeByte(MODE_DEFAULT); @@ -105,15 +103,33 @@ public class TripleExternalizer implements AdvancedExternalizer<KiWiTriple> { KiWiTriple result = new KiWiTriple(); result.setId(input.readLong()); - int mode = input.readInt(); + int mode = input.readByte(); if(mode == MODE_PREFIX) { - String prefix = - } - + String prefix = DataIO.readString(input); + + long sId = input.readLong(); + String sUri = prefix + DataIO.readString(input); + long sTime = input.readLong(); + KiWiUriResource s = new KiWiUriResource(sUri); + s.setId(sId); + s.setCreated(new Date(sTime)); + result.setSubject(s); + + result.setPredicate((KiWiUriResource) input.readObject()); + + long oId = input.readLong(); + String oUri = prefix + DataIO.readString(input); + long oTime = input.readLong(); + KiWiUriResource o = new KiWiUriResource(oUri); + o.setId(oId); + o.setCreated(new Date(oTime)); + result.setObject(o); - result.setSubject((KiWiResource) input.readObject()); - result.setPredicate((KiWiUriResource) input.readObject()); - result.setObject((KiWiNode) input.readObject()); + } else { + result.setSubject((KiWiResource) input.readObject()); + result.setPredicate((KiWiUriResource) input.readObject()); + result.setObject((KiWiNode) input.readObject()); + } result.setContext((KiWiResource) input.readObject()); result.setCreator((KiWiResource) input.readObject()); result.setDeleted(input.readBoolean()); http://git-wip-us.apache.org/repos/asf/marmotta/blob/27a9c265/libraries/kiwi/kiwi-caching-infinispan/src/main/java/org/apache/marmotta/kiwi/externalizer/UriExternalizer.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-caching-infinispan/src/main/java/org/apache/marmotta/kiwi/externalizer/UriExternalizer.java b/libraries/kiwi/kiwi-caching-infinispan/src/main/java/org/apache/marmotta/kiwi/externalizer/UriExternalizer.java index 0daee45..3db4d4e 100644 --- a/libraries/kiwi/kiwi-caching-infinispan/src/main/java/org/apache/marmotta/kiwi/externalizer/UriExternalizer.java +++ b/libraries/kiwi/kiwi-caching-infinispan/src/main/java/org/apache/marmotta/kiwi/externalizer/UriExternalizer.java @@ -17,9 +17,12 @@ package org.apache.marmotta.kiwi.externalizer; +import org.apache.marmotta.commons.io.DataIO; +import org.apache.marmotta.commons.vocabulary.XSD; import org.apache.marmotta.kiwi.model.rdf.KiWiUriResource; import org.infinispan.commons.marshall.AdvancedExternalizer; import org.infinispan.commons.util.Util; +import org.openrdf.model.vocabulary.*; import java.io.IOException; import java.io.ObjectInput; @@ -34,6 +37,15 @@ import java.util.Set; */ public class UriExternalizer implements AdvancedExternalizer<KiWiUriResource> { + private static final int PREFIX_UNKNOWN = 0; + private static final int PREFIX_XSD = 1; + private static final int PREFIX_RDF = 2; + private static final int PREFIX_RDFS = 3; + private static final int PREFIX_SKOS = 4; + private static final int PREFIX_DC = 5; + private static final int PREFIX_DCT = 6; + private static final int PREFIX_OWL = 7; + @Override public Set<Class<? extends KiWiUriResource>> getTypeClasses() { return Util.<Class<? extends KiWiUriResource>>asSet(KiWiUriResource.class); @@ -47,26 +59,78 @@ public class UriExternalizer implements AdvancedExternalizer<KiWiUriResource> { @Override public void writeObject(ObjectOutput output, KiWiUriResource object) throws IOException { output.writeLong(object.getId()); - output.writeInt(object.stringValue().length()); - output.writeChars(object.stringValue()); + + // compression for commonly used constant prefixes + if(object.stringValue().startsWith(XSD.NAMESPACE)) { + output.writeByte(PREFIX_XSD); + DataIO.writeString(output, object.stringValue().substring(XSD.NAMESPACE.length())); + } else if(object.stringValue().startsWith(RDF.NAMESPACE)) { + output.writeByte(PREFIX_RDF); + DataIO.writeString(output, object.stringValue().substring(RDF.NAMESPACE.length())); + } else if(object.stringValue().startsWith(RDFS.NAMESPACE)) { + output.writeByte(PREFIX_RDFS); + DataIO.writeString(output, object.stringValue().substring(RDFS.NAMESPACE.length())); + } else if(object.stringValue().startsWith(SKOS.NAMESPACE)) { + output.writeByte(PREFIX_SKOS); + DataIO.writeString(output, object.stringValue().substring(SKOS.NAMESPACE.length())); + } else if(object.stringValue().startsWith(DC.NAMESPACE)) { + output.writeByte(PREFIX_DC); + DataIO.writeString(output, object.stringValue().substring(DC.NAMESPACE.length())); + } else if(object.stringValue().startsWith(DCTERMS.NAMESPACE)) { + output.writeByte(PREFIX_DCT); + DataIO.writeString(output, object.stringValue().substring(DCTERMS.NAMESPACE.length())); + } else if(object.stringValue().startsWith(OWL.NAMESPACE)) { + output.writeByte(PREFIX_OWL); + DataIO.writeString(output, object.stringValue().substring(OWL.NAMESPACE.length())); + } else { + output.writeByte(PREFIX_UNKNOWN); + DataIO.writeString(output, object.stringValue()); + } + output.writeLong(object.getCreated().getTime()); } @Override public KiWiUriResource readObject(ObjectInput input) throws IOException, ClassNotFoundException { long id = input.readLong(); - int len = input.readInt(); - char[] uri = new char[len]; - for(int i=0; i<len; i++) { - uri[i] = input.readChar(); + int prefixMode = input.readByte(); + String uriPrefix = ""; + String uriSuffix = DataIO.readString(input); + + switch (prefixMode) { + case PREFIX_XSD: + uriPrefix = XSD.NAMESPACE; + break; + case PREFIX_RDF: + uriPrefix = RDF.NAMESPACE; + break; + case PREFIX_RDFS: + uriPrefix = RDFS.NAMESPACE; + break; + case PREFIX_SKOS: + uriPrefix = SKOS.NAMESPACE; + break; + case PREFIX_DC: + uriPrefix = DC.NAMESPACE; + break; + case PREFIX_DCT: + uriPrefix = DCTERMS.NAMESPACE; + break; + case PREFIX_OWL: + uriPrefix = OWL.NAMESPACE; + break; + default: + uriPrefix = ""; + break; } Date created = new Date(input.readLong()); - KiWiUriResource r = new KiWiUriResource(new String(uri),created); + KiWiUriResource r = new KiWiUriResource(uriPrefix + uriSuffix,created); r.setId(id); return r; } + } http://git-wip-us.apache.org/repos/asf/marmotta/blob/27a9c265/libraries/kiwi/kiwi-caching-infinispan/src/test/java/org/apache/marmotta/kiwi/test/ExternalizerTest.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-caching-infinispan/src/test/java/org/apache/marmotta/kiwi/test/ExternalizerTest.java b/libraries/kiwi/kiwi-caching-infinispan/src/test/java/org/apache/marmotta/kiwi/test/ExternalizerTest.java new file mode 100644 index 0000000..74f63d2 --- /dev/null +++ b/libraries/kiwi/kiwi-caching-infinispan/src/test/java/org/apache/marmotta/kiwi/test/ExternalizerTest.java @@ -0,0 +1,228 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.marmotta.kiwi.test; + +import org.apache.commons.lang3.RandomStringUtils; +import org.apache.marmotta.commons.vocabulary.XSD; +import org.apache.marmotta.kiwi.externalizer.*; +import org.apache.marmotta.kiwi.model.rdf.*; +import org.infinispan.commons.marshall.AdvancedExternalizer; +import org.infinispan.commons.marshall.StreamingMarshaller; +import org.infinispan.configuration.cache.CacheMode; +import org.infinispan.configuration.cache.Configuration; +import org.infinispan.configuration.cache.ConfigurationBuilder; +import org.infinispan.configuration.global.GlobalConfiguration; +import org.infinispan.configuration.global.GlobalConfigurationBuilder; +import org.infinispan.manager.DefaultCacheManager; +import org.infinispan.manager.EmbeddedCacheManager; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.openrdf.model.Value; +import org.openrdf.model.ValueFactory; +import org.openrdf.model.vocabulary.OWL; +import org.openrdf.model.vocabulary.RDFS; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.*; +import java.util.Random; + +/** + * Test the different externalizer implementations we provide for Infinispan + * + * @author Sebastian Schaffert ([email protected]) + */ +public class ExternalizerTest { + + private static ValueFactory valueFactory = new TestValueFactory(); + + private static Random rnd = new Random(); + + private static Logger log = LoggerFactory.getLogger(ExternalizerTest.class); + + private static StreamingMarshaller marshaller; + + + @BeforeClass + public static void setup() { + AdvancedExternalizer[] externalizers = new AdvancedExternalizer[] { + new UriExternalizer(), + new BNodeExternalizer(), + new StringLiteralExternalizer(), + new DateLiteralExternalizer(), + new BooleanLiteralExternalizer(), + new IntLiteralExternalizer(), + new DoubleLiteralExternalizer(), + new TripleExternalizer() + }; + + + GlobalConfiguration globalConfiguration = new GlobalConfigurationBuilder() + .transport() + .defaultTransport() + .serialization() + .addAdvancedExternalizer(externalizers) + .build(); + + Configuration defaultConfiguration = new ConfigurationBuilder() + .clustering() + .cacheMode(CacheMode.DIST_ASYNC) + .build(); + + EmbeddedCacheManager cacheManager = new DefaultCacheManager(globalConfiguration, defaultConfiguration, true); + + marshaller = cacheManager.getCache().getAdvancedCache().getComponentRegistry().getCacheMarshaller(); + + } + + + @Test + public void testUriResource() throws Exception { + marshall((KiWiUriResource) valueFactory.createURI("http://localhost/" + RandomStringUtils.randomAlphanumeric(8)), new UriExternalizer()); + } + + @Test + public void testCompressedUriResource() throws Exception { + marshall((KiWiUriResource) valueFactory.createURI(XSD.Double.stringValue()), new UriExternalizer()); + marshall((KiWiUriResource) valueFactory.createURI(RDFS.LABEL.stringValue()), new UriExternalizer()); + marshall((KiWiUriResource) valueFactory.createURI(OWL.SAMEAS.stringValue()), new UriExternalizer()); + } + + + @Test + public void testBNode() throws Exception { + marshall((KiWiAnonResource) valueFactory.createBNode(), new BNodeExternalizer()); + } + + @Test + public void testStringLiteral() throws Exception { + marshall((KiWiStringLiteral) valueFactory.createLiteral(RandomStringUtils.randomAscii(40)), new StringLiteralExternalizer()); + } + + @Test + public void testLangLiteral() throws Exception { + marshall((KiWiStringLiteral) valueFactory.createLiteral(RandomStringUtils.randomAscii(40),"en"), new StringLiteralExternalizer()); + } + + @Test + public void testTypeLiteral() throws Exception { + marshall((KiWiStringLiteral) valueFactory.createLiteral(RandomStringUtils.randomAscii(40),valueFactory.createURI("http://localhost/" + RandomStringUtils.randomAlphanumeric(8))), new StringLiteralExternalizer()); + } + + + @Test + public void testIntLiteral() throws Exception { + marshall((KiWiIntLiteral) valueFactory.createLiteral(rnd.nextInt()), new IntLiteralExternalizer()); + } + + + @Test + public void testTriple() throws Exception { + KiWiUriResource s = (KiWiUriResource) valueFactory.createURI("http://localhost/" + RandomStringUtils.randomAlphanumeric(8)); + KiWiUriResource p = (KiWiUriResource) valueFactory.createURI("http://localhost/" + RandomStringUtils.randomAlphanumeric(8)); + KiWiNode o = (KiWiNode) randomNode(); + KiWiTriple t = (KiWiTriple) valueFactory.createStatement(s,p,o); + + marshall(t, new TripleExternalizer()); + } + + @Test + public void testPrefixCompressedTriple() throws Exception { + KiWiUriResource s = (KiWiUriResource) valueFactory.createURI("http://localhost/" + RandomStringUtils.randomAlphanumeric(8)); + KiWiUriResource p = (KiWiUriResource) valueFactory.createURI("http://localhost/" + RandomStringUtils.randomAlphanumeric(8)); + KiWiUriResource o = (KiWiUriResource) valueFactory.createURI("http://localhost/" + RandomStringUtils.randomAlphanumeric(8)); + KiWiTriple t = (KiWiTriple) valueFactory.createStatement(s,p,o); + + marshall(t, new TripleExternalizer()); + } + + + /** + * Run the given object through the marshaller using an in-memory stream. + * @param origin + * @param <T> + * @return + */ + private <T> void marshall(T origin, AdvancedExternalizer<T> externalizer) throws IOException, ClassNotFoundException, InterruptedException { + log.info("- testing Java ObjectStream ..."); + ByteArrayOutputStream outBytesOS = new ByteArrayOutputStream(); + ObjectOutputStream outOS = new ObjectOutputStream(outBytesOS); + + outOS.writeObject(origin); + + outOS.close(); + + log.info(" object {}: serialized with {} bytes", origin, outBytesOS.size()); + + + log.info("- testing externalizer directly ..."); + ByteArrayOutputStream outBytes = new ByteArrayOutputStream(); + ObjectOutputStream out = new ObjectOutputStream(outBytes); + + externalizer.writeObject(out, origin); + out.close(); + + log.info(" object {}: serialized with {} bytes", origin, outBytes.size()); + + ByteArrayInputStream inBytes = new ByteArrayInputStream(outBytes.toByteArray()); + ObjectInputStream in = new ObjectInputStream(inBytes); + + T destination1 = externalizer.readObject(in); + + Assert.assertEquals(origin,destination1); + + log.info("- testing externalizer with infinispan marshaller ..."); + + byte[] bytes = marshaller.objectToByteBuffer(origin); + log.info(" object {}: serialized with {} bytes", origin, bytes.length); + + Object destination2 = marshaller.objectFromByteBuffer(bytes); + + Assert.assertEquals(origin, destination2); + + } + + + /** + * Return a random RDF value, either a reused object (10% chance) or of any other kind. + * @return + */ + protected Value randomNode() { + Value object; + switch(rnd.nextInt(6)) { + case 0: object = valueFactory.createURI("http://localhost/" + RandomStringUtils.randomAlphanumeric(8)); + break; + case 1: object = valueFactory.createBNode(); + break; + case 2: object = valueFactory.createLiteral(RandomStringUtils.randomAscii(40)); + break; + case 3: object = valueFactory.createLiteral(rnd.nextInt()); + break; + case 4: object = valueFactory.createLiteral(rnd.nextDouble()); + break; + case 5: object = valueFactory.createLiteral(rnd.nextBoolean()); + break; + default: object = valueFactory.createURI("http://localhost/" + RandomStringUtils.randomAlphanumeric(8)); + break; + + } + return object; + } + +} http://git-wip-us.apache.org/repos/asf/marmotta/blob/27a9c265/libraries/kiwi/kiwi-caching-infinispan/src/test/java/org/apache/marmotta/kiwi/test/externalizer/ExternalizerTest.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-caching-infinispan/src/test/java/org/apache/marmotta/kiwi/test/externalizer/ExternalizerTest.java b/libraries/kiwi/kiwi-caching-infinispan/src/test/java/org/apache/marmotta/kiwi/test/externalizer/ExternalizerTest.java deleted file mode 100644 index 9f52813..0000000 --- a/libraries/kiwi/kiwi-caching-infinispan/src/test/java/org/apache/marmotta/kiwi/test/externalizer/ExternalizerTest.java +++ /dev/null @@ -1,206 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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 org.apache.marmotta.kiwi.test.externalizer; - -import org.apache.commons.lang3.RandomStringUtils; -import org.apache.marmotta.kiwi.externalizer.*; -import org.apache.marmotta.kiwi.model.rdf.*; -import org.apache.marmotta.kiwi.test.TestValueFactory; -import org.infinispan.commons.marshall.AdvancedExternalizer; -import org.infinispan.commons.marshall.StreamingMarshaller; -import org.infinispan.configuration.cache.CacheMode; -import org.infinispan.configuration.cache.Configuration; -import org.infinispan.configuration.cache.ConfigurationBuilder; -import org.infinispan.configuration.global.GlobalConfiguration; -import org.infinispan.configuration.global.GlobalConfigurationBuilder; -import org.infinispan.manager.DefaultCacheManager; -import org.infinispan.manager.EmbeddedCacheManager; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; -import org.openrdf.model.Value; -import org.openrdf.model.ValueFactory; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.*; -import java.util.Random; - -/** - * Test the different externalizer implementations we provide for Infinispan - * - * @author Sebastian Schaffert ([email protected]) - */ -public class ExternalizerTest { - - private static ValueFactory valueFactory = new TestValueFactory(); - - private static Random rnd = new Random(); - - private static Logger log = LoggerFactory.getLogger(ExternalizerTest.class); - - private static StreamingMarshaller marshaller; - - - @BeforeClass - public static void setup() { - AdvancedExternalizer[] externalizers = new AdvancedExternalizer[] { - new UriExternalizer(), - new BNodeExternalizer(), - new StringLiteralExternalizer(), - new DateLiteralExternalizer(), - new BooleanLiteralExternalizer(), - new IntLiteralExternalizer(), - new DoubleLiteralExternalizer() - }; - - - GlobalConfiguration globalConfiguration = new GlobalConfigurationBuilder() - .transport() - .defaultTransport() - .serialization() - .addAdvancedExternalizer(externalizers) - .build(); - - Configuration defaultConfiguration = new ConfigurationBuilder() - .clustering() - .cacheMode(CacheMode.DIST_ASYNC) - .build(); - - EmbeddedCacheManager cacheManager = new DefaultCacheManager(globalConfiguration, defaultConfiguration, true); - - marshaller = cacheManager.getCache().getAdvancedCache().getComponentRegistry().getCacheMarshaller(); - - } - - - @Test - public void testUriResource() throws Exception { - marshall((KiWiUriResource) valueFactory.createURI("http://localhost/" + RandomStringUtils.randomAlphanumeric(8)), new UriExternalizer()); - } - - @Test - public void testBNode() throws Exception { - marshall((KiWiAnonResource) valueFactory.createBNode(), new BNodeExternalizer()); - } - - @Test - public void testStringLiteral() throws Exception { - marshall((KiWiStringLiteral) valueFactory.createLiteral(RandomStringUtils.randomAscii(40)), new StringLiteralExternalizer()); - } - - @Test - public void testLangLiteral() throws Exception { - marshall((KiWiStringLiteral) valueFactory.createLiteral(RandomStringUtils.randomAscii(40),"en"), new StringLiteralExternalizer()); - } - - @Test - public void testTypeLiteral() throws Exception { - marshall((KiWiStringLiteral) valueFactory.createLiteral(RandomStringUtils.randomAscii(40),valueFactory.createURI("http://localhost/" + RandomStringUtils.randomAlphanumeric(8))), new StringLiteralExternalizer()); - } - - - @Test - public void testIntLiteral() throws Exception { - marshall((KiWiIntLiteral) valueFactory.createLiteral(rnd.nextInt()), new IntLiteralExternalizer()); - } - - - @Test - public void testTriple() throws Exception { - KiWiUriResource s = (KiWiUriResource) valueFactory.createURI("http://localhost/" + RandomStringUtils.randomAlphanumeric(8)); - KiWiUriResource p = (KiWiUriResource) valueFactory.createURI("http://localhost/" + RandomStringUtils.randomAlphanumeric(8)); - KiWiNode o = (KiWiNode) randomNode(); - KiWiTriple t = (KiWiTriple) valueFactory.createStatement(s,p,o); - - marshall(t, new TripleExternalizer()); - } - - /** - * Run the given object through the marshaller using an in-memory stream. - * @param origin - * @param <T> - * @return - */ - private <T> void marshall(T origin, AdvancedExternalizer<T> externalizer) throws IOException, ClassNotFoundException, InterruptedException { - log.info("- testing Java ObjectStream ..."); - ByteArrayOutputStream outBytesOS = new ByteArrayOutputStream(); - ObjectOutputStream outOS = new ObjectOutputStream(outBytesOS); - - outOS.writeObject(origin); - - outOS.close(); - - log.info(" object {}: serialized with {} bytes", origin, outBytesOS.size()); - - - log.info("- testing externalizer directly ..."); - ByteArrayOutputStream outBytes = new ByteArrayOutputStream(); - ObjectOutputStream out = new ObjectOutputStream(outBytes); - - externalizer.writeObject(out, origin); - out.close(); - - log.info(" object {}: serialized with {} bytes", origin, outBytes.size()); - - ByteArrayInputStream inBytes = new ByteArrayInputStream(outBytes.toByteArray()); - ObjectInputStream in = new ObjectInputStream(inBytes); - - T destination1 = externalizer.readObject(in); - - Assert.assertEquals(origin,destination1); - - log.info("- testing externalizer with infinispan marshaller ..."); - - byte[] bytes = marshaller.objectToByteBuffer(origin); - log.info(" object {}: serialized with {} bytes", origin, bytes.length); - - Object destination2 = marshaller.objectFromByteBuffer(bytes); - - Assert.assertEquals(origin, destination2); - - } - - - /** - * Return a random RDF value, either a reused object (10% chance) or of any other kind. - * @return - */ - protected Value randomNode() { - Value object; - switch(rnd.nextInt(6)) { - case 0: object = valueFactory.createURI("http://localhost/" + RandomStringUtils.randomAlphanumeric(8)); - break; - case 1: object = valueFactory.createBNode(); - break; - case 2: object = valueFactory.createLiteral(RandomStringUtils.randomAscii(40)); - break; - case 3: object = valueFactory.createLiteral(rnd.nextInt()); - break; - case 4: object = valueFactory.createLiteral(rnd.nextDouble()); - break; - case 5: object = valueFactory.createLiteral(rnd.nextBoolean()); - break; - default: object = valueFactory.createURI("http://localhost/" + RandomStringUtils.randomAlphanumeric(8)); - break; - - } - return object; - } - -}
