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

The following examples show how to use org.eclipse.rdf4j.sail.Sail#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: HalyardStatsBasedStatementPatternCardinalityCalculator.java    From Halyard with Apache License 2.0 4 votes vote down vote up
public HalyardStatsBasedStatementPatternCardinalityCalculator(Sail sail) {
	this.valueFactory = sail.getValueFactory();
    this.statsConnection = sail.getConnection();
}
 
Example 2
Source File: RyaSinkTaskTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void singleRecord() {
    // Create the Statements that will be put by the task.
    final ValueFactory vf = SimpleValueFactory.getInstance();
    final Set<Statement> statements = Sets.newHashSet(
            vf.createStatement(
                    vf.createIRI("urn:Alice"),
                    vf.createIRI("urn:WorksAt"),
                    vf.createIRI("urn:Taco Shop"),
                    vf.createIRI("urn:graph1")),
            vf.createStatement(
                    vf.createIRI("urn:Bob"),
                    vf.createIRI("urn:TalksTo"),
                    vf.createIRI("urn:Charlie"),
                    vf.createIRI("urn:graph2")),
            vf.createStatement(
                    vf.createIRI("urn:Eve"),
                    vf.createIRI("urn:ListensTo"),
                    vf.createIRI("urn:Alice"),
                    vf.createIRI("urn:graph1")));

    // Create the task that will be tested.
    final InMemoryRyaSinkTask task = new InMemoryRyaSinkTask();

    // Setup the properties that will be used to configure the task. We don't actually need to set anything
    // here since we're always returning true for ryaInstanceExists(...) and use an in memory RDF store.
    final Map<String, String> props = new HashMap<>();

    try {
        // Start the task.
        task.start(props);

        // Put the statements as a SinkRecord.
        task.put( Collections.singleton(new SinkRecord("topic", 1, null, "key", null, statements, 0)) );

        // Flush the statements.
        task.flush(new HashMap<>());

        // Fetch the stored Statements to show they match the original set.
        final Set<Statement> fetched = new HashSet<>();

        final Sail sail = task.makeSail(props);
        try(SailConnection conn = sail.getConnection();
                CloseableIteration<? extends Statement, SailException> it = conn.getStatements(null, null, null, false)) {
            while(it.hasNext()) {
                fetched.add( it.next() );
            }
        }

        assertEquals(statements, fetched);

    } finally {
        // Stop the task.
        task.stop();
    }
}
 
Example 3
Source File: RyaSinkTaskTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void multipleRecords() {
    // Create the Statements that will be put by the task.
    final ValueFactory vf = SimpleValueFactory.getInstance();
    final Set<Statement> batch1 = Sets.newHashSet(
            vf.createStatement(
                    vf.createIRI("urn:Alice"),
                    vf.createIRI("urn:WorksAt"),
                    vf.createIRI("urn:Taco Shop"),
                    vf.createIRI("urn:graph1")),
            vf.createStatement(
                    vf.createIRI("urn:Bob"),
                    vf.createIRI("urn:TalksTo"),
                    vf.createIRI("urn:Charlie"),
                    vf.createIRI("urn:graph2")));

    final Set<Statement> batch2 = Sets.newHashSet(
            vf.createStatement(
                    vf.createIRI("urn:Eve"),
                    vf.createIRI("urn:ListensTo"),
                    vf.createIRI("urn:Alice"),
                    vf.createIRI("urn:graph1")));

    // Create the task that will be tested.
    final InMemoryRyaSinkTask task = new InMemoryRyaSinkTask();

    // Setup the properties that will be used to configure the task. We don't actually need to set anything
    // here since we're always returning true for ryaInstanceExists(...) and use an in memory RDF store.
    final Map<String, String> props = new HashMap<>();

    try {
        // Start the task.
        task.start(props);

        // Put the statements as SinkRecords.
        final Collection<SinkRecord> records = Sets.newHashSet(
                new SinkRecord("topic", 1, null, "key", null, batch1, 0),
                new SinkRecord("topic", 1, null, "key", null, batch2, 1));
        task.put( records );

        // Flush the statements.
        task.flush(new HashMap<>());

        // Fetch the stored Statements to show they match the original set.
        final Set<Statement> fetched = new HashSet<>();

        final Sail sail = task.makeSail(props);
        try(SailConnection conn = sail.getConnection();
                CloseableIteration<? extends Statement, SailException> it = conn.getStatements(null, null, null, false)) {
            while(it.hasNext()) {
                fetched.add( it.next() );
            }
        }

        assertEquals(Sets.union(batch1, batch2), fetched);

    } finally {
        // Stop the task.
        task.stop();
    }
}
 
