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;
}
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);
}
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;
}
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;
}
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);
}
Aggregations