Search in sources :

Example 61 with SailRepository

use of org.openrdf.repository.sail.SailRepository in project incubator-rya by apache.

the class RyaMongoDbSailFactoryTest method testCreateMongoDbSail.

@Test
public void testCreateMongoDbSail() throws Exception {
    Sail sail = null;
    SailRepository repo = null;
    SailRepositoryConnection conn = null;
    try {
        sail = RyaSailFactory.getInstance(conf);
        repo = new SailRepository(sail);
        conn = repo.getConnection();
    } finally {
        if (conn != null) {
            conn.close();
        }
        if (repo != null) {
            repo.shutDown();
        }
        if (sail != null) {
            sail.shutDown();
        }
    }
}
Also used : SailRepository(org.openrdf.repository.sail.SailRepository) Sail(org.openrdf.sail.Sail) SailRepositoryConnection(org.openrdf.repository.sail.SailRepositoryConnection) Test(org.junit.Test)

Example 62 with SailRepository

use of org.openrdf.repository.sail.SailRepository in project incubator-rya by apache.

the class RyaMongoDbSailFactoryTest method testAddStatement.

@Test
public void testAddStatement() throws Exception {
    Sail sail = null;
    SailRepository repo = null;
    SailRepositoryConnection conn = null;
    try {
        sail = RyaSailFactory.getInstance(conf);
        repo = new SailRepository(sail);
        conn = repo.getConnection();
        final ValueFactory vf = conn.getValueFactory();
        final Statement s = vf.createStatement(vf.createURI("u:a"), vf.createURI("u:b"), vf.createURI("u:c"));
        assertFalse(conn.hasStatement(s, false));
        conn.add(s);
        Assert.assertTrue(conn.hasStatement(s, false));
    } finally {
        if (conn != null) {
            conn.close();
        }
        if (repo != null) {
            repo.shutDown();
        }
        if (sail != null) {
            sail.shutDown();
        }
    }
}
Also used : SailRepository(org.openrdf.repository.sail.SailRepository) Sail(org.openrdf.sail.Sail) Statement(org.openrdf.model.Statement) ValueFactory(org.openrdf.model.ValueFactory) SailRepositoryConnection(org.openrdf.repository.sail.SailRepositoryConnection) Test(org.junit.Test)

Example 63 with SailRepository

use of org.openrdf.repository.sail.SailRepository in project incubator-rya by apache.

the class RyaMongoDbSailFactoryTest method testReuseSail.

@Test
public void testReuseSail() throws Exception {
    Sail sail = null;
    SailRepository repo = null;
    SailRepositoryConnection conn = null;
    try {
        sail = RyaSailFactory.getInstance(conf);
        repo = new SailRepository(sail);
        conn = repo.getConnection();
        final ValueFactory vf = conn.getValueFactory();
        final Statement s = vf.createStatement(vf.createURI("u:a"), vf.createURI("u:b"), vf.createURI("u:c"));
        assertFalse(conn.hasStatement(s, false));
        conn.add(s);
        Assert.assertTrue(conn.hasStatement(s, false));
        conn.remove(s);
        Assert.assertFalse(conn.hasStatement(s, false));
    } finally {
        if (conn != null) {
            conn.close();
        }
        if (repo != null) {
            repo.shutDown();
        }
        if (sail != null) {
            sail.shutDown();
        }
    }
    // Reuse Sail after shutdown
    try {
        sail = RyaSailFactory.getInstance(conf);
        repo = new SailRepository(sail);
        conn = repo.getConnection();
        final ValueFactory vf = conn.getValueFactory();
        final Statement s = vf.createStatement(vf.createURI("u:a"), vf.createURI("u:b"), vf.createURI("u:c"));
        assertFalse(conn.hasStatement(s, false));
        conn.add(s);
        Assert.assertTrue(conn.hasStatement(s, false));
    } finally {
        if (conn != null) {
            conn.close();
        }
        if (repo != null) {
            repo.shutDown();
        }
        if (sail != null) {
            sail.shutDown();
        }
    }
}
Also used : SailRepository(org.openrdf.repository.sail.SailRepository) Sail(org.openrdf.sail.Sail) Statement(org.openrdf.model.Statement) ValueFactory(org.openrdf.model.ValueFactory) SailRepositoryConnection(org.openrdf.repository.sail.SailRepositoryConnection) Test(org.junit.Test)

Example 64 with SailRepository

use of org.openrdf.repository.sail.SailRepository in project stanbol by apache.

the class KiWiRepositoryService method activate.

