use of org.sirix.api.XdmNodeWriteTrx in project sirix by sirixdb.
the class DBStore method create.
@Override
public TemporalCollection<?> create(final String collName, @Nullable final Stream<SubtreeParser> parsers) throws DocumentException {
if (parsers != null) {
final DatabaseConfiguration dbConf = new DatabaseConfiguration(mLocation.resolve(collName));
try {
Databases.truncateDatabase(dbConf);
Databases.createDatabase(dbConf);
final Database database = Databases.openDatabase(dbConf.getFile());
mDatabases.add(database);
final ExecutorService pool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
int i = database.listResources().size() + 1;
try {
SubtreeParser parser = null;
while ((parser = parsers.next()) != null) {
final SubtreeParser nextParser = parser;
final String resourceName = new StringBuilder("resource").append(String.valueOf(i)).toString();
pool.submit(() -> {
database.createResource(ResourceConfiguration.newBuilder(resourceName, dbConf).storageType(mStorageType).useDeweyIDs(true).useTextCompression(true).buildPathSummary(true).build());
try (final ResourceManager resource = database.getResourceManager(new ResourceManagerConfiguration.Builder(resourceName).build());
final XdmNodeWriteTrx wtx = resource.beginNodeWriteTrx()) {
final DBCollection collection = new DBCollection(collName, database);
mCollections.put(database, collection);
nextParser.parse(new SubtreeBuilder(collection, wtx, Insert.ASFIRSTCHILD, Collections.<SubtreeListener<? super AbstractTemporalNode<DBNode>>>emptyList()));
wtx.commit();
}
return null;
});
i++;
}
} finally {
parsers.close();
}
pool.shutdown();
pool.awaitTermination(5, TimeUnit.MINUTES);
return new DBCollection(collName, database);
} catch (final SirixRuntimeException | InterruptedException e) {
throw new DocumentException(e.getCause());
}
}
return null;
}
use of org.sirix.api.XdmNodeWriteTrx in project sirix by sirixdb.
the class CreateCASIndex method execute.
@Override
public Sequence execute(StaticContext sctx, QueryContext ctx, Sequence[] args) throws QueryException {
if (args.length != 2 && args.length != 3) {
throw new QueryException(new QNm("No valid arguments specified!"));
}
final DBNode doc = ((DBNode) args[0]);
final XdmNodeReadTrx rtx = doc.getTrx();
final IndexController controller = rtx.getResourceManager().getWtxIndexController(rtx.getRevisionNumber() - 1);
if (!(doc.getTrx() instanceof XdmNodeWriteTrx)) {
throw new QueryException(new QNm("Collection must be updatable!"));
}
if (controller == null) {
throw new QueryException(new QNm("Document not found: " + ((Str) args[1]).stringValue()));
}
Type type = null;
if (args.length > 1 && args[1] != null) {
final QNm name = new QNm(Namespaces.XS_NSURI, ((Str) args[1]).stringValue());
type = sctx.getTypes().resolveAtomicType(name);
}
final Set<Path<QNm>> paths = new HashSet<>();
if (args.length == 3 && args[2] != null) {
final Iter it = args[2].iterate();
Item next = it.next();
while (next != null) {
paths.add(Path.parse(((Str) next).stringValue()));
next = it.next();
}
}
final IndexDef idxDef = IndexDefs.createCASIdxDef(false, Optional.fromNullable(type), paths, controller.getIndexes().getNrOfIndexDefsWithType(IndexType.CAS));
try {
controller.createIndexes(ImmutableSet.of(idxDef), (XdmNodeWriteTrx) doc.getTrx());
} catch (final SirixIOException e) {
throw new QueryException(new QNm("I/O exception: " + e.getMessage()), e);
}
return idxDef.materialize();
}
use of org.sirix.api.XdmNodeWriteTrx in project sirix by sirixdb.
the class CreateNameIndex method execute.
@Override
public Sequence execute(final StaticContext sctx, final QueryContext ctx, final Sequence[] args) throws QueryException {
if (args.length != 2 && args.length != 3) {
throw new QueryException(new QNm("No valid arguments specified!"));
}
final DBNode doc = ((DBNode) args[0]);
final XdmNodeReadTrx rtx = doc.getTrx();
final IndexController controller = rtx.getResourceManager().getWtxIndexController(rtx.getRevisionNumber() - 1);
if (!(doc.getTrx() instanceof XdmNodeWriteTrx)) {
throw new QueryException(new QNm("Collection must be updatable!"));
}
if (controller == null) {
throw new QueryException(new QNm("Document not found: " + ((Str) args[1]).stringValue()));
}
final Set<QNm> include = new HashSet<>();
if (args.length > 1 && args[1] != null) {
final Iter it = args[1].iterate();
Item next = it.next();
while (next != null) {
include.add((QNm) next);
next = it.next();
}
}
final IndexDef idxDef = IndexDefs.createSelectiveNameIdxDef(include, controller.getIndexes().getNrOfIndexDefsWithType(IndexType.NAME));
try {
controller.createIndexes(ImmutableSet.of(idxDef), (XdmNodeWriteTrx) doc.getTrx());
} catch (final SirixIOException e) {
throw new QueryException(new QNm("I/O exception: " + e.getMessage()), e);
}
return idxDef.materialize();
}
use of org.sirix.api.XdmNodeWriteTrx in project sirix by sirixdb.
the class CreatePathIndex method execute.
@Override
public Sequence execute(final StaticContext sctx, final QueryContext ctx, final Sequence[] args) throws QueryException {
if (args.length != 2 && args.length != 3) {
throw new QueryException(new QNm("No valid arguments specified!"));
}
final DBNode doc = ((DBNode) args[0]);
final XdmNodeReadTrx rtx = doc.getTrx();
final IndexController controller = rtx.getResourceManager().getWtxIndexController(rtx.getRevisionNumber() - 1);
if (!(doc.getTrx() instanceof XdmNodeWriteTrx)) {
throw new QueryException(new QNm("Collection must be updatable!"));
}
if (controller == null) {
throw new QueryException(new QNm("Document not found: " + ((Str) args[1]).stringValue()));
}
if (!(doc.getTrx() instanceof XdmNodeWriteTrx)) {
throw new QueryException(new QNm("Collection must be updatable!"));
}
final Set<Path<QNm>> paths = new HashSet<>();
if (args.length > 2 && args[2] != null) {
final Iter it = args[2].iterate();
Item next = it.next();
while (next != null) {
paths.add(Path.parse(((Str) next).stringValue()));
next = it.next();
}
}
final IndexDef idxDef = IndexDefs.createPathIdxDef(paths, controller.getIndexes().getNrOfIndexDefsWithType(IndexType.PATH));
try {
controller.createIndexes(ImmutableSet.of(idxDef), (XdmNodeWriteTrx) doc.getTrx());
} catch (final SirixIOException e) {
throw new QueryException(new QNm("I/O exception: " + e.getMessage()), e);
}
return idxDef.materialize();
}
use of org.sirix.api.XdmNodeWriteTrx in project sirix by sirixdb.
the class VersioningTest method test2.
/**
* Test revisioning.
*
* @throws SirixException if anything in Sirix fails
*/
public void test2() throws SirixException {
try (final ResourceManager manager = mDatabase.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build())) {
XdmNodeWriteTrx wtx = manager.beginNodeWriteTrx();
wtx.insertElementAsFirstChild(new QNm("foo"));
wtx.commit();
wtx.insertElementAsFirstChild(new QNm("foo"));
wtx.commit();
wtx.insertElementAsFirstChild(new QNm("foo"));
wtx.commit();
wtx.insertElementAsFirstChild(new QNm("foo"));
wtx.commit();
wtx.insertElementAsFirstChild(new QNm("foo"));
wtx.commit();
wtx.close();
try (final XdmNodeReadTrx rtx = manager.beginNodeReadTrx()) {
assertTrue(rtx.moveToFirstChild().hasMoved());
assertTrue(rtx.moveToFirstChild().hasMoved());
assertTrue(rtx.moveToFirstChild().hasMoved());
assertTrue(rtx.moveToFirstChild().hasMoved());
assertTrue(rtx.moveToFirstChild().hasMoved());
}
}
}
Aggregations