Ejemplo n.º 1
0
 public SharedDictionaryWriter(SharedDictionaryImpl dic, IStructuredWriter writer)
 {
     _dic            = dic;
     _writer         = writer;
     _alreadyWritten = new HashSet <object>();
     _writer.Current.ObjectWriteExData += new EventHandler <ObjectWriteExDataEventArgs>(_writer_ObjectWriteExData);
 }
Ejemplo n.º 2
0
		public SharedDictionaryWriter( SharedDictionaryImpl dic, IStructuredWriter writer )
		{
			_dic = dic;
            _writer = writer;
            _alreadyWritten = new HashSet<object>();
            _writer.Current.ObjectWriteExData += new EventHandler<ObjectWriteExDataEventArgs>( _writer_ObjectWriteExData );
        }
Ejemplo n.º 3
0
        public void Setup()
        {
            _lastConfigChangedEventArgs = null;
            _lastSender = null;

            _dic = new SharedDictionaryImpl( null );
            _dic.Changed += ( o, e ) => { _lastSender = o; _lastConfigChangedEventArgs = e; };
        }
Ejemplo n.º 4
0
 public void Dispose()
 {
     if (_writer != null)
     {
         _writer.ServiceContainer.Remove(typeof(ISharedDictionaryWriter));
         _writer = null;
         _dic    = null;
     }
 }
Ejemplo n.º 5
0
 internal SharedDictionaryReader(SharedDictionaryImpl dic, IStructuredReader reader, MergeMode mergeMode)
 {
     _dic                             = dic;
     _mergeMode                       = mergeMode;
     _reader                          = reader;
     _errorCollector                  = new List <ReadElementObjectInfo>();
     _currentPluginId                 = SimpleUniqueId.InvalidId.UniqueId;
     _currentPluginVersion            = null;
     reader.Current.ObjectReadExData += new EventHandler <ObjectReadExDataEventArgs>(Current_ObjectReadExData);
 }
Ejemplo n.º 6
0
 internal bool TryRestore( SharedDictionaryImpl dic, MergeMode mergeMode )
 {
     INamedVersionedUniqueId uid = dic.FindPlugin( PluginId );
     if( uid != null )
     {
         Restore( dic, mergeMode );
         return true;
     }
     return false;
 }
Ejemplo n.º 7
0
 internal void Restore(SharedDictionaryImpl dic, MergeMode mergeMode)
 {
     using (IStructuredReader sr = Bookmark.Restore(dic.ServiceProvider))
     {
         using (var r = dic.RegisterReader(sr, mergeMode))
         {
             r.ReadPluginsData(Obj);
         }
     }
 }
Ejemplo n.º 8
0
 internal void Restore( SharedDictionaryImpl dic, MergeMode mergeMode )
 {
     using( IStructuredReader sr = Bookmark.Restore( dic.ServiceProvider ) )
     {
         using( var r = dic.RegisterReader( sr, mergeMode ) )
         {
             r.ReadPluginsData( Obj );
         }
     }
 }
Ejemplo n.º 9
0
        internal bool TryRestore(SharedDictionaryImpl dic, MergeMode mergeMode)
        {
            INamedVersionedUniqueId uid = dic.FindPlugin(PluginId);

            if (uid != null)
            {
                Restore(dic, mergeMode);
                return(true);
            }
            return(false);
        }
Ejemplo n.º 10
0
        public void Import(ISharedDictionary source, MergeMode mergeMode)
        {
            SharedDictionaryImpl dicSource = (SharedDictionaryImpl)source;

            if (dicSource != null && dicSource != this && dicSource._values.Count > 0)
            {
                ImportFragments(dicSource._fragments, mergeMode);
                if (mergeMode == MergeMode.None)
                {
                    ClearAll();
                }
                foreach (SharedDictionaryEntry s in dicSource._values.Values)
                {
                    ImportValue(new SharedDictionaryEntry(s.Obj, s.PluginId, s.Key, s.Value), mergeMode);
                }
            }
        }
