Skip to content

Commit 4a395db

Browse files
authored
Merge pull request #86 from michaeladler/perf/parse-from-rdf
perf: skip redundant n-quads duplicate checks when parsing RDF
2 parents 0d71f10 + dc94d70 commit 4a395db

File tree

4 files changed

+10
-2
lines changed

4 files changed

+10
-2
lines changed

ld/api_from_rdf.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func (api *JsonLdApi) FromRDF(dataset *RDFDataset, opts *JsonLdOptions) ([]inter
158158
}
159159

160160
// 3.5.6+7)
161-
MergeValue(node.Values, predicate, value)
161+
mergeValue(node.Values, predicate, value, dataset.parsedWithoutDuplicates)
162162

163163
// 3.5.8)
164164
if IsBlankNode(object) || IsIRI(object) {

ld/rdf_dataset.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ type RDFDataset struct {
9898
Graphs map[string][]*Quad
9999

100100
context map[string]string
101+
102+
// indicates that this dataset has had no duplicates (guaranteed by the serializer) at the time it's been parsed
103+
parsedWithoutDuplicates bool
101104
}
102105

103106
// RDFSerializer can serialize and de-serialize RDFDatasets.

ld/serialize_nquads.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ func ParseNQuadsFrom(o interface{}) (*RDFDataset, error) {
239239

240240
// maintain a set of triples for each graph to check for duplicates
241241
triplesByGraph := make(map[string]map[Quad]struct{})
242+
dataset.parsedWithoutDuplicates = true // the following code ensures that no duplicate quads are added
242243

243244
scanner, err := newScannerFor(o)
244245
if err != nil {

ld/utils.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ func deepContains(values []interface{}, value interface{}) bool {
130130

131131
// MergeValue adds a value to a subject. If the value is an array, all values in the array will be added.
132132
func MergeValue(obj map[string]interface{}, key string, value interface{}) {
133+
mergeValue(obj, key, value, false)
134+
}
135+
136+
func mergeValue(obj map[string]interface{}, key string, value interface{}, assumeNoDuplicates bool) {
133137
if obj == nil {
134138
return
135139
}
@@ -140,7 +144,7 @@ func MergeValue(obj map[string]interface{}, key string, value interface{}) {
140144
}
141145
valueMap, isMap := value.(map[string]interface{})
142146
_, valueContainsList := valueMap["@list"]
143-
if key == "@list" || (isMap && valueContainsList) || !deepContains(values, value) {
147+
if key == "@list" || (isMap && valueContainsList) || assumeNoDuplicates || !deepContains(values, value) {
144148
values = append(values, value)
145149
}
146150
obj[key] = values

0 commit comments

Comments
 (0)