Example 4
Source File: RyaSinkTaskTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void flushBetweenPuts() {
    // Create the Statements that will be put by the task.
    final ValueFactory vf = SimpleValueFactory.getInstance();
    final Set<Statement> batch1 = Sets.newHashSet(
            vf.createStatement(
                    vf.createIRI("urn:Alice"),
                    vf.createIRI("urn:WorksAt"),
                    vf.createIRI("urn:Taco Shop"),
                    vf.createIRI("urn:graph1")),
            vf.createStatement(
                    vf.createIRI("urn:Bob"),
                    vf.createIRI("urn:TalksTo"),
                    vf.createIRI("urn:Charlie"),
                    vf.createIRI("urn:graph2")));

    final Set<Statement> batch2 = Sets.newHashSet(
            vf.createStatement(
                    vf.createIRI("urn:Eve"),
                    vf.createIRI("urn:ListensTo"),
                    vf.createIRI("urn:Alice"),
                    vf.createIRI("urn:graph1")));

    // Create the task that will be tested.
    final InMemoryRyaSinkTask task = new InMemoryRyaSinkTask();

    // Setup the properties that will be used to configure the task. We don't actually need to set anything
    // here since we're always returning true for ryaInstanceExists(...) and use an in memory RDF store.
    final Map<String, String> props = new HashMap<>();

    try {
        // Start the task.
        task.start(props);

        // Put the statements with flushes between them.
        task.put( Collections.singleton(new SinkRecord("topic", 1, null, "key", null, batch1, 0)) );
        task.flush(new HashMap<>());
        task.put( Collections.singleton(new SinkRecord("topic", 1, null, "key", null, batch2, 1)) );
        task.flush(new HashMap<>());

        // Fetch the stored Statements to show they match the original set.
        final Set<Statement> fetched = new HashSet<>();

        final Sail sail = task.makeSail(props);
        try(SailConnection conn = sail.getConnection();
                CloseableIteration<? extends Statement, SailException> it = conn.getStatements(null, null, null, false)) {
            while(it.hasNext()) {
                fetched.add( it.next() );
            }
        }

        assertEquals(Sets.union(batch1, batch2), fetched);

    } finally {
        // Stop the task.
        task.stop();
    }
}
 
Example 5
Source File: AccumuloAddUserIT.java    From rya with Apache License 2.0 4 votes vote down vote up
/**
 * Ensure a user that has not been added to the Rya instance can not interact with it.
 */
@Test
public void userNotAddedCanNotInsert() throws Exception {
    final String user = testInstance.createUniqueUser();
    final SecurityOperations secOps = super.getConnector().securityOperations();

    final RyaClient userAClient = AccumuloRyaClientFactory.build(
            new AccumuloConnectionDetails(ADMIN_USER, ADMIN_USER.toCharArray(), getInstanceName(), getZookeepers()),
            super.getClusterInstance().getCluster().getConnector(ADMIN_USER, ADMIN_USER));

    // Install the instance of Rya.
    userAClient.getInstall().install(getRyaInstanceName(), InstallConfiguration.builder().build());

    // Create the user that will not be added to the instance of Rya, but will try to scan it.
    secOps.createLocalUser(user, new PasswordToken(user));

    //Try to add a statement the Rya instance with the unauthorized user. This should fail.
    boolean securityExceptionThrown = false;

    Sail sail = null;
    SailConnection sailConn = null;
    try {
        final AccumuloRdfConfiguration userCConf = makeRyaConfig(getRyaInstanceName(), user, user, getInstanceName(), getZookeepers());
        sail = RyaSailFactory.getInstance(userCConf);
        sailConn = sail.getConnection();

        final ValueFactory vf = sail.getValueFactory();
        sailConn.addStatement(vf.createIRI("urn:Alice"), vf.createIRI("urn:talksTo"), vf.createIRI("urn:Bob"));

    } catch(final RuntimeException e) {
        final Throwable cause = e.getCause();
        if(cause instanceof AccumuloSecurityException) {
            securityExceptionThrown = true;
        }
    } finally {
        if(sailConn != null) {
            sailConn.close();
        }
        if(sail != null) {
            sail.shutDown();
        }
    }

    assertTrue(securityExceptionThrown);
}
 
