Search in sources :

Example 26 with SailException

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

the class RdfCloudTripleStoreConnection method addStatementInternal.

@Override
protected void addStatementInternal(final Resource subject, final URI predicate, final Value object, final Resource... contexts) throws SailException {
    try {
        final String cv_s = conf.getCv();
        final byte[] cv = cv_s == null ? null : cv_s.getBytes(StandardCharsets.UTF_8);
        final List<RyaStatement> ryaStatements = new ArrayList<>();
        if (contexts != null && contexts.length > 0) {
            for (final Resource context : contexts) {
                final RyaStatement statement = new RyaStatement(RdfToRyaConversions.convertResource(subject), RdfToRyaConversions.convertURI(predicate), RdfToRyaConversions.convertValue(object), RdfToRyaConversions.convertResource(context), null, new StatementMetadata(), cv);
                ryaStatements.add(statement);
            }
        } else {
            final RyaStatement statement = new RyaStatement(RdfToRyaConversions.convertResource(subject), RdfToRyaConversions.convertURI(predicate), RdfToRyaConversions.convertValue(object), null, null, new StatementMetadata(), cv);
            ryaStatements.add(statement);
        }
        ryaDAO.add(ryaStatements.iterator());
    } catch (final RyaDAOException e) {
        throw new SailException(e);
    }
}
Also used : ArrayList(java.util.ArrayList) Resource(org.openrdf.model.Resource) StatementMetadata(org.apache.rya.api.domain.StatementMetadata) RyaStatement(org.apache.rya.api.domain.RyaStatement) RyaDAOException(org.apache.rya.api.persist.RyaDAOException) SailException(org.openrdf.sail.SailException)

Example 27 with SailException

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

the class RdfCloudTripleStoreConnection method getStatementsInternal.

@Override
protected CloseableIteration<? extends Statement, SailException> getStatementsInternal(final Resource subject, final URI predicate, final Value object, final boolean flag, final Resource... contexts) throws SailException {
    // try {
    // have to do this to get the inferred values
    // TODO: Will this method reduce performance?
    final Var subjVar = decorateValue(subject, "s");
    final Var predVar = decorateValue(predicate, "p");
    final Var objVar = decorateValue(object, "o");
    StatementPattern sp = null;
    final boolean hasContext = contexts != null && contexts.length > 0;
    final Resource context = (hasContext) ? contexts[0] : null;
    final Var cntxtVar = decorateValue(context, "c");
    // TODO: Only using one context here
    sp = new StatementPattern(subjVar, predVar, objVar, cntxtVar);
    // return new StoreTripleSource(store.getConf()).getStatements(resource, uri, value, contexts);
    final CloseableIteration<? extends BindingSet, QueryEvaluationException> evaluate = evaluate(sp, null, null, false);
    return new // TODO: Use a util class to do this
    CloseableIteration<Statement, SailException>() {

        private boolean isClosed = false;

        @Override
        public void close() throws SailException {
            isClosed = true;
            try {
                evaluate.close();
            } catch (final QueryEvaluationException e) {
                throw new SailException(e);
            }
        }

        @Override
        public boolean hasNext() throws SailException {
            try {
                return evaluate.hasNext();
            } catch (final QueryEvaluationException e) {
                throw new SailException(e);
            }
        }

        @Override
        public Statement next() throws SailException {
            if (!hasNext() || isClosed) {
                throw new NoSuchElementException();
            }
            try {
                final BindingSet next = evaluate.next();
                final Resource bs_subj = (Resource) ((subjVar.hasValue()) ? subjVar.getValue() : next.getBinding(subjVar.getName()).getValue());
                final URI bs_pred = (URI) ((predVar.hasValue()) ? predVar.getValue() : next.getBinding(predVar.getName()).getValue());
                final Value bs_obj = (objVar.hasValue()) ? objVar.getValue() : (Value) next.getBinding(objVar.getName()).getValue();
                final Binding b_cntxt = next.getBinding(cntxtVar.getName());
                // convert BindingSet to Statement
                if (b_cntxt != null) {
                    return new ContextStatementImpl(bs_subj, bs_pred, bs_obj, (Resource) b_cntxt.getValue());
                } else {
                    return new StatementImpl(bs_subj, bs_pred, bs_obj);
                }
            } catch (final QueryEvaluationException e) {
                throw new SailException(e);
            }
        }

        @Override
        public void remove() throws SailException {
            try {
                evaluate.remove();
            } catch (final QueryEvaluationException e) {
                throw new SailException(e);
            }
        }
    };
// } catch (QueryEvaluationException e) {
// throw new SailException(e);
// }
}
Also used : Binding(org.openrdf.query.Binding) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) EmptyBindingSet(org.openrdf.query.impl.EmptyBindingSet) BindingSet(org.openrdf.query.BindingSet) ContextStatementImpl(org.openrdf.model.impl.ContextStatementImpl) Var(org.openrdf.query.algebra.Var) Resource(org.openrdf.model.Resource) SailException(org.openrdf.sail.SailException) URI(org.openrdf.model.URI) RyaURI(org.apache.rya.api.domain.RyaURI) CloseableIteration(info.aduna.iteration.CloseableIteration) StatementPattern(org.openrdf.query.algebra.StatementPattern) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) ContextStatementImpl(org.openrdf.model.impl.ContextStatementImpl) StatementImpl(org.openrdf.model.impl.StatementImpl) Value(org.openrdf.model.Value) NoSuchElementException(java.util.NoSuchElementException)

