Search in sources :

Example 61 with DocumentImpl

use of org.exist.dom.persistent.DocumentImpl in project exist by eXist-db.

the class FluentBrokerAPITest method collectionThenCollectionAndDoc.

@Test
public void collectionThenCollectionAndDoc() throws PermissionDeniedException, EXistException, LockException {
    final XmldbURI docUri = uri("all-test.xml");
    final long collectionCreated = 1234;
    final IMocksControl ctrl = createStrictControl();
    ctrl.checkOrder(true);
    final BrokerPool mockBrokerPool = ctrl.createMock(BrokerPool.class);
    final DBBroker mockBroker = ctrl.createMock(DBBroker.class);
    final Collection mockCollection = ctrl.createMock(Collection.class);
    final LockedDocument mockLockedDocument = ctrl.createMock(LockedDocument.class);
    final DocumentImpl mockDocument = ctrl.createMock(DocumentImpl.class);
    expect(mockBrokerPool.getBroker()).andReturn(mockBroker);
    expect(mockBroker.openCollection(TEST_COLLECTION_URI, READ_LOCK)).andReturn(mockCollection);
    expect(mockCollection.getCreated()).andReturn(collectionCreated);
    expect(mockCollection.getDocumentWithLock(mockBroker, docUri, READ_LOCK)).andReturn(mockLockedDocument);
    expect(mockLockedDocument.getDocument()).andReturn(mockDocument);
    expect(mockCollection.getURI()).andReturn(TEST_COLLECTION_URI);
    expect(mockDocument.getFileURI()).andReturn(docUri);
    // NOTE: checks that Collection lock is release before Document lock
    mockCollection.close();
    mockLockedDocument.close();
    mockBroker.close();
    ctrl.replay();
    final Function<Collection, Long> collectionOp = collection -> collection.getCreated();
    final BiFunction<Collection, DocumentImpl, String> collectionDocOp = (collection, doc) -> collection.getURI().append(doc.getFileURI()).toString();
    final Tuple2<Long, String> result = FluentBrokerAPI.builder(mockBrokerPool).withCollection(TEST_COLLECTION_URI, READ_LOCK).execute(collectionOp).withDocument(collection -> new Tuple2<>(docUri, READ_LOCK)).execute(collectionDocOp).doAll();
    assertEquals(collectionCreated, result._1.longValue());
    assertEquals(TEST_COLLECTION_URI.append(docUri), result._2);
    ctrl.verify();
}
Also used : Tuple3(com.evolvedbinary.j8fu.tuple.Tuple3) Tuple2(com.evolvedbinary.j8fu.tuple.Tuple2) IMocksControl(org.easymock.IMocksControl) LockMode(org.exist.storage.lock.Lock.LockMode) LockedDocument(org.exist.dom.persistent.LockedDocument) EasyMock.createStrictControl(org.easymock.EasyMock.createStrictControl) BiFunction(java.util.function.BiFunction) Test(org.junit.Test) PermissionDeniedException(org.exist.security.PermissionDeniedException) EasyMock.expect(org.easymock.EasyMock.expect) Function(java.util.function.Function) FluentBrokerAPI.uri(org.exist.storage.FluentBrokerAPI.uri) LockException(org.exist.util.LockException) Collection(org.exist.collections.Collection) XmldbURI(org.exist.xmldb.XmldbURI) DocumentImpl(org.exist.dom.persistent.DocumentImpl) EXistException(org.exist.EXistException) Lock(org.exist.storage.lock.Lock) Assert.assertEquals(org.junit.Assert.assertEquals) DocumentImpl(org.exist.dom.persistent.DocumentImpl) IMocksControl(org.easymock.IMocksControl) LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) XmldbURI(org.exist.xmldb.XmldbURI) Test(org.junit.Test)

Example 62 with DocumentImpl

use of org.exist.dom.persistent.DocumentImpl in project exist by eXist-db.

the class RangeIndexWorker method scan.

