001: /*
002: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: MixedGraphMemStore.java,v 1.5 2008/01/02 12:09:51 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.mem;
008:
009: import java.util.*;
010:
011: import com.hp.hpl.jena.graph.*;
012: import com.hp.hpl.jena.util.CollectionFactory;
013: import com.hp.hpl.jena.util.iterator.*;
014:
015: public class MixedGraphMemStore {
016: protected final Graph parent;
017:
018: public MixedGraphMemStore(Graph parent) {
019: this .parent = parent;
020: }
021:
022: protected Map map = CollectionFactory.createHashedMap();
023:
024: protected int size = 0;
025:
026: protected boolean add(Node key, Triple t) {
027: Set s = (Set) map.get(key);
028: if (s == null)
029: map.put(key, s = CollectionFactory.createHashedSet());
030: return s.add(t);
031: }
032:
033: protected boolean remove(Node key, Triple t) {
034: Set s = (Set) map.get(key);
035: if (s != null) {
036: boolean removed = s.remove(t);
037: if (s.isEmpty())
038: map.put(key, null);
039: return removed;
040: } else
041: return false;
042: }
043:
044: public void add(Triple t) {
045: if (add(t.getSubject(), t)) {
046: size += 1;
047: add(t.getPredicate(), t);
048: add(t.getObject(), t);
049: }
050: }
051:
052: public void remove(Triple t) {
053: if (remove(t.getSubject(), t)) {
054: size -= 1;
055: remove(t.getPredicate(), t);
056: remove(t.getObject(), t);
057: }
058: }
059:
060: public boolean contains(Triple t) {
061: Set s = (Set) map.get(t.getSubject());
062: return s != null && s.contains(t);
063: }
064:
065: public ExtendedIterator iterator(final Node key, Triple pattern) {
066: Set s = (Set) map.get(key);
067: if (s == null)
068: return NullIterator.instance;
069: else {
070: final Iterator it = s.iterator();
071: return new NiceIterator() {
072: private Triple remember = null;
073:
074: public Object next() {
075: return remember = (Triple) it.next();
076: }
077:
078: public boolean hasNext() {
079: return it.hasNext();
080: }
081:
082: public void excise(Node k, Triple triple) {
083: if (k != key)
084: MixedGraphMemStore.this .remove(k, triple);
085: }
086:
087: public void remove() {
088: it.remove();
089: size -= 1;
090: excise(remember.getSubject(), remember);
091: excise(remember.getPredicate(), remember);
092: excise(remember.getObject(), remember);
093: parent.getEventManager().notifyDeleteTriple(parent,
094: remember);
095: }
096:
097: }.filterKeep(new TripleMatchFilter(pattern));
098: }
099: }
100:
101: public ExtendedIterator iterator(final Triple pattern) {
102: return new NiceIterator() {
103: protected Iterator keys = map.keySet().iterator();
104: protected Iterator current = NullIterator.instance;
105: protected Triple triple = null;
106: protected Triple remember = null;
107: protected Node key = null;
108: protected Set seen = CollectionFactory.createHashedSet();
109:
110: public Object next() {
111: ensureHasNext();
112: try {
113: return remember = triple;
114: } finally {
115: triple = null;
116: }
117: }
118:
119: public boolean hasNext() {
120: if (triple == null) {
121: while (current.hasNext()) {
122: triple = (Triple) current.next();
123: if (!pattern.matches(triple)
124: || seen.contains(triple)) {
125: triple = null;
126: } else {
127: seen.add(triple);
128: return true;
129: }
130: }
131: if (keys.hasNext()) {
132: key = (Node) keys.next();
133: Set s = (Set) map.get(key);
134: if (s == null)
135: return hasNext();
136: current = s.iterator();
137: return hasNext();
138: }
139: return false;
140: } else
141: return true;
142: }
143:
144: public void excise(Node key, Triple triple) {
145: if (key != this .key)
146: MixedGraphMemStore.this .remove(key, triple);
147: }
148:
149: public void remove() {
150: current.remove();
151: size -= 1;
152: excise(remember.getSubject(), remember);
153: excise(remember.getPredicate(), remember);
154: excise(remember.getObject(), remember);
155: parent.getEventManager().notifyDeleteTriple(parent,
156: remember);
157: }
158: };
159: }
160:
161: public boolean isEmpty() {
162: return size == 0;
163: }
164:
165: public int size() {
166: return size;
167: }
168:
169: public void clear() {
170: map.clear();
171: size = 0;
172: }
173: }
174:
175: /*
176: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
177: * All rights reserved.
178: *
179: * Redistribution and use in source and binary forms, with or without
180: * modification, are permitted provided that the following conditions
181: * are met:
182: * 1. Redistributions of source code must retain the above copyright
183: * notice, this list of conditions and the following disclaimer.
184: * 2. Redistributions in binary form must reproduce the above copyright
185: * notice, this list of conditions and the following disclaimer in the
186: * documentation and/or other materials provided with the distribution.
187: * 3. The name of the author may not be used to endorse or promote products
188: * derived from this software without specific prior written permission.
189: *
190: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
191: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
192: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
193: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
194: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
195: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
196: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
197: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
198: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
199: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
200: */
|