Ejemplo n.º 1
0
        private void _AddRelation <To, ToKeyType>(
            string RelationName,
            RelationTableInfo <To, ToKeyType> ThisRelation,
            Table <To, ToKeyType> .RelationTableInfo <ValueType, KeyType> ThatRelation)
            where ToKeyType : IComparable <ToKeyType>
        {
#if TRACE
            Console.WriteLine("@ " + this.GetType().Namespace + this.GetType().Name + " _AddRelation_X_X");
#endif

            _AddRelationForLoading(RelationName,
                                   ThisRelation,
                                   Accepted: (Key, AcceptedKey, PartTable) =>
            {
#if TRACE
                Console.WriteLine("@ " + this.GetType().Namespace + this.GetType().Name + " _AddRelation_X_X >> Accepted");
#endif
                using (this.IgnoreUpdateAble.UseBlock())
                {
                    PartTable.SaveToParent();
                }


                using (ThisRelation.LinkArray.IgnoreUpdateAble.UseBlock())
                {
                    ThisRelation.LinkArray.Update(AcceptedKey,
                                                  (c) => ThatRelation.Field.Value(c).Accept(Key));
                }
            },
                                   Ignored: (Key, IgnoredKey, PartTable) =>
            {
#if TRACE
                Console.WriteLine("@ " + this.GetType().Namespace + this.GetType().Name + " _AddRelation_X_X >> Ignored");
#endif
                using (this.IgnoreUpdateAble.UseBlock())
                {
                    PartTable.SaveToParent();
                }


                using (ThisRelation.LinkArray.IgnoreUpdateAble.UseBlock())
                {
                    ThisRelation.LinkArray.Update(IgnoredKey,
                                                  (c) => (ThatRelation.Field.Value(c)).Ignore(Key));
                }
            });

            this.KeyChanged += (info) =>
            {
#if TRACE
                Console.WriteLine("@ " + this.GetType().Namespace + this.GetType().Name + " _AddRelation_X_X >> KeyChanged");
#endif
                if (Run.Use(RelationName))
                {
                    using (ThisRelation.LinkArray.IgnoreUpdateAble.UseBlock())
                    {
                        var ThisValue = ThisRelation.Field.Value(info.Value);
                        foreach (var ThatValue in ThisValue)
                        {
                            var ThatRelationTbl = ThatRelation.Field.Value(ThatValue);
                            ThatRelationTbl.KeysInfo.Keys.BinaryDelete(info.OldKey);
                            ThatRelationTbl.KeysInfo.Keys.BinaryInsert(info.NewKey);
                            ThisRelation.LinkArray.Update(ThatValue);
                        }
                    }
                }
            };

            this.Events.Deleted += (info) =>
            {
#if TRACE
                Console.WriteLine("@ " + this.GetType().Namespace + this.GetType().Name + " _AddRelation_X_X >> Deleted");
#endif
                if (Run.Use(RelationName))
                {
                    using (ThisRelation.LinkArray.IgnoreUpdateAble.UseBlock())
                    {
                        ThisRelation.LinkArray.Update(ThisRelation.Field.Value(info.Value),
                                                      (c) => ThatRelation.Field.Value(c).Ignore(
                                                          (KeyType)info.Info[ThatRelation.Field.Value(c).KeyPos].Key));
                    }
                }
            };

            var RelationName_Update = "U" + ThisRelation.Link.ToString() + RelationName;
            if (ThisRelation.IsUpdateAble)
            {
                this.Events.Updated += (info) =>
                {
#if TRACE
                    Console.WriteLine("@ " + this.GetType().Namespace + this.GetType().Name + " _AddRelation_X_X >> Updated");
#endif
                    if (this.IgnoreUpdateAble.BlockLengths == 0)
                    {
                        if (Run.Use(RelationName_Update))
                        {
                            using (ThisRelation.LinkArray.IgnoreUpdateAble.UseBlock())
                            {
                                var ThisValue = ThisRelation.Field.Value(info.Value);
                                foreach (var ThatValue in ThisValue)
                                {
                                    ThisRelation.LinkArray.Update(ThatValue, (c) =>
                                    {
                                        var MyInfo = info.Info[KeyPos];
                                        ThatRelation.Field.Value(c).UpdateAble.Changed(
                                            (KeyType)MyInfo.OldKey, (KeyType)MyInfo.OldKey);
                                    });
                                }
                            }
                        }
                    }
                };
            }
            if (ThisRelation.ClearRelationOnSendUpdate)
            {
                this._ClearRelation(ThisRelation.Link);
            }
            if (ThatRelation.ClearRelationOnSendUpdate)
            {
                ThisRelation.LinkArray._ClearRelation(ThatRelation.Link);
            }
        }
        public void AddRelation <To, ToKeyType>(
            RelationItemInfo <To, ToKeyType> ThisRelation,
            Table <To, ToKeyType> .RelationTableInfo <ValueType, KeyType> ThatRelation)
            where ToKeyType : IComparable <ToKeyType>
        {
#if TRACE
            Console.WriteLine("@ " + this.GetType().Namespace + this.GetType().Name + " _AddRelation_1_X");
#endif
            var RelationName = ThisRelation.Link.Body.ToString() + ThatRelation.Link.Body.ToString();

            this._AddRelationForLoading(RelationName,
                                        ThisRelation);

            ThisRelation.LinkArray._AddRelationForLoading(RelationName,
                                                          ThatRelation,
                                                          Accepted: (Key, AcceptedKey, PartTable) =>
            {
#if TRACE
                Console.WriteLine("@ " + this.GetType().Namespace + this.GetType().Name + " _AddRelation_1_X >> Accepted");
#endif
                using (ThisRelation.LinkArray.IgnoreUpdateAble.Block())
                {
                    PartTable.SaveToParent();
                }

                using (this.IgnoreUpdateAble.Block())
                {
                    this.Update(AcceptedKey,
                                (c) => ThisRelation.Field.Value(c, (f) => { f.Key = Key; return(f); }));
                }
            },
                                                          Ignored: (Key, IgnoredKey, PartTable) =>
            {
#if TRACE
                Console.WriteLine("@ " + this.GetType().Namespace + this.GetType().Name + " _AddRelation_1_X >> Ignored");
#endif
                using (ThisRelation.LinkArray.IgnoreUpdateAble.Block())
                {
                    PartTable.SaveToParent();
                }


                using (this.IgnoreUpdateAble.Block())
                {
                    if (this.PositionOf(IgnoredKey) > -1)
                    {
                        this.Update(IgnoredKey,
                                    (c) => ThisRelation.Field.Value(c, (f) => { f.Key = null; return(f); }));
                    }
                }
            },
                                                          MakeNew: (c) =>
            {
                ThisRelation.Field.Value(c.NewValue, (f) =>
                {
                    f.Key = ThatRelation.OwnerArray.GetKey(c.LoadedValue);
                    return(f);
                });
            });

            this.KeyChanged += (info) =>
            {
#if TRACE
                Console.WriteLine("@ " + this.GetType().Namespace + this.GetType().Name + " _AddRelation_1_X >> KeyChanged");
#endif
                if (Run.Use(RelationName))
                {
                    using (ThisRelation.LinkArray.IgnoreUpdateAble.Block())
                    {
                        var ThisRelationArray = ThisRelation.Field.Value(info.Value);
                        if (ThisRelationArray.Key != null)
                        {
                            ThisRelation.LinkArray.Update((ToKeyType)ThisRelationArray.Key,
                                                          (c) =>
                            {
                                ThatRelation.Field.Value(c).Ignore(info.OldKey);
                                ThatRelation.Field.Value(c).Accept(info.NewKey);
                            });
                        }
                    }
                }
            };

            ThisRelation.LinkArray.KeyChanged += (info) =>
            {
#if TRACE
                Console.WriteLine("@ " + this.GetType().Namespace + this.GetType().Name + " _AddRelation_1_X >> KeyChanged");
#endif
                if (Run.Use(RelationName))
                {
                    using (this.IgnoreUpdateAble.Block())
                    {
                        var ThatRelation_array = ThatRelation.Field.Value(info.Value);
                        for (int i = 0; i < ThatRelation_array.KeysInfo.Keys.Length; i++)
                        {
                            var ThisValue = ThatRelation_array[i];
                            var Key       = this.GetKey(ThisValue);
                            ThisRelation.Field.Value(ThisValue.Value, (f) => { f.Key = info.NewKey; return(f); });
                            ThisRelation.Field.Value(ThisValue.Value, (f) => { f.OldKey = info.NewKey; return(f); });
                            this.Update(Key, ThisValue);
                        }
                    }
                }
            };

            this.Events.Deleted += (Info) =>
            {
#if TRACE
                Console.WriteLine("@ " + this.GetType().Namespace + this.GetType().Name + " _AddRelation_1_X >> Deleted");
#endif
                if (Run.Use(RelationName))
                {
                    using (ThisRelation.LinkArray.IgnoreUpdateAble.Block())
                    {
                        var ThisRelationArray = ThisRelation.Field.Value(Info.Value);
                        if (ThisRelationArray.Key != null)
                        {
                            ThisRelation.LinkArray.Update((ToKeyType)ThisRelationArray.Key,
                                                          (c) =>
                            {
                                ThatRelation.Field.Value(c).Ignore((KeyType)Info.Info[KeyPos].Key);
                            });
                        }
                    }
                }
            };

            if (ThisRelation.IsChild)
            {
                ThisRelation.LinkArray.Events.Deleted += (info) =>
                {
#if TRACE
                    Console.WriteLine("@ " + this.GetType().Namespace + this.GetType().Name + " _AddRelation_1_X >> Deleted IsChild");
#endif
                    if (Run.Use(RelationName))
                    {
                        var ThatRelation_array = ThatRelation.Field.Value(info.Value);
                        foreach (var ThisKey in ThatRelation_array.KeysInfo.Keys)
                        {
                            this.Delete(ThisKey);
                        }
                    }
                };
            }
            else
            {
                ThisRelation.LinkArray.Events.Deleted += (info) =>
                {
#if TRACE
                    Console.WriteLine("@ " + this.GetType().Namespace + this.GetType().Name + " _AddRelation_1_X >> Deleted");
#endif
                    if (Run.Use(RelationName))
                    {
                        var ThatRelation_array = ThatRelation.Field.Value(info.Value);
                        for (int i = 0; i < ThatRelation_array.KeysInfo.Keys.Length; i++)
                        {
                            var ThisValue = ThatRelation_array[i];
                            var Key       = this.GetKey(ThisValue);
                            ThisRelation.Field.Value(ThisValue.Value, (f) => { f.Key = null; return(f); });
                            this.Update(Key, ThisValue);
                        }
                    }
                };
            }

            Action <(ValueType Value, Events <ValueType> .ValueInfo[] Info)> OnSave = (Value) =>
            {
#if TRACE
                Console.WriteLine("@ " + this.GetType().Namespace + this.GetType().Name + " _AddRelation_1_X >> OnSave");
#endif
                if (Run.Use(RelationName))
                {
#if TRACE
                    Console.WriteLine(">> var ThisRelationArray = ThisRelation.Field.Value(ref Value.Value);");
#endif
                    var ThisRelationArray = ThisRelation.Field.Value(Value.Value);
                    var Key    = ThisRelationArray.Key;
                    var OldKey = ThisRelationArray.OldKey;
                    if (Compare(Key, OldKey) != 0)
                    {
                        var ChangedKey = this.GetKey(Value.Value);
                        if (Key != null)
                        {
                            ThisRelation.LinkArray.Update((ToKeyType)ThisRelationArray.Key,
                                                          (c) =>
                            {
#if TRACE
                                Console.WriteLine(">> Key != null");
#endif
                                ThatRelation.Field.Value(c).Accept(ChangedKey);
                            });
                        }
                        if (OldKey != null)
                        {
#if TRACE
                            Console.WriteLine(">> OldKey != null");
#endif
                            ThisRelation.LinkArray.Update((ToKeyType)ThisRelationArray.Key,
                                                          (c) =>
                            {
                                ThatRelation.Field.Value(c).Ignore(ChangedKey);
                            });
                        }
                    }
                }
            };

            this.Events.Inserted += (Value) => OnSave(Value);
            this.Events.Updated  += (Value) => OnSave(Value);

            if (ThatRelation.IsUpdateAble)
            {
                this.Events.Updated += (info) =>
                {
#if TRACE
                    Console.WriteLine("@ " + this.GetType().Namespace + this.GetType().Name + " _AddRelation_1_X >> Updated");
#endif
                    if (this.IgnoreUpdateAble.BlockLengths == 0)
                    {
                        var ThatValue = ThisRelation.Field.Value(info.Value);
                        if (ThatValue.Key != null)
                        {
                            if (Run.Use(RelationName + "UP"))
                            {
                                using (ThisRelation.LinkArray.IgnoreUpdateAble.Block())
                                {
                                    ThisRelation.LinkArray.Update(ThatValue, (c) =>
                                    {
                                        var MyInfo = info.Info[KeyPos];
                                        ThatRelation.Field.Value(c).UpdateAble.Changed(
                                            (KeyType)MyInfo.OldKey, (KeyType)MyInfo.OldKey);
                                    });
                                }
                            }
                        }
                    }
                };
            }


            ThisRelation.LinkArray.Events.Saving += (Value) =>
            {
            };
            if (ThatRelation.ClearRelationOnSendUpdate)
            {
                ThisRelation.LinkArray._ClearRelation(ThatRelation.Link);
            }
            ThisRelation.OwnerArray._MoveRelations(ThisRelation.Link);
        }
