Repository: jena Updated Branches: refs/heads/master 4b5cd2677 -> 3d70d7350
http://git-wip-us.apache.org/repos/asf/jena/blob/3d70d735/jena-spatial/src/test/resources/spatial-solr-config.ttl ---------------------------------------------------------------------- diff --git a/jena-spatial/src/test/resources/spatial-solr-config.ttl b/jena-spatial/src/test/resources/spatial-solr-config.ttl index b8f16ed..dfa0a33 100644 --- a/jena-spatial/src/test/resources/spatial-solr-config.ttl +++ b/jena-spatial/src/test/resources/spatial-solr-config.ttl @@ -1,51 +1,51 @@ - ## Example of a TDB dataset and spatial index - -@prefix : <http://localhost/jena_example/#> . -@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . -@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . -@prefix tdb: <http://jena.hpl.hp.com/2008/tdb#> . -@prefix ja: <http://jena.hpl.hp.com/2005/11/Assembler#> . -@prefix spatial: <http://jena.apache.org/spatial#> . - -# TDB -[] ja:loadClass "org.apache.jena.tdb.TDB" . -tdb:DatasetTDB rdfs:subClassOf ja:RDFDataset . -tdb:GraphTDB rdfs:subClassOf ja:Model . - -# Spatial -[] ja:loadClass "org.apache.jena.query.spatial.SpatialQuery" . -spatial:SpatialDataset rdfs:subClassOf ja:RDFDataset . -spatial:SpatialIndexSolr rdfs:subClassOf spatial:SpatialIndex . - -## --------------------------------------------------------------- -## This URI must be fixed - it's used to assemble the spatial dataset. - -:spatial_dataset rdf:type spatial:SpatialDataset ; - spatial:dataset <#dataset> ; - spatial:index <#indexSolr> ; - . - -<#dataset> rdf:type tdb:DatasetTDB ; - tdb:location "--mem--" ; - . - -<#indexSolr> a spatial:SpatialIndexSolr ; - spatial:server <embedded:SolrARQCollection> ; - spatial:solrHome <file:SolrHome> ; - spatial:definition <#definition> ; - . - -<#definition> a spatial:EntityDefinition ; - spatial:entityField "uri" ; - spatial:geoField "geo" ; - # custom geo predicates for 1) latitude/longitude - spatial:hasSpatialPredicatePairs ( - [ spatial:latitude :latitude_1 ; spatial:longitude :longitude_1 ] - [ spatial:latitude :latitude_2 ; spatial:longitude :longitude_2 ] - ) ; - # custom geo predicates for 2) Well Known Text Literal - spatial:hasWKTPredicates (:wkt_1 :wkt_2) ; - # custom SpatialContextFactory for 2) Well Known Text Literal - spatial:spatialContextFactory - "com.spatial4j.core.context.jts.JtsSpatialContextFactory" + ## Example of a TDB dataset and spatial index + +@prefix : <http://localhost/jena_example/#> . +@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . +@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . +@prefix tdb: <http://jena.hpl.hp.com/2008/tdb#> . +@prefix ja: <http://jena.hpl.hp.com/2005/11/Assembler#> . +@prefix spatial: <http://jena.apache.org/spatial#> . + +# TDB +[] ja:loadClass "org.apache.jena.tdb.TDB" . +tdb:DatasetTDB rdfs:subClassOf ja:RDFDataset . +tdb:GraphTDB rdfs:subClassOf ja:Model . + +# Spatial +[] ja:loadClass "org.apache.jena.query.spatial.SpatialQuery" . +spatial:SpatialDataset rdfs:subClassOf ja:RDFDataset . +spatial:SpatialIndexSolr rdfs:subClassOf spatial:SpatialIndex . + +## --------------------------------------------------------------- +## This URI must be fixed - it's used to assemble the spatial dataset. + +:spatial_dataset rdf:type spatial:SpatialDataset ; + spatial:dataset <#dataset> ; + spatial:index <#indexSolr> ; + . + +<#dataset> rdf:type tdb:DatasetTDB ; + tdb:location "--mem--" ; + . + +<#indexSolr> a spatial:SpatialIndexSolr ; + spatial:server <embedded:SolrARQCollection> ; + spatial:solrHome <file:SolrHome> ; + spatial:definition <#definition> ; + . + +<#definition> a spatial:EntityDefinition ; + spatial:entityField "uri" ; + spatial:geoField "geo" ; + # custom geo predicates for 1) latitude/longitude + spatial:hasSpatialPredicatePairs ( + [ spatial:latitude :latitude_1 ; spatial:longitude :longitude_1 ] + [ spatial:latitude :latitude_2 ; spatial:longitude :longitude_2 ] + ) ; + # custom geo predicates for 2) Well Known Text Literal + spatial:hasWKTPredicates (:wkt_1 :wkt_2) ; + # custom SpatialContextFactory for 2) Well Known Text Literal + spatial:spatialContextFactory + "com.spatial4j.core.context.jts.JtsSpatialContextFactory" . \ No newline at end of file http://git-wip-us.apache.org/repos/asf/jena/blob/3d70d735/jena-tdb/src-examples/tdb/examples/ExTDB1.java ---------------------------------------------------------------------- diff --git a/jena-tdb/src-examples/tdb/examples/ExTDB1.java b/jena-tdb/src-examples/tdb/examples/ExTDB1.java index 2ca34d3..8d5539d 100644 --- a/jena-tdb/src-examples/tdb/examples/ExTDB1.java +++ b/jena-tdb/src-examples/tdb/examples/ExTDB1.java @@ -1,51 +1,51 @@ -/* - * 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 tdb.examples; - -import org.apache.jena.query.Dataset ; -import org.apache.jena.rdf.model.Model; - -import org.apache.jena.tdb.TDBFactory; - -/** Example of creating a TDB-backed model. - * The preferred way is to create a dataset then get the mode required from the dataset. - * The dataset can be used for SPARQL query and update - * but the Model (or Graph) can also be used. - * - * All the Jena APIs work on the model. - * - * Calling TDBFactory is the only place TDB-specific code is needed. - */ - -public class ExTDB1 -{ - public static void main(String... argv) - { - // Direct way: Make a TDB-back Jena model in the named directory. - String directory = "MyDatabases/DB1" ; - Dataset ds = TDBFactory.createDataset(directory) ; - Model model = ds.getDefaultModel() ; - - // ... do work ... - - // Close the dataset. - ds.close(); - - } -} +/* + * 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 tdb.examples; + +import org.apache.jena.query.Dataset ; +import org.apache.jena.rdf.model.Model; + +import org.apache.jena.tdb.TDBFactory; + +/** Example of creating a TDB-backed model. + * The preferred way is to create a dataset then get the mode required from the dataset. + * The dataset can be used for SPARQL query and update + * but the Model (or Graph) can also be used. + * + * All the Jena APIs work on the model. + * + * Calling TDBFactory is the only place TDB-specific code is needed. + */ + +public class ExTDB1 +{ + public static void main(String... argv) + { + // Direct way: Make a TDB-back Jena model in the named directory. + String directory = "MyDatabases/DB1" ; + Dataset ds = TDBFactory.createDataset(directory) ; + Model model = ds.getDefaultModel() ; + + // ... do work ... + + // Close the dataset. + ds.close(); + + } +} http://git-wip-us.apache.org/repos/asf/jena/blob/3d70d735/jena-tdb/src-examples/tdb/examples/ExTDB2.java ---------------------------------------------------------------------- diff --git a/jena-tdb/src-examples/tdb/examples/ExTDB2.java b/jena-tdb/src-examples/tdb/examples/ExTDB2.java index 9548ccf..3d7e825 100644 --- a/jena-tdb/src-examples/tdb/examples/ExTDB2.java +++ b/jena-tdb/src-examples/tdb/examples/ExTDB2.java @@ -1,43 +1,43 @@ -/* - * 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 tdb.examples; - -import org.apache.jena.query.Dataset ; -import org.apache.jena.tdb.TDBFactory ; - -/** - * Using an assembler description (see wiki for details of the assembler format for TDB) - * This way, you can change the model being used without changing the code. - * The assembler file is a configuration file. - * The same assembler description will work as part of a Joseki configuration file. - */ - -public class ExTDB2 -{ - public static void main(String... argv) - { - String assemblerFile = "Store/tdb-assembler.ttl" ; - - Dataset ds = TDBFactory.assembleDataset(assemblerFile) ; - - // ... do work ... - - ds.close() ; - } -} +/* + * 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 tdb.examples; + +import org.apache.jena.query.Dataset ; +import org.apache.jena.tdb.TDBFactory ; + +/** + * Using an assembler description (see wiki for details of the assembler format for TDB) + * This way, you can change the model being used without changing the code. + * The assembler file is a configuration file. + * The same assembler description will work as part of a Joseki configuration file. + */ + +public class ExTDB2 +{ + public static void main(String... argv) + { + String assemblerFile = "Store/tdb-assembler.ttl" ; + + Dataset ds = TDBFactory.assembleDataset(assemblerFile) ; + + // ... do work ... + + ds.close() ; + } +} http://git-wip-us.apache.org/repos/asf/jena/blob/3d70d735/jena-tdb/src-examples/tdb/examples/ExTDB3.java ---------------------------------------------------------------------- diff --git a/jena-tdb/src-examples/tdb/examples/ExTDB3.java b/jena-tdb/src-examples/tdb/examples/ExTDB3.java index 43c6173..8d74c96 100644 --- a/jena-tdb/src-examples/tdb/examples/ExTDB3.java +++ b/jena-tdb/src-examples/tdb/examples/ExTDB3.java @@ -1,66 +1,66 @@ -/* - * 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 tdb.examples; - -import org.apache.jena.riot.RDFDataMgr ; - -import org.apache.jena.assembler.Assembler ; -import org.apache.jena.query.Dataset ; -import org.apache.jena.rdf.model.Model ; -import org.apache.jena.rdf.model.Resource ; -import org.apache.jena.shared.JenaException ; -import org.apache.jena.sparql.core.assembler.DatasetAssemblerVocab ; -import org.apache.jena.sparql.util.TypeNotUniqueException ; -import org.apache.jena.sparql.util.graph.GraphUtils ; -import org.apache.jena.tdb.assembler.VocabTDB ; - -/** - * Examples of finding an assembler for a TDB model in a larger collection - * of descriptions in a single file. - */ -public class ExTDB3 -{ - public static void main(String... argv) - { - String assemblerFile = "Store/tdb-assembler.ttl" ; - - // Find a particular description in the file where there are several: - Model spec = RDFDataMgr.loadModel(assemblerFile) ; - - // Find the right starting point for the description in some way. - Resource root = null ; - - if ( false ) - // If you know the Resource URI: - root = spec.createResource("http://example/myChoiceOfURI" ); - else - { - // Alternatively, look for the a single resource of the right type. - try { - // Find the required description - the file can contain descriptions of many different types. - root = GraphUtils.findRootByType(spec, VocabTDB.tDatasetTDB) ; - if ( root == null ) - throw new JenaException("Failed to find a suitable root") ; - } catch (TypeNotUniqueException ex) - { throw new JenaException("Multiple types for: "+DatasetAssemblerVocab.tDataset) ; } - } - - Dataset ds = (Dataset)Assembler.general.open(root) ; - } -} +/* + * 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 tdb.examples; + +import org.apache.jena.riot.RDFDataMgr ; + +import org.apache.jena.assembler.Assembler ; +import org.apache.jena.query.Dataset ; +import org.apache.jena.rdf.model.Model ; +import org.apache.jena.rdf.model.Resource ; +import org.apache.jena.shared.JenaException ; +import org.apache.jena.sparql.core.assembler.DatasetAssemblerVocab ; +import org.apache.jena.sparql.util.TypeNotUniqueException ; +import org.apache.jena.sparql.util.graph.GraphUtils ; +import org.apache.jena.tdb.assembler.VocabTDB ; + +/** + * Examples of finding an assembler for a TDB model in a larger collection + * of descriptions in a single file. + */ +public class ExTDB3 +{ + public static void main(String... argv) + { + String assemblerFile = "Store/tdb-assembler.ttl" ; + + // Find a particular description in the file where there are several: + Model spec = RDFDataMgr.loadModel(assemblerFile) ; + + // Find the right starting point for the description in some way. + Resource root = null ; + + if ( false ) + // If you know the Resource URI: + root = spec.createResource("http://example/myChoiceOfURI" ); + else + { + // Alternatively, look for the a single resource of the right type. + try { + // Find the required description - the file can contain descriptions of many different types. + root = GraphUtils.findRootByType(spec, VocabTDB.tDatasetTDB) ; + if ( root == null ) + throw new JenaException("Failed to find a suitable root") ; + } catch (TypeNotUniqueException ex) + { throw new JenaException("Multiple types for: "+DatasetAssemblerVocab.tDataset) ; } + } + + Dataset ds = (Dataset)Assembler.general.open(root) ; + } +} http://git-wip-us.apache.org/repos/asf/jena/blob/3d70d735/jena-tdb/src-examples/tdb/examples/ExTDB4.java ---------------------------------------------------------------------- diff --git a/jena-tdb/src-examples/tdb/examples/ExTDB4.java b/jena-tdb/src-examples/tdb/examples/ExTDB4.java index bb9ea1a..4b52b86 100644 --- a/jena-tdb/src-examples/tdb/examples/ExTDB4.java +++ b/jena-tdb/src-examples/tdb/examples/ExTDB4.java @@ -1,60 +1,60 @@ -/* - * 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 tdb.examples; - -import org.apache.jena.query.Dataset ; -import org.apache.jena.query.Query ; -import org.apache.jena.query.QueryExecution ; -import org.apache.jena.query.QueryExecutionFactory ; -import org.apache.jena.query.QueryFactory ; -import org.apache.jena.query.ResultSet ; -import org.apache.jena.query.ResultSetFormatter ; -import org.apache.jena.tdb.TDBFactory ; - -/** Example of creating a TDB-backed model. - * The preferred way is to create a dataset then get the mode required from the dataset. - * The dataset can be used for SPARQL query and update - * but the Model (or Graph) can also be used. - * - * All the Jena APIs work on the model. - * - * Calling TDBFactory is the only place TDB-specific code is needed. - */ - -public class ExTDB4 -{ - public static void main(String... argv) - { - // Direct way: Make a TDB-back Jena model in the named directory. - String directory = "MyDatabases/DB1" ; - Dataset dataset = TDBFactory.createDataset(directory) ; - - // Potentially expensive query. - String sparqlQueryString = "SELECT (count(*) AS ?count) { ?s ?p ?o }" ; - // See http://incubator.apache.org/jena/documentation/query/app_api.html - - Query query = QueryFactory.create(sparqlQueryString) ; - QueryExecution qexec = QueryExecutionFactory.create(query, dataset) ; - ResultSet results = qexec.execSelect() ; - ResultSetFormatter.out(results) ; - qexec.close() ; - - dataset.close(); - } -} +/* + * 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 tdb.examples; + +import org.apache.jena.query.Dataset ; +import org.apache.jena.query.Query ; +import org.apache.jena.query.QueryExecution ; +import org.apache.jena.query.QueryExecutionFactory ; +import org.apache.jena.query.QueryFactory ; +import org.apache.jena.query.ResultSet ; +import org.apache.jena.query.ResultSetFormatter ; +import org.apache.jena.tdb.TDBFactory ; + +/** Example of creating a TDB-backed model. + * The preferred way is to create a dataset then get the mode required from the dataset. + * The dataset can be used for SPARQL query and update + * but the Model (or Graph) can also be used. + * + * All the Jena APIs work on the model. + * + * Calling TDBFactory is the only place TDB-specific code is needed. + */ + +public class ExTDB4 +{ + public static void main(String... argv) + { + // Direct way: Make a TDB-back Jena model in the named directory. + String directory = "MyDatabases/DB1" ; + Dataset dataset = TDBFactory.createDataset(directory) ; + + // Potentially expensive query. + String sparqlQueryString = "SELECT (count(*) AS ?count) { ?s ?p ?o }" ; + // See http://incubator.apache.org/jena/documentation/query/app_api.html + + Query query = QueryFactory.create(sparqlQueryString) ; + QueryExecution qexec = QueryExecutionFactory.create(query, dataset) ; + ResultSet results = qexec.execSelect() ; + ResultSetFormatter.out(results) ; + qexec.close() ; + + dataset.close(); + } +} http://git-wip-us.apache.org/repos/asf/jena/blob/3d70d735/jena-tdb/src-examples/tdb/examples/ExTDB5.java ---------------------------------------------------------------------- diff --git a/jena-tdb/src-examples/tdb/examples/ExTDB5.java b/jena-tdb/src-examples/tdb/examples/ExTDB5.java index 92517d5..255440e 100644 --- a/jena-tdb/src-examples/tdb/examples/ExTDB5.java +++ b/jena-tdb/src-examples/tdb/examples/ExTDB5.java @@ -1,70 +1,70 @@ -/* - * 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 tdb.examples; - -import org.apache.jena.query.Dataset ; -import org.apache.jena.query.Query ; -import org.apache.jena.query.QueryExecution ; -import org.apache.jena.query.QueryExecutionFactory ; -import org.apache.jena.query.QueryFactory ; -import org.apache.jena.query.QuerySolution ; -import org.apache.jena.query.ResultSet ; -import org.apache.jena.tdb.TDBFactory ; - -/** Example of creating a TDB-backed model. - * The preferred way is to create a dataset then get the mode required from the dataset. - * The dataset can be used for SPARQL query and update - * but the Model (or Graph) can also be used. - * - * All the Jena APIs work on the model. - * - * Calling TDBFactory is the only place TDB-specific code is needed. - * - * See also ExTDB_Txn1 for use with transactions. - */ - -public class ExTDB5 -{ - public static void main(String... argv) - { - // Direct way: Make a TDB-back Jena model in the named directory. - String directory = "MyDatabases/DB1" ; - Dataset dataset = TDBFactory.createDataset(directory) ; - - // Potentially expensive query. - String sparqlQueryString = "SELECT (count(*) AS ?count) { ?s ?p ?o }" ; - // See http://incubator.apache.org/jena/documentation/query/app_api.html - - Query query = QueryFactory.create(sparqlQueryString) ; - QueryExecution qexec = QueryExecutionFactory.create(query, dataset) ; - try { - ResultSet results = qexec.execSelect() ; - for ( ; results.hasNext() ; ) - { - QuerySolution soln = results.nextSolution() ; - int count = soln.getLiteral("count").getInt() ; - System.out.println("count = "+count) ; - } - } finally { qexec.close() ; } - - // Close the dataset. - dataset.close(); - - } -} +/* + * 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 tdb.examples; + +import org.apache.jena.query.Dataset ; +import org.apache.jena.query.Query ; +import org.apache.jena.query.QueryExecution ; +import org.apache.jena.query.QueryExecutionFactory ; +import org.apache.jena.query.QueryFactory ; +import org.apache.jena.query.QuerySolution ; +import org.apache.jena.query.ResultSet ; +import org.apache.jena.tdb.TDBFactory ; + +/** Example of creating a TDB-backed model. + * The preferred way is to create a dataset then get the mode required from the dataset. + * The dataset can be used for SPARQL query and update + * but the Model (or Graph) can also be used. + * + * All the Jena APIs work on the model. + * + * Calling TDBFactory is the only place TDB-specific code is needed. + * + * See also ExTDB_Txn1 for use with transactions. + */ + +public class ExTDB5 +{ + public static void main(String... argv) + { + // Direct way: Make a TDB-back Jena model in the named directory. + String directory = "MyDatabases/DB1" ; + Dataset dataset = TDBFactory.createDataset(directory) ; + + // Potentially expensive query. + String sparqlQueryString = "SELECT (count(*) AS ?count) { ?s ?p ?o }" ; + // See http://incubator.apache.org/jena/documentation/query/app_api.html + + Query query = QueryFactory.create(sparqlQueryString) ; + QueryExecution qexec = QueryExecutionFactory.create(query, dataset) ; + try { + ResultSet results = qexec.execSelect() ; + for ( ; results.hasNext() ; ) + { + QuerySolution soln = results.nextSolution() ; + int count = soln.getLiteral("count").getInt() ; + System.out.println("count = "+count) ; + } + } finally { qexec.close() ; } + + // Close the dataset. + dataset.close(); + + } +} http://git-wip-us.apache.org/repos/asf/jena/blob/3d70d735/jena-tdb/src/main/java/org/apache/jena/tdb/base/file/BufferAllocator.java ---------------------------------------------------------------------- diff --git a/jena-tdb/src/main/java/org/apache/jena/tdb/base/file/BufferAllocator.java b/jena-tdb/src/main/java/org/apache/jena/tdb/base/file/BufferAllocator.java index 40cf23d..7a12b69 100644 --- a/jena-tdb/src/main/java/org/apache/jena/tdb/base/file/BufferAllocator.java +++ b/jena-tdb/src/main/java/org/apache/jena/tdb/base/file/BufferAllocator.java @@ -1,43 +1,43 @@ -/* - * 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.tdb.base.file; - -import java.nio.ByteBuffer ; - -import org.apache.jena.atlas.lib.Closeable ; - -/** - * An allocator for retrieving ByteBuffers of a given size. - */ -public interface BufferAllocator extends Closeable -{ - /** - * Allocate and return a ByteBuffer of the given size - * @param capacity the desired size of the ByteBuffer - * @return a ByteBuffer with the capacity set to the desired size - */ - public ByteBuffer allocate(int capacity); - - /** - * Call this method when you are finished with all of the ByteBuffers - * retrieved from allocate. The BufferAllocator is then free to reuse - * memory that was previously handed out. - */ - public void clear(); -} +/* + * 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.tdb.base.file; + +import java.nio.ByteBuffer ; + +import org.apache.jena.atlas.lib.Closeable ; + +/** + * An allocator for retrieving ByteBuffers of a given size. + */ +public interface BufferAllocator extends Closeable +{ + /** + * Allocate and return a ByteBuffer of the given size + * @param capacity the desired size of the ByteBuffer + * @return a ByteBuffer with the capacity set to the desired size + */ + public ByteBuffer allocate(int capacity); + + /** + * Call this method when you are finished with all of the ByteBuffers + * retrieved from allocate. The BufferAllocator is then free to reuse + * memory that was previously handed out. + */ + public void clear(); +} http://git-wip-us.apache.org/repos/asf/jena/blob/3d70d735/jena-tdb/src/main/java/org/apache/jena/tdb/base/file/BufferAllocatorDirect.java ---------------------------------------------------------------------- diff --git a/jena-tdb/src/main/java/org/apache/jena/tdb/base/file/BufferAllocatorDirect.java b/jena-tdb/src/main/java/org/apache/jena/tdb/base/file/BufferAllocatorDirect.java index c51e1a8..667e5c8 100644 --- a/jena-tdb/src/main/java/org/apache/jena/tdb/base/file/BufferAllocatorDirect.java +++ b/jena-tdb/src/main/java/org/apache/jena/tdb/base/file/BufferAllocatorDirect.java @@ -1,46 +1,46 @@ -/* - * 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.tdb.base.file; - -import java.nio.ByteBuffer ; - -/** - * Delegates to {@link ByteBuffer#allocateDirect(int)}. - */ -public class BufferAllocatorDirect implements BufferAllocator -{ - @Override - public ByteBuffer allocate(int capacity) - { - return ByteBuffer.allocateDirect(capacity); - } - - @Override - public void clear() - { - // Do nothing - } - - @Override - public void close() - { - // Do nothing - } - -} +/* + * 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.tdb.base.file; + +import java.nio.ByteBuffer ; + +/** + * Delegates to {@link ByteBuffer#allocateDirect(int)}. + */ +public class BufferAllocatorDirect implements BufferAllocator +{ + @Override + public ByteBuffer allocate(int capacity) + { + return ByteBuffer.allocateDirect(capacity); + } + + @Override + public void clear() + { + // Do nothing + } + + @Override + public void close() + { + // Do nothing + } + +} http://git-wip-us.apache.org/repos/asf/jena/blob/3d70d735/jena-tdb/src/main/java/org/apache/jena/tdb/base/file/BufferAllocatorMapped.java ---------------------------------------------------------------------- diff --git a/jena-tdb/src/main/java/org/apache/jena/tdb/base/file/BufferAllocatorMapped.java b/jena-tdb/src/main/java/org/apache/jena/tdb/base/file/BufferAllocatorMapped.java index 4a51f84..4e20d3c 100644 --- a/jena-tdb/src/main/java/org/apache/jena/tdb/base/file/BufferAllocatorMapped.java +++ b/jena-tdb/src/main/java/org/apache/jena/tdb/base/file/BufferAllocatorMapped.java @@ -1,145 +1,145 @@ -/* - * 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.tdb.base.file; - -import java.io.File ; -import java.io.IOException ; -import java.nio.ByteBuffer ; -import java.nio.MappedByteBuffer ; -import java.nio.channels.FileChannel.MapMode ; -import java.util.ArrayList ; -import java.util.List ; -import java.util.UUID ; - -import org.apache.jena.tdb.sys.SystemTDB ; - -/** - * ByteBuffer access to a temporary file, using memory mapped I/O. The file will - * grow in chunks as necessary by the SystemTDB.SegmentSize. - * <p/> - * This class is not thread-safe. - */ -final public class BufferAllocatorMapped implements BufferAllocator -{ - private final List<MappedByteBuffer> segments; - - private final int segmentSize = SystemTDB.SegmentSize; - private final int blockSize; - private final int blocksPerSegment; - - private final File tmpFile; - private FileBase file; - private int seq = 0; - - public BufferAllocatorMapped(int blockSize) - { - if (blockSize == 0 || blockSize > segmentSize) - throw new IllegalArgumentException("Illegal block size: " + blockSize); - if (segmentSize % blockSize != 0) - throw new IllegalArgumentException(String.format("BufferAllocatorMapped: Segement size(%d) not a multiple of blocksize (%d)", segmentSize, blockSize)) ; - - this.blockSize = blockSize; - blocksPerSegment = segmentSize/blockSize ; - segments = new ArrayList<>(); - - tmpFile = getNewTemporaryFile(); - tmpFile.deleteOnExit(); - } - - /** - * Returns a handle to a temporary file. Does not actually create the file on disk. - */ - private final File getNewTemporaryFile() - { - File sysTempDir = new File(System.getProperty("java.io.tmpdir")) ; - File tmpFile = new File(sysTempDir, "JenaTempByteBuffer-" + UUID.randomUUID().toString() + ".tmp") ; - return tmpFile ; - } - - private final int segment(int id) { return id/blocksPerSegment ; } - private final int byteOffset(int id) { return (id%blocksPerSegment)*blockSize ; } - private final long fileLocation(long segmentNumber) { return segmentNumber*segmentSize ; } - - @Override - public ByteBuffer allocate(int blkSize) - { - if ( blkSize != this.blockSize ) - throw new FileException("Fixed blocksize only: request= "+blkSize+"fixed size="+this.blockSize) ; - - // Create the file lazily - if (null == file) - file = FileBase.create(tmpFile.getPath()); - - // Get and increment the id - int id = seq++; - int seg = segment(id); - int segOff = byteOffset(id); - - MappedByteBuffer segBuffer; - // See if we need to grow the file - if (seg >= segments.size()) - { - try - { - long offset = fileLocation(seg); - segBuffer = file.channel().map(MapMode.READ_WRITE, offset, segmentSize) ; - segments.add(segBuffer); - } - catch (IOException e) - { - throw new FileException("MappedFile.allocate: Segment= " + seg, e); - } - } - else - { - segBuffer = segments.get(seg); - } - - segBuffer.position(segOff); - segBuffer.limit(segOff + blockSize); - - ByteBuffer toReturn = segBuffer.slice(); - - segBuffer.limit(segBuffer.capacity()); - - return toReturn; - } - - @Override - public void clear() - { - // Just reset to the start of the file, we'll allocate overtop of the old memory - seq = 0; - } - - @Override - public void close() - { - // There is no unmap operation for MappedByteBuffers. - // Sun Bug id bug_id=4724038 - // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4724038 - clear(); - segments.clear(); - file.close(); - file = null; - - // May not delete on Windows :/ - tmpFile.delete(); - } -} +/* + * 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.tdb.base.file; + +import java.io.File ; +import java.io.IOException ; +import java.nio.ByteBuffer ; +import java.nio.MappedByteBuffer ; +import java.nio.channels.FileChannel.MapMode ; +import java.util.ArrayList ; +import java.util.List ; +import java.util.UUID ; + +import org.apache.jena.tdb.sys.SystemTDB ; + +/** + * ByteBuffer access to a temporary file, using memory mapped I/O. The file will + * grow in chunks as necessary by the SystemTDB.SegmentSize. + * <p/> + * This class is not thread-safe. + */ +final public class BufferAllocatorMapped implements BufferAllocator +{ + private final List<MappedByteBuffer> segments; + + private final int segmentSize = SystemTDB.SegmentSize; + private final int blockSize; + private final int blocksPerSegment; + + private final File tmpFile; + private FileBase file; + private int seq = 0; + + public BufferAllocatorMapped(int blockSize) + { + if (blockSize == 0 || blockSize > segmentSize) + throw new IllegalArgumentException("Illegal block size: " + blockSize); + if (segmentSize % blockSize != 0) + throw new IllegalArgumentException(String.format("BufferAllocatorMapped: Segement size(%d) not a multiple of blocksize (%d)", segmentSize, blockSize)) ; + + this.blockSize = blockSize; + blocksPerSegment = segmentSize/blockSize ; + segments = new ArrayList<>(); + + tmpFile = getNewTemporaryFile(); + tmpFile.deleteOnExit(); + } + + /** + * Returns a handle to a temporary file. Does not actually create the file on disk. + */ + private final File getNewTemporaryFile() + { + File sysTempDir = new File(System.getProperty("java.io.tmpdir")) ; + File tmpFile = new File(sysTempDir, "JenaTempByteBuffer-" + UUID.randomUUID().toString() + ".tmp") ; + return tmpFile ; + } + + private final int segment(int id) { return id/blocksPerSegment ; } + private final int byteOffset(int id) { return (id%blocksPerSegment)*blockSize ; } + private final long fileLocation(long segmentNumber) { return segmentNumber*segmentSize ; } + + @Override + public ByteBuffer allocate(int blkSize) + { + if ( blkSize != this.blockSize ) + throw new FileException("Fixed blocksize only: request= "+blkSize+"fixed size="+this.blockSize) ; + + // Create the file lazily + if (null == file) + file = FileBase.create(tmpFile.getPath()); + + // Get and increment the id + int id = seq++; + int seg = segment(id); + int segOff = byteOffset(id); + + MappedByteBuffer segBuffer; + // See if we need to grow the file + if (seg >= segments.size()) + { + try + { + long offset = fileLocation(seg); + segBuffer = file.channel().map(MapMode.READ_WRITE, offset, segmentSize) ; + segments.add(segBuffer); + } + catch (IOException e) + { + throw new FileException("MappedFile.allocate: Segment= " + seg, e); + } + } + else + { + segBuffer = segments.get(seg); + } + + segBuffer.position(segOff); + segBuffer.limit(segOff + blockSize); + + ByteBuffer toReturn = segBuffer.slice(); + + segBuffer.limit(segBuffer.capacity()); + + return toReturn; + } + + @Override + public void clear() + { + // Just reset to the start of the file, we'll allocate overtop of the old memory + seq = 0; + } + + @Override + public void close() + { + // There is no unmap operation for MappedByteBuffers. + // Sun Bug id bug_id=4724038 + // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4724038 + clear(); + segments.clear(); + file.close(); + file = null; + + // May not delete on Windows :/ + tmpFile.delete(); + } +} http://git-wip-us.apache.org/repos/asf/jena/blob/3d70d735/jena-tdb/src/main/java/org/apache/jena/tdb/base/file/BufferAllocatorMem.java ---------------------------------------------------------------------- diff --git a/jena-tdb/src/main/java/org/apache/jena/tdb/base/file/BufferAllocatorMem.java b/jena-tdb/src/main/java/org/apache/jena/tdb/base/file/BufferAllocatorMem.java index 4d3bb19..c82dff2 100644 --- a/jena-tdb/src/main/java/org/apache/jena/tdb/base/file/BufferAllocatorMem.java +++ b/jena-tdb/src/main/java/org/apache/jena/tdb/base/file/BufferAllocatorMem.java @@ -1,46 +1,46 @@ -/* - * 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.tdb.base.file; - -import java.nio.ByteBuffer ; - -/** - * Delegates to {@link ByteBuffer#allocate(int)}. - */ -public class BufferAllocatorMem implements BufferAllocator -{ - @Override - public ByteBuffer allocate(int capacity) - { - return ByteBuffer.allocate(capacity); - } - - @Override - public void clear() - { - // Do nothing - } - - @Override - public void close() - { - // Do nothing - } - -} +/* + * 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.tdb.base.file; + +import java.nio.ByteBuffer ; + +/** + * Delegates to {@link ByteBuffer#allocate(int)}. + */ +public class BufferAllocatorMem implements BufferAllocator +{ + @Override + public ByteBuffer allocate(int capacity) + { + return ByteBuffer.allocate(capacity); + } + + @Override + public void clear() + { + // Do nothing + } + + @Override + public void close() + { + // Do nothing + } + +} http://git-wip-us.apache.org/repos/asf/jena/blob/3d70d735/jena-tdb/src/test/java/org/apache/jena/tdb/extra/T_QuadsObjectIsNull.java ---------------------------------------------------------------------- diff --git a/jena-tdb/src/test/java/org/apache/jena/tdb/extra/T_QuadsObjectIsNull.java b/jena-tdb/src/test/java/org/apache/jena/tdb/extra/T_QuadsObjectIsNull.java index ca701df..76218c7 100644 --- a/jena-tdb/src/test/java/org/apache/jena/tdb/extra/T_QuadsObjectIsNull.java +++ b/jena-tdb/src/test/java/org/apache/jena/tdb/extra/T_QuadsObjectIsNull.java @@ -1,110 +1,110 @@ -/* - * 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.tdb.extra ; - -import java.util.Iterator ; - -import org.apache.jena.atlas.lib.FileOps ; -import org.apache.jena.atlas.logging.LogCtl ; -import org.apache.jena.query.ReadWrite ; -import org.apache.jena.sparql.core.Quad ; -import org.apache.jena.sparql.sse.SSE ; -import org.apache.jena.tdb.TDBFactory ; -import org.apache.jena.tdb.base.file.Location ; -import org.apache.jena.tdb.sys.SystemTDB ; -import org.apache.jena.tdb.transaction.DatasetGraphTransaction ; -import org.apache.jena.tdb.transaction.Journal ; -import org.apache.jena.tdb.transaction.JournalControl ; -import org.apache.jena.tdb.transaction.NodeTableTrans ; - -public class T_QuadsObjectIsNull { -// static { -// ARQ.getContext().set(SystemTDB.symFileMode, "direct"); -// TDB.getContext().set(TDB.symUnionDefaultGraph, true); -// } - - static String DIR = "DBX" ; - static Location location = Location.create(DIR) ; - - public static void main(String[] args) { - - if ( false ) - { - LogCtl.enable(SystemTDB.syslog.getName()) ; - LogCtl.enable(Journal.class) ; - LogCtl.enable(JournalControl.class) ; - LogCtl.enable(NodeTableTrans.class) ; - } - if ( false ) - { - String journal = "DBX/journal.jrnl" ; - if ( FileOps.exists(journal)) - JournalControl.print(journal) ; - } - - if ( false ) { - FileOps.ensureDir(DIR) ; - FileOps.clearDirectory(DIR) ; - } - one() ; - } - - public static void write(DatasetGraphTransaction dsg, Quad quad) - { - dsg.begin(ReadWrite.WRITE) ; - dsg.add(quad) ; - if ( ! dsg.contains(quad) ) - throw new RuntimeException("No quad: "+quad) ; - dsg.commit() ; - dsg.end() ; - } - - private static void dump(DatasetGraphTransaction dsg) - { - dsg.begin(ReadWrite.READ); - Iterator<Quad> iter = dsg.find() ; - for ( ; iter.hasNext() ; ) - { - Quad q = iter.next() ; - System.out.println(q) ; - } - //RiotWriter.writeNQuads(System.out, dsg) ; - dsg.commit(); - dsg.end(); - } - - public static void one() - { - Quad q1 = SSE.parseQuad("(<g1> <s1> <p1> '1')") ; - Quad q2 = SSE.parseQuad("(<g2> <s2> <p2> '2')") ; - Quad q3 = SSE.parseQuad("(<g3> <s3> <p3> '3')") ; - - DatasetGraphTransaction dsg = (DatasetGraphTransaction)TDBFactory.createDatasetGraph(location); - System.out.println("Start") ; - dump(dsg) ; - - write(dsg, q1) ; - write(dsg, q2) ; - //write(dsg, q3) ; - System.out.println("Finish") ; - dump(dsg) ; - } - - -} +/* + * 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.tdb.extra ; + +import java.util.Iterator ; + +import org.apache.jena.atlas.lib.FileOps ; +import org.apache.jena.atlas.logging.LogCtl ; +import org.apache.jena.query.ReadWrite ; +import org.apache.jena.sparql.core.Quad ; +import org.apache.jena.sparql.sse.SSE ; +import org.apache.jena.tdb.TDBFactory ; +import org.apache.jena.tdb.base.file.Location ; +import org.apache.jena.tdb.sys.SystemTDB ; +import org.apache.jena.tdb.transaction.DatasetGraphTransaction ; +import org.apache.jena.tdb.transaction.Journal ; +import org.apache.jena.tdb.transaction.JournalControl ; +import org.apache.jena.tdb.transaction.NodeTableTrans ; + +public class T_QuadsObjectIsNull { +// static { +// ARQ.getContext().set(SystemTDB.symFileMode, "direct"); +// TDB.getContext().set(TDB.symUnionDefaultGraph, true); +// } + + static String DIR = "DBX" ; + static Location location = Location.create(DIR) ; + + public static void main(String[] args) { + + if ( false ) + { + LogCtl.enable(SystemTDB.syslog.getName()) ; + LogCtl.enable(Journal.class) ; + LogCtl.enable(JournalControl.class) ; + LogCtl.enable(NodeTableTrans.class) ; + } + if ( false ) + { + String journal = "DBX/journal.jrnl" ; + if ( FileOps.exists(journal)) + JournalControl.print(journal) ; + } + + if ( false ) { + FileOps.ensureDir(DIR) ; + FileOps.clearDirectory(DIR) ; + } + one() ; + } + + public static void write(DatasetGraphTransaction dsg, Quad quad) + { + dsg.begin(ReadWrite.WRITE) ; + dsg.add(quad) ; + if ( ! dsg.contains(quad) ) + throw new RuntimeException("No quad: "+quad) ; + dsg.commit() ; + dsg.end() ; + } + + private static void dump(DatasetGraphTransaction dsg) + { + dsg.begin(ReadWrite.READ); + Iterator<Quad> iter = dsg.find() ; + for ( ; iter.hasNext() ; ) + { + Quad q = iter.next() ; + System.out.println(q) ; + } + //RiotWriter.writeNQuads(System.out, dsg) ; + dsg.commit(); + dsg.end(); + } + + public static void one() + { + Quad q1 = SSE.parseQuad("(<g1> <s1> <p1> '1')") ; + Quad q2 = SSE.parseQuad("(<g2> <s2> <p2> '2')") ; + Quad q3 = SSE.parseQuad("(<g3> <s3> <p3> '3')") ; + + DatasetGraphTransaction dsg = (DatasetGraphTransaction)TDBFactory.createDatasetGraph(location); + System.out.println("Start") ; + dump(dsg) ; + + write(dsg, q1) ; + write(dsg, q2) ; + //write(dsg, q3) ; + System.out.println("Finish") ; + dump(dsg) ; + } + + +} http://git-wip-us.apache.org/repos/asf/jena/blob/3d70d735/jena-tdb/src/test/java/org/apache/jena/tdb/extra/T_TimeoutTDBPattern.java ---------------------------------------------------------------------- diff --git a/jena-tdb/src/test/java/org/apache/jena/tdb/extra/T_TimeoutTDBPattern.java b/jena-tdb/src/test/java/org/apache/jena/tdb/extra/T_TimeoutTDBPattern.java index 107bf2c..9d3e09e 100644 --- a/jena-tdb/src/test/java/org/apache/jena/tdb/extra/T_TimeoutTDBPattern.java +++ b/jena-tdb/src/test/java/org/apache/jena/tdb/extra/T_TimeoutTDBPattern.java @@ -1,112 +1,112 @@ -/** - * 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.tdb.extra; - -import java.text.MessageFormat ; -import java.util.Date ; -import java.util.concurrent.TimeUnit ; - -import org.apache.jena.query.* ; -import org.apache.jena.rdf.model.* ; -import org.apache.jena.tdb.TDBFactory ; - -// From Jena-289. - -public class T_TimeoutTDBPattern -{ - private static final int timeout1_sec = 3; - private static final int timeout2_sec = 5; - - private static final int RESOURCES = 100000; - private static final int COMMIT_EVERY = 1000; - private static final int TRIPLES_PER_RESOURCE = 100; - private static final String RES_NS = "http://example.com/"; - private static final String PROP_NS = "http://example.org/ns/1.0/"; - - public static void main(String[] args) { - String location = "DB_Jena289" ; - Dataset ds = TDBFactory.createDataset(location); - - if (ds.asDatasetGraph().isEmpty()) - create(ds) ; - - // 10M triples. - // No match to { ?a ?b ?c . ?c ?d ?e } - - final String sparql = "SELECT * WHERE { ?a ?b ?c . ?c ?d ?e }"; - - Query query = QueryFactory.create(sparql); - - ds.begin(ReadWrite.READ); - System.out.println(MessageFormat.format("{0,date} {0,time} Executing query [timeout1={1}s timeout2={2}s]: {3}", - new Date(System.currentTimeMillis()), timeout1_sec, timeout2_sec, sparql)); - try(QueryExecution qexec = QueryExecutionFactory.create(query, ds)) { - if ( true ) - qexec.setTimeout(timeout1_sec, TimeUnit.SECONDS, timeout2_sec, TimeUnit.SECONDS); - long start = System.nanoTime() ; - long finish = start ; - ResultSet rs = qexec.execSelect(); - - try { - long x = ResultSetFormatter.consume(rs) ; - finish = System.nanoTime() ; - System.out.println("Results: "+x) ; - } catch (QueryCancelledException ex) - { - finish = System.nanoTime() ; - System.out.println("Cancelled") ; - } - System.out.printf("%.2fs\n",(finish-start)/(1000.0*1000.0*1000.0)) ; - } catch (Throwable t) { - t.printStackTrace(); // OOME - } finally { - ds.end(); - ds.close(); - System.out.println(MessageFormat.format("{0,date} {0,time} Finished", - new Date(System.currentTimeMillis()))); - } - } - - private static void create(Dataset ds) - { - for (int iR = 0; iR < RESOURCES; iR++) { // 100,000 - if (iR % COMMIT_EVERY == 0) { - if (ds.isInTransaction()) { - ds.commit(); - ds.end(); - } - ds.begin(ReadWrite.WRITE); - } - - Model model = ModelFactory.createDefaultModel(); - Resource res = model.createResource(RES_NS + "resource" + iR); - for (int iP = 0; iP < TRIPLES_PER_RESOURCE; iP++) { // 100 - Property prop = ResourceFactory.createProperty(PROP_NS, "property" + iP); - model.add(res, prop, model.createTypedLiteral("Property value " + iP)); - } - //ds.addNamedModel(res.getURI(), model); - ds.getDefaultModel().add(model); - System.out.println("Created " + res.getURI()); - } - ds.commit(); - ds.end(); - } -} - - +/** + * 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.tdb.extra; + +import java.text.MessageFormat ; +import java.util.Date ; +import java.util.concurrent.TimeUnit ; + +import org.apache.jena.query.* ; +import org.apache.jena.rdf.model.* ; +import org.apache.jena.tdb.TDBFactory ; + +// From Jena-289. + +public class T_TimeoutTDBPattern +{ + private static final int timeout1_sec = 3; + private static final int timeout2_sec = 5; + + private static final int RESOURCES = 100000; + private static final int COMMIT_EVERY = 1000; + private static final int TRIPLES_PER_RESOURCE = 100; + private static final String RES_NS = "http://example.com/"; + private static final String PROP_NS = "http://example.org/ns/1.0/"; + + public static void main(String[] args) { + String location = "DB_Jena289" ; + Dataset ds = TDBFactory.createDataset(location); + + if (ds.asDatasetGraph().isEmpty()) + create(ds) ; + + // 10M triples. + // No match to { ?a ?b ?c . ?c ?d ?e } + + final String sparql = "SELECT * WHERE { ?a ?b ?c . ?c ?d ?e }"; + + Query query = QueryFactory.create(sparql); + + ds.begin(ReadWrite.READ); + System.out.println(MessageFormat.format("{0,date} {0,time} Executing query [timeout1={1}s timeout2={2}s]: {3}", + new Date(System.currentTimeMillis()), timeout1_sec, timeout2_sec, sparql)); + try(QueryExecution qexec = QueryExecutionFactory.create(query, ds)) { + if ( true ) + qexec.setTimeout(timeout1_sec, TimeUnit.SECONDS, timeout2_sec, TimeUnit.SECONDS); + long start = System.nanoTime() ; + long finish = start ; + ResultSet rs = qexec.execSelect(); + + try { + long x = ResultSetFormatter.consume(rs) ; + finish = System.nanoTime() ; + System.out.println("Results: "+x) ; + } catch (QueryCancelledException ex) + { + finish = System.nanoTime() ; + System.out.println("Cancelled") ; + } + System.out.printf("%.2fs\n",(finish-start)/(1000.0*1000.0*1000.0)) ; + } catch (Throwable t) { + t.printStackTrace(); // OOME + } finally { + ds.end(); + ds.close(); + System.out.println(MessageFormat.format("{0,date} {0,time} Finished", + new Date(System.currentTimeMillis()))); + } + } + + private static void create(Dataset ds) + { + for (int iR = 0; iR < RESOURCES; iR++) { // 100,000 + if (iR % COMMIT_EVERY == 0) { + if (ds.isInTransaction()) { + ds.commit(); + ds.end(); + } + ds.begin(ReadWrite.WRITE); + } + + Model model = ModelFactory.createDefaultModel(); + Resource res = model.createResource(RES_NS + "resource" + iR); + for (int iP = 0; iP < TRIPLES_PER_RESOURCE; iP++) { // 100 + Property prop = ResourceFactory.createProperty(PROP_NS, "property" + iP); + model.add(res, prop, model.createTypedLiteral("Property value " + iP)); + } + //ds.addNamedModel(res.getURI(), model); + ds.getDefaultModel().add(model); + System.out.println("Created " + res.getURI()); + } + ds.commit(); + ds.end(); + } +} + + http://git-wip-us.apache.org/repos/asf/jena/blob/3d70d735/jena-tdb/src/test/java/org/apache/jena/tdb/graph/TestDatasetGraphAccessorTDB.java ---------------------------------------------------------------------- diff --git a/jena-tdb/src/test/java/org/apache/jena/tdb/graph/TestDatasetGraphAccessorTDB.java b/jena-tdb/src/test/java/org/apache/jena/tdb/graph/TestDatasetGraphAccessorTDB.java index 1d8d1f8..d501dbf 100644 --- a/jena-tdb/src/test/java/org/apache/jena/tdb/graph/TestDatasetGraphAccessorTDB.java +++ b/jena-tdb/src/test/java/org/apache/jena/tdb/graph/TestDatasetGraphAccessorTDB.java @@ -16,20 +16,20 @@ * limitations under the License. */ -package org.apache.jena.tdb.graph; - +package org.apache.jena.tdb.graph; + import org.apache.jena.query.DatasetAccessorFactory ; import org.apache.jena.sparql.core.DatasetGraph ; import org.apache.jena.tdb.TDBFactory ; import org.apache.jena.web.DatasetGraphAccessor ; import org.apache.jena.web.AbstractTestDatasetGraphAccessor ; - -public class TestDatasetGraphAccessorTDB extends AbstractTestDatasetGraphAccessor -{ - @Override - protected DatasetGraphAccessor getDatasetUpdater() - { - DatasetGraph dsg = TDBFactory.createDatasetGraph() ; - return DatasetAccessorFactory.make(dsg) ; - } + +public class TestDatasetGraphAccessorTDB extends AbstractTestDatasetGraphAccessor +{ + @Override + protected DatasetGraphAccessor getDatasetUpdater() + { + DatasetGraph dsg = TDBFactory.createDatasetGraph() ; + return DatasetAccessorFactory.make(dsg) ; + } }
