Skip to content

Commit 975be31

Browse files
authored
GH-5529 simplify LmdbIRI so that it's more obvious how the initialisation works (#5530)
2 parents 34aa42b + 66fe084 commit 975be31

5 files changed

Lines changed: 147 additions & 22 deletions

File tree

core/model-api/src/main/java/org/eclipse/rdf4j/model/base/AbstractIRI.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,22 @@ private int split() {
124124
}
125125
}
126126

127+
@Override
128+
public int hashCode() {
129+
return iri.hashCode();
130+
}
131+
132+
@Override
133+
public boolean equals(Object o) {
134+
if (o == this) {
135+
return true;
136+
}
137+
if (!(o instanceof IRI)) {
138+
return false;
139+
}
140+
return iri.equals(o.toString());
141+
}
142+
127143
}
128144

129145
}

core/model/src/main/java/org/eclipse/rdf4j/model/impl/SimpleIRI.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,20 @@ public String getLocalName() {
123123
return iriString.substring(localNameIdx);
124124
}
125125

126+
@Override
127+
public int hashCode() {
128+
return iriString.hashCode();
129+
}
130+
131+
@Override
132+
public boolean equals(Object o) {
133+
if (o == this) {
134+
return true;
135+
}
136+
if (!(o instanceof IRI)) {
137+
return false;
138+
}
139+
140+
return iriString.equals(o.toString());
141+
}
126142
}

core/sail/lmdb/src/main/java/org/eclipse/rdf4j/sail/lmdb/ValueStore.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1623,7 +1623,8 @@ private LmdbIRI data2uri(long id, byte[] data, LmdbIRI value) throws IOException
16231623
if (value == null) {
16241624
return new LmdbIRI(revision, namespace, localName, id);
16251625
} else {
1626-
value.setIRIString(namespace + localName);
1626+
value.setNamespaceAndIri(namespace, localName);
1627+
// value.setIRIString(namespace + localName);
16271628
return value;
16281629
}
16291630
}

core/sail/lmdb/src/main/java/org/eclipse/rdf4j/sail/lmdb/model/LmdbIRI.java

Lines changed: 105 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
package org.eclipse.rdf4j.sail.lmdb.model;
1212

1313
import java.io.ObjectStreamException;
14+
import java.util.Objects;
1415

15-
import org.eclipse.rdf4j.model.impl.SimpleIRI;
16+
import org.eclipse.rdf4j.model.IRI;
17+
import org.eclipse.rdf4j.model.util.URIUtil;
1618
import org.eclipse.rdf4j.sail.lmdb.ValueStoreRevision;
1719
import org.slf4j.Logger;
1820
import org.slf4j.LoggerFactory;
1921

