Search in sources :

Example 16 with TDBException

use of org.apache.jena.tdb2.TDBException in project jena by apache.

the class TupleIndexRecord method findWorker.

private Iterator<Tuple<NodeId>> findWorker(Tuple<NodeId> patternNaturalOrder, boolean partialScanAllowed, boolean fullScanAllowed) {
    if (Check) {
        if (tupleLength != patternNaturalOrder.len())
            throw new TDBException(String.format("Mismatch: tuple length %d / index for length %d", patternNaturalOrder.len(), tupleLength));
    }
    // Convert to index order.
    Tuple<NodeId> pattern = tupleMap.map(patternNaturalOrder);
    // Canonical form.
    int numSlots = 0;
    // Index of last leading pattern NodeId.  Start less than numSlots-1
    int leadingIdx = -2;
    boolean leading = true;
    // Records.
    Record minRec = factory.createKeyOnly();
    Record maxRec = factory.createKeyOnly();
    // Set the prefixes.
    for (int i = 0; i < pattern.len(); i++) {
        NodeId X = pattern.get(i);
        if (NodeId.isAny(X)) {
            X = null;
            // No longer seting leading key slots.
            leading = false;
            continue;
        }
        // if ( NodeId.isDoesNotExist(X) )
        // return Iter.nullIterator();
        numSlots++;
        if (leading) {
            leadingIdx = i;
            NodeIdFactory.set(X, minRec.getKey(), i * SizeOfNodeId);
            NodeIdFactory.set(X, maxRec.getKey(), i * SizeOfNodeId);
        }
    }
    // Is it a simple existence test?
    if (numSlots == pattern.len()) {
        if (index.contains(minRec))
            return new SingletonIterator<>(pattern);
        else
            return new NullIterator<>();
    }
    if (true) {
        Iterator<Tuple<NodeId>> tuples;
        if (leadingIdx < 0) {
            if (!fullScanAllowed)
                return null;
            // Full scan necessary
            tuples = index.iterator(null, null, recordMapper);
        } else {
            // Adjust the maxRec.
            NodeId X = pattern.get(leadingIdx);
            // Set the max Record to the leading NodeIds, +1.
            // Example, SP? inclusive to S(P+1)? exclusive where ? is zero.
            NodeIdFactory.setNext(X, maxRec.getKey(), leadingIdx * SizeOfNodeId);
            tuples = index.iterator(minRec, maxRec, recordMapper);
        }
        if (leadingIdx < numSlots - 1) {
            if (!partialScanAllowed)
                return null;
            // Didn't match all defined slots in request.
            // Partial or full scan needed.
            // pattern.unmap(colMap);
            tuples = scan(tuples, patternNaturalOrder);
        }
        return tuples;
    }
    Iterator<Record> iter = null;
    if (leadingIdx < 0) {
        if (!fullScanAllowed)
            return null;
        // Full scan necessary
        iter = index.iterator();
    } else {
        // Adjust the maxRec.
        NodeId X = pattern.get(leadingIdx);
        // Set the max Record to the leading NodeIds, +1.
        // Example, SP? inclusive to S(P+1)? exclusive where ? is zero.
        NodeIdFactory.setNext(X, maxRec.getKey(), leadingIdx * SizeOfNodeId);
        iter = index.iterator(minRec, maxRec);
    }
    Iterator<Tuple<NodeId>> tuples = Iter.map(iter, item -> TupleLib.tuple(item, tupleMap));
    if (leadingIdx < numSlots - 1) {
        if (!partialScanAllowed)
            return null;
        // Didn't match all defined slots in request.
        // Partial or full scan needed.
        // pattern.unmap(colMap);
        tuples = scan(tuples, patternNaturalOrder);
    }
    return tuples;
}
Also used : TDBException(org.apache.jena.tdb2.TDBException) SizeOfNodeId(org.apache.jena.tdb2.sys.SystemTDB.SizeOfNodeId) NodeId(org.apache.jena.tdb2.store.NodeId) Record(org.apache.jena.dboe.base.record.Record) Tuple(org.apache.jena.atlas.lib.tuple.Tuple)

Example 17 with TDBException

use of org.apache.jena.tdb2.TDBException in project jena by apache.

the class TupleTable method find.

/**
 * Find all matching tuples - a slot of NodeId.NodeIdAny means match any
 */
public Iterator<Tuple<NodeId>> find(Tuple<NodeId> pattern) {
    if (tupleLen != pattern.len())
        throw new TDBException(format("Mismatch: finding tuple of length %d in a table of tuples of length %d", pattern.len(), tupleLen));
    int numSlots = 0;
    // Canonical form.
    for (int i = 0; i < tupleLen; i++) {
        NodeId x = pattern.get(i);
        if (!NodeId.isAny(x))
            numSlots++;
        if (NodeId.isDoesNotExist(x))
            return Iter.nullIterator();
    }
    if (numSlots == 0)
        return scanAllIndex.all();
    int indexNumSlots = 0;
    TupleIndex index = null;
    for (TupleIndex idx : indexes) {
        if (idx != null) {
            int w = idx.weight(pattern);
            if (w > indexNumSlots) {
                indexNumSlots = w;
                index = idx;
            }
        }
    }
    if (index == null)
        // No index at all.  Scan.
        index = indexes[0];
    return index.find(pattern);
}
Also used : TDBException(org.apache.jena.tdb2.TDBException) NodeId(org.apache.jena.tdb2.store.NodeId)

Example 18 with TDBException

use of org.apache.jena.tdb2.TDBException in project jena by apache.

the class TupleTable method add.

/**
 * Insert a tuple
 */
public void add(Tuple<NodeId> t) {
    // the indexes when the triple is already present.
    if (tupleLen != t.len())
        throw new TDBException(format("Mismatch: inserting tuple of length %d into a table of tuples of length %d", t.len(), tupleLen));
    for (int i = 0; i < indexes.length; i++) {
        if (indexes[i] == null)
            continue;
        indexes[i].add(t);
        syncNeeded = true;
    }
}
Also used : TDBException(org.apache.jena.tdb2.TDBException)

Example 19 with TDBException

use of org.apache.jena.tdb2.TDBException 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 20 with TDBException

use of org.apache.jena.tdb2.TDBException 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

TDBException (org.apache.jena.tdb2.TDBException)20 NodeId (org.apache.jena.tdb2.store.NodeId)6 Node (org.apache.jena.graph.Node)4 DatasetGraphTDB (org.apache.jena.tdb2.store.DatasetGraphTDB)4 Location (org.apache.jena.dboe.base.file.Location)3 Record (org.apache.jena.dboe.base.record.Record)3 RiotThriftException (org.apache.jena.riot.thrift.RiotThriftException)2 RDF_Term (org.apache.jena.riot.thrift.wire.RDF_Term)2 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)2 TException (org.apache.thrift.TException)2 ArrayList (java.util.ArrayList)1 RuntimeIOException (org.apache.jena.atlas.RuntimeIOException)1 Tuple (org.apache.jena.atlas.lib.tuple.Tuple)1 TupleMap (org.apache.jena.atlas.lib.tuple.TupleMap)1 BlockMgr (org.apache.jena.dboe.base.block.BlockMgr)1 BufferChannel (org.apache.jena.dboe.base.file.BufferChannel)1 FileSet (org.apache.jena.dboe.base.file.FileSet)1 ProcessFileLock (org.apache.jena.dboe.base.file.ProcessFileLock)1 RecordFactory (org.apache.jena.dboe.base.record.RecordFactory)1 BPlusTree (org.apache.jena.dboe.trans.bplustree.BPlusTree)1