Skip to content

Commit 5b90ec1

Browse files
committed
GH-3404: Tidy up NodeId and DecimalNode56
1 parent d97ac8f commit 5b90ec1

File tree

5 files changed

+58
-43
lines changed

5 files changed

+58
-43
lines changed

jena-arq/src/main/java/org/apache/jena/sparql/util/XSDNumUtils.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,6 @@ public static String canonicalDecimalStrNoIntegerDot(BigDecimal bd) {
147147
return bd.stripTrailingZeros().toPlainString();
148148
}
149149

150-
/** Return a canonical decimal with a trailing ".0". */
151-
public static BigDecimal canonicalDecimalWithDot(BigDecimal decimal) {
152-
BigDecimal result = decimal;
153-
if (result.scale() > 1) {
154-
result = decimal.stripTrailingZeros();
155-
}
156-
if (result.scale() <= 0) {
157-
result = result.setScale(1);
158-
}
159-
return result;
160-
}
161-
162150
/**
163151
* Integer-valued decimals have a trailing ".0".
164152
* (In XML Schema Datatype 1.1 they did not have a ".0".)
@@ -178,4 +166,20 @@ public static String canonicalDecimalStrWithDot(BigDecimal decimal) {
178166
str = str + ".0";
179167
return str;
180168
}
169+
170+
/**
171+
* Return a canonical decimal with a trailing ".0".
172+
* This is the BigDecimal form used to encode NodeIds in TDB2.
173+
* It has a trailing ".0" so it is Turtle compatible.
174+
*/
175+
public static BigDecimal canonicalDecimal(BigDecimal decimal) {
176+
BigDecimal result = decimal;
177+
if (result.scale() > 1) {
178+
result = decimal.stripTrailingZeros();
179+
}
180+
if (result.scale() <= 0) {
181+
result = result.setScale(1);
182+
}
183+
return result;
184+
}
181185
}

