Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update queries #3597

Merged
merged 4 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 9 additions & 1 deletion src/commands/protocols/get/sender/local-get-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class LocalGetCommand extends Command {
this.paranetService = ctx.paranetService;
this.ualService = ctx.ualService;
this.repositoryModuleManager = ctx.repositoryModuleManager;
this.blockchainModuleManager = ctx.blockchainModuleManager;

this.errorType = ERROR_TYPE.GET.GET_LOCAL_ERROR;
}
Expand All @@ -31,10 +32,10 @@ class LocalGetCommand extends Command {
includeMetadata,
contract,
knowledgeCollectionId,
knowledgeAssetId,
contentType,
assertionId,
} = command.data;
let { knowledgeAssetId } = command.data;
await this.operationIdService.updateOperationIdStatus(
operationId,
blockchain,
Expand Down Expand Up @@ -116,6 +117,13 @@ class LocalGetCommand extends Command {
break;
}
}
// TODO: Do this in clean way
if (!knowledgeAssetId) {
knowledgeAssetId = this.blockchainModuleManager.getKnowledgeAssetsRange(
blockchain,
knowledgeCollectionId,
);
}

if (!result?.length) {
result = await this.tripleStoreService.getAssertion(
Expand Down
6 changes: 6 additions & 0 deletions src/modules/blockchain/blockchain-module-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ class BlockchainModuleManager extends BaseModuleManager {
]);
}

async getKnowledgeAssetsRange(blockchain, knowledgeCollectionId) {
return this.callImplementationFunction(blockchain, 'getKnowledgeAssetsRange', [
knowledgeCollectionId,
]);
}

async getParanetKnowledgeAssetsCount(blockchain, paranetId) {
return this.callImplementationFunction(blockchain, 'getParanetKnowledgeAssetsCount', [
paranetId,
Expand Down
17 changes: 17 additions & 0 deletions src/modules/blockchain/implementation/web3-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,23 @@ class Web3Service {
return Number(knowledgeCollectionSize);
}

async getKnowledgeAssetsRange(assetStorageContractAddress, knowledgeCollectionId) {
const assetStorageContractInstance =
this.assetStorageContracts[assetStorageContractAddress.toString().toLowerCase()];
if (!assetStorageContractInstance)
throw new Error('Unknown asset storage contract address');
const knowledgeAssetsRange = await this.callContractFunction(
assetStorageContractInstance,
'getKnowledgeAssetsRange',
[knowledgeCollectionId],
);
return {
startTokenId: knowledgeAssetsRange[0],
endTokenId: knowledgeAssetsRange[1],
burned: knowledgeAssetsRange[2],
};
}

async getMinimumStake() {
const minimumStake = await this.callContractFunction(
this.contracts.ParametersStorage,
Expand Down
64 changes: 30 additions & 34 deletions src/modules/triple-store/implementation/ot-triple-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,52 +291,48 @@ class OtTripleStore {
await this.queryVoid(repository, query);
}

async getKnowledgeCollectionNamedGraphs(repository, ual, visibility) {
async getKnowledgeCollectionNamedGraphs(repository, ual, tokenIds, visibility) {
const namedGraphs = Array.from(
{ length: tokenIds.endTokenId - tokenIds.startTokenId + 1 },
(_, i) => tokenIds.startTokenId + i,
)
.filter((id) => !tokenIds.burned.includes(id))
.map((id) => `${ual}/${id}`);
const assertion = {};
if (visibility === TRIPLES_VISIBILITY.PUBLIC || visibility === TRIPLES_VISIBILITY.ALL) {
const query = `
PREFIX schema: <http://schema.org/>
CONSTRUCT {
?s ?p ?o .
}
WHERE {
{
SELECT ?s ?p ?o ?g
WHERE {
GRAPH ?g {
?s ?p ?o .
}
FILTER (
STRSTARTS(STR(?g), "${ual}")
&& STRENDS(STR(?g), "${TRIPLES_VISIBILITY.PUBLIC}")
)
}
ORDER BY ?g ?s ?p ?o
}
WHERE {
GRAPH ?g {
?s ?p ?o .
}
}`;
VALUES ?g {
${namedGraphs
.map((graph) => `${graph}/${TRIPLES_VISIBILITY.PUBLIC}`)
.join('\n')}
}
}`;
assertion.public = await this.construct(repository, query);
}
if (visibility === TRIPLES_VISIBILITY.PRIVATE || visibility === TRIPLES_VISIBILITY.ALL) {
const query = `
PREFIX schema: <http://schema.org/>
CONSTRUCT {
?s ?p ?o .
PREFIX schema: <http://schema.org/>
CONSTRUCT {
?s ?p ?o .
}
WHERE {
GRAPH ?g {
?s ?p ?o .
}
WHERE {
{
SELECT ?s ?p ?o ?g
WHERE {
GRAPH ?g {
?s ?p ?o .
}
FILTER (
STRSTARTS(STR(?g), "${ual}")
&& STRENDS(STR(?g), "${TRIPLES_VISIBILITY.PRIVATE}")
)
}
ORDER BY ?g ?s ?p ?o
}
}`;
VALUES ?g {
${namedGraphs
.map((graph) => `${graph}/${TRIPLES_VISIBILITY.PRIVATE}`)
.join('\n')}
}
}`;
assertion.private = await this.construct(repository, query);
}

Expand Down
11 changes: 5 additions & 6 deletions src/service/triple-store-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,14 +380,12 @@ class TripleStoreService {
repository = TRIPLE_STORE_REPOSITORY.DKG,
) {
// TODO: Use stateId
const ual = `did:dkg:${blockchain}/${contract}/${knowledgeCollectionId}${
knowledgeAssetId ? `/${knowledgeAssetId}` : ''
}`;

this.logger.debug(`Getting Assertion with the UAL: ${ual}.`);
let ual = `did:dkg:${blockchain}/${contract}/${knowledgeCollectionId}`;

let nquads;
if (knowledgeAssetId) {
if (typeof knowledgeAssetId === 'string') {
ual = `${ual}/${knowledgeAssetId}`;
this.logger.debug(`Getting Assertion with the UAL: ${ual}.`);
nquads = await this.tripleStoreModuleManager.getKnowledgeAssetNamedGraph(
this.repositoryImplementations[repository],
repository,
Expand All @@ -396,6 +394,7 @@ class TripleStoreService {
visibility,
);
} else {
this.logger.debug(`Getting Assertion with the UAL: ${ual}.`);
nquads = await this.tripleStoreModuleManager.getKnowledgeCollectionNamedGraphs(
this.repositoryImplementations[repository],
repository,
Expand Down
Loading