Search in sources :

Example 16 with TerminatedException

use of org.exist.xquery.TerminatedException in project exist by eXist-db.

the class NativeValueIndex method scanIndexKeys.

public ValueOccurrences[] scanIndexKeys(final DocumentSet docs, final NodeSet contextSet, final Indexable start) {
    final int type = start.getType();
    final boolean stringType = Type.subTypeOf(type, Type.STRING);
    final IndexScanCallback cb = new IndexScanCallback(docs, contextSet, type, false);
    for (final Iterator<Collection> i = docs.getCollectionIterator(); i.hasNext(); ) {
        try (final ManagedLock<ReentrantLock> bfileLock = lockManager.acquireBtreeReadLock(dbValues.getLockName())) {
            final Collection c = i.next();
            final int collectionId = c.getId();
            // Compute a key for the start value in the collection
            final Value startKey = new SimpleValue(collectionId, start);
            if (stringType) {
                final IndexQuery query = new IndexQuery(IndexQuery.TRUNC_RIGHT, startKey);
                dbValues.query(query, cb);
            } else {
                final Value prefixKey = new SimplePrefixValue(collectionId, start.getType());
                final IndexQuery query = new IndexQuery(IndexQuery.GEQ, startKey);
                dbValues.query(query, prefixKey, cb);
            }
        } catch (final EXistException | IOException | TerminatedException | BTreeException e) {
            LOG.error(e.getMessage(), e);
        } catch (final LockException e) {
            LOG.warn("Failed to acquire lock for '{}'", FileUtils.fileName(dbValues.getFile()), e);
        }
    }
    final Map<AtomicValue, ValueOccurrences> map = cb.map;
    final ValueOccurrences[] result = new ValueOccurrences[map.size()];
    return map.values().toArray(result);
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) IndexQuery(org.exist.storage.btree.IndexQuery) AtomicValue(org.exist.xquery.value.AtomicValue) EXistException(org.exist.EXistException) IOException(java.io.IOException) BTreeException(org.exist.storage.btree.BTreeException) AtomicValue(org.exist.xquery.value.AtomicValue) StringValue(org.exist.xquery.value.StringValue) Value(org.exist.storage.btree.Value) Collection(org.exist.collections.Collection) TerminatedException(org.exist.xquery.TerminatedException)

Example 17 with TerminatedException

use of org.exist.xquery.TerminatedException in project exist by eXist-db.

the class ConsistencyCheck method checkCollection.

private void checkCollection(final Collection collection, final List<ErrorReport> errors, final ProgressCallback callback) throws TerminatedException {
    final XmldbURI uri = collection.getURI();
    if (callback != null) {
        callback.startCollection(uri.toString());
    }
    checkPermissions(collection, errors);
    try {
        for (final Iterator<XmldbURI> i = collection.collectionIteratorNoLock(broker); i.hasNext(); ) {
            final XmldbURI childUri = i.next();
            try {
                final Collection child = broker.getCollection(uri.append(childUri));
                if (child == null) {
                    final ErrorReport.CollectionError error = new org.exist.backup.ErrorReport.CollectionError(org.exist.backup.ErrorReport.CHILD_COLLECTION, "Child collection not found: " + childUri + ", parent is " + uri);
                    error.setCollectionId(collection.getId());
                    error.setCollectionURI(childUri);
                    errors.add(error);
                    if (callback != null) {
                        callback.error(error);
                    }
                    continue;
                }
                if (child.getId() != collection.getId()) {
                    checkCollection(child, errors, callback);
                }
            } catch (final Exception e) {
                final ErrorReport.CollectionError error = new ErrorReport.CollectionError(org.exist.backup.ErrorReport.CHILD_COLLECTION, "Error while loading child collection: " + childUri + ", parent is " + uri);
                error.setCollectionId(collection.getId());
                error.setCollectionURI(childUri);
                errors.add(error);
                if (callback != null) {
                    callback.error(error);
                }
            }
        }
    } catch (final PermissionDeniedException pde) {
        final ErrorReport.CollectionError error = new ErrorReport.CollectionError(org.exist.backup.ErrorReport.CHILD_COLLECTION, "Error while loading collection: " + collection.getURI() + ", parent is " + uri);
        error.setCollectionId(collection.getId());
        error.setCollectionURI(collection.getURI());
        errors.add(error);
        if (callback != null) {
            callback.error(error);
        }
    }
}
Also used : Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException) XmldbURI(org.exist.xmldb.XmldbURI) PermissionDeniedException(org.exist.security.PermissionDeniedException) TerminatedException(org.exist.xquery.TerminatedException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException)

