use of org.sirix.xquery.node.DBStore in project sirix by sirixdb.
the class Load method execute.
@Override
public Sequence execute(final StaticContext sctx, final QueryContext ctx, final Sequence[] args) throws QueryException {
try {
final String collName = FunUtil.getString(args, 0, "collName", "collection", null, true);
final Sequence resources = args[2];
if (resources == null)
throw new QueryException(new QNm("No sequence of resources specified!"));
final boolean createNew = args.length == 4 ? args[3].booleanValue() : true;
final String resName = FunUtil.getString(args, 1, "resName", "resource", null, createNew ? false : true);
final DBStore store = (DBStore) ctx.getStore();
DBCollection coll;
if (createNew) {
coll = (DBCollection) create(store, collName, resName, resources);
} else {
try {
coll = (DBCollection) store.lookup(collName);
add(store, coll, resName, resources);
} catch (DocumentException e) {
// collection does not exist
coll = (DBCollection) create(store, collName, resName, resources);
}
}
return coll;
} catch (final Exception e) {
throw new QueryException(new QNm(e.getMessage()), e);
}
}
use of org.sirix.xquery.node.DBStore in project sirix by sirixdb.
the class Store method execute.
@Override
public Sequence execute(final StaticContext sctx, final QueryContext ctx, final Sequence[] args) throws QueryException {
try {
final String collName = FunUtil.getString(args, 0, "collName", "collection", null, true);
final Sequence nodes = args[2];
if (nodes == null)
throw new QueryException(new QNm("No sequence of nodes specified!"));
final boolean createNew = args.length == 4 ? args[3].booleanValue() : true;
final String resName = FunUtil.getString(args, 1, "resName", "resource", null, createNew ? false : true);
final DBStore store = (DBStore) ctx.getStore();
if (createNew) {
create(store, collName, resName, nodes);
} else {
try {
final DBCollection coll = (DBCollection) store.lookup(collName);
add(store, coll, resName, nodes);
} catch (DocumentException e) {
// collection does not exist
create(store, collName, resName, nodes);
}
}
return null;
} catch (final Exception e) {
throw new QueryException(new QNm(e.getMessage()), e);
}
}
use of org.sirix.xquery.node.DBStore in project sirix by sirixdb.
the class XQueryUsage method loadDocumentAndQueryTemporal.
/**
* Load a document and query it (temporal enhancements).
*/
private static void loadDocumentAndQueryTemporal() throws QueryException, IOException, SirixException {
// Initialize query context and store (implicit transaction commit).
try (final DBStore store = DBStore.newBuilder().build()) {
final QueryContext ctx1 = new SirixQueryContext(store);
final CompileChain compileChain = new SirixCompileChain(store);
final Path doc1 = generateSampleDoc("sample1");
final URI docUri = doc1.toUri();
// Use XQuery to load sample document into store.
System.out.println("Loading document:");
final String xq1 = String.format("sdb:load('mydocs.col', 'resource1', '%s')", docUri.toString());
System.out.println(xq1);
new XQuery(compileChain, xq1).evaluate(ctx1);
}
try (final DBStore store = DBStore.newBuilder().build()) {
final QueryContext ctx = new QueryContext(store);
System.out.println();
System.out.println("Query loaded document:");
final String xq3 = "let $doc:= doc('mydocs.col')/log return sdb:select-node($doc, 7) ";
System.out.println(xq3);
XQuery q = new XQuery(new SirixCompileChain(store), xq3);
q.prettyPrint();
q.serialize(ctx, System.out);
}
try (final DBStore store = DBStore.newBuilder().build()) {
final QueryContext ctx = new SirixQueryContext(store);
System.out.println();
System.out.println("Query loaded document:");
final String xq3 = "doc('mydocs.col')/log/all-time::*";
System.out.println(xq3);
XQuery q = new XQuery(new SirixCompileChain(store), xq3);
q.prettyPrint();
q.serialize(ctx, System.out);
}
// Create and commit CAS indexes on all attribute- and text-nodes.
try (final DBStore store = DBStore.newBuilder().build()) {
final QueryContext ctx3 = new QueryContext(store);
System.out.println();
System.out.println("Create a cas index for all attributes and another one for text-nodes. A third one is created for all integers:");
final XQuery q = new XQuery(new SirixCompileChain(store), "let $doc := sdb:doc('mydocs.col', 'resource1', (), fn:boolean(1)) " + "let $casStats1 := sdb:create-cas-index($doc, 'xs:string', '//@*') " + "let $casStats2 := sdb:create-cas-index($doc, 'xs:string', '//*') " + "let $casStats3 := sdb:create-cas-index($doc, 'xs:integer', '//*') " + "return <rev>{sdb:commit($doc)}</rev>");
q.serialize(ctx3, System.out);
System.out.println();
System.out.println("CAS index creation done.");
}
// Create and commit path index on all elements.
try (final DBStore store = DBStore.newBuilder().build()) {
final QueryContext ctx3 = new QueryContext(store);
System.out.println();
System.out.println("Create path index for all elements (all paths):");
final XQuery q = new XQuery(new SirixCompileChain(store), "let $doc := sdb:doc('mydocs.col', 'resource1', (), fn:boolean(1)) " + "let $stats := sdb:create-path-index($doc, '//*') " + "return <rev>{sdb:commit($doc)}</rev>");
q.serialize(ctx3, System.out);
System.out.println();
System.out.println("Path index creation done.");
}
// Create and commit name index on all elements with QName 'src' or 'msg'.
try (final DBStore store = DBStore.newBuilder().build()) {
final QueryContext ctx3 = new SirixQueryContext(store, CommitStrategy.EXPLICIT);
System.out.println();
System.out.println("Create name index for all elements with name 'src' or 'msg':");
final XQuery q = new XQuery(new SirixCompileChain(store), "let $doc := sdb:doc('mydocs.col', 'resource1', (), fn:boolean(1)) " + "let $stats := sdb:create-name-index($doc, fn:QName((), 'src')) " + "return <rev>{sdb:commit($doc)}</rev>");
q.serialize(ctx3, System.out);
System.out.println();
System.out.println("Name index creation done.");
}
// Query CAS index.
try (final DBStore store = DBStore.newBuilder().build()) {
System.out.println("");
System.out.println("Find CAS index for all attribute values.");
final QueryContext ctx3 = new SirixQueryContext(store);
final String query = "let $doc := sdb:doc('mydocs.col', 'resource1') return sdb:scan-cas-index($doc, sdb:find-cas-index($doc, 'xs:string', '//@*'), 'bar', true(), 0, ())";
final Sequence seq = new XQuery(new SirixCompileChain(store), query).execute(ctx3);
// final Iter iter = seq.iterate();
// for (Item item = iter.next(); item != null; item = iter.next()) {
// System.out.println(item);
// }
final Comparator<Tuple> comparator = (o1, o2) -> ((Node<?>) o1).cmp((Node<?>) o2);
final Sequence sortedSeq = new SortedNodeSequence(comparator, seq, true);
final Iter sortedIter = sortedSeq.iterate();
System.out.println("Sorted index entries in document order: ");
for (Item item = sortedIter.next(); item != null; item = sortedIter.next()) {
System.out.println(item);
}
}
// Query CAS index.
try (final DBStore store = DBStore.newBuilder().build()) {
System.out.println("");
System.out.println("Find CAS index for all text values which are integers between 10 and 100.");
final QueryContext ctx3 = new SirixQueryContext(store);
final String query = "let $doc := sdb:doc('mydocs.col', 'resource1') return sdb:scan-cas-index-range($doc, sdb:find-cas-index($doc, 'xs:integer', '//*'), 10, 100, true(), true(), ())";
final Sequence seq = new XQuery(new SirixCompileChain(store), query).execute(ctx3);
// final Iter iter = seq.iterate();
// for (Item item = iter.next(); item != null; item = iter.next()) {
// System.out.println(item);
// }
final Comparator<Tuple> comparator = (o1, o2) -> ((Node<?>) o1).cmp((Node<?>) o2);
final Sequence sortedSeq = new SortedNodeSequence(comparator, seq, true);
final Iter sortedIter = sortedSeq.iterate();
System.out.println("Sorted index entries in document order: ");
for (Item item = sortedIter.next(); item != null; item = sortedIter.next()) {
System.out.println(item);
}
}
// Query path index which are children of the log-element (only elements).
try (final DBStore store = DBStore.newBuilder().build()) {
System.out.println("");
System.out.println("Find path index for all elements which are children of the log-element (only elements).");
final QueryContext ctx3 = new SirixQueryContext(store);
final DBNode node = (DBNode) new XQuery(new SirixCompileChain(store), "doc('mydocs.col')").execute(ctx3);
final Optional<IndexDef> index = node.getTrx().getResourceManager().getRtxIndexController(node.getTrx().getRevisionNumber()).getIndexes().findPathIndex(org.brackit.xquery.util.path.Path.parse("//log/*"));
System.out.println(index);
// last param '()' queries whole index.
final String query = "let $doc := sdb:doc('mydocs.col', 'resource1') " + "return sdb:scan-path-index($doc, " + index.get().getID() + ", '//log/*')";
final Sequence seq = new XQuery(new SirixCompileChain(store), query).execute(ctx3);
final Comparator<Tuple> comparator = (o1, o2) -> ((Node<?>) o1).cmp((Node<?>) o2);
final Sequence sortedSeq = new SortedNodeSequence(comparator, seq, true);
final Iter sortedIter = sortedSeq.iterate();
System.out.println("Sorted index entries in document order: ");
for (Item item = sortedIter.next(); item != null; item = sortedIter.next()) {
System.out.println(item);
}
}
// Query name index.
try (final DBStore store = DBStore.newBuilder().build()) {
System.out.println("");
System.out.println("Query name index (src-element).");
final QueryContext ctx3 = new QueryContext(store);
final String query = "let $doc := sdb:doc('mydocs.col', 'resource1')" + " let $sequence := sdb:scan-name-index($doc, sdb:find-name-index($doc, fn:QName((), 'src')), fn:QName((), 'src'))" + " return sdb:sort($sequence)";
final XQuery q = new XQuery(new SirixCompileChain(store), query);
q.prettyPrint();
q.serialize(ctx3, System.out);
}
try (final DBStore store = DBStore.newBuilder().build()) {
final QueryContext ctx = new SirixQueryContext(store);
System.out.println();
System.out.println("Query loaded document:");
final String xq3 = "doc('mydocs.col')/log/all-time::*";
System.out.println(xq3);
XQuery q = new XQuery(new SirixCompileChain(store), xq3);
q.prettyPrint();
q.serialize(ctx, System.out);
// Serialize first version to XML
// ($user.home$/sirix-data/output-revision-1.xml).
final QueryContext ctx4 = new QueryContext(store);
final String xq4 = "doc('mydocs.col', 1)";
q = new XQuery(xq4);
try (final PrintStream out = new PrintStream(new FileOutputStream(LOCATION.resolve("output-revision-1.xml").toFile()))) {
q.prettyPrint().serialize(ctx4, out);
}
System.out.println();
// Serialize second version to XML
// ($user.home$/sirix-data/output-revision-1.xml).
final QueryContext ctx5 = new SirixQueryContext(store);
final String xq5 = "sdb:serialize(doc('mydocs.col', 2), fn:boolean(1), 'output-revision-2.xml')";
q = new XQuery(xq5);
q.execute(ctx5);
System.out.println();
// Serialize first, second and third version to XML
// ($user.home$/sirix-data/output-revisions.xml).
final QueryContext ctx6 = new SirixQueryContext(store);
final String xq6 = "for $i in ((doc('mydocs.col', 1), doc('mydocs.col', 2), doc('mydocs.col', 3))) return $i";
q = new XQuery(xq6);
try (final PrintStream out = new PrintStream(new FileOutputStream(LOCATION.resolve("output-revisions.xml").toFile()))) {
q.prettyPrint().serialize(ctx6, out);
}
System.out.println();
}
try (final DBStore store = DBStore.newBuilder().build()) {
final Path doc = Paths.get("src", "main", "resources", "test.xml");
final QueryContext ctx = new SirixQueryContext(store);
System.out.println();
final String xq3 = String.format("sdb:load('mycoll.col', 'mydoc.xml', '%s')", doc.toUri().toString());
System.out.println(xq3);
final XQuery q = new XQuery(new SirixCompileChain(store), xq3);
q.execute(ctx);
}
}
use of org.sirix.xquery.node.DBStore in project sirix by sirixdb.
the class XQueryUsage method loadDocumentAndUpdate.
/**
* Load a document and update it.
*/
private static void loadDocumentAndUpdate() throws QueryException, IOException {
// Prepare sample document.
final Path doc = generateSampleDoc("sample");
// Initialize query context and store.
try (final DBStore store = DBStore.newBuilder().build()) {
final QueryContext ctx = new SirixQueryContext(store);
// Use XQuery to load sample document into store.
System.out.println("Loading document:");
URI docUri = doc.toUri();
final String xq1 = String.format("sdb:load('mycol.xml', 'mydoc.xml', '%s')", docUri.toString());
System.out.println(xq1);
new XQuery(xq1).evaluate(ctx);
// Reuse store and query loaded document.
final QueryContext ctx2 = new SirixQueryContext(store);
System.out.println();
System.out.println("Query loaded document:");
final String xq2 = "let $doc := sdb:doc('mycol.xml', 'mydoc.xml')\n" + "for $log in $doc/log return \n" + "( insert nodes <a><b/></a> into $log )\n";
System.out.println(xq2);
new XQuery(xq2).execute(ctx2);
final XQuery query = new XQuery("sdb:doc('mycol.xml', 'mydoc.xml')");
query.prettyPrint().serialize(ctx2, System.out);
System.out.println();
}
}
use of org.sirix.xquery.node.DBStore in project sirix by sirixdb.
the class XQueryUsage method loadDocumentAndQuery.
/**
* Load a document and query it.
*/
private static void loadDocumentAndQuery() throws QueryException, IOException, SirixException {
final Path doc = Paths.get("src", "main", "resources", "test.xml");
// Initialize query context and store.
final DBStore store = DBStore.newBuilder().build();
QueryContext ctx = new QueryContext(store);
// Use XQuery to load sample document into store.
System.out.println("Loading document:");
URI docUri = doc.toUri();
String xq1 = String.format("bit:load('mydoc.xml', '%s')", docUri.toString());
System.out.println(xq1);
new XQuery(xq1).evaluate(ctx);
try (final Database database = Databases.openDatabase(LOCATION.resolve("mydoc.xml"))) {
// Reuse store and query loaded document.
final QueryContext ctx2 = new QueryContext(store);
System.out.println();
System.out.println("Query loaded document:");
final String xq2 = "doc('mydoc.xml')/nachrichten/nachricht[betreff/text()='sommer' or betreff/text()='strand' or text/text()='sommer' or text/text()='strand']";
System.out.println(xq2);
XQuery query = new XQuery(new SirixCompileChain(store), xq2);
query.prettyPrint().serialize(ctx2, System.out);
}
System.out.println();
store.close();
}
Aggregations