using MongoDB.Bson; using MongoDB.Driver; using MongoDB.Entities; using PrivaPub.Models.Federation; using PrivaPub.Models.Group; using PrivaPub.Models.Jobs; using PrivaPub.Models.Post; using PrivaPub.Models.Social; using PrivaPub.Models.User; namespace PrivaPub.Infrastructure.Data { public static class Indexes { public static readonly TimeSpan TombstoneLifetime = TimeSpan.FromDays(90); public static async Task Create(CancellationToken token = default) { await Unique(p => p.ObjectURI, Builders.Filter.Type(p => p.ObjectURI, BsonType.String), token); await Plain(token, p => p.GroupUserId, p => p.ID); await Plain(token, p => p.GroupId, p => p.ID); await Plain(token, p => p.ActorURI); await Plain(token, p => p.ConversationId, p => p.ID); await Plain(token, p => p.InReplyToURI); await DB.Default.Index().Key(p => p.Geo, KeyType.Geo2DSphere).CreateAsync(token); await Unique(p => p.ObjectURI, Builders.Filter.Type(p => p.ObjectURI, BsonType.String), token); await Plain(token, p => p.GroupId, p => p.ID); await Unique(a => a.ActorURI, Builders.Filter.Type(a => a.ActorURI, BsonType.String), token); await Plain(token, a => a.PublicKeyId); await DB.Default.Index() .Key(f => f.LocalActorId, KeyType.Ascending) .Key(f => f.LocalActorKind, KeyType.Ascending) .Key(f => f.ActorURI, KeyType.Ascending) .Option(o => o.Unique = true) .CreateAsync(token); await Plain(token, f => f.ActorURI); await DB.Default.Index() .Key(r => r.RootId, KeyType.Ascending) .Key(r => r.AvatarId, KeyType.Ascending) .Option(o => o.Unique = true) .CreateAsync(token); await Plain(token, r => r.AvatarId); await Unique(r => r.Name, Builders.Filter.Type(r => r.Name, BsonType.String), token); await Unique(u => u.UserName, Builders.Filter.Type(u => u.UserName, BsonType.String), token); await Plain(token, a => a.UserName); await Plain(token, g => g.UserName); await Plain(token, g => g.InvitationCode); await Plain(token, g => g.ParticipantsKey); await Plain(token, d => d.DeliveredAt, d => d.AbandonedAt, d => d.NextAttemptAt); await Plain(token, j => j.Kind, j => j.State, j => j.RunAt); await Plain(token, j => j.State, j => j.LeasedUntil); await Unique(j => j.DedupeKey, Builders.Filter.Type(j => j.DedupeKey, BsonType.String), token); await DB.Default.Index() .Key(j => j.FinishedAt, KeyType.Ascending) .Option(o => o.ExpireAfter = TimeSpan.FromDays(7)) .CreateAsync(token); await DB.Default.Index() .Key(f => f.AvatarId, KeyType.Ascending) .Key(f => f.TargetActorURI, KeyType.Ascending) .Option(o => o.Unique = true) .CreateAsync(token); await Plain(token, f => f.TargetActorURI, f => f.State); await Plain(token, f => f.TargetAccountId, f => f.State); await Plain(token, f => f.FollowActivityURI); await DB.Default.Index() .Key(e => e.AvatarId, KeyType.Ascending) .Key(e => e.PostId, KeyType.Ascending) .Option(o => o.Unique = true) .CreateAsync(token); await Plain(token, e => e.PostId); await Unique(n => n.DedupeKey, Builders.Filter.Type(n => n.DedupeKey, BsonType.String), token); await Plain(token, n => n.AvatarId, n => n.ID); await DB.Default.Index() .Key(f => f.AccountId, KeyType.Ascending) .Key(f => f.PostId, KeyType.Ascending) .Option(o => o.Unique = true) .CreateAsync(token); await Plain(token, f => f.ActivityURI); await DB.Default.Index() .Key(m => m.AvatarId, KeyType.Ascending) .Key(m => m.Timeline, KeyType.Ascending) .Option(o => o.Unique = true) .CreateAsync(token); foreach (var pair in new (Func, string)[] { (() => DB.Default.Index().Key(b => b.AvatarId, KeyType.Ascending).Key(b => b.TargetActorURI, KeyType.Ascending).Option(o => o.Unique = true).CreateAsync(token), "block"), (() => DB.Default.Index().Key(m => m.AvatarId, KeyType.Ascending).Key(m => m.TargetActorURI, KeyType.Ascending).Option(o => o.Unique = true).CreateAsync(token), "mute"), (() => DB.Default.Index().Key(b => b.AvatarId, KeyType.Ascending).Key(b => b.Domain, KeyType.Ascending).Option(o => o.Unique = true).CreateAsync(token), "domain block"), (() => DB.Default.Index().Key(b => b.AvatarId, KeyType.Ascending).Key(b => b.PostId, KeyType.Ascending).Option(o => o.Unique = true).CreateAsync(token), "bookmark"), (() => DB.Default.Index().Key(p => p.AvatarId, KeyType.Ascending).Key(p => p.PostId, KeyType.Ascending).Option(o => o.Unique = true).CreateAsync(token), "pin") }) await pair.Item1(); await Plain(token, b => b.TargetActorURI); await Plain(token, m => m.TargetActorURI); await Plain(token, r => r.IsResolved, r => r.ID); await Unique(b => b.Domain, Builders.Filter.Type(b => b.Domain, BsonType.String), token); await Unique(i => i.Host, Builders.Filter.Type(i => i.Host, BsonType.String), token); await Unique(r => r.ObjectURI, Builders.Filter.Type(r => r.ObjectURI, BsonType.String), token); await Plain(token, r => r.PostId); await Unique(d => d.ObjectURI, Builders.Filter.Type(d => d.ObjectURI, BsonType.String), token); await DB.Default.Index().Key(d => d.DeletedAt, KeyType.Ascending).Option(o => o.ExpireAfter = TombstoneLifetime).CreateAsync(token); await DB.Default.Index().Key(d => d.PostId, KeyType.Ascending).Key(d => d.ActorURI, KeyType.Ascending).Option(o => o.Unique = true).CreateAsync(token); await Plain(token, d => d.ActivityURI); await DB.Default.Index().Key(v => v.PostId, KeyType.Ascending).Key(v => v.ActorURI, KeyType.Ascending).Key(v => v.Choice, KeyType.Ascending) .Option(o => o.Unique = true).CreateAsync(token); await DB.Default.Index().Key(v => v.PostId, KeyType.Ascending).Key(v => v.VoterAccountId, KeyType.Ascending).CreateAsync(token); await DB.Default.Index().Key(r => r.PostId, KeyType.Ascending).Key(r => r.ActorURI, KeyType.Ascending).Key(r => r.Emoji, KeyType.Ascending) .Option(o => o.Unique = true).CreateAsync(token); await Plain(token, r => r.ActivityURI); await Unique(p => p.Url, Builders.Filter.Type(p => p.Url, BsonType.String), token); } static async Task Unique(System.Linq.Expressions.Expression> key, FilterDefinition partial, CancellationToken token) where T : IEntity => await DB.Default.Index() .Key(key, KeyType.Ascending) .Option(o => { o.Unique = true; o.PartialFilterExpression = partial; }) .CreateAsync(token); static async Task Plain(CancellationToken token, params System.Linq.Expressions.Expression>[] keys) where T : IEntity { var index = DB.Default.Index(); foreach (var key in keys) index.Key(key, KeyType.Ascending); await index.CreateAsync(token); } } }