01: /*
02: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2007.
03: *
04: * Licensed under the Aduna BSD-style license.
05: */
06: package org.openrdf.sail.nativerdf;
07:
08: import java.io.IOException;
09:
10: import info.aduna.iteration.CloseableIteration;
11: import info.aduna.iteration.ExceptionConvertingIteration;
12:
13: import org.openrdf.model.Resource;
14: import org.openrdf.model.Statement;
15: import org.openrdf.model.URI;
16: import org.openrdf.model.Value;
17: import org.openrdf.model.ValueFactory;
18: import org.openrdf.query.QueryEvaluationException;
19: import org.openrdf.query.algebra.evaluation.TripleSource;
20:
21: public class NativeTripleSource implements TripleSource {
22:
23: /*-----------*
24: * Constants *
25: *-----------*/
26:
27: protected final NativeStore nativeStore;
28:
29: protected final boolean includeInferred;
30:
31: protected final boolean readTransaction;
32:
33: /*--------------*
34: * Constructors *
35: *--------------*/
36:
37: protected NativeTripleSource(NativeStore store,
38: boolean includeInferred, boolean readTransaction) {
39: this .nativeStore = store;
40: this .includeInferred = includeInferred;
41: this .readTransaction = readTransaction;
42: }
43:
44: /*---------*
45: * Methods *
46: *---------*/
47:
48: public CloseableIteration<? extends Statement, QueryEvaluationException> getStatements(
49: Resource subj, URI pred, Value obj, Resource... contexts)
50: throws QueryEvaluationException {
51: try {
52: return new ExceptionConvertingIteration<Statement, QueryEvaluationException>(
53: nativeStore.createStatementIterator(subj, pred,
54: obj, includeInferred, readTransaction,
55: contexts)) {
56:
57: @Override
58: protected QueryEvaluationException convert(Exception e) {
59: if (e instanceof IOException) {
60: return new QueryEvaluationException(e);
61: } else if (e instanceof RuntimeException) {
62: throw (RuntimeException) e;
63: } else if (e == null) {
64: throw new IllegalArgumentException(
65: "e must not be null");
66: } else {
67: throw new IllegalArgumentException(
68: "Unexpected exception type: "
69: + e.getClass());
70: }
71: }
72: };
73: } catch (IOException e) {
74: throw new QueryEvaluationException(
75: "Unable to get statements", e);
76: }
77: }
78:
79: public ValueFactory getValueFactory() {
80: return nativeStore.getValueFactory();
81: }
82: }
|