Ejemplo n.º 11
0
        public void None()
        {
            SharedDictionaryImpl source = new SharedDictionaryImpl( SharedDicTestContext.ServiceProvider );
            source[this, SharedDicTestContext.Plugins[0], "key1"] = "value1";
            source[this, SharedDicTestContext.Plugins[0], "key2"] = "value2";
            source[this, SharedDicTestContext.Plugins[0], "key3"] = "value3";

            object key = new object();
            SharedDictionaryImpl target = new SharedDictionaryImpl( SharedDicTestContext.ServiceProvider );
            target[key, SharedDicTestContext.Plugins[1], "previousKey"] = "previousValue";

            target.Import( source, MergeMode.None );

            Assert.IsNull( target[key, SharedDicTestContext.Plugins[1], "previousKey"] );
            Assert.That( (string)target[this, SharedDicTestContext.Plugins[0], "key1"] == "value1" );
            Assert.That( (string)target[this, SharedDicTestContext.Plugins[0], "key2"] == "value2" );
            Assert.That( (string)target[this, SharedDicTestContext.Plugins[0], "key3"] == "value3" );
        }
Ejemplo n.º 12
0
        public void ReplaceExisting()
        {
            object key = new object();

            SharedDictionaryImpl source = new SharedDictionaryImpl( null );
            source[this, SharedDicTestContext.Plugins[0], "key1"] = "value1";
            source[this, SharedDicTestContext.Plugins[0], "key2"] = "value2";
            source[this, SharedDicTestContext.Plugins[0], "key3"] = "value3";
            source[key, SharedDicTestContext.Plugins[1], "previousKey"] = "value";

            SharedDictionaryImpl target = new SharedDictionaryImpl( null );
            target[key, SharedDicTestContext.Plugins[1], "previousKey"] = "previousValue";

            target.Import( source, MergeMode.ReplaceExisting );

            Assert.That( (string)target[key, SharedDicTestContext.Plugins[1], "previousKey"] == "value" );
            Assert.That( (string)target[this, SharedDicTestContext.Plugins[0], "key1"] == "value1" );
            Assert.That( (string)target[this, SharedDicTestContext.Plugins[0], "key2"] == "value2" );
            Assert.That( (string)target[this, SharedDicTestContext.Plugins[0], "key3"] == "value3" );
        }
Ejemplo n.º 13
0
        public void ReplaceExistingTryMerge()
        {
            object key = new object();

            SharedDictionaryImpl source = new SharedDictionaryImpl( SharedDicTestContext.ServiceProvider );
            source[this, SharedDicTestContext.Plugins[0], "key1"] = "value1";
            source[this, SharedDicTestContext.Plugins[0], "key2"] = "value2";
            source[this, SharedDicTestContext.Plugins[0], "key3"] = "value3";
            source[this, SharedDicTestContext.Plugins[0], "key4"] = "newValue";

            MergeableObject mergeableObj = new MergeableObject() { Property = 10 };
            source[key, SharedDicTestContext.Plugins[1], "mergeableObject"] = mergeableObj;

            SharedDictionaryImpl target = new SharedDictionaryImpl( null );
            MergeableObject newMergeableObj = new MergeableObject() { Property = 20 };
            target[key, SharedDicTestContext.Plugins[1], "mergeableObject"] = newMergeableObj;
            target[this, SharedDicTestContext.Plugins[0], "key4"] = "value4";

            target.Import( source, MergeMode.ReplaceExistingTryMerge );

            Assert.That( ((MergeableObject)target[key, SharedDicTestContext.Plugins[1], "mergeableObject"]).GetHashCode() == newMergeableObj.GetHashCode() );
            Assert.That( ((MergeableObject)target[key, SharedDicTestContext.Plugins[1], "mergeableObject"]).Property == 30 );
            Assert.That( (string)target[this, SharedDicTestContext.Plugins[0], "key1"] == "value1" );
            Assert.That( (string)target[this, SharedDicTestContext.Plugins[0], "key2"] == "value2" );
            Assert.That( (string)target[this, SharedDicTestContext.Plugins[0], "key3"] == "value3" );
            Assert.That( (string)target[this, SharedDicTestContext.Plugins[0], "key4"] == "newValue" );
        }