Example 28 with SailException

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

the class MergeDriverClient method main.

public static void main(final String[] args) throws ParseException, MergeConfigurationException, UnknownHostException, MergerException, java.text.ParseException, SailException, AccumuloException, AccumuloSecurityException, InferenceEngineException, RepositoryException, MalformedQueryException, UpdateExecutionException {
    final String log4jConfiguration = System.getProperties().getProperty("log4j.configuration");
    if (StringUtils.isNotBlank(log4jConfiguration)) {
        final String parsedConfiguration = PathUtils.clean(StringUtils.removeStart(log4jConfiguration, "file:"));
        final File configFile = new File(parsedConfiguration);
        if (configFile.exists()) {
            DOMConfigurator.configure(parsedConfiguration);
        } else {
            BasicConfigurator.configure();
        }
    }
    final MergeConfigurationCLI config = new MergeConfigurationCLI(args);
    try {
        configuration = config.createConfiguration();
    } catch (final MergeConfigurationException e) {
        LOG.error("Configuration failed.", e);
    }
    final boolean useTimeSync = configuration.getUseNtpServer();
    Optional<Long> offset = Optional.absent();
    if (useTimeSync) {
        final String tomcat = configuration.getChildTomcatUrl();
        final String ntpHost = configuration.getNtpServerHost();
        try {
            offset = Optional.<Long>fromNullable(TimeUtils.getNtpServerAndMachineTimeDifference(ntpHost, tomcat));
        } catch (final IOException e) {
            LOG.error("Unable to get time difference between time server: " + ntpHost + " and the server: " + tomcat, e);
        }
    }
    final StatementStoreFactory storeFactory = new StatementStoreFactory(configuration);
    try {
        final RyaStatementStore parentStore = storeFactory.getParentStatementStore();
        final RyaStatementStore childStore = storeFactory.getChildStatementStore();
        LOG.info("Starting Merge Tool");
        if (configuration.getParentDBType() == ACCUMULO && configuration.getChildDBType() == ACCUMULO) {
            final AccumuloRyaStatementStore childAStore = (AccumuloRyaStatementStore) childStore;
            final AccumuloRyaStatementStore parentAStore = (AccumuloRyaStatementStore) parentStore;
        // do map reduce merging.
        // TODO: Run Merger
        } else {
            if (configuration.getMergePolicy() == TIMESTAMP) {
                final TimestampPolicyMergeConfiguration timeConfig = (TimestampPolicyMergeConfiguration) configuration;
                final Long timeOffset;
                if (offset.isPresent()) {
                    timeOffset = offset.get();
                } else {
                    timeOffset = 0L;
                }
                final MemoryTimeMerger merger = new MemoryTimeMerger(parentStore, childStore, new VisibilityStatementMerger(), timeConfig.getToolStartTime(), configuration.getParentRyaInstanceName(), timeOffset);
                merger.runJob();
            }
        }
    } catch (final Exception e) {
        LOG.error("Something went wrong creating a Rya Statement Store connection.", e);
    }
    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(final Thread thread, final Throwable throwable) {
            LOG.error("Uncaught exception in " + thread.getName(), throwable);
        }
    });
    LOG.info("Finished running Merge Tool");
    System.exit(1);
}
Also used : StatementStoreFactory(org.apache.rya.export.client.merge.StatementStoreFactory) TimestampPolicyMergeConfiguration(org.apache.rya.export.api.conf.policy.TimestampPolicyMergeConfiguration) VisibilityStatementMerger(org.apache.rya.export.client.merge.VisibilityStatementMerger) MemoryTimeMerger(org.apache.rya.export.client.merge.MemoryTimeMerger) IOException(java.io.IOException) MergeConfigurationCLI(org.apache.rya.export.client.conf.MergeConfigurationCLI) InferenceEngineException(org.apache.rya.rdftriplestore.inference.InferenceEngineException) SailException(org.openrdf.sail.SailException) RepositoryException(org.openrdf.repository.RepositoryException) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) UpdateExecutionException(org.openrdf.query.UpdateExecutionException) MalformedQueryException(org.openrdf.query.MalformedQueryException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) AccumuloException(org.apache.accumulo.core.client.AccumuloException) MergerException(org.apache.rya.export.api.MergerException) ParseException(org.apache.commons.cli.ParseException) MergeConfigurationException(org.apache.rya.export.api.conf.MergeConfigurationException) MergeConfigurationException(org.apache.rya.export.api.conf.MergeConfigurationException) AccumuloRyaStatementStore(org.apache.rya.export.accumulo.AccumuloRyaStatementStore) File(java.io.File) AccumuloRyaStatementStore(org.apache.rya.export.accumulo.AccumuloRyaStatementStore) RyaStatementStore(org.apache.rya.export.api.store.RyaStatementStore)

