Search in sources :

Example 6 with TDBException

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

the class SolverLib method execute.

// The worker.  Callers choose the NodeTupleTable.  
//     graphNode may be Node.ANY, meaning we should make triples unique.
//     graphNode may be null, meaning default graph
private static QueryIterator execute(NodeTupleTable nodeTupleTable, Node graphNode, BasicPattern pattern, QueryIterator input, Predicate<Tuple<NodeId>> filter, ExecutionContext execCxt) {
    if (Quad.isUnionGraph(graphNode))
        graphNode = Node.ANY;
    if (Quad.isDefaultGraph(graphNode))
        graphNode = null;
    List<Triple> triples = pattern.getList();
    boolean anyGraph = (graphNode == null ? false : (Node.ANY.equals(graphNode)));
    int tupleLen = nodeTupleTable.getTupleTable().getTupleLen();
    if (graphNode == null) {
        if (3 != tupleLen)
            throw new TDBException("SolverLib: Null graph node but tuples are of length " + tupleLen);
    } else {
        if (4 != tupleLen)
            throw new TDBException("SolverLib: Graph node specified but tuples are of length " + tupleLen);
    }
    // Convert from a QueryIterator (Bindings of Var/Node) to BindingNodeId
    NodeTable nodeTable = nodeTupleTable.getNodeTable();
    Iterator<BindingNodeId> chain = Iter.map(input, SolverLib.convFromBinding(nodeTable));
    List<Abortable> killList = new ArrayList<>();
    for (Triple triple : triples) {
        Tuple<Node> tuple = null;
        if (graphNode == null)
            // 3-tuples
            tuple = tuple(triple.getSubject(), triple.getPredicate(), triple.getObject());
        else
            // 4-tuples.
            tuple = tuple(graphNode, triple.getSubject(), triple.getPredicate(), triple.getObject());
        chain = solve(nodeTupleTable, tuple, anyGraph, chain, filter, execCxt);
        chain = makeAbortable(chain, killList);
    }
    // DEBUG POINT
    if (false) {
        if (chain.hasNext())
            chain = Iter.debug(chain);
        else
            System.out.println("No results");
    }
    // Timeout wrapper ****
    // QueryIterTDB gets called async.
    // Iter.abortable?
    // Or each iterator has a place to test.
    // or pass in a thing to test?
    // Need to make sure the bindings here point to parent.
    Iterator<Binding> iterBinding = convertToNodes(chain, nodeTable);
    // "killList" will be aborted on timeout.
    return new QueryIterTDB(iterBinding, killList, input, execCxt);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) TDBException(org.apache.jena.tdb.TDBException) Node(org.apache.jena.graph.Node) Triple(org.apache.jena.graph.Triple) NodeTable(org.apache.jena.tdb.store.nodetable.NodeTable)

Example 7 with TDBException

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

the class IndexAssembler method open.

@Override
public TupleIndex open(Assembler a, Resource root, Mode mode) {
    exactlyOneProperty(root, pDescription);
    String desc = getAsStringValue(root, pDescription).toUpperCase(Locale.ENGLISH);
    exactlyOneProperty(root, pFile);
    String filename = getAsStringValue(root, pFile);
    // Need to get location from the enclosing PGraphAssembler
    if (location != null)
        filename = location.absolute(filename);
    String primary = null;
    RecordFactory rf = null;
    switch(desc.length()) {
        case 3:
            primary = Names.primaryIndexTriples;
            rf = SystemTDB.indexRecordTripleFactory;
            break;
        case 4:
            primary = Names.primaryIndexQuads;
            rf = SystemTDB.indexRecordQuadFactory;
            break;
        default:
            throw new TDBException("Bad length for index description: " + desc);
    }
    // Problems with spotting the index technology.
    //FileSet.fromFilename(filename) ;
    FileSet fileset = null;
    IndexParams idxParams = StoreParams.getDftStoreParams();
    RangeIndex rIndex = IndexFactory.buildRangeIndex(fileset, rf, idxParams);
    return new TupleIndexRecord(desc.length(), new ColumnMap(primary, desc), desc, rf, rIndex);
}
Also used : ColumnMap(org.apache.jena.tdb.lib.ColumnMap) RecordFactory(org.apache.jena.tdb.base.record.RecordFactory) TupleIndexRecord(org.apache.jena.tdb.store.tupletable.TupleIndexRecord) FileSet(org.apache.jena.tdb.base.file.FileSet) TDBException(org.apache.jena.tdb.TDBException) IndexParams(org.apache.jena.tdb.index.IndexParams) RangeIndex(org.apache.jena.tdb.index.RangeIndex)

Example 8 with TDBException

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

the class LocationLock method release.

/**
     * Releases the lock so that other JVMs can use the location
     */