jena-tdb2/src/main/java/org/apache/jena/tdb2/store/NodeId.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,24 @@ private final void check(NodeIdType type, int v1, long v2) {
104104
// public long getPtrLo() { return value2; }
105105
// public int getPtrHi() { return value1 & 0x00FFFFFF; }
106106

107-
// 64 bit
107+
/** The pointer part of a NodeId reference. */
108108
public long getPtrLocation() { return value2; }
109-
// Long.
109+
110+
// 96 bit
110111
// public long getPtrLo() { return value2; }
111112
// public int getPtrHi() { return value1; }
112113

113-
public int getTypeValue() { return type.type(); }
114+
// 64 bit.
115+
public int getTypeValue() {
116+
return type.type();
117+
}
118+
119+
/** The value (encoding) part of an inline literal (56 bits) */
120+
public long getValue56() {
121+
return value2;
122+
}
114123

115-
public boolean isInline() {
124+
public boolean isInline() {
116125
return isInline(this);
117126
}
118127

@@ -141,8 +150,11 @@ public boolean isValue() {
141150
// public static boolean isDefined(NodeId nodeId) { return nodeId == NodeIdDefined; }
142151
// public static boolean isUndefined(NodeId nodeId) { return nodeId == NodeIdUndefined; }
143152

144-
/** Create from a long-encoded value */
145-
/*package*/ static NodeId createRaw(NodeIdType type, long value) {
153+
/**
154+
* Create from a long-encoded value.
155+
* Caution: an illegal value for the long argument will cause serious problems.
156+
*/
157+
public static NodeId createRaw(NodeIdType type, long value) {
146158
return new NodeId(type, 0, value);
147159
}
148160

jena-tdb2/src/main/java/org/apache/jena/tdb2/store/NodeIdFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static NodeId createPtr(long lo) {
6868
return createNew(PTR, 0, lo);
6969
}
7070

71-
/*package*/ /*long*/ static NodeId createPtrLong(int hi, long lo) {
71+
/*package*/ static NodeId createPtrLong(int hi, long lo) {
7272
return create(PTR, hi, lo);
7373
}
7474

jena-tdb2/src/main/java/org/apache/jena/tdb2/store/NodeIdInline.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ public static boolean hasInlineDatatype(Node node) {
163163
// But at this point we know it's a valid literal so the excessive
164164
// chopping by .trim is safe.
165165
BigDecimal decimal = new BigDecimal(lit.getLexicalForm().trim());
166-
decimal = XSDNumUtils.canonicalDecimalWithDot(decimal);
166+
if ( false )
167+
decimal = XSDNumUtils.canonicalDecimal(decimal);
167168

168169
// Does range checking.
169170
DecimalNode56 dn = DecimalNode56.valueOf(decimal);

jena-tdb2/src/main/java/org/apache/jena/tdb2/store/value/DecimalNode56.java

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,41 +23,39 @@
2323

2424
import org.apache.jena.atlas.lib.BitsLong;
2525

26-
2726
// Decimal packed into 56 bits.
2827
public class DecimalNode56
2928
{
3029
//private static Logger log = LoggerFactory.getLogger(DecimalNode.class);
3130

32-
BigDecimal decimal = null;
33-
34-
// signed 8 bits of scale, signed 48 bits of value.
35-
// Decimal precision is 47 bits (it's signed) or around 14 places.
36-
// Not finance industry accuracy nor XSD (18 places minimum) but still useful.
31+
// 56 bits signed 8 bits of scale, signed 48 bits of value.
32+
// Decimal precision is 47 bits (it's signed) or around 13 places (canonical form).
33+
// This is not finance industry accuracy nor XSD (18 places minimum) but still useful.
3734

38-
static final int SCALE_LEN = 8;
39-
static final int VALUE_LEN = 48;
40-
static final int ENC_LEN = 48 + SCALE_LEN;
35+
private static final int SCALE_LEN = 8;
36+
private static final int VALUE_LEN = 48;
37+
private static final int ENC_LEN = 48 + SCALE_LEN;
4138

42-
static final long MAX_VALUE = (1L << (VALUE_LEN - 1)) - 1;
43-
static final long MIN_VALUE = -(1L << (VALUE_LEN - 1));
39+
private static final long MAX_VALUE = (1L << (VALUE_LEN - 1)) - 1;
40+
private static final long MIN_VALUE = -(1L << (VALUE_LEN - 1));
4441

45-
static final int MAX_SCALE = (1 << (SCALE_LEN - 1)) - 1;
46-
static final int MIN_SCALE = -(1 << (SCALE_LEN - 1));
42+
private static final int MAX_SCALE = (1 << (SCALE_LEN - 1)) - 1;
43+
private static final int MIN_SCALE = -(1 << (SCALE_LEN - 1));
4744

48-
static final BigInteger MAX_I = BigInteger.valueOf(MAX_VALUE);
49-
static final BigInteger MIN_I = BigInteger.valueOf(MIN_VALUE);
45+
private static final BigInteger MAX_I = BigInteger.valueOf(MAX_VALUE);
46+
private static final BigInteger MIN_I = BigInteger.valueOf(MIN_VALUE);
5047

51-
// Bits counts
52-
static private int SCALE_LO = 56 - SCALE_LEN;
53-
static private int SCALE_HI = 56; // Exclusive
54-
// index
48+
// Bits positions [LO, HI)
49+
private static final int SCALE_LO = ENC_LEN - SCALE_LEN;
50+
private static final int SCALE_HI = ENC_LEN;
5551

56-
static private int VALUE_LO = 0;
57-
static private int VALUE_HI = VALUE_LO + VALUE_LEN;
52+
private static final int VALUE_LO = 0;
53+
private static final int VALUE_HI = VALUE_LO + VALUE_LEN;
5854

59-
private int scale;
60-
private long value;
55+
// Object fields.
56+
private BigDecimal decimal = null;
57+
private int scale;
58+
private long value;
6159

6260
public static DecimalNode56 valueOf(BigDecimal decimal) {
6361
int scale = decimal.scale();

0 commit comments

Comments
 (0)