@Activate
protected final void activate(ComponentContext context) throws ConfigurationException, RepositoryException {
    log.info("activate KiWi repository ...");
    if (context == null || context.getProperties() == null) {
        throw new IllegalStateException("No valid" + ComponentContext.class + " parsed in activate!");
    }
    // copy the read-only configuration as we might need to change it before
    // adding it to the registered service
    final Dictionary<String, Object> config = copyConfig(context);
    final BundleContext bc = context.getBundleContext();
    // we want to substitute variables used in the dbURL with configuration,
    // framework and system properties
    StrSubstitutor strSubstitutor = new StrSubstitutor(new StrLookup<Object>() {

        @Override
        public String lookup(String key) {
            Object val = config.get(key);
            if (val == null) {
                val = bc.getProperty(key);
            }
            return val.toString();
        }
    });
    String name = (String) config.get(REPOSITORY_ID);
    if (StringUtils.isBlank(name)) {
        throw new ConfigurationException(REPOSITORY_ID, "The parsed Repository ID MUST NOT be NULL nor blank!");
    } else {
        log.debug(" - name: {}", name);
    }
    KiWiDialect dialect;
    String db_type;
    if (StringUtils.equalsIgnoreCase("postgres", (String) config.get(DB_DIALECT))) {
        dialect = new PostgreSQLDialect();
        db_type = "postgresql";
    } else if (StringUtils.equalsIgnoreCase("mysql", (String) config.get(DB_DIALECT))) {
        dialect = new MySQLDialect();
        db_type = "mysql";
    } else if (StringUtils.equalsIgnoreCase("h2", (String) config.get(DB_DIALECT))) {
        dialect = new H2Dialect();
        db_type = "h2";
    } else {
        throw new ConfigurationException(DB_DIALECT, "No valid database dialect was given");
    }
    log.debug(" - dialect: {}", dialect);
    String db_url = (String) config.get(DB_URL);
    if (StringUtils.isBlank(db_url)) {
        // build the db url from parameters
        String db_host = (String) config.get(DB_HOST);
        if (StringUtils.isBlank(db_host)) {
            db_host = DEFAULT_DB_HOST;
        }
        log.debug(" - db host: {}", db_host);
        String db_name = (String) config.get(DB_NAME);
        if (StringUtils.isBlank(db_name)) {
            db_name = DEFAULT_DB_NAME;
        }
        log.debug(" - db name:  {}", name);
        int db_port;
        Object value = config.get(DB_PORT);
        if (value instanceof Number) {
            db_port = ((Number) value).intValue();
        } else if (value != null && !StringUtils.isBlank(value.toString())) {
            db_port = Integer.parseInt(value.toString());
        } else {
            db_port = DEFAULT_DB_PORT;
        }
        log.debug(" - db port: {}", db_port);
        String db_opts = (String) config.get(DB_OPTS);
        log.debug(" - db options: {}", db_opts);
        StringBuilder dbUrlBuilder = new StringBuilder("jdbc:").append(db_type);
        if (dialect instanceof H2Dialect) {
            // H2 uses a file path and not a host so we do not need the ://
            dbUrlBuilder.append(':').append(db_host);
        } else {
            dbUrlBuilder.append("://").append(db_host);
        }
        if (db_port > 0) {
            dbUrlBuilder.append(':').append(db_port);
        }
        if (!StringUtils.isBlank(db_name)) {
            dbUrlBuilder.append('/').append(db_name);
        }
        if (!StringUtils.isBlank(db_opts)) {
            dbUrlBuilder.append(db_opts);
        }
        dbUrl = strSubstitutor.replace(dbUrlBuilder);
    } else if (!db_url.startsWith("jdbc:")) {
        throw new ConfigurationException(DB_URL, "Database URLs are expected to start with " + "'jdbc:' (parsed: '" + db_url + "')!");
    } else {
        dbUrl = strSubstitutor.replace(db_url);
    }
    String db_user = (String) config.get(DB_USER);
    if (StringUtils.isBlank(db_user)) {
        db_user = DEFAULT_DB_USER;
    } else {
        db_user = strSubstitutor.replace(db_user);
    }
    log.debug(" - db user: {}", db_user);
    String db_pass = (String) config.get(DB_PASS);
    if (StringUtils.isBlank(db_pass)) {
        log.debug(" - db pwd is set to default");
        db_pass = DEFAULT_DB_PASS;
    } else {
        log.debug(" - db pwd is set to parsed value");
    }
    KiWiConfiguration configuration = new KiWiConfiguration("Marmotta KiWi", dbUrl, db_user, db_pass, dialect);
    // parse cluster options
    String cluster = (String) config.get(CLUSTER);
    if (!StringUtils.isBlank(cluster)) {
        log.debug(" - cluster: {}", cluster);
        configuration.setClustered(true);
        configuration.setClusterName(cluster);
        String clusterAddress = (String) config.get(CLUSTER_ADDRESS);
        if (!StringUtils.isBlank(clusterAddress)) {
            configuration.setClusterAddress(strSubstitutor.replace(clusterAddress));
        }
        log.debug(" - cluster address: {}", configuration.getClusterAddress());
        Object clusterPort = config.get(CLUSTER_PORT);
        if (clusterPort instanceof Number) {
            int port = ((Number) clusterPort).intValue();
            if (port > 0) {
                configuration.setClusterPort(port);
            }
        // else use default
        } else if (clusterPort != null) {
            try {
                int port = Integer.parseInt(strSubstitutor.replace(clusterPort));
                if (port > 0) {
                    configuration.setClusterPort(port);
                }
            } catch (NumberFormatException e) {
                throw new ConfigurationException(CLUSTER_PORT, "Unable to parse " + "Cluster Port from configured value '" + clusterPort + "'!", e);
            }
        }
        log.debug(" - cluster port ({})", configuration.getClusterPort());
        String cachingBackend = (String) config.get(CACHING_BACKEND);
        if (StringUtils.isBlank(cachingBackend)) {
            configuration.setCachingBackend(CachingBackends.valueOf(DEFAULT_CACHING_BACKEND));
        } else {
            try {
                configuration.setCachingBackend(CachingBackends.valueOf(strSubstitutor.replace(cachingBackend)));
            } catch (IllegalArgumentException e) {
                throw new ConfigurationException(CACHING_BACKEND, "Unsupported CachingBackend '" + cachingBackend + "' (supported: " + Arrays.toString(CachingBackends.values()) + ")!", e);
            }
        }
        log.debug(" - caching Backend: {}", configuration.getCachingBackend());
        String cacheMode = (String) config.get(CACHE_MODE);
        if (StringUtils.isBlank(cacheMode)) {
            cacheMode = DEFAULT_CACHE_MODE;
        }
        try {
            configuration.setCacheMode(CacheMode.valueOf(strSubstitutor.replace(cacheMode)));
        } catch (IllegalArgumentException e) {
            throw new ConfigurationException(CACHE_MODE, "Unsupported CacheMode '" + cacheMode + "' (supported: " + Arrays.toString(CacheMode.values()) + ")!");
        }
        log.debug(" - cache mode: {}", configuration.getCacheMode());
    } else {
        // not clustered
        log.debug(" - no cluster configured");
        configuration.setClustered(false);
    }
    log.info(" ... initialise KiWi repository: {}", dbUrl);
    KiWiStore store = new KiWiStore(configuration);
    repository = new SailRepository(new KiWiSparqlSail(store));
    repository.initialize();
    // set the repository type property to KiWiStore
    config.put(SAIL_IMPL, KiWiStore.class.getName());
    repoRegistration = context.getBundleContext().registerService(Repository.class.getName(), repository, config);
    log.info("  - successfully registered KiWi Repository {}", name);
}
Also used : KiWiSparqlSail(org.apache.marmotta.kiwi.sparql.sail.KiWiSparqlSail) H2Dialect(org.apache.marmotta.kiwi.persistence.h2.H2Dialect) SailRepository(org.openrdf.repository.sail.SailRepository) KiWiStore(org.apache.marmotta.kiwi.sail.KiWiStore) KiWiDialect(org.apache.marmotta.kiwi.persistence.KiWiDialect) StrSubstitutor(org.apache.commons.lang3.text.StrSubstitutor) MySQLDialect(org.apache.marmotta.kiwi.persistence.mysql.MySQLDialect) PostgreSQLDialect(org.apache.marmotta.kiwi.persistence.pgsql.PostgreSQLDialect) ConfigurationException(org.osgi.service.cm.ConfigurationException) KiWiConfiguration(org.apache.marmotta.kiwi.config.KiWiConfiguration) BundleContext(org.osgi.framework.BundleContext) Activate(org.apache.felix.scr.annotations.Activate)