private void scan(DocumentSet docs, NodeSet nodes, String start, String end, long max, TreeMap<String, Occurrences> map, IndexReader reader, String field) throws IOException {
    List<AtomicReaderContext> leaves = reader.leaves();
    for (AtomicReaderContext context : leaves) {
        NumericDocValues docIdValues = context.reader().getNumericDocValues(FIELD_DOC_ID);
        BinaryDocValues nodeIdValues = context.reader().getBinaryDocValues(FIELD_NODE_ID);
        Bits liveDocs = context.reader().getLiveDocs();
        Terms terms = context.reader().terms(field);
        if (terms == null)
            continue;
        TermsEnum termsIter = terms.iterator(null);
        if (termsIter.next() == null) {
            continue;
        }
        do {
            if (map.size() >= max) {
                break;
            }
            BytesRef ref = termsIter.term();
            String term = ref.utf8ToString();
            boolean include = true;
            if (end != null) {
                if (term.compareTo(end) > 0)
                    include = false;
            } else if (start != null && !term.startsWith(start))
                include = false;
            if (include) {
                DocsEnum docsEnum = termsIter.docs(null, null);
                while (docsEnum.nextDoc() != DocsEnum.NO_MORE_DOCS) {
                    if (liveDocs != null && !liveDocs.get(docsEnum.docID())) {
                        continue;
                    }
                    int docId = (int) docIdValues.get(docsEnum.docID());
                    DocumentImpl storedDocument = docs.getDoc(docId);
                    if (storedDocument == null)
                        continue;
                    NodeId nodeId = null;
                    if (nodes != null) {
                        final BytesRef nodeIdRef = nodeIdValues.get(docsEnum.docID());
                        int units = ByteConversion.byteToShort(nodeIdRef.bytes, nodeIdRef.offset);
                        nodeId = index.getBrokerPool().getNodeFactory().createFromData(units, nodeIdRef.bytes, nodeIdRef.offset + 2);
                    }
                    if (nodeId == null || nodes.get(storedDocument, nodeId) != null) {
                        Occurrences oc = map.get(term);
                        if (oc == null) {
                            oc = new Occurrences(term);
                            map.put(term, oc);
                        }
                        oc.addDocument(storedDocument);
                        oc.addOccurrences(docsEnum.freq());
                    }
                }
            }
        } while (termsIter.next() != null);
    }
}
Also used : Occurrences(org.exist.util.Occurrences) DocumentImpl(org.exist.dom.persistent.DocumentImpl) NodeId(org.exist.numbering.NodeId) Bits(org.apache.lucene.util.Bits) BytesRef(org.apache.lucene.util.BytesRef)

Example 63 with DocumentImpl

use of org.exist.dom.persistent.DocumentImpl in project exist by eXist-db.

the class RangeIndexWorker method removeCollection.

@Override
public void removeCollection(Collection collection, DBBroker broker, boolean reindex) throws PermissionDeniedException {
    if (LOG.isDebugEnabled())
        LOG.debug("Removing collection {}", collection.getURI());
    IndexWriter writer = null;
    try {
        writer = index.getWriter();
        for (Iterator<DocumentImpl> i = collection.iterator(broker); i.hasNext(); ) {
            DocumentImpl doc = i.next();
            final BytesRefBuilder bytes = new BytesRefBuilder();
            NumericUtils.intToPrefixCoded(doc.getDocId(), 0, bytes);
            Term dt = new Term(FIELD_DOC_ID, bytes.toBytesRef());
            writer.deleteDocuments(dt);
        }
    } catch (IOException | PermissionDeniedException | LockException e) {
        LOG.error("Error while removing lucene index: {}", e.getMessage(), e);
    } finally {
        index.releaseWriter(writer);
        if (reindex) {
            try {
                index.sync();
            } catch (DBException e) {
                LOG.warn("Exception during reindex: {}", e.getMessage(), e);
            }
        }
        mode = ReindexMode.STORE;
    }
    if (LOG.isDebugEnabled())
        LOG.debug("Collection removed.");
}
Also used : DBException(org.exist.storage.btree.DBException) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) LockException(org.exist.util.LockException) PermissionDeniedException(org.exist.security.PermissionDeniedException) IOException(java.io.IOException) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Example 64 with DocumentImpl

use of org.exist.dom.persistent.DocumentImpl in project exist by eXist-db.

the class RewriteConfig method configure.

