// ========================================================================== // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex UG (haftungsbeschränkt) // All rights reserved. Licensed under the MIT license. // ========================================================================== using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using MongoDB.Driver; using NodaTime; using Squidex.Domain.Apps.Entities.Apps; using Squidex.Domain.Apps.Entities.Contents; using Squidex.Domain.Apps.Entities.Contents.Text; using Squidex.Domain.Apps.Entities.MongoDb.Contents.Operations; using Squidex.Domain.Apps.Entities.Schemas; using Squidex.Infrastructure; using Squidex.Infrastructure.Log; using Squidex.Infrastructure.MongoDb; using Squidex.Infrastructure.Queries; namespace Squidex.Domain.Apps.Entities.MongoDb.Contents { public sealed class MongoContentCollectionAll : MongoRepositoryBase { private readonly QueryContent queryContentAsync; private readonly QueryContentsByIds queryContentsById; private readonly QueryContentsByQuery queryContentsByQuery; private readonly QueryIdsAsync queryIdsAsync; private readonly QueryReferrersAsync queryReferrersAsync; private readonly QueryScheduledContents queryScheduledItems; public MongoContentCollectionAll(IMongoDatabase database, IAppProvider appProvider, ITextIndex indexer, DataConverter converter) : base(database) { queryContentAsync = new QueryContent(converter); queryContentsById = new QueryContentsByIds(converter, appProvider); queryContentsByQuery = new QueryContentsByQuery(converter, indexer); queryIdsAsync = new QueryIdsAsync(appProvider); queryReferrersAsync = new QueryReferrersAsync(); queryScheduledItems = new QueryScheduledContents(); } public IMongoCollection GetInternalCollection() { return Collection; } protected override string CollectionName() { return "State_Contents_All"; } protected override async Task SetupCollectionAsync(IMongoCollection collection, CancellationToken ct = default) { await queryContentAsync.PrepareAsync(collection, ct); await queryContentsById.PrepareAsync(collection, ct); await queryContentsByQuery.PrepareAsync(collection, ct); await queryIdsAsync.PrepareAsync(collection, ct); await queryReferrersAsync.PrepareAsync(collection, ct); await queryScheduledItems.PrepareAsync(collection, ct); } public async Task> QueryAsync(IAppEntity app, ISchemaEntity schema, ClrQuery query, Guid? referenced) { using (Profiler.TraceMethod("QueryAsyncByQuery")) { return await queryContentsByQuery.DoAsync(app, schema, query, referenced, SearchScope.All); } } public async Task> QueryAsync(IAppEntity app, ISchemaEntity schema, HashSet ids) { Guard.NotNull(app, nameof(app)); using (Profiler.TraceMethod("QueryAsyncByIds")) { var result = await queryContentsById.DoAsync(app.Id, schema, ids, false); return ResultList.Create(result.Count, result.Select(x => x.Content)); } } public async Task> QueryAsync(IAppEntity app, HashSet ids) { Guard.NotNull(app, nameof(app)); using (Profiler.TraceMethod("QueryAsyncByIdsWithoutSchema")) { var result = await queryContentsById.DoAsync(app.Id, null, ids, false); return result; } } public async Task FindContentAsync(ISchemaEntity schema, Guid id) { using (Profiler.TraceMethod()) { return await queryContentAsync.DoAsync(schema, id); } } public async Task QueryScheduledWithoutDataAsync(Instant now, Func callback) { using (Profiler.TraceMethod()) { await queryScheduledItems.DoAsync(now, callback); } } public async Task> QueryIdsAsync(Guid appId, HashSet ids) { using (Profiler.TraceMethod()) { return await queryIdsAsync.DoAsync(appId, ids); } } public async Task> QueryIdsAsync(Guid appId, Guid schemaId, FilterNode filterNode) { using (Profiler.TraceMethod()) { return await queryIdsAsync.DoAsync(appId, schemaId, filterNode); } } public async Task HasReferrersAsync(Guid contentId) { using (Profiler.TraceMethod()) { return await queryReferrersAsync.DoAsync(contentId); } } public Task ResetScheduledAsync(Guid id) { return Collection.UpdateOneAsync(x => x.Id == id, Update.Unset(x => x.ScheduleJob).Unset(x => x.ScheduledAt)); } public Task FindAsync(Guid id) { return Collection.Find(x => x.Id == id).FirstOrDefaultAsync(); } public Task UpsertVersionedAsync(Guid id, long oldVersion, MongoContentEntity entity) { return Collection.UpsertVersionedAsync(id, oldVersion, entity); } public Task RemoveAsync(Guid id) { return Collection.DeleteOneAsync(x => x.Id == id); } } }