Search in sources :

Example 26 with DatasetGraphTDB

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);
}
Also used : Node(org.apache.jena.graph.Node) StatsResults(org.apache.jena.tdb2.solver.stats.StatsResults) DatasetGraphTDB(org.apache.jena.tdb2.store.DatasetGraphTDB)

Example 27 with DatasetGraphTDB

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;
}
Also used : BPlusTreeParams(org.apache.jena.dboe.trans.bplustree.BPlusTreeParams) FileSet(org.apache.jena.dboe.base.file.FileSet) TDBException(org.apache.jena.tdb2.TDBException) BufferChannel(org.apache.jena.dboe.base.file.BufferChannel) DatasetGraphTDB(org.apache.jena.tdb2.store.DatasetGraphTDB) TupleMap(org.apache.jena.atlas.lib.tuple.TupleMap) ProgressIterator(org.apache.jena.system.progress.ProgressIterator) ProgressMonitor(org.apache.jena.system.progress.ProgressMonitor) RecordFactory(org.apache.jena.dboe.base.record.RecordFactory) BlockMgr(org.apache.jena.dboe.base.block.BlockMgr) Record(org.apache.jena.dboe.base.record.Record) TupleIndex(org.apache.jena.tdb2.store.tupletable.TupleIndex) BPlusTree(org.apache.jena.dboe.trans.bplustree.BPlusTree) Location(org.apache.jena.dboe.base.file.Location)

Example 28 with DatasetGraphTDB

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);
    });
}
Also used : Node(org.apache.jena.graph.Node) NodeId(org.apache.jena.tdb2.store.NodeId) NodeTable(org.apache.jena.tdb2.store.nodetable.NodeTable) DatasetGraphTDB(org.apache.jena.tdb2.store.DatasetGraphTDB)

Example 29 with DatasetGraphTDB

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());
}
Also used : TDBException(org.apache.jena.tdb2.TDBException) TransactionalSystem(org.apache.jena.dboe.transaction.txn.TransactionalSystem) TransactionCoordinator(org.apache.jena.dboe.transaction.txn.TransactionCoordinator) DatasetGraphTDB(org.apache.jena.tdb2.store.DatasetGraphTDB)

Example 30 with DatasetGraphTDB

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);
            }
        }
    }
}
Also used : TDBException(org.apache.jena.tdb2.TDBException) RuntimeIOException(org.apache.jena.atlas.RuntimeIOException) Location(org.apache.jena.dboe.base.file.Location) DatasetGraphTDB(org.apache.jena.tdb2.store.DatasetGraphTDB)

Aggregations

DatasetGraphTDB (org.apache.jena.tdb2.store.DatasetGraphTDB)19 Location (org.apache.jena.dboe.base.file.Location)10 Node (org.apache.jena.graph.Node)8 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)8 DatasetGraphSwitchable (org.apache.jena.tdb2.store.DatasetGraphSwitchable)8 Test (org.junit.Test)8 NodeId (org.apache.jena.tdb2.store.NodeId)7 NodeTable (org.apache.jena.tdb2.store.nodetable.NodeTable)7 Tuple (org.apache.jena.atlas.lib.tuple.Tuple)6 StoreConnection (org.apache.jena.tdb2.sys.StoreConnection)4 ConfigTest (org.apache.jena.tdb2.ConfigTest)3 TDBException (org.apache.jena.tdb2.TDBException)3 BufferChannel (org.apache.jena.dboe.base.file.BufferChannel)2 FileSet (org.apache.jena.dboe.base.file.FileSet)2 Record (org.apache.jena.dboe.base.record.Record)2 RecordFactory (org.apache.jena.dboe.base.record.RecordFactory)2 BPlusTree (org.apache.jena.dboe.trans.bplustree.BPlusTree)2 Graph (org.apache.jena.graph.Graph)2 NodeTupleTable (org.apache.jena.tdb2.store.nodetupletable.NodeTupleTable)2 ModDataset (arq.cmdline.ModDataset)1