Ejemplo n.º 14
0
        public void MergeFragments_PreserveExisting()
        {
            INamedVersionedUniqueId uid1 = SharedDicTestContext.Plugins[0];
            INamedVersionedUniqueId uid2 = SharedDicTestContext.Plugins[1];
            INamedVersionedUniqueId uid3 = SharedDicTestContext.Plugins[2];

            SharedDictionaryImpl dic = new SharedDictionaryImpl( SharedDicTestContext.ServiceProvider );
            dic[this, uid1, "key1"] = "value1";
            dic[this, uid1, "key2"] = "value2";
            dic[this, uid2, "key1"] = "value1";
            dic[this, uid2, "key2"] = "value2";
            dic[this, uid3, "key1"] = "value1";
            dic[this, uid3, "key2"] = "value2";

            string path = TestBase.GetTestFilePath( "SharedDic", "MergeFragments" );
            SharedDicTestContext.Write( "Test", path, dic, this );

            IList<ReadElementObjectInfo> errors;
            SharedDictionaryImpl dicFullFrag = (SharedDictionaryImpl)SharedDicTestContext.Read( "Test", path, this, out errors );
            Assert.That( errors.Count == 0 );
            Assert.That( dicFullFrag.GetSkippedFragments( this ).Count == 3 );

            SharedDictionaryImpl dicFrag = (SharedDictionaryImpl)SharedDicTestContext.Read( "Test", path, this, d => { d.Ensure( uid1 ); d.Ensure( uid2 ); }, out errors );
            Assert.That( errors.Count == 0 );
            Assert.That( dicFrag.GetSkippedFragments( this ).Count == 1 );

            int hashCode = dicFrag.GetSkippedFragments( this )[0].GetHashCode();

            dicFrag.ImportFragments( dicFullFrag.Fragments, MergeMode.PreserveExisting );

            Assert.That( dicFrag.GetSkippedFragments( this ) != null );
            Assert.That( dicFrag.GetSkippedFragments( this ).Count == 1 );
            Assert.That( dicFrag.GetSkippedFragments( this )[0].GetHashCode() == hashCode );
        }
