1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
|
/*
* $Id$
*
* This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
* project.
*
* Copyright (C) 1998-2018 OpenLink Software
*
* This project is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; only version 2 of the License, dated June 1991.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
package virtuoso.rdf4j.driver;
import org.eclipse.rdf4j.model.ValueFactory;
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
import org.eclipse.rdf4j.repository.Repository;
import org.eclipse.rdf4j.repository.RepositoryConnection;
import org.eclipse.rdf4j.repository.RepositoryException;
import virtuoso.jdbc4.VirtuosoConnectionPoolDataSource;
import javax.sql.ConnectionPoolDataSource;
import javax.sql.DataSource;
import javax.sql.XADataSource;
import java.io.File;
import java.sql.DriverManager;
/**
* A Sesame repository that contains RDF data that can be queried and updated.
* Access to the repository can be acquired by openening a connection to it.
* This connection can then be used to query and/or update the contents of the
* repository. Depending on the implementation of the repository, it may or may
* not support multiple concurrent connections.
* <p>
* Please note that a repository needs to be initialized before it can be used
* and that it should be shut down before it is discarded/garbage collected.
* Forgetting the latter can result in loss of data (depending on the Repository
* implementation)!
*
*/
public class VirtuosoRepository implements Repository {
public static final int CONCUR_DEFAULT = 0;
public static final int CONCUR_PESSIMISTIC = 1;
public static final int CONCUR_OPTIMISTIC = 2;
ValueFactory valueFactory = SimpleValueFactory.getInstance();
File dataDir;
private VirtuosoConnectionPoolDataSource pds = new VirtuosoConnectionPoolDataSource();
private String url_hostlist;
private String user;
private String password;
private int resultsHandlerType = 0;
private boolean roundrobin;
private String charset = "UTF-8";
static final String utf8 = "charset=utf-8";
private boolean initialized = false;
private DataSource _ds;
private ConnectionPoolDataSource _pds;
private XADataSource _xads;
boolean useLazyAdd = true;
boolean insertBNodeAsVirtuosoIRI = false;
boolean insertStringLiteralAsSimple = false;
String defGraph;
int prefetchSize = 100;
int batchSize = 5000;
int queryTimeout = 0;
String ruleSet;
String macroLib;
int concurencyMode = CONCUR_DEFAULT;
boolean useDefGraphForQueries = false;
public VirtuosoRepository(ConnectionPoolDataSource ds, String defGraph, boolean useLazyAdd) {
super();
this.defGraph = defGraph;
this.useLazyAdd = useLazyAdd;
this._pds = ds;
}
public VirtuosoRepository(DataSource ds, String defGraph, boolean useLazyAdd) {
super();
this.defGraph = defGraph;
this.useLazyAdd = useLazyAdd;
this._ds = ds;
}
public VirtuosoRepository(XADataSource ds, String defGraph) {
super();
this.defGraph = defGraph;
this.useLazyAdd = false;
this._xads = ds;
}
/**
* Construct a VirtuosoRepository with a specified parameters
*
* @param url_hostlist
* the Virtuoso JDBC URL connection string or hostlist for poolled connection.
* Virtuoso database hostlist
* <pre>
* "hostone:1112,hosttwo:1113"
* or "hostone,hosttwo" if default port=1111 is used on hosts
* </pre>
* @param user
* the database user on whose behalf the connection is being made
* @param password
* the user's password
* @param defGraph
* a default Graph name, used for Sesame calls, when contexts list
* is empty, exclude <tt>exportStatements, hasStatement, getStatements</tt> methods
* @param useLazyAdd
* set <tt>true</tt> to enable using batch optimization for sequence of
* <pre>
* add(Resource subject, URI predicate, Value object, Resource... contexts);
* add(Statement statement, Resource... contexts);
* </pre>
* methods, when autoCommit mode is off. The triples will be sent to DBMS on commit call
* or when batch size become more than predefined batch max_size.
* LazyAdd will be set false for XADataSource connection
*
*/
public VirtuosoRepository(String url_hostlist, String user, String password, String defGraph, boolean useLazyAdd) {
super();
this.url_hostlist = url_hostlist.trim();
this.user = user;
this.password = password;
this.defGraph = defGraph;
this.useLazyAdd = useLazyAdd;
}
/**
* Construct a VirtuosoRepository with a specified parameters
* <tt>defGraph</tt> will be set to <tt>"sesame:nil"</tt>.
*
* @param url_hostlist
* the Virtuoso JDBC URL connection string or hostlist for poolled connection.
* Virtuoso database hostlist
* <pre>
* "hostone:1112,hosttwo:1113"
* or "hostone,hosttwo" if default port=1111 is used on hosts
* </pre>
* @param user
* the database user on whose behalf the connection is being made
* @param password
* the user's password
* @param useLazyAdd
* set <tt>true</tt> to enable using batch optimization for sequence of
* <pre>
* add(Resource subject, URI predicate, Value object, Resource... contexts);
* add(Statement statement, Resource... contexts);
* </pre>
* methods, when autoCommit mode is off. The triples will be sent to DBMS on commit call
* or when batch size become more than predefined batch max_size.
* LazyAdd will be set false for XADataSource connection
*
*/
public VirtuosoRepository(String url_hostlist, String user, String password, boolean useLazyAdd) {
this(url_hostlist, user, password, "sesame:nil", useLazyAdd);
}
/**
* Construct a VirtuosoRepository with a specified parameters.
* useLazyAdd will be set to <tt>false</tt>.
*
* @param url_hostlist
* the Virtuoso JDBC URL connection string or hostlist for poolled connection.
* Virtuoso database hostlist
* <pre>
* "hostone:1112,hosttwo:1113"
* or "hostone,hosttwo" if the default port=1111 is used for hosts
* </pre>
* @param user
* the database user on whose behalf the connection is being made
* @param password
* the user's password
* @param defGraph
* a default Graph name, used for Sesame calls, when contexts list
* is empty, exclude <tt>exportStatements, hasStatement, getStatements</tt> methods
*
*/
public VirtuosoRepository(String url_hostlist, String user, String password, String defGraph) {
this(url_hostlist, user, password, defGraph, true);
}
/**
* Construct a VirtuosoRepository with a specified parameters.
* <tt>useLazyAdd</tt> will be set to <tt>false</tt>.
* <tt>defGraph</tt> will be set to <tt>"sesame:nil"</tt>.
*
* @param url_hostlist
* the Virtuoso JDBC URL connection string or hostlist for poolled connection.
* Virtuoso database hostlist
* <pre>
* "hostone:1112,hosttwo:1113"
* or "hostone,hosttwo" if the default port=1111 is used for hosts
* </pre>
* @param user
* the database user on whose behalf the connection is being made
* @param password
* the user's password
*/
public VirtuosoRepository(String url_hostlist, String user, String password) {
this(url_hostlist, user, password, true);
}
/**
* Opens a connection to this repository that can be used for querying and
* updating the contents of the repository. Created connections need to be
* closed to make sure that any resources they keep hold of are released. The
* best way to do this is to use a try-finally-block as follows:
*
* <pre>
* Connection con = repository.getConnection();
* try {
* // perform operations on the connection
* }
* finally {
* con.close();
* }
* </pre>
*
* @return A connection that allows operations on this repository.
* @throws RepositoryException
* If something went wrong during the creation of the Connection.
*/
public RepositoryConnection getConnection() throws RepositoryException {
if (_xads != null) {
try {
javax.sql.XAConnection xconn = _xads.getXAConnection();
java.sql.Connection connection = xconn.getConnection();
this.useLazyAdd = false;
return new VirtuosoRepositoryConnection(this, connection);
}
catch (Exception e) {
System.out.println("Connection has FAILED.");
throw new RepositoryException(e);
}
}
else if (_pds != null) {
try {
javax.sql.PooledConnection pconn = _pds.getPooledConnection();
java.sql.Connection connection = pconn.getConnection();
return new VirtuosoRepositoryConnection(this, connection);
}
catch (Exception e) {
System.out.println("Connection has FAILED.");
throw new RepositoryException(e);
}
}
else if (_ds != null) {
try {
java.sql.Connection connection = _ds.getConnection();
return new VirtuosoRepositoryConnection(this, connection);
}
catch (Exception e) {
System.out.println("Connection has FAILED.");
throw new RepositoryException(e);
}
}
else if (url_hostlist.startsWith("jdbc:virtuoso://")) {
try {
Class.forName("virtuoso.jdbc4.Driver");
String url = url_hostlist;
if (url.toLowerCase().indexOf(utf8) == -1) {
if (url.charAt(url.length()-1) != '/')
url = url + "/charset=UTF-8";
else
url = url + "charset=UTF-8";
}
if (roundrobin && url.toLowerCase().indexOf("roundrobin=") == -1) {
if (url.charAt(url.length()-1) != '/')
url = url + "/roundrobin=1";
else
url = url + "roundrobin=1";
}
if (url.toLowerCase().indexOf("log_enable=") == -1) {
if (url.charAt(url.length()-1) != '/')
url = url + "/log_enable=1";
else
url = url + "log_enable=1";
}
java.sql.Connection connection = DriverManager.getConnection(url, user, password);
return new VirtuosoRepositoryConnection(this, connection);
}
catch (Exception e) {
System.out.println("Connection to " + url_hostlist + " has FAILED.");
throw new RepositoryException(e);
}
}
else {
try {
pds.setServerName(url_hostlist);
pds.setUser(user);
pds.setPassword(password);
pds.setCharset(charset);
pds.setRoundrobin(roundrobin);
javax.sql.PooledConnection pconn = pds.getPooledConnection();
java.sql.Connection connection = pconn.getConnection();
return new VirtuosoRepositoryConnection(this, connection);
}
catch (Exception e) {
System.out.println("Connection to " + url_hostlist + " has FAILED.");
throw new RepositoryException(e);
}
}
}
/**
* Set the buffer fetch size(default 100)
*
* @param sz
* buffer fetch size.
*/
public void setFetchSize(int sz) {
this.prefetchSize = sz;
}
/**
* Get the buffer fetch size
*
* @return buffer fetch size
*/
public int getFetchSize() {
return this.prefetchSize;
}
/**
* Set the batch size for Inserts data(default 5000)
*
* @param sz
* batch size.
*/
public void setBatchSize(int sz) {
this.batchSize = sz;
}
/**
* Get the batch size for Insert data
*
* @return batch size for Insert data
*/
public int getBatchSize() {
return this.batchSize;
}
/**
* Set the query timeout(default 0)
*
* @param seconds
* queryTimeout seconds, 0 - unlimited.
*/
public void setQueryTimeout(int seconds) {
this.queryTimeout = seconds;
}
/**
* Get the query timeout seconds
*
* @return query timeout
*/
public int getQueryTimeout() {
return this.queryTimeout;
}
/**
* Set the UseLazyAdd state for connection(default true)
* for XADataSource connection set false and can't be changed
* @param v
* true - useLazyAdd
*/
public void setUseLazyAdd(boolean v) {
this.useLazyAdd = v;
}
/**
* Get the UseLazyAdd state for connection
*
* @return useLazyAdd state
*/
public boolean getUseLazyAdd() {
return this.useLazyAdd;
}
/**
* Set the RoundRobin state for connection(default false)
*
* @param v
* true - use roundrobin
*/
public void setRoundrobin(boolean v) {
this.roundrobin = v;
}
/**
* Get the RoundRobin state for connection
*
* @return roundrobin state
*/
public boolean getRoundrobin() {
return this.roundrobin;
}
/**
* Set the insertBNodeAsURI state for connection(default false)
*
* @param v
* true - insert BNode as Virtuoso IRI
* false - insert BNode as Virtuoso Native BNode
*/
public void setInsertBNodeAsVirtuosoIRI(boolean v) {
this.insertBNodeAsVirtuosoIRI = v;
}
/**
* Get the insertBNodeAsURI state for connection
*
* @return insertBNodeAsURI state
*/
public boolean getInsertBNodeAsVirtuosoIRI() {
return this.insertBNodeAsVirtuosoIRI;
}
/**
* Get the insertStringLiteralAsSimple state for connection
*
* @return insertStringLiteralAsSimple state
*/
public boolean getInsertStringLiteralAsSimple() {
return this.insertStringLiteralAsSimple;
}
/**
* Set the insertStringLiteralAsSimple state for connection(default false)
*
* @param v
* true - insert String Literals as Simple Literals
* false - insert String Literals as is
*/
public void setInsertStringLiteralAsSimple(boolean v) {
this.insertStringLiteralAsSimple = v;
}
/**
* Set inference RuleSet name
*
* @param name
* RuleSet name.
*/
public void setRuleSet(String name) {
if (name != null && name.equals("null"))
name = null;
this.ruleSet = name;
}
/**
* Get the inference RuleSet name
*
* @return ruleSet name
*/
public String getRuleSet() {
return this.ruleSet;
}
/**
* Set inference MacroLib name
*
* @param name
* macroLib name.
*/
public void setMacroLib(String name) {
if (name != null && name.equals("null"))
name = null;
this.macroLib = name;
}
/**
* Get the inference MacroLib name
*
* @return macroLib name
*/
public String getMacroLib() {
return this.macroLib;
}
/**
* Set the concurrency mode for Insert/Update/Delete operations and SPARUL queries
*
* @param mode
* Concurrency mode
*/
public void setConcurrencyMode(int mode) throws RepositoryException
{
if (mode != CONCUR_DEFAULT && mode != CONCUR_OPTIMISTIC && mode != CONCUR_PESSIMISTIC)
throw new IllegalArgumentException("Unsupported concurrency mode: "+mode);
this.concurencyMode = mode;
}
/**
* Get the concurrency mode for Insert/Update/Delete operations and SPARUL queries
*
* @return concurrency mode
*/
public int getConcurrencyMode() {
return this.concurencyMode;
}
/**
* Use repository defGraph with SPARQL queries, if query default graph wasn't set (default false)
* @param v
* true - useDefGraphForQueries
*/
public void setUseDefGraphForQueries(boolean v) {
this.useDefGraphForQueries = v;
}
/**
* Get the UseDefGraphForQueries state for connection
*/
public boolean getUseDefGraphForQueries() {
return this.useDefGraphForQueries;
}
/**
* Get the directory where data and logging for this repository is stored.
*
* @return the directory where data for this repository is stored.
*/
public File getDataDir() {
return this.dataDir;
}
/**
* Gets a ValueFactory for this Repository.
*
* @return A repository-specific ValueFactory.
*/
public ValueFactory getValueFactory() {
return this.valueFactory;
}
/**
* Initializes this repository. A repository needs to be initialized before
* it can be used.
*
* @throws RepositoryException
* If the initialization failed.
*/
public void initialize() throws RepositoryException {
initialized = true;
}
/**
* Indicates if the Repository has been initialized. Note that the
* initialization status may change if the Repository is shut down.
*
* @return true iff the repository has been initialized.
*/
public boolean isInitialized() {
return initialized;
}
/**
* Checks whether this repository is writable, i.e. if the data contained in
* this repository can be changed. The writability of the repository is
* determined by the writability of the Sail that this repository operates
* on.
*/
public boolean isWritable() throws RepositoryException {
if (!initialized) {
throw new IllegalStateException("VirtuosoRepository not initialized.");
}
return true; // user login has authenticated this connection
}
/**
* Set the directory where data and logging for this repository is stored.
*
* @param dataDir
* the directory where data for this repository is stored
*/
public void setDataDir(File dataDir) {
this.dataDir = dataDir;
}
/**
* Shuts the repository down, releasing any resources that it keeps hold of.
* Once shut down, the repository can no longer be used until it is
* re-initialized.
*/
public void shutDown() throws RepositoryException {
initialized = false;
}
/***
public int getResultsHandlerType() {
return resultsHandlerType;
}
public void setResultsHandlerType(int handlerType) {
this.resultsHandlerType = handlerType;
}
***/
/**
* Create ruleSet in DBMS
*
* @param ruleSetName
* @param uriGraphRuleSet
*
* @throws RepositoryException
* If something went wrong during the creation of the Connection.
*/
public void createRuleSet(String ruleSetName, String uriGraphRuleSet) throws RepositoryException
{
java.sql.Connection con = ((VirtuosoRepositoryConnection)getConnection()).getQuadStoreConnection();
try {
java.sql.Statement st = con.createStatement();
st.execute("rdfs_rule_set('"+ruleSetName+"', '"+uriGraphRuleSet+"')");
st.close();
} catch (Exception e) {
throw new RepositoryException(e);
}
}
}
|