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

5x Fix cache performance issues #6616

Merged
merged 6 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { Injectable } from '@nestjs/common';
import { EntitySchemaRelationOptions } from 'typeorm';

import { FieldMetadataEntity } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
import { isRelationFieldMetadataType } from 'src/engine/utils/is-relation-field-metadata-type.util';
import { determineRelationDetails } from 'src/engine/twenty-orm/utils/determine-relation-details.util';
import { isRelationFieldMetadataType } from 'src/engine/utils/is-relation-field-metadata-type.util';
import { WorkspaceCacheStorageService } from 'src/engine/workspace-cache-storage/workspace-cache-storage.service';

type EntitySchemaRelationMap = {
Expand Down Expand Up @@ -37,11 +37,15 @@ export class EntitySchemaRelationFactory {
);
}

const objectMetadataCollection =
await this.workspaceCacheStorageService.getObjectMetadataCollection(
workspaceId,
);

const relationDetails = await determineRelationDetails(
workspaceId,
fieldMetadata,
relationMetadata,
this.workspaceCacheStorageService,
objectMetadataCollection,
);

entitySchemaRelationMap[fieldMetadata.name] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export class WorkspaceDatasourceFactory {
{
workspaceId,
workspaceCacheStorage: this.workspaceCacheStorageService,
objectMetadataCollection: cachedObjectMetadataCollection,
},
{
url:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
import { WorkspaceCacheStorageService } from 'src/engine/workspace-cache-storage/workspace-cache-storage.service';

export interface WorkspaceInternalContext {
workspaceId: string;
workspaceCacheStorage: WorkspaceCacheStorageService;
objectMetadataCollection: ObjectMetadataEntity[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import {
SaveOptions,
UpdateResult,
} from 'typeorm';
import { PickKeysByType } from 'typeorm/common/PickKeysByType';
import { QueryDeepPartialEntity } from 'typeorm/query-builder/QueryPartialEntity';
import { UpsertOptions } from 'typeorm/repository/UpsertOptions';
import { PickKeysByType } from 'typeorm/common/PickKeysByType';

import { WorkspaceInternalContext } from 'src/engine/twenty-orm/interfaces/workspace-internal-context.interface';

Expand Down Expand Up @@ -235,6 +235,7 @@ export class WorkspaceRepository<
formattedEntityOrEntities,
options,
);

const formattedResult = await this.formatResult(result);

return formattedResult;
Expand Down Expand Up @@ -423,8 +424,17 @@ export class WorkspaceRepository<
entityManager?: EntityManager,
): Promise<InsertResult> {
const manager = entityManager || this.manager;
const objectMetadataCollection =
await this.internalContext.workspaceCacheStorage.getObjectMetadataCollection(
this.internalContext.workspaceId,
);

const formatedEntity = await this.formatData(entity);
const result = await manager.insert(this.target, formatedEntity);

if (!objectMetadataCollection) {
throw new Error('Object metadata collection is missing');
}
const formattedResult = await this.formatResult(result);

return formattedResult;
Expand Down Expand Up @@ -608,7 +618,9 @@ export class WorkspaceRepository<
/**
* PRIVATE METHODS
*/
private async getObjectMetadataFromTarget() {
private async getObjectMetadataFromTarget(
objectMetadataCollection: ObjectMetadataEntity[],
) {
const objectMetadataName =
typeof this.target === 'string'
? this.target
Expand All @@ -621,18 +633,11 @@ export class WorkspaceRepository<
throw new Error('Object metadata name is missing');
}

const objectMetadata =
await this.internalContext.workspaceCacheStorage.getObjectMetadata(
this.internalContext.workspaceId,
(objectMetadata) => objectMetadata.nameSingular === objectMetadataName,
);
const objectMetadata = objectMetadataCollection?.find(
(objectMetadata) => objectMetadata.nameSingular === objectMetadataName,
);
bosiraphael marked this conversation as resolved.
Show resolved Hide resolved

if (!objectMetadata) {
const objectMetadataCollection =
await this.internalContext.workspaceCacheStorage.getObjectMetadataCollection(
this.internalContext.workspaceId,
);

throw new Error(
`Object metadata for object "${objectMetadataName}" is missing ` +
`in workspace "${this.internalContext.workspaceId}" ` +
Expand Down Expand Up @@ -678,7 +683,9 @@ export class WorkspaceRepository<
) as Promise<T>;
}

const objectMetadata = await this.getObjectMetadataFromTarget();
const objectMetadata = await this.getObjectMetadataFromTarget(
this.internalContext.objectMetadataCollection,
);

const compositeFieldMetadataCollection =
await this.getCompositeFieldMetadataCollection(objectMetadata);
Expand Down Expand Up @@ -730,7 +737,9 @@ export class WorkspaceRepository<
data: T,
objectMetadata?: ObjectMetadataEntity,
): Promise<T> {
objectMetadata ??= await this.getObjectMetadataFromTarget();
objectMetadata ??= await this.getObjectMetadataFromTarget(
this.internalContext.objectMetadataCollection,
);

if (!data) {
return data;
Expand Down Expand Up @@ -806,15 +815,13 @@ export class WorkspaceRepository<

if (relationMetadata) {
const toObjectMetadata =
await this.internalContext.workspaceCacheStorage.getObjectMetadata(
relationMetadata.workspaceId,
this.internalContext.objectMetadataCollection.find(
(objectMetadata) =>
objectMetadata.id === relationMetadata.toObjectMetadataId,
);

const fromObjectMetadata =
await this.internalContext.workspaceCacheStorage.getObjectMetadata(
relationMetadata.workspaceId,
this.internalContext.objectMetadataCollection.find(
(objectMetadata) =>
objectMetadata.id === relationMetadata.fromObjectMetadataId,
);
Expand All @@ -833,6 +840,7 @@ export class WorkspaceRepository<

newData[key] = await this.formatResult(
value,

relationType === 'one-to-many'
? toObjectMetadata
: fromObjectMetadata,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { FieldMetadataEntity } from 'src/engine/metadata-modules/field-metadata/
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
import { RelationMetadataEntity } from 'src/engine/metadata-modules/relation-metadata/relation-metadata.entity';
import { computeRelationType } from 'src/engine/twenty-orm/utils/compute-relation-type.util';
import { WorkspaceCacheStorageService } from 'src/engine/workspace-cache-storage/workspace-cache-storage.service';

interface RelationDetails {
relationType: RelationType;
Expand All @@ -14,10 +13,9 @@ interface RelationDetails {
}

export async function determineRelationDetails(
workspaceId: string,
fieldMetadata: FieldMetadataEntity,
relationMetadata: RelationMetadataEntity,
workspaceCacheStorageService: WorkspaceCacheStorageService,
objectMetadataCollection?: ObjectMetadataEntity[],
): Promise<RelationDetails> {
const relationType = computeRelationType(fieldMetadata, relationMetadata);
let fromObjectMetadata: ObjectMetadataEntity | undefined =
Expand All @@ -28,8 +26,8 @@ export async function determineRelationDetails(
// RelationMetadata always store the relation from the perspective of the `from` object, MANY_TO_ONE relations are not stored yet
if (relationType === 'many-to-one') {
fromObjectMetadata = fieldMetadata.object;
toObjectMetadata = await workspaceCacheStorageService.getObjectMetadata(
workspaceId,

toObjectMetadata = objectMetadataCollection?.find(
(objectMetadata) =>
objectMetadata.id === relationMetadata.fromObjectMetadataId,
);
bosiraphael marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Loading