Search in sources :

Example 46 with NodeId

use of org.apache.jena.tdb2.store.NodeId in project jena by apache.

the class NodeIdInline method inline$.

private static NodeId inline$(Node node) {
    if (!hasInlineDatatype(node))
        return null;
    LiteralLabel lit = node.getLiteral();
    if (node.getLiteralDatatype().equals(XSDDatatype.XSDdecimal)) {
        // Check lexical form.
        if (!XSDDatatype.XSDdecimal.isValidLiteral(lit))
            return null;
        // Not lit.getValue() because that may be a narrower type e.g. Integer.
        // .trim is how Jena does it but it rather savage. spc, \n \r \t.
        // But at this point we know it's a valid literal so the excessive
        // chopping by .trim is safe.
        BigDecimal decimal = new BigDecimal(lit.getLexicalForm().trim());
        // Does range checking.
        DecimalNode56 dn = DecimalNode56.valueOf(decimal);
        // null is "does not fit"
        if (dn != null)
            // setType
            return NodeId.createRaw(XSD_DECIMAL, dn.pack());
        else
            return null;
    } else {
        // Not decimal.
        if (XSDDatatype.XSDinteger.isValidLiteral(lit)) {
            // Quick check.
            if (lit.getLexicalForm().length() > 19)
                return null;
            // Derived types.
            NodeIdType type = derivedTypeMap.inverse().getOrDefault(lit.getDatatype(), NodeIdType.XSD_INTEGER);
            try {
                long v = ((Number) lit.getValue()).longValue();
                v = IntegerNode.pack56(v);
                // Value -1 is "does not fit"
                if (v == -1)
                    return null;
                return NodeId.createRaw(type, v);
            }// Out of range for the type, not a long etc etc.
             catch (Throwable ex) {
                return null;
            }
        }
    }
    if (XSDDatatype.XSDdouble.isValidLiteral(lit)) {
        double d = ((Number) lit.getValue()).doubleValue();
        long v = DoubleNode62.pack(d);
        if (v == DoubleNode62.NO_ENCODING)
            return null;
        // The special byte encoding of XSD_DOUBLE is handled in NodeIdFactory.encode/.decode.
        return NodeId.createRaw(XSD_DOUBLE, v);
    }
    if (XSDDatatype.XSDfloat.isValidLiteral(lit)) {
        float f = ((Number) lit.getValue()).floatValue();
        long v = FloatNode.pack(f);
        return NodeId.createRaw(XSD_FLOAT, v);
    }
    if (lit.getDatatype().equals(XSDDatatype.XSDdateTimeStamp) && XSDDatatype.XSDdateTimeStamp.isValidLiteral(lit)) {
        long v = DateTimeNode.packDateTime(lit.getLexicalForm());
        if (v == -1)
            return null;
        return NodeId.createRaw(XSD_DATETIMESTAMP, v);
    }
    if (XSDDatatype.XSDdateTime.isValidLiteral(lit)) {
        // Could use the Jena/XSDDateTime object here rather than reparse the lexical form.
        // But this works and it's close to a release ...
        long v = DateTimeNode.packDateTime(lit.getLexicalForm());
        if (v == -1)
            return null;
        return NodeId.createRaw(XSD_DATETIME, v);
    }
    if (XSDDatatype.XSDdate.isValidLiteral(lit)) {
        long v = DateTimeNode.packDate(lit.getLexicalForm());
        if (v == -1)
            return null;
        return NodeId.createRaw(XSD_DATE, v);
    }
    if (XSDDatatype.XSDboolean.isValidLiteral(lit)) {
        long v = 0;
        boolean b = (Boolean) lit.getValue();
        // return new NodeValueBoolean(b, node);
        if (b)
            v = v | 0x01;
        return NodeId.createRaw(XSD_BOOLEAN, v);
    }
    return null;
}
Also used : NodeIdType(org.apache.jena.tdb2.store.NodeIdType) LiteralLabel(org.apache.jena.graph.impl.LiteralLabel) BigDecimal(java.math.BigDecimal)

Example 47 with NodeId

use of org.apache.jena.tdb2.store.NodeId in project jena by apache.

the class DataToTuplesInline method nodes.

