use of org.sirix.api.ResourceManager in project sirix by sirixdb.
the class FMSETest method test.
/**
* Test a folder of XML files.
*
* @param FOLDER path string
* @throws Exception if any exception occurs
*/
private void test(final String FOLDER) throws Exception {
Database database = TestHelper.getDatabase(PATHS.PATH1.getFile());
ResourceManager resource = database.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build());
final Path folder = Paths.get(FOLDER);
final List<Path> list = Files.list(folder).filter(path -> path.getFileName().endsWith(".xml")).collect(toList());
// Sort files list according to file names.
list.sort((first, second) -> {
final String firstName = first.getFileName().toString().substring(0, first.getFileName().toString().indexOf('.'));
final String secondName = second.getFileName().toString().substring(0, second.getFileName().toString().indexOf('.'));
if (Integer.parseInt(firstName) < Integer.parseInt(secondName)) {
return -1;
} else if (Integer.parseInt(firstName) > Integer.parseInt(secondName)) {
return +1;
} else {
return 0;
}
});
boolean first = true;
// Shredder files.
for (final Path file : list) {
if (file.getFileName().endsWith(".xml")) {
if (first) {
first = false;
try (final XdmNodeWriteTrx wtx = resource.beginNodeWriteTrx()) {
final XMLShredder shredder = new XMLShredder.Builder(wtx, XMLShredder.createFileReader(file), Insert.ASFIRSTCHILD).commitAfterwards().build();
shredder.call();
}
} else {
FMSEImport.main(new String[] { PATHS.PATH1.getFile().toAbsolutePath().toString(), file.toAbsolutePath().toString() });
}
resource.close();
resource = database.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build());
final OutputStream out = new ByteArrayOutputStream();
final XMLSerializer serializer = new XMLSerializerBuilder(resource, out).build();
serializer.call();
final StringBuilder sBuilder = TestHelper.readFile(file, false);
final Diff diff = new Diff(sBuilder.toString(), out.toString());
final DetailedDiff detDiff = new DetailedDiff(diff);
@SuppressWarnings("unchecked") final List<Difference> differences = detDiff.getAllDifferences();
for (final Difference difference : differences) {
System.err.println("***********************");
System.err.println(difference);
System.err.println("***********************");
}
assertTrue("pieces of XML are similar " + diff, diff.similar());
assertTrue("but are they identical? " + diff, diff.identical());
}
}
database.close();
}
use of org.sirix.api.ResourceManager in project sirix by sirixdb.
the class XMLShredderTest method testShreddingLargeText.
@Test
public void testShreddingLargeText() throws Exception {
final Database database = TestHelper.getDatabase(PATHS.PATH2.getFile());
final ResourceManager manager = database.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build());
final XdmNodeWriteTrx wtx = manager.beginNodeWriteTrx();
final XMLShredder shredder = new XMLShredder.Builder(wtx, XMLShredder.createFileReader(XML3), Insert.ASFIRSTCHILD).commitAfterwards().build();
shredder.call();
wtx.close();
final XdmNodeReadTrx rtx = manager.beginNodeReadTrx();
assertTrue(rtx.moveToFirstChild().hasMoved());
assertTrue(rtx.moveToFirstChild().hasMoved());
final StringBuilder tnkBuilder = new StringBuilder();
do {
tnkBuilder.append(rtx.getValue());
} while (rtx.moveToRightSibling().hasMoved());
final String tnkString = tnkBuilder.toString();
rtx.close();
manager.close();
final XMLEventReader validater = XMLShredder.createFileReader(XML3);
final StringBuilder xmlBuilder = new StringBuilder();
while (validater.hasNext()) {
final XMLEvent event = validater.nextEvent();
switch(event.getEventType()) {
case XMLStreamConstants.CHARACTERS:
final String text = event.asCharacters().getData().trim();
if (text.length() > 0) {
xmlBuilder.append(text);
}
break;
}
}
assertEquals(xmlBuilder.toString(), tnkString);
}
use of org.sirix.api.ResourceManager 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.ResourceManager 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.ResourceManager 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());
}
}
}
Aggregations