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.concurrent.locks.Lock;
010: import java.util.concurrent.locks.ReentrantReadWriteLock;
011:
012: import org.openrdf.model.BNode;
013: import org.openrdf.model.Literal;
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.model.ValueFactory;
019: import org.openrdf.model.impl.ValueFactoryBase;
020: import org.openrdf.sail.rdbms.exceptions.RdbmsException;
021: import org.openrdf.sail.rdbms.exceptions.RdbmsRuntimeException;
022: import org.openrdf.sail.rdbms.managers.BNodeManager;
023: import org.openrdf.sail.rdbms.managers.LiteralManager;
024: import org.openrdf.sail.rdbms.managers.PredicateManager;
025: import org.openrdf.sail.rdbms.managers.UriManager;
026: import org.openrdf.sail.rdbms.model.RdbmsBNode;
027: import org.openrdf.sail.rdbms.model.RdbmsLiteral;
028: import org.openrdf.sail.rdbms.model.RdbmsResource;
029: import org.openrdf.sail.rdbms.model.RdbmsStatement;
030: import org.openrdf.sail.rdbms.model.RdbmsURI;
031: import org.openrdf.sail.rdbms.model.RdbmsValue;
032: import org.openrdf.sail.rdbms.schema.IdSequence;
033: import org.openrdf.sail.rdbms.schema.LiteralTable;
034: import org.openrdf.sail.rdbms.schema.ValueTable;
035:
036: /**
037: * Provides basic value creation both for traditional values as well as values
038: * with an internal id. {@link RdbmsValue}s behaviour similar to the default
039: * {@link Value} implementation with the addition that they also include an
040: * internal id and a version associated with that id. The internal ids should
041: * not be accessed directly, but rather either through this class or the
042: * corresponding manager class.
043: *
044: * @author James Leigh
045: *
046: */
047: public class RdbmsValueFactory extends ValueFactoryBase {
048: @Deprecated
049: public static final String NIL_LABEL = "nil";
050: private ValueFactory vf;
051: private BNodeManager bnodes;
052: private UriManager uris;
053: private LiteralManager literals;
054: private PredicateManager predicates;
055: private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
056: private IdSequence ids;
057:
058: public void setIdSequence(IdSequence ids) {
059: this .ids = ids;
060: }
061:
062: public void setBNodeManager(BNodeManager bnodes) {
063: this .bnodes = bnodes;
064: }
065:
066: public void setURIManager(UriManager uris) {
067: this .uris = uris;
068: }
069:
070: public void setLiteralManager(LiteralManager literals) {
071: this .literals = literals;
072: }
073:
074: public void setPredicateManager(PredicateManager predicates) {
075: this .predicates = predicates;
076: }
077:
078: public void setDelegate(ValueFactory vf) {
079: this .vf = vf;
080: }
081:
082: public void flush() throws RdbmsException {
083: try {
084: bnodes.flush();
085: uris.flush();
086: literals.flush();
087: } catch (SQLException e) {
088: throw new RdbmsException(e);
089: } catch (InterruptedException e) {
090: throw new RdbmsException(e);
091: }
092: }
093:
094: public RdbmsBNode createBNode(String nodeID) {
095: RdbmsBNode resource = bnodes.findInCache(nodeID);
096: if (resource == null) {
097: try {
098: BNode impl = vf.createBNode(nodeID);
099: resource = new RdbmsBNode(impl);
100: bnodes.cache(resource);
101: } catch (SQLException e) {
102: throw new RdbmsRuntimeException(e);
103: } catch (InterruptedException e) {
104: throw new RdbmsRuntimeException(e);
105: }
106: }
107: return resource;
108: }
109:
110: public RdbmsLiteral createLiteral(String label) {
111: return asRdbmsLiteral(vf.createLiteral(label));
112: }
113:
114: public RdbmsLiteral createLiteral(String label, String language) {
115: if (LiteralTable.ONLY_INSERT_LABEL)
116: return createLiteral(label);
117: return asRdbmsLiteral(vf.createLiteral(label, language));
118: }
119:
120: public RdbmsLiteral createLiteral(String label, URI datatype) {
121: if (LiteralTable.ONLY_INSERT_LABEL)
122: return createLiteral(label);
123: return asRdbmsLiteral(vf.createLiteral(label, datatype));
124: }
125:
126: public RdbmsStatement createStatement(Resource subject,
127: URI predicate, Value object) {
128: return createStatement(subject, predicate, object, null);
129: }
130:
131: public RdbmsStatement createStatement(Resource subject,
132: URI predicate, Value object, Resource context) {
133: RdbmsResource subj = asRdbmsResource(subject);
134: RdbmsURI pred = asRdbmsURI(predicate);
135: RdbmsValue obj = asRdbmsValue(object);
136: RdbmsResource ctx = asRdbmsResource(context);
137: return new RdbmsStatement(subj, pred, obj, ctx);
138: }
139:
140: public RdbmsURI createURI(String uri) {
141: RdbmsURI resource = uris.findInCache(uri);
142: if (resource == null) {
143: try {
144: URI impl = vf.createURI(uri);
145: resource = new RdbmsURI(impl);
146: uris.cache(resource);
147: } catch (SQLException e) {
148: throw new RdbmsRuntimeException(e);
149: } catch (InterruptedException e) {
150: throw new RdbmsRuntimeException(e);
151: }
152: }
153: return resource;
154: }
155:
156: public RdbmsURI createURI(String namespace, String localName) {
157: return createURI(namespace + localName);
158: }
159:
160: public RdbmsResource getRdbmsResource(Number num, String stringValue) {
161: Number id = ids.idOf(num);
162: if (ids.isURI(id))
163: return new RdbmsURI(id, uris.getIdVersion(), vf
164: .createURI(stringValue));
165: return new RdbmsBNode(id, bnodes.getIdVersion(), vf
166: .createBNode(stringValue));
167: }
168:
169: public RdbmsLiteral getRdbmsLiteral(Number num, String label,
170: String language, String datatype) {
171: Number id = ids.idOf(num);
172: if (datatype == null && language == null)
173: return new RdbmsLiteral(id, literals.getIdVersion(), vf
174: .createLiteral(label));
175: if (datatype == null)
176: return new RdbmsLiteral(id, literals.getIdVersion(), vf
177: .createLiteral(label, language));
178: return new RdbmsLiteral(id, literals.getIdVersion(), vf
179: .createLiteral(label, vf.createURI(datatype)));
180: }
181:
182: public RdbmsResource asRdbmsResource(Resource node) {
183: if (node == null)
184: return null;
185: if (node instanceof URI)
186: return asRdbmsURI((URI) node);
187: if (node instanceof RdbmsBNode) {
188: try {
189: bnodes.cache((RdbmsBNode) node);
190: return (RdbmsBNode) node;
191: } catch (SQLException e) {
192: throw new RdbmsRuntimeException(e);
193: } catch (InterruptedException e) {
194: throw new RdbmsRuntimeException(e);
195: }
196: }
197: return createBNode(((BNode) node).getID());
198: }
199:
200: public RdbmsURI asRdbmsURI(URI uri) {
201: if (uri == null)
202: return null;
203: if (uri instanceof RdbmsURI) {
204: try {
205: uris.cache((RdbmsURI) uri);
206: return (RdbmsURI) uri;
207: } catch (SQLException e) {
208: throw new RdbmsRuntimeException(e);
209: } catch (InterruptedException e) {
210: throw new RdbmsRuntimeException(e);
211: }
212: }
213: return createURI(uri.stringValue());
214: }
215:
216: public RdbmsValue asRdbmsValue(Value value) {
217: if (value == null)
218: return null;
219: if (value instanceof Literal)
220: return asRdbmsLiteral((Literal) value);
221: return asRdbmsResource((Resource) value);
222: }
223:
224: public RdbmsLiteral asRdbmsLiteral(Literal literal) {
225: try {
226: if (literal instanceof RdbmsLiteral) {
227: literals.cache((RdbmsLiteral) literal);
228: return (RdbmsLiteral) literal;
229: }
230: RdbmsLiteral lit = literals.findInCache(literal);
231: if (lit == null) {
232: lit = new RdbmsLiteral(literal);
233: literals.cache(lit);
234: }
235: return lit;
236: } catch (SQLException e) {
237: throw new RdbmsRuntimeException(e);
238: } catch (InterruptedException e) {
239: throw new RdbmsRuntimeException(e);
240: }
241: }
242:
243: public RdbmsResource[] asRdbmsResource(Resource... contexts) {
244: RdbmsResource[] ctxs = new RdbmsResource[contexts.length];
245: for (int i = 0; i < ctxs.length; i++) {
246: ctxs[i] = asRdbmsResource(contexts[i]);
247: }
248: return ctxs;
249: }
250:
251: public RdbmsStatement asRdbmsStatement(Statement stmt) {
252: if (stmt instanceof RdbmsStatement)
253: return (RdbmsStatement) stmt;
254: Resource s = stmt.getSubject();
255: URI p = stmt.getPredicate();
256: Value o = stmt.getObject();
257: Resource c = stmt.getContext();
258: return createStatement(s, p, o, c);
259: }
260:
261: public Number getInternalId(Value r) throws RdbmsException {
262: try {
263: if (r == null)
264: return ValueTable.NIL_ID;
265: RdbmsValue value = asRdbmsValue(r);
266: if (value instanceof RdbmsURI)
267: return uris.getInternalId((RdbmsURI) value);
268: if (value instanceof RdbmsBNode)
269: return bnodes.getInternalId((RdbmsBNode) value);
270: return literals.getInternalId((RdbmsLiteral) value);
271: } catch (SQLException e) {
272: throw new RdbmsException(e);
273: } catch (InterruptedException e) {
274: throw new RdbmsRuntimeException(e);
275: }
276: }
277:
278: public Number getPredicateId(RdbmsURI predicate)
279: throws RdbmsException {
280: try {
281: return predicates.getIdOfPredicate(predicate);
282: } catch (SQLException e) {
283: throw new RdbmsException(e);
284: } catch (InterruptedException e) {
285: throw new RdbmsRuntimeException(e);
286: }
287: }
288:
289: public Lock getIdReadLock() {
290: return lock.readLock();
291: }
292:
293: public Lock getIdWriteLock() {
294: return lock.writeLock();
295: }
296: }
|