Esempio n. 1
0
        private async Task <long> FindCacheRow <T>(Nuid id, string table_name, int col, T value)
        {
            if (!await CacheExist(id))
            {
                return(Global.INVALID_ROW);
            }

            long row = Global.INVALID_ROW;

            try
            {
                IRedisDatabase             db         = GetCache(id);
                string                     key        = CacheUtils.BuildTable(id, table_name);
                Dictionary <string, NList> row_values = await db.HashGetAllAsync <NList>(key);

                foreach (KeyValuePair <string, NList> pair in row_values)
                {
                    if (pair.Value.Get <T>(col).Equals(value))
                    {
                        row = long.Parse(pair.Key);
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                _Logger.LogError(ex, "'{0} FindCacheRow error for table={1}", id, table_name);
            }

            return(row);
        }
Esempio n. 2
0
        private async Task <NList> GetCacheRowValue(Nuid id, string table_name, long row)
        {
            if (!await CacheExist(id))
            {
                return(NList.Empty);
            }

            IRedisDatabase db  = GetCache(id);
            string         key = CacheUtils.BuildTable(id, table_name);

            return(await db.HashGetAsync <NList>(key, row.ToString()));
        }
Esempio n. 3
0
        private async Task <NList> GetCacheTableKeyValue <TPrimaryKey>(Nuid id, string table_name, TPrimaryKey primary_key)
        {
            if (!await CacheExist(id))
            {
                return(NList.Empty);
            }

            IRedisDatabase db  = GetCache(id);
            string         key = CacheUtils.BuildTable(id, table_name);

            return(await db.HashGetAsync <NList>(key, primary_key.ToString()));
        }
Esempio n. 4
0
        private async Task <Entity> GetCacheEntity(Nuid entity_id)
        {
            try
            {
                IRedisDatabase db          = GetCache(entity_id);
                ITransaction   query_trans = db.Database.CreateTransaction();

                string type = await GetCacheType(entity_id);

                EntityTransaction trans = new EntityTransaction(entity_id, type);


                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!!");
                }

                return(await BuildCacheEntity(trans));
            }
            catch (Exception ex)
            {
                _Logger.LogError(ex, "GetCacheEntities Failed");
            }

            return(null);
        }
Esempio n. 5
0
        private async Task <NList> GetCacheRows(Nuid id, string table_name)
        {
            if (!await CacheExist(id))
            {
                return(NList.Empty);
            }

            IRedisDatabase db  = GetCache(id);
            string         key = CacheUtils.BuildTable(id, table_name);

            NList rows = NList.New();

            foreach (string hash_key in await db.HashKeysAsync(key))
            {
                int row = int.Parse(hash_key);
                rows.Add(row);
            }

            return(rows.Count > 0 ? rows : NList.Empty);
        }
Esempio n. 6
0
        private async Task <bool> ClearCacheTable(Nuid id, string table_name)
        {
            if (!await CacheExist(id))
            {
                return(false);
            }

            bool result = false;

            try
            {
                IRedisDatabase db  = GetCache(id);
                string         key = CacheUtils.BuildTable(id, table_name);
                result = await db.RemoveAsync(key);
            }
            catch (Exception ex)
            {
                _Logger.LogError(ex, "'{0} ClearCacheTable error for table={1}", id, table_name);
            }

            return(result);
        }
Esempio n. 7
0
        private async Task <bool> DelCacheRow(Nuid id, string table_name, long row)
        {
            if (!await CacheExist(id))
            {
                return(false);
            }

            bool result = false;

            try
            {
                IRedisDatabase db  = GetCache(id);
                string         key = CacheUtils.BuildTable(id, table_name);
                result = await db.HashDeleteAsync(key, row.ToString());
            }
            catch (Exception ex)
            {
                _Logger.LogError(ex, "'{0} DelCacheRow error for table={1} row={2}", id, table_name, row);
            }

            return(result);
        }
Esempio n. 8
0
        private async Task <bool> SetCacheTableKeyValue <TPrimaryKey>(Nuid id, string table_name, TPrimaryKey primary_key, NList value)
        {
            if (!await CacheExist(id))
            {
                return(false);
            }

            bool result = false;

            try
            {
                IRedisDatabase db  = GetCache(id);
                string         key = CacheUtils.BuildTable(id, table_name);
                result = await db.HashSetAsync(key, primary_key.ToString(), value);
            }
            catch (Exception ex)
            {
                _Logger.LogError(ex, "'{0} SetCacheRow error for table={1} row={2} value={3}", id, table_name, primary_key, value);
            }

            return(result);
        }
Esempio n. 9
0
        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));
            }
        }
