001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 2008.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.sail.rdbms;
007:
008: import java.sql.SQLException;
009: import java.util.Collection;
010:
011: import info.aduna.iteration.CloseableIteration;
012:
013: import org.openrdf.model.Namespace;
014: import org.openrdf.model.Resource;
015: import org.openrdf.model.Statement;
016: import org.openrdf.model.URI;
017: import org.openrdf.model.Value;
018: import org.openrdf.query.BindingSet;
019: import org.openrdf.query.Dataset;
020: import org.openrdf.query.QueryEvaluationException;
021: import org.openrdf.query.algebra.TupleExpr;
022: import org.openrdf.query.algebra.evaluation.EvaluationStrategy;
023: import org.openrdf.sail.SailConnection;
024: import org.openrdf.sail.SailException;
025: import org.openrdf.sail.helpers.DefaultSailChangedEvent;
026: import org.openrdf.sail.helpers.SailConnectionBase;
027: import org.openrdf.sail.rdbms.evaluation.RdbmsEvaluationFactory;
028: import org.openrdf.sail.rdbms.exceptions.RdbmsException;
029: import org.openrdf.sail.rdbms.iteration.NamespaceIteration;
030: import org.openrdf.sail.rdbms.iteration.RdbmsResourceIteration;
031: import org.openrdf.sail.rdbms.managers.NamespaceManager;
032: import org.openrdf.sail.rdbms.model.RdbmsResource;
033: import org.openrdf.sail.rdbms.model.RdbmsURI;
034: import org.openrdf.sail.rdbms.model.RdbmsValue;
035: import org.openrdf.sail.rdbms.optimizers.RdbmsQueryOptimizer;
036:
037: /**
038: * Coordinates the triple store, namespace manager, optimizer, and evaluation
039: * strategy into the {@link SailConnection} interface.
040: *
041: * @author James Leigh
042: *
043: */
044: public class RdbmsConnection extends SailConnectionBase {
045: private RdbmsStore sail;
046: private RdbmsValueFactory vf;
047: private RdbmsTripleRepository triples;
048: private NamespaceManager namespaces;
049: private RdbmsQueryOptimizer optimizer;
050: private RdbmsEvaluationFactory factory;
051:
052: public RdbmsConnection(RdbmsStore sail,
053: RdbmsTripleRepository triples) {
054: super (sail);
055: this .sail = sail;
056: this .vf = sail.getValueFactory();
057: this .triples = triples;
058: }
059:
060: public void setNamespaces(NamespaceManager namespaces) {
061: this .namespaces = namespaces;
062: }
063:
064: public void setRdbmsQueryOptimizer(RdbmsQueryOptimizer optimizer) {
065: this .optimizer = optimizer;
066: }
067:
068: public void setRdbmsEvaluationFactory(RdbmsEvaluationFactory factory) {
069: this .factory = factory;
070: }
071:
072: @Override
073: protected void addStatementInternal(Resource subj, URI pred,
074: Value obj, Resource... contexts) throws SailException {
075: try {
076: if (contexts.length == 0) {
077: triples.add(vf.createStatement(subj, pred, obj));
078: } else {
079: for (Resource ctx : contexts) {
080: triples.add(vf
081: .createStatement(subj, pred, obj, ctx));
082: }
083: }
084: } catch (SQLException e) {
085: throw new RdbmsException(e);
086: } catch (InterruptedException e) {
087: throw new RdbmsException(e);
088: }
089: }
090:
091: @Override
092: protected void clearInternal(Resource... contexts)
093: throws SailException {
094: removeStatementsInternal(null, null, null, contexts);
095: }
096:
097: @Override
098: protected void closeInternal() throws SailException {
099: try {
100: triples.close();
101: } catch (SQLException e) {
102: throw new RdbmsException(e);
103: }
104: }
105:
106: @Override
107: protected void commitInternal() throws SailException {
108: try {
109: triples.commit();
110: } catch (SQLException e) {
111: throw new RdbmsException(e);
112: } catch (InterruptedException e) {
113: throw new RdbmsException(e);
114: }
115:
116: sail.notifySailChanged(triples.getSailChangedEvent());
117: // create a fresh event object.
118: triples.setSailChangedEvent(new DefaultSailChangedEvent(sail));
119: }
120:
121: @Override
122: protected RdbmsResourceIteration getContextIDsInternal()
123: throws SailException {
124: try {
125: return triples.findContexts();
126: } catch (SQLException e) {
127: throw new RdbmsException(e);
128: }
129: }
130:
131: @Override
132: protected CloseableIteration<? extends Statement, SailException> getStatementsInternal(
133: Resource subj, URI pred, Value obj,
134: boolean includeInferred, Resource... contexts)
135: throws SailException {
136: RdbmsResource s = vf.asRdbmsResource(subj);
137: RdbmsURI p = vf.asRdbmsURI(pred);
138: RdbmsValue o = vf.asRdbmsValue(obj);
139: RdbmsResource[] c = vf.asRdbmsResource(contexts);
140: return triples.find(s, p, o, c);
141: }
142:
143: @Override
144: protected void removeStatementsInternal(Resource subj, URI pred,
145: Value obj, Resource... contexts) throws SailException {
146: RdbmsResource s = vf.asRdbmsResource(subj);
147: RdbmsURI p = vf.asRdbmsURI(pred);
148: RdbmsValue o = vf.asRdbmsValue(obj);
149: RdbmsResource[] c = vf.asRdbmsResource(contexts);
150: triples.remove(s, p, o, c);
151: }
152:
153: @Override
154: protected void rollbackInternal() throws SailException {
155: try {
156: triples.rollback();
157: } catch (SQLException e) {
158: throw new RdbmsException(e);
159: }
160: }
161:
162: @Override
163: protected CloseableIteration<BindingSet, QueryEvaluationException> evaluateInternal(
164: TupleExpr expr, Dataset dataset, BindingSet bindings,
165: boolean includeInferred) throws SailException {
166: triples.flush();
167: try {
168: TupleExpr tupleExpr;
169: EvaluationStrategy strategy;
170: strategy = factory.createRdbmsEvaluation(dataset);
171: tupleExpr = optimizer.optimize(expr, dataset, bindings,
172: strategy);
173: return strategy.evaluate(tupleExpr, bindings);
174: } catch (QueryEvaluationException e) {
175: throw new SailException(e);
176: }
177: }
178:
179: @Override
180: protected void clearNamespacesInternal() throws SailException {
181: namespaces.clearPrefixes();
182: }
183:
184: @Override
185: protected String getNamespaceInternal(String prefix)
186: throws SailException {
187: Namespace ns = namespaces.findByPrefix(prefix);
188: if (ns == null)
189: return null;
190: return ns.getName();
191: }
192:
193: @Override
194: protected CloseableIteration<? extends Namespace, SailException> getNamespacesInternal()
195: throws SailException {
196: Collection<? extends Namespace> ns = namespaces
197: .getNamespacesWithPrefix();
198: return new NamespaceIteration(ns.iterator());
199: }
200:
201: @Override
202: protected void removeNamespaceInternal(String prefix)
203: throws SailException {
204: namespaces.removePrefix(prefix);
205: }
206:
207: @Override
208: protected void setNamespaceInternal(String prefix, String name)
209: throws SailException {
210: namespaces.setPrefix(prefix, name);
211: }
212:
213: @Override
214: protected long sizeInternal(Resource... contexts)
215: throws SailException {
216: try {
217: return triples.size(vf.asRdbmsResource(contexts));
218: } catch (SQLException e) {
219: throw new RdbmsException(e);
220: }
221: }
222:
223: @Override
224: protected void startTransactionInternal() throws SailException {
225: try {
226: triples.begin();
227: } catch (SQLException e) {
228: throw new RdbmsException(e);
229: }
230: }
231:
232: }
|