use of org.exist.EXistException in project exist by eXist-db.
the class SortIndexWorker method getId.
private short getId(final String name) throws EXistException, LockException {
final byte[] key = new byte[1 + UTF8.encoded(name)];
key[0] = 1;
UTF8.encode(name, key, 1);
try (final ManagedLock<ReentrantLock> btreeLock = lockManager.acquireBtreeReadLock(index.btree.getLockName())) {
return (short) index.btree.findValue(new Value(key));
} catch (final BTreeException | IOException e) {
throw new EXistException("Exception caught while reading sort index: " + e.getMessage(), e);
}
}
use of org.exist.EXistException in project exist by eXist-db.
the class SortIndexWorker method getOrRegisterId.
/**
* Register the given index name and return a short id for it.
*
* @param name the name of the index
*
* @return a unique id to be used for the index entries
*
* @throws EXistException if an error occurs with the database
* @throws LockException if a locking error occurs
*/
private short getOrRegisterId(final String name) throws EXistException, LockException {
short id = getId(name);
if (id < 0) {
final byte[] fromKey = { 1 };
final byte[] endKey = { 2 };
final IndexQuery query = new IndexQuery(IndexQuery.RANGE, new Value(fromKey), new Value(endKey));
try (final ManagedLock<ReentrantLock> btreeLock = lockManager.acquireBtreeWriteLock(index.btree.getLockName())) {
final FindIdCallback callback = new FindIdCallback(false);
index.btree.query(query, callback);
id = (short) (callback.max + 1);
registerId(id, name);
} catch (final IOException | TerminatedException | BTreeException e) {
throw new EXistException("Exception caught while reading sort index: " + e.getMessage(), e);
}
}
return id;
}
use of org.exist.EXistException in project exist by eXist-db.
the class RemoveIndex method eval.
@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
final SortIndexWorker index = (SortIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(SortIndex.ID);
final String id = args[0].getStringValue();
try {
if (getArgumentCount() == 2) {
final NodeValue nv = (NodeValue) args[1].itemAt(0);
if (nv.getImplementationType() == NodeValue.IN_MEMORY_NODE)
throw new XPathException(this, "Second argument to remove should be a persistent node, not " + "an in-memory node.");
final NodeProxy proxy = (NodeProxy) nv;
index.remove(id, proxy.getOwnerDocument());
} else {
index.remove(id);
}
} catch (final EXistException e) {
throw new XPathException(this, e.getMessage(), e);
} catch (final LockException e) {
throw new XPathException(this, "Caught lock error while removing index. Giving up.", e);
}
return Sequence.EMPTY_SEQUENCE;
}
use of org.exist.EXistException in project exist by eXist-db.
the class GetIndex method eval.
@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
if (args[1].isEmpty())
return Sequence.EMPTY_SEQUENCE;
final String id = args[0].getStringValue();
final NodeProxy node = (NodeProxy) args[1].itemAt(0);
final SortIndexWorker index = (SortIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(SortIndex.ID);
long pos = 0;
try {
pos = index.getIndex(id, node);
} catch (final EXistException e) {
throw new XPathException(this, e.getMessage(), e);
} catch (final LockException e) {
throw new XPathException(this, "Caught lock error while searching index. Giving up.", e);
}
return pos < 0 ? Sequence.EMPTY_SEQUENCE : new IntegerValue(pos, Type.LONG);
}
use of org.exist.EXistException 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();
}
Aggregations