Aggregations

SailRepository (org.openrdf.repository.sail.SailRepository)64 Sail (org.openrdf.sail.Sail)45 SailRepositoryConnection (org.openrdf.repository.sail.SailRepositoryConnection)41 Test (org.junit.Test)27 BindingSet (org.openrdf.query.BindingSet)14 RepositoryException (org.openrdf.repository.RepositoryException)14 TupleQueryResult (org.openrdf.query.TupleQueryResult)13 AccumuloRdfConfiguration (org.apache.rya.accumulo.AccumuloRdfConfiguration)11 TupleQuery (org.openrdf.query.TupleQuery)10 Configuration (org.apache.hadoop.conf.Configuration)9 SailException (org.openrdf.sail.SailException)9 HashSet (java.util.HashSet)8 ArrayList (java.util.ArrayList)7 AccumuloException (org.apache.accumulo.core.client.AccumuloException)7 AccumuloSecurityException (org.apache.accumulo.core.client.AccumuloSecurityException)7 RyaClientException (org.apache.rya.api.client.RyaClientException)7 RyaDAOException (org.apache.rya.api.persist.RyaDAOException)7 Repository (org.openrdf.repository.Repository)7 IOException (java.io.IOException)6 RyaURI (org.apache.rya.api.domain.RyaURI)6