Ejemplo n.º 15
0
        public void SyncDestroyObject()
        {
            INamedVersionedUniqueId uid1 = SharedDicTestContext.Plugins[0];
            INamedVersionedUniqueId uid2 = SharedDicTestContext.Plugins[1];
            INamedVersionedUniqueId uid3 = SharedDicTestContext.Plugins[2];

            string path = TestBase.GetTestFilePath( "SharedDic", "MergeFragments" );
            SharedDictionaryImpl dicFullFrag;

            // Create fragments file.
            {
                SharedDictionaryImpl dic = new SharedDictionaryImpl( SharedDicTestContext.ServiceProvider );
                dic[this, uid1, "key1"] = "value1";
                dic[this, uid1, "key2"] = "value2";
                dic[this, uid2, "key1"] = "value1";
                dic[this, uid2, "key2"] = "value2";
                dic[this, uid3, "key1"] = "value1";
                dic[this, uid3, "key2"] = "value2";

                SharedDicTestContext.Write( "Test", path, dic, this );
            }
            {
                IList<ReadElementObjectInfo> errors;
                dicFullFrag = (SharedDictionaryImpl)SharedDicTestContext.Read( "Test", path, this, out errors );
                Assert.That( errors.Count == 0 );
                Assert.That( dicFullFrag.GetSkippedFragments( this ).Count == 3 );

                dicFullFrag.Destroy( this );
                Assert.That( dicFullFrag.GetSkippedFragments( this ) == null, "Destroying the object destroyed the fragments." );
            }

            {
                IList<ReadElementObjectInfo> errors;
                dicFullFrag = (SharedDictionaryImpl)SharedDicTestContext.Read( "Test", path, this, out errors );
                Assert.That( errors.Count == 0 );
                Assert.That( dicFullFrag.GetSkippedFragments( this ).Count == 3 );

                // If we Ensure() the id, then the fragment will be restored.
                // We test here the ClearFragments internal.
                dicFullFrag.ClearFragments( uid1 );
                Assert.That( dicFullFrag.GetSkippedFragments( this ).Count == 2, "Destroying the fragment explicitely." );

                dicFullFrag.Destroy( uid2 );
                Assert.That( dicFullFrag.GetSkippedFragments( this ).Count == 1, "Destroying the fragment explicitely." );

                dicFullFrag.Destroy( uid3 );
                Assert.That( dicFullFrag.GetSkippedFragments( this ) == null, "Destroying the fragment explicitely." );
            }

            {
                IList<ReadElementObjectInfo> errors;
                dicFullFrag = (SharedDictionaryImpl)SharedDicTestContext.Read( "Test", path, this, out errors );
                Assert.That( errors.Count == 0 );
                Assert.That( dicFullFrag.GetSkippedFragments( this ).Count == 3 );

                // This destroy an unknown plugin: 
                // we must destroy the fragments even if the id is not known.
                dicFullFrag.Destroy( uid2 );
                Assert.That( dicFullFrag.GetSkippedFragments( this ).Count == 2, "Destroying the fragment via an unknown Id." );

                dicFullFrag.Destroy( uid3 );
                Assert.That( dicFullFrag.GetSkippedFragments( this ).Count == 1, "Destroying the fragment via an unknown Id." );

                // No op.
                dicFullFrag.Destroy( uid3 );

                dicFullFrag.Destroy( uid1 );
                Assert.That( dicFullFrag.GetSkippedFragments( this ) == null );
            }

            {
                IList<ReadElementObjectInfo> errors;
                dicFullFrag = (SharedDictionaryImpl)SharedDicTestContext.Read( "Test", path, this, out errors );
                Assert.That( errors.Count == 0 );
                Assert.That( dicFullFrag.GetSkippedFragments( this ).Count == 3 );

                dicFullFrag.ClearAll();

                Assert.That( dicFullFrag.GetSkippedFragments( this ) == null );
            }
        }
Ejemplo n.º 16
0
        public void MergeFragments_ErrorDuplicate()
        {
            INamedVersionedUniqueId uid1 = SharedDicTestContext.Plugins[0];
            INamedVersionedUniqueId uid2 = SharedDicTestContext.Plugins[1];
            INamedVersionedUniqueId uid3 = SharedDicTestContext.Plugins[2];

            SharedDictionaryImpl dic = new SharedDictionaryImpl( SharedDicTestContext.ServiceProvider );
            dic[this, uid1, "key1"] = "value1";
            dic[this, uid1, "key2"] = "value2";
            dic[this, uid2, "key1"] = "value1";
            dic[this, uid2, "key2"] = "value2";
            dic[this, uid3, "key1"] = "value1";
            dic[this, uid3, "key2"] = "value2";
            Assert.That( dic.Contains( uid1 ) && dic.Contains( uid2 ) && dic.Contains( uid3 ) );

            string path = TestBase.GetTestFilePath( "SharedDic", "ErrorDuplicate" );
            SharedDicTestContext.Write( "Test", path, dic, this );

            IList<ReadElementObjectInfo> errors;
            SharedDictionaryImpl dicFullFrag = (SharedDictionaryImpl)SharedDicTestContext.Read( "Test", path, this, out errors );
            Assert.That( errors.Count == 0 );

            SharedDictionaryImpl dicFrag = (SharedDictionaryImpl)SharedDicTestContext.Read( "Test", path, this, d => { d.Ensure( uid1 ); d.Ensure( uid2 ); }, out errors );
            Assert.That( errors.Count == 0 );

            Assert.That( dic.GetSkippedFragments( this ) == null );
            Assert.That( dicFullFrag.GetSkippedFragments( this ).Count == 3 );
            Assert.That( dicFrag.GetSkippedFragments( this ).Count == 1 );

            Assert.Throws<CKException>( () => dicFrag.ImportFragments( dicFullFrag.Fragments, MergeMode.ErrorOnDuplicate ) );

            Assert.That( dicFrag.GetSkippedFragments( this ).Count == 1 );
        }
