Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

json-ld cached doc loader #102

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions util/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ func IsValidStruct(data interface{}) error {
func NewLDProcessor() LDProcessor {
// JSON LD processing
proc := ld.NewJsonLdProcessor()
// Initialize a new doc loader with caching capability
// LDProcessor is expected to be re-used for multiple json-ld operations
docLoader := ld.NewRFC7324CachingDocumentLoader(nil)
options := ld.NewJsonLdOptions("")
options.Format = "application/n-quads"
options.Algorithm = "URDNA2015"
options.ProcessingMode = ld.JsonLd_1_1
options.ProduceGeneralizedRdf = true
options.DocumentLoader = docLoader
return LDProcessor{
JsonLdProcessor: proc,
JsonLdOptions: options,
Expand All @@ -48,6 +52,22 @@ func (l LDProcessor) GetOptions() *ld.JsonLdOptions {
return l.JsonLdOptions
}

func (l LDProcessor) GetContextFromMap(dataMap map[string]interface{}) (*ld.Context, error) {
var activeCtx *ld.Context
var err error
ldCtx := ld.NewContext(nil, l.JsonLdOptions)
contextMap, ok := dataMap["@context"].(map[string]interface{})
if !ok {
activeCtx, err = ldCtx.Parse(dataMap)
} else {
activeCtx, err = ldCtx.Parse(contextMap)
}
if err != nil {
return nil, err
}
return activeCtx, nil
}

func LDNormalize(document interface{}) (interface{}, error) {
processor := NewLDProcessor()
return processor.Normalize(document, processor.GetOptions())
Expand Down
47 changes: 47 additions & 0 deletions util/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package util

import (
"testing"
"time"

"github.com/piprate/json-gold/ld"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -90,3 +92,48 @@ func TestMergeUniqueValues(t *testing.T) {
assert.True(tt, len(res) == len(a)+2)
})
}

func TestLDProcessor(t *testing.T) {
testJSONDLContextURLStr := "http://schema.org/"
ldProcessor := NewLDProcessor()

t.Run("caching document loader", func(tt *testing.T) {
numOfLoads := 5
nonCachedLoader := ld.NewDefaultDocumentLoader(nil)
t0 := time.Now()
for i := 0; i < numOfLoads; i++ {
doc, err := nonCachedLoader.LoadDocument(testJSONDLContextURLStr)
assert.NoError(tt, err)
assert.NotNil(tt, doc)
}
dtNonCached := time.Now().Sub(t0)
tt.Logf("non-cached document loader for %d tests dt: %v\n", numOfLoads, dtNonCached)

ldProcessor := NewLDProcessor()
t1 := time.Now()
for i := 0; i < numOfLoads; i++ {
doc, err := ldProcessor.DocumentLoader.LoadDocument(testJSONDLContextURLStr)
assert.NoError(tt, err)
assert.NotNil(tt, doc)
}
dtCached := time.Now().Sub(t1)
tt.Logf("caching document loader for %d tests dt: %v\n", numOfLoads, dtCached)

assert.True(tt, dtNonCached/dtCached > 2.0)
})

t.Run("get context from map", func(tt *testing.T) {
contextMap := map[string]interface{}{
"dc": "http://purl.org/dc/elements/1.1/",
"ex": "http://example.org/vocab#",
"ex:contains": map[string]interface{}{
"@type": "@id",
},
}

activeCtx, err := ldProcessor.GetContextFromMap(contextMap)
//expected activeCtx output is &{values:map[@base: processingMode:json-ld-1.1] options:0xc0001288f0 termDefinitions:map[dc:map[@id:http://purl.org/dc/elements/1.1/ @reverse:false _prefix:true] ex:map[@id:http://example.org/vocab# @reverse:false _prefix:true] ex:contains:map[@id:http://example.org/vocab#contains @reverse:false @type:@id]] inverse:map[] protected:map[] previousContext:<nil>}
assert.NoError(tt, err)
assert.NotNil(tt, activeCtx)
})
}