(ctx context.Context, file *ent.File, owner *ent.User, max int, entityType types.EntityType)
| 854 | } |
| 855 | |
| 856 | func (f *fileClient) CapEntities(ctx context.Context, file *ent.File, owner *ent.User, max int, entityType types.EntityType) (StorageDiff, error) { |
| 857 | entities, err := file.Edges.EntitiesOrErr() |
| 858 | if err != nil { |
| 859 | return nil, fmt.Errorf("failed to cap file entities: %v", err) |
| 860 | } |
| 861 | |
| 862 | versionCount := 0 |
| 863 | diff := make(StorageDiff) |
| 864 | for _, e := range entities { |
| 865 | if e.Type != int(entityType) { |
| 866 | continue |
| 867 | } |
| 868 | |
| 869 | versionCount++ |
| 870 | if versionCount > max { |
| 871 | // By default, eager-loaded entity is sorted by ID in descending order. |
| 872 | // So we can just unlink the entity and it will be the older version. |
| 873 | newDiff, err := f.UnlinkEntity(ctx, e, file, owner) |
| 874 | if err != nil { |
| 875 | return diff, fmt.Errorf("failed to cap file entities: %v", err) |
| 876 | } |
| 877 | |
| 878 | diff.Merge(newDiff) |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | return diff, nil |
| 883 | } |
| 884 | |
| 885 | func (f *fileClient) UnlinkEntity(ctx context.Context, entity *ent.Entity, file *ent.File, owner *ent.User) (StorageDiff, error) { |
| 886 | if err := f.client.Entity.UpdateOne(entity).RemoveFile(file).AddReferenceCount(-1).Exec(ctx); err != nil { |
nothing calls this directly
no test coverage detected