private static Tuple<NodeId> nodes(NodeTable nt, Quad quad) {
    NodeId g = idForNode(nt, quad.getGraph());
    NodeId s = idForNode(nt, quad.getSubject());
    NodeId p = idForNode(nt, quad.getPredicate());
    NodeId o = idForNode(nt, quad.getObject());
    return TupleFactory.tuple(g, s, p, o);
}
Also used : NodeId(org.apache.jena.tdb2.store.NodeId)

Example 48 with NodeId

use of org.apache.jena.tdb2.store.NodeId in project jena by apache.

the class NodeTableOps method bulkNodeIdToNodeImpl.

/**
 * Convert a bulk operation into a loop
 */
public static List<Node> bulkNodeIdToNodeImpl(NodeTable nt, List<NodeId> nodeIds) {
    List<Node> nodes = new ArrayList<>(nodeIds.size());
    for (NodeId nodeId : nodeIds) {
        Node n = nt.getNodeForNodeId(nodeId);
        nodes.add(n);
    }
    return nodes;
}
Also used : Node(org.apache.jena.graph.Node) ArrayList(java.util.ArrayList) NodeId(org.apache.jena.tdb2.store.NodeId)

Example 49 with NodeId

use of org.apache.jena.tdb2.store.NodeId in project jena by apache.

the class NodeTableOps method bulkNodeToNodeIdImpl.

/**
 * Convert a bulk operation into a loop
 */
public static List<NodeId> bulkNodeToNodeIdImpl(NodeTable nt, List<Node> nodes, boolean withAllocation) {
    List<NodeId> nodeIds = new ArrayList<>(nodes.size());
    for (Node node : nodes) {
        NodeId nid = withAllocation ? nt.getAllocateNodeId(node) : nt.getNodeIdForNode(node);
        nodeIds.add(nid);
    }
    return nodeIds;
}
Also used : Node(org.apache.jena.graph.Node) NodeId(org.apache.jena.tdb2.store.NodeId) ArrayList(java.util.ArrayList)

Example 50 with NodeId

use of org.apache.jena.tdb2.store.NodeId in project jena by apache.

the class TestNodeIdInline method test.

private void test(String x, Node correct) {
    Node n = NodeFactoryExtra.parseNode(x);
    NodeId nodeId = NodeId.inline(n);
    assertNotNull("Expected inlining: " + x, nodeId);
    boolean b = NodeId.hasInlineDatatype(n);
    assertTrue("Converted NodeId but datatype test was false", b);
    Node n2 = NodeId.extract(nodeId);
    assertNotNull("Expected recovery", n2);
    String s = "(" + correct.getLiteralLexicalForm() + "," + n2.getLiteralLexicalForm() + ")";
    assertTrue("Not same value: " + s, correct.sameValueAs(n2));
    // Term equality.
    assertEquals("Not same term", correct, n2);
}
Also used : Node(org.apache.jena.graph.Node) NodeId(org.apache.jena.tdb2.store.NodeId)

Aggregations

NodeId (org.apache.jena.tdb2.store.NodeId)47 Node (org.apache.jena.graph.Node)24 Tuple (org.apache.jena.atlas.lib.tuple.Tuple)16 TDBException (org.apache.jena.tdb2.TDBException)12 NodeTable (org.apache.jena.tdb2.store.nodetable.NodeTable)11 Quad (org.apache.jena.sparql.core.Quad)7 ArrayList (java.util.ArrayList)6 Var (org.apache.jena.sparql.core.Var)6 Triple (org.apache.jena.graph.Triple)5 NodeTupleTable (org.apache.jena.tdb2.store.nodetupletable.NodeTupleTable)5 Iterator (java.util.Iterator)4 Binding (org.apache.jena.sparql.engine.binding.Binding)4 StatsCollectorNodeId (org.apache.jena.tdb2.solver.stats.StatsCollectorNodeId)4 Predicate (java.util.function.Predicate)3 Iter (org.apache.jena.atlas.iterator.Iter)3 Record (org.apache.jena.dboe.base.record.Record)3 TupleIndex (org.apache.jena.tdb2.store.tupletable.TupleIndex)3 Test (org.junit.Test)3 List (java.util.List)2 Function (java.util.function.Function)2