1 5 6 package com.hp.hpl.jena.db; 7 8 import com.hp.hpl.jena.db.impl.*; 9 import com.hp.hpl.jena.graph.*; 10 import com.hp.hpl.jena.graph.impl.*; 11 import com.hp.hpl.jena.graph.query.QueryHandler; 12 import com.hp.hpl.jena.shared.*; 13 import com.hp.hpl.jena.util.iterator.*; 14 15 import java.util.*; 16 17 53 public class GraphRDB extends GraphBase implements Graph { 54 55 57 static public final String DEFAULT = "DEFAULT"; 58 59 protected IRDBDriver m_driver = null; 60 protected DBPropGraph m_properties = null; 61 protected DBPrefixMappingImpl m_prefixMapping = null; 62 protected List m_specializedGraphs = null; 63 protected List m_specializedGraphReifiers = null; 64 protected Reifier m_reifier = null; 65 66 protected int m_reificationBehaviour = 0; 67 68 84 public static final int OPTIMIZE_ALL_REIFICATIONS_AND_HIDE_NOTHING = 1; 85 86 106 public static final int OPTIMIZE_AND_HIDE_FULL_AND_PARTIAL_REIFICATIONS = 2; 107 108 125 public static final int OPTIMIZE_AND_HIDE_ONLY_FULL_REIFICATIONS = 3; 126 127 130 public static int styleRDB( ReificationStyle style ) 131 { 132 if (style == ReificationStyle.Standard) 133 return GraphRDB.OPTIMIZE_ALL_REIFICATIONS_AND_HIDE_NOTHING; 134 if (style == ReificationStyle.Convenient) 135 return GraphRDB.OPTIMIZE_AND_HIDE_FULL_AND_PARTIAL_REIFICATIONS; 136 if (style == ReificationStyle.Minimal) 137 return GraphRDB.OPTIMIZE_AND_HIDE_ONLY_FULL_REIFICATIONS; 138 throw new JenaException( "unsupported reification style" ); 139 } 140 141 144 public static ReificationStyle styleRDB( int behaviour ) 145 { 146 if (behaviour == OPTIMIZE_ALL_REIFICATIONS_AND_HIDE_NOTHING) 147 return ReificationStyle.Standard; 148 if (behaviour == OPTIMIZE_AND_HIDE_FULL_AND_PARTIAL_REIFICATIONS) 149 return ReificationStyle.Convenient; 150 if (behaviour == OPTIMIZE_AND_HIDE_ONLY_FULL_REIFICATIONS) 151 return ReificationStyle.Minimal; 152 throw new JenaException( "unsupported reification behaviour" ); 153 } 154 155 168 public GraphRDB( IDBConnection con, String graphID, Graph requestedProperties, boolean isNew) { 169 this(con, graphID, requestedProperties, OPTIMIZE_AND_HIDE_FULL_AND_PARTIAL_REIFICATIONS, isNew); 170 } 171 172 187 public GraphRDB( IDBConnection con, String graphID, Graph requestedProperties, int reificationBehaviour, boolean isNew) { 188 super( styleRDB( reificationBehaviour ) ); 189 m_reificationBehaviour = reificationBehaviour; 190 191 if(graphID == null) 192 graphID = DEFAULT; 193 else if ( graphID.equals(DEFAULT) ) 194 throw new JenaException("The model name \"" + DEFAULT + "\" is reserved."); 195 196 m_driver = con.getDriver(); 198 SpecializedGraph sysGraph = m_driver.getSystemSpecializedGraph(true); 199 200 m_properties = DBPropGraph.findPropGraphByName( sysGraph, graphID ); 202 203 if( m_properties != null) { 204 if( isNew ) 205 throw new AlreadyExistsException( graphID ); 206 if( requestedProperties != null ) 207 throw new RDFRDBException("Error: attempt to change a graph's properties after it has been used."); 208 m_specializedGraphs = m_driver.recreateSpecializedGraphs( m_properties ); 209 } 210 else { 211 if( !isNew ) 212 throw new DoesNotExistException( graphID ); 213 if( requestedProperties == null ) 214 throw new RDFRDBException("Error: requested properties is null"); 215 216 221 m_specializedGraphs = m_driver.createSpecializedGraphs( graphID, requestedProperties ); 222 m_properties = DBPropGraph.findPropGraphByName( sysGraph, graphID ); 223 if ( m_properties == null ) 224 throw new RDFRDBException("Graph properties not found after creating graph."); 225 } 226 227 230 m_specializedGraphReifiers = new ArrayList(); 231 Iterator it = m_specializedGraphs.iterator(); 232 while( it.hasNext() ) { 233 Object o = it.next(); 234 if( o instanceof SpecializedGraphReifier ) 235 m_specializedGraphReifiers.add(o); 236 } 237 } 238 239 248 public Node getNode() { 249 if(m_properties == null) 250 throw new RDFRDBException("Error - attempt to call getNode() on a GraphRDB that has already been removed"); 251 return m_properties.getNode(); 252 } 253 254 263 public ExtendedIterator getPropertyTriples() { 264 if(m_properties == null) 265 throw new RDFRDBException("Error - attempt to call getPropertyTriples on a GraphRDB that has been removed."); 266 return m_properties.listTriples(); 267 } 268 269 protected boolean isOpen() 270 { return m_specializedGraphs != null; } 271 272 protected void checkOpen() 273 { if (isOpen() == false) throw new ClosedException( "GraphRDB", this ); } 274 275 278 public void performAdd(Triple t) { 279 checkOpen(); 280 SpecializedGraph.CompletionFlag complete = new SpecializedGraph.CompletionFlag(); 281 Iterator it = m_specializedGraphs.iterator(); 282 while( it.hasNext() ) { 283 SpecializedGraph sg = (SpecializedGraph) it.next(); 284 if( sg instanceof SpecializedGraphReifier && m_reificationBehaviour == OPTIMIZE_AND_HIDE_ONLY_FULL_REIFICATIONS) 285 continue; sg.add( t, complete); 287 if( complete.isDone()) 288 return; 289 } 290 291 throw new JenaException("Error - GraphRDB.add(Triple) failed to find a suitable store for the triple:"+t.toString()); 292 293 } 294 295 299 public void add(List triples) { 300 checkOpen(); 301 ArrayList localTriples = new ArrayList( triples ); 302 SpecializedGraph.CompletionFlag complete = new SpecializedGraph.CompletionFlag(); 303 Iterator it = m_specializedGraphs.iterator(); 304 while( it.hasNext() ) { 305 SpecializedGraph sg = (SpecializedGraph) it.next(); 306 if( sg instanceof SpecializedGraphReifier && m_reificationBehaviour == OPTIMIZE_AND_HIDE_ONLY_FULL_REIFICATIONS) 307 continue; sg.add( localTriples, complete); 309 if( complete.isDone()) 310 return; 311 } 312 313 throw new JenaException("Error - GraphRDB.add(List) failed to find a suitable store for at least one triple:"+triples.get(0).toString()); 314 315 } 316 317 320 public void performDelete(Triple t) { 321 checkOpen(); 322 SpecializedGraph.CompletionFlag complete = new SpecializedGraph.CompletionFlag(); 323 Iterator it = m_specializedGraphs.iterator(); 324 while( it.hasNext() ) { 325 SpecializedGraph sg = (SpecializedGraph) it.next(); 326 if( sg instanceof SpecializedGraphReifier && m_reificationBehaviour == OPTIMIZE_AND_HIDE_ONLY_FULL_REIFICATIONS) 327 continue; sg.delete( t, complete); 329 if( complete.isDone()) 330 return; 331 } 332 333 throw new JenaException("Error - GraphRDB.delete(Triple) failed to find a suitable store for the triple:"+t.toString()); 334 335 } 336 337 341 public void delete( List triples ) { 342 checkOpen(); 343 ArrayList localTriples = new ArrayList( triples ); 344 SpecializedGraph.CompletionFlag complete = new SpecializedGraph.CompletionFlag(); 345 Iterator it = m_specializedGraphs.iterator(); 346 while( it.hasNext() ) { 347 SpecializedGraph sg = (SpecializedGraph) it.next(); 348 if( sg instanceof SpecializedGraphReifier && m_reificationBehaviour == OPTIMIZE_AND_HIDE_ONLY_FULL_REIFICATIONS) 349 continue; sg.delete( localTriples, complete); 351 if( complete.isDone()) 352 return; 353 } 354 355 throw new JenaException("Error - GraphRDB.delete(Triple) failed to find a suitable store for at least one triple:"+triples.get(0).toString()); 356 357 } 358 359 362 public int graphBaseSize() { 363 checkOpen(); 364 int result = 0; 365 Iterator it = m_specializedGraphs.iterator(); 366 while( it.hasNext() ) { 367 SpecializedGraph sg = (SpecializedGraph) it.next(); 368 if( sg instanceof SpecializedGraphReifier && 369 (m_reificationBehaviour == OPTIMIZE_AND_HIDE_ONLY_FULL_REIFICATIONS || 370 m_reificationBehaviour == OPTIMIZE_AND_HIDE_FULL_AND_PARTIAL_REIFICATIONS)) 371 continue; result += sg.tripleCount(); 373 } 374 return result; 375 } 376 377 380 public boolean graphBaseContains(Triple t) { 381 checkOpen(); 382 SpecializedGraph.CompletionFlag complete = new SpecializedGraph.CompletionFlag(); 383 Iterator it = m_specializedGraphs.iterator(); 384 while( it.hasNext() ) { 385 SpecializedGraph sg = (SpecializedGraph) it.next(); 386 if( sg instanceof SpecializedGraphReifier && 387 (m_reificationBehaviour == OPTIMIZE_AND_HIDE_ONLY_FULL_REIFICATIONS || 388 m_reificationBehaviour == OPTIMIZE_AND_HIDE_FULL_AND_PARTIAL_REIFICATIONS)) 389 continue; boolean result = sg.contains( t, complete); 391 if( result == true || complete.isDone() == true ) 392 return result; 393 } 394 return false; 395 } 396 397 400 public ExtendedIterator graphBaseFind(TripleMatch m) { 401 checkOpen(); 402 ExtendedIterator result = NullIterator.instance; 403 SpecializedGraph.CompletionFlag complete = new SpecializedGraph.CompletionFlag(); 404 Iterator it = m_specializedGraphs.iterator(); 405 while( it.hasNext() ) { 406 SpecializedGraph sg = (SpecializedGraph) it.next(); 407 if( sg instanceof SpecializedGraphReifier && 408 (m_reificationBehaviour == OPTIMIZE_AND_HIDE_ONLY_FULL_REIFICATIONS || 409 m_reificationBehaviour == OPTIMIZE_AND_HIDE_FULL_AND_PARTIAL_REIFICATIONS)) 410 continue; ExtendedIterator partialResult = sg.find( m, complete); 412 result = result.andThen(partialResult); 413 if( complete.isDone()) 414 break; 415 } 416 return SimpleEventManager.notifyingRemove( this, result ); 417 } 418 419 public ExtendedIterator reifierTriples( TripleMatch m ) 420 { return NullIterator.instance; } 421 422 public int reifierSize() 423 { return 0; } 424 425 428 public BulkUpdateHandler getBulkUpdateHandler() 429 { return new DBBulkUpdateHandler( this ); } 430 431 435 public Reifier getReifier() { 436 if (m_reifier == null) 437 m_reifier = new DBReifier( this, style, m_specializedGraphReifiers, m_specializedGraphReifiers ); 438 return m_reifier; 439 } 440 441 444 public PrefixMapping getPrefixMapping() { 445 if( m_prefixMapping == null) 446 m_prefixMapping = new DBPrefixMappingImpl( m_properties ); 447 return m_prefixMapping; 448 } 449 450 451 454 public TransactionHandler getTransactionHandler() { 455 return new DBTransactionHandler(m_driver, this); 456 } 457 458 461 public synchronized void close() { 462 if( m_specializedGraphs != null) { 463 Iterator it = m_specializedGraphs.iterator(); 464 while( it.hasNext() ) { 465 SpecializedGraph sg = (SpecializedGraph) it.next(); 466 sg.close(); 467 } 468 m_specializedGraphs = null; 469 } 470 } 471 472 481 public synchronized void remove() { 482 checkOpen(); 483 m_driver.removeSpecializedGraphs( m_properties, m_specializedGraphs ); 485 m_properties = null; 486 m_specializedGraphs = null; 487 } 488 489 495 public IDBConnection getConnection() { 496 if( m_driver == null ) 497 return null; 498 return m_driver.getConnection(); 499 } 500 501 506 public int reificationBehavior( ) 507 { 508 return m_reificationBehaviour; 509 } 510 511 512 513 518 public Iterator getSpecializedGraphs() { 519 return m_specializedGraphs.iterator(); 520 } 521 522 private QueryHandler q = null; 523 524 527 public QueryHandler queryHandler() 528 { 529 if (q == null) q = new DBQueryHandler( this); 530 return q; 531 } 532 533 537 public boolean getDoDuplicateCheck() { 538 return m_driver.getDoDuplicateCheck(); 539 } 540 541 545 public void setDoDuplicateCheck(boolean bool) { 546 m_driver.setDoDuplicateCheck(bool); 547 boolean nb = !bool; 548 if (isOpen()) { 549 Iterator it = m_specializedGraphs.iterator(); 550 while (it.hasNext()) { 551 SpecializedGraph sg = (SpecializedGraph) it.next(); 552 sg.getPSet().setSkipDuplicateCheck(nb); 553 } 554 } 555 } 556 557 } 558 559 | Popular Tags |