Skip to content

Commit

Permalink
Fix/slow query performance for dot notation queries (#12)
Browse files Browse the repository at this point in the history
* chore: version bumps

* fix: simplify getRelationshipToIdentity

* refactor: comments + clearer structure

* chore: use object like the superclass
  • Loading branch information
jkoenig134 authored Feb 6, 2024
1 parent 34494b2 commit c3fcf5e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/runtime/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nmshd/runtime",
"version": "3.5.0",
"version": "3.5.1",
"description": "The enmeshed client runtime.",
"homepage": "https://enmeshed.eu",
"repository": {
Expand Down Expand Up @@ -67,7 +67,7 @@
"@nmshd/consumption": "3.9.2",
"@nmshd/content": "2.8.4",
"@nmshd/crypto": "2.0.5",
"@nmshd/transport": "2.2.1",
"@nmshd/transport": "2.2.2",
"ajv": "^8.12.0",
"ajv-errors": "^3.0.0",
"ajv-formats": "^2.1.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/transport/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nmshd/transport",
"version": "2.2.1",
"version": "2.2.2",
"description": "The transport library handles backbone communication and content encryption.",
"homepage": "https://enmeshed.eu",
"repository": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,18 @@ export class RelationshipsController extends TransportController {
}

public async getRelationshipToIdentity(address: CoreAddress, status?: RelationshipStatus): Promise<Relationship | undefined> {
const query: any = {
[`${nameof<Relationship>((r) => r.peer)}.${nameof<Identity>((r) => r.address)}`]: address.toString()
};
if (status) {
query[`${nameof<Relationship>((r) => r.status)}`] = status;
const query: any = { peerAddress: address.toString() };
if (status) query[`${nameof<Relationship>((r) => r.status)}`] = status;
let relationshipDoc = await this.relationships.findOne(query);

if (!relationshipDoc) {
// If we don't find the relationship by peerAddress, we have to check again by peer.address
// as the Relationship could have been created before the peerAddress was introduced
const query = { [`${nameof<Relationship>((r) => r.peer)}.${nameof<Identity>((r) => r.address)}`]: address.toString() };
if (status) query[`${nameof<Relationship>((r) => r.status)}`] = status;
relationshipDoc = await this.relationships.findOne(query);
}

const relationshipDoc = await this.relationships.findOne(query);
if (!relationshipDoc) {
return;
}
Expand Down
11 changes: 11 additions & 0 deletions packages/transport/src/modules/relationships/local/Relationship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ export class Relationship extends CoreSynchronizable implements IRelationship {
@serialize()
public metadataModifiedAt?: CoreDate;

public override toJSON(verbose?: boolean | undefined, serializeAsString?: boolean | undefined): Object {
const json = super.toJSON(verbose, serializeAsString) as any;

// Adds flattened peerAddress and templateId to the JSON stored in the database.
// This helps us to boost the performance of database queries that include these fields.
json.peerAddress = this.peer.address.toString();
json.templateId = this.cache?.template.id.toString();

return json;
}

public static fromRequestSent(id: CoreId, template: IRelationshipTemplate, peer: IIdentity, creationChange: IRelationshipChange, relationshipSecretId: CoreId): Relationship {
const cache = CachedRelationship.from({
changes: [creationChange],
Expand Down

0 comments on commit c3fcf5e

Please sign in to comment.