Example 18 with TerminatedException

use of org.exist.xquery.TerminatedException in project exist by eXist-db.

the class ExportGUI method checkDB.

private List<ErrorReport> checkDB() {
    if (!startDB()) {
        return (null);
    }
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        Object[] selected = directAccessBtn.getSelectedObjects();
        final boolean directAccess = (selected != null) && (selected[0] != null);
        selected = scanBtn.getSelectedObjects();
        final boolean scan = (selected != null) && (selected[0] != null);
        final ConsistencyCheck checker = new ConsistencyCheck(broker, transaction, directAccess, scan);
        final org.exist.backup.ConsistencyCheck.ProgressCallback cb = new ConsistencyCheck.ProgressCallback() {

            public void startDocument(final String path, final int current, final int count) {
                progress.setString(path);
                progress.setValue(progress.getValue() + 1);
            }

            public void error(final ErrorReport error) {
                displayMessage(error.toString());
                displayMessage("---------------------------------------------------");
            }

            public void startCollection(final String path) {
                progress.setString(path);
            }
        };
        progress.setIndeterminate(true);
        messages.setText("");
        displayMessage("Checking collections ...");
        final List<ErrorReport> errors = checker.checkCollectionTree(cb);
        if (errors.isEmpty()) {
            displayMessage("No errors found.");
        } else {
            displayMessage("Errors found.");
        }
        progress.setStringPainted(true);
        progress.setString("Counting documents ...");
        documentCount = checker.getDocumentCount();
        progress.setIndeterminate(false);
        progress.setValue(0);
        progress.setMinimum(0);
        progress.setMaximum(documentCount);
        displayMessage("Checking documents ...");
        checker.checkDocuments(cb, errors);
        if (errors.isEmpty()) {
            displayMessage("No errors found.");
        } else {
            displayMessage("Errors found.");
        }
        progress.setString("");
        transaction.commit();
        return errors;
    } catch (final EXistException | PermissionDeniedException e) {
        System.err.println("ERROR: Failed to retrieve database broker: " + e.getMessage());
    } catch (final TerminatedException e) {
        System.err.println("WARN: Check terminated by db.");
    } finally {
        progress.setValue(0);
        currentTask.setText(" ");
    }
    return (null);
}
Also used : Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException) DBBroker(org.exist.storage.DBBroker) PermissionDeniedException(org.exist.security.PermissionDeniedException) TerminatedException(org.exist.xquery.TerminatedException)

Example 19 with TerminatedException

use of org.exist.xquery.TerminatedException in project exist by eXist-db.

the class SystemExport method export.

/**
 * Export the contents of the database, trying to preserve as much data as possible. To be effective, this method should be used in combination
 * with class {@link ConsistencyCheck}.
 *
 * @param targetDir   the output directory or file to which data will be written. Output will be written to a zip file if target ends with
 *                    .zip.
 * @param incremental DOCUMENT ME!
 * @param maxInc      DOCUMENT ME!
 * @param zip         DOCUMENT ME!
 * @param errorList   a list of {@link ErrorReport} objects as returned by methods in {@link ConsistencyCheck}.
 * @return DOCUMENT ME!
 */
public Path export(final String targetDir, boolean incremental, final int maxInc, final boolean zip, final List<ErrorReport> errorList) {
    Path backupFile = null;
    try {
        final BackupDirectory directory = new BackupDirectory(targetDir);
        BackupDescriptor prevBackup = null;
        if (incremental) {
            prevBackup = directory.lastBackupFile();
            LOG.info("Creating incremental backup. Prev backup: {}", (prevBackup == null) ? "none" : prevBackup.getSymbolicPath());
        }
        final Properties properties = new Properties();
        int seqNr = 1;
        if (incremental) {
            properties.setProperty(BackupDescriptor.PREVIOUS_PROP_NAME, (prevBackup == null) ? "" : prevBackup.getName());
            if (prevBackup != null) {
                final Properties prevProp = prevBackup.getProperties();
                if (prevProp != null) {
                    final String seqNrStr = prevProp.getProperty(BackupDescriptor.NUMBER_IN_SEQUENCE_PROP_NAME, "1");
                    try {
                        seqNr = Integer.parseInt(seqNrStr);
                        if (seqNr == maxInc) {
                            seqNr = 1;
                            incremental = false;
                            prevBackup = null;
                        } else {
                            ++seqNr;
                        }
                    } catch (final NumberFormatException e) {
                        LOG.warn("Bad sequence number in backup descriptor: {}", prevBackup.getName());
                    }
                }
            }
        }
        properties.setProperty(BackupDescriptor.NUMBER_IN_SEQUENCE_PROP_NAME, Integer.toString(seqNr));
        properties.setProperty(BackupDescriptor.INCREMENTAL_PROP_NAME, incremental ? "yes" : "no");
        try {
            properties.setProperty(BackupDescriptor.DATE_PROP_NAME, new DateTimeValue(new Date()).getStringValue());
        } catch (final XPathException e) {
        }
        backupFile = directory.createBackup(incremental && (prevBackup != null), zip);
        final FunctionE<Path, BackupWriter, IOException> fWriter;
        if (zip) {
            fWriter = p -> new ZipWriter(p, XmldbURI.ROOT_COLLECTION);
        } else {
            fWriter = FileSystemWriter::new;
        }
        try (final BackupWriter output = fWriter.apply(backupFile)) {
            output.setProperties(properties);
            // File repoBackup = RepoBackup.backup(broker);
            // output.addToRoot(RepoBackup.REPO_ARCHIVE, repoBackup);
            // FileUtils.forceDelete(repoBackup);
            final Date date = (prevBackup == null) ? null : prevBackup.getDate();
            final CollectionCallback cb = new CollectionCallback(output, date, prevBackup, errorList, true);
            broker.getCollectionsFailsafe(transaction, cb);
            exportOrphans(output, cb.getDocs(), errorList);
        }
        return backupFile;
    } catch (final IOException e) {
        reportError("A write error occurred while exporting data: '" + e.getMessage() + "'. Aborting export.", e);
        return null;
    } catch (final TerminatedException e) {
        if (backupFile != null) {
            FileUtils.deleteQuietly(backupFile);
        }
        return null;
    }
}
Also used : Path(java.nio.file.Path) DateTimeValue(org.exist.xquery.value.DateTimeValue) XPathException(org.exist.xquery.XPathException) TerminatedException(org.exist.xquery.TerminatedException)

Aggregations

TerminatedException (org.exist.xquery.TerminatedException)19 EXistException (org.exist.EXistException)9 IOException (java.io.IOException)8 ReentrantLock (java.util.concurrent.locks.ReentrantLock)8 PermissionDeniedException (org.exist.security.PermissionDeniedException)6 BTreeException (org.exist.storage.btree.BTreeException)6 IndexQuery (org.exist.storage.btree.IndexQuery)6 Value (org.exist.storage.btree.Value)6 XMLStreamException (javax.xml.stream.XMLStreamException)3 PooledObject (org.apache.commons.pool2.PooledObject)3 DefaultPooledObject (org.apache.commons.pool2.impl.DefaultPooledObject)3 Collection (org.exist.collections.Collection)3 DOMTransaction (org.exist.storage.dom.DOMTransaction)3 XmldbURI (org.exist.xmldb.XmldbURI)3 XPathException (org.exist.xquery.XPathException)3 DateTimeValue (org.exist.xquery.value.DateTimeValue)3 Path (java.nio.file.Path)2 ACLPermission (org.exist.security.ACLPermission)2 Permission (org.exist.security.Permission)2 DBBroker (org.exist.storage.DBBroker)2