Skip to content

Commit

Permalink
Skohub: Simplify getConcepts (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefandesu committed Jun 21, 2022
1 parent ce8480c commit 9a95c23
Showing 1 changed file with 11 additions and 23 deletions.
34 changes: 11 additions & 23 deletions src/providers/skohub-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,44 +121,32 @@ export default class SkohubProvider extends BaseProvider {
}
}

async getConcepts({ concepts, ...config }) {
async getConcepts({ concepts }) {
if (!_.isArray(concepts)) {
concepts = [concepts]
}
concepts = concepts.map(c => ({ uri: c.uri, inScheme: c.inScheme }))

const newConcepts = []
for (let concept of concepts) {
const { uri, inScheme } = concept

if (!(inScheme && inScheme[0] && inScheme[0].uri)) {
throw new errors.InvalidOrMissingParameterError({ parameter: "inScheme", message: "Missing inScheme URI" })
}

var scheme = this._jskos.schemes.find(s => s.uri === inScheme[0].uri)
if (scheme) {
scheme = await this._loadScheme(scheme, config)
}
if (!scheme) {
continue
}
const result = []

for (const concept of concepts) {
const { uri } = concept
const found = this._cache[uri]
if (found) {
newConcepts.push(found)
} else if (_.last(scheme.concepts) === null) {
result.push(found)
} else {
try {
const loaded = await this._loadConcept(uri)
if (loaded) {
newConcepts.push(loaded)
result.push(loaded)
this._cache[loaded.uri] = loaded
}
} catch (error) {
// Ignore error
}
}
}
return newConcepts

return result
}

async getAncestors({ concept, ...config }) {
Expand All @@ -173,7 +161,6 @@ export default class SkohubProvider extends BaseProvider {
return []
}
const broader = concept.broader[0]
broader.inScheme = concept.inScheme
return [broader].concat(await this.getAncestors({ concept: broader, ...config })).map(c => ({ uri: c.uri }))
}

Expand Down Expand Up @@ -231,7 +218,7 @@ export default class SkohubProvider extends BaseProvider {
// 2. Use Flexsearch to get result URIs from index
const result = index.search(search)
// 3. Load concept data for results
const concepts = await this.getConcepts({ concepts: result.map(uri => ({ uri, inScheme: [scheme] })) })
const concepts = await this.getConcepts({ concepts: result.map(uri => ({ uri })) })
return concepts.slice(0, limit)
}

Expand Down Expand Up @@ -304,3 +291,4 @@ export default class SkohubProvider extends BaseProvider {
}

SkohubProvider.providerName = "Skohub"
SkohubProvider.providerType = "http://bartoc.org/api-type/skohub"

0 comments on commit 9a95c23

Please sign in to comment.