Example 29 with SailException

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

the class AccumuloLoadStatementsFile method loadStatements.

@Override
public void loadStatements(final String ryaInstanceName, final Path statementsFile, final RDFFormat format) throws InstanceDoesNotExistException, RyaClientException {
    requireNonNull(ryaInstanceName);
    requireNonNull(statementsFile);
    requireNonNull(format);
    // Ensure the Rya Instance exists.
    if (!instanceExists.exists(ryaInstanceName)) {
        throw new InstanceDoesNotExistException(String.format("There is no Rya instance named '%s'.", ryaInstanceName));
    }
    Sail sail = null;
    SailRepository sailRepo = null;
    SailRepositoryConnection sailRepoConn = null;
    try {
        // Get a Sail object that is connected to the Rya instance.
        final AccumuloRdfConfiguration ryaConf = getAccumuloConnectionDetails().buildAccumuloRdfConfiguration(ryaInstanceName);
        // RYA-327 should address this hardcoded value.
        ryaConf.setFlush(false);
        sail = RyaSailFactory.getInstance(ryaConf);
        // Load the file.
        sailRepo = new SailRepository(sail);
        sailRepoConn = sailRepo.getConnection();
        sailRepoConn.add(statementsFile.toFile(), null, format);
    } catch (final SailException | AccumuloException | AccumuloSecurityException | RyaDAOException | InferenceEngineException e) {
        log.warn("Exception while loading:", e);
        throw new RyaClientException("A problem connecting to the Rya instance named '" + ryaInstanceName + "' has caused the load to fail.", e);
    } catch (final RepositoryException | RDFParseException | UnsupportedRDFormatException | IOException e) {
        log.warn("Exception while loading:", e);
        throw new RyaClientException("A problem processing the RDF file has caused the load into Rya instance named " + ryaInstanceName + "to fail.", e);
    } finally {
        // Shut it all down.
        if (sailRepoConn != null) {
            try {
                sailRepoConn.close();
            } catch (final RepositoryException e) {
                log.warn("Couldn't close the SailRepoConnection that is attached to the Rya instance.", e);
            }
        }
        if (sailRepo != null) {
            try {
                sailRepo.shutDown();
            } catch (final RepositoryException e) {
                log.warn("Couldn't shut down the SailRepository that is attached to the Rya instance.", e);
            }
        }
        if (sail != null) {
            try {
                sail.shutDown();
            } catch (final SailException e) {
                log.warn("Couldn't shut down the Sail that is attached to the Rya instance.", e);
            }
        }
    }
}
Also used : AccumuloException(org.apache.accumulo.core.client.AccumuloException) UnsupportedRDFormatException(org.openrdf.rio.UnsupportedRDFormatException) RyaClientException(org.apache.rya.api.client.RyaClientException) SailRepository(org.openrdf.repository.sail.SailRepository) InferenceEngineException(org.apache.rya.rdftriplestore.inference.InferenceEngineException) RepositoryException(org.openrdf.repository.RepositoryException) InstanceDoesNotExistException(org.apache.rya.api.client.InstanceDoesNotExistException) SailException(org.openrdf.sail.SailException) IOException(java.io.IOException) SailRepositoryConnection(org.openrdf.repository.sail.SailRepositoryConnection) AccumuloRdfConfiguration(org.apache.rya.accumulo.AccumuloRdfConfiguration) Sail(org.openrdf.sail.Sail) RyaDAOException(org.apache.rya.api.persist.RyaDAOException) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) RDFParseException(org.openrdf.rio.RDFParseException)

Example 30 with SailException

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

the class MongoBatchUpdatePCJ method updatePCJResults.

