Files
mixtape/zero.Core/Extensions/RavenDocumentStoreExtensions.cs
T

164 lines
5.3 KiB
C#
Raw Normal View History

2020-11-16 14:48:23 +01:00
using Raven.Client.Documents;
using Raven.Client.Documents.Conventions;
2020-04-14 20:16:04 +02:00
using Raven.Client.Documents.Operations.CompareExchange;
2020-09-17 16:10:21 +02:00
using Raven.Client.Json.Serialization.NewtonsoftJson;
2020-04-14 20:16:04 +02:00
using System;
using System.Linq;
using System.Reflection;
2020-04-14 20:16:04 +02:00
using System.Threading.Tasks;
using zero.Core.Attributes;
using zero.Core.Entities;
using zero.Core.Options;
2020-05-07 11:45:28 +02:00
using zero.Core.Utils;
2020-04-14 20:16:04 +02:00
namespace zero.Core.Extensions
{
2020-08-17 14:46:13 +02:00
public static class RavenDocumentStoreExtensions
2020-04-14 20:16:04 +02:00
{
2020-11-24 12:02:03 +01:00
const char DOT = '.';
/// <summary>
/// Setup conventions for the document store
/// </summary>
public static IDocumentStore Setup(this IDocumentStore store, IZeroOptions options)
{
Type[] polymorphTypes = new Type[2] { typeof(SpaceContent), typeof(Page) };
Type dbConventionType = typeof(IZeroDbConventions);
2020-11-24 12:02:03 +01:00
store.Conventions.IdentityPartsSeparator = DOT;
2020-11-16 16:03:22 +01:00
store.Conventions.RegisterAsyncIdConvention<IZeroEntity>((_, entity) =>
2020-11-16 14:24:48 +01:00
{
string prefix = String.Empty;
string guid = IdGenerator.Create();
2020-11-16 16:03:22 +01:00
string collection = store.Conventions.GetCollectionName(entity);
2020-11-16 22:57:51 +01:00
//CollectionAttribute collectionAttribute = entity.GetType().GetCustomAttribute<CollectionAttribute>(true);
if ((entity is IMedia && ((IMedia)entity).IsCore) || (entity is IMediaFolder && ((IMediaFolder)entity).IsCore))
{
prefix = Constants.Database.CoreIdPrefix;
}
if (entity is IBackofficeUser or IBackofficeUserRole)
{
guid = IdGenerator.Create(8);
}
2020-11-16 16:03:22 +01:00
string tag = store.Conventions.TransformTypeCollectionNameToDocumentIdPrefix(collection);
return Task.FromResult(prefix + tag + store.Conventions.IdentityPartsSeparator + guid);
2020-11-16 22:57:51 +01:00
});
store.Conventions.RegisterAsyncIdConvention<IPage>((_, entity) =>
{
return store.Conventions.AsyncDocumentIdGenerator(_, entity);
2020-11-16 14:24:48 +01:00
});
2020-11-24 12:02:03 +01:00
(store.Conventions.Serialization as NewtonsoftJsonSerializationConventions).CustomizeJsonDeserializer = x =>
{
x.Converters.Add(new RefJsonConverter());
};
(store.Conventions.Serialization as NewtonsoftJsonSerializationConventions).CustomizeJsonSerializer = x =>
{
x.Converters.Add(new RefJsonConverter());
};
store.Conventions.FindCollectionName = type =>
{
Type finalType = type;
// do not alter non-internal entities
if (!dbConventionType.IsAssignableFrom(type))
{
return DocumentConventions.DefaultGetCollectionName(type);
}
// use name from attribute if available
2020-05-22 21:19:49 +02:00
CollectionAttribute collection = type.GetCustomAttribute<CollectionAttribute>(true);
if (collection != null)
{
return collection.Name;
}
// use base interface if available
Type interfaceBaseType = type.GetInterfaces().FirstOrDefault(x => x.IsInterface && dbConventionType.IsAssignableFrom(x) && x.Name != dbConventionType.Name);
if (interfaceBaseType != null)
{
// use name from attribute if available
collection = interfaceBaseType.GetCustomAttribute<CollectionAttribute>(true);
if (collection != null)
{
return options.Raven.CollectionPrefix + collection.Name;
}
}
// use base type for polymorphism
Type polymorphBaseType = polymorphTypes.FirstOrDefault(x => type.IsSubclassOf(x));
if (polymorphBaseType != null)
{
finalType = polymorphBaseType;
}
return options.Raven.CollectionPrefix + DocumentConventions.DefaultGetCollectionName(finalType);
};
store.Conventions.TransformTypeCollectionNameToDocumentIdPrefix = name =>
{
if (!options.Raven.CollectionPrefix.IsNullOrWhiteSpace())
{
name = options.Raven.CollectionPrefix.EnsureEndsWith(store.Conventions.IdentityPartsSeparator) + name.TrimStart(options.Raven.CollectionPrefix);
}
return name.ToCamelCaseId();
};
return store;
}
2020-04-14 20:16:04 +02:00
/// <summary>
/// Reserves a key cluster-wide
/// </summary>
public static async Task<bool> ReserveAsync(this IDocumentStore store, string key, string value = null)
{
if (String.IsNullOrWhiteSpace(key))
{
return false;
}
if (value == null)
{
value = key;
}
var operation = new PutCompareExchangeValueOperation<string>(key, value, 0);
CompareExchangeResult<string> result = await store.Operations.SendAsync(operation).ConfigureAwait(false);
return result.Successful;
}
/// <summary>
/// Removes a cluster-wide key reservation
/// </summary>
public static async Task<bool> RemoveReservationAsync(this IDocumentStore store, string key)
{
if (!String.IsNullOrWhiteSpace(key))
{
return false;
}
CompareExchangeValue<string> readResult = store.Operations.Send(new GetCompareExchangeValueOperation<string>(key));
if (readResult == null)
{
return false;
}
DeleteCompareExchangeValueOperation<string> operation = new DeleteCompareExchangeValueOperation<string>(key, readResult.Index);
CompareExchangeResult<string> result = await store.Operations.SendAsync(operation).ConfigureAwait(false);
return result.Successful;
}
}
}