Example 6
Source File: AccumuloAddUserIT.java    From rya with Apache License 2.0 4 votes vote down vote up
/**
 * Ensure a user that has been added to the Rya instance can interact with it.
 */
@Test
public void userAddedCanInsert() throws Exception {
    final String user = testInstance.createUniqueUser();
    final SecurityOperations secOps = super.getConnector().securityOperations();

    final RyaClient userAClient = AccumuloRyaClientFactory.build(
            new AccumuloConnectionDetails(ADMIN_USER, ADMIN_USER.toCharArray(), getInstanceName(), getZookeepers()),
            super.getClusterInstance().getCluster().getConnector(ADMIN_USER, ADMIN_USER));

    // Create the user that will not be added to the instance of Rya, but will try to scan it.
    secOps.createLocalUser(user, new PasswordToken(user));

    // Install the instance of Rya.
    userAClient.getInstall().install(getRyaInstanceName(), InstallConfiguration.builder().build());

    // Add the user.
    userAClient.getAddUser().get().addUser(getRyaInstanceName(), user);

    // Try to add a statement to the Rya instance. This should succeed.
    Sail sail = null;
    SailConnection sailConn = null;

    try {
        final AccumuloRdfConfiguration userDConf = makeRyaConfig(getRyaInstanceName(), user, user, getInstanceName(), getZookeepers());
        sail = RyaSailFactory.getInstance(userDConf);
        sailConn = sail.getConnection();

        final ValueFactory vf = sail.getValueFactory();
        sailConn.begin();
        sailConn.addStatement(vf.createIRI("urn:Alice"), vf.createIRI("urn:talksTo"), vf.createIRI("urn:Bob"));
        sailConn.close();

    } finally {
        if(sailConn != null) {
            sailConn.close();
        }
        if(sail != null) {
            sail.shutDown();
        }
    }
}
 
Example 7
Source File: AccumuloRemoveUserIT.java    From rya with Apache License 2.0 4 votes vote down vote up
/**
 * Ensure a user that has been removed from the Rya instance can not interact with it.
 */
@Test
public void removedUserCanNotInsert() throws Exception {
    final String adminUser = testInstance.createUniqueUser();
    final String user = testInstance.createUniqueUser();
    final SecurityOperations secOps = super.getConnector().securityOperations();

    // Create the user that will install the instance of Rya.
    secOps.createLocalUser(adminUser, new PasswordToken(adminUser));
    secOps.grantSystemPermission(adminUser, SystemPermission.CREATE_TABLE);

    final RyaClient userAClient = AccumuloRyaClientFactory.build(
            new AccumuloConnectionDetails(adminUser, adminUser.toCharArray(), getInstanceName(), getZookeepers()),
            super.getClusterInstance().getCluster().getConnector(adminUser, adminUser));

    // Create the user that will be added to the instance of Rya.
    secOps.createLocalUser(user, new PasswordToken(user));

    final RyaClient userCClient = AccumuloRyaClientFactory.build(
            new AccumuloConnectionDetails(user, user.toCharArray(), getInstanceName(), getZookeepers()),
            super.getClusterInstance().getCluster().getConnector(user, user));

    // Install the instance of Rya.
    userAClient.getInstall().install(getRyaInstanceName(), InstallConfiguration.builder().build());

    // Add userC.
    userAClient.getAddUser().get().addUser(getRyaInstanceName(), user);

    // Remove userA.
    userCClient.getRemoveUser().get().removeUser(getRyaInstanceName(), adminUser);

    // Show that userA can not insert anything.
    boolean securityExceptionThrown = false;

    Sail sail = null;
    SailConnection sailConn = null;
    try {
        final AccumuloRdfConfiguration userAConf = makeRyaConfig(getRyaInstanceName(), adminUser, adminUser, getInstanceName(), getZookeepers());
        sail = RyaSailFactory.getInstance(userAConf);
        sailConn = sail.getConnection();

        final ValueFactory vf = sail.getValueFactory();
        sailConn.addStatement(vf.createIRI("urn:Alice"), vf.createIRI("urn:talksTo"), vf.createIRI("urn:Bob"));

    } catch(final RuntimeException e) {
        final Throwable cause = e.getCause();
        if(cause instanceof AccumuloSecurityException) {
            securityExceptionThrown = true;
        }
    } finally {
        if(sailConn != null) {
            sailConn.close();
        }
        if(sail != null) {
            sail.shutDown();
        }
    }

    assertTrue(securityExceptionThrown);
}