use of org.sirix.api.ResourceManager in project sirix by sirixdb.
the class DBCollection method getDocuments.
@Override
public Stream<DBNode> getDocuments(final boolean updatable) throws DocumentException {
final List<Path> resources = mDatabase.listResources();
final List<DBNode> documents = new ArrayList<>(resources.size());
for (final Path resourcePath : resources) {
try {
final String resourceName = resourcePath.getFileName().toString();
final ResourceManager resource = mDatabase.getResourceManager(ResourceManagerConfiguration.newBuilder(resourceName).build());
final XdmNodeReadTrx trx = updatable ? resource.beginNodeWriteTrx() : resource.beginNodeReadTrx();
documents.add(new DBNode(trx, this));
} catch (final SirixException e) {
throw new DocumentException(e.getCause());
}
}
return new ArrayStream<DBNode>(documents.toArray(new DBNode[documents.size()]));
}
use of org.sirix.api.ResourceManager in project sirix by sirixdb.
the class DBCollection method getDocumentInternal.
private DBNode getDocumentInternal(final ResourceManagerConfiguration resourceManagerConfig, final int revision, final boolean updatable) throws SirixException {
final ResourceManager resource = mDatabase.getResourceManager(resourceManagerConfig);
final int version = revision == -1 ? resource.getMostRecentRevisionNumber() : revision;
final XdmNodeReadTrx trx;
if (updatable) {
if (resource.getAvailableNodeWriteTrx() == 0) {
final Optional<XdmNodeWriteTrx> optionalWriteTrx;
optionalWriteTrx = resource.getXdmNodeWriteTrx();
if (optionalWriteTrx.isPresent()) {
trx = optionalWriteTrx.get();
} else {
trx = resource.beginNodeWriteTrx();
}
} else {
trx = resource.beginNodeWriteTrx();
}
if (version < resource.getMostRecentRevisionNumber())
((XdmNodeWriteTrx) trx).revertTo(version);
} else {
trx = resource.beginNodeReadTrx(version);
}
return new DBNode(trx, this);
}
use of org.sirix.api.ResourceManager in project sirix by sirixdb.
the class DBNode method getWtx.
private XdmNodeWriteTrx getWtx() {
final ResourceManager resource = mRtx.getResourceManager();
final XdmNodeWriteTrx wtx;
if (resource.getAvailableNodeWriteTrx() == 0) {
wtx = resource.getXdmNodeWriteTrx().get();
} else {
wtx = resource.beginNodeWriteTrx();
}
if (mRtx.getRevisionNumber() < resource.getMostRecentRevisionNumber())
wtx.revertTo(mRtx.getRevisionNumber());
wtx.moveTo(mNodeKey);
return wtx;
}
use of org.sirix.api.ResourceManager in project sirix by sirixdb.
the class DBStore method create.
public TemporalCollection<?> create(final String collName, final Optional<String> optResName, final SubtreeParser parser) throws DocumentException {
final DatabaseConfiguration dbConf = new DatabaseConfiguration(mLocation.resolve(collName));
try {
Databases.truncateDatabase(dbConf);
Databases.createDatabase(dbConf);
try (final Database database = Databases.openDatabase(dbConf.getFile())) {
mDatabases.add(database);
final String resName = optResName.isPresent() ? optResName.get() : new StringBuilder(3).append("resource").append(database.listResources().size() + 1).toString();
database.createResource(ResourceConfiguration.newBuilder(resName, dbConf).useDeweyIDs(true).useTextCompression(true).buildPathSummary(true).storageType(mStorageType).build());
final DBCollection collection = new DBCollection(collName, database);
mCollections.put(database, collection);
try (final ResourceManager resource = database.getResourceManager(new ResourceManagerConfiguration.Builder(resName).build());
final XdmNodeWriteTrx wtx = resource.beginNodeWriteTrx()) {
parser.parse(new SubtreeBuilder(collection, wtx, Insert.ASFIRSTCHILD, Collections.<SubtreeListener<? super AbstractTemporalNode<DBNode>>>emptyList()));
wtx.commit();
}
return collection;
}
} catch (final SirixException e) {
throw new DocumentException(e.getCause());
}
}
use of org.sirix.api.ResourceManager in project sirix by sirixdb.
the class Commit method execute.
@Override
public Sequence execute(StaticContext sctx, QueryContext ctx, Sequence[] args) throws QueryException {
final DBNode doc = ((DBNode) args[0]);
if (doc.getTrx() instanceof XdmNodeWriteTrx) {
final XdmNodeWriteTrx wtx = (XdmNodeWriteTrx) doc.getTrx();
final long revision = wtx.getRevisionNumber();
wtx.commit();
return new Int64(revision);
} else {
final ResourceManager manager = doc.getTrx().getResourceManager();
final XdmNodeWriteTrx wtx;
if (manager.getAvailableNodeWriteTrx() == 0) {
wtx = manager.getXdmNodeWriteTrx().get();
} else {
wtx = manager.beginNodeWriteTrx();
}
final int revision = doc.getTrx().getRevisionNumber();
if (revision < manager.getMostRecentRevisionNumber()) {
wtx.revertTo(doc.getTrx().getRevisionNumber());
}
wtx.commit();
return new Int64(revision);
}
}
Aggregations