1 6 7 package com.hp.hpl.jena.db.impl; 8 9 import java.sql.PreparedStatement ; 10 import java.sql.ResultSet ; 11 import java.sql.SQLException ; 12 import java.util.*; 13 14 import com.hp.hpl.jena.db.RDFRDBException; 15 import com.hp.hpl.jena.db.impl.DriverRDB; 16 import com.hp.hpl.jena.graph.*; 17 import com.hp.hpl.jena.shared.*; 18 import com.hp.hpl.jena.util.iterator.ExtendedIterator; 19 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 23 43 44 public class PSet_TripleStore_RDB implements IPSet { 45 46 49 53 public String m_tblName = null; 54 55 56 protected String ID_SQL_TYPE = null; 57 58 59 protected boolean SKIP_DUPLICATE_CHECK = false; 60 61 62 protected boolean CACHE_PREPARED_STATEMENTS = true; 63 64 65 protected SQLCache m_sql = null; 66 67 70 71 protected final static int DEFAULT_CACHE = 1000; 72 73 74 protected ICache literalCache = new SimpleCache(DEFAULT_CACHE); 75 76 77 protected ICache resourceCache = new SimpleCache(DEFAULT_CACHE); 78 79 82 protected IRDBDriver m_driver = null; 83 84 87 90 public PSet_TripleStore_RDB(){ 91 } 92 93 96 public void setDriver(IRDBDriver driver) throws RDFRDBException { 97 m_driver = driver; 98 } 99 100 protected static Log logger = LogFactory.getLog(PSet_TripleStore_RDB.class); 101 102 public void setSQLType(String value) { ID_SQL_TYPE = value; } 103 public void setSkipDuplicateCheck(boolean value) { SKIP_DUPLICATE_CHECK = value;} 104 public void setSQLCache(SQLCache cache ) { m_sql = cache; } 105 public SQLCache getSQLCache() { return m_sql; } 106 public void setCachePreparedStatements(boolean value) { CACHE_PREPARED_STATEMENTS = value; } 107 108 109 113 public void setTblName(String tblName){ 114 m_tblName = tblName; 115 } 116 117 121 public String getTblName() { 122 return m_tblName; 123 } 124 125 126 129 public void close() { 130 } 132 133 136 137 public IRDBDriver driver() { 138 return m_driver; 139 } 140 141 142 145 public void cleanDB() { 146 147 try { 149 m_sql.runSQLGroup("dropStatementTable",getTblName()); 150 } catch (SQLException e) { 151 logger.warn( "Problem dropping table " + getTblName(), e ); 152 throw new RDFRDBException("Failed to drop table ", e); 153 } 154 155 } 156 157 160 public String toString() { 161 return this.getClass().getPackage().getName(); 162 } 163 164 168 public Node_Literal getLiteralFromCache(IDBID id) { 169 return (Node_Literal) literalCache.get(id); 170 } 171 172 176 public IDBID wrapDBID(Object id) throws RDFRDBException { 177 if (id instanceof Number ) { 178 return new DBIDInt(((Number )id).intValue()); 179 } else if (id == null) { 180 return null; 181 } else { 182 throw new RDFRDBException("Unexpected DB identifier type: " + id); 183 } 185 } 186 187 192 public int rowCount(String tName) { 193 194 try { 195 int result = 0; 196 String op = "getRowCount"; 197 PreparedStatement ps = m_sql.getPreparedSQLStatement(op,tName); 198 ResultSet rs = ps.executeQuery(); 199 while ( rs.next() ) result = rs.getInt(1); 200 rs.close(); 201 m_sql.returnPreparedSQLStatement(ps); 202 return result; 203 } catch (SQLException e) { 204 logger.debug("tried to count rows in " + tName); 205 logger.debug("Caught exception: ", e); 206 throw new JenaException("Exception during database access", e); } 208 } 209 210 213 220 public Triple extractTripleFromRowData( 221 String subj, 222 String pred, 223 String obj) { 224 225 Node subjNode = subj == null ? null : m_driver.RDBStringToNode(subj); 226 Node predNode = pred == null ? null : m_driver.RDBStringToNode(pred); 227 Node objNode = obj == null ? null : m_driver.RDBStringToNode(obj); 228 229 return ( Triple.create(subjNode, predNode, objNode) ); 230 } 231 232 233 234 237 public Object wrapFlag(boolean flag) { 238 return flag ? new Short ((short)1) : new Short ((short)0); 239 } 240 241 254 public void deleteTriple(Triple t, IDBID graphID) { 255 deleteTriple(t, graphID, false, null); 256 } 257 258 271 public void deleteTriple(Triple t, IDBID graphID, boolean isBatch, 272 Hashtable batchedPreparedStatements) { 273 deleteTripleAR(t,graphID,null,isBatch,batchedPreparedStatements); 274 } 275 289 public void deleteTripleAR( 290 Triple t, 291 IDBID graphID, 292 Node reifNode, 293 boolean isBatch, 294 Hashtable batchedPreparedStatements) { 295 boolean isReif = reifNode != null; 296 297 String subj = 298 t.getSubject().equals(Node.NULL) ? null : m_driver.nodeToRDBString(t.getSubject(),false); 299 String pred = 300 t.getPredicate().equals(Node.NULL) ? null : m_driver.nodeToRDBString(t.getPredicate(),false); 301 String obj = 302 t.getObject() == Node.ANY ? null : m_driver.nodeToRDBString(t.getObject(),false); 303 int gid = ((DBIDInt) graphID).getIntID(); 305 int argc = 1; 306 String stmtStr; 307 308 if ((subj == null) || (pred == null) || (obj == null)) { 309 return; 312 } 313 314 PreparedStatement ps = null; 316 stmtStr = isReif ? "deleteReified" : "deleteStatement"; 317 try { 318 ps = 319 getPreparedStatement( 320 stmtStr, 321 getTblName(), 322 isBatch, 323 batchedPreparedStatements); 324 326 } catch (SQLException e1) { 327 logger.debug( "SQLException caught " + e1.getErrorCode(), e1); 328 throw new JenaException("Exception during database access", e1); } 330 331 try { 333 ps.setString(argc++, subj); 334 ps.setString(argc++, pred); 335 ps.setString(argc++, obj); 336 337 ps.setInt(argc++, gid); 338 339 if (isReif) { 340 String stmtURI = m_driver.nodeToRDBString(reifNode,false); 341 ps.setString(argc++, stmtURI); 342 ps.setString(argc++,"T"); 343 } 344 } catch (SQLException e1) { 345 logger.debug("(in delete) SQLException caught ", e1); 346 throw new JenaException("Exception during database access", e1); } 348 349 try { 350 if (isBatch) { 351 ps.addBatch(); 352 } else { 353 ps.executeUpdate(); 354 m_sql.returnPreparedSQLStatement(ps); 355 } 356 } catch (SQLException e1) { 357 logger.error("Exception executing delete: ", e1); 358 throw new JenaException("Exception during database access", e1); } 360 } 361 362 373 public void storeTriple(Triple t, IDBID graphID) { 374 storeTriple(t,graphID,false, null); 375 } 376 377 388 public PreparedStatement getPreparedStatement(String op, 389 String tableName, 390 boolean isBatch, 391 Hashtable batchedPreparedStatements) throws SQLException { 392 PreparedStatement ps = null; 393 String opname = SQLCache.concatOpName(op,tableName); 394 if (isBatch) { 395 ps = (PreparedStatement ) batchedPreparedStatements.get(opname); 396 if (ps == null) { 397 ps = m_sql.getPreparedSQLStatement(op,tableName); 398 batchedPreparedStatements.put(opname,ps); 399 } 400 } else { 401 ps = m_sql.getPreparedSQLStatement(op,tableName); 402 } 403 404 if (ps == null) { 405 logger.error("prepared statement not found for " + opname); 406 } 407 return ps; 408 } 409 410 411 422 public void storeTriple(Triple t, 423 IDBID graphID, 424 boolean isBatch, 425 Hashtable batchedPreparedStatements) { 426 storeTripleAR(t,graphID,null,false,isBatch,batchedPreparedStatements); 427 } 428 429 442 public void storeTripleAR( 443 Triple t, 444 IDBID graphID, 445 Node reifNode, 446 boolean hasType, 447 boolean isBatch, 448 Hashtable batchedPreparedStatements) { 449 String objURI; 450 Object obj_val; 451 boolean isReif = reifNode != null; 452 453 if (!SKIP_DUPLICATE_CHECK && !isReif) { 455 if (statementTableContains(graphID, t)) { 457 return; 458 } 459 } 460 461 String obj_res, obj_lex, obj_lit; 462 String subj = 464 t.getSubject().equals(Node.NULL) ? null : m_driver.nodeToRDBString(t.getSubject(),true); 465 String pred = 466 t.getPredicate().equals(Node.NULL) ? null : m_driver.nodeToRDBString(t.getPredicate(),true); 467 String obj = 468 t.getObject().equals(Node.NULL) ? null : m_driver.nodeToRDBString(t.getObject(),true); 469 int gid = ((DBIDInt) graphID).getIntID(); 471 472 int argc = 1; 473 String stmtStr; 474 475 if ((subj == null) || (pred == null) || (obj == null)) { 476 if (!isReif) 477 throw new JenaException("Attempt to assert triple with missing values"); 478 } 479 481 PreparedStatement ps = null; 482 stmtStr = isReif ? "insertReified" : "insertStatement"; 483 try { 484 ps = 485 getPreparedStatement( 486 stmtStr, 487 getTblName(), 488 isBatch, 489 batchedPreparedStatements); 490 492 } catch (SQLException e1) { 493 logger.debug("SQLException caught " + e1.getErrorCode(), e1); 494 throw new JenaException("Exception during database access", e1); } 496 try { 498 if (subj == null) 499 ps.setNull(argc++, java.sql.Types.VARCHAR); 500 else 501 ps.setString(argc++, subj); 502 if (pred == null) 503 ps.setNull(argc++, java.sql.Types.VARCHAR); 504 else 505 ps.setString(argc++, pred); 506 if (obj == null) 507 ps.setNull(argc++, java.sql.Types.VARCHAR); 508 else 509 ps.setString(argc++, obj); 510 511 ps.setInt(argc++, gid); 513 if (isReif) { 514 String stmtURI = m_driver.nodeToRDBString(reifNode,true); 515 ps.setString(argc++, stmtURI); 516 if (hasType == true) 517 ps.setString(argc++, "T"); 518 else 519 ps.setString(argc++, " "); } 521 522 } catch (SQLException e1) { 523 logger.debug("SQLException caught " + e1.getErrorCode(), e1); 524 throw new JenaException("Exception during database access", e1); } 526 527 try { 528 if (isBatch) { 529 ps.addBatch(); 530 } else { 531 ps.executeUpdate(); 532 m_sql.returnPreparedSQLStatement(ps); 533 } 534 } catch (SQLException e1) { 536 if (!((e1.getErrorCode() == 1) 538 && (m_driver.getDatabaseType().equalsIgnoreCase("oracle")))) { 539 logger.error( 540 "SQLException caught during insert" 541 + e1.getErrorCode(), 542 e1); 543 throw new JenaException("Exception during database access", e1 ); 544 } 545 } 546 } 547 548 565 public void storeTripleList(List triples, IDBID my_GID) { 566 571 Triple t; 573 String cmd; 574 boolean autoState = false; 575 DriverRDB drvr = (DriverRDB) m_driver; 576 autoState = drvr.xactOp(DriverRDB.xactAutoOff); 578 579 Iterator it = triples.iterator(); 580 581 while (it.hasNext()) { 582 t = (Triple) it.next(); 583 storeTriple(t, my_GID, false, null); 585 } 586 596 if (autoState) 597 drvr.xactOp(DriverRDB.xactCommit); 598 599 ArrayList c = new ArrayList(triples); 601 triples.removeAll(c); 602 603 623 if (autoState) 624 drvr.xactOp(DriverRDB.xactAutoOn); 625 } 626 627 644 public void deleteTripleList(List triples, IDBID my_GID) { 645 650 651 Triple t; 653 String cmd; 654 boolean autoState = false; 655 DriverRDB drvr = (DriverRDB) m_driver; 656 autoState = drvr.xactOp(DriverRDB.xactAutoOff); 658 Iterator it = triples.iterator(); 659 660 while (it.hasNext()) { 661 t = (Triple) it.next(); 662 deleteTriple(t, my_GID, false, null); 664 } 665 666 676 677 if (autoState) 678 drvr.xactOp(DriverRDB.xactCommit); 679 680 ArrayList c = new ArrayList(triples); 682 triples.removeAll(c); 683 684 704 if (autoState) 705 drvr.xactOp(DriverRDB.xactAutoOn); 706 } 707 708 713 public int tripleCount() { 714 return(rowCount(getTblName())); 715 } 716 717 718 724 public boolean statementTableContains(IDBID graphID, Triple t) { 725 ExtendedIterator it = find( t, graphID ); 726 boolean res = it.hasNext(); 727 it.close(); 728 return res; 729 } 730 731 734 public ExtendedIterator find(TripleMatch t, IDBID graphID) { 735 String astName = getTblName(); 736 Node subj_node = t.getMatchSubject(); 737 Node pred_node = t.getMatchPredicate(); 738 Node obj_node = t.getMatchObject(); 739 Node_Literal objLit; 740 int gid = ((DBIDInt) graphID).getIntID(); 742 boolean notFound = false; 743 int hack = 0; 744 745 ResultSetTripleIterator result = 746 new ResultSetTripleIterator(this, graphID); 747 748 PreparedStatement ps = null; 749 750 String subj = null; 751 String pred = null; 752 String obj = null; 753 String op = "selectStatement"; 754 String qual = ""; 755 int args = 1; 756 if ( hack != 0 ) { 757 subj_node = pred_node = obj_node = null; 758 } 759 if (subj_node != null) { 760 subj = m_driver.nodeToRDBString(subj_node, false); 761 if (subj == null) 762 notFound = true; 763 else 764 qual += "S"; 765 } 766 if (pred_node != null) { 767 pred = m_driver.nodeToRDBString(pred_node, false); 768 if (pred == null) 769 notFound = true; 770 else 771 qual += "P"; 772 } 773 if (obj_node != null) { 774 obj = m_driver.nodeToRDBString(obj_node, false); 775 if (obj == null) 776 notFound = true; 777 else 778 qual += "O"; 779 } 780 if (notFound == false) 781 try { 782 op += qual; 783 797 ps = m_sql.getPreparedSQLStatement(op, getTblName()); 798 if (obj != null) 799 ps.setString(args++, obj); 800 if (subj != null) 801 ps.setString(args++, subj); 802 if (pred != null) 803 ps.setString(args++, pred); 804 805 ps.setInt(args++, gid); 806 m_sql.executeSQL(ps, op, result); 808 809 } catch (Exception e) { 811 notFound = true; 812 logger.debug( "find encountered exception: args=" + args + " err: ", e); 813 throw new JenaException("Exception during database access", e); } 815 816 if ( notFound ) result.close(); 817 return (new TripleMatchIterator(t.asTriple(), result)); 818 } 819 820 823 public void removeStatementsFromDB(IDBID graphID) { 824 String gid = graphID.getID().toString(); 825 826 try { 827 PreparedStatement ps = m_sql.getPreparedSQLStatement("removeRowsFromTable",getTblName()); 828 ps.clearParameters(); 829 830 ps.setString(1,gid); 831 ps.executeUpdate(); 832 } catch (SQLException e) { 833 logger.error("Problem removing statements from table: ", e); 834 throw new JenaException("Exception during database access", e); } 836 } 837 } 838 839 865 866 867 | Popular Tags |