2026-04-07 15:24:07 +02:00
|
|
|
using System.Linq.Expressions;
|
2022-12-07 14:05:11 +01:00
|
|
|
|
2026-05-05 11:34:32 +02:00
|
|
|
namespace Mixtape.Raven;
|
2022-12-07 14:05:11 +01:00
|
|
|
|
|
|
|
|
public class RavenOptions
|
|
|
|
|
{
|
|
|
|
|
public string Url { get; set; }
|
|
|
|
|
|
|
|
|
|
public string Database { get; set; }
|
|
|
|
|
|
2026-04-07 15:22:10 +02:00
|
|
|
public string CollectionPrefix { get; set; } = string.Empty;
|
2022-12-07 14:05:11 +01:00
|
|
|
|
2022-12-07 14:29:07 +01:00
|
|
|
public int CacheInMinutes { get; set; } = 60;
|
|
|
|
|
|
2022-12-07 14:05:11 +01:00
|
|
|
public RavenIndexesOptions Indexes { get; set; } = new();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class RavenIndexesOptions : List<RavenIndexesOptions.Map>
|
|
|
|
|
{
|
|
|
|
|
public class Map
|
|
|
|
|
{
|
|
|
|
|
internal Type Type { get; set; }
|
|
|
|
|
|
2026-05-05 11:34:32 +02:00
|
|
|
internal Expression<Func<IMixtapeIndexDefinition>> CreateIndex { get; set; }
|
2022-12-07 14:05:11 +01:00
|
|
|
|
2026-05-05 11:34:32 +02:00
|
|
|
internal Map(Type type, Expression<Func<IMixtapeIndexDefinition>> create)
|
2022-12-07 14:05:11 +01:00
|
|
|
{
|
|
|
|
|
Type = type;
|
|
|
|
|
CreateIndex = create;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2026-05-11 15:17:00 +02:00
|
|
|
public RavenIndexModifiersOptions Modifiers { get; private set; } = [];
|
2022-12-07 14:05:11 +01:00
|
|
|
|
2026-05-05 11:34:32 +02:00
|
|
|
public void Add<T>() where T : IMixtapeIndexDefinition, new()
|
2022-12-07 14:05:11 +01:00
|
|
|
{
|
|
|
|
|
base.Add(new Map(typeof(T), () => new T()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Add(Type indexType)
|
|
|
|
|
{
|
2026-05-05 11:34:32 +02:00
|
|
|
base.Add(new Map(indexType, () => (IMixtapeIndexDefinition)Activator.CreateInstance(indexType)));
|
2022-12-07 14:05:11 +01:00
|
|
|
}
|
|
|
|
|
|
2026-05-05 11:34:32 +02:00
|
|
|
public void Add<T>(T index) where T : IMixtapeIndexDefinition
|
2022-12-07 14:05:11 +01:00
|
|
|
{
|
|
|
|
|
base.Add(new Map(typeof(T), () => index));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddRange(params Type[] indexes)
|
|
|
|
|
{
|
|
|
|
|
foreach (Type type in indexes)
|
|
|
|
|
{
|
|
|
|
|
Add(type);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Replace<T, TReplaceWith>()
|
2026-05-05 11:34:32 +02:00
|
|
|
where T : IMixtapeIndexDefinition, new()
|
|
|
|
|
where TReplaceWith : IMixtapeIndexDefinition, new()
|
2022-12-07 14:05:11 +01:00
|
|
|
{
|
|
|
|
|
Replace(typeof(T), typeof(TReplaceWith));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Replace(Type origin, Type replaceWith)
|
|
|
|
|
{
|
|
|
|
|
var item = this.FirstOrDefault(x => x.Type == origin);
|
|
|
|
|
if (item != null)
|
|
|
|
|
{
|
|
|
|
|
Remove(item);
|
|
|
|
|
}
|
|
|
|
|
Add(replaceWith);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-05 11:34:32 +02:00
|
|
|
public IEnumerable<IMixtapeIndexDefinition> BuildAll(IMixtapeOptions options, IDocumentStore store)
|
2022-12-07 14:05:11 +01:00
|
|
|
{
|
|
|
|
|
RavenOptions ravenOptions = options.For<RavenOptions>();
|
|
|
|
|
|
|
|
|
|
foreach (Map map in this)
|
|
|
|
|
{
|
2026-05-05 11:34:32 +02:00
|
|
|
IMixtapeIndexDefinition index = map.CreateIndex.Compile().Invoke();
|
2022-12-07 14:05:11 +01:00
|
|
|
index.Setup(options, store);
|
|
|
|
|
index.RunModifiers(ravenOptions);
|
|
|
|
|
yield return index;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class RavenIndexModifiersOptions : List<RavenIndexModifiersOptions.Modifier>
|
|
|
|
|
{
|
|
|
|
|
public class Modifier
|
|
|
|
|
{
|
|
|
|
|
public Type Type { get; set; }
|
|
|
|
|
|
2026-05-05 11:34:32 +02:00
|
|
|
public Expression<Action<IMixtapeIndexDefinition>> Modify { get; set; }
|
2022-12-07 14:05:11 +01:00
|
|
|
}
|
|
|
|
|
|
2026-05-05 11:34:32 +02:00
|
|
|
public void Add<T>(Action<T> modify) where T : IMixtapeIndexDefinition, new()
|
2022-12-07 14:05:11 +01:00
|
|
|
{
|
|
|
|
|
Add(new()
|
|
|
|
|
{
|
|
|
|
|
Type = typeof(T),
|
|
|
|
|
Modify = x => modify((T)x)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2026-05-05 11:34:32 +02:00
|
|
|
public IEnumerable<Modifier> GetAllForType<T>() where T : IMixtapeIndexDefinition, new() => GetAllForType(typeof(T));
|
2022-12-07 14:05:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<Modifier> GetAllForType(Type type)
|
|
|
|
|
{
|
|
|
|
|
foreach (Modifier modifier in this.Where(x => x.Type.IsAssignableFrom(type)))
|
|
|
|
|
{
|
|
|
|
|
yield return modifier;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|