private void configure(final String controllerConfig) throws ServletException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Loading XQueryURLRewrite configuration from {}", controllerConfig);
    }
    if (controllerConfig.startsWith(XmldbURI.XMLDB_URI_PREFIX)) {
        try (final DBBroker broker = urlRewrite.getBrokerPool().get(Optional.ofNullable(urlRewrite.getDefaultUser()))) {
            try (final LockedDocument lockedDocument = broker.getXMLResource(XmldbURI.create(controllerConfig), LockMode.READ_LOCK)) {
                final DocumentImpl doc = lockedDocument == null ? null : lockedDocument.getDocument();
                if (doc != null) {
                    parse(doc);
                }
            }
        } catch (final EXistException | PermissionDeniedException e) {
            throw new ServletException("Failed to parse controller.xml: " + e.getMessage(), e);
        }
    } else {
        try {
            final Path d = Paths.get(urlRewrite.getConfig().getServletContext().getRealPath("/")).normalize();
            final Path configFile = d.resolve(controllerConfig);
            if (Files.isReadable(configFile)) {
                final Document doc = parseConfig(configFile);
                parse(doc);
            }
        } catch (final ParserConfigurationException | IOException | SAXException e) {
            throw new ServletException("Failed to parse controller.xml: " + e.getMessage(), e);
        }
    }
    urlRewrite.clearCaches();
}
Also used : Path(java.nio.file.Path) EXistException(org.exist.EXistException) IOException(java.io.IOException) Document(org.w3c.dom.Document) LockedDocument(org.exist.dom.persistent.LockedDocument) DocumentImpl(org.exist.dom.persistent.DocumentImpl) SAXException(org.xml.sax.SAXException) ServletException(javax.servlet.ServletException) DBBroker(org.exist.storage.DBBroker) LockedDocument(org.exist.dom.persistent.LockedDocument) PermissionDeniedException(org.exist.security.PermissionDeniedException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 65 with DocumentImpl

use of org.exist.dom.persistent.DocumentImpl in project exist by eXist-db.

the class XQueryURLRewrite method findSourceFromDb.

@Nullable
private SourceInfo findSourceFromDb(final DBBroker broker, final String basePath, final String path, final String[] components) {
    LockedDocument lockedControllerDoc = null;
    try {
        final XmldbURI locationUri = XmldbURI.xmldbUriFor(basePath);
        XmldbURI resourceUri = locationUri;
        for (final String component : components) {
            resourceUri = resourceUri.append(component);
        }
        lockedControllerDoc = findDbControllerXql(broker, locationUri, resourceUri);
        if (lockedControllerDoc == null) {
            LOG.warn("XQueryURLRewrite controller could not be found for path: {}", path);
            return null;
        }
        final DocumentImpl controllerDoc = lockedControllerDoc.getDocument();
        if (LOG.isTraceEnabled()) {
            LOG.trace("Found controller file: {}", controllerDoc.getURI());
        }
        if (controllerDoc.getResourceType() != DocumentImpl.BINARY_FILE || !"application/xquery".equals(controllerDoc.getMimeType())) {
            LOG.warn("XQuery resource: {} is not an XQuery or declares a wrong mime-type", query);
            return null;
        }
        final String controllerPath = controllerDoc.getCollection().getURI().getRawCollectionPath();
        return new SourceInfo(new DBSource(broker, (BinaryDocument) controllerDoc, true), "xmldb:exist://" + controllerPath, controllerPath.substring(locationUri.getCollectionPath().length()));
    } catch (final URISyntaxException e) {
        LOG.warn("Bad URI for base path: {}", e.getMessage(), e);
        return null;
    } finally {
        if (lockedControllerDoc != null) {
            lockedControllerDoc.close();
        }
    }
}
Also used : BinaryDocument(org.exist.dom.persistent.BinaryDocument) LockedDocument(org.exist.dom.persistent.LockedDocument) DBSource(org.exist.source.DBSource) URISyntaxException(java.net.URISyntaxException) DocumentImpl(org.exist.dom.persistent.DocumentImpl) XmldbURI(org.exist.xmldb.XmldbURI) Nullable(javax.annotation.Nullable)

Aggregations

DocumentImpl (org.exist.dom.persistent.DocumentImpl)128 PermissionDeniedException (org.exist.security.PermissionDeniedException)51 Collection (org.exist.collections.Collection)40 LockedDocument (org.exist.dom.persistent.LockedDocument)39 Txn (org.exist.storage.txn.Txn)35 XmldbURI (org.exist.xmldb.XmldbURI)34 EXistException (org.exist.EXistException)27 LockException (org.exist.util.LockException)26 DBBroker (org.exist.storage.DBBroker)25 IOException (java.io.IOException)19 NodeProxy (org.exist.dom.persistent.NodeProxy)18 URISyntaxException (java.net.URISyntaxException)17 BrokerPool (org.exist.storage.BrokerPool)17 TransactionManager (org.exist.storage.txn.TransactionManager)17 Test (org.junit.Test)17 XPathException (org.exist.xquery.XPathException)16 NodeId (org.exist.numbering.NodeId)14 SAXException (org.xml.sax.SAXException)13 StoredNode (org.exist.dom.persistent.StoredNode)12 BinaryDocument (org.exist.dom.persistent.BinaryDocument)11