Skip to content

Commit

Permalink
Performance improvement.
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Mar 7, 2019
1 parent 9fc698f commit d56fc32
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected override string CollectionName()
return "State_Contents";
}

public async Task<IResultList<IContentEntity>> QueryAsync(IAppEntity app, ISchemaEntity schema, Query query, List<Guid> ids, Status[] status = null, bool useDraft = false)
public async Task<IResultList<IContentEntity>> QueryAsync(IAppEntity app, ISchemaEntity schema, Query query, List<Guid> ids, Status[] status, bool useDraft)
{
try
{
Expand All @@ -67,6 +67,7 @@ public async Task<IResultList<IContentEntity>> QueryAsync(IAppEntity app, ISchem
var contentCount = Collection.Find(filter).CountDocumentsAsync();
var contentItems =
Collection.Find(filter)
.WithoutDraft(useDraft)
.ContentTake(query)
.ContentSkip(query)
.ContentSort(query)
Expand Down Expand Up @@ -94,11 +95,11 @@ public async Task<IResultList<IContentEntity>> QueryAsync(IAppEntity app, ISchem
}
}

public async Task<IResultList<IContentEntity>> QueryAsync(IAppEntity app, ISchemaEntity schema, HashSet<Guid> ids, Status[] status = null)
public async Task<IResultList<IContentEntity>> QueryAsync(IAppEntity app, ISchemaEntity schema, HashSet<Guid> ids, Status[] status, bool useDraft)
{
var find = Collection.Find(FilterFactory.Build(schema.Id, ids, status));

var contentItems = find.ToListAsync();
var contentItems = find.WithoutDraft(useDraft).ToListAsync();
var contentCount = find.CountDocumentsAsync();

await Task.WhenAll(contentItems, contentCount);
Expand All @@ -111,11 +112,11 @@ public async Task<IResultList<IContentEntity>> QueryAsync(IAppEntity app, ISchem
return ResultList.Create<IContentEntity>(contentCount.Result, contentItems.Result);
}

public async Task<IContentEntity> FindContentAsync(IAppEntity app, ISchemaEntity schema, Guid id, Status[] status = null)
public async Task<IContentEntity> FindContentAsync(IAppEntity app, ISchemaEntity schema, Guid id, Status[] status, bool useDraft)
{
var find = Collection.Find(FilterFactory.Build(schema.Id, id, status));

var contentEntity = await find.FirstOrDefaultAsync();
var contentEntity = await find.WithoutDraft(useDraft).FirstOrDefaultAsync();

contentEntity?.ParseData(schema.SchemaDef, Serializer);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public async Task<IResultList<IContentEntity>> QueryAsync(IAppEntity app, ISchem
{
using (Profiler.TraceMethod<MongoContentRepository>("QueryAsyncByQuery"))
{
var useDraft = RequiresPublished(status);
var useDraft = UseDraft(status);

var fullTextIds = await indexer.SearchAsync(query.FullText, app, schema.Id, useDraft);

Expand All @@ -72,29 +72,19 @@ public async Task<IResultList<IContentEntity>> QueryAsync(IAppEntity app, ISchem
{
using (Profiler.TraceMethod<MongoContentRepository>("QueryAsyncByIds"))
{
if (RequiresPublished(status))
{
return await contents.QueryAsync(app, schema, ids);
}
else
{
return await contents.QueryAsync(app, schema, ids, status);
}
var useDraft = UseDraft(status);

return await contents.QueryAsync(app, schema, ids, status, useDraft);
}
}

public async Task<IContentEntity> FindContentAsync(IAppEntity app, ISchemaEntity schema, Status[] status, Guid id)
{
using (Profiler.TraceMethod<MongoContentRepository>())
{
if (RequiresPublished(status))
{
return await contents.FindContentAsync(app, schema, id);
}
else
{
return await contents.FindContentAsync(app, schema, id, status);
}
var useDraft = UseDraft(status);

return await contents.FindContentAsync(app, schema, id, status, useDraft);
}
}

Expand Down Expand Up @@ -132,9 +122,9 @@ public Task DeleteArchiveAsync()
return database.DropCollectionAsync("States_Contents_Archive");
}

private static bool RequiresPublished(Status[] status)
private static bool UseDraft(Status[] status)
{
return status?.Length == 1 && status[0] == Status.Published;
return !(status?.Length == 1 && status[0] == Status.Published);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Squidex.Domain.Apps.Core.Contents;
using Squidex.Domain.Apps.Core.GenerateEdmSchema;
using Squidex.Domain.Apps.Core.Schemas;
using Squidex.Infrastructure.MongoDb;
using Squidex.Infrastructure.MongoDb.Queries;
using Squidex.Infrastructure.Queries;

Expand Down Expand Up @@ -109,6 +110,11 @@ public static IFindFluent<MongoContentEntity, MongoContentEntity> ContentSkip(th
return cursor.Skip(query);
}

public static IFindFluent<MongoContentEntity, MongoContentEntity> WithoutDraft(this IFindFluent<MongoContentEntity, MongoContentEntity> cursor, bool useDraft)
{
return !useDraft ? cursor.Not(x => x.DataDraftByIds, x => x.IsDeleted) : cursor;
}

public static FilterDefinition<MongoContentEntity> Build(Guid schemaId, Guid id, Status[] status)
{
return CreateFilter(schemaId, new List<Guid> { id }, status, null);
Expand Down

0 comments on commit d56fc32

Please sign in to comment.