use of org.exist.dom.persistent.DocumentImpl in project exist by eXist-db.
the class HistoryTriggerTest method checkHistoryOfOriginal.
private void checkHistoryOfOriginal(final BrokerPool brokerPool, final XmldbURI originalDocName, final String orginalDocContent) throws EXistException, PermissionDeniedException, LockException {
try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()));
final Txn transaction = brokerPool.getTransactionManager().beginTransaction()) {
try (final Collection historyCollection = broker.openCollection(HistoryTrigger.DEFAULT_ROOT_PATH.append(TEST_COLLECTION_URI).append(originalDocName), Lock.LockMode.READ_LOCK)) {
assertNotNull(historyCollection);
final DocumentSet documentSet = historyCollection.getDocuments(broker, new DefaultDocumentSet());
assertEquals(1, documentSet.getDocumentCount());
final Iterator<DocumentImpl> it = documentSet.getDocumentIterator();
assertTrue(it.hasNext());
final DocumentImpl doc = it.next();
final Diff diff = DiffBuilder.compare(Input.from(orginalDocContent)).withTest(Input.from(doc)).build();
assertFalse(diff.toString(), diff.hasDifferences());
assertFalse(it.hasNext());
}
transaction.commit();
}
}
use of org.exist.dom.persistent.DocumentImpl in project exist by eXist-db.
the class NativeStructuralIndexWorkerTest method documentIdSet.
private DocumentSet documentIdSet(final List<Integer> documentIds) {
final DocumentSet mockDocumentSet = createMock(DocumentSet.class);
final List<DocumentImpl> docs = documentIds.stream().map(id -> {
final DocumentImpl mockDocument = createMock(DocumentImpl.class);
expect(mockDocument.getDocId()).andReturn(id).anyTimes();
return mockDocument;
}).collect(Collectors.toList());
expect(mockDocumentSet.getDocumentIterator()).andReturn(docs.iterator());
replay(mockDocumentSet);
docs.forEach(EasyMock::replay);
return mockDocumentSet;
}
use of org.exist.dom.persistent.DocumentImpl in project exist by eXist-db.
the class IntersectTest method memtree_intersect_persistent.
/**
* Tests the XQuery `intersect` operator against an
* in-memory node on the left and a persistent node on the right
*/
@Test
public void memtree_intersect_persistent() throws XPathException, NoSuchMethodException {
final XQueryContext mockContext = createMock(XQueryContext.class);
final PathExpr mockLeft = createMock(PathExpr.class);
final PathExpr mockRight = createMock(PathExpr.class);
final Sequence mockContextSequence = createMock(Sequence.class);
final Item mockContextItem = createMock(Item.class);
final Profiler mockProfiler = createMock(Profiler.class);
final DocumentImpl mockPersistentDoc = createMock(DocumentImpl.class);
final NodeProxy mockPersistentNode = createMockBuilder(NodeProxy.class).withConstructor(DocumentImpl.class, NodeId.class).withArgs(mockPersistentDoc, new DLN(1)).addMockedMethods(NodeProxy.class.getMethod("isEmpty", new Class[] {}), NodeProxy.class.getMethod("getItemType", new Class[] {}), NodeProxy.class.getMethod("equals", new Class[] { Object.class })).createMock();
expect(mockContext.nextExpressionId()).andReturn(1);
expect(mockContext.getProfiler()).andReturn(mockProfiler);
// memtree node
expect(mockLeft.eval(mockContextSequence, mockContextItem)).andReturn((org.exist.dom.memtree.ElementImpl) createInMemoryDocument().getDocumentElement());
// persistent node
expect(mockRight.eval(mockContextSequence, mockContextItem)).andReturn(mockPersistentNode);
expect(mockPersistentNode.isEmpty()).andReturn(false);
expect(mockPersistentNode.getItemType()).andReturn(Type.NODE);
expect(mockContext.getProfiler()).andReturn(mockProfiler);
replay(mockPersistentDoc, mockPersistentNode, mockRight, mockLeft, mockContext);
// test
final Intersect intersect = new Intersect(mockContext, mockLeft, mockRight);
final Sequence result = intersect.eval(mockContextSequence, mockContextItem);
assertEquals(0, ((ValueSequence) result).size());
verify(mockPersistentDoc, mockPersistentNode, mockRight, mockLeft, mockContext);
}
use of org.exist.dom.persistent.DocumentImpl in project exist by eXist-db.
the class Index method eval.
@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
try {
// Retrieve Lucene
LuceneIndexWorker index = (LuceneIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(LuceneIndex.ID);
if (isCalledAs("index")) {
// Get first parameter, this is the document
String path = args[0].itemAt(0).getStringValue();
// Retrieve document from database
try (final LockedDocument lockedDoc = context.getBroker().getXMLResource(XmldbURI.xmldbUriFor(path), LockMode.READ_LOCK)) {
// Verify the document actually exists
final DocumentImpl doc = lockedDoc == null ? null : lockedDoc.getDocument();
if (doc == null) {
throw new XPathException(this, "Document " + path + " does not exist.");
}
boolean flush = args.length == 2 || args[2].effectiveBooleanValue();
// Note: code order is important here,
index.setDocument(doc, ReindexMode.STORE);
index.setMode(ReindexMode.STORE);
// Get 'solr' node from second parameter
NodeValue descriptor = (NodeValue) args[1].itemAt(0);
// Pas document and index instructions to indexer
index.indexNonXML(descriptor);
if (flush) {
// Make sure things are written
index.writeNonXML();
}
}
} else {
// "close"
index.writeNonXML();
}
} catch (Exception ex) {
// PermissionDeniedException
logger.error(ex.getMessage(), ex);
throw new XPathException(this, ex);
}
// Return nothing [status would be nice]
return Sequence.EMPTY_SEQUENCE;
}
use of org.exist.dom.persistent.DocumentImpl in project exist by eXist-db.
the class ExistDocument method unlock.
/**
* Unlock document in database.
*/
void unlock() throws PermissionDeniedException, DocumentNotLockedException, EXistException {
if (LOG.isDebugEnabled()) {
LOG.debug("unlock {}", xmldbUri);
}
final TransactionManager txnManager = brokerPool.getTransactionManager();
// Try to get document
try (final DBBroker broker = brokerPool.get(Optional.ofNullable(subject));
final Txn txn = txnManager.beginTransaction();
final LockedDocument lockedDocument = broker.getXMLResource(xmldbUri, LockMode.WRITE_LOCK)) {
final DocumentImpl document = lockedDocument.getDocument();
if (document == null) {
final String msg = String.format("No resource found for path: %s", xmldbUri);
LOG.debug(msg);
throw new EXistException(msg);
}
// Get current userlock
Account lock = document.getUserLock();
// Check if Resource is already locked.
if (lock == null) {
LOG.debug("Resource {} is not locked.", xmldbUri);
throw new DocumentNotLockedException("" + xmldbUri);
}
// Check if Resource is from subject
if (!lock.getName().equals(subject.getName()) && !subject.hasDbaRole()) {
LOG.debug("Resource lock is from user {}", lock.getName());
throw new PermissionDeniedException(lock.getName());
}
// Update document
document.setUserLock(null);
document.setLockToken(null);
// Make it persistant
broker.storeMetadata(txn, document);
txnManager.commit(txn);
} catch (EXistException | PermissionDeniedException e) {
LOG.error(e);
throw e;
} catch (TriggerException e) {
LOG.error(e);
throw new EXistException(e);
} finally {
if (LOG.isDebugEnabled()) {
LOG.debug("Finished create lock");
}
}
}
Aggregations