001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.repository.sail;
007:
008: import info.aduna.iteration.CloseableIteration;
009:
010: import org.openrdf.OpenRDFUtil;
011: import org.openrdf.model.Namespace;
012: import org.openrdf.model.Resource;
013: import org.openrdf.model.Statement;
014: import org.openrdf.model.URI;
015: import org.openrdf.model.Value;
016: import org.openrdf.query.MalformedQueryException;
017: import org.openrdf.query.QueryLanguage;
018: import org.openrdf.query.parser.ParsedBooleanQuery;
019: import org.openrdf.query.parser.ParsedGraphQuery;
020: import org.openrdf.query.parser.ParsedQuery;
021: import org.openrdf.query.parser.ParsedTupleQuery;
022: import org.openrdf.query.parser.QueryParserUtil;
023: import org.openrdf.repository.RepositoryConnection;
024: import org.openrdf.repository.RepositoryException;
025: import org.openrdf.repository.RepositoryResult;
026: import org.openrdf.repository.base.RepositoryConnectionBase;
027: import org.openrdf.rio.RDFHandler;
028: import org.openrdf.rio.RDFHandlerException;
029: import org.openrdf.sail.SailConnection;
030: import org.openrdf.sail.SailException;
031:
032: /**
033: * An implementation of the {@link RepositoryConnection} interface that wraps a
034: * {@link SailConnection}.
035: *
036: * @author jeen
037: * @author Arjohn Kampman
038: */
039: public class SailRepositoryConnection extends RepositoryConnectionBase {
040:
041: /*-----------*
042: * Variables *
043: *-----------*/
044:
045: /**
046: * The Sail connection wrapped by this repository connection object.
047: */
048: private SailConnection sailConnection;
049:
050: /*--------------*
051: * Constructors *
052: *--------------*/
053:
054: /**
055: * Creates a new repository connection that will wrap the supplied
056: * SailConnection. ConnectionImpl objects are created by
057: * {@link SailRepository#getConnection}.
058: */
059: SailRepositoryConnection(SailRepository repository,
060: SailConnection sailConnection) {
061: super (repository);
062: this .sailConnection = sailConnection;
063: }
064:
065: /*---------*
066: * Methods *
067: *---------*/
068:
069: /**
070: * Returns the underlying SailConnection.
071: */
072: public SailConnection getSailConnection() {
073: return sailConnection;
074: }
075:
076: public void commit() throws RepositoryException {
077: try {
078: sailConnection.commit();
079: } catch (SailException e) {
080: throw new RepositoryException(e);
081: }
082: }
083:
084: public void rollback() throws RepositoryException {
085: try {
086: sailConnection.rollback();
087: } catch (SailException e) {
088: throw new RepositoryException(e);
089: }
090: }
091:
092: @Override
093: public void close() throws RepositoryException {
094: try {
095: sailConnection.close();
096: super .close();
097: } catch (SailException e) {
098: throw new RepositoryException(e);
099: }
100: }
101:
102: public SailQuery prepareQuery(QueryLanguage ql, String queryString,
103: String baseURI) throws MalformedQueryException {
104: ParsedQuery parsedQuery = QueryParserUtil.parseQuery(ql,
105: queryString, baseURI);
106:
107: if (parsedQuery instanceof ParsedTupleQuery) {
108: return new SailTupleQuery((ParsedTupleQuery) parsedQuery,
109: this );
110: } else if (parsedQuery instanceof ParsedGraphQuery) {
111: return new SailGraphQuery((ParsedGraphQuery) parsedQuery,
112: this );
113: } else if (parsedQuery instanceof ParsedBooleanQuery) {
114: return new SailBooleanQuery(
115: (ParsedBooleanQuery) parsedQuery, this );
116: } else {
117: throw new RuntimeException("Unexpected query type: "
118: + parsedQuery.getClass());
119: }
120: }
121:
122: public SailTupleQuery prepareTupleQuery(QueryLanguage ql,
123: String queryString, String baseURI)
124: throws MalformedQueryException {
125: ParsedTupleQuery parsedQuery = QueryParserUtil.parseTupleQuery(
126: ql, queryString, baseURI);
127: return new SailTupleQuery(parsedQuery, this );
128: }
129:
130: public SailGraphQuery prepareGraphQuery(QueryLanguage ql,
131: String queryString, String baseURI)
132: throws MalformedQueryException {
133: ParsedGraphQuery parsedQuery = QueryParserUtil.parseGraphQuery(
134: ql, queryString, baseURI);
135: return new SailGraphQuery(parsedQuery, this );
136: }
137:
138: public SailBooleanQuery prepareBooleanQuery(QueryLanguage ql,
139: String queryString, String baseURI)
140: throws MalformedQueryException {
141: ParsedBooleanQuery parsedQuery = QueryParserUtil
142: .parseBooleanQuery(ql, queryString, baseURI);
143: return new SailBooleanQuery(parsedQuery, this );
144: }
145:
146: public RepositoryResult<Resource> getContextIDs()
147: throws RepositoryException {
148: try {
149: return createRepositoryResult(sailConnection
150: .getContextIDs());
151: } catch (SailException e) {
152: throw new RepositoryException(
153: "Unable to get context IDs from Sail", e);
154: }
155: }
156:
157: public RepositoryResult<Statement> getStatements(Resource subj,
158: URI pred, Value obj, boolean includeInferred,
159: Resource... contexts) throws RepositoryException {
160: if (logger.isDebugEnabled()) {
161: logger.debug("getStatements({}, {}, {}, {}, {})",
162: new Object[] { subj, pred, obj, includeInferred,
163: contexts });
164: }
165:
166: OpenRDFUtil.verifyContextNotNull(contexts);
167:
168: try {
169: return createRepositoryResult(sailConnection.getStatements(
170: subj, pred, obj, includeInferred, contexts));
171: } catch (SailException e) {
172: throw new RepositoryException(
173: "Unable to get statements from Sail", e);
174: }
175: }
176:
177: public void exportStatements(Resource subj, URI pred, Value obj,
178: boolean includeInferred, RDFHandler handler,
179: Resource... contexts) throws RepositoryException,
180: RDFHandlerException {
181: handler.startRDF();
182:
183: // Export namespace information
184: CloseableIteration<? extends Namespace, RepositoryException> nsIter = getNamespaces();
185: try {
186: while (nsIter.hasNext()) {
187: Namespace ns = nsIter.next();
188: handler.handleNamespace(ns.getPrefix(), ns.getName());
189: }
190: } finally {
191: nsIter.close();
192: }
193:
194: // Export statements
195: CloseableIteration<? extends Statement, RepositoryException> stIter = getStatements(
196: subj, pred, obj, includeInferred, contexts);
197:
198: try {
199: while (stIter.hasNext()) {
200: handler.handleStatement(stIter.next());
201: }
202: } finally {
203: stIter.close();
204: }
205:
206: handler.endRDF();
207: }
208:
209: public long size(Resource... contexts) throws RepositoryException {
210: try {
211: return sailConnection.size(contexts);
212: } catch (SailException e) {
213: throw new RepositoryException(e);
214: }
215: }
216:
217: @Override
218: protected void addWithoutCommit(Resource subject, URI predicate,
219: Value object, Resource... contexts)
220: throws RepositoryException {
221: try {
222: sailConnection.addStatement(subject, predicate, object,
223: contexts);
224: } catch (SailException e) {
225: throw new RepositoryException(e);
226: }
227: }
228:
229: @Override
230: protected void removeWithoutCommit(Resource subject, URI predicate,
231: Value object, Resource... contexts)
232: throws RepositoryException {
233: try {
234: sailConnection.removeStatements(subject, predicate, object,
235: contexts);
236: } catch (SailException e) {
237: throw new RepositoryException(e);
238: }
239: }
240:
241: @Override
242: public void clear(Resource... contexts) throws RepositoryException {
243: OpenRDFUtil.verifyContextNotNull(contexts);
244:
245: try {
246: sailConnection.clear(contexts);
247: autoCommit();
248: } catch (SailException e) {
249: throw new RepositoryException(e);
250: }
251: }
252:
253: public void setNamespace(String prefix, String name)
254: throws RepositoryException {
255: try {
256: sailConnection.setNamespace(prefix, name);
257: autoCommit();
258: } catch (SailException e) {
259: throw new RepositoryException(e);
260: }
261: }
262:
263: public void removeNamespace(String prefix)
264: throws RepositoryException {
265: try {
266: sailConnection.removeNamespace(prefix);
267: autoCommit();
268: } catch (SailException e) {
269: throw new RepositoryException(e);
270: }
271: }
272:
273: public void clearNamespaces() throws RepositoryException {
274: try {
275: sailConnection.clearNamespaces();
276: autoCommit();
277: } catch (SailException e) {
278: throw new RepositoryException(e);
279: }
280: }
281:
282: public RepositoryResult<Namespace> getNamespaces()
283: throws RepositoryException {
284: try {
285: return createRepositoryResult(sailConnection
286: .getNamespaces());
287: } catch (SailException e) {
288: throw new RepositoryException(
289: "Unable to get namespaces from Sail", e);
290: }
291: }
292:
293: public String getNamespace(String prefix)
294: throws RepositoryException {
295: try {
296: return sailConnection.getNamespace(prefix);
297: } catch (SailException e) {
298: throw new RepositoryException(e);
299: }
300: }
301:
302: /**
303: * Wraps a CloseableIteration coming from a Sail in a RepositoryResult
304: * object, applying the required conversions
305: */
306: protected <E> RepositoryResult<E> createRepositoryResult(
307: CloseableIteration<? extends E, SailException> sailIter) {
308: return new RepositoryResult<E>(new SailCloseableIteration<E>(
309: sailIter));
310: }
311: }
|