public void release() {
    // Memory locations cannot be locked so nothing to do
    if (location.isMem())
        return;
    int owner = this.getOwner();
    // Nobody owned the lock so nothing to do
    if (owner == NO_OWNER)
        return;
    // Some other process owns the lock so we can't release it
    if (owner != ProcessUtils.getPid(NO_OWNER))
        throw new TDBException("Cannot release the lock on location " + location.getDirectoryPath() + " since this process does not own the lock");
    File lockFile = getLockFile();
    // No lock file exists so nothing to do
    if (!lockFile.exists())
        return;
    // Try and delete the lock file thereby releasing the lock
    if (!lockFile.delete())
        throw new TDBException("Failed to release the lock on location " + location.getDirectoryPath() + ", it may be necessary to manually remove the lock file");
}
Also used : TDBException(org.apache.jena.tdb.TDBException) File(java.io.File)

Example 9 with TDBException

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

the class LocationLock method obtain.

/**
     * Obtains the lock in order to prevent other JVMs using the location
     */
public void obtain() {
    // Memory locations cannot be locked so nothing to do
    if (location.isMem())
        return;
    // Get current owner
    int owner = this.getOwner();
    int pid = ProcessUtils.getPid(NO_OWNER);
    if (owner == NO_OWNER) {
        // No owner currently so try to obtain the lock
        if (pid == NO_OWNER) {
            // In the case where we cannot obtain our PID then we cannot
            // obtain a lock
            SystemTDB.errlog.warn("Location " + location.getDirectoryPath() + " cannot be locked as unable to obtain PID of current process, if another JVM accessed this location while this process is accessing it then data corruption may occur");
            return;
        }
        takeLock(pid);
    } else if (owner == ProcessUtils.getPid(NO_OWNER)) {
    // We already own the lock so nothing to do
    } else {
        // Check if the owner is alive
        if (ProcessUtils.isAlive(owner)) {
            throw new TDBException("Location " + location.getDirectoryPath() + " is currently locked by PID " + owner + "(this process is PID " + pid + ")" + ".  TDB databases do not permit concurrent usage across JVMs so in order to prevent possible data corruption you cannot open this location from the JVM that does not own the lock for the dataset");
        }
        // Otherwise the previous owner is dead so we can take the lock
        takeLock(ProcessUtils.getPid(NO_OWNER));
    }
}
Also used : TDBException(org.apache.jena.tdb.TDBException)

Example 10 with TDBException

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

the class NodeId method extract.

/** Decode an inline nodeID, return null if not an inline node */
public static Node extract(NodeId nodeId) {
    if (nodeId == NodeId.NodeDoesNotExist)
        return null;
    long v = nodeId.value;
    int type = nodeId.type();
    switch(type) {
        case NONE:
            return null;
        case SPECIAL:
            return null;
        case INTEGER:
            {
                long val = IntegerNode.unpack(v);
                Node n = NodeFactory.createLiteral(Long.toString(val), XSDDatatype.XSDinteger);
                return n;
            }
        case DECIMAL:
            {
                BigDecimal d = DecimalNode.unpackAsBigDecimal(v);
                String x = d.toPlainString();
                return NodeFactory.createLiteral(x, XSDDatatype.XSDdecimal);
            }
        case DATETIME:
            {
                long val = BitsLong.clear(v, 56, 64);
                String lex = DateTimeNode.unpackDateTime(val);
                return NodeFactory.createLiteral(lex, XSDDatatype.XSDdateTime);
            }
        case DATE:
            {
                long val = BitsLong.clear(v, 56, 64);
                String lex = DateTimeNode.unpackDate(val);
                return NodeFactory.createLiteral(lex, XSDDatatype.XSDdate);
            }
        case BOOLEAN:
            {
                long val = BitsLong.clear(v, 56, 64);
                if (val == 0)
                    return NodeConst.nodeFalse;
                if (val == 1)
                    return NodeConst.nodeTrue;
                throw new TDBException("Unrecognized boolean node id : " + val);
            }
        default:
            throw new TDBException("Unrecognized node id type: " + type);
    }
}
Also used : Node(org.apache.jena.graph.Node) TDBException(org.apache.jena.tdb.TDBException) BigDecimal(java.math.BigDecimal)

Aggregations

TDBException (org.apache.jena.tdb.TDBException)21 NodeId (org.apache.jena.tdb.store.NodeId)5 Node (org.apache.jena.graph.Node)4 File (java.io.File)2 Record (org.apache.jena.tdb.base.record.Record)2 NodeTable (org.apache.jena.tdb.store.nodetable.NodeTable)2 BufferedWriter (java.io.BufferedWriter)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 BigDecimal (java.math.BigDecimal)1 HashSet (java.util.HashSet)1 JsonArray (org.apache.jena.atlas.json.JsonArray)1 Tuple (org.apache.jena.atlas.lib.tuple.Tuple)1 Triple (org.apache.jena.graph.Triple)1 RiotException (org.apache.jena.riot.RiotException)1 Token (org.apache.jena.riot.tokens.Token)1 Tokenizer (org.apache.jena.riot.tokens.Tokenizer)1 Binding (org.apache.jena.sparql.engine.binding.Binding)1 Block (org.apache.jena.tdb.base.block.Block)1 FileSet (org.apache.jena.tdb.base.file.FileSet)1