Ejemplo n.º 17
0
 public void Dispose()
 {
     if( _writer != null )
     {
         _writer.ServiceContainer.Remove( typeof( ISharedDictionaryWriter ) );
         _writer = null;
         _dic = null;
     }
 }
Ejemplo n.º 18
0
        void TryReloadNestedSkippedFragments( ITestObject complexObject )
        {
            object rootObject = new object();

            SimpleNamedVersionedUniqueId p1 = new SimpleNamedVersionedUniqueId( Guid.NewGuid(), new Version( 1, 0, 0 ), "plugin1" );
            SimpleNamedVersionedUniqueId p2 = new SimpleNamedVersionedUniqueId( Guid.NewGuid(), new Version( 1, 0, 0 ), "plugin2" );

            string path = TestBase.GetTestFilePath( "SharedDic", "NestedSkippedFragments" );

            // Write !
            {
                SharedDictionaryImpl dic = new SharedDictionaryImpl( SharedDicTestContext.ServiceProvider );
                dic[rootObject, p1, "complexObject"] = complexObject;
                dic[complexObject, p2, "subKey"] = "subValue";

                SharedDicTestContext.Write( "Test", path, dic, rootObject );

                TestBase.DumpFileToConsole( path );
            }
            // Read nothing then p1 and p2
            {
                IList<ReadElementObjectInfo> errors = new List<ReadElementObjectInfo>();
                SharedDictionaryImpl dic = (SharedDictionaryImpl)SharedDicTestContext.Read( "Test", path, rootObject, out errors );

                dic.Ensure( p1 );
                dic.Ensure( p2 );

                CheckAllDataLoaded( rootObject, complexObject, p1, p2, dic );
            }
            // Read nothing then p2 and p1
            {
                IList<ReadElementObjectInfo> errors = new List<ReadElementObjectInfo>();
                SharedDictionaryImpl dic = (SharedDictionaryImpl)SharedDicTestContext.Read( "Test", path, rootObject, out errors );

                dic.Ensure( p2 );
                dic.Ensure( p1 );

                CheckAllDataLoaded( rootObject, complexObject, p1, p2, dic );
            }
            // Ensure p1 then read p2
            {
                IList<ReadElementObjectInfo> errors = new List<ReadElementObjectInfo>();
                SharedDictionaryImpl dic = (SharedDictionaryImpl)SharedDicTestContext.Read( "Test", path, rootObject, d => d.Ensure( p1 ), out errors );

                dic.Ensure( p2 );

                CheckAllDataLoaded( rootObject, complexObject, p1, p2, dic );
            }
            // Ensure p2 then read p1
            {
                IList<ReadElementObjectInfo> errors = new List<ReadElementObjectInfo>();
                SharedDictionaryImpl dic = (SharedDictionaryImpl)SharedDicTestContext.Read( "Test", path, rootObject, d => d.Ensure( p2 ), out errors );

                dic.Ensure( p1 );

                CheckAllDataLoaded( rootObject, complexObject, p1, p2, dic );
            }
            // Ensure p1 and p2 then read nothing
            {
                IList<ReadElementObjectInfo> errors = new List<ReadElementObjectInfo>();
                SharedDictionaryImpl dic = (SharedDictionaryImpl)SharedDicTestContext.Read( "Test", path, rootObject, d => { d.Ensure( p1 ); d.Ensure( p2 ); }, out errors );

                CheckAllDataLoaded( rootObject, complexObject, p1, p2, dic );
            }
            // Ensure p2 and p1 then read nothing
            {
                IList<ReadElementObjectInfo> errors = new List<ReadElementObjectInfo>();
                SharedDictionaryImpl dic = (SharedDictionaryImpl)SharedDicTestContext.Read( "Test", path, rootObject, d => { d.Ensure( p2 ); d.Ensure( p1 ); }, out errors );

                CheckAllDataLoaded( rootObject, complexObject, p1, p2, dic );
            }
        }