Esempio n. 10
0
        public async Task Destroy(Nuid id)
        {
            if (id.Origin != Identity)
            {
                INode node = GrainFactory.GetGrain <INode>(id.Origin);
                if (await node.IsActive())
                {
                    await node.Destroy(id);

                    return;
                }
            }

            if (NodeType == NodeType.Grain)
            {
                Entity entity = EntityManager.Get(id);
                if (entity == null)
                {
                    _Logger.LogError($"{id} Destroy Entity Failed When not found!");
                    return;
                }

                if (EntityManager.Remove(id))
                {
                    _Logger.LogError($"{id} Destroy Entity Failed When EntityManager Remove!");
                    return;
                }
            }
            else if (NodeType == NodeType.Cache)
            {
                if (!await CacheExist(id))
                {
                    _Logger.LogError($"{id} Destroy Entity Failed when not CacheExist!");
                    return;
                }

                string entity_type = await GetCacheType(id);

                EntityPrefab entity_prefab = Prefabs.GetEntity(entity_type);
                if (entity_prefab == null)
                {
                    _Logger.LogError($"{id} Destroy Entity Failed when not EntityPrefab {entity_type}!");
                    return;
                }

                int            db    = (int)(Identity % CacheUtils.EntityDBs);
                IRedisDatabase redis = _CacheClient.GetDb(db);

                ITransaction trans = redis.Database.CreateTransaction();

                foreach (TablePrefab table_prefab in entity_prefab.tables.Values)
                {
                    string table_key  = CacheUtils.BuildTable(id, table_prefab.name);
                    Task   table_task = trans.KeyDeleteAsync(table_key);
                }

                string field_key  = CacheUtils.BuildFields(id);
                Task   field_task = trans.KeyDeleteAsync(field_key);

                string entity_key  = CacheUtils.BuildEntities(id);
                Task   entity_task = trans.HashDeleteAsync(entity_key, id.Unique);

                bool result = await trans.ExecuteAsync();

                if (!result)
                {
                    _Logger.LogError($"{id} Destroy Entity Failed when ITransaction Execute");
                    return;
                }
            }

            await CallbackEntity(id, EntityEvent.OnDestroy, NList.Empty);

            await SyncEntity(id, NList.New().Add(id).Add((int)EntityEvent.OnDestroy));
        }
Esempio n. 11
0
        private async Task <NList> GetCacheKeys(Nuid id, string table_name)
        {
            if (!await CacheExist(id))
            {
                return(NList.Empty);
            }

            IRedisDatabase db  = GetCache(id);
            string         key = CacheUtils.BuildTable(id, table_name);

            string type = await GetCacheType(id);

            EntityPrefab entity_prefab = Prefabs.GetEntity(type);

            if (entity_prefab == null)
            {
                return(NList.Empty);
            }

            TablePrefab table_prefab = entity_prefab.tables[table_name];

            if (table_prefab == null)
            {
                return(NList.Empty);
            }

            NList rows = NList.New();

            foreach (string hash_key in await db.HashKeysAsync(key))
            {
                switch (table_prefab.primary_key.type)
                {
                case VarType.Bool:
                {
                    bool row = bool.Parse(hash_key);
                    rows.Add(row);
                }
                break;

                case VarType.Int:
                {
                    int row = int.Parse(hash_key);
                    rows.Add(row);
                }
                break;

                case VarType.Long:
                {
                    long row = long.Parse(hash_key);
                    rows.Add(row);
                }
                break;

                case VarType.Nuid:
                {
                    Nuid row = Nuid.Parse(hash_key);
                    rows.Add(row);
                }
                break;

                case VarType.String:
                {
                    string row = hash_key;
                    rows.Add(row);
                }
                break;

                default:
                    break;
                }
            }

            return(rows.Count > 0 ? rows : NList.Empty);
        }
Esempio n. 12
0
        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!!");
                }
            }
        }
Esempio n. 13
0
        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);
        }