001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: */
005:
006: package com.hp.hpl.jena.db.impl;
007:
008: import com.hp.hpl.jena.graph.*;
009: import com.hp.hpl.jena.util.iterator.*;
010: import com.hp.hpl.jena.vocabulary.DB;
011:
012: import java.util.*;
013:
014: /**
015: *
016: * A wrapper to assist in getting and setting DB information from
017: * a persistent store.
018: *
019: * This is written in the style of enhanced nodes - no state is
020: * stored in the DBStoreDesc, instead all state is in the
021: * underlying graph and this is just provided as a convenience.
022: *
023: * (We don't use enhanced nodes because, since we control everything
024: * in the persistent store system description, we can avoid any
025: * need to handle polymorhphism).
026: *
027: *
028: * @author csayers
029: * @version $Revision: 1.29 $
030: * @since Jena 2.0
031: */
032: public class DBPropGraph extends DBProp {
033:
034: public static Node_URI graphName = (Node_URI) DB.graphName.asNode();
035: public static Node_URI graphType = (Node_URI) DB.graphType.asNode();
036: public static Node_URI graphLSet = (Node_URI) DB.graphLSet.asNode();
037: public static Node_URI graphPrefix = (Node_URI) DB.graphPrefix
038: .asNode();
039: public static Node_URI graphId = (Node_URI) DB.graphId.asNode();
040: public static Node_URI stmtTable = (Node_URI) DB.stmtTable.asNode();
041: public static Node_URI reifTable = (Node_URI) DB.reifTable.asNode();
042:
043: public DBPropGraph(SpecializedGraph g, String symbolicName,
044: String type) {
045: super (g);
046:
047: putPropString(graphName, symbolicName);
048: putPropString(graphType, type);
049: }
050:
051: public DBPropGraph(SpecializedGraph g, Node n) {
052: super (g, n);
053: }
054:
055: public DBPropGraph(SpecializedGraph g, String newSymbolicName,
056: Graph oldProperties) {
057: super (g);
058:
059: putPropString(graphName, newSymbolicName);
060: // only copy user-configurable properties
061: Iterator it = oldProperties.find(Node.ANY, Node.ANY, Node.ANY);
062: while (it.hasNext()) {
063: Triple t = (Triple) it.next();
064: if (t.getPredicate().equals(graphName)
065: || t.getPredicate().equals(graphId)
066: || t.getPredicate().equals(stmtTable)
067: || t.getPredicate().equals(reifTable))
068: continue;
069: putPropNode((Node_URI) t.getPredicate(), t.getObject());
070: }
071: }
072:
073: /**
074: Answer false if already within a transaction, otherwise start a transaction
075: and answer true; use in conjunction with <code>conditionalCommit</code>.
076: */
077: public boolean begin() {
078: return getDriver().xactOp(DriverRDB.xactBeginIfNone);
079: }
080:
081: /**
082: If <code>commit</code> is true, commit the current transaction; to be used
083: in conjunction with <code>begin</code>.
084: */
085: public void conditionalCommit(boolean commit) {
086: if (commit)
087: getDriver().xactOp(DriverRDB.xactCommit);
088: }
089:
090: private DriverRDB getDriver() {
091: return (DriverRDB) graph.getPSet().driver();
092: }
093:
094: public void addLSet(DBPropLSet lset) {
095: putPropNode(graphLSet, lset.getNode());
096: }
097:
098: public void addPrefix(String prefix, String uri) {
099: Node prefixNode = Node.createLiteral(prefix), uriNode = Node
100: .createLiteral(uri);
101: boolean commit = begin();
102: Node B = bnodeForPrefix(prefixNode);
103: if (B == null)
104: addPrefixMaplet(prefixNode, uriNode);
105: else
106: updatePrefixMaplet(B, uriNode);
107: conditionalCommit(commit);
108: }
109:
110: /**
111: Update the existing prefix maplet off bnode <code>B</code> so that its
112: prefixURI is now <code>uriNode</code>.
113: */
114: private void updatePrefixMaplet(Node B, Node uriNode) {
115: Node current = getPropNode(B, DBPropPrefix.prefixURI);
116: if (!uriNode.equals(current)) {
117: delete(B, DBPropPrefix.prefixURI, current);
118: add(B, DBPropPrefix.prefixURI, uriNode);
119: }
120: }
121:
122: /**
123: Add a new prefix maplet <code>[prefixValue prefixNode; prefixURI uriNode]</code>
124: to the graph.
125: */
126: private void addPrefixMaplet(Node prefixNode, Node uriNode) {
127: Node BB = Node.createAnon();
128: add(self, graphPrefix, BB);
129: add(BB, DBPropPrefix.prefixURI, uriNode);
130: add(BB, DBPropPrefix.prefixValue, prefixNode);
131: }
132:
133: /**
134: Add the triple <code>(S, P, O)</code> to the graph.
135: */
136: private void add(Node S, Node P, Node O) {
137: graph.add(Triple.create(S, P, O), newComplete());
138: }
139:
140: /**
141: Remove the triple <code>(S, P, O)</code> from the graph.
142: */
143: private void delete(Node S, Node P, Node O) {
144: graph.delete(Triple.create(S, P, O), newComplete());
145: }
146:
147: /**
148: Answer the bnode which gives the prefix and uri in this graph for the given
149: literal<code>prefixNode</code>, or null if there isn't one.
150: */
151: public Node bnodeForPrefix(Node prefixNode) {
152: ExtendedIterator A = graph.find(self, graphPrefix, Node.ANY,
153: newComplete());
154: try {
155: while (A.hasNext()) {
156: Node B = ((Triple) A.next()).getObject();
157: if (graph.contains(Triple.create(B,
158: DBPropPrefix.prefixValue, prefixNode),
159: newComplete()))
160: return B;
161: }
162: return null;
163: } finally {
164: A.close();
165: }
166: }
167:
168: /**
169: @deprecated this method should never have been publically visible
170: @param prefix
171: */
172: public void addPrefix(DBPropPrefix prefix) {
173: // First drop existing uses of prefix or URI
174: DBPropPrefix existing = getPrefix(prefix.getValue());
175: if (existing != null) {
176: removePrefix(existing);
177: }
178: putPropNode(graphPrefix, prefix.getNode());
179: }
180:
181: /**
182: @deprecated this method should never habve been publically visible
183: */
184: public void removePrefix(DBPropPrefix prefix) {
185: SpecializedGraph.CompletionFlag complete = newComplete();
186: Iterator matches = graph.find(self, graphPrefix, prefix
187: .getNode(), complete);
188: if (matches.hasNext()) {
189: graph.delete((Triple) (matches.next()), complete);
190: prefix.remove();
191: }
192: }
193:
194: public void removePrefix(String prefix) {
195: Node prefixNode = Node.createLiteral(prefix);
196: boolean commit = begin();
197: Node B = bnodeForPrefix(prefixNode);
198: if (B != null) {
199: Node uriNode = getPropNode(B, DBPropPrefix.prefixURI);
200: delete(self, graphPrefix, B);
201: delete(B, DBPropPrefix.prefixURI, prefixNode);
202: delete(B, DBPropPrefix.prefixValue, uriNode);
203: }
204: conditionalCommit(commit);
205: }
206:
207: public void addGraphId(int id) {
208: putPropString(graphId, Integer.toString(id));
209: }
210:
211: public void addStmtTable(String table) {
212: putPropString(stmtTable, table);
213: }
214:
215: public void addReifTable(String table) {
216: putPropString(reifTable, table);
217: }
218:
219: public String getName() {
220: return getPropString(graphName);
221: }
222:
223: public String getType() {
224: return getPropString(graphType);
225: }
226:
227: public String getStmtTable() {
228: return getPropString(stmtTable);
229: }
230:
231: public String getReifTable() {
232: return getPropString(reifTable);
233: }
234:
235: public int getGraphId() {
236: String i = getPropString(graphId);
237: return i == null ? -1 : Integer.parseInt(i);
238: }
239:
240: public ExtendedIterator getAllLSets() {
241: return graph.find(self, graphLSet, null, newComplete())
242: .mapWith(new MapToLSet());
243: }
244:
245: public ExtendedIterator getAllPrefixes() {
246: return graph.find(self, graphPrefix, null, newComplete())
247: .mapWith(new MapToPrefix());
248: }
249:
250: /**
251: @deprecated this method should not have been visible
252: @param prefix
253: @return
254: */
255: public DBPropPrefix getPrefix(String prefix) {
256: ExtendedIterator prefixes = graph.find(self, graphPrefix, null,
257: newComplete()).mapWith(new MapToPrefix());
258: while (prefixes.hasNext()) {
259: DBPropPrefix dbp = (DBPropPrefix) prefixes.next();
260: if (dbp.getValue().compareTo(prefix) == 0)
261: return dbp;
262: }
263: return null;
264: }
265:
266: /**
267: @deprecated this method should not have been visible
268: @param uri
269: @return
270: */
271: public DBPropPrefix getURI(String uri) {
272: ExtendedIterator prefixes = getAllPrefixes();
273: while (prefixes.hasNext()) {
274: DBPropPrefix prefix = (DBPropPrefix) prefixes.next();
275: if (prefix.getURI().compareTo(uri) == 0)
276: return prefix;
277: }
278: return null;
279: }
280:
281: public ExtendedIterator listTriples() {
282: // First get all the triples that directly desrcribe this graph
283: ExtendedIterator result = DBProp.listTriples(graph, self);
284:
285: // Now get all the triples that describe any lsets
286: ExtendedIterator lsets = getAllLSets();
287: while (lsets.hasNext()) {
288: result = result.andThen(((DBPropLSet) lsets.next())
289: .listTriples());
290: }
291:
292: // Now get all the triples that describe any prefixes
293: ExtendedIterator prefixes = getAllPrefixes();
294: while (prefixes.hasNext()) {
295: result = result.andThen(((DBPropPrefix) prefixes.next())
296: .listTriples());
297: }
298: return result;
299: }
300:
301: private class MapToLSet implements Map1 {
302: public Object map1(Object o) {
303: Triple t = (Triple) o;
304: return new DBPropLSet(graph, t.getObject());
305: }
306: }
307:
308: /**
309: @deprecated only required in deprecated code
310: @author kers
311: */
312: private class MapToPrefix implements Map1 {
313: public Object map1(Object o) {
314: Triple t = (Triple) o;
315: return new DBPropPrefix(graph, t.getObject());
316: }
317: }
318:
319: public static DBPropGraph findPropGraphByName(
320: SpecializedGraph graph, String name) {
321: Node myNode = Node.createLiteral(name);
322: ClosableIterator it = graph.find(null, graphName, myNode,
323: newComplete());
324:
325: try {
326: if (it.hasNext())
327: return new DBPropGraph(graph, ((Triple) it.next())
328: .getSubject());
329: return null;
330: } finally {
331: it.close();
332: }
333: }
334:
335: /*
336: * return true if the DBPropGraph has the required
337: * properties for the named, stored graph.
338: */
339:
340: public boolean isDBPropGraphOk(String name) {
341: String s = getName();
342: boolean res = (s == null) ? false : s.equals(name);
343: res = res & (getGraphId() != -1);
344: res = res & (getType() != null);
345: res = res & (getStmtTable() != null);
346: res = res & (getReifTable() != null);
347: return res;
348: }
349:
350: public void remove() {
351: Iterator it = getAllPrefixes();
352: while (it.hasNext()) {
353: ((DBPropPrefix) it.next()).remove();
354: }
355: it = getAllLSets();
356: while (it.hasNext()) {
357: ((DBPropLSet) it.next()).remove();
358: }
359: super .remove();
360: }
361:
362: }
363:
364: /*
365: * (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
366: * All rights reserved.
367: *
368: * Redistribution and use in source and binary forms, with or without
369: * modification, are permitted provided that the following conditions
370: * are met:
371: * 1. Redistributions of source code must retain the above copyright
372: * notice, this list of conditions and the following disclaimer.
373: * 2. Redistributions in binary form must reproduce the above copyright
374: * notice, this list of conditions and the following disclaimer in the
375: * documentation and/or other materials provided with the distribution.
376: * 3. The name of the author may not be used to endorse or promote products
377: * derived from this software without specific prior written permission.
378:
379: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
380: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
381: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
382: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
383: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
384: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
385: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
386: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
387: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
388: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
389: */
|