148 lines
4.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace ET.Server
{
[EntitySystemOf(typeof(DBComponent))]
[FriendOf(typeof(DBComponent))]
public static partial class DBComponentSystem
{
[EntitySystem]
private static void Awake(this DBComponent self, string dbConnection, string dbName)
{
if (string.Equals(dbConnection, "memory", StringComparison.OrdinalIgnoreCase))
{
self.Inner = new MemoryDBComponent();
return;
}
#if DOTNET || UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_LINUX || UNITY_STANDALONE_OSX
self.Inner = new MongoDBComponent(dbConnection, dbName);
#else
self.Inner = new MemoryDBComponent();
#endif
}
[EntitySystem]
private static void Awake(this DBComponent self, IDBComponent component)
{
self.Inner = component;
}
[EntitySystem]
private static void Destroy(this DBComponent self)
{
self.Inner = null;
}
public static ETTask Save<T>(this DBComponent self, long entityId, T entity) where T : class
{
if (self.Inner == null)
{
throw new Exception("DBComponent not initialized.");
}
return SaveInternal(self, entityId, entity);
}
public static ETTask<T> QueryByEntityId<T>(this DBComponent self, long entityId) where T : class
{
if (self.Inner == null)
{
throw new Exception("DBComponent not initialized.");
}
return QueryByEntityIdInternal<T>(self, entityId);
}
public static ETTask<List<T>> Query<T>(this DBComponent self, Func<T, bool> predicate = null) where T : class
{
if (self.Inner == null)
{
throw new Exception("DBComponent not initialized.");
}
return QueryInternal(self, predicate);
}
public static ETTask Update<T>(this DBComponent self, long entityId, T entity) where T : class
{
if (self.Inner == null)
{
throw new Exception("DBComponent not initialized.");
}
return UpdateInternal(self, entityId, entity);
}
public static ETTask Delete<T>(this DBComponent self, long entityId) where T : class
{
if (self.Inner == null)
{
throw new Exception("DBComponent not initialized.");
}
return DeleteInternal<T>(self, entityId);
}
private static async ETTask SaveInternal<T>(DBComponent self, long entityId, T entity) where T : class
{
Stopwatch sw = Stopwatch.StartNew();
await self.Inner.Save(entityId, entity);
sw.Stop();
if (sw.ElapsedMilliseconds > 200)
{
LogExtensions.Slow("DB", $"Save<{typeof(T).Name}>", sw.ElapsedMilliseconds, $"id={entityId}");
}
}
private static async ETTask<T> QueryByEntityIdInternal<T>(DBComponent self, long entityId) where T : class
{
Stopwatch sw = Stopwatch.StartNew();
T result = await self.Inner.QueryByEntityId<T>(entityId);
sw.Stop();
if (sw.ElapsedMilliseconds > 100)
{
LogExtensions.Slow("DB", $"QueryByEntityId<{typeof(T).Name}>", sw.ElapsedMilliseconds, $"id={entityId}");
}
return result;
}
private static async ETTask<List<T>> QueryInternal<T>(DBComponent self, Func<T, bool> predicate = null) where T : class
{
Stopwatch sw = Stopwatch.StartNew();
List<T> result = await self.Inner.Query(predicate);
sw.Stop();
if (sw.ElapsedMilliseconds > 100)
{
LogExtensions.Slow("DB", $"Query<{typeof(T).Name}>", sw.ElapsedMilliseconds);
}
return result;
}
private static async ETTask UpdateInternal<T>(DBComponent self, long entityId, T entity) where T : class
{
Stopwatch sw = Stopwatch.StartNew();
await self.Inner.Update(entityId, entity);
sw.Stop();
if (sw.ElapsedMilliseconds > 200)
{
LogExtensions.Slow("DB", $"Update<{typeof(T).Name}>", sw.ElapsedMilliseconds, $"id={entityId}");
}
}
private static async ETTask DeleteInternal<T>(DBComponent self, long entityId) where T : class
{
Stopwatch sw = Stopwatch.StartNew();
await self.Inner.Delete<T>(entityId);
sw.Stop();
if (sw.ElapsedMilliseconds > 200)
{
LogExtensions.Slow("DB", $"Delete<{typeof(T).Name}>", sw.ElapsedMilliseconds, $"id={entityId}");
}
}
}
}