You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@clerezza.apache.org by rw...@apache.org on 2012/06/19 12:31:10 UTC
svn commit: r1351644 - in
/incubator/clerezza/trunk/parent/rdf.jena.tdb.storage/src:
main/java/org/apache/clerezza/rdf/jena/tdb/storage/
main/resources/OSGI-INF/ main/resources/OSGI-INF/metatype/
test/java/org/apache/clerezza/rdf/jena/tdb/storage/
Author: rwesten
Date: Tue Jun 19 10:31:09 2012
New Revision: 1351644
URL: http://svn.apache.org/viewvc?rev=1351644&view=rev
Log:
CLEREZZA-691: Implementation of the Single Jena TDB dataset provider.
Changes to the last patch:
* Configuration policy is now OPTIONAL - this results in the creation of a default instance (with the servicePID equals to the class name of the component - "org.apache.clerezza.rdf.jena.tdb.storage.SingleTdbDatasetTcProvider"
* data directory is now a optional parameter: By default the {bundle-data-dir}/singleTdb/{servicePID} is used.
* Default priority is now higher as of the old TdbDatasetTcProvider (as suggested by Reto) - NOTE this might cause a change of the default TcProvider used by the TcManager
Added:
incubator/clerezza/trunk/parent/rdf.jena.tdb.storage/src/main/java/org/apache/clerezza/rdf/jena/tdb/storage/SingleTdbDatasetTcProvider.java (with props)
incubator/clerezza/trunk/parent/rdf.jena.tdb.storage/src/main/resources/OSGI-INF/
incubator/clerezza/trunk/parent/rdf.jena.tdb.storage/src/main/resources/OSGI-INF/metatype/
incubator/clerezza/trunk/parent/rdf.jena.tdb.storage/src/main/resources/OSGI-INF/metatype/metatype.properties (with props)
incubator/clerezza/trunk/parent/rdf.jena.tdb.storage/src/test/java/org/apache/clerezza/rdf/jena/tdb/storage/MultiThreadedSingleTdbDatasetTest.java (with props)
incubator/clerezza/trunk/parent/rdf.jena.tdb.storage/src/test/java/org/apache/clerezza/rdf/jena/tdb/storage/SingleTdbDatasetMGraphTest.java (with props)
incubator/clerezza/trunk/parent/rdf.jena.tdb.storage/src/test/java/org/apache/clerezza/rdf/jena/tdb/storage/SingleTdbDatasetTcProviderTest.java (with props)
Added: incubator/clerezza/trunk/parent/rdf.jena.tdb.storage/src/main/java/org/apache/clerezza/rdf/jena/tdb/storage/SingleTdbDatasetTcProvider.java
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/parent/rdf.jena.tdb.storage/src/main/java/org/apache/clerezza/rdf/jena/tdb/storage/SingleTdbDatasetTcProvider.java?rev=1351644&view=auto
==============================================================================
--- incubator/clerezza/trunk/parent/rdf.jena.tdb.storage/src/main/java/org/apache/clerezza/rdf/jena/tdb/storage/SingleTdbDatasetTcProvider.java (added)
+++ incubator/clerezza/trunk/parent/rdf.jena.tdb.storage/src/main/java/org/apache/clerezza/rdf/jena/tdb/storage/SingleTdbDatasetTcProvider.java Tue Jun 19 10:31:09 2012
@@ -0,0 +1,914 @@
+package org.apache.clerezza.rdf.jena.tdb.storage;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.math.BigDecimal;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.nio.charset.Charset;
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map.Entry;
+import java.util.Set;
+
+import org.apache.clerezza.rdf.core.Graph;
+import org.apache.clerezza.rdf.core.MGraph;
+import org.apache.clerezza.rdf.core.TripleCollection;
+import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.clerezza.rdf.core.access.EntityAlreadyExistsException;
+import org.apache.clerezza.rdf.core.access.EntityUndeletableException;
+import org.apache.clerezza.rdf.core.access.LockableMGraphWrapper;
+import org.apache.clerezza.rdf.core.access.NoSuchEntityException;
+import org.apache.clerezza.rdf.core.access.TcProvider;
+import org.apache.clerezza.rdf.core.access.WeightedTcProvider;
+import org.apache.clerezza.rdf.core.impl.SimpleGraph;
+import org.apache.clerezza.rdf.core.impl.util.PrivilegedGraphWrapper;
+import org.apache.clerezza.rdf.core.impl.util.PrivilegedMGraphWrapper;
+import org.apache.clerezza.rdf.jena.storage.JenaGraphAdaptor;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.ConfigurationPolicy;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Properties;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Service;
+import org.openjena.atlas.lib.Tuple;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.service.cm.ConfigurationAdmin;
+import org.osgi.service.cm.ConfigurationException;
+import org.osgi.service.component.ComponentContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.hp.hpl.jena.query.Dataset;
+import com.hp.hpl.jena.rdf.model.Model;
+import com.hp.hpl.jena.sparql.core.DatasetGraph;
+import com.hp.hpl.jena.sparql.core.Quad;
+import com.hp.hpl.jena.tdb.TDB;
+import com.hp.hpl.jena.tdb.TDBFactory;
+/**
+ * {@link WeightedTcProvider} implementation for Jena TDB that uses a single
+ * {@link TDBFactory#createDataset(String) Dataset} to store all created
+ * {@link Graph} and {@link MGraph} instances.<p>
+ * The {@link #TDB_DIR} is uses to configure the directory on the disc. It
+ * supports property substitution <code>'${property}'</code> based on properties defined
+ * in the {@link BundleContext#getProperty(String)} and
+ * {@link System#getProperty(String)}. This is to easily allow configurations
+ * such as <code>"${myHome}/myRdfStore"</code><p>
+ * The {@link #DEFAULT_GRAPH_NAME} property can be used to define the
+ * name of the Graph that exposes the {@link Dataset#getDefaultModel()} as
+ * both {@link TcProvider#getGraph(UriRef)} and {@link TcProvider#getMGraph(UriRef)}.
+ * This easily allows to access the union graph of the Jena TDB dataset.<p>
+ * This {@link TcProvider} {@link ConfigurationPolicy#REQUIRE requires} an
+ * configuration and uses the {@link Component#configurationFactory()
+ * configuration factory}. Therefore it will be bot active until a valid
+ * configuration is parsed to the {@link ConfigurationAdmin} service. However
+ * it supports multiple instances to be created.<p>
+ * Users that want to use multiple instances will need to use special filters
+ * to ensure that the correct instance is injected to components. As by default
+ * the instance with the highest {@link #WEIGHT} will be used by Clerezza
+ * to create instances. A good practice to filter for multiple instances is
+ * to add an additional user defined key to the configuration that can later
+ * be used for filtering. Such additional keys will be savely ignored by
+ * this implementation.<p>
+ *
+ * @author Rupert Westenthaler
+ *
+ */
+@Component(metatype=true, immediate=true,
+ configurationFactory=true, policy=ConfigurationPolicy.OPTIONAL)
+@Service(WeightedTcProvider.class)
+@Properties(value={
+ @Property(name=SingleTdbDatasetTcProvider.TDB_DIR),
+ @Property(name=SingleTdbDatasetTcProvider.DEFAULT_GRAPH_NAME),
+ @Property(name=SingleTdbDatasetTcProvider.SYNC_INTERVAL, intValue=SingleTdbDatasetTcProvider.DEFAULT_SYNC_INTERVAL),
+ @Property(name=SingleTdbDatasetTcProvider.WEIGHT, intValue=106)
+})
+public class SingleTdbDatasetTcProvider implements WeightedTcProvider {
+
+ public static final String TDB_DIR = "tdb-dir";
+ public static final String DEFAULT_GRAPH_NAME = "default-graph-name";
+ public static final String WEIGHT = "weight";
+ public static final String SYNC_INTERVAL = "sync-interval";
+ public static final String USE_GRAPH_NAME_SUFFIXES = "use-graph-name-suffixes";
+
+ public static final int DEFAULT_SYNC_INTERVAL = 6;
+ public static final int MIN_SYNC_INTERVAL = 3;
+
+ private final Logger log = LoggerFactory.getLogger(SingleTdbDatasetTcProvider.class);
+
+ private int weight;
+ private int syncInterval = DEFAULT_SYNC_INTERVAL;
+ private SyncThread syncThread;
+
+ private Dataset dataset;
+
+ private File graphConfigFile;
+ private File mGraphConfigFile;
+
+ private HashMap<UriRef,ModelGraph> initModels = new HashMap<UriRef,ModelGraph>();
+ /**
+ * the {@link UriRef}s of the graphs (read-only)
+ */
+ private Set<UriRef> graphNames;
+ private Set<UriRef> mGraphNames;
+ private UriRef defaultGraphName;
+ /**
+ * Represents the Jena {@link Model} and the Clerezza {@link Graph} or
+ * {@link MGraph}. It also provide access to the {@link JenaGraphAdaptor}
+ * so that this component can add parsed data to {@link Graph}s created
+ * by calls to {@link SingleTdbDatasetTcProvider#createGraph(UriRef, TripleCollection)}.
+ * @author Rupert Westenthaler
+ *
+ */
+ private class ModelGraph {
+ /**
+ * The Jena Model
+ */
+ private final Model model;
+ /**
+ * The JenaGraphAdapter. Note that in case of read-only in anonymous
+ * subclass is used that prevents the creation of an in-memory copy
+ * of the data when calling {@link JenaGraphAdaptor#getGraph()}.
+ */
+ private JenaGraphAdaptor jenaAdatper;
+ /**
+ * The {@link Graph}(in case of read-only) or {@link MGraph} (if read/write)
+ * that can be shared with other components. The instance stored by this
+ * variable will use all the required Wrappers such as such as
+ * {@link LockableMGraphWrapper lockable} and {@link PrivilegedMGraphWrapper
+ * privileged}.
+ */
+ private TripleCollection graph;
+ /**
+ * keeps the state if this represnets an {@link Graph} (read-only) or
+ * {@link MGraph}(read/write) ModelGraph.
+ */
+ private final boolean readWrite;
+
+ /**
+ * Constructs and initialises the ModelGraph
+ * @param model the Jena Model
+ * @param readWrite if the Clerezza conterpart should be read- and
+ * write-able or read-only.
+ */
+ protected ModelGraph(Model model, boolean readWrite){
+ if(model == null){
+ throw new IllegalArgumentException("The parsed Model MUST NOT be NULL");
+ }
+ this.model = model;
+ this.readWrite = readWrite;
+ if(!readWrite){ //construct an graph
+ jenaAdatper = new JenaGraphAdaptor(model.getGraph()){
+ /**
+ * Ensure that no in-memory copies are created for read only
+ * Jena Graphs
+ * @return
+ */
+ @Override
+ public Graph getGraph() {
+ return new SimpleGraph(this,true);
+ }
+ };
+ graph = new PrivilegedGraphWrapper(jenaAdatper.getGraph());
+ } else { //construct an MGraph
+ jenaAdatper = new JenaGraphAdaptor(model.getGraph());
+ this.graph = new LockableMGraphWrapper(
+ new PrivilegedMGraphWrapper(jenaAdatper));
+ }
+ }
+ /**
+ * The {@link JenaGraphAdaptor}. For internal use only! Do not pass
+ * this instance to other components. Use {@link #getGraph()} and
+ * {@link #getMGraph()} instead!
+ * @return the plain {@link JenaGraphAdaptor}
+ */
+ protected JenaGraphAdaptor getJenaAdapter(){
+ return jenaAdatper;
+ }
+// public boolean isReadonly(){
+// return !readWrite;
+// }
+ public boolean isReadWrite(){
+ return readWrite;
+ }
+ /**
+ * Getter for the {@link MGraph}
+ * @return the {@link MGraph}
+ * @throws IllegalStateException if this {@link ModelGraph} is NOT
+ * {@link #readWrite}
+ */
+ public MGraph getMGraph(){
+ if(!readWrite){
+ throw new IllegalStateException("Unable to return MGraph for read-only models");
+ }
+ return (MGraph)graph;
+ }
+ /**
+ * Getter for the {@link Graph}
+ * @return the {@link Graph}
+ * @throws IllegalStateException if this {@link ModelGraph} is
+ * {@link #readWrite}
+ */
+ public Graph getGraph() {
+ if(readWrite){
+ throw new IllegalStateException("Unable to return Graph for read/write models.");
+ }
+ return (Graph)graph;
+ }
+ /**
+ * closes this ModelGraph and frees up all Jena TDB related resources.
+ */
+ public void close(){
+ this.graph = null;
+ this.jenaAdatper = null;
+ sync();
+ this.model.close();
+ }
+ /**
+ * Synchronize the Jena Model with the fiel system by calling
+ * {@link TDB#sync(Model)}
+ */
+ public void sync(){
+ TDB.sync(model);
+ }
+ /**
+ * Removes all triples from the Jena Model and than calls {@link #close()}
+ * to free remaining resources. Note that in Jena TDB a named model is
+ * deleted if no more triples with the given context are present within
+ * the {@link Quad} store of the Jena TDB {@link DatasetGraph}.
+ */
+ public void delete(){
+ this.model.removeAll();
+ close();
+ }
+
+ }
+ /**
+ * This backround thread ensures that changes to {@link Model}s are
+ * synchronized with the file system. Only {@link ModelGraph}s where
+ * <code>{@link ModelGraph#isReadWrite()} == true</code> are synced.<p>
+ * This is similar to the synchronize thread used by the {@link TdbTcProvider}.
+ * This thread is started during the
+ * {@link SingleTdbDatasetTcProvider#activate(ComponentContext) activation}
+ * ad the shutdown is requested during
+ * {@link SingleTdbDatasetTcProvider#deactivate(ComponentContext) deactivation}
+ */
+ class SyncThread extends Thread {
+ private boolean stopRequested = false;
+
+ @Override
+ public void run() {
+ while (!stopRequested) {
+ try {
+ Thread.sleep(syncInterval*1000);
+ } catch (InterruptedException ex) {
+ interrupt();
+ }
+ if (!stopRequested) {
+ synchronized (dataset) {
+ for(ModelGraph mg : initModels.values()){
+ if(mg.isReadWrite()){
+ mg.sync();
+ } //else we do not need to sync read-only models
+ }
+ }
+ }
+ }
+ }
+
+ public void requestStop() {
+ stopRequested = true;
+ }
+ }
+ /**
+ * Defualt constructor used by OSGI
+ */
+ public SingleTdbDatasetTcProvider(){}
+
+ /**
+ * Creates a TDB single dataset {@link TcProvider} based on the parsed
+ * configuration.<p>
+ * The following properties are supported:<ul>
+ * <li> {@link #TDB_DIR} (required): The directory used by Jena TDB. Property
+ * substitution "${proeprty-name}" with {@link System#getProperties()} is
+ * supported.
+ * <li> {@link #DEFAULT_GRAPH_NAME}: The name ({@link UriRef}) of the
+ * {@link Graph} that exports the union graph. This graph allows to query
+ * triples in any named model managed by this {@link TcProvider}.
+ * <li> {@link #SYNC_INTERVAL}: The sync interval that
+ * is used to write changes in the graph to the file system. If missing
+ * the {@link #DEFAULT_SYNC_INTERVAL} is used. Values lower than
+ * {@link #MIN_SYNC_INTERVAL} are ignored
+ * <li>{@link #WEIGHT}: The weight of this {@link TcProvider}. If missing
+ * <code>0</code> is used as weight.
+ * </ul>
+ * <b>NOTE</b> Users need to call {@link #close()} to free up system
+ * resources when they are no longer need this instance.
+ * @param config The configuration
+ * @throws IOException the
+ * @throws ConfigurationException
+ */
+ public SingleTdbDatasetTcProvider(Dictionary<String,Object> config) throws ConfigurationException, IOException{
+ activate(null,config);
+ }
+ /**
+ * Activate method sed by OSGI
+ * @param ctx
+ * @throws ConfigurationException
+ * @throws IOException
+ */
+ @Activate
+ @SuppressWarnings("unchecked")
+ protected void activate(ComponentContext ctx) throws ConfigurationException, IOException {
+ activate(ctx.getBundleContext(),ctx.getProperties());
+ }
+ /**
+ * Internally used for activation to support the instantiation via
+ * {@link #SingleTdbDatasetTcProvider(Dictionary)} - to be used outside
+ * an OSGI container.
+ * @param bc the BundleContext or <code>null</code> if activating outside
+ * an OSGI container. The BundleContext is just used to lookup properties
+ * for {@link #substituteProperty(String, BundleContext)}.
+ * @param config The configuration for this Instance. Note that {@link #TDB_DIR}
+ * is required to be present.
+ * @throws ConfigurationException if the parsed configuration is invalid
+ * @throws IOException on any error while creating/accesing the Jena TDB
+ * directory.
+ */
+ private void activate(BundleContext bc,Dictionary<String,Object> config) throws ConfigurationException, IOException {
+ log.info("Activating singe Dataset TDB provider");
+ Object value = config.get(WEIGHT);
+ if(value instanceof Number){
+ weight = ((Number)value).intValue();
+ } else if(value != null){
+ try {
+ weight = new BigDecimal(value.toString()).intValueExact();
+ } catch (RuntimeException e) {
+ throw new ConfigurationException(WEIGHT, "Unable to parse integer weight!", e);
+ }
+ } else { //weight not defined
+ weight = 0;
+ }
+ value = config.get(SYNC_INTERVAL);
+ if(value instanceof Number){
+ syncInterval = Math.max(((Number)value).intValue(),MIN_SYNC_INTERVAL);
+ } else if(value != null){
+ try {
+ syncInterval = Math.max(new BigDecimal(value.toString()).intValueExact(),MIN_SYNC_INTERVAL);
+ } catch (RuntimeException e) {
+ throw new ConfigurationException(SYNC_INTERVAL, "Unable to parse integer weight!", e);
+ }
+ } else { //weight not defined
+ syncInterval = DEFAULT_SYNC_INTERVAL;
+ }
+ value = config.get(TDB_DIR);
+ File dataDir;
+ if(value != null && !value.toString().isEmpty()){
+ dataDir = new File(substituteProperty(value.toString(),bc)).getAbsoluteFile();
+ } else {
+ value = config.get(Constants.SERVICE_PID);
+ if(value == null){
+ throw new ConfigurationException(TDB_DIR, "No Data Directory for "
+ + "the Jena TDB store parsed. Also unable to use the "
+ + "'service.pid' property as default because this property "
+ + "is not present in the parsed configuration.");
+ }
+ dataDir = bc.getDataFile("singleTdb"+File.separatorChar+value.toString());
+ log.info("No TDB directory parsed - use default '{}'",dataDir);
+ }
+ //parse the default graph name
+ value = config.get(DEFAULT_GRAPH_NAME);
+ if(value != null && !value.toString().isEmpty()){
+ try {
+ new URI(value.toString());
+ defaultGraphName = new UriRef(value.toString());
+ } catch (URISyntaxException e) {
+ throw new ConfigurationException(DEFAULT_GRAPH_NAME, "The parsed name '"
+ + value + "'for the default graph (union over all "
+ + "named graphs managed by this Jena TDB dataset) MUST BE "
+ + "an valid URI or NULL do deactivate this feature!",e);
+ }
+ } else {
+ defaultGraphName = null; //deactivate the default graph name
+ }
+
+ //validate the parsed directory!
+ if(!dataDir.exists()){
+ if(dataDir.mkdirs()){
+ log.info("Created Jena TDB data directory {}",dataDir);
+ } else {
+ throw new ConfigurationException(TDB_DIR, "Unable to create Jena TDB data directory '"+dataDir+"'!");
+ }
+ } else if(!dataDir.isDirectory()){
+ throw new ConfigurationException("tdb.dir", "Configured jena TDB data directory '"
+ + dataDir+"' already exists, but is not a Directory!");
+ } //else exists and is a directory ... nothing to do
+ TDB.getContext().set(TDB.symUnionDefaultGraph, true);
+ dataset = TDBFactory.createDataset(dataDir.getAbsolutePath());
+
+ //init the graph config (stores the graph and mgraph names in a config file)
+ initGraphConfigs(dataDir,config);
+
+ //finally ensure the the defualtGraphName is not also used as a graph/mgraph name
+ if(graphNames.contains(defaultGraphName)){
+ throw new ConfigurationException(DEFAULT_GRAPH_NAME, "The configured default graph name '"
+ +defaultGraphName+"' is also used as a Graph name!");
+ }
+ if(mGraphNames.contains(defaultGraphName)){
+ throw new ConfigurationException(DEFAULT_GRAPH_NAME, "The configured default graph name '"
+ +defaultGraphName+"' is also used as a MGraph name!");
+ }
+
+ syncThread = new SyncThread();
+ syncThread.setDaemon(true);
+ syncThread.setName("SyncDaemon for Jena TDB "+dataDir.getAbsolutePath());
+ syncThread.start();
+ }
+ /**
+ * call close in finalisation
+ */
+ @Override
+ protected void finalize() throws Throwable {
+ close();
+ super.finalize();
+ }
+ /**
+ * Closes this {@link TcProvider} instance and frees up all system resources.
+ * This method needs only to be called when using this TcProvider outside
+ * an OSGI environment.
+ */
+ public void close(){
+ deactivate(null);
+ }
+ /**
+ * Deactivates this component. Called by the OSGI environment if this
+ * component gets deactivated.
+ * @param ctx the ComponentContext. May be <code>null</code>
+ */
+ @Deactivate
+ protected void deactivate(ComponentContext ctx) {
+ if(dataset != null){ //avoid NPE on multiple calls
+ synchronized (dataset) {
+ for(ModelGraph mg : initModels.values()){
+ mg.close(); //close also syncs!
+ }
+ TDB.sync(dataset);
+ dataset.close();
+ dataset = null;
+ }
+ }
+ if(syncThread != null){
+ syncThread.requestStop();
+ syncThread = null;
+ }
+ initModels = null;
+ dataset = null;
+ graphConfigFile = null;
+ graphNames = null;
+ mGraphConfigFile = null;
+ mGraphNames = null;
+ }
+
+ /**
+ * Internal method used to retrieve an existing Jena {@link ModelGraph}
+ * instance from {@link #initModels} or initialises a new Jena TDB {@link Model}
+ * and Clerezza {@link Graph}s/{@link MGraph}s.
+ * @param name the name of the Graph to initialise/create
+ * @param readWrite if <code>true</code> a {@link MGraph} is initialised.
+ * Otherwise a {@link Graph} is created.
+ * @param create if this method is allowed to create an new {@link Model} or
+ * if an already existing model is initialised.
+ * @return the initialised {@link Model} and @link Graph} or {@link MGraph}.
+ * The returned instance will be also cached in {@link #initModels}.
+ * @throws NoSuchEntityException If <code>create == false</code> and no
+ * {@link Model} for the parsed <code>name</code> exists.
+ */
+ private ModelGraph getModelGraph(UriRef name, boolean readWrite,boolean create) throws NoSuchEntityException {
+ ModelGraph modelGraph;
+ synchronized (dataset) {
+ modelGraph = initModels.get(name);
+ if(modelGraph != null && create){
+ throw new EntityAlreadyExistsException(name);
+ } else if(modelGraph == null){
+ String modelName = name.getUnicodeString();
+ modelGraph = new ModelGraph(name.equals(defaultGraphName) ?
+ dataset.getNamedModel("urn:x-arq:UnionGraph") :
+ dataset.getNamedModel(modelName),readWrite);
+ this.initModels.put(name, modelGraph);
+ }
+ }
+ return modelGraph;
+ }
+ /*
+ * (non-Javadoc)
+ * @see org.apache.clerezza.rdf.core.access.TcProvider#getGraph(org.apache.clerezza.rdf.core.UriRef)
+ */
+ @Override
+ public Graph getGraph(UriRef name) throws NoSuchEntityException {
+ if(name == null){
+ throw new IllegalArgumentException("The parsed Graph UriRef MUST NOT be NULL!");
+ }
+ synchronized (dataset) {
+ if(graphNames.contains(name) || name.equals(defaultGraphName)){
+ return getModelGraph(name,false,false).getGraph();
+ } else {
+ throw new NoSuchEntityException(name);
+ }
+ }
+ }
+ /*
+ * (non-Javadoc)
+ * @see org.apache.clerezza.rdf.core.access.TcProvider#getMGraph(org.apache.clerezza.rdf.core.UriRef)
+ */
+ @Override
+ public MGraph getMGraph(UriRef name) throws NoSuchEntityException {
+ if(name == null){
+ throw new IllegalArgumentException("The parsed Graph UriRef MUST NOT be NULL!");
+ }
+ synchronized (dataset) {
+ if(mGraphNames.contains(name)){
+ return getModelGraph(name,true,false).getMGraph();
+ } else {
+ throw new NoSuchEntityException(name);
+ }
+ }
+ }
+ /*
+ * (non-Javadoc)
+ * @see org.apache.clerezza.rdf.core.access.TcProvider#getTriples(org.apache.clerezza.rdf.core.UriRef)
+ */
+ @Override
+ public TripleCollection getTriples(UriRef name) throws NoSuchEntityException {
+ if(name == null){
+ throw new IllegalArgumentException("The parsed Graph UriRef MUST NOT be NULL!");
+ }
+ synchronized (dataset) {
+ if(graphNames.contains(name)){
+ return getGraph(name);
+ } else if(mGraphNames.contains(name) || name.equals(defaultGraphName)){
+ return getMGraph(name);
+ } else {
+ throw new NoSuchEntityException(name);
+ }
+ }
+ }
+ /*
+ * (non-Javadoc)
+ * @see org.apache.clerezza.rdf.core.access.TcProvider#listGraphs()
+ */
+ @Override
+ public Set<UriRef> listGraphs() {
+ HashSet<UriRef> names = new HashSet<UriRef>(graphNames);
+ if(defaultGraphName != null){
+ names.add(defaultGraphName);
+ }
+ return names;
+ }
+ /*
+ * (non-Javadoc)
+ * @see org.apache.clerezza.rdf.core.access.TcProvider#listMGraphs()
+ */
+ @Override
+ public Set<UriRef> listMGraphs() {
+ Set<UriRef> tcNames = listTripleCollections();
+ tcNames.removeAll(graphNames);
+ return tcNames;
+ }
+ /*
+ * (non-Javadoc)
+ * @see org.apache.clerezza.rdf.core.access.TcProvider#listTripleCollections()
+ */
+ @Override
+ public Set<UriRef> listTripleCollections() {
+ Set<UriRef> graphNames = new HashSet<UriRef>();
+ synchronized (dataset) {
+ for(Iterator<String> names = dataset.listNames();
+ names.hasNext();
+ graphNames.add(new UriRef(names.next())));
+ }
+ if(defaultGraphName != null){
+ graphNames.add(defaultGraphName);
+ }
+ return graphNames;
+ }
+ /*
+ * (non-Javadoc)
+ * @see org.apache.clerezza.rdf.core.access.TcProvider#createMGraph(org.apache.clerezza.rdf.core.UriRef)
+ */
+ @Override
+ public MGraph createMGraph(UriRef name) throws UnsupportedOperationException,
+ EntityAlreadyExistsException {
+ if(name == null){
+ throw new IllegalArgumentException("The parsed MGrpah name MUST NOT be NULL!");
+ }
+ synchronized (dataset) {
+ if(graphNames.contains(name) || mGraphNames.contains(name) || name.equals(defaultGraphName)){
+ throw new EntityAlreadyExistsException(name);
+ }
+ MGraph graph = getModelGraph(name,true,true).getMGraph();
+ mGraphNames.add(name);
+ try {
+ writeMGraphConfig();
+ } catch (IOException e) {
+ throw new IllegalStateException("Unable to wirte MGraphName config file '"
+ + mGraphConfigFile+"'!",e);
+ }
+ return graph;
+ }
+ }
+ /*
+ * (non-Javadoc)
+ * @see org.apache.clerezza.rdf.core.access.TcProvider#createGraph(org.apache.clerezza.rdf.core.UriRef, org.apache.clerezza.rdf.core.TripleCollection)
+ */
+ @Override
+ public Graph createGraph(UriRef name, TripleCollection triples) throws UnsupportedOperationException,
+ EntityAlreadyExistsException {
+ if(name == null){
+ throw new IllegalArgumentException("The parsed Grpah name MUST NOT be NULL!");
+ }
+ ModelGraph mg;
+ synchronized (dataset) {
+ if(graphNames.contains(name) || mGraphNames.contains(name) || name.equals(defaultGraphName)){
+ throw new EntityAlreadyExistsException(name);
+ }
+ mg = getModelGraph(name,false,true);
+ graphNames.add(name);
+ try {
+ writeGraphConfig();
+ } catch (IOException e) {
+ throw new IllegalStateException("Unable to wirte GraphName config file '"
+ + graphConfigFile+"'!",e);
+ }
+ }
+ //add the parsed data!
+ if(triples != null) { //load the initial and final set of triples
+ mg.getJenaAdapter().addAll(triples);
+ synchronized(dataset){
+ mg.sync();
+ }
+ }
+ return mg.getGraph();
+ }
+ /*
+ * (non-Javadoc)
+ * @see org.apache.clerezza.rdf.core.access.TcProvider#deleteTripleCollection(org.apache.clerezza.rdf.core.UriRef)
+ */
+ @Override
+ public void deleteTripleCollection(UriRef name) throws UnsupportedOperationException,
+ NoSuchEntityException,
+ EntityUndeletableException {
+ if(name == null){
+ throw new IllegalArgumentException("The parsed MGrpah name MUST NOT be NULL!");
+ }
+ synchronized (dataset) {
+ if(mGraphNames.remove(name)){
+ try {
+ writeMGraphConfig();
+ } catch (IOException e){
+ mGraphNames.add(name);//make it consistent with the file
+ throw new IllegalStateException("Unable to wirte MGraphName config file '"
+ + graphConfigFile+"'!",e);
+ }
+ ModelGraph mg = getModelGraph(name, true, false);
+ mg.delete();
+ } else if(graphNames.remove(name)){
+ try {
+ writeGraphConfig();
+ } catch (IOException e){
+ graphNames.add(name); //make it consistent with the file
+ throw new IllegalStateException("Unable to wirte GraphName config file '"
+ + graphConfigFile+"'!",e);
+ }
+ ModelGraph mg = getModelGraph(name, false, false);
+ mg.delete();
+ } else if (name.equals(defaultGraphName)){
+ throw new EntityUndeletableException(defaultGraphName);
+ }
+ //delete the graph from the initModels list
+ initModels.remove(name);
+ }
+ }
+ /*
+ * (non-Javadoc)
+ * @see org.apache.clerezza.rdf.core.access.TcProvider#getNames(org.apache.clerezza.rdf.core.Graph)
+ */
+ @Override
+ public Set<UriRef> getNames(Graph graph) {
+ //TODO: this method would require to compare the triples within the graph
+ // because an equals check will not work with BNodes.
+ Set<UriRef> graphNames = new HashSet<UriRef>();
+ for(Entry<UriRef,ModelGraph> entry : initModels.entrySet()){
+ if(graphNames.contains(entry.getKey()) && //avoid lazy initialisation of grpahs for MGraphs
+ graph.equals(entry.getValue().getGraph())){
+ graphNames.add(entry.getKey());
+ }
+ }
+ return graphNames;
+ }
+ /*
+ * (non-Javadoc)
+ * @see org.apache.clerezza.rdf.core.access.WeightedTcProvider#getWeight()
+ */
+ @Override
+ public int getWeight() {
+ return weight;
+ }
+
+
+ /**
+ * Substitutes ${property.name} with the values retrieved via <ul>
+ * <li> {@link BundleContext#getProperty(String)} or
+ * <li> {@link System#getProperty(String, String)} if the parsed
+ * {@link BundleContext} is <code>null</code>
+ * </ul>
+ * Substitutes with an empty string if the property is not present. If
+ * the substitution does not end with {@link File#separatorChar}, than it is
+ * appended to allow easily creating paths relative to root directory available
+ * as property regardless if the property includes/excludes the final
+ * separator char.
+ * <p>
+ * Nested substitutions are NOT supported. However multiple substitutions are supported.
+ * <p>
+ * If someone knows a default implementation feel free to replace!
+ *
+ * @param value
+ * the value to substitute
+ * @param bundleContext
+ * If not <code>null</code> the {@link BundleContext#getProperty(String)} is used instead of
+ * the {@link System#getProperty(String)}. By that it is possible to use OSGI only properties
+ * for substitution.
+ * @return the substituted value
+ */
+ private static String substituteProperty(String value, BundleContext bundleContext) {
+ int prevAt = 0;
+ int foundAt = 0;
+ StringBuilder substitution = new StringBuilder();
+ while ((foundAt = value.indexOf("${", prevAt)) >= prevAt) {
+ substitution.append(value.substring(prevAt, foundAt));
+ String propertyName = value.substring(foundAt + 2, value.indexOf('}', foundAt));
+ String propertyValue = bundleContext == null ? // if no bundleContext is available
+ System.getProperty(propertyName) : // use the System properties
+ bundleContext.getProperty(propertyName);
+ if(propertyValue != null) {
+ substitution.append(propertyValue);
+ if(propertyValue.charAt(propertyValue.length()-1) != File.separatorChar){
+ substitution.append(File.separatorChar);
+ }
+ } //else nothing to append
+ prevAt = foundAt + propertyName.length() + 3; // +3 -> "${}".length
+ }
+ substitution.append(value.substring(prevAt, value.length()));
+ return substitution.toString();
+ }
+ private static final Charset UTF8 = Charset.forName("UTF-8");
+ private static final byte[] lineSep = "\n".getBytes(UTF8);
+ /**
+ * Writes the configuration file storing the initialised {@link Graph} names
+ * @throws IOException
+ */
+ private void writeGraphConfig() throws IOException {
+ writeConfig(graphConfigFile,graphNames);
+ }
+ /**
+ * Writes the configuration file storing the initialised {@link MGraph} names
+ * @throws IOException
+ */
+ private void writeMGraphConfig() throws IOException {
+ writeConfig(mGraphConfigFile,mGraphNames);
+ }
+ /**
+ * Writes the parsed names to the parsed file. One name in each line.
+ * TODO: This might not scale to millions of graphs. If this would be
+ * necessary one should store {@link MGraph} and {@link Graph} names in the
+ * {@link Dataset#getDefaultModel() default model} of the TDB
+ * {@link Dataset}.<br>
+ * However for now I have decided against that, because this way makes it
+ * more easy to manually play around with the condiguration (such as
+ * manually adding the name of an Graph/MGraph that already exists in
+ * an pre-existing Dataset.
+ * @throws IOException
+ * @throws FileNotFoundException
+ */
+ private static void writeConfig(File file,Set<UriRef> names) throws IOException, FileNotFoundException {
+ if(file.exists()){
+ if(!file.delete()){
+ throw new IOException("Unable to delete GraphConfigFile '"
+ + file.getAbsolutePath()+"'!");
+ }
+ }
+ OutputStream out = new FileOutputStream(file);
+ for (UriRef name : names) {
+ out.write(name.getUnicodeString().getBytes(UTF8));
+ out.write(lineSep);
+ }
+ out.close();
+ }
+ private void initGraphConfigs(File tdbDir,Dictionary<String,Object> config) throws IOException {
+
+ graphConfigFile = new File(tdbDir,"tcprovider.graphnames");
+ graphNames = new HashSet<UriRef>();
+ boolean configPresent = readGraphConfig(graphConfigFile, graphNames);
+ log.info("Present named Models");
+ for(Iterator<String> it = dataset.listNames();it.hasNext();){
+ log.info(" > {}",it.next());
+ }
+ if(configPresent) {
+ //validate that all Graphs and MGraphs in the configFile also are
+ //also present in the Jena TDB dataset
+
+ //NOTE: validation of graph model MUST BE deactivated, because
+ // Clerezza TcProvider needs to support the preservation of
+ // empty graphs!
+// synchronized (dataset) {
+// boolean modified = false;
+// Iterator<UriRef> it = graphNames.iterator();
+// while(it.hasNext()){
+// String name = it.next().getUnicodeString();
+// if(!(dataset.containsNamedModel(name) ||
+// dataset.containsNamedModel(name+GRAPH_NAME_SUFFIX))){
+// log.info("Remove GraphName {} form GrpahNameConfig because " +
+// "it is not part of the TDB dataset",name);
+// it.remove();
+// modified = true;
+// }
+// }
+// if(modified){
+// writeGraphConfig();
+// }
+// }
+ } else {
+ writeGraphConfig();
+ }
+ mGraphConfigFile = new File(tdbDir,"tcprovider.mgraphnames");
+ mGraphNames = new HashSet<UriRef>();
+ configPresent = readGraphConfig(mGraphConfigFile, mGraphNames);
+ if(configPresent) {
+ //validate that all Graphs and MGraphs in the configFile also are
+ //also present in the Jena TDB dataset
+
+ //NOTE: validation of graph model MUST BE deactivated, because
+ // Clerezza TcProvider needs to support the preservation of
+ // empty graphs!
+// synchronized (dataset) {
+// boolean modified = false;
+// Iterator<UriRef> it = mGraphNames.iterator();
+// while(it.hasNext()){
+// String name = it.next().getUnicodeString();
+// if(!(dataset.containsNamedModel(name) ||
+// dataset.containsNamedModel(name+MGRAPH_NAME_SUFFIX))){
+// log.info("Remove MGraphName {} form GrpahNameConfig because " +
+// "it is not part of the TDB dataset",name);
+// it.remove();
+// modified = true;
+// }
+// }
+// if(modified){
+// writeMGraphConfig();
+// }
+// }
+ } else { //read pre-existing models in the dataset
+ synchronized (dataset) {
+ for(Iterator<String> it = dataset.listNames();it.hasNext();){
+ mGraphNames.add(new UriRef(it.next()));
+ }
+ writeMGraphConfig();
+ }
+ }
+ }
+ /**
+ * Read a graph configuration file.
+ * @throws FileNotFoundException
+ * @throws IOException
+ * @see #writeGraphConfig()
+ */
+ private static boolean readGraphConfig(File file,Set<UriRef> names) throws FileNotFoundException, IOException {
+ if(file.exists()){ //read existing
+ if(!file.isFile()){
+ throw new IllegalStateException("Graph name configuration file '"
+ + file.getAbsolutePath()+"' exsits but is not of type File!");
+ }
+ BufferedReader reader = new BufferedReader(new InputStreamReader(
+ new FileInputStream(file),UTF8));
+ String line = reader.readLine();
+ while (line != null) {
+ names.add(new UriRef(line));
+ line = reader.readLine();
+ }
+ reader.close();
+ return true;
+ } else {// no config file indicates first initialisation
+ return false;
+ }
+ }
+
+}
Propchange: incubator/clerezza/trunk/parent/rdf.jena.tdb.storage/src/main/java/org/apache/clerezza/rdf/jena/tdb/storage/SingleTdbDatasetTcProvider.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: incubator/clerezza/trunk/parent/rdf.jena.tdb.storage/src/main/resources/OSGI-INF/metatype/metatype.properties
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/parent/rdf.jena.tdb.storage/src/main/resources/OSGI-INF/metatype/metatype.properties?rev=1351644&view=auto
==============================================================================
--- incubator/clerezza/trunk/parent/rdf.jena.tdb.storage/src/main/resources/OSGI-INF/metatype/metatype.properties (added)
+++ incubator/clerezza/trunk/parent/rdf.jena.tdb.storage/src/main/resources/OSGI-INF/metatype/metatype.properties Tue Jun 19 10:31:09 2012
@@ -0,0 +1,47 @@
+# 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.
+
+# Deactivate properties for component because those are not common for Apache Clerezza
+#org.apache.clerezza.rdf.jena.tdb.storage.SingleTdbDatasetTcProvider.name=Apache Stanbol Clerezza \
+#Single Jena TDB Provider
+#org.apache.clerezza.rdf.jena.tdb.storage.SingleTdbDatasetTcProvider.description=Tc Provider \
+#implementation that stores Clerezza TripleCollections as named graphs in a single Jena \
+#TDB store.
+
+#==============================================================
+#Properties and Options used to configure TDB dataset providers
+#==============================================================
+
+tdb-dir.name=Jena TDB directory
+tdb-dir.description= The directory for Jena the TDB database. This field \
+supports property substitution: '${property}' will be replaced with the value \
+of the 'property' available via the BundleContext or the system properties \
+(e.g. "${myHome}/myRdfStore")
+default-graph-name.name=Default Graph Name
+default-graph-name.description=The URI name of the default graph of the Jena \
+TDB database. Requests to the TcProvider with this URI will return an MGraph \
+that is the union over all the graphs managed by this TcProvider. If empty or \
+not defined the default graph is not exposed.
+weight.name=Weight
+weight.description=The weight of this TcProvider. Set to an low value (e.g. \
+value < 0) if you want to inject this instance by using a Filter. Set to a high \
+value (e.g. values > 1000) if you want this instance to be the default used by \
+Clerezza.
+sync-interval.name=Sync Interval
+sync-interval.description=The interval in seconds until changes are saved to \
+the file system. Sync will always occur if this provider is deactivated. However \
+on crashes all data since the last sync will be lost. The default is 6sec. \
+Higher values will reduce the overhead for periodically writing to the file \
+system.
\ No newline at end of file
Propchange: incubator/clerezza/trunk/parent/rdf.jena.tdb.storage/src/main/resources/OSGI-INF/metatype/metatype.properties
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: incubator/clerezza/trunk/parent/rdf.jena.tdb.storage/src/test/java/org/apache/clerezza/rdf/jena/tdb/storage/MultiThreadedSingleTdbDatasetTest.java
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/parent/rdf.jena.tdb.storage/src/test/java/org/apache/clerezza/rdf/jena/tdb/storage/MultiThreadedSingleTdbDatasetTest.java?rev=1351644&view=auto
==============================================================================
--- incubator/clerezza/trunk/parent/rdf.jena.tdb.storage/src/test/java/org/apache/clerezza/rdf/jena/tdb/storage/MultiThreadedSingleTdbDatasetTest.java (added)
+++ incubator/clerezza/trunk/parent/rdf.jena.tdb.storage/src/test/java/org/apache/clerezza/rdf/jena/tdb/storage/MultiThreadedSingleTdbDatasetTest.java Tue Jun 19 10:31:09 2012
@@ -0,0 +1,148 @@
+package org.apache.clerezza.rdf.jena.tdb.storage;
+/*
+ *
+ * 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.
+ *
+*/
+
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Collections;
+import java.util.Dictionary;
+import java.util.HashSet;
+import java.util.Hashtable;
+import java.util.Set;
+
+import junit.framework.Assert;
+
+import org.apache.clerezza.rdf.core.BNode;
+import org.apache.clerezza.rdf.core.Literal;
+import org.apache.clerezza.rdf.core.MGraph;
+import org.apache.clerezza.rdf.core.Resource;
+import org.apache.clerezza.rdf.core.Triple;
+import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.clerezza.rdf.core.impl.PlainLiteralImpl;
+import org.apache.clerezza.rdf.core.impl.TripleImpl;
+import org.apache.felix.scr.annotations.Activate;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.osgi.service.cm.ConfigurationException;
+import org.wymiwyg.commons.util.Util;
+
+
+public class MultiThreadedSingleTdbDatasetTest {
+
+
+
+ /**
+ * how many threads to start
+ */
+ private static final int THREAD_COUNT = 100;
+ /**
+ * how many seconds to let them run
+ */
+ private static final int DELAY = 30;
+
+
+ private MGraph mGraph;
+ private Set<Triple> testTriples = Collections.synchronizedSet(new HashSet<Triple>());
+
+ class TestThread extends Thread {
+
+ private final int id;
+ private boolean stopRequested;
+ private int addedTripleCount = 0;
+
+ public TestThread(final int id) {
+ this.id = id;
+ start();
+ }
+
+ public void requestStop() {
+ stopRequested = true;
+ }
+
+ @Override
+ public void run() {
+ while (!stopRequested) {
+ Literal randomLiteral = new PlainLiteralImpl(Util.createRandomString(22));
+ Triple triple = new TripleImpl(new BNode(), new UriRef("http://example.com/property"), randomLiteral);
+ mGraph.add(triple);
+ addedTripleCount++;
+ if ((addedTripleCount % 100) == 0) {
+ testTriples.add(triple);
+ }
+ }
+ }
+
+ public int getAddedTripleCount() {
+ return addedTripleCount;
+ }
+
+ }
+
+
+
+ private static File tempFile;
+ private static Dictionary<String,Object> config;
+ private static SingleTdbDatasetTcProvider provider;
+ @BeforeClass
+ public static void setup() throws IOException, ConfigurationException {
+ tempFile = File.createTempFile("tdbdatasettest", null);
+ tempFile.delete();
+ tempFile.mkdirs();
+ config = new Hashtable<String,Object>();
+ config.put(SingleTdbDatasetTcProvider.TDB_DIR, tempFile.getAbsolutePath());
+ provider = new SingleTdbDatasetTcProvider(config);
+ }
+ @Before
+ public void createGraph(){
+ this.mGraph = provider.createMGraph(new UriRef("http://www.example.org/multiThreadTest"));
+ }
+ @Test
+ public void perform() throws InterruptedException {
+ TestThread[] threads = new TestThread[THREAD_COUNT];
+ for (int i = 0; i < threads.length; i++) {
+ threads[i] = new TestThread(i);
+ }
+ Thread.sleep(DELAY*1000);
+ for (TestThread testThread : threads) {
+ testThread.requestStop();
+ }
+ for (TestThread testThread : threads) {
+ testThread.join();
+ }
+ int addedTriples = 0;
+ for (TestThread testThread : threads) {
+ addedTriples += testThread.getAddedTripleCount();
+ }
+ Assert.assertEquals(addedTriples, mGraph.size());
+ for (Triple testTriple : testTriples) {
+ Assert.assertTrue(mGraph.contains(testTriple));
+ }
+ }
+ @AfterClass
+ public static void cleanUpDirectory() {
+ provider.deactivate(null);
+ TdbTcProvider.delete(tempFile);
+ }
+
+}
Propchange: incubator/clerezza/trunk/parent/rdf.jena.tdb.storage/src/test/java/org/apache/clerezza/rdf/jena/tdb/storage/MultiThreadedSingleTdbDatasetTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: incubator/clerezza/trunk/parent/rdf.jena.tdb.storage/src/test/java/org/apache/clerezza/rdf/jena/tdb/storage/SingleTdbDatasetMGraphTest.java
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/parent/rdf.jena.tdb.storage/src/test/java/org/apache/clerezza/rdf/jena/tdb/storage/SingleTdbDatasetMGraphTest.java?rev=1351644&view=auto
==============================================================================
--- incubator/clerezza/trunk/parent/rdf.jena.tdb.storage/src/test/java/org/apache/clerezza/rdf/jena/tdb/storage/SingleTdbDatasetMGraphTest.java (added)
+++ incubator/clerezza/trunk/parent/rdf.jena.tdb.storage/src/test/java/org/apache/clerezza/rdf/jena/tdb/storage/SingleTdbDatasetMGraphTest.java Tue Jun 19 10:31:09 2012
@@ -0,0 +1,50 @@
+package org.apache.clerezza.rdf.jena.tdb.storage;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.Hashtable;
+
+import org.apache.clerezza.rdf.core.MGraph;
+import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.clerezza.rdf.core.test.MGraphTest;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.osgi.service.cm.ConfigurationException;
+
+public class SingleTdbDatasetMGraphTest extends MGraphTest {
+
+ private static final String MGRAPHNAME_PREFIX = "http://text.example.org/testGraph";
+ private static int testGraphCounter = 0;
+
+ private static File tempFile;
+ private static Dictionary<String,Object> config;
+ private static SingleTdbDatasetTcProvider provider;
+ @BeforeClass
+ public static void setup() throws IOException, ConfigurationException {
+ tempFile = File.createTempFile("tdbdatasettest", null);
+ tempFile.delete();
+ tempFile.mkdirs();
+ config = new Hashtable<String,Object>();
+ config.put(SingleTdbDatasetTcProvider.TDB_DIR, tempFile.getAbsolutePath());
+ config.put(SingleTdbDatasetTcProvider.DEFAULT_GRAPH_NAME, "http://www.example.org/defaultGraph");
+ provider = new SingleTdbDatasetTcProvider(config);
+ }
+ @AfterClass
+ public static void cleanUpDirectory() {
+ for(int i = 0; i < testGraphCounter;i++){
+ provider.deleteTripleCollection(new UriRef(MGRAPHNAME_PREFIX+i));
+ }
+ provider.deactivate(null);
+ TdbTcProvider.delete(tempFile);
+ }
+ @Override
+ protected MGraph getEmptyMGraph() {
+ MGraph graph = provider.createMGraph(new UriRef(MGRAPHNAME_PREFIX+testGraphCounter));
+ testGraphCounter++;
+ return graph;
+ }
+
+}
Propchange: incubator/clerezza/trunk/parent/rdf.jena.tdb.storage/src/test/java/org/apache/clerezza/rdf/jena/tdb/storage/SingleTdbDatasetMGraphTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: incubator/clerezza/trunk/parent/rdf.jena.tdb.storage/src/test/java/org/apache/clerezza/rdf/jena/tdb/storage/SingleTdbDatasetTcProviderTest.java
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/parent/rdf.jena.tdb.storage/src/test/java/org/apache/clerezza/rdf/jena/tdb/storage/SingleTdbDatasetTcProviderTest.java?rev=1351644&view=auto
==============================================================================
--- incubator/clerezza/trunk/parent/rdf.jena.tdb.storage/src/test/java/org/apache/clerezza/rdf/jena/tdb/storage/SingleTdbDatasetTcProviderTest.java (added)
+++ incubator/clerezza/trunk/parent/rdf.jena.tdb.storage/src/test/java/org/apache/clerezza/rdf/jena/tdb/storage/SingleTdbDatasetTcProviderTest.java Tue Jun 19 10:31:09 2012
@@ -0,0 +1,147 @@
+package org.apache.clerezza.rdf.jena.tdb.storage;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Dictionary;
+import java.util.HashSet;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.Set;
+
+import org.apache.clerezza.rdf.core.Graph;
+import org.apache.clerezza.rdf.core.MGraph;
+import org.apache.clerezza.rdf.core.NonLiteral;
+import org.apache.clerezza.rdf.core.Triple;
+import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.clerezza.rdf.core.access.NoSuchEntityException;
+import org.apache.clerezza.rdf.core.access.TcProvider;
+import org.apache.clerezza.rdf.core.impl.PlainLiteralImpl;
+import org.apache.clerezza.rdf.core.impl.SimpleMGraph;
+import org.apache.clerezza.rdf.core.impl.TripleImpl;
+import org.apache.clerezza.rdf.core.test.TcProviderTest;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.osgi.framework.Constants;
+import org.osgi.service.cm.ConfigurationException;
+
+public class SingleTdbDatasetTcProviderTest extends TcProviderTest {
+
+ private static File tempFile;
+ private static Dictionary<String,Object> config;
+ private static SingleTdbDatasetTcProvider provider;
+ private static UriRef UNION_GRAPH_NAME = new UriRef("http://www.example.org/unionGraph");
+ @BeforeClass
+ public static void setup() throws IOException, ConfigurationException {
+ tempFile = File.createTempFile("tdbdatasettest", null);
+ tempFile.delete();
+ tempFile.mkdirs();
+ config = new Hashtable<String,Object>();
+ config.put(SingleTdbDatasetTcProvider.TDB_DIR, tempFile.getAbsolutePath());
+ config.put(SingleTdbDatasetTcProvider.DEFAULT_GRAPH_NAME, UNION_GRAPH_NAME.getUnicodeString());
+ //config.put(SingleTdbDatasetTcProvider.USE_GRAPH_NAME_SUFFIXES, true);
+ }
+ @AfterClass
+ public static void cleanUpDirectory() {
+ //nothin todo because cleanData does the job already
+ }
+ @Override
+ protected TcProvider getInstance() {
+ //tests want us to deactivate/activate the TcProvider on multiple calls
+ //to getInstance within the same test
+ if(provider !=null){
+ provider.deactivate(null);
+ provider = null;
+ }
+ try {
+ provider = new SingleTdbDatasetTcProvider(config);
+ } catch (Exception e) {
+ throw new RuntimeException("forwarding ...",e);
+ }
+ return provider;
+ }
+ @After
+ public void cleanData(){
+ //We need to remove all remaining data after a test
+ if(provider != null){
+ provider.deactivate(null);
+ TdbTcProvider.delete(tempFile);
+ provider = null;
+ }
+ }
+ /**
+ * The union grpah is read only!
+ */
+ @Test(expected=NoSuchEntityException.class)
+ public void testUnionMgraph(){
+ TcProvider provider = getInstance();
+ provider.getMGraph(UNION_GRAPH_NAME);
+ }
+ /**
+ * Assert union graph on an empty dataset
+ */
+ @Test
+ public void testEmptyUnionGraph(){
+ TcProvider provider = getInstance();
+ Graph grpah = provider.getGraph(UNION_GRAPH_NAME);
+ Assert.assertNotNull(grpah);
+ }
+
+ @Test
+ public void testUnionGraph() throws Exception{
+ TcProvider provider = getInstance();
+ UriRef type = new UriRef("http://www.w3.org/2000/01/rdf-schema#type");
+ UriRef name = new UriRef("http://schema.org/name");
+ UriRef personType = new UriRef("http://schema.org/Person");
+ UriRef companyType = new UriRef("http://schema.org/Company");
+ UriRef worksFor = new UriRef("http://schema.org/works-for");
+
+ //create a graph with persons
+ MGraph persons = new SimpleMGraph();
+ UriRef tim = new UriRef("http://people.org/tim.johnson");
+ persons.add(new TripleImpl(tim, type, personType));
+ persons.add(new TripleImpl(tim, name, new PlainLiteralImpl("Tim Johnson")));
+ UriRef john = new UriRef("http://people.org/john.swenson");
+ persons.add(new TripleImpl(john, type, personType));
+ persons.add(new TripleImpl(john, name, new PlainLiteralImpl("John Swenson")));
+ provider.createGraph(new UriRef("urn:persons"), persons.getGraph());
+
+ //create a MGraph with data about persons
+ MGraph orgdata = provider.createMGraph(new UriRef("urn:orgdata"));
+ UriRef talinor = new UriRef("http://company.org/talinor");
+ orgdata.add(new TripleImpl(talinor, type, companyType));
+ orgdata.add(new TripleImpl(talinor, name, new PlainLiteralImpl("Talinor Inc.")));
+ orgdata.add(new TripleImpl(john, worksFor, talinor));
+ UriRef kondalor = new UriRef("http://company.org/kondalor");
+ orgdata.add(new TripleImpl(kondalor, type, companyType));
+ orgdata.add(new TripleImpl(kondalor, name, new PlainLiteralImpl("Kondalor Ges.m.b.H.")));
+ orgdata.add(new TripleImpl(tim, worksFor, kondalor));
+
+ //now get the union graph
+ Graph data = provider.getGraph(UNION_GRAPH_NAME);
+ //NOTE: Jena TDB does not support getSize for the union graph
+// int expectedTripleCount = persons.size()+orgdata.size();
+// Assert.assertEquals("Uniongraph has "+data.size()
+// +" triples (expected "+expectedTripleCount+")",
+// expectedTripleCount, data.size());
+ Iterator<Triple> it = data.filter(null, type, companyType);
+ Set<UriRef> expected = new HashSet<UriRef>(Arrays.asList(talinor,kondalor));
+ while(it.hasNext()){
+ NonLiteral subject = it.next().getSubject();
+ Assert.assertTrue("Unexpected "+subject, expected.remove(subject));
+ }
+ Assert.assertTrue("Missing "+expected, expected.isEmpty());
+
+ it = data.filter(null, type, personType);
+ expected = new HashSet<UriRef>(Arrays.asList(john,tim));
+ while(it.hasNext()){
+ NonLiteral subject = it.next().getSubject();
+ Assert.assertTrue("Unexpected "+subject, expected.remove(subject));
+ }
+ Assert.assertTrue("Missing "+expected, expected.isEmpty());
+
+ }
+}
Propchange: incubator/clerezza/trunk/parent/rdf.jena.tdb.storage/src/test/java/org/apache/clerezza/rdf/jena/tdb/storage/SingleTdbDatasetTcProviderTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain