use of org.apache.jena.tdb2.store.DatasetGraphTDB in project jena by apache.
the class tdbstats method exec.
@Override
protected void exec() {
DatasetGraphTDB dsg = TDBInternal.getDatasetGraphTDB(getDatasetGraph());
Node gn = getGraphName();
StatsResults results = stats(dsg, gn);
Stats.write(System.out, results);
}
use of org.apache.jena.tdb2.store.DatasetGraphTDB in project jena by apache.
the class ProcBuildIndexX method indexBuilder.
private static long indexBuilder(DatasetGraph dsg, InputStream input, String indexName) {
long tickPoint = BulkLoaderX.DataTick;
int superTick = BulkLoaderX.DataSuperTick;
// Location of storage, not the DB.
DatasetGraphTDB dsgtdb = TDBInternal.getDatasetGraphTDB(dsg);
Location location = dsgtdb.getLocation();
int keyLength = SystemTDB.SizeOfNodeId * indexName.length();
int valueLength = 0;
// The name is the order.
String primary = indexName;
String primaryOrder;
int dftKeyLength;
int dftValueLength;
int tupleLength = indexName.length();
TupleIndex index;
if (tupleLength == 3) {
primaryOrder = Names.primaryIndexTriples;
dftKeyLength = SystemTDB.LenIndexTripleRecord;
dftValueLength = 0;
// Find index.
index = findIndex(dsgtdb.getTripleTable().getNodeTupleTable().getTupleTable().getIndexes(), indexName);
} else if (tupleLength == 4) {
primaryOrder = Names.primaryIndexQuads;
dftKeyLength = SystemTDB.LenIndexQuadRecord;
dftValueLength = 0;
index = findIndex(dsgtdb.getQuadTable().getNodeTupleTable().getTupleTable().getIndexes(), indexName);
} else {
throw new TDBException("Index name: " + indexName);
}
TupleMap colMap = TupleMap.create(primaryOrder, indexName);
int readCacheSize = 10;
int writeCacheSize = 100;
int blockSize = SystemTDB.BlockSize;
RecordFactory recordFactory = new RecordFactory(dftKeyLength, dftValueLength);
int order = BPlusTreeParams.calcOrder(blockSize, recordFactory);
BPlusTreeParams bptParams = new BPlusTreeParams(order, recordFactory);
int blockSizeNodes = blockSize;
int blockSizeRecords = blockSize;
FileSet destination = new FileSet(location, indexName);
BufferChannel blkState = FileFactory.createBufferChannel(destination, Names.extBptState);
BlockMgr blkMgrNodes = BlockMgrFactory.create(destination, Names.extBptTree, blockSizeNodes, readCacheSize, writeCacheSize);
BlockMgr blkMgrRecords = BlockMgrFactory.create(destination, Names.extBptRecords, blockSizeRecords, readCacheSize, writeCacheSize);
int rowBlock = 1000;
Iterator<Record> iter = new RecordsFromInput(input, tupleLength, colMap, rowBlock);
// ProgressMonitor.
ProgressMonitor monitor = ProgressMonitorOutput.create(BulkLoaderX.LOG_Index, indexName, tickPoint, superTick);
ProgressIterator<Record> iter2 = new ProgressIterator<>(iter, monitor);
monitor.start();
BPlusTree bpt2 = BPlusTreeRewriter.packIntoBPlusTree(iter2, bptParams, recordFactory, blkState, blkMgrNodes, blkMgrRecords);
bpt2.close();
monitor.finish();
// [BULK] End stage.
long count = monitor.getTicks();
return count;
}
use of org.apache.jena.tdb2.store.DatasetGraphTDB in project jena by apache.
the class NodeTableLib method printNodeTable.
/**
* Print the main node table - development helper
*/
public static void printNodeTable(DatasetGraph dsg, long limit) {
dsg.executeRead(() -> {
DatasetGraphTDB dsgtdb = TDBInternal.getDatasetGraphTDB(dsg);
NodeTable nodeTable = dsgtdb.getTripleTable().getNodeTupleTable().getNodeTable();
int x = 0;
for (var iter = nodeTable.all(); iter.hasNext(); ) {
var pair = iter.next();
x++;
if (x > limit)
return;
NodeId nid = pair.getLeft();
Node n = pair.getRight();
System.out.printf("%s %s\n", nid, NodeFmtLib.strNT(n));
}
// long x = Iter.count(nodeTable.all());
System.out.println("Node table length: " + x);
});
}
use of org.apache.jena.tdb2.store.DatasetGraphTDB in project jena by apache.
the class DatabaseOps method compact.
// XXX Later - switch in a recording dataset, not block writers, and reply after
// switch over before releasing the new dataset to the container.
// Maybe copy indexes and switch the DSG over (drop switchable).
/**
* Copy the latest version from one location to another.
*/
private static void compact(DatasetGraphSwitchable container, Location loc1, Location loc2) {
if (loc1.isMem() || loc2.isMem())
throw new TDBException("Compact involves a memory location: " + loc1 + " : " + loc2);
copyFiles(loc1, loc2);
StoreConnection srcConn = StoreConnection.connectExisting(loc1);
if (srcConn == null)
throw new TDBException("No database at location : " + loc1);
if (!(container.get() instanceof DatasetGraphTDB))
throw new TDBException("Not a TDB2 database in DatasetGraphSwitchable");
DatasetGraphTDB dsgCurrent = (DatasetGraphTDB) container.get();
if (!dsgCurrent.getLocation().equals(loc1))
throw new TDBException("Inconsistent locations for base : " + dsgCurrent.getLocation() + " , " + dsgCurrent.getLocation());
DatasetGraphTDB dsgBase = srcConn.getDatasetGraphTDB();
if (dsgBase != dsgCurrent)
throw new TDBException("Inconsistent datasets : " + dsgCurrent.getLocation() + " , " + dsgBase.getLocation());
TransactionalSystem txnSystem = dsgBase.getTxnSystem();
TransactionCoordinator txnMgr = dsgBase.getTxnSystem().getTxnMgr();
// Stop update. On exit there are no writers and none will start until switched over.
txnMgr.tryBlockWriters();
// txnMgr.begin(WRITE, false) will now bounce.
// Copy the latest generation.
DatasetGraphTDB dsgCompact = StoreConnection.connectCreate(loc2).getDatasetGraphTDB();
CopyDSG.copy(dsgBase, dsgCompact);
TransactionCoordinator txnMgr2 = dsgCompact.getTxnSystem().getTxnMgr();
txnMgr2.startExclusiveMode();
txnMgr.startExclusiveMode();
// Switch.
if (!container.change(dsgCurrent, dsgCompact)) {
Log.warn(DatabaseOps.class, "Inconistent: old datasetgraph not as expected");
container.set(dsgCompact);
}
txnMgr2.finishExclusiveMode();
// New database running.
// Clean-up.
// txnMgr.finishExclusiveMode();
// Don't call : txnMgr.startWriters();
StoreConnection.release(dsgBase.getLocation());
}
use of org.apache.jena.tdb2.store.DatasetGraphTDB in project jena by apache.
the class DatabaseOps method compact.
public static void compact(DatasetGraphSwitchable container, boolean shouldDeleteOld) {
checkSupportsAdmin(container);
synchronized (compactionLock) {
Path base = container.getContainerPath();
Path db1 = findLocation(base, dbPrefix);
if (db1 == null)
throw new TDBException("No location: (" + base + ", " + dbPrefix + ")");
Location loc1 = IO_DB.asLocation(db1);
// -- Checks
Location loc1a = ((DatasetGraphTDB) container.get()).getLocation();
if (loc1a.isMem()) {
}
if (!loc1a.exists())
throw new TDBException("No such location: " + loc1a);
// Is this the same database location?
if (!loc1.equals(loc1a))
throw new TDBException("Inconsistent (not latest?) : " + loc1a + " : " + loc1);
// -- Checks
// Version
int v = IO_DB.extractIndex(db1.getFileName().toString(), dbPrefix, SEP);
String next = FilenameUtils.filename(dbPrefix, SEP, v + 1);
Path db2 = db1.getParent().resolve(next);
IOX.createDirectory(db2);
Location loc2 = IO_DB.asLocation(db2);
LOG.debug(String.format("Compact %s -> %s\n", db1.getFileName(), db2.getFileName()));
compact(container, loc1, loc2);
if (shouldDeleteOld) {
Path loc1Path = IO_DB.asPath(loc1);
LOG.debug("Deleting old database after successful compaction (old db path='" + loc1Path + "')...");
try (Stream<Path> walk = Files.walk(loc1Path)) {
walk.sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
} catch (IOException ex) {
throw IOX.exception(ex);
}
}
}
}
Aggregations