Author: andy
Date: Sun Aug 31 09:34:40 2014
New Revision: 1621573
URL: http://svn.apache.org/r1621573
Log:
JENA-774 : RDF Thrift implementation for Jena
Added:
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/BinRDF.java
(with props)
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/Binding2Thrift.java
(with props)
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/IteratorThriftRDF.java
(with props)
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/RiotThriftException.java
(with props)
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/StreamRDF2Thrift.java
(with props)
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/StreamRowTRDFPrinter.java
(with props)
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/TRDF.java
(with props)
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/Thift2Binding.java
(with props)
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/Thrift2StreamRDF.java
(with props)
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/ThriftConvert.java
(with props)
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/VisitorStreamRowTRDF.java
(with props)
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/WriterDatasetThrift.java
(with props)
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/WriterGraphThrift.java
(with props)
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/wire/
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/wire/RDF_ANY.java
(with props)
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/wire/RDF_BNode.java
(with props)
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/wire/RDF_DataTuple.java
(with props)
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/wire/RDF_Decimal.java
(with props)
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/wire/RDF_IRI.java
(with props)
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/wire/RDF_Literal.java
(with props)
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/wire/RDF_PrefixDecl.java
(with props)
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/wire/RDF_PrefixName.java
(with props)
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/wire/RDF_Quad.java
(with props)
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/wire/RDF_REPEAT.java
(with props)
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/wire/RDF_StreamRow.java
(with props)
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/wire/RDF_Term.java
(with props)
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/wire/RDF_Triple.java
(with props)
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/wire/RDF_UNDEF.java
(with props)
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/wire/RDF_VAR.java
(with props)
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/wire/RDF_VarTuple.java
(with props)
jena/trunk/jena-arq/src/main/java/riotcmd/dumpthrift.java (with props)
jena/trunk/jena-arq/src/test/java/org/apache/jena/riot/thrift/
jena/trunk/jena-arq/src/test/java/org/apache/jena/riot/thrift/TS_RDFThrift.java
(with props)
jena/trunk/jena-arq/src/test/java/org/apache/jena/riot/thrift/TestResultSetThrift.java
(with props)
jena/trunk/jena-arq/src/test/java/org/apache/jena/riot/thrift/TestStreamRDFThrift.java
(with props)
jena/trunk/jena-arq/src/test/java/org/apache/jena/riot/thrift/TestThriftSetup.java
(with props)
jena/trunk/jena-arq/src/test/java/org/apache/jena/riot/thrift/TestThriftTerm.java
(with props)
Modified:
jena/trunk/jena-arq/pom.xml
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/Lang.java
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/RDFFormat.java
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/RDFLanguages.java
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/RDFParserRegistry.java
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/RDFWriterRegistry.java
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetReaderRegistry.java
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetWriterRegistry.java
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/system/StreamRDFWriter.java
jena/trunk/jena-arq/src/test/java/org/apache/jena/riot/TC_Riot.java
Modified: jena/trunk/jena-arq/pom.xml
URL:
http://svn.apache.org/viewvc/jena/trunk/jena-arq/pom.xml?rev=1621573&r1=1621572&r2=1621573&view=diff
==============================================================================
--- jena/trunk/jena-arq/pom.xml (original)
+++ jena/trunk/jena-arq/pom.xml Sun Aug 31 09:34:40 2014
@@ -87,6 +87,12 @@
<artifactId>httpclient-cache</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.apache.thrift</groupId>
+ <artifactId>libthrift</artifactId>
+ <version>0.9.1</version>
+ </dependency>
+
<!--
Intercept any uses of Jakarta Commons Logging
e.g. Apache Common HTTP client.
Modified: jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/Lang.java
URL:
http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/Lang.java?rev=1621573&r1=1621572&r2=1621573&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/Lang.java (original)
+++ jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/Lang.java Sun Aug 31
09:34:40 2014
@@ -78,6 +78,9 @@ public class Lang
/** Alternative constant {@linkplain #NQUADS} */
public static Lang NQ ;
+ //** The RDF syntax "RDF Thrift" : see
http://jena.apache.org/documentation/io */
+ public static Lang THRIFT ;
+
/** "CSV" - CSV data read into an RDF model with simple conversion : See
Jena-625 */
public static Lang CSV ;
Modified: jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/RDFFormat.java
URL:
http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/RDFFormat.java?rev=1621573&r1=1621572&r2=1621573&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/RDFFormat.java
(original)
+++ jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/RDFFormat.java Sun
Aug 31 09:34:40 2014
@@ -18,6 +18,7 @@
package org.apache.jena.riot ;
+import static org.apache.jena.riot.RDFLanguages.THRIFT ;
import org.apache.jena.atlas.lib.Lib ;
/** Constants for writable formats */
@@ -73,6 +74,32 @@ public class RDFFormat {
public static RDFFormat RDFXML_PLAIN = new RDFFormat(Lang.RDFXML,
PLAIN) ;
public static RDFFormat RDFJSON = new
RDFFormat(Lang.RDFJSON) ;
+
+
+ /** Variant for Thrift using values */
+ public static final RDFFormatVariant ValueEncoding = new
RDFFormatVariant("Value") ;
+
+ /**
+ * RDF Thrift output. This format is failful representation of RDF writtern
+ * and it is suitable for database dumps. It does not encode numeric
+ * literals as values (see {@linkplain #RDF_THRIFT_VALUES}).
+ *
+ * @see #RDF_THRIFT_VALUES
+ */
+
+ public static final RDFFormat RDF_THRIFT = new RDFFormat(THRIFT) ;
+ /**
+ * A variant of an an RDFFormat that uses value encoding (e.g. integers,
+ * doubles, decimals as binary). This does not preserve exact represenation
+ * (+001 is the same value as 1, +1 and 001) which may matter for database
+ * dumps. It looses datatype for derived types (xsd;long, xsd:int,
xsd:short
+ * and xsd:byte become xsd:integer).
+ * For large volumes of numeric data, it may provide a significant
reduction in size
+ * in combination with using prefixes for subjects and predicates.
+ *
+ * @see #RDF_THRIFT
+ */
+ public static final RDFFormat RDF_THRIFT_VALUES = new RDFFormat(THRIFT,
ValueEncoding) ;
/**
* The "null" output format (a sink that prints nothing, usually quite
Modified:
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/RDFLanguages.java
URL:
http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/RDFLanguages.java?rev=1621573&r1=1621572&r2=1621573&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/RDFLanguages.java
(original)
+++ jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/RDFLanguages.java
Sun Aug 31 09:34:40 2014
@@ -118,12 +118,18 @@ public class RDFLanguages
/** Alternative constant {@linkplain #NQUADS} */
public static final Lang NQ = NQUADS ;
- /** CSV - CSV data read into an RDF model with simple conversion : See
Jena-625 */
+ /** CSV data. This can be read into an RDF model with simple conversion */
public static final Lang CSV = LangBuilder.create(strLangCSV,
contentTypeTextCSV)
.addAltNames("csv")
.addFileExtensions("csv")
.build() ;
+ /** The RDF syntax "RDF Thrift" : see
http://jena.apache.org/documentation/io */
+ public static final Lang THRIFT = LangBuilder.create("RDF_THRIFT",
"application/rdf+thrift")
+ .addAltNames("RDF-THRIFT",
"TRDF")
+ .addFileExtensions("rt",
"trdf")
+ .build() ;
+
/** Text */
public static final Lang TEXT = LangBuilder.create("text",
contentTypeTextPlain)
.addAltNames("TEXT")
@@ -166,7 +172,8 @@ public class RDFLanguages
Lang.RDFJSON = RDFLanguages.RDFJSON ;
Lang.NQUADS = RDFLanguages.NQUADS ;
Lang.NQ = RDFLanguages.NQ ;
- Lang.TRIG = RDFLanguages.TRIG ;
+ Lang.TRIG = RDFLanguages.TRIG ;
+ Lang.THRIFT = RDFLanguages.THRIFT ;
Lang.CSV = RDFLanguages.CSV ;
Lang.RDFNULL = RDFLanguages.RDFNULL ;
}
@@ -183,6 +190,7 @@ public class RDFLanguages
register(RDFJSON) ;
register(TRIG) ;
register(NQUADS) ;
+ register(THRIFT) ;
register(CSV) ;
register(RDFNULL) ;
Modified:
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/RDFParserRegistry.java
URL:
http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/RDFParserRegistry.java?rev=1621573&r1=1621572&r2=1621573&view=diff
==============================================================================
---
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/RDFParserRegistry.java
(original)
+++
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/RDFParserRegistry.java
Sun Aug 31 09:34:40 2014
@@ -18,7 +18,16 @@
package org.apache.jena.riot;
-import static org.apache.jena.riot.RDFLanguages.* ;
+import static org.apache.jena.riot.RDFLanguages.CSV ;
+import static org.apache.jena.riot.RDFLanguages.JSONLD ;
+import static org.apache.jena.riot.RDFLanguages.N3 ;
+import static org.apache.jena.riot.RDFLanguages.NQUADS ;
+import static org.apache.jena.riot.RDFLanguages.NTRIPLES ;
+import static org.apache.jena.riot.RDFLanguages.RDFJSON ;
+import static org.apache.jena.riot.RDFLanguages.RDFXML ;
+import static org.apache.jena.riot.RDFLanguages.THRIFT ;
+import static org.apache.jena.riot.RDFLanguages.TRIG ;
+import static org.apache.jena.riot.RDFLanguages.TURTLE ;
import java.io.InputStream ;
import java.io.Reader ;
@@ -34,6 +43,7 @@ import org.apache.jena.riot.system.Error
import org.apache.jena.riot.system.ErrorHandlerFactory ;
import org.apache.jena.riot.system.ParserProfile ;
import org.apache.jena.riot.system.StreamRDF ;
+import org.apache.jena.riot.thrift.BinRDF ;
import com.hp.hpl.jena.sparql.util.Context ;
//import org.apache.jena.atlas.lib.Sink ;
@@ -61,8 +71,9 @@ public class RDFParserRegistry
private static Set<Lang> langQuads = DS.set() ;
/** Generic parser factory. */
- private static ReaderRIOTFactory parserFactory = new
ReaderRIOTFactoryImpl() ;
- private static ReaderRIOTFactory parserFactoryJsonLD = new
ReaderRIOTFactoryJSONLD() ;
+ private static ReaderRIOTFactory parserFactory = new
ReaderRIOTFactoryImpl() ;
+ private static ReaderRIOTFactory parserFactoryJsonLD = new
ReaderRIOTFactoryJSONLD() ;
+ private static ReaderRIOTFactory parserFactoryThrift = new
ReaderRIOTFactoryThrift() ;
private static boolean initialized = false ;
static { init() ; }
@@ -85,10 +96,16 @@ public class RDFParserRegistry
registerLangTriples(JSONLD, parserFactoryJsonLD) ;
registerLangTriples(RDFJSON, parserFactory) ;
registerLangTriples(CSV, parserFactory) ;
+ registerLangTriples(THRIFT, parserFactoryThrift) ;
registerLangQuads(JSONLD, parserFactoryJsonLD) ;
registerLangQuads(NQUADS, parserFactory) ;
registerLangQuads(TRIG, parserFactory) ;
+ registerLangQuads(THRIFT, parserFactoryThrift) ;
+
+
+
+ //registerLangQuads(THRIFT, parserFactoryThrift) ;
}
/** Register a language and it's parser factory.
@@ -193,5 +210,43 @@ public class RDFParserRegistry
return new JsonLDReader() ;
}
}
+ // ---- reader
+ // to StreamRDF
+ private static class ReaderRIOTFactoryThrift implements ReaderRIOTFactory {
+ @Override
+ public ReaderRIOT create(Lang language) {
+ return new ReaderRIOT_RDFBin() ;
+ }}
+
+ // XXX Where? ==> lang
+ private static class ReaderRIOT_RDFBin implements ReaderRIOT {
+ @Override
+ public void read(InputStream in, String baseURI, ContentType ct,
StreamRDF output, Context context) {
+ BinRDF.inputStreamToStream(in, output) ;
+ }
+
+ @Override
+ public void read(Reader reader, String baseURI, ContentType ct,
StreamRDF output, Context context) {
+ throw new RiotException("RDF Thrift : Reading binary data from a
java.io.reader is not supported. Please use an InputStream") ;
+ }
+
+ @Override
+ public ErrorHandler getErrorHandler() {
+ return null ;
+ }
+
+ @Override
+ public void setErrorHandler(ErrorHandler errorHandler) {}
+
+ @Override
+ public ParserProfile getParserProfile() {
+ return null ;
+ }
+
+ @Override
+ public void setParserProfile(ParserProfile profile) {}
+
+ }
+
}
Modified:
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/RDFWriterRegistry.java
URL:
http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/RDFWriterRegistry.java?rev=1621573&r1=1621572&r2=1621573&view=diff
==============================================================================
---
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/RDFWriterRegistry.java
(original)
+++
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/RDFWriterRegistry.java
Sun Aug 31 09:34:40 2014
@@ -24,12 +24,11 @@ import java.util.HashMap ;
import java.util.Map ;
import org.apache.jena.atlas.lib.Lib ;
-import org.apache.jena.riot.Lang ;
-import org.apache.jena.riot.RIOT ;
-import org.apache.jena.riot.RiotException ;
import org.apache.jena.riot.out.CharSpace ;
import org.apache.jena.riot.out.JsonLDWriter ;
import org.apache.jena.riot.system.RiotLib ;
+import org.apache.jena.riot.thrift.WriterDatasetThrift ;
+import org.apache.jena.riot.thrift.WriterGraphThrift ;
import org.apache.jena.riot.writer.* ;
public class RDFWriterRegistry
@@ -113,6 +112,20 @@ public class RDFWriterRegistry
}
} ;
+ static WriterGraphRIOTFactory wgThriftFactory = new
WriterGraphRIOTFactory(){
+ @Override
+ public WriterGraphRIOT create(RDFFormat syntaxForm) {
+ return new WriterGraphThrift(syntaxForm) ;
+ }
+ } ;
+
+ static WriterDatasetRIOTFactory wdsThriftFactory = new
WriterDatasetRIOTFactory(){
+ @Override
+ public WriterDatasetRIOT create(RDFFormat syntaxForm) {
+ return new WriterDatasetThrift(syntaxForm) ;
+ }
+ } ;
+
public static void init() {}
static { init$() ; }
private static void init$()
@@ -129,6 +142,7 @@ public class RDFWriterRegistry
register(Lang.TRIG, RDFFormat.TRIG) ;
register(Lang.NQUADS, RDFFormat.NQUADS) ;
register(Lang.RDFNULL, RDFFormat.RDFNULL) ;
+ register(Lang.THRIFT, RDFFormat.RDF_THRIFT) ;
// Writer factories.
register(RDFFormat.TURTLE_PRETTY, wgfactory) ;
@@ -156,6 +170,9 @@ public class RDFWriterRegistry
register(RDFFormat.NQUADS_ASCII, wgfactory) ;
register(RDFFormat.RDFNULL, wgfactory) ;
+ register(RDFFormat.RDF_THRIFT, wgThriftFactory) ;
+ register(RDFFormat.RDF_THRIFT_VALUES, wgThriftFactory) ;
+
// Datasets
register(RDFFormat.TRIG_PRETTY, wdsfactory) ;
register(RDFFormat.TRIG_BLOCKS, wdsfactory) ;
@@ -168,6 +185,10 @@ public class RDFWriterRegistry
register(RDFFormat.JSONLD, wdsJsonldfactory) ;
register(RDFFormat.JSONLD_FLAT, wdsJsonldfactory) ;
register(RDFFormat.JSONLD_PRETTY, wdsJsonldfactory) ;
+
+ register(RDFFormat.RDF_THRIFT, wdsThriftFactory) ;
+ register(RDFFormat.RDF_THRIFT_VALUES, wdsThriftFactory) ;
+
}
/** Register the serialization for graphs and it's associated factory
Modified:
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetReaderRegistry.java
URL:
http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetReaderRegistry.java?rev=1621573&r1=1621572&r2=1621573&view=diff
==============================================================================
---
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetReaderRegistry.java
(original)
+++
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetReaderRegistry.java
Sun Aug 31 09:34:40 2014
@@ -33,6 +33,7 @@ import java.util.Objects ;
import org.apache.jena.atlas.lib.NotImplemented ;
import org.apache.jena.riot.Lang ;
import org.apache.jena.riot.RiotException ;
+import org.apache.jena.riot.thrift.BinRDF ;
import com.hp.hpl.jena.query.ResultSet ;
import com.hp.hpl.jena.sparql.resultset.CSVInput ;
@@ -65,10 +66,10 @@ public class ResultSetReaderRegistry {
initialized = true ;
ResultSetReaderFactory factory = new ResultSetReaderFactoryStd() ;
- register(SPARQLResultSetXML, factory) ;
- register(SPARQLResultSetJSON, factory) ;
- register(SPARQLResultSetCSV, factory) ;
- register(SPARQLResultSetTSV, factory) ;
+ register(SPARQLResultSetXML, factory) ;
+ register(SPARQLResultSetJSON, factory) ;
+ register(SPARQLResultSetCSV, factory) ;
+ register(SPARQLResultSetTSV, factory) ;
register(SPARQLResultSetThrift, factory) ;
}
@@ -82,10 +83,28 @@ public class ResultSetReaderRegistry {
if ( lang.equals(SPARQLResultSetJSON) ) return readerJSON ;
if ( lang.equals(SPARQLResultSetCSV) ) return readerCSV ;
if ( lang.equals(SPARQLResultSetTSV) ) return readerTSV ;
+ if ( lang.equals(SPARQLResultSetThrift) ) return readerThrift ;
throw new RiotException("Lang not registered (ResultSet reader)") ;
}
}
+ private static class ResultSetReaderThriftFactory implements
ResultSetReaderFactory {
+ @Override
+ public ResultSetReader create(Lang lang) {
+ return new ResultSetReader() {
+ @Override
+ public ResultSet read(InputStream in, Context context) {
+ return BinRDF.readResultSet(in) ;
+ }
+ @Override
+ public ResultSet read(Reader in, Context context) {
+ throw new NotImplemented("Reading binary data from a
java.io.Reader is not possible") ;
+ }} ;
+ }
+ }
+ // These all call static methods, so have no state and so don't
+ // need to be created for each read operation.
+
private static ResultSetReader readerXML = new ResultSetReader() {
@Override public ResultSet read(InputStream in, Context context) {
return XMLInput.fromXML(in); }
@Override public ResultSet read(Reader in, Context context) {
return XMLInput.fromXML(in); }
@@ -106,8 +125,17 @@ public class ResultSetReaderRegistry {
@Override public ResultSet read(Reader in, Context context) {
throw new NotImplemented("Reader") ; }
} ;
+ private static ResultSetReader readerThrift = new ResultSetReader() {
+ @Override public ResultSet read(InputStream in, Context context) {
return BinRDF.readResultSet(in) ;}
+ @Override public ResultSet read(Reader in, Context context) {
+ throw new NotImplemented("Reading binary data from a
java.io.Reader is not possible") ;
+ }
+ } ;
+
private static ResultSetReader readerNo = new ResultSetReader() {
@Override public ResultSet read(InputStream in, Context context) {
return null ; }
@Override public ResultSet read(Reader in, Context context) {
return null ; }
} ;
+
+
}
Modified:
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetWriterRegistry.java
URL:
http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetWriterRegistry.java?rev=1621573&r1=1621572&r2=1621573&view=diff
==============================================================================
---
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetWriterRegistry.java
(original)
+++
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/resultset/ResultSetWriterRegistry.java
Sun Aug 31 09:34:40 2014
@@ -34,6 +34,7 @@ import java.util.Objects ;
import org.apache.jena.atlas.lib.NotImplemented ;
import org.apache.jena.riot.Lang ;
import org.apache.jena.riot.RiotException ;
+import org.apache.jena.riot.thrift.BinRDF ;
import com.hp.hpl.jena.query.ResultSet ;
import com.hp.hpl.jena.sparql.core.Prologue ;
@@ -139,5 +140,22 @@ public class ResultSetWriterRegistry {
throw new RiotException("Lang not registered (ResultSet writer)") ;
}
}
+
+ private static class ResultSetWriterThriftFactory implements
ResultSetWriterFactory {
+ @Override
+ public ResultSetWriter create(Lang lang) {
+ return new ResultSetWriter() {
+ @Override
+ public void write(OutputStream out, ResultSet resultSet,
Context context)
+ { BinRDF.writeResultSet(out, resultSet) ; }
+
+ @Override
+ public void write(Writer out, ResultSet resultSet, Context
context) {
+ throw new NotImplemented("Writing binary data to a
java.io.Writer is not possible") ;
+
+ }
+ } ;
+ }
+ }
}
Modified:
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/system/StreamRDFWriter.java
URL:
http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/system/StreamRDFWriter.java?rev=1621573&r1=1621572&r2=1621573&view=diff
==============================================================================
---
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/system/StreamRDFWriter.java
(original)
+++
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/system/StreamRDFWriter.java
Sun Aug 31 09:34:40 2014
@@ -28,6 +28,7 @@ import org.apache.jena.atlas.io.AWriter
import org.apache.jena.atlas.io.IO ;
import org.apache.jena.riot.* ;
import org.apache.jena.riot.out.CharSpace ;
+import org.apache.jena.riot.thrift.BinRDF ;
import org.apache.jena.riot.writer.WriterStreamRDFBlocks ;
import org.apache.jena.riot.writer.WriterStreamRDFFlat ;
import org.apache.jena.riot.writer.WriterStreamRDFPlain ;
@@ -76,13 +77,13 @@ public class StreamRDFWriter {
}
} ;
-// private static StreamWriterFactory streamWriterFactoryThrift = new
StreamWriterFactory() {
-// @Override
-// public StreamRDF create(OutputStream output, RDFFormat format) {
-// boolean withValues = JenaThrift.RDF_THRIFT_VALUES.equals(format)
;
-// return BinRDF.streamToOutputStream(output, withValues) ;
-// }
-// } ;
+ private static StreamRDFWriterFactory streamWriterFactoryThrift = new
StreamRDFWriterFactory() {
+ @Override
+ public StreamRDF create(OutputStream output, RDFFormat format) {
+ boolean withValues = RDFFormat.RDF_THRIFT_VALUES.equals(format) ;
+ return BinRDF.streamToOutputStream(output, withValues) ;
+ }
+ } ;
private static WriterRegistry<StreamRDFWriterFactory> registry = new
WriterRegistry<>() ;
@@ -112,7 +113,7 @@ public class StreamRDFWriter {
register(Lang.TRIG, RDFFormat.TRIG_BLOCKS) ;
register(Lang.NTRIPLES, RDFFormat.NTRIPLES) ;
register(Lang.NQUADS, RDFFormat.NQUADS) ;
-// register(Lang.THRIFT, RDFFormat.RDF_THRIFT) ;
+ register(Lang.THRIFT, RDFFormat.RDF_THRIFT) ;
register(RDFFormat.TURTLE_BLOCKS, streamWriterFactoryBlocks) ;
register(RDFFormat.TURTLE_FLAT, streamWriterFactoryFlat) ;
@@ -127,8 +128,8 @@ public class StreamRDFWriter {
register(RDFFormat.NQUADS_UTF8, streamWriterFactoryTriplesQuads) ;
register(RDFFormat.NQUADS_ASCII,
streamWriterFactoryTriplesQuadsAscii) ;
-// register(JenaThrift.RDF_THRIFT, streamWriterFactoryThrift) ;
-// register(JenaThrift.RDF_THRIFT_VALUES, streamWriterFactoryThrift) ;
+ register(RDFFormat.RDF_THRIFT, streamWriterFactoryThrift) ;
+ register(RDFFormat.RDF_THRIFT_VALUES, streamWriterFactoryThrift) ;
}
/** Get a StreamRDF destination that will output in syntax <tt>Lang</tt>
Added: jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/BinRDF.java
URL:
http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/BinRDF.java?rev=1621573&view=auto
==============================================================================
--- jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/BinRDF.java
(added)
+++ jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/BinRDF.java
Sun Aug 31 09:34:40 2014
@@ -0,0 +1,251 @@
+/**
+ * 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.thrift;
+
+import java.io.BufferedOutputStream ;
+import java.io.InputStream ;
+import java.io.OutputStream ;
+import java.util.List ;
+
+import org.apache.jena.atlas.io.IO ;
+import org.apache.jena.atlas.io.IndentedWriter ;
+import org.apache.jena.atlas.iterator.Action ;
+import org.apache.jena.riot.system.PrefixMap ;
+import org.apache.jena.riot.system.PrefixMapFactory ;
+import org.apache.jena.riot.system.StreamRDF ;
+import org.apache.jena.riot.thrift.wire.RDF_StreamRow ;
+import org.apache.thrift.TException ;
+import org.apache.thrift.protocol.TProtocol ;
+import org.apache.thrift.transport.TTransportException ;
+
+import com.hp.hpl.jena.query.ResultSet ;
+import com.hp.hpl.jena.sparql.core.Var ;
+import com.hp.hpl.jena.sparql.engine.ResultSetStream ;
+import com.hp.hpl.jena.sparql.engine.binding.Binding ;
+
+/** Operations on binary RDF (which uses <a
href="http://thrift.apache.org/">Apache Thrift</a>).
+ * See also {@linkplain ThriftConvert}, for specific functions on binary RDF.
+ * @See ThriftConvert
+ */
+public class BinRDF {
+
+ private static int BUFSIZE_IN = 128*1024 ;
+ private static int BUFSIZE_OUT = 128*1024 ;
+
+ /**
+ * Create an {@linkplain StreamRDF} for output. A filename ending {@code
.gz} will have
+ * a gzip compressor added to the output path. A filename of "-" is {@code
System.out}.
+ * The file is closed when {@linkplain StreamRDF#finish()} is called
unless it is {@code System.out}.
+ * Call {@linkplain StreamRDF#start()}...{@linkplain StreamRDF#finish()}.
+ *
+ * @param filename The file
+ * @return StreamRDF A stream to send to.
+ */
+
+ public static StreamRDF streamToFile(String filename) {
+ return streamToFile(filename, false) ;
+ }
+
+ /**
+ * Create an {@linkplain StreamRDF} for output. A filename ending {@code
.gz} will have
+ * a gzip compressor added to the output path. A filename of "-" is {@code
System.out}.
+ * The file is closed when {@linkplain StreamRDF#finish()} is called
unless it is {@code System.out}.
+ * Call {@linkplain StreamRDF#start()}...{@linkplain StreamRDF#finish()}.
+ *
+ * @param filename The file
+ * @param withValues - whether to encode numeric values as values.
+ * @return StreamRDF A stream to send to.
+ */
+ public static StreamRDF streamToFile(String filename, boolean withValues) {
+ OutputStream out = IO.openOutputFile(filename) ;
+ // Is this internally buffered as well?
+ BufferedOutputStream bout = new BufferedOutputStream(out, BUFSIZE_OUT)
;
+ TProtocol protocol = TRDF.protocol(bout) ;
+ return new StreamRDF2Thrift(protocol, withValues) ;
+ }
+
+ /**
+ * Create an {@linkplain StreamRDF} for output.
+ * The {@code OutputStream} is closed when {@linkplain StreamRDF#finish()}
is called unless it is {@code System.out}.
+ * Call {@linkplain StreamRDF#start()}...{@linkplain StreamRDF#finish()}.
+ * @param out OutputStream
+ * @return StreamRDF A stream to send to.
+ */
+ public static StreamRDF streamToOutputStream(OutputStream out) {
+ return streamToOutputStream(out, false) ;
+ }
+
+ /**
+ * Create an {@linkplain StreamRDF} for output.
+ * The {@code OutputStream} is closed when {@linkplain StreamRDF#finish()}
is called unless it is {@code System.out}.
+ * Call {@linkplain StreamRDF#start()}...{@linkplain StreamRDF#finish()}.
+ * @param out OutputStream
+ * @param withValues - whether to encode numeric values as values.
+ * @return StreamRDF A stream to send to.
+ */
+ public static StreamRDF streamToOutputStream(OutputStream out, boolean
withValues) {
+ return new StreamRDF2Thrift(out, withValues) ;
+ }
+
+ /**
+ * Create an {@linkplain StreamRDF} for output.
+ * The {@code OutputStream} is closed when {@linkplain StreamRDF#finish()}
is called unless it is {@code System.out}.
+ * Call {@linkplain StreamRDF#start()}...{@linkplain StreamRDF#finish()}.
+ * @param protocol Output and encoding.
+ * @return StreamRDF A stream to send to.
+ */
+ public static StreamRDF streamToTProtocol(TProtocol protocol) {
+ return streamToTProtocol(protocol, false) ;
+ }
+
+ /**
+ * Create an {@linkplain StreamRDF} for output.
+ * The {@code OutputStream} is closed when {@linkplain StreamRDF#finish()}
is called unless it is {@code System.out}.
+ * Call {@linkplain StreamRDF#start()}...{@linkplain StreamRDF#finish()}.
+ * @param protocol Output and encoding.
+ * @param withValues - whether to encode numeric values as values.
+ * @return StreamRDF A stream to send to.
+ */
+ public static StreamRDF streamToTProtocol(TProtocol protocol, boolean
withValues) {
+ return new StreamRDF2Thrift(protocol, withValues) ;
+ }
+
+ /**
+ * Decode the contents of the file and send to the {@linkplain StreamRDF}.
+ * A filename ending {@code .gz} will have a gzip decompressor added.
+ * A filename of "-" is {@code System.in}.
+ * @param filename The file.
+ * @param dest Sink
+ */
+ public static void fileToStream(String filename, StreamRDF dest) {
+ InputStream in = IO.openFile(filename) ;
+ TProtocol protocol = TRDF.protocol(in) ;
+ protocolToStream(protocol, dest) ;
+ }
+
+ /**
+ * Decode the contents of the input stream and send to the {@linkplain
StreamRDF}.
+ * @param in InputStream
+ * @param dest StreamRDF
+ */
+ public static void inputStreamToStream(InputStream in, StreamRDF dest) {
+ TProtocol protocol = TRDF.protocol(in) ;
+ protocolToStream(protocol, dest) ;
+ }
+
+ /**
+ * Decode the contents of the TProtocol and send to the {@linkplain
StreamRDF}.
+ * @param protocol TProtocol
+ * @param dest Sink
+ */
+ public static void protocolToStream(TProtocol protocol, StreamRDF dest) {
+ PrefixMap pmap = PrefixMapFactory.create() ;
+ final Thrift2StreamRDF s = new Thrift2StreamRDF(pmap, dest) ;
+ dest.start() ;
+ // ** Java8
+ //apply(protocol, z -> TRDF.visit(z, s)) ;
+
+ applyVisitor(protocol, s) ;
+
+ dest.finish() ;
+ // No need to flush - we read from the protocol ;
+ }
+
+ // ** Java7 support
+ public static void applyVisitor(TProtocol protocol, final
VisitorStreamRowTRDF visitor) {
+ Action<RDF_StreamRow> action = new Action<RDF_StreamRow>() {
+ @Override
+ public void apply(RDF_StreamRow z) { TRDF.visit(z, visitor) ; }
+ } ;
+ apply(protocol, action) ;
+ }
+
+ /**
+ * Send the contents of a RDF-encoded Thrift file to an "action"
+ * @param protocol TProtocol
+ * @param action Code to act on the row.
+ */
+ public static void apply(TProtocol protocol, Action<RDF_StreamRow> action)
{
+ RDF_StreamRow row = new RDF_StreamRow() ;
+ while(protocol.getTransport().isOpen()) {
+ try { row.read(protocol) ; }
+ catch (TTransportException e) {
+ if ( e.getType() == TTransportException.END_OF_FILE )
+ break ;
+ }
+ catch (TException ex) { TRDF.exception(ex) ; }
+ action.apply(row) ;
+ row.clear() ;
+ }
+ }
+
+ /** Debug help - print details of a Thrift stream.
+ * Destructive on the InputStream.
+ * @param out OutputStream
+ * @param in InputStream
+ */
+ public static void dump(OutputStream out, InputStream in) {
+ IndentedWriter iOut = new IndentedWriter(out) ;
+ StreamRowTRDFPrinter printer = new StreamRowTRDFPrinter(iOut) ;
+ TProtocol protocol = TRDF.protocol(in) ;
+ BinRDF.applyVisitor(protocol, printer) ;
+ iOut.flush() ;
+ }
+
+
+ public static ResultSet readResultSet(InputStream in) {
+ return readResultSet(TRDF.protocol(in)) ;
+ }
+
+ public static ResultSet readResultSet(TProtocol protocol) {
+ Thift2Binding t2b = new Thift2Binding(protocol) ;
+ List<String> varsNames = Var.varNames(t2b.getVars()) ;
+ return new ResultSetStream(varsNames, null, t2b) ;
+ }
+
+ public static void writeResultSet(OutputStream out, ResultSet resultSet) {
+ writeResultSet(out, resultSet, false) ;
+ }
+
+ public static void writeResultSet(OutputStream out, ResultSet resultSet,
boolean withValues) {
+ BufferedOutputStream bout =
+ ( out instanceof BufferedOutputStream )
+ ? (BufferedOutputStream)out
+ : new BufferedOutputStream(out, TRDF.OutputBufferSize) ;
+ writeResultSet(TRDF.protocol(bout), resultSet, withValues) ;
+ IO.flush(out) ;
+ }
+
+ public static void writeResultSet(TProtocol protocol, ResultSet resultSet)
{
+ writeResultSet(protocol, resultSet, false) ;
+ }
+
+ public static void writeResultSet(TProtocol protocol, ResultSet resultSet,
boolean encodeValues) {
+ List<Var> vars = Var.varList(resultSet.getResultVars()) ;
+ try ( Binding2Thrift b2t = new Binding2Thrift(protocol, vars,
encodeValues) ) {
+ for ( ; resultSet.hasNext() ; ) {
+ Binding b = resultSet.nextBinding() ;
+ b2t.output(b) ;
+ }
+ }
+ //Done by Binding2Thrift.close() -- LibThriftRDF.flush(protocol) ;
+ }
+
+}
+
Propchange:
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/BinRDF.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/Binding2Thrift.java
URL:
http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/Binding2Thrift.java?rev=1621573&view=auto
==============================================================================
---
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/Binding2Thrift.java
(added)
+++
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/Binding2Thrift.java
Sun Aug 31 09:34:40 2014
@@ -0,0 +1,104 @@
+/**
+ * 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.thrift;
+
+import java.io.OutputStream ;
+import java.util.Collection ;
+import java.util.Iterator ;
+
+import org.apache.jena.riot.thrift.wire.RDF_DataTuple ;
+import org.apache.jena.riot.thrift.wire.RDF_Term ;
+import org.apache.jena.riot.thrift.wire.RDF_VAR ;
+import org.apache.jena.riot.thrift.wire.RDF_VarTuple ;
+import org.apache.thrift.TException ;
+import org.apache.thrift.protocol.TProtocol ;
+import org.apache.thrift.transport.TIOStreamTransport ;
+
+import com.hp.hpl.jena.graph.Node ;
+import com.hp.hpl.jena.sparql.core.Var ;
+import com.hp.hpl.jena.sparql.engine.binding.Binding ;
+
+/** Converted from Bindings to SPARQL result set encoded in Thrift */
+public class Binding2Thrift implements AutoCloseable {
+ private final RDF_DataTuple row = new RDF_DataTuple() ;
+ private final Collection<Var> vars ;
+ private final OutputStream out ;
+ private final TProtocol protocol ;
+ private final boolean encodeValues ;
+
+ public Binding2Thrift(OutputStream out, Collection<Var> vars, boolean
encodeValues) {
+ this.out = out ;
+ this.vars = vars ;
+ TIOStreamTransport transport = new TIOStreamTransport(out) ;
+ this.protocol = TRDF.protocol(transport) ;
+ this.encodeValues = encodeValues ;
+ varsRow() ;
+ }
+
+ private void varsRow() {
+ RDF_VarTuple vrow = new RDF_VarTuple() ;
+ // ** Java8
+// vars.iterator().forEachRemaining( v -> {
+// RDF_VAR rv = new RDF_VAR() ;
+// rv.setName(v.getName()) ;
+// vrow.addToVars(rv) ;
+// }) ;
+ for ( Var v : vars ) {
+ RDF_VAR rv = new RDF_VAR() ;
+ rv.setName(v.getName()) ;
+ vrow.addToVars(rv) ;
+ }
+ try { vrow.write(protocol) ; }
+ catch (TException e) { TRDF.exception(e) ; }
+ }
+
+ public Binding2Thrift(TProtocol out, Collection<Var> vars, boolean
encodeValues) {
+ this.vars = vars ;
+ this.out = null ;
+ this.protocol = out ;
+ this.encodeValues = encodeValues ;
+ varsRow() ;
+ }
+
+ public void output(Binding binding) {
+ Iterator<Var> vIter = (vars == null ? null : vars.iterator()) ;
+ if ( vIter == null )
+ vIter = binding.vars() ;
+ // ** Java8
+// vIter.forEachRemaining(v -> {
+// Node n = binding.get(v) ;
+// RDF_Term rt = ( n == null ) ? TRDF.tUNDEF :
ThriftConvert.convert(n) ;
+// row.addToRow(rt) ;
+// }) ;
+ while(vIter.hasNext()) {
+ Var v = vIter.next();
+ Node n = binding.get(v) ;
+ RDF_Term rt = ( n == null ) ? TRDF.tUNDEF :
ThriftConvert.convert(n, encodeValues) ;
+ row.addToRow(rt) ;
+ }
+ try { row.write(protocol) ; }
+ catch (TException e) { TRDF.exception(e) ; }
+ row.clear() ;
+ }
+
+ @Override
+ public void close() {
+ TRDF.flush(protocol) ;
+ }
+}
Propchange:
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/Binding2Thrift.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/IteratorThriftRDF.java
URL:
http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/IteratorThriftRDF.java?rev=1621573&view=auto
==============================================================================
---
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/IteratorThriftRDF.java
(added)
+++
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/IteratorThriftRDF.java
Sun Aug 31 09:34:40 2014
@@ -0,0 +1,71 @@
+/**
+ * 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.thrift;
+
+import java.util.Iterator ;
+
+import org.apache.jena.riot.system.* ;
+import org.apache.jena.riot.thrift.wire.RDF_StreamRow ;
+import org.apache.thrift.TException ;
+import org.apache.thrift.protocol.TProtocol ;
+import org.apache.thrift.transport.TTransportException ;
+
+/**
+ * Iterator over a Thrift-encoded RDF stream.
+ */
+public class IteratorThriftRDF extends IteratorStreamRDF implements
Iterator<StreamRowRDF> {
+
+ private final PrefixMap pmap = PrefixMapFactory.create() ;
+ private final StreamRDFCollectOne collector = new
StreamRDFCollectOne(pmap) ;
+ private final Thrift2StreamRDF converter = new Thrift2StreamRDF(pmap,
collector) ;
+
+ private final RDF_StreamRow row = new RDF_StreamRow() ;
+ private final TProtocol protocol ;
+ private StreamRDFCollectOne slot ;
+ private boolean finished = false ;
+
+ public IteratorThriftRDF(TProtocol protocol) {
+ this.protocol = protocol ;
+ }
+
+ @Override
+ protected boolean hasMore() {
+ return true ;
+ }
+
+ @Override
+ protected StreamRowRDF moveToNext() {
+ if ( ! protocol.getTransport().isOpen() )
+ return null ;
+
+ try { row.read(protocol) ; }
+ catch (TTransportException e) {
+ if ( e.getType() == TTransportException.END_OF_FILE )
+ return null ;
+ }
+ catch (TException ex) { TRDF.exception(ex) ; }
+
+ TRDF.visit(row, converter);
+
+ row.clear() ;
+ return collector.getRow() ;
+ }
+
+}
+
Propchange:
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/IteratorThriftRDF.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/RiotThriftException.java
URL:
http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/RiotThriftException.java?rev=1621573&view=auto
==============================================================================
---
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/RiotThriftException.java
(added)
+++
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/RiotThriftException.java
Sun Aug 31 09:34:40 2014
@@ -0,0 +1,29 @@
+/**
+ * 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.thrift;
+
+import org.apache.jena.riot.RiotException ;
+
+public class RiotThriftException extends RiotException {
+ public RiotThriftException() { super() ; }
+ public RiotThriftException(String msg) { super(msg) ; }
+ public RiotThriftException(Throwable th) { super(th) ; }
+ public RiotThriftException(String msg, Throwable th) { super(msg, th) ; }
+}
+
Propchange:
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/RiotThriftException.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/StreamRDF2Thrift.java
URL:
http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/StreamRDF2Thrift.java?rev=1621573&view=auto
==============================================================================
---
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/StreamRDF2Thrift.java
(added)
+++
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/StreamRDF2Thrift.java
Sun Aug 31 09:34:40 2014
@@ -0,0 +1,162 @@
+/**
+ * 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.thrift;
+
+import java.io.OutputStream ;
+
+import org.apache.jena.atlas.logging.Log ;
+import org.apache.jena.riot.RiotException ;
+import org.apache.jena.riot.system.PrefixMap ;
+import org.apache.jena.riot.system.PrefixMapFactory ;
+import org.apache.jena.riot.system.StreamRDF ;
+import org.apache.jena.riot.thrift.wire.* ;
+import org.apache.thrift.TException ;
+import org.apache.thrift.protocol.TProtocol ;
+
+import com.hp.hpl.jena.graph.Node ;
+import com.hp.hpl.jena.graph.Triple ;
+import com.hp.hpl.jena.sparql.core.Quad ;
+
+/** Encode StreamRDF in Thrift.
+ * Usually used via {@linkplain BinRDF} functions.
+ *
+ * @see Thrift2StreamRDF (for each RDF_StreamRow) for the reverse process.
+ */
+public class StreamRDF2Thrift implements StreamRDF, AutoCloseable
+{
+ // No REPEAT support.
+ private final OutputStream out ;
+ private final TProtocol protocol ;
+ private PrefixMap pmap = PrefixMapFactory.create() ;
+ private final boolean encodeValues ;
+
+// public StreamRDF2Thrift(OutputStream out) {
+// this(out, false) ;
+// }
+
+ public StreamRDF2Thrift(OutputStream out, boolean encodeValues) {
+ this.out = out ;
+ this.protocol = TRDF.protocol(out) ;
+ this.encodeValues = encodeValues ;
+ }
+
+// public StreamRDF2Thrift(TProtocol out) {
+// this(out, false) ;
+// }
+
+ public StreamRDF2Thrift(TProtocol out, boolean encodeValues) {
+ this.out = null ;
+ this.protocol = out ;
+ this.pmap = PrefixMapFactory.create() ;
+ this.encodeValues = encodeValues ;
+ }
+
+ @Override
+ public void start() { }
+
+ private final RDF_StreamRow tStreamRow = new RDF_StreamRow() ;
+
+ private final RDF_Triple ttriple = new RDF_Triple() ;
+ private final RDF_Quad tquad = new RDF_Quad() ;
+
+ private final RDF_Term tsubject = new RDF_Term() ;
+ private final RDF_Term tpredicate = new RDF_Term() ;
+ private final RDF_Term tobject = new RDF_Term() ;
+ private final RDF_Term tgraph = new RDF_Term() ;
+
+ @Override
+ public void triple(Triple triple) {
+ doTriple(triple.getSubject(), triple.getPredicate(),
triple.getObject()) ;
+ }
+
+ private void doTriple(Node subject, Node predicate, Node object) {
+ ThriftConvert.toThrift(subject, pmap, tsubject, encodeValues) ;
+ ThriftConvert.toThrift(predicate, pmap, tpredicate, encodeValues) ;
+ ThriftConvert.toThrift(object, pmap, tobject, encodeValues) ;
+ ttriple.setS(tsubject) ;
+ ttriple.setP(tpredicate) ;
+ ttriple.setO(tobject) ;
+
+ tStreamRow.setTriple(ttriple) ;
+ try { tStreamRow.write(protocol) ; }
+ catch (TException e) { TRDF.exception(e) ; }
+ tStreamRow.clear();
+ ttriple.clear();
+ tsubject.clear();
+ tpredicate.clear() ;
+ tobject.clear() ;
+ }
+
+ @Override
+ public void quad(Quad quad) {
+ if ( quad.getGraph() == null || quad.isDefaultGraph() ) {
+ doTriple(quad.getSubject(), quad.getPredicate(), quad.getObject())
;
+ return ;
+ }
+
+ ThriftConvert.toThrift(quad.getGraph(), pmap, tgraph, encodeValues) ;
+ ThriftConvert.toThrift(quad.getSubject(), pmap, tsubject,
encodeValues) ;
+ ThriftConvert.toThrift(quad.getPredicate(), pmap, tpredicate,
encodeValues) ;
+ ThriftConvert.toThrift(quad.getObject(), pmap, tobject, encodeValues) ;
+
+ tquad.setG(tgraph) ;
+ tquad.setS(tsubject) ;
+ tquad.setP(tpredicate) ;
+ tquad.setO(tobject) ;
+ tStreamRow.setQuad(tquad) ;
+
+ try { tStreamRow.write(protocol) ; }
+ catch (TException e) { TRDF.exception(e) ; }
+
+ tStreamRow.clear() ;
+ tquad.clear();
+ tgraph.clear();
+ tsubject.clear();
+ tpredicate.clear() ;
+ tobject.clear() ;
+ }
+
+ @Override
+ public void base(String base) {
+ // Ignore.
+ }
+
+ @Override
+ public void prefix(String prefix, String iri) {
+ try { pmap.add(prefix, iri) ; }
+ catch ( RiotException ex) {
+ Log.warn(this, "Prefix mapping error", ex) ;
+ }
+ RDF_PrefixDecl tprefix = new RDF_PrefixDecl(prefix, iri) ;
+ tStreamRow.setPrefixDecl(tprefix) ;
+ try { tStreamRow.write(protocol) ; }
+ catch (TException e) { TRDF.exception(e) ; }
+ tStreamRow.clear();
+ }
+
+ @Override
+ public void close() {
+ finish() ;
+ }
+
+ @Override
+ public void finish() {
+ TRDF.flush(protocol) ;
+ }
+}
Propchange:
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/StreamRDF2Thrift.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/StreamRowTRDFPrinter.java
URL:
http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/StreamRowTRDFPrinter.java?rev=1621573&view=auto
==============================================================================
---
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/StreamRowTRDFPrinter.java
(added)
+++
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/StreamRowTRDFPrinter.java
Sun Aug 31 09:34:40 2014
@@ -0,0 +1,91 @@
+/**
+ * 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.thrift;
+
+import org.apache.jena.atlas.io.IndentedWriter ;
+import org.apache.jena.riot.thrift.wire.RDF_PrefixDecl ;
+import org.apache.jena.riot.thrift.wire.RDF_Quad ;
+import org.apache.jena.riot.thrift.wire.RDF_Term ;
+import org.apache.jena.riot.thrift.wire.RDF_Triple ;
+
+/** Print (in debug format) an rdf-thrift stream */
+public class StreamRowTRDFPrinter implements VisitorStreamRowTRDF
+{
+ private static final boolean ONELINE = false ;
+ private final IndentedWriter out ;
+
+ public StreamRowTRDFPrinter(IndentedWriter out) { this.out = out ; }
+
+ @Override
+ public void visit(RDF_Triple triple) {
+ out.print("RDF_Triple") ;
+ out.incIndent();
+ gap() ;
+ print(triple.getS()) ;
+ gap() ;
+ print(triple.getP()) ;
+ gap() ;
+ print(triple.getO()) ;
+ lineEnd() ;
+ out.decIndent();
+ }
+
+ @Override
+ public void visit(RDF_Quad quad) {
+ out.print("RDF_Quad") ;
+ out.incIndent();
+ gap() ;
+ print(quad.getS()) ;
+ gap() ;
+ print(quad.getP()) ;
+ gap() ;
+ print(quad.getO()) ;
+ if ( quad.isSetG() ) {
+ gap() ;
+ print(quad.getG()) ;
+ }
+ lineEnd() ;
+ out.decIndent();
+ }
+
+ @Override
+ public void visit(RDF_PrefixDecl prefix) {
+ out.printf("RDF_PrefixDecl (%s: %s)\n", prefix.getPrefix(),
prefix.getUri()) ;
+ }
+
+ private void gap() {
+ if ( ONELINE )
+ out.print(" ") ;
+ else
+ out.println() ;
+ }
+
+ private void lineEnd() {
+ if ( ! ONELINE )
+ out.println() ;
+ }
+
+ private void startRow() { }
+
+ private void finishRow() { }
+
+ private void print(RDF_Term term) {
+ out.print(term.toString()) ;
+ }
+}
Propchange:
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/StreamRowTRDFPrinter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/TRDF.java
URL:
http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/TRDF.java?rev=1621573&view=auto
==============================================================================
--- jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/TRDF.java
(added)
+++ jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/TRDF.java Sun
Aug 31 09:34:40 2014
@@ -0,0 +1,136 @@
+/**
+ * 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.thrift;
+
+import java.io.BufferedInputStream ;
+import java.io.BufferedOutputStream ;
+import java.io.InputStream ;
+import java.io.OutputStream ;
+
+import org.apache.jena.atlas.io.IO ;
+import org.apache.jena.atlas.logging.Log ;
+import org.apache.jena.riot.RiotException ;
+import org.apache.jena.riot.system.StreamRDF ;
+import org.apache.jena.riot.thrift.wire.RDF_ANY ;
+import org.apache.jena.riot.thrift.wire.RDF_StreamRow ;
+import org.apache.jena.riot.thrift.wire.RDF_Term ;
+import org.apache.jena.riot.thrift.wire.RDF_UNDEF ;
+import org.apache.thrift.TException ;
+import org.apache.thrift.protocol.TCompactProtocol ;
+import org.apache.thrift.protocol.TJSONProtocol ;
+import org.apache.thrift.protocol.TProtocol ;
+import org.apache.thrift.protocol.TTupleProtocol ;
+import org.apache.thrift.transport.TIOStreamTransport ;
+import org.apache.thrift.transport.TTransport ;
+
+/** Support operations for RDF Thrift */
+public class TRDF {
+ public static final int InputBufferSize = 128*1024 ;
+ public static final int OutputBufferSize = 128*1024 ;
+
+ /**
+ * Create Thrift protocol for the InputStream.
+ * @param in InputStream
+ */
+ public static TProtocol protocol(InputStream in) {
+ try {
+ if ( ! ( in instanceof BufferedInputStream ) )
+ in = new BufferedInputStream(in, InputBufferSize) ;
+ TTransport transport = new TIOStreamTransport(in) ;
+ transport.open() ;
+ TProtocol protocol = protocol(transport) ;
+ return protocol ;
+ } catch (TException ex) { TRDF.exception(ex) ; return null ; }
+ }
+
+ /**
+ * Create Thrift protocol for the OutputStream.
+ * The caller must call {@linkplain TRDF#flush(TProtocol)}
+ * which will flush the underlying (internally buffered) output stream.
+ * @param out OutputStream
+ */
+ public static TProtocol protocol(OutputStream out) {
+ try {
+ // Flushing the protocol will flush the BufferedOutputStream
+ if ( !( out instanceof BufferedOutputStream ) )
+ out = new BufferedOutputStream(out, OutputBufferSize) ;
+ TTransport transport = new TIOStreamTransport(out) ;
+ transport.open() ;
+ TProtocol protocol = protocol(transport) ;
+ return protocol ;
+ } catch (TException ex) { TRDF.exception(ex) ; return null ; }
+ }
+
+ /**
+ * Decode the contents of the input stream and send to the {@linkplain
StreamRDF}.
+ * @param filename
+ */
+ public static TProtocol protocol(String filename) {
+ InputStream in = IO.openFile(filename) ;
+ TProtocol protocol = protocol(in) ;
+ return protocol ;
+ }
+
+ public static TProtocol protocol(TTransport transport) {
+ if ( true ) return new TCompactProtocol(transport) ;
+
+ // Keep the warnings down.
+ if ( false ) return new TTupleProtocol(transport) ;
+ if ( false ) return new TJSONProtocol(transport) ;
+ throw new RiotThriftException("No protocol impl choosen") ;
+ }
+
+ /** Flush a TProtocol; expections converted to {@linkplain RiotException}
*/
+ public static void flush(TProtocol protocol) {
+ flush(protocol.getTransport()) ;
+ }
+
+ /** Flush a TTransport; expections converted to {@linkplain RiotException}
*/
+ public static void flush(TTransport transport) {
+ try { transport.flush() ; }
+ catch (TException ex) { TRDF.exception(ex) ; }
+ }
+
+ public static final RDF_ANY ANY = new RDF_ANY() ;
+ /** The Thrift RDF Term 'ANY' */
+ public static final RDF_Term tANY = new RDF_Term() ;
+ /** The Thrift RDF Term 'UNDEF' */
+ public static final RDF_UNDEF UNDEF = new RDF_UNDEF() ;
+ public static final RDF_Term tUNDEF = new RDF_Term();
+
+ static { tANY.setAny(new RDF_ANY()) ; }
+
+ static { tUNDEF.setUndefined(new RDF_UNDEF()) ; }
+
+ public static void visit(RDF_StreamRow row, VisitorStreamRowTRDF visitor) {
+ if ( row.isSetTriple() ) {
+ visitor.visit(row.getTriple()) ;
+ } else if ( row.isSetQuad() ) {
+ visitor.visit(row.getQuad()) ;
+ } else if ( row.isSetPrefixDecl() ) {
+ visitor.visit(row.getPrefixDecl()) ;
+ } else {
+ Log.warn(ThriftConvert.class, "visit: Unrecognized: "+row) ;
+ }
+ }
+
+ public static void exception(TException ex) { throw new
RiotThriftException(ex) ; }
+
+}
+
Propchange:
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/TRDF.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/Thift2Binding.java
URL:
http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/Thift2Binding.java?rev=1621573&view=auto
==============================================================================
---
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/Thift2Binding.java
(added)
+++
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/Thift2Binding.java
Sun Aug 31 09:34:40 2014
@@ -0,0 +1,115 @@
+/**
+ * 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.thrift;
+
+import java.io.InputStream ;
+import java.util.ArrayList ;
+import java.util.Iterator ;
+import java.util.List ;
+
+import org.apache.jena.atlas.iterator.IteratorSlotted ;
+import org.apache.jena.riot.thrift.wire.RDF_DataTuple ;
+import org.apache.jena.riot.thrift.wire.RDF_Term ;
+import org.apache.jena.riot.thrift.wire.RDF_VAR ;
+import org.apache.jena.riot.thrift.wire.RDF_VarTuple ;
+import org.apache.thrift.TException ;
+import org.apache.thrift.protocol.TProtocol ;
+import org.apache.thrift.transport.TIOStreamTransport ;
+import org.apache.thrift.transport.TTransportException ;
+
+import com.hp.hpl.jena.graph.Node ;
+import com.hp.hpl.jena.sparql.core.Var ;
+import com.hp.hpl.jena.sparql.engine.binding.Binding ;
+import com.hp.hpl.jena.sparql.engine.binding.BindingFactory ;
+import com.hp.hpl.jena.sparql.engine.binding.BindingMap ;
+
+/** Converted from SPARQL result set encoded in Thrift to Bindings */
+public class Thift2Binding extends IteratorSlotted<Binding> implements
Iterator<Binding> {
+
+ private List<Var> vars = new ArrayList<>() ;
+ private List<String> varNames = new ArrayList<>() ;
+ private final RDF_DataTuple row = new RDF_DataTuple() ;
+ private InputStream in ;
+ private TProtocol protocol ;
+
+ public Thift2Binding(InputStream in) {
+ this.in = in ;
+ TIOStreamTransport transport = new TIOStreamTransport(in) ;
+ this.protocol = TRDF.protocol(transport) ;
+ readVars() ;
+ }
+
+ public Thift2Binding(TProtocol out) {
+ this.in = null ;
+ this.protocol = out ;
+ readVars() ;
+ }
+
+ private void readVars() {
+ RDF_VarTuple vrow = new RDF_VarTuple() ;
+ try { vrow.read(protocol) ; }
+ catch (TException e) { TRDF.exception(e) ; }
+ // ** Java8
+// vrow.getVars().forEach(rv -> {
+// String vn = rv.getName() ;
+// Var v = Var.alloc(rv.getName()) ;
+// varNames.add(vn) ;
+// }) ;
+ for ( RDF_VAR rv : vrow.getVars() ) {
+ String vn = rv.getName() ;
+ Var v = Var.alloc(rv.getName()) ;
+ varNames.add(vn) ;
+ }
+ vars = Var.varList(varNames) ;
+ }
+
+ public List<Var> getVars() { return vars ; }
+
+ public List<String> getVarNames() { return varNames ; }
+
+
+ @Override
+ protected Binding moveToNext() {
+ try { row.read(protocol) ; }
+ catch (TTransportException e) { return null ; }
+ catch (TException e) { TRDF.exception(e) ; }
+
+ if ( row.getRowSize() != vars.size() )
+ throw new RiotThriftException(String.format("Vars %d : Row length
: %d", vars.size(), row.getRowSize())) ;
+
+ BindingMap b = BindingFactory.create() ;
+
+ for ( int i = 0 ; i < vars.size() ; i++ ) {
+ // Old school
+ Var v = vars.get(i) ;
+ RDF_Term rt = row.getRow().get(i) ;
+ if ( rt.isSetUndefined() )
+ continue ;
+ Node n = ThriftConvert.convert(rt) ;
+ b.add(v, n) ;
+ }
+ row.clear() ;
+ return b ;
+ }
+
+ @Override
+ protected boolean hasMore() {
+ return true ;
+ }
+}
Propchange:
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/Thift2Binding.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/Thrift2StreamRDF.java
URL:
http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/Thrift2StreamRDF.java?rev=1621573&view=auto
==============================================================================
---
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/Thrift2StreamRDF.java
(added)
+++
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/Thrift2StreamRDF.java
Sun Aug 31 09:34:40 2014
@@ -0,0 +1,68 @@
+/**
+ * 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.thrift;
+
+import org.apache.jena.riot.RiotException ;
+import org.apache.jena.riot.system.PrefixMap ;
+import org.apache.jena.riot.system.StreamRDF ;
+import org.apache.jena.riot.thrift.wire.RDF_PrefixDecl ;
+import org.apache.jena.riot.thrift.wire.RDF_Quad ;
+import org.apache.jena.riot.thrift.wire.RDF_Triple ;
+
+import com.hp.hpl.jena.graph.Triple ;
+import com.hp.hpl.jena.sparql.core.Quad ;
+
+/** Thrift RDF (wire format) to RDF terms (Jena java objects)
+ *
+ * @see StreamRDF2Thrift for the reverse process.
+ */
+
+public class Thrift2StreamRDF implements VisitorStreamRowTRDF {
+
+ private final StreamRDF dest ;
+ private final PrefixMap pmap ;
+
+ public Thrift2StreamRDF(PrefixMap pmap, StreamRDF stream) {
+ this.pmap = pmap ;
+ this.dest = stream ;
+ }
+
+ @Override
+ public void visit(RDF_Triple rt) {
+ Triple t = ThriftConvert.convert(rt, pmap) ;
+ dest.triple(t) ;
+ }
+
+ @Override
+ public void visit(RDF_Quad rq) {
+ Quad q = ThriftConvert.convert(rq, pmap) ;
+ dest.quad(q) ;
+ }
+
+ @Override
+ public void visit(RDF_PrefixDecl prefixDecl) {
+ String prefix = prefixDecl.getPrefix() ;
+ String iriStr = prefixDecl.getUri() ;
+ try {
+ pmap.add(prefix, iriStr) ;
+ } catch (RiotException ex) {}
+ dest.prefix(prefix, iriStr) ;
+ }
+
+}
Propchange:
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/Thrift2StreamRDF.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/ThriftConvert.java
URL:
http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/ThriftConvert.java?rev=1621573&view=auto
==============================================================================
---
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/ThriftConvert.java
(added)
+++
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/ThriftConvert.java
Sun Aug 31 09:34:40 2014
@@ -0,0 +1,368 @@
+/**
+ * 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.thrift;
+
+import static org.apache.jena.riot.thrift.TRDF.ANY ;
+
+import java.math.BigDecimal ;
+import java.math.BigInteger ;
+
+import org.apache.jena.atlas.lib.Pair ;
+import org.apache.jena.riot.system.PrefixMap ;
+import org.apache.jena.riot.system.PrefixMapFactory ;
+import org.apache.jena.riot.thrift.wire.* ;
+
+import com.hp.hpl.jena.datatypes.RDFDatatype ;
+import com.hp.hpl.jena.datatypes.xsd.XSDDatatype ;
+import com.hp.hpl.jena.graph.Node ;
+import com.hp.hpl.jena.graph.NodeFactory ;
+import com.hp.hpl.jena.graph.Triple ;
+import com.hp.hpl.jena.rdf.model.AnonId ;
+import com.hp.hpl.jena.sparql.core.Quad ;
+import com.hp.hpl.jena.sparql.core.Var ;
+
+/** Convert to and from Thrift wire objects.
+ * See {@linkplain StreamRDF2Thrift} and {@linkplain Thrift2StreamRDF}
+ * for ways to convert as streams (they recycle intermediate objects).
+ * @see StreamRDF2Thrift
+ * @see Thrift2StreamRDF
+ */
+public class ThriftConvert
+{
+ private static final PrefixMap emptyPrefixMap =
PrefixMapFactory.emptyPrefixMap() ;
+
+ public static Node convert(RDF_Term term) {
+ return convert(term, emptyPrefixMap) ;
+ }
+
+ public static Node convert(RDF_Term term, PrefixMap pmap) {
+ if ( term.isSetPrefixName() ) {
+ String x = expand(term.getPrefixName(), pmap) ;
+ if ( x != null )
+ return NodeFactory.createURI(x) ;
+ throw new RiotThriftException("Failed to expand "+term) ;
+ // Log.warn(BinRDF.class, "Failed to expand "+term)
;
+ // return
NodeFactory.createURI(prefix+":"+localname) ;
+ }
+
+ if ( term.isSetIri() )
+ return NodeFactory.createURI(term.getIri().getIri()) ;
+
+ if ( term.isSetBnode() )
+ return NodeFactory.createAnon(new
AnonId(term.getBnode().getLabel())) ;
+
+ if ( term.isSetLiteral() ) {
+ RDF_Literal lit = term.getLiteral() ;
+ String lex = lit.getLex() ;
+ String dtString = null ;
+ if ( lit.isSetDatatype() )
+ dtString = lit.getDatatype() ;
+ else if ( lit.isSetDtPrefix()) {
+ String x = expand(lit.getDtPrefix(), pmap) ;
+ if ( x == null )
+ throw new RiotThriftException("Failed to expand datatype
prefix name:"+term) ;
+ dtString = x ;
+ }
+ RDFDatatype dt = NodeFactory.getType(dtString) ;
+
+ String lang = lit.getLangtag() ;
+ return NodeFactory.createLiteral(lex, lang, dt) ;
+ }
+
+ if ( term.isSetValInteger() ) {
+ long x = term.getValInteger() ;
+ String lex = Long.toString(x, 10) ;
+ RDFDatatype dt = XSDDatatype.XSDinteger ;
+ return NodeFactory.createLiteral(lex, null, dt) ;
+ }
+
+ if ( term.isSetValDouble() ) {
+ double x = term.getValDouble() ;
+ String lex = Double.toString(x) ;
+ RDFDatatype dt = XSDDatatype.XSDdouble ;
+ return NodeFactory.createLiteral(lex, null, dt) ;
+ }
+
+ if ( term.isSetValDecimal() ) {
+ long value = term.getValDecimal().getValue() ;
+ int scale = term.getValDecimal().getScale() ;
+ BigDecimal d = BigDecimal.valueOf(value, scale) ;
+ String lex = d.toPlainString() ;
+ RDFDatatype dt = XSDDatatype.XSDdecimal ;
+ return NodeFactory.createLiteral(lex, null, dt) ;
+ }
+
+ if ( term.isSetVariable() )
+ return Var.alloc(term.getVariable().getName()) ;
+
+ if ( term.isSetAny() )
+ return Node.ANY ;
+
+ throw new RiotThriftException("No conversion to a Node:
"+term.toString()) ;
+ }
+
+ private static String expand(RDF_PrefixName prefixName, PrefixMap pmap) {
+ if ( pmap == null )
+ return null ;
+
+ String prefix = prefixName.getPrefix() ;
+ String localname = prefixName.getLocalName() ;
+ String x = pmap.expand(prefix, localname) ;
+ if ( x == null )
+ throw new RiotThriftException("Failed to expand "+prefixName) ;
+ return x ;
+ }
+
+
+ public static RDF_Term convert(Node node, boolean allowValues) {
+ return convert(node, null, allowValues) ;
+ }
+
+ public static RDF_Term convert(Node node, PrefixMap pmap, boolean
allowValues) {
+ RDF_Term n = new RDF_Term() ;
+ toThrift(node, pmap, n, allowValues) ;
+ return n ;
+ }
+
+ static final BigInteger MAX_I = BigInteger.valueOf(Long.MAX_VALUE) ;
+ static final BigInteger MIN_I = BigInteger.valueOf(Long.MIN_VALUE) ;
+
+ /** Attempt to encode a node by value (integer, decimal, double) into an
RDF_term.
+ * @param node
+ * @param term
+ * @return true if the term was set, else false.
+ */
+
+ public static boolean toThriftValue(Node node, RDF_Term term) {
+ if ( ! node.isLiteral() )
+ return false ;
+
+ // Value cases : Integer, Double, Decimal
+ String lex = node.getLiteralLexicalForm() ;
+ RDFDatatype rdt = node.getLiteralDatatype() ;
+
+ if ( rdt == null )
+ return false ;
+
+ if ( rdt.equals(XSDDatatype.XSDdecimal) ) {
+ if ( rdt.isValid(lex)) {
+ BigDecimal decimal = new BigDecimal(lex.trim()) ;
+ int scale = decimal.scale() ;
+ BigInteger bigInt = decimal.unscaledValue() ;
+ if ( bigInt.compareTo(MAX_I) <= 0 && bigInt.compareTo(MIN_I)
>= 0 ) {
+ // This check makes sure that bigInt.longValue() is safe
+ RDF_Decimal d = new RDF_Decimal(bigInt.longValue(), scale)
;
+ RDF_Term literal = new RDF_Term() ;
+ term.setValDecimal(d) ;
+ return true ;
+ }
+ }
+ } else if (
+ rdt.equals(XSDDatatype.XSDinteger) ||
+ rdt.equals(XSDDatatype.XSDlong) ||
+ rdt.equals(XSDDatatype.XSDint) ||
+ rdt.equals(XSDDatatype.XSDshort) ||
+ rdt.equals(XSDDatatype.XSDbyte)
+ ) {
+ // and 4 unsigned equivalents
+ // and positive, negative, nonPostive nonNegativeInteger
+
+ // Conservative - no derived types.
+ if ( rdt.isValid(lex)) {
+ try {
+ long v = ((Number)node.getLiteralValue()).longValue() ;
+ term.setValInteger(v) ;
+ return true ;
+ }
+ // Out of range for the type, not a long etc etc.
+ catch (Throwable ex) { }
+ }
+ } else if ( rdt.equals(XSDDatatype.XSDdouble) ) {
+ // XSDfloat??
+ if ( rdt.isValid(lex)) {
+ try {
+ double v = ((Double)node.getLiteralValue()).doubleValue() ;
+ term.setValDouble(v) ;
+ return true ;
+ }
+ // Out of range for the type, ...
+ catch (Throwable ex) { }
+ }
+ }
+ return false ;
+ }
+
+ public static void toThrift(Node node, PrefixMap pmap, RDF_Term term,
boolean allowValues) {
+ if ( node.isURI() ) {
+ RDF_PrefixName prefixName = contract(node.getURI(), pmap) ;
+ if ( prefixName != null ) {
+ term.setPrefixName(prefixName) ;
+ return ;
+ }
+ }
+
+ if ( node.isBlank() ) {
+ RDF_BNode b = new RDF_BNode(node.getBlankNodeLabel()) ;
+ term.setBnode(b) ;
+ return ;
+ }
+
+ if ( node.isURI() ) {
+ RDF_IRI iri = new RDF_IRI(node.getURI()) ;
+ term.setIri(iri) ;
+ return ;
+ }
+
+ if ( node.isLiteral() ) {
+ // Value cases : Integer, Double, Decimal
+ if ( allowValues) {
+ boolean b = toThriftValue(node, term) ;
+ if ( b /* term.isSet() */ )
+ return ;
+ }
+
+ String lex = node.getLiteralLexicalForm() ;
+ String dt = node.getLiteralDatatypeURI() ;
+ String lang = node.getLiteralLanguage() ;
+
+ // General encoding.
+ RDF_Literal literal = new RDF_Literal(lex) ;
+ if ( dt != null ) {
+ RDF_PrefixName dtPrefixName = contract(dt, pmap) ;
+ if ( dtPrefixName != null )
+ literal.setDtPrefix(dtPrefixName) ;
+ else
+ literal.setDatatype(dt) ;
+ }
+ if ( lang != null && ! lang.isEmpty() )
+ literal.setLangtag(lang) ;
+ term.setLiteral(literal) ;
+ return ;
+ }
+
+ if ( Var.isVar(node) ) {
+ RDF_VAR var = new RDF_VAR(node.getName()) ;
+ term.setVariable(var) ;
+ return ;
+ }
+ if ( Node.ANY.equals(node)) {
+ term.setAny(ANY) ;
+ return ;
+ }
+ throw new RiotThriftException("Node converstion not supported: "+node)
;
+ }
+
+ private static RDF_PrefixName contract(String uriStr, PrefixMap pmap) {
+ if ( pmap == null )
+ return null ;
+ Pair<String, String> p = pmap.abbrev(uriStr) ;
+ if ( p == null )
+ return null ;
+ return new RDF_PrefixName(p.getLeft(), p.getRight()) ;
+ }
+
+ public static Triple convert(RDF_Triple triple) {
+ return convert(triple, null) ;
+ }
+
+ public static Triple convert(RDF_Triple rt, PrefixMap pmap) {
+ Node s = convert(rt.getS(), pmap) ;
+ Node p = convert(rt.getP(), pmap) ;
+ Node o = convert(rt.getO(), pmap) ;
+ return Triple.create(s, p, o) ;
+ }
+
+ public static RDF_Triple convert(Triple triple, boolean allowValues) {
+ return convert(triple, null, allowValues) ;
+ }
+
+ public static RDF_Triple convert(Triple triple, PrefixMap pmap, boolean
allowValues) {
+ RDF_Triple t = new RDF_Triple() ;
+ RDF_Term s = convert(triple.getSubject(), pmap, allowValues) ;
+ RDF_Term p = convert(triple.getPredicate(), pmap, allowValues) ;
+ RDF_Term o = convert(triple.getObject(), pmap, allowValues) ;
+ t.setS(s) ;
+ t.setP(p) ;
+ t.setO(o) ;
+ return t ;
+ }
+
+ public static Quad convert(RDF_Quad quad) {
+ return convert(quad, null) ;
+ }
+
+ public static Quad convert(RDF_Quad rq, PrefixMap pmap) {
+ Node g = (rq.isSetG() ? convert(rq.getG(), pmap) : null ) ;
+ Node s = convert(rq.getS(), pmap) ;
+ Node p = convert(rq.getP(), pmap) ;
+ Node o = convert(rq.getO(), pmap) ;
+ return Quad.create(g, s, p, o) ;
+ }
+
+ public static RDF_Quad convert(Quad quad, boolean allowValues) {
+ return convert(quad, null, allowValues) ;
+ }
+
+ public static RDF_Quad convert(Quad quad, PrefixMap pmap, boolean
allowValues) {
+ RDF_Quad q = new RDF_Quad() ;
+ RDF_Term g = null ;
+ if ( quad.getGraph() != null )
+ g = convert(quad.getGraph(), pmap, allowValues) ;
+ RDF_Term s = convert(quad.getSubject(), pmap, allowValues) ;
+ RDF_Term p = convert(quad.getPredicate(), pmap, allowValues) ;
+ RDF_Term o = convert(quad.getObject(), pmap, allowValues) ;
+ if ( g != null )
+ q.setG(g) ;
+ q.setS(s) ;
+ q.setP(p) ;
+ q.setO(o) ;
+ return q ;
+ }
+
+ // RDF_Tuple => RDF_row (for result sets) or List<RDFTerm>
+
+// public static Tuple<Node> convert(RDF_Tuple row) {
+// return convert(row, null) ;
+// }
+//
+// public static Tuple<Node> convert(RDF_Tuple row, PrefixMap pmap) {
+// List<RDF_Term> terms = row.getTerms() ;
+// Node[] tuple = new Node[terms.size()] ;
+// int idx = 0 ;
+// for ( RDF_Term rt : terms ) {
+// tuple[idx] = convert(rt, pmap) ;
+// idx ++ ;
+// }
+
+// return Tuple.create(tuple) ;
+// }
+//
+// public static RDF_Tuple convert(Tuple<Node> tuple) {
+// return convert(tuple, null) ;
+// }
+//
+// public static RDF_Tuple convert(Tuple<Node> tuple, PrefixMap pmap) {
+// RDF_Tuple rTuple = new RDF_Tuple() ;
+// for ( Node n : tuple ) {
+// RDF_Term rt = convert(n, pmap) ;
+// rTuple.addToTerms(rt) ;
+// }
+// return rTuple ;
+// }
+}
Propchange:
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/ThriftConvert.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/VisitorStreamRowTRDF.java
URL:
http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/VisitorStreamRowTRDF.java?rev=1621573&view=auto
==============================================================================
---
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/VisitorStreamRowTRDF.java
(added)
+++
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/VisitorStreamRowTRDF.java
Sun Aug 31 09:34:40 2014
@@ -0,0 +1,30 @@
+/**
+ * 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.thrift;
+
+import org.apache.jena.riot.thrift.wire.RDF_PrefixDecl ;
+import org.apache.jena.riot.thrift.wire.RDF_Quad ;
+import org.apache.jena.riot.thrift.wire.RDF_Triple ;
+
+/** Visitor for RDF_StreamRow */
+public interface VisitorStreamRowTRDF {
+ public void visit(RDF_Triple triple) ;
+ public void visit(RDF_Quad quad) ;
+ public void visit(RDF_PrefixDecl prefix) ;
+}
Propchange:
jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/thrift/VisitorStreamRowTRDF.java
------------------------------------------------------------------------------
svn:mime-type = text/plain