Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class InitRIOT implements JenaSubsystemLifecycle {
@Override
public void start() {
RIOT.init();
SerializerRDF.init();
}

@Override
Expand Down
53 changes: 53 additions & 0 deletions jena-arq/src/main/java/org/apache/jena/riot/system/SNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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.jena.riot.system;

import java.io.IOException;
import java.io.ObjectStreamException;
import java.io.Serializable;

import org.apache.jena.graph.Node;
import org.apache.jena.riot.thrift.TRDF;
import org.apache.jena.riot.thrift.wire.RDF_Term;
import org.apache.thrift.protocol.TProtocol;

/** Serialization of a {@link Node} using Thrift for the serialization. */
public final class SNode implements Serializable {
private static final long serialVersionUID = 0xb17aa042f299adb9L;
private transient Node node;

public SNode(Node node) { this.node = node; }
public Node getNode() { return node; }

private void writeObject(java.io.ObjectOutputStream out) throws IOException {
TProtocol protocol = TRDF.protocol(out);
RDF_Term tterm = new RDF_Term();
SerializerRDF.write(protocol, tterm, node);
TRDF.flush(protocol);
}

private void readObject(java.io.ObjectInputStream in) throws IOException {
TProtocol protocol = TRDF.protocol(in);
RDF_Term tterm = new RDF_Term();
node = SerializerRDF.read(protocol, tterm);
}

Object readResolve() throws ObjectStreamException
{ return node; }
}
62 changes: 62 additions & 0 deletions jena-arq/src/main/java/org/apache/jena/riot/system/SQuad.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.jena.riot.system;

import java.io.IOException;
import java.io.ObjectStreamException;
import java.io.Serializable;

import org.apache.jena.graph.Node;
import org.apache.jena.riot.thrift.TRDF;
import org.apache.jena.riot.thrift.wire.RDF_Term;
import org.apache.jena.sparql.core.Quad;
import org.apache.thrift.protocol.TProtocol;

/** Serialization of a {@link Quad} using Thrift for the serialization. */
public final class SQuad implements Serializable {
private static final long serialVersionUID = 0x993530716d72ff06L;
private transient Quad quad;

public SQuad(Quad quad) { this.quad = quad; }
public Quad getQuad() { return quad; }

private void writeObject(java.io.ObjectOutputStream out) throws IOException {
TProtocol protocol = TRDF.protocol(out);
RDF_Term tterm = new RDF_Term();
SerializerRDF.write(protocol, tterm, quad.getGraph());
SerializerRDF.write(protocol, tterm, quad.getSubject());
SerializerRDF.write(protocol, tterm, quad.getPredicate());
SerializerRDF.write(protocol, tterm, quad.getObject());
TRDF.flush(protocol);
}

private void readObject(java.io.ObjectInputStream in) throws IOException {
TProtocol protocol = TRDF.protocol(in);
RDF_Term tterm = new RDF_Term();
Node g = SerializerRDF.read(protocol, tterm);
Node s = SerializerRDF.read(protocol, tterm);
Node p = SerializerRDF.read(protocol, tterm);
Node o = SerializerRDF.read(protocol, tterm);
quad = Quad.create(g, s, p, o);
}

Object readResolve() throws ObjectStreamException
{ return quad; }

}
59 changes: 59 additions & 0 deletions jena-arq/src/main/java/org/apache/jena/riot/system/STriple.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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.jena.riot.system;

import java.io.IOException;
import java.io.ObjectStreamException;
import java.io.Serializable;

import org.apache.jena.graph.Node;
import org.apache.jena.graph.Triple;
import org.apache.jena.riot.thrift.TRDF;
import org.apache.jena.riot.thrift.wire.RDF_Term;
import org.apache.thrift.protocol.TProtocol;

/** Serialization of a {@link Triple} using Thrift for the serialization. */
public final class STriple implements Serializable {
private static final long serialVersionUID = 0xa08f3324dc69187dL;
private transient Triple triple;

public STriple(Triple triple) { this.triple = triple; }
public Triple getTriple() { return triple; }

private void writeObject(java.io.ObjectOutputStream out) throws IOException {
TProtocol protocol = TRDF.protocol(out);
RDF_Term tterm = new RDF_Term();
SerializerRDF.write(protocol, tterm, triple.getSubject());
SerializerRDF.write(protocol, tterm, triple.getPredicate());
SerializerRDF.write(protocol, tterm, triple.getObject());
TRDF.flush(protocol);
}

private void readObject(java.io.ObjectInputStream in) throws IOException {
TProtocol protocol = TRDF.protocol(in);
RDF_Term tterm = new RDF_Term();
Node s = SerializerRDF.read(protocol, tterm);
Node p = SerializerRDF.read(protocol, tterm);
Node o = SerializerRDF.read(protocol, tterm);
triple = Triple.create(s, p, o);
}

Object readResolve() throws ObjectStreamException
{ return triple; }
}
58 changes: 58 additions & 0 deletions jena-arq/src/main/java/org/apache/jena/riot/system/Serializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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.jena.riot.system;

import java.io.Serializable;
import java.util.function.Function;

import org.apache.jena.sparql.core.Quad;

/** The injection point for the Quad {@link Serializable} process.
* This class is public to allow system initialization to inject
* handler functions for {@link Quad}.
*
* See also {@code Node} and {@ocde Triple}.
*/
public class Serializer {
/*package*/ static Function<Quad, Object> quadWriteReplaceFunction = null;

/** Set the quad serializer replacement function.
* This is a function called by {@code Triple.writeReplace} during the {@link Serializable} process.
* The return is an object used in place of {@link Quad} for the serialization.
*
* <PRE>
* ANY-ACCESS-MODIFIER Object writeReplace() throws ObjectStreamException;
* </PRE><p>
* The returned object must provide
* <PRE>
* ANY-ACCESS-MODIFIER Object readResolve() throws ObjectStreamException;
* </PRE><p>
* where "Object" is a {@link Quad}.
*
* @see java.io.Serializable
*/
public static void setQuadSerializer(Function<Quad, Object> writeReplaceFunction) {
quadWriteReplaceFunction = writeReplaceFunction;
}

/** Return the current triple serializer replacement function. */
public static Function<Quad, Object> getQuadSerializer() {
return quadWriteReplaceFunction;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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.jena.riot.system;

import org.apache.jena.graph.Node;
import org.apache.jena.riot.thrift.TRDF;
import org.apache.jena.riot.thrift.ThriftConvert;
import org.apache.jena.riot.thrift.wire.RDF_Term;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TProtocol;

public class SerializerRDF {

public static void init() {
org.apache.jena.system.Serializer.setNodeSerializer(SNode::new);
org.apache.jena.system.Serializer.setTripleSerializer(STriple::new);
org.apache.jena.riot.system.Serializer.setQuadSerializer(SQuad::new);
}

/* <PRE>
* ANY-ACCESS-MODIFIER Object writeReplace() throws ObjectStreamException;
* </PRE><p>
*
* This writeReplace method is invoked by serialization if the method
* exists and it would be accessible from a method defined within the
* class of the object being serialized. Thus, the method can have private,
* protected and package-private access. Subclass access to this method
* follows java accessibility rules. <p>
*
* Classes that need to designate a replacement when an instance of it
* is read from the stream should implement this special method with the
* exact signature.
*
* <PRE>
* ANY-ACCESS-MODIFIER Object readResolve() throws ObjectStreamException;
* </PRE><p>
*/

static Node read(TProtocol protocol, RDF_Term tterm) {
tterm.clear();
try { tterm.read(protocol); }
catch (TException e) { TRDF.exception(e); }
return ThriftConvert.convert(tterm);
}

// For now - no prefix map, no value encoding.
// The benefit unclear, sometimes even a bit slower (few percent).
private static final PrefixMap pmap = null;
private static final boolean encodeValues = false;

static void write(TProtocol protocol, RDF_Term tterm, Node node) {
tterm.clear();
ThriftConvert.toThrift(node, pmap, tterm, encodeValues);
try { tterm.write(protocol); }
catch (TException e) { TRDF.exception(e); }
}
}
25 changes: 23 additions & 2 deletions jena-arq/src/main/java/org/apache/jena/sparql/core/Quad.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@

package org.apache.jena.sparql.core;

import java.io.IOException;
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.util.Objects;
import java.util.function.Function;

import org.apache.jena.graph.Node ;
import org.apache.jena.graph.NodeFactory ;
import org.apache.jena.graph.Triple ;
import org.apache.jena.riot.system.Serializer;

public class Quad
{
public class Quad implements Serializable
{
// Create QuadNames? GraphNames?

/** Name of the default for explict use in GRAPH */
Expand Down Expand Up @@ -163,6 +168,22 @@ public boolean isLegalAsData()
return true ;
}

// ---- Serializable
protected Object writeReplace() throws ObjectStreamException {
Function<Quad, Object> function = Serializer.getQuadSerializer() ;
if ( function == null )
throw new IllegalStateException("Function for Quad.writeReplace not set") ;
return function.apply(this);
}
// Any attempt to serialize without replacement is an error.
private void writeObject(java.io.ObjectOutputStream out) throws IOException {
throw new IllegalStateException();
}
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
throw new IllegalStateException();
}
// ---- Serializable

@Override
public int hashCode()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
, TestIO_JenaWriters.class
, TestLangRegistration.class
, TestFormatRegistration.class
, TestJsonLDReadWrite.class // Some simple testing of the jsonld-java engine.
, TestJsonLDReadWrite.class // Some simple testing of the jsonld-java engine.
, TestSerializable.class

// May be subject to performance vagaries, with the improvements made
// to the fast implementation this should be fairly safe
Expand Down
Loading