[+] TEngineServer

[+] TEngineServer
This commit is contained in:
ALEXTANG
2023-07-13 17:17:26 +08:00
parent a69f53592e
commit 0c8f3a5f92
790 changed files with 52737 additions and 2533 deletions

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: bd10fb6ee217e1e4a82db518e161aaec
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,45 @@
#if TENGINE_NET
using System.Linq.Expressions;
#pragma warning disable CS8625
namespace TEngine.Core.DataBase;
public interface IDateBase
{
IDateBase Initialize(string connectionString, string dbName);
FTask<long> Count<T>(string collection = null) where T : Entity;
FTask<long> Count<T>(Expression<Func<T, bool>> filter, string collection = null) where T : Entity;
FTask<bool> Exist<T>(string collection = null) where T : Entity;
FTask<bool> Exist<T>(Expression<Func<T, bool>> filter, string collection = null) where T : Entity;
FTask<T> Query<T>(long id, string collection = null) where T : Entity;
FTask<(int count, List<T> dates)> QueryCountAndDatesByPage<T>(Expression<Func<T, bool>> filter, int pageIndex, int pageSize, string collection = null) where T : Entity;
FTask<(int count, List<T> dates)> QueryCountAndDatesByPage<T>(Expression<Func<T, bool>> filter, int pageIndex, int pageSize, string[] cols, string collection = null) where T : Entity;
FTask<List<T>> QueryByPage<T>(Expression<Func<T, bool>> filter, int pageIndex, int pageSize, string collection = null) where T : Entity;
FTask<List<T>> QueryByPage<T>(Expression<Func<T, bool>> filter, int pageIndex, int pageSize, string[] cols, string collection = null) where T : Entity;
FTask<List<T>> QueryByPageOrderBy<T>(Expression<Func<T, bool>> filter, int pageIndex, int pageSize, Expression<Func<T, object>> orderByExpression, bool isAsc = true, string collection = null) where T : Entity;
FTask<T> First<T>(Expression<Func<T, bool>> filter, string collection = null) where T : Entity;
FTask<T> First<T>(string json, string[] cols, string collection = null) where T : Entity;
FTask<List<T>> QueryOrderBy<T>(Expression<Func<T, bool>> filter, Expression<Func<T, object>> orderByExpression, bool isAsc = true, string collection = null) where T : Entity;
FTask<List<T>> Query<T>(Expression<Func<T, bool>> filter, string collection = null) where T : Entity;
FTask Query(long id, List<string> collectionNames, List<Entity> result);
FTask<List<T>> QueryJson<T>(string json, string collection = null) where T : Entity;
FTask<List<T>> QueryJson<T>(string json, string[] cols, string collection = null) where T : Entity;
FTask<List<T>> QueryJson<T>(long taskId, string json, string collection = null) where T : Entity;
FTask<List<T>> Query<T>(Expression<Func<T, bool>> filter, string[] cols, string collection = null) where T : class;
FTask Save<T>(T entity, string collection = null) where T : Entity, new();
FTask Save(long id, List<Entity> entities);
FTask Save<T>(object transactionSession, T entity, string collection = null) where T : Entity;
FTask Insert<T>(T entity, string collection = null) where T : Entity, new();
FTask InsertBatch<T>(IEnumerable<T> list, string collection = null) where T : Entity, new();
FTask InsertBatch<T>(object transactionSession, IEnumerable<T> list, string collection = null) where T : Entity, new();
FTask<long> Remove<T>(object transactionSession, long id, string collection = null) where T : Entity, new();
FTask<long> Remove<T>(long id, string collection = null) where T : Entity, new();
FTask<long> Remove<T>(long id,object transactionSession, Expression<Func<T, bool>> filter, string collection = null) where T : Entity, new();
FTask<long> Remove<T>(long id, Expression<Func<T, bool>> filter, string collection = null) where T : Entity, new();
FTask<long> Sum<T>(Expression<Func<T, bool>> filter, Expression<Func<T, object>> sumExpression, string collection = null) where T : Entity;
FTask CreateIndex<T>(string collection, params object[] keys) where T : Entity;
FTask CreateIndex<T>(params object[] keys) where T : Entity;
FTask CreateDB<T>() where T : Entity;
FTask CreateDB(Type type);
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 801a4f2fbf5d7944480423ade9d8a998
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,42 @@
#if TENGINE_NET
namespace TEngine.Core.DataBase;
public sealed class World
{
public uint Id { get; private init; }
public IDateBase DateBase { get; private init; }
public WorldConfigInfo Config => ConfigTableManage.WorldConfigInfo(Id);
private static readonly Dictionary<uint, World> Worlds = new();
public World(WorldConfigInfo worldConfigInfo)
{
Id = worldConfigInfo.Id;
var dbType = worldConfigInfo.DbType.ToLower();
switch (dbType)
{
case "mongodb":
{
DateBase = new MongoDataBase();
DateBase.Initialize(worldConfigInfo.DbConnection, worldConfigInfo.DbName);
break;
}
default:
throw new Exception("No supported database");
}
}
public static World Create(uint id)
{
if (Worlds.TryGetValue(id, out var world))
{
return world;
}
var worldConfigInfo = ConfigTableManage.WorldConfigInfo(id);
world = new World(worldConfigInfo);
Worlds.Add(id, world);
return world;
}
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e207b62400a4d2742945b50eb84f8233
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,12 @@
#if TENGINE_NET
namespace TEngine.Core.DataBase;
public class WorldConfigInfo
{
public uint Id;
public string WorldName;
public string DbConnection;
public string DbName;
public string DbType;
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1c1d63a41ee591348a2aeab0a1ff5b2f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,539 @@
#if TENGINE_NET
using System.Linq.Expressions;
using TEngine.Core;
using MongoDB.Bson;
using MongoDB.Driver;
namespace TEngine.Core.DataBase;
public sealed class MongoDataBase : IDateBase
{
private string _dbName;
private string _connectionString;
private MongoClient _mongoClient;
private IMongoDatabase _mongoDatabase;
private readonly HashSet<string> _collections = new HashSet<string>();
private readonly CoroutineLockQueueType _mongoDataBaseLock = new CoroutineLockQueueType("MongoDataBaseLock");
public IDateBase Initialize(string connectionString, string dbName)
{
_dbName = dbName;
_connectionString = connectionString;
_mongoClient = new MongoClient(connectionString);
_mongoDatabase = _mongoClient.GetDatabase(dbName);
// 记录所有集合名
_collections.UnionWith(_mongoDatabase.ListCollectionNames().ToList());
return this;
}
#region Other
public async FTask<long> Sum<T>(Expression<Func<T, bool>> filter, Expression<Func<T, object>> sumExpression, string collection = null) where T : Entity
{
var member = (MemberExpression) ((UnaryExpression) sumExpression.Body).Operand;
var projection =
new BsonDocument("_id", "null").Add("Result", new BsonDocument("$sum", $"${member.Member.Name}"));
var data = await GetCollection<T>(collection).Aggregate().Match(filter).Group(projection)
.FirstOrDefaultAsync();
return data == null ? 0 : Convert.ToInt64(data["Result"]);
}
#endregion
#region GetCollection
private IMongoCollection<T> GetCollection<T>(string collection = null)
{
return _mongoDatabase.GetCollection<T>(collection ?? typeof(T).Name);
}
private IMongoCollection<Entity> GetCollection(string name)
{
return _mongoDatabase.GetCollection<Entity>(name);
}
#endregion
#region Count
public async FTask<long> Count<T>(string collection = null) where T : Entity
{
return await GetCollection<T>(collection).CountDocumentsAsync(d => true);
}
public async FTask<long> Count<T>(Expression<Func<T, bool>> filter, string collection = null) where T : Entity
{
return await GetCollection<T>(collection).CountDocumentsAsync(filter);
}
#endregion
#region Exist
public async FTask<bool> Exist<T>(string collection = null) where T : Entity
{
return await Count<T>(collection) > 0;
}
public async FTask<bool> Exist<T>(Expression<Func<T, bool>> filter, string collection = null) where T : Entity
{
return await Count(filter, collection) > 0;
}
#endregion
#region Query
public async FTask<T> Query<T>(long id, string collection = null) where T : Entity
{
using (await _mongoDataBaseLock.Lock(id))
{
var cursor = await GetCollection<T>(collection).FindAsync(d => d.Id == id);
var v = await cursor.FirstOrDefaultAsync();
return v;
}
}
public async FTask<(int count, List<T> dates)> QueryCountAndDatesByPage<T>(Expression<Func<T, bool>> filter, int pageIndex, int pageSize, string collection = null) where T : Entity
{
using (await _mongoDataBaseLock.Lock(RandomHelper.RandInt64()))
{
var count = await Count(filter);
var dates = await QueryByPage(filter, pageIndex, pageSize, collection);
return ((int)count, dates);
}
}
public async FTask<(int count, List<T> dates)> QueryCountAndDatesByPage<T>(Expression<Func<T, bool>> filter, int pageIndex, int pageSize, string[] cols, string collection = null) where T : Entity
{
using (await _mongoDataBaseLock.Lock(RandomHelper.RandInt64()))
{
var count = await Count(filter);
var dates = await QueryByPage(filter, pageIndex, pageSize, cols, collection);
return ((int) count, dates);
}
}
public async FTask<List<T>> QueryByPage<T>(Expression<Func<T, bool>> filter, int pageIndex, int pageSize, string collection = null) where T : Entity
{
using (await _mongoDataBaseLock.Lock(RandomHelper.RandInt64()))
{
return await GetCollection<T>(collection).Find(filter).Skip((pageIndex - 1) * pageSize)
.Limit(pageSize)
.ToListAsync();
}
}
public async FTask<List<T>> QueryByPage<T>(Expression<Func<T, bool>> filter, int pageIndex, int pageSize, string[] cols, string collection = null) where T : Entity
{
using (await _mongoDataBaseLock.Lock(RandomHelper.RandInt64()))
{
var projection = Builders<T>.Projection.Include("");
foreach (var col in cols)
{
projection = projection.Include(col);
}
return await GetCollection<T>(collection).Find(filter).Project<T>(projection)
.Skip((pageIndex - 1) * pageSize).Limit(pageSize).ToListAsync();
}
}
public async FTask<List<T>> QueryByPageOrderBy<T>(Expression<Func<T, bool>> filter, int pageIndex, int pageSize, Expression<Func<T, object>> orderByExpression, bool isAsc = true, string collection = null) where T : Entity
{
using (await _mongoDataBaseLock.Lock(RandomHelper.RandInt64()))
{
if (isAsc)
{
return await GetCollection<T>(collection).Find(filter).SortBy(orderByExpression)
.Skip((pageIndex - 1) * pageSize).Limit(pageSize).ToListAsync();
}
return await GetCollection<T>(collection).Find(filter).SortByDescending(orderByExpression)
.Skip((pageIndex - 1) * pageSize).Limit(pageSize).ToListAsync();
}
}
public async FTask<T> First<T>(Expression<Func<T, bool>> filter, string collection = null) where T : Entity
{
using (await _mongoDataBaseLock.Lock(RandomHelper.RandInt64()))
{
var cursor = await GetCollection<T>(collection).FindAsync(filter);
return await cursor.FirstOrDefaultAsync();
}
}
public async FTask<T> First<T>(string json, string[] cols, string collection = null) where T : Entity
{
using (await _mongoDataBaseLock.Lock(RandomHelper.RandInt64()))
{
var projection = Builders<T>.Projection.Include("");
foreach (var col in cols)
{
projection = projection.Include(col);
}
var options = new FindOptions<T, T> {Projection = projection};
FilterDefinition<T> filterDefinition = new JsonFilterDefinition<T>(json);
var cursor = await GetCollection<T>(collection).FindAsync(filterDefinition, options);
return await cursor.FirstOrDefaultAsync();
}
}
public async FTask<List<T>> QueryOrderBy<T>(Expression<Func<T, bool>> filter, Expression<Func<T, object>> orderByExpression, bool isAsc = true, string collection = null) where T : Entity
{
using (await _mongoDataBaseLock.Lock(RandomHelper.RandInt64()))
{
if (isAsc)
{
return await GetCollection<T>(collection).Find(filter).SortBy(orderByExpression).ToListAsync();
}
return await GetCollection<T>(collection).Find(filter).SortByDescending(orderByExpression)
.ToListAsync();
}
}
public async FTask<List<T>> Query<T>(Expression<Func<T, bool>> filter, string collection = null) where T : Entity
{
using (await _mongoDataBaseLock.Lock(RandomHelper.RandInt64()))
{
var cursor = await GetCollection<T>(collection).FindAsync(filter);
var v = await cursor.ToListAsync();
return v;
}
}
public async FTask Query(long id, List<string> collectionNames, List<Entity> result)
{
using (await _mongoDataBaseLock.Lock(id))
{
if (collectionNames == null || collectionNames.Count == 0)
{
return;
}
foreach (var collectionName in collectionNames)
{
var cursor = await GetCollection(collectionName).FindAsync(d => d.Id == id);
var e = await cursor.FirstOrDefaultAsync();
if (e == null)
{
continue;
}
result.Add(e);
}
}
}
public async FTask<List<T>> QueryJson<T>(string json, string collection = null) where T : Entity
{
using (await _mongoDataBaseLock.Lock(RandomHelper.RandInt64()))
{
FilterDefinition<T> filterDefinition = new JsonFilterDefinition<T>(json);
var cursor = await GetCollection<T>(collection).FindAsync(filterDefinition);
var v = await cursor.ToListAsync();
return v;
}
}
public async FTask<List<T>> QueryJson<T>(string json, string[] cols, string collection = null) where T : Entity
{
using (await _mongoDataBaseLock.Lock(RandomHelper.RandInt64()))
{
var projection = Builders<T>.Projection.Include("");
foreach (var col in cols)
{
projection = projection.Include(col);
}
var options = new FindOptions<T, T> {Projection = projection};
FilterDefinition<T> filterDefinition = new JsonFilterDefinition<T>(json);
var cursor = await GetCollection<T>(collection).FindAsync(filterDefinition, options);
var v = await cursor.ToListAsync();
return v;
}
}
public async FTask<List<T>> QueryJson<T>(long taskId, string json, string collection = null) where T : Entity
{
using (await _mongoDataBaseLock.Lock(taskId))
{
FilterDefinition<T> filterDefinition = new JsonFilterDefinition<T>(json);
var cursor = await GetCollection<T>(collection).FindAsync(filterDefinition);
var v = await cursor.ToListAsync();
return v;
}
}
public async FTask<List<T>> Query<T>(Expression<Func<T, bool>> filter, string[] cols, string collection = null) where T : class
{
using (await _mongoDataBaseLock.Lock(RandomHelper.RandInt64()))
{
var projection = Builders<T>.Projection.Include(cols[0]);
for (var i = 1; i < cols.Length; i++)
{
projection = projection.Include(cols[i]);
}
return await GetCollection<T>(collection).Find(filter).Project<T>(projection).ToListAsync();
}
}
#endregion
#region Save
public async FTask Save<T>(object transactionSession, T entity, string collection = null) where T : Entity
{
if (entity == null)
{
Log.Error($"save entity is null: {typeof(T).Name}");
return;
}
var clone = MongoHelper.Instance.Clone(entity);
using (await _mongoDataBaseLock.Lock(clone.Id))
{
await GetCollection(collection ?? clone.GetType().Name).ReplaceOneAsync(
(IClientSessionHandle) transactionSession, d => d.Id == clone.Id, clone,
new ReplaceOptions {IsUpsert = true});
}
}
public async FTask Save<T>(T entity, string collection = null) where T : Entity, new()
{
if (entity == null)
{
Log.Error($"save entity is null: {typeof(T).Name}");
return;
}
var clone = MongoHelper.Instance.Clone(entity);
using (await _mongoDataBaseLock.Lock(clone.Id))
{
await GetCollection(collection ?? clone.GetType().Name).ReplaceOneAsync(d => d.Id == clone.Id, clone,
new ReplaceOptions {IsUpsert = true});
}
}
private async FTask SaveBase<T>(T entity, string collection = null) where T : Entity
{
if (entity == null)
{
Log.Error($"save entity is null: {typeof(T).Name}");
return;
}
var clone = MongoHelper.Instance.Clone(entity);
using (await _mongoDataBaseLock.Lock(clone.Id))
{
await GetCollection(collection ?? clone.GetType().Name).ReplaceOneAsync(d => d.Id == clone.Id, clone, new ReplaceOptions {IsUpsert = true});
}
}
public async FTask Save(long id, List<Entity> entities)
{
if (entities == null)
{
Log.Error("save entity is null");
return;
}
var clones = MongoHelper.Instance.Clone(entities);
using (await _mongoDataBaseLock.Lock(id))
{
foreach (Entity clone in clones)
{
try
{
await GetCollection(clone.GetType().Name).ReplaceOneAsync(d => d.Id == clone.Id, clone,
new ReplaceOptions {IsUpsert = true});
}
catch (Exception e)
{
Log.Error($"Save List Entity Error: {clone.GetType().Name} {clone}\n{e}");
}
}
}
}
#endregion
#region Insert
public FTask Insert<T>(T entity, string collection = null) where T : Entity, new()
{
return Save(entity);
}
public async FTask InsertBatch<T>(IEnumerable<T> list, string collection = null) where T : Entity, new()
{
using (await _mongoDataBaseLock.Lock(RandomHelper.RandInt64()))
{
await GetCollection<T>(collection ?? typeof(T).Name).InsertManyAsync(list);
}
}
public async FTask InsertBatch<T>(object transactionSession, IEnumerable<T> list, string collection = null) where T : Entity, new()
{
using (await _mongoDataBaseLock.Lock(RandomHelper.RandInt64()))
{
await GetCollection<T>(collection ?? typeof(T).Name).InsertManyAsync((IClientSessionHandle) transactionSession, list);
}
}
#endregion
#region Remove
public async FTask<long> Remove<T>(object transactionSession, long id, string collection = null) where T : Entity, new()
{
using (await _mongoDataBaseLock.Lock(id))
{
var result = await GetCollection<T>(collection).DeleteOneAsync((IClientSessionHandle) transactionSession, d => d.Id == id);
return result.DeletedCount;
}
}
public async FTask<long> Remove<T>(long id, string collection = null) where T : Entity, new()
{
using (await _mongoDataBaseLock.Lock(id))
{
var result = await GetCollection<T>(collection).DeleteOneAsync(d => d.Id == id);
return result.DeletedCount;
}
}
public async FTask<long> Remove<T>(long id, object transactionSession, Expression<Func<T, bool>> filter, string collection = null) where T : Entity, new()
{
using (await _mongoDataBaseLock.Lock(id))
{
var result = await GetCollection<T>(collection).DeleteManyAsync((IClientSessionHandle) transactionSession, filter);
return result.DeletedCount;
}
}
public async FTask<long> Remove<T>(long id, Expression<Func<T, bool>> filter, string collection = null) where T : Entity, new()
{
using (await _mongoDataBaseLock.Lock(id))
{
var result = await GetCollection<T>(collection).DeleteManyAsync(filter);
return result.DeletedCount;
}
}
#endregion
#region Index
/// <summary>
/// 创建数据库索引
/// </summary>
/// <param name="collection"></param>
/// <param name="keys"></param>
/// <typeparam name="T"></typeparam>
/// <code>
/// 使用例子(可多个):
/// 1 : Builders.IndexKeys.Ascending(d=>d.Id)
/// 2 : Builders.IndexKeys.Descending(d=>d.Id).Ascending(d=>d.Name)
/// 3 : Builders.IndexKeys.Descending(d=>d.Id),Builders.IndexKeys.Descending(d=>d.Name)
/// </code>
public async FTask CreateIndex<T>(string collection, params object[] keys) where T : Entity
{
if (keys == null || keys.Length <= 0)
{
return;
}
var indexModels = new List<CreateIndexModel<T>>();
foreach (object key in keys)
{
IndexKeysDefinition<T> indexKeysDefinition = (IndexKeysDefinition<T>) key;
indexModels.Add(new CreateIndexModel<T>(indexKeysDefinition));
}
await GetCollection<T>(collection).Indexes.CreateManyAsync(indexModels);
}
public async FTask CreateIndex<T>(params object[] keys) where T : Entity
{
if (keys == null)
{
return;
}
List<CreateIndexModel<T>> indexModels = new List<CreateIndexModel<T>>();
foreach (object key in keys)
{
IndexKeysDefinition<T> indexKeysDefinition = (IndexKeysDefinition<T>) key;
indexModels.Add(new CreateIndexModel<T>(indexKeysDefinition));
}
await GetCollection<T>().Indexes.CreateManyAsync(indexModels);
}
#endregion
#region CreateDB
public async FTask CreateDB<T>() where T : Entity
{
// 已经存在数据库表
string name = typeof(T).Name;
if (_collections.Contains(name))
{
return;
}
await _mongoDatabase.CreateCollectionAsync(name);
_collections.Add(name);
}
public async FTask CreateDB(Type type)
{
string name = type.Name;
if (_collections.Contains(name))
{
return;
}
await _mongoDatabase.CreateCollectionAsync(name);
_collections.Add(name);
}
#endregion
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0aaa971dc58538e46b0e43df7cc72a2f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: