Search in sources :

Example 1 with TDBException

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

the class NodecSSE method encode.

@Override
public int encode(Node node, ByteBuffer bb, PrefixMapping pmap) {
    String str = null;
    if (node.isURI()) {
        // Pesky spaces etc
        String x = StrUtils.encodeHex(node.getURI(), MarkerChar, invalidIRIChars);
        if (x != node.getURI())
            node = NodeFactory.createURI(x);
    }
    if (node.isLiteral() && NodeUtils.isLangString(node)) {
        // Check syntactically valid.
        String lang = node.getLiteralLanguage();
        if (!LangTag.check(lang))
            throw new TDBException("bad language tag: " + node);
    }
    if (node.isBlank() && !onlySafeBNodeLabels) {
        // Special case.
        str = "_:" + node.getBlankNodeLabel();
    }
    // Node->String
    if (str == null)
        str = NodeFmtLib.str(node, (String) null, pmap0);
    // String -> bytes ;
    BlockUTF8.fromChars(str, bb);
    bb.flip();
    return bb.limit();
}
Also used : TDBException(org.apache.jena.tdb.TDBException)

Example 2 with TDBException

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

the class NodecSSE method decode.

@Override
public Node decode(ByteBuffer bb, PrefixMapping pmap) {
    // Ideally, this would be straight from the byte buffer.
    // But currently we go bytes -> string -> node 
    // Byte -> String
    String str = BlockUTF8.toString(bb);
    // Easy cases.
    if (str.startsWith("_:")) {
        // Must be done this way.
        // In particular, bnode labels can contain ":" from Jena
        // TokenizerText does not recognize these.
        str = str.substring(2);
        return NodeFactory.createBlankNode(str);
    }
    if (str.startsWith("<")) {
        // Do directly.
        // (is it quicker?)
        str = str.substring(1, str.length() - 1);
        str = StrUtils.unescapeString(str);
        str = StrUtils.decodeHex(str, MarkerChar);
        return NodeFactory.createURI(str);
    }
    Tokenizer tokenizer = TokenizerFactory.makeTokenizerString(str);
    if (!tokenizer.hasNext())
        throw new TDBException("Failed to tokenise: " + str);
    Token t = tokenizer.next();
    try {
        Node n = t.asNode();
        if (n == null)
            throw new TDBException("Not a node: " + str);
        return n;
    } catch (RiotException ex) {
        throw new TDBException("Bad string for node: " + str);
    }
}
Also used : RiotException(org.apache.jena.riot.RiotException) TDBException(org.apache.jena.tdb.TDBException) Node(org.apache.jena.graph.Node) Token(org.apache.jena.riot.tokens.Token) Tokenizer(org.apache.jena.riot.tokens.Tokenizer)

Example 3 with TDBException

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

the class TupleTable method find.

/** Find all matching tuples - a slot of NodeId.NodeIdAny (or null) 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 (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.tdb.TDBException) NodeId(org.apache.jena.tdb.store.NodeId)

Example 4 with TDBException

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

the class NodeLib method encodeStore.

public static long encodeStore(Node node, ObjectFile file) {
    // Buffer pool?
    // Nodes can be writtern during reads.
    // Make sure this operation is sync'ed. 
    int maxSize = nodec.maxSize(node);
    Block block = file.allocWrite(maxSize);
    try {
        int len = nodec.encode(node, block.getByteBuffer(), null);
        file.completeWrite(block);
        return block.getId();
    } catch (TDBException ex) {
        file.abortWrite(block);
        throw ex;
    }
}
Also used : TDBException(org.apache.jena.tdb.TDBException) Block(org.apache.jena.tdb.base.block.Block)

Example 5 with TDBException

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

the class NodeLib method setHash.

public static void setHash(Hash h, Node n) {
    NodeType nt = NodeType.lookup(n);
    switch(nt) {
        case URI:
            hash(h, n.getURI(), null, null, nt);
            return;
        case BNODE:
            hash(h, n.getBlankNodeLabel(), null, null, nt);
            return;
        case LITERAL:
            String dt = n.getLiteralDatatypeURI();
            if (NodeUtils.isSimpleString(n) || NodeUtils.isLangString(n)) {
                // RDF 1.1 : No datatype for:
                //   xsd:String as simple literals
                //   rdf:langString and @ 
                dt = null;
            }
            hash(h, n.getLiteralLexicalForm(), n.getLiteralLanguage(), dt, nt);
            return;
        case OTHER:
            throw new TDBException("Attempt to hash something strange: " + n);
    }
    throw new TDBException("NodeType broken: " + n);
}
Also used : NodeType(org.apache.jena.tdb.store.NodeType) TDBException(org.apache.jena.tdb.TDBException)

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