20-
public class LmdbIRI extends SimpleIRI implements LmdbResource {
22+
public class LmdbIRI implements LmdbResource, IRI {
2123

2224
private static final long serialVersionUID = -5888138591826143179L;
2325
private static final Logger log = LoggerFactory.getLogger(LmdbIRI.class);
@@ -31,13 +33,21 @@ public class LmdbIRI extends SimpleIRI implements LmdbResource {
3133
private volatile long internalID;
3234

3335
private volatile boolean initialized = false;
36+
/**
37+
* The IRI string.
38+
*/
39+
private String iriString;
40+
41+
/**
42+
* An index indicating the first character of the local name in the IRI string, -1 if not yet set.
43+
*/
44+
private int localNameIdx;
3445

3546
/*--------------*
3647
* Constructors *
3748
*--------------*/
3849

3950
public LmdbIRI(ValueStoreRevision revision, long internalID) {
40-
super();
4151
setInternalID(internalID, revision);
4252
}
4353

@@ -46,17 +56,19 @@ public LmdbIRI(ValueStoreRevision revision, String uri) {
4656
}
4757

4858
public LmdbIRI(ValueStoreRevision revision, String uri, long internalID) {
49-
super(uri);
59+
setIRIString(uri);
5060
setInternalID(internalID, revision);
5161
this.initialized = true;
5262
}
5363

54-
public LmdbIRI(ValueStoreRevision revision, String namespace, String localname) {
55-
this(revision, namespace + localname);
64+
public LmdbIRI(ValueStoreRevision revision, String namespace, String localName) {
65+
this(revision, namespace, localName, UNKNOWN_ID);
5666
}
5767

58-
public LmdbIRI(ValueStoreRevision revision, String namespace, String localname, long internalID) {
59-
this(revision, namespace + localname, internalID);
68+
public LmdbIRI(ValueStoreRevision revision, String namespace, String localName, long internalID) {
69+
this.revision = revision;
70+
setNamespaceAndIri(namespace, localName);
71+
setInternalID(internalID, revision);
6072
}
6173

6274
/*---------*
@@ -79,31 +91,50 @@ public long getInternalID() {
7991
return internalID;
8092
}
8193

82-
@Override
83-
public void setIRIString(String iriString) {
84-
super.setIRIString(iriString);
94+
private void setIRIString(String iriString) {
95+
Objects.requireNonNull(iriString, "iriString must not be null");
96+
97+
if (iriString.indexOf(':') < 0) {
98+
throw new IllegalArgumentException("Not a valid (absolute) IRI: " + iriString);
99+
}
100+
101+
this.iriString = iriString;
102+
this.localNameIdx = -1;
85103
}
86104

87105
@Override
88106
public String getNamespace() {
107+
89108
init();
90-
return super.getNamespace();
109+
if (localNameIdx < 0) {
110+
localNameIdx = URIUtil.getLocalNameIndex(iriString);
111+
}
112+
113+
return iriString.substring(0, localNameIdx);
91114
}
92115

93116
@Override
94117
public String getLocalName() {
118+
95119
init();
96-
return super.getLocalName();
120+
if (localNameIdx < 0) {
121+
localNameIdx = URIUtil.getLocalNameIndex(iriString);
122+
}
123+
124+
return iriString.substring(localNameIdx);
97125
}
98126

99127
@Override
100128
public String stringValue() {
129+
if (iriString != null) {
130+
return iriString;
131+
}
101132
init();
102-
return super.stringValue();
133+
return iriString;
103134
}
104135

105136
public void init() {
106-
if (!initialized) {
137+
if (iriString == null && !initialized) {
107138
synchronized (this) {
108139
if (!initialized) {
109140
boolean resolved = revision.resolveValue(internalID, this);
@@ -122,20 +153,75 @@ public boolean equals(Object o) {
122153
return true;
123154
}
124155

125-
if (o instanceof LmdbIRI && internalID != UNKNOWN_ID) {
156+
if (o == null) {
157+
return false;
158+
}
159+
160+
if (o.getClass() == LmdbIRI.class) {
161+
if (internalID == UNKNOWN_ID) {
162+
boolean equals = stringValue().equals(((IRI) o).stringValue());
163+
if (equals && revision.equals(((LmdbIRI) o).revision)) {
164+
internalID = ((LmdbIRI) o).internalID;
165+
}
166+
return equals;
167+
}
168+
126169
LmdbIRI otherLmdbURI = (LmdbIRI) o;
127170

128-
if (otherLmdbURI.internalID != UNKNOWN_ID && revision.equals(otherLmdbURI.revision)) {
171+
if (revision.equals(otherLmdbURI.revision)) {
172+
if (otherLmdbURI.internalID == UNKNOWN_ID) {
173+
boolean equals = stringValue().equals(((IRI) o).stringValue());
174+
if (equals) {
175+
otherLmdbURI.internalID = this.internalID;
176+
}
177+
return equals;
178+
}
179+
129180
// LmdbURI's from the same revision of the same lmdb store, with
130181
// both ID's set
131-
return internalID == otherLmdbURI.internalID;
182+
boolean equal = internalID == otherLmdbURI.internalID;
183+
if (equal) {
184+
if (iriString == null) {
185+
iriString = otherLmdbURI.iriString;
186+
localNameIdx = otherLmdbURI.localNameIdx;
187+
} else if (otherLmdbURI.iriString == null) {
188+
otherLmdbURI.iriString = iriString;
189+
otherLmdbURI.localNameIdx = localNameIdx;
190+
}
191+
}
192+
return equal;
132193
}
133194
}
134-
return super.equals(o);
195+
196+
if (!(o instanceof IRI)) {
197+
return false;
198+
}
199+
200+
return stringValue().equals(((IRI) o).stringValue());
201+
}
202+
203+
@Override
204+
public int hashCode() {
205+
if (this.iriString != null) {
206+
return this.iriString.hashCode();
207+
}
208+
209+
init();
210+
return iriString.hashCode();
135211
}
136212

137213
protected Object writeReplace() throws ObjectStreamException {
138214
init();
139215
return this;
140216
}
217+
218+
@Override
219+
public String toString() {
220+
return stringValue();
221+
}
222+
223+
public void setNamespaceAndIri(String namespace, String localName) {
224+
localNameIdx = namespace.length();
225+
this.iriString = namespace + localName;
226+
}
141227
}

core/sail/lmdb/src/main/java/org/eclipse/rdf4j/sail/lmdb/model/LmdbLiteral.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public LmdbLiteral(ValueStoreRevision revision, long internalID) {
6767
}
6868

6969
public LmdbLiteral(ValueStoreRevision revision, String label, long internalID) {
70+
assert label != null;
7071
this.label = label;
7172
coreDatatype = CoreDatatype.XSD.STRING;
7273
datatype = CoreDatatype.XSD.STRING.getIri();
@@ -79,6 +80,7 @@ public LmdbLiteral(ValueStoreRevision revision, String label, String lang) {
7980
}
8081

8182
public LmdbLiteral(ValueStoreRevision revision, String label, String lang, long internalID) {
83+
assert label != null;
8284
this.label = label;
8385
this.language = lang;
8486
coreDatatype = CoreDatatype.RDF.LANGSTRING;
@@ -100,6 +102,7 @@ public LmdbLiteral(ValueStoreRevision revision, String label, CoreDatatype datat
100102
}
101103

102104
public LmdbLiteral(ValueStoreRevision revision, String label, IRI datatype, long internalID) {
105+
assert label != null;
103106
this.label = label;
104107
this.datatype = datatype;
105108
this.coreDatatype = null;
@@ -109,6 +112,7 @@ public LmdbLiteral(ValueStoreRevision revision, String label, IRI datatype, long
109112

110113
public LmdbLiteral(ValueStoreRevision revision, String label, IRI datatype, CoreDatatype coreDatatype,
111114
long internalID) {
115+
assert label != null;
112116
this.label = label;
113117
assert datatype != null;
114118
assert coreDatatype != null;
@@ -120,6 +124,7 @@ public LmdbLiteral(ValueStoreRevision revision, String label, IRI datatype, Core
120124
}
121125

122126
public LmdbLiteral(ValueStoreRevision revision, String label, CoreDatatype coreDatatype, long internalID) {
127+
assert label != null;
123128
this.label = label;
124129
this.coreDatatype = coreDatatype;
125130
this.datatype = coreDatatype.getIri();
@@ -196,9 +201,10 @@ public void init() {
196201
if (!initialized) {
197202
synchronized (this) {
198203
if (!initialized) {
199-
revision.resolveValue(internalID, this);
204+
boolean resolved = revision.resolveValue(internalID, this);
205+
initialized = resolved;
206+
assert resolved;
200207
}
201-
initialized = true;
202208
}
203209
}
204210
}

0 commit comments

Comments
 (0)