Java Code Examples for org.eclipse.rdf4j.repository.sail.SailRepository#getConnection()

The following examples show how to use org.eclipse.rdf4j.repository.sail.SailRepository#getConnection() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TargetNodeMinCountEdgeCaseTests.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test(expected = ShaclSailValidationException.class)
public void testMinCountWithInvalidInitialDataset2() throws Throwable {

	SailRepository sailRepository = new SailRepository(new ShaclSail(new MemoryStore()));

	try (SailRepositoryConnection connection = sailRepository.getConnection()) {
		connection.begin();
		connection.add(validPerson1, ssn, value1);
		connection.add(validPerson1, ssn, value2);
		connection.commit();

		connection.begin();
		connection.add(new StringReader(shaclShapes), "", RDFFormat.TURTLE, RDF4J.SHACL_SHAPE_GRAPH);
		connection.commit();
	} catch (Exception e) {
		throw e.getCause();
	}

}
 
Example 2
Source File: InferenceTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void initiallyInferredStatementsTest() {
	ElasticsearchStore elasticsearchStore = new ElasticsearchStore(singletonClientProvider, "testindex");

	SailRepository sailRepository = new SailRepository(new SchemaCachingRDFSInferencer(elasticsearchStore));

	try (SailRepositoryConnection connection = sailRepository.getConnection()) {
		long explicitStatements = connection.getStatements(null, null, null, false).stream().count();
		assertEquals(0, explicitStatements);

		long inferredStatements = connection.getStatements(null, null, null, true).stream().count();
		assertEquals(141, inferredStatements);
	}

	sailRepository.shutDown();
}
 
Example 3
Source File: UniqueLangBenchmarkEmpty.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Benchmark
public void shacl() throws Exception {

	SailRepository repository = new SailRepository(Utils.getInitializedShaclSail("shaclUniqueLang.ttl"));

	try (SailRepositoryConnection connection = repository.getConnection()) {
		for (List<Statement> statements : allStatements) {
			connection.begin(IsolationLevels.SNAPSHOT);
			connection.add(statements);
			connection.commit();
		}
	}

	repository.shutDown();

}
 
Example 4
Source File: InitBenchmark.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Benchmark
public void initWithElasticsearchClientCreation() {

	SailRepository elasticsearchStore = new SailRepository(
			new ElasticsearchStore("localhost", embeddedElastic.getTransportTcpPort(), "cluster1", "testindex",
					false));

	try (SailRepositoryConnection connection = elasticsearchStore.getConnection()) {
		connection.begin(IsolationLevels.NONE);
		connection.clear();
		connection.commit();
	}

	elasticsearchStore.shutDown();

}
 
Example 5
Source File: ElasticsearchStoreTransactionsTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testRollbackClearSimple() {
	SailRepository elasticsearchStore = new SailRepository(this.elasticsearchStore);
	try (SailRepositoryConnection connection = elasticsearchStore.getConnection()) {

		BNode context = vf.createBNode();

		connection.begin(IsolationLevels.READ_COMMITTED);
		connection.add(RDF.TYPE, RDF.TYPE, RDFS.RESOURCE);
		connection.add(RDF.TYPE, RDF.TYPE, RDF.PROPERTY, context);
		connection.commit();

		connection.begin();
		connection.clear();
		assertEquals(0, connection.size());
		connection.rollback();

		assertEquals(2, connection.size());

	}

}
 
Example 6
Source File: TargetNodeMinCountEdgeCaseTests.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testMinCountWithValidInitialDataset() throws Throwable {

	SailRepository sailRepository = new SailRepository(new ShaclSail(new MemoryStore()));

	try (SailRepositoryConnection connection = sailRepository.getConnection()) {
		connection.begin();
		connection.add(validPerson1, ssn, value1);
		connection.add(validPerson1, ssn, value2);
		connection.add(validPerson2, ssn, value1);
		connection.add(validPerson2, ssn, value2);
		connection.commit();

		connection.begin();
		connection.add(new StringReader(shaclShapes), "", RDFFormat.TURTLE, RDF4J.SHACL_SHAPE_GRAPH);
		connection.commit();
	} catch (Exception e) {
		throw e.getCause();
	}

}
 
Example 7
Source File: BasicBenchmarks.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Benchmark
public void addRemoveSingleTransaction() {
	SailRepository spinSail = new SailRepository(new SpinSail(new MemoryStore()));
	spinSail.init();

	try (SailRepositoryConnection connection = spinSail.getConnection()) {
		connection.begin();
		connection.add(bob, name, nameBob);
		connection.add(alice, name, nameAlice);

		connection.remove(bob, name, nameBob);

		connection.remove(alice, null, null);
		connection.commit();

	}
	spinSail.shutDown();

}
 
Example 8
Source File: BasicBenchmarks.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Benchmark
public void remove() {
	SailRepository spinSail = new SailRepository(new SpinSail(new MemoryStore()));
	spinSail.init();

	try (SailRepositoryConnection connection = spinSail.getConnection()) {
		connection.remove(bob, name, nameBob);
		connection.remove(alice, null, null);
	}

	spinSail.shutDown();

}
 