Ejemplo n.º 3
0
            public async Task MakeUpdate(ulong ServerLastUpCode)
            {
                if (Table.UpdateAble.UpdateCode > ServerLastUpCode)
                {
                    Table.UpdateAble.Clear();
                    if (IsPartOfTable)
                    {
                        ParentTable.UpdateAble.Clear();
                    }
                }
                while (StartPos <= EndPos)
                {
                    await FindLastTrue();

                    StartPos++;
                    if (StartPos >= ServerItemsCount)
                    {
                        break;
                    }
                    else
                    {
                        var NextUpCode = await GetUpdateCodeAtPos(StartPos);

                        DeleteExtraItems(NextUpCode);
                        if (StartPos >= Table.UpdateAble.UpdateCodes.Length)
                        {
                            break;
                        }
                        var MyUpCode = Table.UpdateAble.UpdateCodes[StartPos];
                        if (MyUpCode.UpdateCode > NextUpCode)
                        {
                            await UpdateFromPosToUpCode(StartPos, MyUpCode.UpdateCode);
                        }
                    }
                }
                if (StartPos < ServerItemsCount)
                {
                    await UpdateFromPosToEnd(StartPos);
                }
                else
                {
                    DeleteItemsFrom(ServerItemsCount);
                }

                if (IsPartOfTable)
                {
                    foreach (var Delete in ShouldDelete)
                    {
                        if (await IsExistAtParent(Delete))
                        {
                            PartTable.Ignore(Delete);
                        }
                        else
                        {
                            PartTable.Delete(Delete);
                        }
                    }
                }
                else
                {
                    foreach (var Delete in ShouldDelete)
                    {
                        Table.Delete(Delete);
                    }
                }

                Table.UpdateAble.UpdateCode.Save(ServerLastUpCode);

                if (IsPartOfTable)
                {
                    PartTable.SaveToParent();
                }
#if DEBUG_UpdateAble
                await Debuger();
#endif
            }