-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1934 from OriginTrail/v6/prerelease/testnet
OriginTrail 6.0.0-beta.1.35 Testnet Release
- Loading branch information
Showing
18 changed files
with
2,140 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
const axios = require('axios'); | ||
const qs = require('qs'); | ||
const constants = require('../modules/constants'); | ||
|
||
class FusekiService { | ||
constructor(config) { | ||
this.config = config; | ||
} | ||
|
||
async initialize(logger) { | ||
this.logger = logger; | ||
this.config.axios = { | ||
method: 'post', | ||
url: `${this.config.url}/${this.config.repositoryName}`, | ||
}; | ||
this.logger.info('Fuseki module initialized successfully'); | ||
} | ||
|
||
async insert(triples, rootHash) { | ||
const askQuery = `ASK WHERE { GRAPH <${rootHash}> { ?s ?p ?o } }`; | ||
const exists = await this.ask(askQuery); | ||
if (!exists) { | ||
this.config.axios = { | ||
method: 'put', | ||
url: `${this.config.url}/${this.config.repositoryName}/data?graph=${rootHash}`, | ||
headers: { | ||
'Content-Type': 'application/n-quads', | ||
}, | ||
data: triples, | ||
}; | ||
|
||
await axios(this.config.axios).then(() => true) | ||
.catch((error) => { | ||
this.logger.error({ | ||
msg: `Failed to write into Fuseki: ${error} - ${error.stack}`, | ||
Event_name: constants.ERROR_TYPE.TRIPLE_STORE_INSERT_ERROR, | ||
}); | ||
return false; | ||
}); | ||
} | ||
// TODO: else -> Should log if we already have data | ||
} | ||
|
||
async execute(query) { | ||
return new Promise(async (accept, reject) => { | ||
const data = qs.stringify({ | ||
query, | ||
}); | ||
this.config.axios = { | ||
method: 'post', | ||
url: `${this.config.url}/${this.config.repositoryName}/sparql`, | ||
headers: { | ||
Accept: 'application/sparql-results+json', | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}, | ||
data, | ||
}; | ||
axios(this.config.axios).then((response) => { | ||
accept(response.data); | ||
}).catch((e) => reject(e)); | ||
}); | ||
} | ||
|
||
async construct(query) { | ||
return new Promise(async (accept, reject) => { | ||
const data = qs.stringify({ | ||
query, | ||
}); | ||
this.config.axios = { | ||
method: 'post', | ||
url: `${this.config.url}/${this.config.repositoryName}/sparql`, | ||
headers: { | ||
Accept: 'application/n-quads', | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}, | ||
data, | ||
}; | ||
axios(this.config.axios).then((response) => { | ||
accept(response.data); | ||
}).catch((e) => reject(e)); | ||
}); | ||
} | ||
|
||
async ask(query) { | ||
return new Promise(async (accept, reject) => { | ||
const data = qs.stringify({ | ||
query, | ||
}); | ||
this.config.axios = { | ||
method: 'post', | ||
url: `${this.config.url}/${this.config.repositoryName}/sparql`, | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}, | ||
data, | ||
}; | ||
axios(this.config.axios).then((response) => { | ||
accept(response.data.boolean); | ||
}).catch((e) => reject(e)); | ||
}); | ||
} | ||
|
||
async resolve(uri) { | ||
const query = `PREFIX schema: <http://schema.org/> | ||
CONSTRUCT { ?s ?p ?o } | ||
WHERE { | ||
GRAPH <${constants.DID_PREFIX}:${uri}> { | ||
?s ?p ?o | ||
} | ||
}`; | ||
const nquads = await this.construct(query); | ||
return nquads; | ||
} | ||
|
||
async assertionsByAsset(uri) { | ||
const query = `PREFIX schema: <http://schema.org/> | ||
SELECT ?assertionId ?issuer ?timestamp | ||
WHERE { | ||
?assertionId schema:hasUALs "${uri}" ; | ||
schema:hasTimestamp ?timestamp ; | ||
schema:hasIssuer ?issuer . | ||
} | ||
ORDER BY DESC(?timestamp)`; | ||
const result = await this.execute(query); | ||
|
||
return result.results.bindings; | ||
} | ||
|
||
async findAssertions(nquads) { | ||
const query = `SELECT ?g | ||
WHERE { | ||
GRAPH ?g { | ||
${nquads} | ||
} | ||
}`; | ||
let graph = await this.execute(query); | ||
graph = graph.results.bindings.map((x) => x.g.value.replace(`${constants.DID_PREFIX}:`, '')); | ||
if (graph.length && graph[0] === 'http://www.bigdata.com/rdf#nullGraph') { | ||
return []; | ||
} | ||
return graph; | ||
} | ||
|
||
async findAssertionsByKeyword(query, options, localQuery) { | ||
const sparqlQuery = `PREFIX schema: <http://schema.org/> | ||
SELECT distinct ?assertionId | ||
WHERE { | ||
?assertionId schema:hasKeywords ?keyword . | ||
${!localQuery ? ' ?assertionId schema:hasVisibility "public" .' : ''} | ||
${options.prefix ? `FILTER contains(lcase(?keyword),'${query}')` : `FILTER (lcase(?keyword) = '${query}')`} | ||
} | ||
${options.limit ? `LIMIT ${options.limit}` : ''}`; | ||
const result = await this.execute(sparqlQuery); | ||
return result.results.bindings; | ||
} | ||
|
||
async findAssetsByKeyword(query, options, localQuery) { | ||
const sparqlQuery = `PREFIX schema: <http://schema.org/> | ||
SELECT ?assertionId ?assetId | ||
WHERE { | ||
?assertionId schema:hasTimestamp ?latestTimestamp ; | ||
${!localQuery ? 'schema:hasVisibility "public" ;' : ''} | ||
schema:hasUALs ?assetId . | ||
{ | ||
SELECT ?assetId (MAX(?timestamp) AS ?latestTimestamp) | ||
WHERE { | ||
?assertionId schema:hasKeywords ?keyword ; | ||
schema:hasIssuer ?issuer ; | ||
schema:hasType ?type ; | ||
schema:hasTimestamp ?timestamp ; | ||
schema:hasUALs ?assetId . | ||
${options.prefix ? `FILTER contains(lcase(?keyword),'${query}')` : `FILTER (lcase(?keyword) = '${query}')`} | ||
${options.issuers ? `FILTER (?issuer IN (${JSON.stringify(options.issuers).slice(1, -1)}))` : ''} | ||
${options.types ? `FILTER (?type IN (${JSON.stringify(options.types).slice(1, -1)}))` : ''} | ||
} | ||
GROUP BY ?assetId | ||
${options.limit ? `LIMIT ${options.limit}` : ''} | ||
} | ||
}`; | ||
const result = await this.execute(sparqlQuery); | ||
return result.results.bindings; | ||
} | ||
|
||
async healthCheck() { | ||
try { | ||
const response = await axios.get(`${this.config.url}/$/ping`, {}); | ||
if (response.data !== null) { | ||
return true; | ||
} | ||
return false; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
|
||
getName() { | ||
return 'Fuseki'; | ||
} | ||
} | ||
|
||
module.exports = FusekiService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#/lib/systemd/system/fuseki.service | ||
|
||
[Unit] | ||
Description=Fuseki - OriginTrail V6 Stage 1 Beta Node | ||
Documentation=https://github.com/OriginTrail/ot-node | ||
After=network.target | ||
|
||
[Service] | ||
Environment=JVM_ARGS=-Xmx4G | ||
Type=simple | ||
User=root | ||
WorkingDirectory=/root/fuseki | ||
ExecStart=/usr/bin/java -jar /root/fuseki/fuseki-server.jar --update --set tdb:unionDefaultGraph=true --loc /root/fuseki/tdb /node0 | ||
Restart=on-failure | ||
|
||
[Install] | ||
WantedBy=multi-user.target |
Oops, something went wrong.