Ejemplo n.º 19
0
 SharedDictionaryImpl CreateDummySharedDic( object dataHolder, params INamedVersionedUniqueId[] identifiers )
 {
     SharedDictionaryImpl dic = new SharedDictionaryImpl( SharedDicTestContext.ServiceProvider );
     foreach( INamedVersionedUniqueId id in identifiers )
     {
         dic[dataHolder, id, "key1"] = "value1";
         dic[dataHolder, id, "key2"] = "value2";
     }
     return dic;
 }
Ejemplo n.º 20
0
 private static void CheckAllDataLoaded( object rootObject, ITestObject complexObject, SimpleNamedVersionedUniqueId p1, SimpleNamedVersionedUniqueId p2, SharedDictionaryImpl dic )
 {
     ITestObject obj = (ITestObject)dic[rootObject, p1, "complexObject"];
     Assert.That( obj != null );
     Assert.That( obj.Value == complexObject.Value );
     string value = (string)dic[obj, p2, "subKey"];
     Assert.That( value == "subValue" );
 }
Ejemplo n.º 21
0
 internal FinalDictionary(SharedDictionaryImpl dic, object obj, INamedVersionedUniqueId p)
 {
     _dic      = dic;
     _obj      = obj;
     _pluginId = p;
 }
Ejemplo n.º 22
0
        public void TryImportBadSkippedFragments()
        {
            INamedVersionedUniqueId uid1 = SharedDicTestContext.Plugins[0];
            INamedVersionedUniqueId uid2 = SharedDicTestContext.Plugins[1];

            string path = TestBase.GetTestFilePath( "SharedDic", "TryImportBadSkippedFragments" );
            #region Creates actual fragments

            object key = new object();

            // Creates a dummy dictionnary and writes it.
            SharedDictionaryImpl dic = CreateDummySharedDic( key, uid1, uid2 );
            SharedDicTestContext.Write( "Test", path, dic, key );

            // Creates a second dictionnary to load previous data (with skippedFragments).
            IList<ReadElementObjectInfo> errors;
            SharedDictionaryImpl dicFrag = (SharedDictionaryImpl)SharedDicTestContext.Read( "Test", path, key, d => d.Ensure( uid1 ), out errors );
            Assert.IsTrue( new FileInfo( path ).Length > 0, "File must exist and be not empty." );

            Assert.That( errors.Count, Is.EqualTo( 0 ) );
            Assert.That( dicFrag.GetSkippedFragments( key ) != null );
            Assert.That( dicFrag[key, uid2, "key1"], Is.Null );
            Assert.That( dicFrag[key, uid2, "key2"], Is.Null );

            #endregion

            SharedDictionaryImpl dic2 = new SharedDictionaryImpl( SharedDicTestContext.ServiceProvider );
            dic2.Ensure( uid1 );

            dic2[this, uid1, "key1"] = "value1";
            dic2[this, uid1, "key2"] = "value2";
            Assert.That( dic2[this, uid2, "key1"], Is.Null );
            Assert.That( dic2[this, uid2, "key2"], Is.Null );

            dic2.ImportFragments( dicFrag.Fragments, MergeMode.None );

            Assert.That( dic2.GetSkippedFragments( this ) == null );
        }