use of org.sirix.api.XdmNodeWriteTrx in project sirix by sirixdb.
the class XdmResourceManager method close.
@Override
public synchronized void close() {
if (!mClosed) {
// Close all open node transactions.
for (XdmNodeReadTrx rtx : mNodeReaderMap.values()) {
if (rtx instanceof XdmNodeWriteTrx) {
((XdmNodeWriteTrx) rtx).rollback();
}
rtx.close();
rtx = null;
}
// Close all open node page transactions.
for (PageReadTrx rtx : mNodePageTrxMap.values()) {
rtx.close();
rtx = null;
}
// Close all open page transactions.
for (PageReadTrx rtx : mPageTrxMap.values()) {
rtx.close();
rtx = null;
}
// Immediately release all ressources.
mLastCommittedUberPage = null;
mNodeReaderMap.clear();
mPageTrxMap.clear();
mNodePageTrxMap.clear();
mResourceStore.closeResource(mResourceConfig.getResource());
mFac.close();
mClosed = true;
}
}
use of org.sirix.api.XdmNodeWriteTrx in project sirix by sirixdb.
the class ResourceTransactionUsage method main.
public static void main(final String[] args) {
final Path file = LOCATION.resolve("db");
final DatabaseConfiguration config = new DatabaseConfiguration(file);
if (Files.exists(file)) {
Databases.truncateDatabase(config);
}
Databases.createDatabase(config);
try (final Database database = Databases.openDatabase(file)) {
database.createResource(new ResourceConfiguration.Builder("resource", config).build());
try (final ResourceManager resource = database.getResourceManager(new ResourceManagerConfiguration.Builder("resource").build());
final XdmNodeWriteTrx wtx = resource.beginNodeWriteTrx()) {
wtx.insertSubtreeAsFirstChild(XMLShredder.createFileReader(LOCATION.resolve("input.xml")));
wtx.moveTo(2);
wtx.moveSubtreeToFirstChild(4).commit();
final OutputStream out = new ByteArrayOutputStream();
new XMLSerializer.XMLSerializerBuilder(resource, out).prettyPrint().build().call();
System.out.println(out);
}
} catch (final SirixException | IOException | XMLStreamException e) {
// LOG or do anything, the database is closed properly.
}
}
use of org.sirix.api.XdmNodeWriteTrx in project sirix by sirixdb.
the class FileHierarchyWalker method parseDir.
/**
* Parse a directory and create a simple XML representation.
*
* @param path path to directory from which to shredder all content into sirix
* @param database sirix {@IDatabase} to shred into
* @param visitor an optional visitor
* @throws SirixException if any sirix operation fails
* @throws IOException if an I/O error occurs
* @throws NullPointerException if one of the arguments is {@code null}
*/
public static Map<Path, org.sirix.fs.FileSystemPath> parseDir(final Path path, final Database database, Optional<Visitor<XdmNodeWriteTrx>> visitor) throws SirixException, IOException {
checkNotNull(visitor);
checkNotNull(path);
try (final ResourceManager resource = checkNotNull(database).getResourceManager(new ResourceManagerConfiguration.Builder("shredded").build());
final XdmNodeWriteTrx wtx = resource.beginNodeWriteTrx()) {
final Builder builder = new Builder(wtx);
if (visitor.isPresent()) {
builder.setVisitor(visitor.get());
}
Map<Path, org.sirix.fs.FileSystemPath> index = Collections.emptyMap();
try (final HierarchyFileVisitor fileVisitor = HierarchyFileVisitor.getInstance(builder)) {
Files.walkFileTree(path, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, fileVisitor);
index = fileVisitor.getIndex();
}
return index;
}
}
use of org.sirix.api.XdmNodeWriteTrx in project sirix by sirixdb.
the class VersioningTest method test1.
/**
* Test revisioning.
*
* @throws SirixException if anything in Sirix fails
*/
public void test1() throws SirixException {
try (final ResourceManager manager = mDatabase.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build())) {
XdmNodeWriteTrx wtx = manager.beginNodeWriteTrx();
for (int i = 0; i < Constants.NDP_NODE_COUNT - 1; i++) {
wtx.insertElementAsFirstChild(new QNm("foo"));
}
wtx.commit();
assertTrue(wtx.getNodeKey() == Constants.NDP_NODE_COUNT - 1);
wtx.close();
wtx = manager.beginNodeWriteTrx();
setBaaaz(wtx);
setFooBar(wtx);
setFoooo(wtx);
wtx.moveTo(Constants.NDP_NODE_COUNT - 1);
fillNodePage(wtx);
wtx.commit();
wtx.close();
wtx = manager.beginNodeWriteTrx();
wtx.moveTo((Constants.NDP_NODE_COUNT << 1) - 1);
fillNodePage(wtx);
wtx.commit();
wtx.close();
wtx = manager.beginNodeWriteTrx();
wtx.moveTo((Constants.NDP_NODE_COUNT * 3) - 1);
fillNodePage(wtx);
wtx.commit();
wtx.close();
wtx = manager.beginNodeWriteTrx();
wtx.moveTo((Constants.NDP_NODE_COUNT << 2) - 1);
fillNodePage(wtx);
wtx.commit();
wtx.close();
try (final XdmNodeReadTrx rtx = manager.beginNodeReadTrx()) {
assertTrue(rtx.moveToFirstChild().hasMoved());
assertEquals(new QNm("foobar"), rtx.getName());
assertTrue(rtx.moveToFirstChild().hasMoved());
assertEquals(new QNm("foooo"), rtx.getName());
for (int i = 0; i < Constants.NDP_NODE_COUNT - 4; i++) {
assertTrue(rtx.moveToFirstChild().hasMoved());
}
assertEquals(new QNm("baaaz"), rtx.getName());
}
}
}
use of org.sirix.api.XdmNodeWriteTrx in project sirix by sirixdb.
the class VersioningTest method test.
/**
* Test revisioning.
*
* @throws SirixException if anything in Sirix fails
*/
public void test() throws SirixException {
try (final ResourceManager manager = mDatabase.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build())) {
try (final XdmNodeWriteTrx wtx = manager.beginNodeWriteTrx()) {
for (int i = 0; i < Constants.NDP_NODE_COUNT - 1; i++) {
wtx.insertElementAsFirstChild(new QNm("foo"));
}
wtx.commit();
assertTrue(wtx.getNodeKey() == Constants.NDP_NODE_COUNT - 1);
fillNodePage(wtx);
wtx.commit();
assertTrue(wtx.getNodeKey() == (Constants.NDP_NODE_COUNT << 1) - 1);
fillNodePage(wtx);
wtx.commit();
assertTrue(wtx.getNodeKey() == (Constants.NDP_NODE_COUNT * 3) - 1);
fillNodePage(wtx);
wtx.commit();
assertTrue(wtx.getNodeKey() == (Constants.NDP_NODE_COUNT << 2) - 1);
fillNodePage(wtx);
wtx.commit();
assertTrue(wtx.getNodeKey() == (Constants.NDP_NODE_COUNT * 5) - 1);
try (final XdmNodeReadTrx rtx = manager.beginNodeReadTrx()) {
for (int i = 0; i < Constants.NDP_NODE_COUNT - 1; i++) {
assertTrue(rtx.moveToFirstChild().hasMoved());
}
move(rtx);
move(rtx);
move(rtx);
move(rtx);
}
}
}
}
Aggregations