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(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 QueryByEntityId(this DBComponent self, long entityId) where T : class { if (self.Inner == null) { throw new Exception("DBComponent not initialized."); } return QueryByEntityIdInternal(self, entityId); } public static ETTask> Query(this DBComponent self, Func predicate = null) where T : class { if (self.Inner == null) { throw new Exception("DBComponent not initialized."); } return QueryInternal(self, predicate); } public static ETTask Update(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(this DBComponent self, long entityId) where T : class { if (self.Inner == null) { throw new Exception("DBComponent not initialized."); } return DeleteInternal(self, entityId); } private static async ETTask SaveInternal(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 QueryByEntityIdInternal(DBComponent self, long entityId) where T : class { Stopwatch sw = Stopwatch.StartNew(); T result = await self.Inner.QueryByEntityId(entityId); sw.Stop(); if (sw.ElapsedMilliseconds > 100) { LogExtensions.Slow("DB", $"QueryByEntityId<{typeof(T).Name}>", sw.ElapsedMilliseconds, $"id={entityId}"); } return result; } private static async ETTask> QueryInternal(DBComponent self, Func predicate = null) where T : class { Stopwatch sw = Stopwatch.StartNew(); List 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(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(DBComponent self, long entityId) where T : class { Stopwatch sw = Stopwatch.StartNew(); await self.Inner.Delete(entityId); sw.Stop(); if (sw.ElapsedMilliseconds > 200) { LogExtensions.Slow("DB", $"Delete<{typeof(T).Name}>", sw.ElapsedMilliseconds, $"id={entityId}"); } } } }