Example 9
Source File: Utils.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void loadShapeData(SailRepository repo, String resourceName) throws IOException {
	((ShaclSail) repo.getSail()).disableValidation();

	try (InputStream shapesData = Utils.class.getClassLoader().getResourceAsStream(resourceName)) {

		try (RepositoryConnection conn = repo.getConnection()) {
			conn.begin(IsolationLevels.NONE);
			conn.add(shapesData, "", RDFFormat.TURTLE, RDF4J.SHACL_SHAPE_GRAPH);
			conn.commit();
		}
	}
	((ShaclSail) repo.getSail()).enableValidation();
}
 
Example 10
Source File: FederationQueryTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Repository createMember(String memberID) throws RepositoryException, RDFParseException, IOException {
	SailRepository member = new SailRepository(new MemoryStore());
	member.initialize();
	try (SailRepositoryConnection con = member.getConnection()) {
		String resource = "testcases/federation-member-" + memberID + ".ttl";
		con.add(classLoader.getResource(resource), "", RDFFormat.TURTLE);
		reference.add(classLoader.getResource(resource), "", RDFFormat.TURTLE);
	}
	return member;
}
 
Example 11
Source File: TrackAddedStatementsTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testValidationFailedCausesRollback() throws Exception {

	SailRepository shaclRepository = Utils.getInitializedShaclRepository("shacl.ttl", false);
	((ShaclSail) shaclRepository.getSail()).setIgnoreNoShapesLoadedException(true);
	shaclRepository.init();

	try (SailRepositoryConnection connection = shaclRepository.getConnection()) {

		connection.begin();

		connection.add(RDFS.RESOURCE, RDF.TYPE, RDFS.RESOURCE);

		try {
			connection.commit();
			fail("commit should have failed");
		} catch (RepositoryException e) {
			// do nothing, expected
		}

	}

	try (SailRepositoryConnection connection = shaclRepository.getConnection()) {
		assertEquals(0, size(connection));
	}

}
 
Example 12
Source File: ElasticsearchStoreWALTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void failedTransactionAdd(int count) {
	ClientProviderWithDebugStats clientProvider = new ClientProviderWithDebugStats("localhost",
			embeddedElastic.getTransportTcpPort(), "cluster1");

	ElasticsearchStore es = new ElasticsearchStore(clientProvider, "testindex");
	SailRepository elasticsearchStore = new SailRepository(es);

	es.setElasticsearchBulkSize(1024);

	try (SailRepositoryConnection connection = elasticsearchStore.getConnection()) {

		connection.begin(IsolationLevels.READ_COMMITTED);
		for (int i = 0; i < count; i++) {
			connection.add(RDFS.RESOURCE, RDFS.LABEL, connection.getValueFactory().createLiteral(i));
		}

		Thread thread = new Thread(() -> {
			try {
				while (clientProvider.getBulkCalls() < 3) {
					Thread.sleep(1);
				}
				clientProvider.close();
			} catch (Exception ignored) {

			}
		});
		thread.start();

		connection.commit();

	}

}
 
Example 13
Source File: SpinSailWithoutRDFSInferencerTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Before
public void setup() throws RepositoryException {
	NotifyingSail baseSail = new MemoryStore();
	SpinSail spinSail = new SpinSail(baseSail);
	repo = new SailRepository(spinSail);
	repo.initialize();
	conn = repo.getConnection();
}
 
Example 14
Source File: ModifyShapesPostInitTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test()
public void checkForNoExceptionWithEmptyTransaction() {

	SailRepository sailRepository = new SailRepository(new ShaclSail(new MemoryStore()));
	sailRepository.init();

	try (SailRepositoryConnection connection = sailRepository.getConnection()) {
		connection.begin();
		connection.commit();
	}

}
 
Example 15
Source File: ElasticsearchStoreWALTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void failedTransactionRemove() {
	ClientProviderWithDebugStats clientProvider = new ClientProviderWithDebugStats("localhost",
			embeddedElastic.getTransportTcpPort(), "cluster1");

	ElasticsearchStore es = new ElasticsearchStore(clientProvider, "testindex");
	SailRepository elasticsearchStore = new SailRepository(es);

	es.setElasticsearchBulkSize(1024);

	try (SailRepositoryConnection connection = elasticsearchStore.getConnection()) {

		connection.begin(IsolationLevels.READ_COMMITTED);

		connection.clear();

		long bulkCalls = clientProvider.getBulkCalls();
		Thread thread = new Thread(() -> {
			try {
				while (clientProvider.getBulkCalls() < bulkCalls + 3) {
					Thread.sleep(1);
				}
				clientProvider.close();
			} catch (Exception ignored) {

			}
		});
		thread.start();

		connection.commit();

	}

}
 
