use of org.exist.xupdate.Modification in project exist by eXist-db.
the class RpcConnection method xupdateResource.
private int xupdateResource(final XmldbURI docUri, final String xupdate) throws PermissionDeniedException, EXistException {
return withDb((broker, transaction) -> {
final MutableDocumentSet docs = this.<MutableDocumentSet>readDocument(docUri).apply((document, broker1, transaction1) -> {
// TODO : register a lock (which one ?) within the transaction ?
final MutableDocumentSet documentSet = new DefaultDocumentSet();
documentSet.add(document);
return documentSet;
});
try (final Reader reader = new StringReader(xupdate)) {
final XUpdateProcessor processor = new XUpdateProcessor(broker, docs);
final Modification[] modifications = processor.parse(new InputSource(reader));
long mods = 0;
for (final Modification modification : modifications) {
mods += modification.process(transaction);
broker.flush();
}
return (int) mods;
} catch (final XPathException | ParserConfigurationException e) {
throw new EXistException(e);
}
});
}
use of org.exist.xupdate.Modification in project exist by eXist-db.
the class RpcConnection method xupdate.
private int xupdate(final XmldbURI collUri, final String xupdate) throws PermissionDeniedException, EXistException {
return withDb((broker, transaction) -> {
final Collection collectionRef = this.<Collection>readCollection(collUri).apply((collection, broker1, transaction1) -> collection);
// TODO : register a lock (which one ?) in the transaction ?
final DocumentSet docs = collectionRef.allDocs(broker, new DefaultDocumentSet(), true);
try (final Reader reader = new StringReader(xupdate)) {
final XUpdateProcessor processor = new XUpdateProcessor(broker, docs);
final Modification[] modifications = processor.parse(new InputSource(reader));
long mods = 0;
for (final Modification modification : modifications) {
mods += modification.process(transaction);
broker.flush();
}
return (int) mods;
} catch (final XPathException | ParserConfigurationException e) {
throw new EXistException(e);
}
});
}
use of org.exist.xupdate.Modification in project exist by eXist-db.
the class LocalXUpdateQueryService method updateResource.
@Override
public long updateResource(final String id, final String commands) throws XMLDBException {
return this.<Long>withDb((broker, transaction) -> {
final long start = System.currentTimeMillis();
final MutableDocumentSet docs = this.<MutableDocumentSet>read(broker, transaction, collection.getPathURI()).apply((collection, broker1, transaction1) -> {
MutableDocumentSet d = new DefaultDocumentSet();
if (id == null) {
d = collection.allDocs(broker1, d, true);
} else {
try {
final XmldbURI resourceURI = XmldbURI.xmldbUriFor(id);
try (final LockedDocument lockedDocument = collection.getDocumentWithLock(broker1, resourceURI, Lock.LockMode.READ_LOCK)) {
// NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
collection.close();
final DocumentImpl doc = lockedDocument == null ? null : lockedDocument.getDocument();
if (doc == null) {
throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "Resource not found: " + id);
}
d.add(doc);
}
} catch (final URISyntaxException e) {
throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
}
}
return d;
});
try (final Reader reader = new StringReader(commands)) {
if (processor == null) {
processor = new XUpdateProcessor(broker, docs);
} else {
processor.setBroker(broker);
processor.setDocumentSet(docs);
}
final Modification[] modifications = processor.parse(new InputSource(reader));
long mods = 0;
for (Modification modification : modifications) {
mods += modification.process(transaction);
broker.flush();
}
if (LOG.isDebugEnabled()) {
LOG.debug("xupdate took {}ms.", System.currentTimeMillis() - start);
}
return mods;
} catch (final ParserConfigurationException | SAXException | LockException e) {
throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
} finally {
if (processor != null) {
processor.reset();
}
}
});
}
use of org.exist.xupdate.Modification in project exist by eXist-db.
the class UpdateRecoverTest method store.
private void store(final BrokerPool pool) throws EXistException, PermissionDeniedException, IOException, SAXException, LockException, ParserConfigurationException, XPathException {
final TransactionManager transact = pool.getTransactionManager();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
DocumentImpl doc;
try (final Txn transaction = transact.beginTransaction()) {
final Collection root = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI);
assertNotNull(root);
broker.saveCollection(transaction, root);
final Collection test2 = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI2);
assertNotNull(test2);
broker.saveCollection(transaction, test2);
broker.storeDocument(transaction, TestConstants.TEST_XML_URI, new StringInputSource(TEST_XML), MimeType.XML_TYPE, test2);
doc = test2.getDocument(broker, TestConstants.TEST_XML_URI);
// TODO : unlock the collection here ?
transact.commit(transaction);
}
try (final Txn transaction = transact.beginTransaction()) {
final MutableDocumentSet docs = new DefaultDocumentSet();
docs.add(doc);
final XUpdateProcessor proc = new XUpdateProcessor(broker, docs);
assertNotNull(proc);
// insert some nodes
for (int i = 1; i <= 200; i++) {
final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + " <xu:insert-before select=\"/products/product[1]\">" + " <product>" + " <description>Product " + i + "</description>" + " <price>" + (i * 2.5) + "</price>" + " <stock>" + (i * 10) + "</stock>" + " </product>" + " </xu:insert-before>" + "</xu:modifications>";
proc.setBroker(broker);
proc.setDocumentSet(docs);
final Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
modifications[0].process(transaction);
proc.reset();
}
// add attribute
for (int i = 1; i <= 200; i++) {
final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + " <xu:append select=\"/products/product[" + i + "]\">" + " <xu:attribute name=\"id\">" + i + "</xu:attribute>" + " </xu:append>" + "</xu:modifications>";
proc.setBroker(broker);
proc.setDocumentSet(docs);
final Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
modifications[0].process(transaction);
proc.reset();
}
// replace some
for (int i = 1; i <= 100; i++) {
final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + " <xu:replace select=\"/products/product[" + i + "]\">" + " <product id=\"" + i + "\">" + " <description>Replaced product</description>" + " <price>" + (i * 0.75) + "</price>" + " </product>" + " </xu:replace>" + "</xu:modifications>";
proc.setBroker(broker);
proc.setDocumentSet(docs);
final Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
long mods = modifications[0].process(transaction);
proc.reset();
}
// remove some
for (int i = 1; i <= 100; i++) {
final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + " <xu:remove select=\"/products/product[last()]\"/>" + "</xu:modifications>";
proc.setBroker(broker);
proc.setDocumentSet(docs);
final Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
modifications[0].process(transaction);
proc.reset();
}
for (int i = 1; i <= 100; i++) {
final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + " <xu:append select=\"/products\">" + " <product>" + " <xu:attribute name=\"id\"><xu:value-of select=\"count(/products/product) + 1\"/></xu:attribute>" + " <description>Product " + i + "</description>" + " <price>" + (i * 2.5) + "</price>" + " <stock>" + (i * 10) + "</stock>" + " </product>" + " </xu:append>" + "</xu:modifications>";
proc.setBroker(broker);
proc.setDocumentSet(docs);
final Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
modifications[0].process(transaction);
proc.reset();
}
// rename element "description" to "descript"
String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + " <xu:rename select=\"/products/product/description\">descript</xu:rename>" + "</xu:modifications>";
proc.setBroker(broker);
proc.setDocumentSet(docs);
Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
modifications[0].process(transaction);
proc.reset();
// update attribute values
for (int i = 1; i <= 200; i++) {
xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + " <xu:update select=\"/products/product[" + i + "]/@id\">" + i + "u</xu:update>" + "</xu:modifications>";
proc.setBroker(broker);
proc.setDocumentSet(docs);
modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
long mods = modifications[0].process(transaction);
proc.reset();
}
// append new element to records
for (int i = 1; i <= 200; i++) {
xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + " <xu:append select=\"/products/product[" + i + "]\">" + " <date><xu:value-of select=\"current-dateTime()\"/></date>" + " </xu:append>" + "</xu:modifications>";
proc.setBroker(broker);
proc.setDocumentSet(docs);
modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
modifications[0].process(transaction);
proc.reset();
}
// update element content
for (int i = 1; i <= 200; i++) {
xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + " <xu:update select=\"/products/product[" + i + "]/price\">19.99</xu:update>" + "</xu:modifications>";
proc.setBroker(broker);
proc.setDocumentSet(docs);
modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
long mods = modifications[0].process(transaction);
proc.reset();
}
transact.commit(transaction);
}
}
}
use of org.exist.xupdate.Modification in project exist by eXist-db.
the class RangeIndexUpdateTest method updates.
@Test
public void updates() throws EXistException, PermissionDeniedException, XPathException, ParserConfigurationException, IOException, SAXException, LockException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
final TransactionManager transact = pool.getTransactionManager();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = transact.beginTransaction()) {
checkIndex(broker, docs, ITEM_QNAME, new StringValue("Chair"), 1);
checkIndex(broker, docs, ITEM_QNAME, new StringValue("Table892.25"), 1);
checkIndex(broker, docs, ITEM_QNAME, new StringValue("Cabinet1525.00"), 1);
final XQuery xquery = pool.getXQueryService();
assertNotNull(xquery);
final Sequence seq = xquery.execute(broker, "//item[. = 'Chair']", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
final XUpdateProcessor proc = new XUpdateProcessor(broker, docs);
assertNotNull(proc);
proc.setBroker(broker);
proc.setDocumentSet(docs);
String xupdate = XUPDATE_START + " <xu:update select=\"//item[@id = '1']/description\">Wardrobe</xu:update>" + XUPDATE_END;
Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
long mods = modifications[0].process(transaction);
proc.reset();
assertEquals(1, mods);
checkIndex(broker, docs, ITEM_QNAME, new StringValue("Chair"), 0);
checkIndex(broker, docs, ITEM_QNAME, new StringValue("Wardrobe"), 1);
proc.setBroker(broker);
proc.setDocumentSet(docs);
xupdate = XUPDATE_START + " <xu:update select=\"//item[@id = '1']/description/text()\">Wheelchair</xu:update>" + XUPDATE_END;
modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
mods = modifications[0].process(transaction);
proc.reset();
assertEquals(1, mods);
checkIndex(broker, docs, ITEM_QNAME, new StringValue("Wardrobe"), 0);
checkIndex(broker, docs, ITEM_QNAME, new StringValue("Wheelchair"), 1);
proc.setBroker(broker);
proc.setDocumentSet(docs);
xupdate = XUPDATE_START + " <xu:update select=\"//item[@id = '1']/@attr\">abc</xu:update>" + XUPDATE_END;
modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
mods = modifications[0].process(transaction);
proc.reset();
assertEquals(1, mods);
checkIndex(broker, docs, null, new StringValue("abc"), 1);
transact.commit(transaction);
}
}
Aggregations