public async Task <Nuid> Create(string type, Nuid origin, NList args) { long unique = IdGenerator.NewIdentity(); Nuid id = Nuid.New(unique, origin.Origin); return(await Create(id, type, args)); }
public async Task <Nuid> Create(string type, Nuid origin, NList args) { long unique = IdUtils.UGen.CreateId(); Nuid id = Nuid.New(unique, origin.Origin); return(await Create(id, type, args)); }
public override async Task OnActivateAsync() { Identity = this.GetPrimaryKeyLong(); _Logger = ServiceProvider.GetService <ILoggerFactory>().CreateLogger("Node[" + Identity + "]"); _CacheClient = ServiceProvider.GetService <IRedisCacheClient>(); _IMongoClient = ServiceProvider.GetService <IMongoClient>(); EntityDB = ServiceProvider.GetService <IEntityDB>(); IdGenerator = ServiceProvider.GetService <IIdGeneratorService>(); EntityManager = new EntityManager(); TimerManager = new TimerManager(this); NodeType = await EntityDB.GetNodeType(Identity); BatchCahceList = new List <NList>(); RegisterTimer(ScheduledSave, null, TimeSpan.FromSeconds(0), TimeSpan.FromMilliseconds(TimeUtils.MINITE)); bool persist = await EntityDB.IsPersist(Identity); if (persist) { IReadOnlyList <Entity> entities = await LoadPersistEntities(); if (NodeType == NodeType.Grain) { foreach (Entity entity in entities) { await Load(entity); } } else if (NodeType == NodeType.Cache) { if (!await CacheExist(Nuid.New(Identity, Identity))) { await SetCacheEntities(entities); } } } await base.OnActivateAsync(); }
public override async Task OnActivateAsync() { await base.OnActivateAsync(); _SystemListener = new Dictionary <int, Func <NList, Task> >(); _Logger = ServiceProvider.GetService <ILoggerFactory>().CreateLogger("RoleAgent[" + Role + "]"); Role = this.GetPrimaryKeyLong(); _RoleNode = GrainFactory.GetGrain <INode>(Role); _RoleId = Nuid.New(Role, Role); if (!await EntityDB.IsPersist(Role)) { await EntityDB.SetNodeType(Role, NodeType.Grain); await _RoleNode.Create(_RoleId, Player.TYPE, NList.New()); } await _RoleNode.BindAgent(this); RegisterSystem(SystemMsg.CLIENT.CUSTOM, OnRecv_Custom); }
private async Task PullPersistFields(string entity_type) { var database = _IMongoClient.GetDatabase(PersistUtils.ENTITY_DB); var collection = database.GetCollection <BsonDocument>(entity_type); FilterDefinitionBuilder <BsonDocument> builder = Builders <BsonDocument> .Filter; FilterDefinition <BsonDocument> filter = builder.And(builder.Eq("unique", Identity), builder.Eq("origin", Identity)); //获取数据 var result = (await collection.FindAsync <BsonDocument>(filter)).ToList(); EntityPrefab entity_prefab = Prefabs.GetEntity(entity_type); foreach (BsonDocument doc in result) { long unique = doc.GetValue("unique").AsInt64; long origin = doc.GetValue("origin").AsInt64; Entity entity = EntityManager.Create(Nuid.New(unique, origin), entity_type); foreach (FieldPrefab field_prefab in entity_prefab.fields.Values) { if (!field_prefab.save) { continue; } Field field = entity.GetField(field_prefab.name); if (field == null) { continue; } BsonValue bsonValue = doc.GetValue(field_prefab.name); switch (field_prefab.type) { case VarType.Bool: { bool value = bsonValue.AsBoolean; field.TrySet(value, out NList res); } break; case VarType.Int: { int value = bsonValue.AsInt32; field.TrySet(value, out NList res); } break; case VarType.Long: { long value = bsonValue.AsInt64; field.TrySet(value, out NList res); } break; case VarType.Float: { float value = (float)bsonValue.AsDouble; field.TrySet(value, out NList res); } break; case VarType.String: { string value = bsonValue.AsString; field.TrySet(value, out NList res); } break; case VarType.Time: { DateTime value = bsonValue.AsBsonDateTime.ToUniversalTime(); field.TrySet(value, out NList res); } break; case VarType.Nuid: { string value = bsonValue.AsBsonDocument.ToJson(); Nuid nuid = JsonUtils.ToObject <Nuid>(value); field.TrySet(nuid, out NList res); } break; case VarType.List: { string value = bsonValue.AsBsonDocument.ToJson(); NList lst = JsonUtils.ToObject <NList>(value); field.TrySet(lst, out NList res); } break; } } } //int count = await collection.CountAsync(); //BsonDocument b = collection.AsQueryable(); //var list = (await collection.FindAsync(x => x.GetValue(Global.MARK_UNIQUE) == Identity)).ToList(); }
private async Task <IReadOnlyList <Entity> > LoadPersistEntities() { var database = _IMongoClient.GetDatabase(PersistUtils.ENTITY_DB); try { var collection = database.GetCollection <EntityList>(PersistUtils.ENTITIES); var filter = Builders <EntityList> .Filter.Eq(n => n.origin, Identity); IAsyncCursor <EntityList> res = await collection.FindAsync(filter); EntityList entity_list = await res.FirstOrDefaultAsync(); if (entity_list == null) { return(null); } List <Entity> entities = new List <Entity>(); foreach (EntityChild entity_child in entity_list.entities) { EntityPrefab entity_prefab = Prefabs.GetEntity(entity_child.type); if (entity_prefab == null) { continue; } BsonDocument doc = entity_child.entity; long unique = doc.GetValue(Global.MARK_UNIQUE).AsInt64; Entity entity = Entity.Gen(entity_child.type); entity.Id = Nuid.New(unique, Identity); foreach (FieldPrefab field_prefab in entity_prefab.fields.Values) { if (!field_prefab.save) { continue; } Field field = entity.GetField(field_prefab.name); if (field == null) { continue; } BsonValue bsonValue = doc.GetValue(field_prefab.name); switch (field_prefab.type) { case VarType.Bool: { bool value = bsonValue.AsBoolean; field.TrySet(value, out _); } break; case VarType.Int: { int value = bsonValue.AsInt32; field.TrySet(value, out _); } break; case VarType.Long: { long value = bsonValue.AsInt64; field.TrySet(value, out _); } break; case VarType.Float: { float value = (float)bsonValue.AsDouble; field.TrySet(value, out _); } break; case VarType.String: { string value = bsonValue.AsString; field.TrySet(value, out _); } break; case VarType.Nuid: { string value = bsonValue.AsBsonDocument.ToJson(); Nuid nuid = JsonUtils.ToObject <Nuid>(value); field.TrySet(value, out _); } break; case VarType.List: { string value = bsonValue.AsBsonDocument.ToJson(); NList lst = JsonUtils.ToObject <NList>(value); field.TrySet(value, out _); } break; default: break; } } foreach (TablePrefab table_prefab in entity_prefab.tables.Values) { if (!table_prefab.save) { continue; } BsonArray bsonarr = doc.GetValue(table_prefab.name) as BsonArray; foreach (BsonDocument key_value_bson in bsonarr.Values) { BsonValue pk_bson = key_value_bson.GetValue(nameof(TablePrefab.primary_key)); switch (table_prefab.primary_key.type) { case VarType.Bool: { bool value = pk_bson.AsBoolean; LoadTableKeyValue(value); } break; case VarType.Int: { int value = pk_bson.AsInt32; LoadTableKeyValue(value); } break; case VarType.Long: { long value = pk_bson.AsInt64; LoadTableKeyValue(value); } break; case VarType.String: { string value = pk_bson.AsString; LoadTableKeyValue(value); } break; case VarType.Nuid: { string value = pk_bson.AsBsonDocument.ToJson(); Nuid nuid = JsonUtils.ToObject <Nuid>(value); LoadTableKeyValue(nuid); } break; default: break; } void LoadTableKeyValue <TPrimaryKey>(TPrimaryKey primary_key) { Table <TPrimaryKey> table = entity.GetTable(table_prefab.name) as Table <TPrimaryKey>; NList key_value = NList.New(); for (int col = 0; col < table_prefab.cols; col++) { TablePrefab.ColumnPrefab column = table_prefab.columns[col]; BsonValue col_bson = key_value_bson.GetValue(column.name); switch (column.type) { case VarType.Bool: { bool value = col_bson.AsBoolean; key_value.Add(value); } break; case VarType.Int: { int value = col_bson.AsInt32; key_value.Add(value); } break; case VarType.Long: { long value = col_bson.AsInt64; key_value.Add(value); } break; case VarType.Float: { float value = (float)col_bson.AsDouble; key_value.Add(value); } break; case VarType.String: { string value = col_bson.AsString; key_value.Add(value); } break; case VarType.Nuid: { string value = col_bson.AsBsonDocument.ToJson(); Nuid nuid = JsonUtils.ToObject <Nuid>(value); key_value.Add(value); } break; case VarType.List: { string value = col_bson.AsBsonDocument.ToJson(); NList lst = JsonUtils.ToObject <NList>(value); key_value.Add(value); } break; default: break; } } table.TrySetKeyValue(primary_key, key_value, out _); } } } entities.Add(entity); } return(entities); } catch (Exception ex) { _Logger.LogError(ex, "LoadPersisitEntities Failed!"); } return(null); }
private async Task SetCacheEntities(IReadOnlyList <Entity> entities) { IRedisDatabase db = GetCache(); ITransaction trans = db.Database.CreateTransaction(); { string key = CacheUtils.BuildEntities(Nuid.New(Identity, Identity)); HashEntry[] hashFields = new HashEntry[entities.Count]; for (int i = 0; i < entities.Count; i++) { hashFields[i] = new HashEntry(entities[i].Id.Unique, entities[i].Type); } Task _ = trans.HashSetAsync(key, hashFields); } { foreach (Entity entity in entities) { EntityPrefab entity_prefab = Prefabs.GetEntity(entity.Type); if (entity_prefab == null) { continue; } string fields_key = CacheUtils.BuildFields(entity.Id); Field[] fields = entity.GetFields(); List <HashEntry> cache_fields = new List <HashEntry>(); for (int i = 0; i < fields.Length; i++) { FieldPrefab field_prefab = entity_prefab.fields[fields[i].Name]; if (field_prefab == null) { continue; } string field_value = ""; switch (field_prefab.type) { case VarType.Bool: field_value = JsonUtils.ToJson(fields[i].Get <bool>()); break; case VarType.Int: field_value = JsonUtils.ToJson(fields[i].Get <int>()); break; case VarType.Long: field_value = JsonUtils.ToJson(fields[i].Get <long>()); break; case VarType.Float: field_value = JsonUtils.ToJson(fields[i].Get <float>()); break; case VarType.String: field_value = JsonUtils.ToJson(fields[i].Get <string>()); break; case VarType.Nuid: field_value = JsonUtils.ToJson(fields[i].Get <Nuid>()); break; case VarType.List: field_value = JsonUtils.ToJson(fields[i].Get <NList>()); break; default: break; } cache_fields.Add(new HashEntry(fields[i].Name, field_value)); } Task _ = trans.HashSetAsync(fields_key, cache_fields.ToArray()); Table[] tables = entity.GetTables(); foreach (Table table in tables) { string table_key = CacheUtils.BuildTable(entity.Id, table.GetName()); TablePrefab table_prefab = entity_prefab.tables[table.GetName()]; if (table_prefab == null) { continue; } List <HashEntry> cache_key_values = new List <HashEntry>(); void SetCacheKeyValue <TPrimaryKey>() { Table <TPrimaryKey> t = table as Table <TPrimaryKey>; IReadOnlyList <TPrimaryKey> keys = t.GetPrimaryKeys(); foreach (TPrimaryKey key in keys) { string json = JsonUtils.ToJson(t.GetKeyValue(key)); cache_key_values.Add(new HashEntry(key.ToString(), json)); } } switch (table_prefab.primary_key.type) { case VarType.Bool: SetCacheKeyValue <bool>(); break; case VarType.Int: SetCacheKeyValue <int>(); break; case VarType.Long: SetCacheKeyValue <long>(); break; case VarType.Float: SetCacheKeyValue <float>(); break; case VarType.String: SetCacheKeyValue <string>(); break; case VarType.Nuid: SetCacheKeyValue <Nuid>(); break; default: break; } Task __ = trans.HashSetAsync(table_key, cache_key_values.ToArray()); } } bool redis_execute = await trans.ExecuteAsync(); if (!redis_execute) { throw new Exception("trans ExecuteAsync ERROR!!"); } } }
private async Task <IReadOnlyList <Entity> > GetCacheEntities() { Nuid id = Nuid.New(Identity, Identity); IRedisDatabase db = GetCache(id); string entites_key = CacheUtils.BuildEntities(id); List <Entity> sorts = new List <Entity>(); List <EntityTransaction> entity_trans = new List <EntityTransaction>(); try { HashEntry[] entities = await db.Database.HashGetAllAsync(entites_key); foreach (HashEntry member in entities) { long child_id = long.Parse(member.Name); string type = member.Value; Nuid entity_id = Nuid.New(child_id, Identity); EntityTransaction trans = new EntityTransaction(entity_id, type); if (trans.Entity == null) { continue; } entity_trans.Add(trans); } ITransaction query_trans = db.Database.CreateTransaction(); foreach (EntityTransaction trans in entity_trans) { EntityPrefab entity_prefab = Prefabs.GetEntity(trans.Type); if (entity_prefab == null) { throw new Exception($"Prefabs.GetEntity cant found {trans.Type}"); } string field_key = CacheUtils.BuildFields(trans.Id); trans.Fields = query_trans.HashGetAllAsync(field_key); foreach (Table table in trans.Entity.GetTables()) { TablePrefab table_prefab = entity_prefab.tables[table.GetName()]; string table_key = CacheUtils.BuildTable(trans.Id, table.GetName()); Task <HashEntry[]> key_values = query_trans.HashGetAllAsync(table_key); trans.AddTableTrans(table.GetName(), key_values); } } bool redis_execute = await query_trans.ExecuteAsync(); if (!redis_execute) { throw new Exception("query_trans ExecuteAsync ERROR!!"); } foreach (EntityTransaction trans in entity_trans) { Entity entity = await BuildCacheEntity(trans); sorts.Add(entity); } } catch (Exception ex) { _Logger.LogError(ex, "GetCacheEntities Failed"); } entity_trans.Clear(); sorts.Sort((x, y) => { EntityPrefab entity_x = Prefabs.GetEntity(x.Type); EntityPrefab entity_y = Prefabs.GetEntity(y.Type); return(entity_y.priority - entity_x.priority); }); return(sorts); }