Search in sources :

Example 16 with DateTimeValue

use of org.exist.xquery.value.DateTimeValue 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)

Example 17 with DateTimeValue

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

the class GetRunningXQueries method getRunningXQuery.

private void getRunningXQuery(MemTreeBuilder builder, XQueryContext context, XQueryWatchDog watchdog) throws XPathException {
    builder.startElement(new QName("xquery", NAMESPACE_URI, PREFIX), null);
    builder.addAttribute(new QName("id", null, null), "" + context.hashCode());
    builder.addAttribute(new QName("sourceType", null, null), context.getSource().type());
    builder.addAttribute(new QName("started", null, null), new DateTimeValue(new Date(watchdog.getStartTime())).getStringValue());
    builder.addAttribute(new QName("terminating", null, null), (watchdog.isTerminating() ? "true" : "false"));
    builder.addAttribute(new QName("caller", null, null), context == getContext() ? "true" : "false");
    builder.startElement(new QName("sourceKey", NAMESPACE_URI, PREFIX), null);
    builder.characters(context.getSource().pathOrShortIdentifier());
    builder.endElement();
    builder.startElement(new QName("xqueryExpression", NAMESPACE_URI, PREFIX), null);
    builder.characters(context.getRootExpression().toString());
    builder.endElement();
    builder.endElement();
}
Also used : DateTimeValue(org.exist.xquery.value.DateTimeValue) QName(org.exist.dom.QName) Date(java.util.Date)

Example 18 with DateTimeValue

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

the class SystemTime method eval.

public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().start(this);
        context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
        if (contextSequence != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
        }
        if (contextItem != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
        }
    }
    Sequence result = new DateTimeValue(new Date());
    if (isCalledAs("system-dateTime")) {
    // do nothing, result already in right form
    } else if (isCalledAs("system-date")) {
        result = result.convertTo(Type.DATE);
    } else if (isCalledAs("system-time")) {
        result = result.convertTo(Type.TIME);
    } else {
        throw (new Error("can't handle function " + getName().getLocalPart()));
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return (result);
}
Also used : DateTimeValue(org.exist.xquery.value.DateTimeValue) Sequence(org.exist.xquery.value.Sequence) Date(java.util.Date)

Example 19 with DateTimeValue

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

the class FindLastModified method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    final NodeSet nodes = args[0].toNodeSet();
    if (nodes.isEmpty()) {
        return Sequence.EMPTY_SEQUENCE;
    }
    final NodeSet result = new NewArrayNodeSet();
    final DateTimeValue dtv = (DateTimeValue) args[1].itemAt(0);
    final long lastModified = dtv.getDate().getTime();
    for (final NodeProxy proxy : nodes) {
        final DocumentImpl doc = proxy.getOwnerDocument();
        final long modified = doc.getLastModified();
        boolean matches;
        if (this.isCalledAs("find-last-modified-since")) {
            matches = modified > lastModified;
        } else {
            matches = modified <= lastModified;
        }
        if (matches) {
            result.add(proxy);
        }
    }
    return result;
}
Also used : NodeSet(org.exist.dom.persistent.NodeSet) NewArrayNodeSet(org.exist.dom.persistent.NewArrayNodeSet) NewArrayNodeSet(org.exist.dom.persistent.NewArrayNodeSet) DateTimeValue(org.exist.xquery.value.DateTimeValue) NodeProxy(org.exist.dom.persistent.NodeProxy) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Aggregations

DateTimeValue (org.exist.xquery.value.DateTimeValue)19 XPathException (org.exist.xquery.XPathException)12 Date (java.util.Date)9 IOException (java.io.IOException)8 Permission (org.exist.security.Permission)6 PermissionDeniedException (org.exist.security.PermissionDeniedException)5 XmldbURI (org.exist.xmldb.XmldbURI)5 SAXException (org.xml.sax.SAXException)5 Collection (org.exist.collections.Collection)4 QName (org.exist.dom.QName)4 ACLPermission (org.exist.security.ACLPermission)4 Path (java.nio.file.Path)3 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)3 SAXSerializer (org.exist.util.serializer.SAXSerializer)3 Sequence (org.exist.xquery.value.Sequence)3 AttributesImpl (org.xml.sax.helpers.AttributesImpl)3 Tuple (com.evolvedbinary.j8fu.tuple.Tuple.Tuple)2 Tuple2 (com.evolvedbinary.j8fu.tuple.Tuple2)2 URISyntaxException (java.net.URISyntaxException)2 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)2