001: /*
002: (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved.
004: $Id: ReificationCacheMap.java,v 1.3 2008/01/02 12:08:23 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.db.impl;
008:
009: import com.hp.hpl.jena.graph.*;
010: import com.hp.hpl.jena.shared.JenaException;
011:
012: /**
013: Reification cache map, extracted from SpecialisedGraphReifier_RDB.
014: */
015: public class ReificationCacheMap {
016: /**
017: Comment for <code>reifier_RDB</code>
018: */
019: private final SpecializedGraphReifier_RDB reifier_RDB;
020: protected int cacheSize = 1;
021: protected ReificationCache[] cache;
022: protected boolean[] inUse;
023:
024: ReificationCacheMap(SpecializedGraphReifier_RDB reifier_RDB,
025: int size) {
026: this .reifier_RDB = reifier_RDB;
027: inUse = new boolean[size];
028: cache = new ReificationCache[size];
029: for (int i = 0; i < size; i++)
030: inUse[i] = false;
031: }
032:
033: ReificationCache lookup(Node stmtURI) {
034: for (int i = 0; i < cache.length; i++) {
035: if (inUse[i] && (cache[i].getStmtURI().equals(stmtURI)))
036: return cache[i];
037: }
038: return null;
039: }
040:
041: public void flushAll() {
042: for (int i = 0; i < cache.length; i++)
043: inUse[i] = false;
044: }
045:
046: public void flush(ReificationCache entry) {
047: flushAll(); // optimize later
048: }
049:
050: public ReificationCache load(Node stmtURI) {
051: ReificationCache entry = lookup(stmtURI);
052: if (entry != null)
053: return entry;
054: return load(stmtURI, null, null, null);
055: }
056:
057: public ReificationCache load(Node stmtURI, Triple s,
058: ReificationStatementMask sm, ReificationStatementMask dm) {
059: flushAll();
060: ReificationStatementMask m = new ReificationStatementMask();
061: Triple t;
062: boolean hasSubj, hasPred, hasObj, hasType;
063: boolean checkSame = sm != null;
064: int cnt = 0;
065: ResultSetReifIterator it = this .reifier_RDB.m_reif
066: .findReifStmt(stmtURI, false, this .reifier_RDB.my_GID,
067: false);
068: while (it.hasNext()) {
069: cnt++;
070: Triple db = (Triple) it.next();
071: ReificationStatementMask n = new ReificationStatementMask();
072: hasSubj = !db.getSubject().equals(Node.NULL);
073: if (hasSubj && checkSame)
074: if (db.getSubject().equals(s.getSubject()))
075: sm.setHasSubj();
076: else
077: dm.setHasSubj();
078: hasPred = !db.getPredicate().equals(Node.NULL);
079: if (hasPred && checkSame)
080: if (db.getPredicate().equals(s.getPredicate()))
081: sm.setHasPred();
082: else
083: dm.setHasPred();
084: hasObj = !db.getObject().equals(Node.NULL);
085: if (hasObj && checkSame)
086: if (db.getObject().equals(s.getObject()))
087: sm.setHasObj();
088: else
089: dm.setHasObj();
090:
091: hasType = it.getHasType();
092:
093: n.setMask(hasSubj, hasPred, hasObj, hasType);
094: if (n.hasNada())
095: throw new JenaException("Fragment has no data");
096: m.setMerge(n);
097: }
098: if (cnt == 0)
099: return null; // no fragments for subject
100:
101: if (m.hasSPOT() && (cnt == 1))
102: m.setIsStmt();
103:
104: inUse[0] = true;
105: cache[0] = new ReificationCache(stmtURI, m, cnt);
106: return cache[0];
107: }
108:
109: }
110:
111: /*
112: * (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development
113: * Company, LP All rights reserved.
114: *
115: * Redistribution and use in source and binary forms, with or without
116: * modification, are permitted provided that the following conditions are met:
117: * 1. Redistributions of source code must retain the above copyright notice,
118: * this list of conditions and the following disclaimer. 2. Redistributions in
119: * binary form must reproduce the above copyright notice, this list of
120: * conditions and the following disclaimer in the documentation and/or other
121: * materials provided with the distribution. 3. The name of the author may not
122: * be used to endorse or promote products derived from this software without
123: * specific prior written permission.
124: *
125: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
126: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
127: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
128: * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
129: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
130: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
131: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
132: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
133: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
134: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
135: */
|