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();
}
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);
}
}
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.");
}
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();
}
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();
}
}
}
Aggregations