Example 16
Source File: ElasticsearchStoreWALTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Ignore // No WAL implemented yet
@Test
public void testRemoveLargeDataset() {

	int count = 100000;

	fill(count);

	boolean transactionFaild = false;
	try {
		failedTransactionRemove();
	} catch (Exception e) {
		System.out.println(e.getClass().getName());
		transactionFaild = true;
	}

	assertTrue(transactionFaild);

	SailRepository elasticsearchStore = new SailRepository(
			new ElasticsearchStore("localhost", embeddedElastic.getTransportTcpPort(), "cluster1", "testindex"));

	try (SailRepositoryConnection connection = elasticsearchStore.getConnection()) {

		long size = connection.size();
		System.out.println(size);
		assertEquals("Since transaction failed there should be no statements in the store", count, size);

	}

}
 
Example 17
Source File: SplSailTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Before
public void setup() throws RepositoryException {
	NotifyingSail baseSail = new MemoryStore();
	DedupingInferencer deduper = new DedupingInferencer(baseSail);
	NotifyingSail rdfsInferencer = new SchemaCachingRDFSInferencer(deduper, false);
	SpinSail spinSail = new SpinSail(rdfsInferencer);
	repo = new SailRepository(spinSail);
	repo.initialize();
	conn = repo.getConnection();
}
 
Example 18
Source File: RdfCloudTripleStoreConnectionTest.java    From rya with Apache License 2.0 4 votes vote down vote up
public void testUpdateWAuthOnConfig() throws Exception {
        String sparqlUpdate = getSparqlUpdate();

        RdfCloudTripleStore tstore = new MockRdfCloudStore();
        NamespaceManager nm = new NamespaceManager(tstore.getRyaDAO(), tstore.getConf());
        tstore.setNamespaceManager(nm);
        SailRepository repo = new SailRepository(tstore);
        tstore.getRyaDAO().getConf().setCv("1|2");
        repo.initialize();

        RepositoryConnection conn = repo.getConnection();
        Update u = conn.prepareUpdate(QueryLanguage.SPARQL, sparqlUpdate);
        u.execute();

        String query =
                "PREFIX  ex:  <http://www.example.org/exampleDocument#>\n" +
                "PREFIX  voc:  <http://www.example.org/vocabulary#>\n" +
                "PREFIX  foaf:  <http://xmlns.com/foaf/0.1/>\n" +
                "PREFIX  rdfs:  <http://www.w3.org/2000/01/rdf-schema#>\n" +
                "\n" +
                "SELECT * \n" +
//              "FROM NAMED <http://www.example.org/exampleDocument#G1>\n" +
                "WHERE\n" +
                "{\n" +
                "  GRAPH ex:G1\n" +
                "  {\n" +
                "    ?m voc:name ?name ;\n" +
                "           voc:homepage ?hp .\n" +
                "  } .\n" +
                " GRAPH ex:G2\n" +
                "  {\n" +
                "    ?m voc:hasSkill ?skill .\n" +
                "  } .\n" +
                "}";
        TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
        tupleQuery.setBinding(RdfCloudTripleStoreConfiguration.CONF_QUERY_AUTH, VF.createLiteral("2"));
        CountTupleHandler tupleHandler = new CountTupleHandler();
        tupleQuery.evaluate(tupleHandler);
        assertEquals(1, tupleHandler.getCount());

        tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query); //no auth
        tupleHandler = new CountTupleHandler();
        tupleQuery.evaluate(tupleHandler);
        assertEquals(0, tupleHandler.getCount());

        conn.close();

        repo.shutDown();
    }
 
Example 19
Source File: TrackAddedStatementsTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testCleanup() throws Exception {

	SailRepository shaclRepository = Utils.getInitializedShaclRepository("empty.ttl", false);
	((ShaclSail) shaclRepository.getSail()).setIgnoreNoShapesLoadedException(true);
	shaclRepository.init();

	try (SailRepositoryConnection connection = shaclRepository.getConnection()) {

		connection.add(RDFS.RESOURCE, RDF.TYPE, RDFS.RESOURCE);

		ShaclSailConnection shaclSailConnection = (ShaclSailConnection) connection.getSailConnection();

		assertNull(shaclSailConnection.addedStatements);
		assertNull(shaclSailConnection.removedStatements);
	}

}
 
Example 20
Source File: SerializableTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
	public void testMaxCountSnapshot() throws IOException, InterruptedException {
		for (int i = 0; i < 10; i++) {
			SailRepository repo = Utils.getInitializedShaclRepository("shaclMax.ttl", false);

			Sail sail = repo.getSail();
//			((ShaclSail) sail).setGlobalLogValidationExecution(true);

			multithreadedMaxCountViolation(IsolationLevels.SNAPSHOT, repo);

			try (SailRepositoryConnection connection = repo.getConnection()) {
				connection.begin();

				ValidationReport revalidate = ((ShaclSailConnection) connection.getSailConnection()).revalidate();
				Rio.write(revalidate.asModel(), System.out, RDFFormat.TURTLE);

				assertTrue(revalidate.conforms());

				connection.commit();
			}

			repo.shutDown();
		}

	}