예제 #1
0
파일: Node.Cache.cs 프로젝트: neshlabs/nesh
        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);
        }
예제 #2
0
파일: Node.Cache.cs 프로젝트: neshlabs/nesh
        async Task <Entity> BuildCacheEntity(EntityTransaction trans)
        {
            EntityPrefab entity_prefab = Prefabs.GetEntity(trans.Type);

            if (entity_prefab == null)
            {
                throw new Exception($"Prefabs.GetEntity cant found {trans.Type}");
            }

            HashEntry[] fields = await trans.Fields;

            foreach (HashEntry entry in fields)
            {
                FieldPrefab field_prefab = entity_prefab.fields[entry.Name];
                if (field_prefab == null)
                {
                    continue;
                }

                switch (field_prefab.type)
                {
                case VarType.Bool:
                {
                    SetFieldValue <bool>(entry.Name, entry.Value, ref trans.Entity);
                }
                break;

                case VarType.Int:
                {
                    SetFieldValue <int>(entry.Name, entry.Value, ref trans.Entity);
                }
                break;

                case VarType.Float:
                {
                    SetFieldValue <float>(entry.Name, entry.Value, ref trans.Entity);
                }
                break;

                case VarType.Long:
                {
                    SetFieldValue <long>(entry.Name, entry.Value, ref trans.Entity);
                }
                break;

                case VarType.String:
                {
                    SetFieldValue <string>(entry.Name, entry.Value, ref trans.Entity);
                }
                break;

                case VarType.Nuid:
                {
                    SetFieldValue <Nuid>(entry.Name, entry.Value, ref trans.Entity);
                }
                break;

                case VarType.List:
                {
                    SetFieldValue <NList>(entry.Name, entry.Value, ref trans.Entity);
                }
                break;

                default:
                    break;
                }

                void SetFieldValue <T>(string field_name, RedisValue field_value, ref Entity entity)
                {
                    T value = JsonUtils.ToObject <T>(field_value);

                    Field field = entity.GetField(field_name);

                    if (field == null)
                    {
                        return;
                    }

                    NList res = NList.Empty;

                    field.TrySet(value, out res);
                }
            }

            foreach (KeyValuePair <string, Task <HashEntry[]> > table_task in trans.Tables)
            {
                TablePrefab table_prefab = entity_prefab.tables[table_task.Key];
                if (table_prefab == null)
                {
                    continue;
                }

                Table table_trans = trans.Entity.GetTable(table_task.Key);
                if (table_trans == null)
                {
                    continue;
                }

                HashEntry[] table_key_values = await table_task.Value;

                switch (table_prefab.primary_key.type)
                {
                case VarType.Bool:
                {
                    Table <bool> table = table_trans as Table <bool>;
                    foreach (HashEntry entry in table_key_values)
                    {
                        bool  key       = bool.Parse(entry.Name);
                        NList key_value = JsonUtils.ToObject <NList>(entry.Value);
                        table.TrySetKeyValue(key, key_value, out _);
                    }
                }
                break;

                case VarType.Int:
                {
                    Table <int> table = table_trans as Table <int>;
                    foreach (HashEntry entry in table_key_values)
                    {
                        int   key       = int.Parse(entry.Name);
                        NList key_value = JsonUtils.ToObject <NList>(entry.Value);
                        table.TrySetKeyValue(key, key_value, out _);
                    }
                }
                break;

                case VarType.Long:
                {
                    Table <long> table = table_trans as Table <long>;
                    foreach (HashEntry entry in table_key_values)
                    {
                        long  key       = long.Parse(entry.Name);
                        NList key_value = JsonUtils.ToObject <NList>(entry.Value);
                        table.TrySetKeyValue(key, key_value, out _);
                    }
                }
                break;

                case VarType.String:
                {
                    Table <string> table = table_trans as Table <string>;
                    foreach (HashEntry entry in table_key_values)
                    {
                        string key       = entry.Name;
                        NList  key_value = JsonUtils.ToObject <NList>(entry.Value);
                        table.TrySetKeyValue(key, key_value, out _);
                    }
                }
                break;

                case VarType.Nuid:
                {
                    Table <Nuid> table = table_trans as Table <Nuid>;
                    foreach (HashEntry entry in table_key_values)
                    {
                        Nuid  key       = Nuid.Parse(entry.Name);
                        NList key_value = JsonUtils.ToObject <NList>(entry.Value);
                        table.TrySetKeyValue(key, key_value, out _);
                    }
                }
                break;

                default:
                    break;
                }
            }

            return(trans.Entity);
        }