public async Task <long> AddRow(Nuid id, string table_name, NList value) { if (NList.IsEmpty(value)) { return(Global.INVALID_ROW); } Entity entity = EntityManager.Get(id); if (entity != null) { Table table = entity.GetTable(table_name); if (table == null) { return(Global.INVALID_ROW); } NList result; if (!table.TryAddRow(value, out result)) { return(Global.INVALID_ROW); } long row = result.Get <long>(0); NList row_value = result.Get <NList>(1); BatchCahceList.Add(NList.New().Add((int)CacheOption.SetRow).Add(id).Add(table_name).Add(row).Add(row_value)); await CallbackTable(id, table_name, TableEvent.AddRow, result); return(row); } else { if (id.Origin == Identity) { long row = IdUtils.RGen.CreateId(); if (await SetCacheRow(id, table_name, row, value)) { NList result = NList.New(); result.Add(row); result.Append(value); await CallbackTable(id, table_name, TableEvent.AddRow, result); return(row); } else { return(Global.INVALID_ROW); } } else { INode node = GrainFactory.GetGrain <INode>(id.Origin); return(await node.AddRow(id, table_name, value)); } } }
public async Task SetRowCol <T>(Nuid id, string table_name, long row, int col, T value) { if (value == null) { return; } Entity entity = EntityManager.Get(id); if (entity != null) { Table table = entity.GetTable(table_name); if (table == null) { return; } NList result; if (!table.TrySetRowCol(row, col, value, out result)) { return; } NList row_value = table.GetRow(row); BatchCahceList.Add(NList.New().Add((int)CacheOption.SetRow).Add(id).Add(table_name).Add(row).Add(row_value)); await CallbackTable(id, table_name, TableEvent.SetCol, result); } else { if (id.Origin == Identity) { NList row_value = NList.New(); NList old_value = await GetCacheRowValue(id, table_name, row); T old_row_value = old_value.Get <T>(col); row_value.Append(old_value); row_value.Set(col, value); if (await SetCacheRow(id, table_name, row, row_value)) { NList result = NList.New(); result.Add(row); result.Add(col); result.Add(old_row_value); result.Add(value); await CallbackTable(id, table_name, TableEvent.SetCol, result); } } else { INode node = GrainFactory.GetGrain <INode>(id.Origin); await node.SetRowCol(id, table_name, row, col, value); } } }
public async Task SetField <T>(Nuid id, string field_name, T field_value) { if (field_value == null) { return; } Entity entity = EntityManager.Get(id); if (entity != null) { Field field = entity.GetField(field_name); if (field == null) { return; } NList result; if (!field.TrySet(field_value, out result)) { return; } BatchCahceList.Add(NList.New().Add((int)CacheOption.SetField).Add(id).Add(field_name).Add(ProtoUtils.Serialize(field_value))); await CallbackField(id, field_name, FieldEvent.Change, result); } else { if (id.Origin == Identity) { T old_value = await GetCacheField <T>(id, field_name); if (await SetCacheField(id, field_name, field_value)) { NList result = NList.New(); result.Add(old_value); result.Add(field_value); await CallbackField(id, field_name, FieldEvent.Change, result); } } else { INode node = GrainFactory.GetGrain <INode>(id.Origin); await node.SetField(id, field_name, field_value); } } }
public async Task SetRowValue(Nuid id, string table_name, long row, NList value) { if (NList.IsEmpty(value)) { return; } Entity entity = EntityManager.Get(id); if (entity != null) { Table table = entity.GetTable(table_name); if (table == null) { return; } NList result; if (!table.TrySetRow(row, value, out result)) { return; } BatchCahceList.Add(NList.New().Add((int)CacheOption.SetRow).Add(id).Add(table_name).Add(row).Add(value)); await CallbackTable(id, table_name, TableEvent.SetRow, result); } else { if (id.Origin == Identity) { if (await SetCacheRow(id, table_name, row, value)) { NList result = NList.New(); result.Add(row); result.Append(value); await CallbackTable(id, table_name, TableEvent.SetRow, result); } } else { INode node = GrainFactory.GetGrain <INode>(id.Origin); await node.SetRowValue(id, table_name, row, value); } } }
public async Task <Nuid> Create(Nuid id, string type, NList args) { if (id.Origin == Identity) { Entity entity = EntityManager.Create(id, type); if (entity == null) { return(Nuid.Empty); } BatchCahceList.Add(NList.New().Add((int)CacheOption.SetEntity).Add(id).Add(type)); await CallbackEntity(id, EntityEvent.OnCreate, args); } else { INode node = GrainFactory.GetGrain <INode>(id.Origin); return(await node.Create(id, type, args)); } return(id); }
public async Task ClearTable(Nuid id, string table_name) { Entity entity = EntityManager.Get(id); if (entity != null) { Table table = entity.GetTable(table_name); if (table == null) { return; } if (table.IsEmpty) { return; } table.Clear(); BatchCahceList.Add(NList.New().Add((int)CacheOption.ClearTable).Add(id).Add(table_name)); await CallbackTable(id, table_name, TableEvent.Clear, NList.Empty); } else { if (id.Origin == Identity) { if (await ClearCacheTable(id, table_name)) { await CallbackTable(id, table_name, TableEvent.Clear, NList.Empty); } } else { INode node = GrainFactory.GetGrain <INode>(id.Origin); await node.ClearTable(id, table_name); } } }
private async Task BatchCache(object arg) { if (BatchCahceList.Count <= 0) { return; } try { int db = (int)(Identity % CacheUtils.EntityDBs); IRedisDatabase redis = _CacheClient.GetDb(db); ITransaction trans = redis.Database.CreateTransaction(); foreach (NList batch in BatchCahceList) { CacheOption option = (CacheOption)batch.Get <int>(0); Nuid entity_id = batch.Get <Nuid>(1); switch (option) { case CacheOption.SetEntity: { string entity_type = batch.Get <string>(2); string key = CacheUtils.BuildEntities(entity_id); Task task = trans.HashSetAsync(key, entity_id.Unique.ToString(), entity_type); } break; case CacheOption.DelEntity: { string entity_type = batch.Get <string>(2); EntityPrefab entity_prefab = Prefabs.GetEntity(entity_type); if (entity_prefab == null) { continue; } foreach (TablePrefab table_prefab in entity_prefab.tables.Values) { string table_key = CacheUtils.BuildTable(entity_id, table_prefab.name); Task table_task = trans.KeyDeleteAsync(table_key); } string field_key = CacheUtils.BuildFields(entity_id); Task field_task = trans.KeyDeleteAsync(field_key); string entity_key = CacheUtils.BuildEntities(entity_id); Task entity_task = trans.HashDeleteAsync(entity_key, entity_id.Unique.ToString()); } break; case CacheOption.SetField: { string field_name = batch.Get <string>(2); byte[] field_value = batch.Get <byte[]>(3); string key = CacheUtils.BuildFields(entity_id); Task task = trans.HashSetAsync(key, field_name, field_value); } break; case CacheOption.SetRow: { string table_name = batch.Get <string>(2); long row = batch.Get <long>(3); NList row_value = batch.Get <NList>(4); string key = CacheUtils.BuildTable(entity_id, table_name); Task task = trans.HashSetAsync(key, row, ProtoUtils.Serialize(row_value)); } break; case CacheOption.DelRow: { string table_name = batch.Get <string>(2); long row = batch.Get <long>(3); string key = CacheUtils.BuildTable(entity_id, table_name); Task task = trans.HashDeleteAsync(key, row); } break; case CacheOption.ClearTable: { string table_name = batch.Get <string>(2); string key = CacheUtils.BuildTable(entity_id, table_name); Task task = trans.KeyDeleteAsync(key); } break; default: break; } } bool result = await trans.ExecuteAsync(); if (result) { BatchCahceList.Clear(); } else { throw new Exception(); } } catch (Exception ex) { _Logger.LogError(ex, string.Format("{0} BatchCache ExecuteAsync Failed", Identity)); } }