Return-Path: X-Original-To: apmail-marmotta-commits-archive@minotaur.apache.org Delivered-To: apmail-marmotta-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 5DACA10DA9 for ; Mon, 17 Mar 2014 10:23:47 +0000 (UTC) Received: (qmail 22876 invoked by uid 500); 17 Mar 2014 10:23:12 -0000 Delivered-To: apmail-marmotta-commits-archive@marmotta.apache.org Received: (qmail 22671 invoked by uid 500); 17 Mar 2014 10:23:04 -0000 Mailing-List: contact commits-help@marmotta.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@marmotta.apache.org Delivered-To: mailing list commits@marmotta.apache.org Received: (qmail 22186 invoked by uid 99); 17 Mar 2014 10:22:53 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 17 Mar 2014 10:22:53 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id B889B94592B; Mon, 17 Mar 2014 10:22:52 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: sschaffert@apache.org To: commits@marmotta.apache.org Date: Mon, 17 Mar 2014 10:22:59 -0000 Message-Id: <9cbba4cec45c482d80ccad68cc1c5b20@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [08/32] git commit: optimized serializers for nodes 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 Authored: Mon Mar 3 18:17:00 2014 +0100 Committer: Sebastian Schaffert 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 (sschaffert@apache.org) + */ +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 0) { - char[] lb = new char[llen]; - for(int i=0; i { @Override public Integer getId() { - return 13; + return ExternalizerIds.TRIPLE; } @Override @@ -64,19 +65,16 @@ public class TripleExternalizer implements AdvancedExternalizer { 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 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 { + 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> getTypeClasses() { return Util.>asSet(KiWiUriResource.class); @@ -47,26 +59,78 @@ public class UriExternalizer implements AdvancedExternalizer { @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 + * @return + */ + private void marshall(T origin, AdvancedExternalizer 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 (sschaffert@apache.org) - */ -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 - * @return - */ - private void marshall(T origin, AdvancedExternalizer 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; - } - -}