1 6 7 package com.hp.hpl.jena.mem; 8 9 import java.util.*; 10 11 import com.hp.hpl.jena.graph.*; 12 import com.hp.hpl.jena.shared.*; 13 import com.hp.hpl.jena.util.CollectionFactory; 14 import com.hp.hpl.jena.util.iterator.*; 15 16 19 public class MixedGraphMem extends GraphMemBase implements Graph 20 { 21 protected Thing thing = new Thing( this ); 22 23 public static class Thing 24 { 25 protected final Graph parent; 26 27 public Thing( Graph parent ) 28 { this.parent = parent; } 29 30 protected Map map = CollectionFactory.createHashedMap(); 31 32 protected int size = 0; 33 34 protected boolean add( Node key, Triple t ) 35 { 36 Set s = (Set) map.get( key ); 37 if (s == null) map.put( key, s = CollectionFactory.createHashedSet() ); 38 return s.add( t ); 39 } 40 41 protected boolean remove( Node key, Triple t ) 42 { 43 Set s = (Set) map.get( key ); 44 if (s != null) 45 { 46 boolean removed = s.remove( t ); 47 if (s.isEmpty()) map.put( key, null ); 48 return removed; 49 } 50 else 51 return false; 52 } 53 54 public void add( Triple t ) 55 { 56 if (add( t.getSubject(), t )) size += 1; 57 add( t.getPredicate(), t ); 58 add( t.getObject(), t ); 59 } 60 61 public void remove( Triple t ) 62 { 63 if (remove( t.getSubject(), t )) size -= 1; 64 remove( t.getPredicate(), t ); 65 remove( t.getObject(), t ); 66 } 67 68 public boolean contains( Triple t ) 69 { 70 Set s = (Set) map.get( t.getSubject() ); 71 return s != null && s.contains( t ); 72 } 73 74 public ExtendedIterator iterator( final Node key, Triple pattern ) 75 { 76 Set s = (Set) map.get( key ); 77 if (s == null) 78 return NullIterator.instance; 79 else 80 { 81 final Iterator it = s.iterator(); 82 return new NiceIterator() 83 { 84 private Triple remember = null; 85 86 public Object next() 87 { return remember = (Triple) it.next(); } 88 89 public boolean hasNext() 90 { return it.hasNext(); } 91 92 public void excise( Node k, Triple triple ) 93 { 94 if (k != key) Thing.this.remove( k, triple ); 95 } 96 97 public void remove() 98 { 99 it.remove(); 100 size -= 1; 101 excise( remember.getSubject(), remember ); 102 excise( remember.getPredicate(), remember ); 103 excise( remember.getObject(), remember ); 104 parent.getEventManager().notifyDeleteTriple( parent, remember ); 105 } 106 107 } .filterKeep( new TripleMatchFilter( pattern ) ); 108 } 109 } 110 111 public ExtendedIterator iterator( final Triple pattern ) 112 { 113 return new NiceIterator() 114 { 115 protected Iterator keys = map.keySet().iterator(); 116 protected Iterator current = NullIterator.instance; 117 protected Triple triple = null; 118 protected Triple remember = null; 119 protected Node key = null; 120 protected Set seen = CollectionFactory.createHashedSet(); 121 protected Filter filter = new TripleMatchFilter( pattern ); 122 123 protected Triple ANY = Triple.create( "?? ?? ??" ); 124 125 public Object next() 126 { 127 ensureHasNext(); 128 try { return remember = triple; } finally { triple = null; } 129 } 130 131 public boolean hasNext() 132 { 133 if (triple == null) 134 { 135 while (current.hasNext()) 136 { 137 triple = (Triple) current.next(); 138 if (!pattern.matches( triple ) || seen.contains( triple )) 139 { 140 triple = null; 141 } 142 else 143 { 144 seen.add( triple ); 145 return true; 146 } 147 } 148 if (keys.hasNext()) 149 { 150 key = (Node) keys.next(); 151 Set s = (Set) map.get( key ); 152 if (s == null) return hasNext(); 153 current = s.iterator(); 154 return hasNext(); 155 } 156 return false; 157 } 158 else 159 return true; 160 } 161 162 public void excise( Node key, Triple triple ) 163 { 164 if (key != this.key) Thing.this.remove( key, triple ); 165 } 166 167 public void remove() 168 { 169 current.remove(); 170 size -= 1; 171 excise( remember.getSubject(), remember ); 172 excise( remember.getPredicate(), remember ); 173 excise( remember.getObject(), remember ); 174 parent.getEventManager().notifyDeleteTriple( parent, remember ); 175 } 176 }; 177 } 178 179 public boolean isEmpty() 180 { return size == 0; } 181 182 public int size() 183 { return size; } 184 185 public void clear() 186 { map.clear(); size = 0; } 187 } 188 189 public MixedGraphMem() 190 { this( ReificationStyle.Minimal ); } 191 192 public MixedGraphMem( ReificationStyle style ) 193 { super( style ); } 194 195 public void performAdd( Triple t ) 196 { if (!getReifier().handledAdd( t )) thing.add( t ); } 197 198 public void performDelete( Triple t ) 199 { if (!getReifier().handledRemove( t )) thing.remove( t ); } 200 201 public int graphBaseSize() 202 { return thing.size(); } 203 204 209 public boolean graphBaseContains( Triple t ) 210 { return t.isConcrete() ? thing.contains( t ) : containsByFind( t ); } 211 212 protected void destroy() 213 { thing = null; } 214 215 public boolean isEmpty() 216 { 217 checkOpen(); 218 return thing.isEmpty(); 219 } 220 221 public BulkUpdateHandler getBulkUpdateHandler() 222 { 223 if (bulkHandler == null) bulkHandler = new GraphMemBulkUpdateHandler( this ) 224 { 225 protected void clearComponents() 226 { 227 MixedGraphMem g = (MixedGraphMem) graph; 228 g.thing.clear(); 229 } 230 }; 231 return bulkHandler; 232 } 233 234 public ExtendedIterator graphBaseFind( TripleMatch m ) 235 { 236 Triple t = m.asTriple(); 237 Node S = t.getSubject(), P = t.getPredicate(), O = t.getObject(); 238 return 239 S.isConcrete() ? thing.iterator( S, t ) 240 : P.isConcrete() ? thing.iterator( P, t ) 241 : O.isURI() || O.isBlank() ? thing.iterator( O, t ) 242 : thing.iterator( m.asTriple() ) 243 ; 244 } 245 246 } 247 248 249 278 | Popular Tags |