private void updatePCJResults(final String ryaInstanceName, final String pcjId, final MongoClient client) throws InstanceDoesNotExistException, PCJDoesNotExistException, RyaClientException {
    // Things that have to be closed before we exit.
    Sail sail = null;
    SailConnection sailConn = null;
    try (final PrecomputedJoinStorage pcjStorage = new MongoPcjStorage(client, ryaInstanceName)) {
        // Create an instance of Sail backed by the Rya instance.
        sail = connectToRya(ryaInstanceName);
        final SailRepository sailRepo = new SailRepository(sail);
        final SailRepositoryConnection sailRepoConn = sailRepo.getConnection();
        // Purge the old results from the PCJ.
        try {
            pcjStorage.purge(pcjId);
        } catch (final PCJStorageException e) {
            throw new RyaClientException("Could not batch update PCJ with ID '" + pcjId + "' because the old " + "results could not be purged from it.", e);
        }
        // Parse the PCJ's SPARQL query.
        final PcjMetadata metadata = pcjStorage.getPcjMetadata(pcjId);
        final String sparql = metadata.getSparql();
        sailConn = sail.getConnection();
        final TupleQuery tupleQuery = sailRepoConn.prepareTupleQuery(QueryLanguage.SPARQL, sparql);
        // Execute the query.
        final List<VisibilityBindingSet> batch = new ArrayList<>(1000);
        tupleQuery.evaluate(new TupleQueryResultHandlerBase() {

            @Override
            public void handleSolution(final BindingSet bindingSet) throws TupleQueryResultHandlerException {
                final VisibilityBindingSet result = new VisibilityBindingSet(bindingSet, "");
                log.warn("Visibility information on the binding set is lost during a batch update." + "  This can create data leaks.");
                batch.add(result);
                if (batch.size() == 1000) {
                    try {
                        pcjStorage.addResults(pcjId, batch);
                    } catch (final PCJStorageException e) {
                        throw new TupleQueryResultHandlerException("Fail to batch load new results into the PCJ with ID '" + pcjId + "'.", e);
                    }
                    batch.clear();
                }
            }
        });
        if (!batch.isEmpty()) {
            pcjStorage.addResults(pcjId, batch);
            batch.clear();
        }
    } catch (final MalformedQueryException | PCJStorageException | SailException | QueryEvaluationException | RepositoryException | TupleQueryResultHandlerException e) {
        throw new RyaClientException("Fail to batch load new results into the PCJ with ID '" + pcjId + "'.", e);
    } finally {
        if (sailConn != null) {
            try {
                sailConn.close();
            } catch (final SailException e) {
                log.warn(e.getMessage(), e);
            }
        }
        if (sail != null) {
            try {
                sail.shutDown();
            } catch (final SailException e) {
                log.warn(e.getMessage(), e);
            }
        }
    }
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) BindingSet(org.openrdf.query.BindingSet) RyaClientException(org.apache.rya.api.client.RyaClientException) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) TupleQueryResultHandlerBase(org.openrdf.query.TupleQueryResultHandlerBase) TupleQueryResultHandlerException(org.openrdf.query.TupleQueryResultHandlerException) SailRepository(org.openrdf.repository.sail.SailRepository) ArrayList(java.util.ArrayList) TupleQuery(org.openrdf.query.TupleQuery) RepositoryException(org.openrdf.repository.RepositoryException) RyaDetailsRepositoryException(org.apache.rya.api.instance.RyaDetailsRepository.RyaDetailsRepositoryException) SailException(org.openrdf.sail.SailException) SailRepositoryConnection(org.openrdf.repository.sail.SailRepositoryConnection) MongoPcjStorage(org.apache.rya.indexing.pcj.storage.mongo.MongoPcjStorage) SailConnection(org.openrdf.sail.SailConnection) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) Sail(org.openrdf.sail.Sail) PrecomputedJoinStorage(org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage) MalformedQueryException(org.openrdf.query.MalformedQueryException) PcjMetadata(org.apache.rya.indexing.pcj.storage.PcjMetadata) PCJStorageException(org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage.PCJStorageException)

Aggregations

SailException (org.openrdf.sail.SailException)46 RyaDAOException (org.apache.rya.api.persist.RyaDAOException)17 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)14 RyaClientException (org.apache.rya.api.client.RyaClientException)13 SailConnection (org.openrdf.sail.SailConnection)12 AccumuloException (org.apache.accumulo.core.client.AccumuloException)11 AccumuloSecurityException (org.apache.accumulo.core.client.AccumuloSecurityException)11 Sail (org.openrdf.sail.Sail)11 InferenceEngineException (org.apache.rya.rdftriplestore.inference.InferenceEngineException)10 URI (org.openrdf.model.URI)10 RepositoryException (org.openrdf.repository.RepositoryException)10 MalformedQueryException (org.openrdf.query.MalformedQueryException)9 InstanceDoesNotExistException (org.apache.rya.api.client.InstanceDoesNotExistException)8 Statement (org.openrdf.model.Statement)8 SailRepository (org.openrdf.repository.sail.SailRepository)7 SailRepositoryConnection (org.openrdf.repository.sail.SailRepositoryConnection)7 Resource (org.openrdf.model.Resource)5 IOException (java.io.IOException)4 HashSet (java.util.HashSet)4 AccumuloRdfConfiguration (org.apache.rya.accumulo.AccumuloRdfConfiguration)4