private int ReplicateBidirectional(ITestableReplicationProviderInside providerA,
                                           ITestableReplicationProviderInside providerB, Type clazz)
        {
            int replicatedObjects = 0;
            IReplicationSession replicationSession = Replication.Begin(providerA, providerB,
                                                                       null, _fixtures.reflector);
            IObjectSet changedInA = clazz == null?providerA.ObjectsChangedSinceLastReplication
                                        () : providerA.ObjectsChangedSinceLastReplication(clazz);

            foreach (object obj in changedInA)
            {
                replicatedObjects++;
                replicationSession.Replicate(obj);
            }
            IObjectSet changedInB = clazz == null?providerB.ObjectsChangedSinceLastReplication
                                        () : providerB.ObjectsChangedSinceLastReplication(clazz);

            foreach (object obj in changedInB)
            {
                replicatedObjects++;
                replicationSession.Replicate(obj);
            }
            replicationSession.Commit();
            return(replicatedObjects);
        }
Пример #2
0
        private static void ReplicateBidirectional(IReplicationSession replication)
        {
            IObjectSet changesOnDesktop = replication.ProviderA().ObjectsChangedSinceLastReplication();
            IObjectSet changesOnMobile  = replication.ProviderB().ObjectsChangedSinceLastReplication();

            foreach (object changedObjectOnDesktop in changesOnDesktop)
            {
                replication.Replicate(changedObjectOnDesktop);
            }

            foreach (object changedObjectOnMobile in changesOnMobile)
            {
                replication.Replicate(changedObjectOnMobile);
            }
        }
Пример #3
0
        private static void OneWayReplicationExample()
        {
            DeleteDatabases();
            StoreObjectsIn(DesktopDatabaseName);

            //#example: Prepare unidirectional replication
            IObjectContainer desktopDatabase = OpenDatabase(DesktopDatabaseName);
            IObjectContainer mobileDatabase  = OpenDatabase(MobileDatabaseName);

            IReplicationProvider dektopReplicationProvider
                = new Db4oEmbeddedReplicationProvider(desktopDatabase);
            IReplicationProvider mobileReplicationProvider
                = new Db4oEmbeddedReplicationProvider(mobileDatabase);

            IReplicationSession replicationSession
                = Replication.Begin(dektopReplicationProvider, mobileReplicationProvider);

            // set the replication-direction from the desktop database to the mobile database.
            replicationSession.SetDirection(replicationSession.ProviderA(), replicationSession.ProviderB());
            //#end example

            //#example: One direction replication
            IObjectSet changes = replicationSession.ProviderA().ObjectsChangedSinceLastReplication();

            foreach (object changedObject in changes)
            {
                replicationSession.Replicate(changedObject);
            }
            replicationSession.Commit();
            //#end example

            PrintCars(mobileDatabase);

            CloseDBs(desktopDatabase, mobileDatabase);
        }
Пример #4
0
        private static void SelectiveReplicationByClass()
        {
            DeleteDatabases();
            StoreObjectsIn(DesktopDatabaseName);

            IObjectContainer desktopDatabase = OpenDatabase(DesktopDatabaseName);
            IObjectContainer mobileDatabase  = OpenDatabase(MobileDatabaseName);

            IReplicationProvider dektopReplicationProvider
                = new Db4oEmbeddedReplicationProvider(desktopDatabase);
            IReplicationProvider mobileReplicationProvider
                = new Db4oEmbeddedReplicationProvider(mobileDatabase);

            IReplicationSession replicationSession
                = Replication.Begin(dektopReplicationProvider, mobileReplicationProvider);

            // #example: Selective replication by class
            IObjectSet changesOnDesktop =
                replicationSession.ProviderA().ObjectsChangedSinceLastReplication(typeof(Pilot));

            foreach (object changedObjectOnDesktop in changesOnDesktop)
            {
                replicationSession.Replicate(changedObjectOnDesktop);
            }

            replicationSession.Commit();
            // #end example

            // the car's aren't replicated, only the pilots
            PrintCars(mobileDatabase);
            PrintPilots(mobileDatabase);

            CloseDBs(desktopDatabase, mobileDatabase);
        }
Пример #5
0
        public virtual void Test()
        {
            ITestableReplicationProviderInside providerA = A().Provider();
            ITestableReplicationProviderInside providerB = B().Provider();

            providerA.StoreNew(new Pilot());
            providerA.Commit();
            IReplicationSession replication = Replication.Begin(providerA, providerB, null, _fixtures
                                                                .reflector);
            TimeStamps initialTimeStampsB = AssertTimeStampsForFirstReplication(providerB);
            IObjectSet modifiedObjects    = providerA.ObjectsChangedSinceLastReplication();

            while (modifiedObjects.HasNext())
            {
                replication.Replicate(modifiedObjects.Next());
            }
            replication.Commit();
            Pilot replicatedPilot = (Pilot)providerB.GetStoredObjects(typeof(Pilot)).Next();
            long  version         = providerB.ObjectVersion(replicatedPilot);

            Assert.AreEqual(initialTimeStampsB.Commit(), version);
            replication = Replication.Begin(providerA, providerB, null, _fixtures.reflector);
            TimeStamps timestampsBAfterReplication = AssertTimeStampsForSecondReplication(initialTimeStampsB
                                                                                          , providerB);

            replication.Commit();
            Pilot pilotStoredAfterReplication = new Pilot();

            providerB.StoreNew(pilotStoredAfterReplication);
            providerB.Commit();
            providerB.WaitForPreviousCommits();
            version = providerB.ObjectVersion(pilotStoredAfterReplication);
            Assert.IsGreater(timestampsBAfterReplication.Commit(), version);
        }
        protected virtual void AssertReplicates(object holder)
        {
            IEmbeddedObjectContainer sourceDb = OpenContainer("source");

            sourceDb.Store(holder);
            sourceDb.Commit();
            IEmbeddedObjectContainer targetDb = OpenContainer("target");

            try
            {
                Db4oEmbeddedReplicationProvider providerA = new Db4oEmbeddedReplicationProvider(sourceDb
                                                                                                );
                Db4oEmbeddedReplicationProvider providerB = new Db4oEmbeddedReplicationProvider(targetDb
                                                                                                );
                IReplicationSession replication = Replication.Begin(providerA, providerB);
                IObjectSet          changed     = replication.ProviderA().ObjectsChangedSinceLastReplication();
                while (changed.HasNext())
                {
                    object o = changed.Next();
                    if (holder.GetType() == o.GetType())
                    {
                        replication.Replicate(o);
                        break;
                    }
                }
                replication.Commit();
            }
            finally
            {
                sourceDb.Close();
                targetDb.Close();
            }
        }
Пример #7
0
        private static void SelectiveReplicationWithQuery()
        {
            DeleteDatabases();
            StoreObjectsIn(DesktopDatabaseName);

            IObjectContainer desktopDatabase = OpenDatabase(DesktopDatabaseName);
            IObjectContainer mobileDatabase  = OpenDatabase(MobileDatabaseName);

            IReplicationProvider dektopReplicationProvider
                = new Db4oEmbeddedReplicationProvider(desktopDatabase);
            IReplicationProvider mobileReplicationProvider
                = new Db4oEmbeddedReplicationProvider(mobileDatabase);

            IReplicationSession replicationSession
                = Replication.Begin(dektopReplicationProvider, mobileReplicationProvider);

            // #example: Selective replication with a query
            IList <Car> changesOnDesktop =
                desktopDatabase.Query(delegate(Car car) { return(car.Name.StartsWith("M")); });

            foreach (Car changedObjectOnDesktop  in changesOnDesktop)
            {
                replicationSession.Replicate(changedObjectOnDesktop);
            }

            replicationSession.Commit();
            // #end example

            // now only the cars which names start with "M" are replicated
            PrintCars(mobileDatabase);

            CloseDBs(desktopDatabase, mobileDatabase);
        }
Пример #8
0
        private static void ReplicateChanges(IReplicationSession replication, IReplicationProvider provider)
        {
            IObjectSet changes = provider.ObjectsChangedSinceLastReplication();

            foreach (object changedObject in changes)
            {
                replication.Replicate(changedObject);
            }
        }
Пример #9
0
        private static void BiDirectionalReplicationExample()
        {
            DeleteDatabases();
            StoreObjectsIn(DesktopDatabaseName);
            StoreObjectsIn(MobileDatabaseName);

            // #example: Prepare bidirectional replication
            IObjectContainer desktopDatabase = OpenDatabase(DesktopDatabaseName);
            IObjectContainer mobileDatabase  = OpenDatabase(MobileDatabaseName);

            IReplicationProvider dektopReplicationProvider
                = new Db4oEmbeddedReplicationProvider(desktopDatabase);
            IReplicationProvider mobileReplicationProvider
                = new Db4oEmbeddedReplicationProvider(mobileDatabase);

            IReplicationSession replicationSession
                = Replication.Begin(dektopReplicationProvider, mobileReplicationProvider);
            // #end example

            //#example: Bidirectional replication
            // First get the changes of the two replication-partners
            IObjectSet changesOnDesktop = replicationSession.ProviderA().ObjectsChangedSinceLastReplication();
            IObjectSet changesOnMobile  = replicationSession.ProviderB().ObjectsChangedSinceLastReplication();

            // then iterate over both change-sets and replicate it
            foreach (object changedObjectOnDesktop in changesOnDesktop)
            {
                replicationSession.Replicate(changedObjectOnDesktop);
            }

            foreach (object changedObjectOnMobile in changesOnMobile)
            {
                replicationSession.Replicate(changedObjectOnMobile);
            }

            replicationSession.Commit();
            //#end example

            PrintCars(mobileDatabase);
            PrintCars(desktopDatabase);

            CloseDBs(desktopDatabase, mobileDatabase);
        }
Пример #10
0
 protected virtual void ReplicateAll(IReplicationSession replication, IEnumerator
                                     allObjects)
 {
     while (allObjects.MoveNext())
     {
         object changed = allObjects.Current;
         //			System.out.println("Replicating = " + changed);
         replication.Replicate(changed);
     }
     replication.Commit();
 }
Пример #11
0
        protected override void ReplicateClass(ITestableReplicationProviderInside providerA
                                               , ITestableReplicationProviderInside providerB, Type clazz)
        {
            IReplicationSession replication = Replication.Begin(providerA, providerB, _fixtures
                                                                .reflector);
            IEnumerator allObjects = providerA.ObjectsChangedSinceLastReplication(clazz).GetEnumerator
                                         ();

            while (allObjects.MoveNext())
            {
                object obj = allObjects.Current;
                //System.out.println("obj = " + obj);
                replication.Replicate(obj);
            }
            replication.Commit();
        }
Пример #12
0
        private int ReplicateAll(ITestableReplicationProviderInside peerA, ITestableReplicationProviderInside
                                 peerB, bool modifiedOnly)
        {
            IReplicationSession replication = Replication.Begin(peerA, peerB, null, _fixtures
                                                                .reflector);
            IEnumerator it = modifiedOnly ? peerA.ObjectsChangedSinceLastReplication(typeof(R0
                                                                                            )).GetEnumerator() : peerA.GetStoredObjects(typeof(R0)).GetEnumerator();
            int replicated = 0;

            while (it.MoveNext())
            {
                R0 r0 = (R0)it.Current;
                replication.Replicate(r0);
                replicated++;
            }
            replication.Commit();
            EnsureCount(peerA, Linkers);
            EnsureCount(peerB, Linkers);
            return(replicated);
        }
Пример #13
0
        private static void MigrationOnTheFly()
        {
            DeleteDatabases();

            IObjectContainer desktopDatabaseWithoutUUID = Db4oEmbedded.OpenFile(DesktopDatabaseName);

            desktopDatabaseWithoutUUID.Store(new Car(new Pilot("Max"), "Max's Car"));
            desktopDatabaseWithoutUUID.Store(new Car(new Pilot("Joe"), "Joe's Car"));
            desktopDatabaseWithoutUUID.Commit();
            desktopDatabaseWithoutUUID.Close();

            IObjectContainer desktopDatabase = OpenDatabase(DesktopDatabaseName);
            IObjectContainer mobileDatabase  = OpenDatabase(MobileDatabaseName);

            IReplicationProvider desktopRelicationPartner
                = new Db4oEmbeddedReplicationProvider(desktopDatabase);
            IReplicationProvider mobileRelicationPartner
                = new Db4oEmbeddedReplicationProvider(mobileDatabase);

            // #example: Migrate on the fly
            IReplicationSession replicationSession = Replication.Begin(desktopRelicationPartner, mobileRelicationPartner);
            IList <Car>         initialReplication = desktopDatabase.Query <Car>();

            foreach (Car changedObjectOnDesktop in initialReplication)
            {
                IObjectInfo infoAboutObject = desktopDatabase.Ext().GetObjectInfo(changedObjectOnDesktop);
                if (null == infoAboutObject.GetUUID())
                {
                    desktopDatabase.Ext().Store(changedObjectOnDesktop, 2);
                }
                replicationSession.Replicate(changedObjectOnDesktop);
            }
            replicationSession.Commit();
            // #end example

            PrintCars(mobileDatabase);

            CloseDBs(desktopDatabase, mobileDatabase);
        }
		private void ReplicateQueryingFrom(IReplicationSession replication, IReplicationProvider
			 origin, IReplicationProvider other)
		{
			ReplicationConflictException exception = null;
			IEnumerator changes = origin.ObjectsChangedSinceLastReplication().GetEnumerator();
			while (changes.MoveNext())
			{
				object changed = changes.Current;
				try
				{
					replication.Replicate(changed);
				}
				catch (ReplicationConflictException e)
				{
					exception = e;
				}
			}
			if (exception != null)
			{
				throw exception;
			}
		}
Пример #15
0
        private static void SelectiveReplicationWithCondition()
        {
            DeleteDatabases();
            StoreObjectsIn(DesktopDatabaseName);

            IObjectContainer desktopDatabase = OpenDatabase(DesktopDatabaseName);
            IObjectContainer mobileDatabase  = OpenDatabase(MobileDatabaseName);

            IReplicationProvider dektopReplicationProvider
                = new Db4oEmbeddedReplicationProvider(desktopDatabase);
            IReplicationProvider mobileReplicationProvider
                = new Db4oEmbeddedReplicationProvider(mobileDatabase);

            IReplicationSession replicationSession
                = Replication.Begin(dektopReplicationProvider, mobileReplicationProvider);

            // #example: Selective replication with a condition
            IObjectSet changesOnDesktop = replicationSession.ProviderA().ObjectsChangedSinceLastReplication();

            foreach (object changedObjectOnDesktop in changesOnDesktop)
            {
                if (changedObjectOnDesktop is Car)
                {
                    if (((Car)changedObjectOnDesktop).Name.StartsWith("M"))
                    {
                        replicationSession.Replicate(changedObjectOnDesktop);
                    }
                }
            }

            replicationSession.Commit();
            // #end example

            // now only the cars which names start with "M" are replicated
            PrintCars(mobileDatabase);

            CloseDBs(desktopDatabase, mobileDatabase);
        }
        private void ReplicateQueryingFrom(IReplicationSession replication, IReplicationProvider
                                           origin, IReplicationProvider other)
        {
            ReplicationConflictException exception = null;
            IEnumerator changes = origin.ObjectsChangedSinceLastReplication().GetEnumerator();

            while (changes.MoveNext())
            {
                object changed = changes.Current;
                try
                {
                    replication.Replicate(changed);
                }
                catch (ReplicationConflictException e)
                {
                    exception = e;
                }
            }
            if (exception != null)
            {
                throw exception;
            }
        }
Пример #17
0
        private static void ReplicateBidirectional(IReplicationSession replication)
        {
            IObjectSet changesOnDesktop = replication.ProviderA().ObjectsChangedSinceLastReplication();
            IObjectSet changesOnMobile = replication.ProviderB().ObjectsChangedSinceLastReplication();

            foreach (object changedObjectOnDesktop in changesOnDesktop)
            {
                replication.Replicate(changedObjectOnDesktop);
            }

            foreach (object changedObjectOnMobile in changesOnMobile)
            {
                replication.Replicate(changedObjectOnMobile);
            }
        }
Пример #18
0
        private static void ConcurrencyLimitations()
        {
            DeleteDatabases();

            // #example: Lost replication
            IObjectServer    serverDatabase = OpenDatabaseServer(DesktopDatabaseName);
            IObjectContainer mobileDatabase = OpenDatabase(MobileDatabaseName);

            {
                IObjectContainer serverDbConnection =
                    Db4oClientServer.OpenClient(Host, Port, UserName, UserName);
                serverDbConnection.Store(new Pilot("Pilot 1"));
                serverDbConnection.Commit();

                // The replication starts here
                IObjectContainer connectionForReplication =
                    Db4oClientServer.OpenClient(Host, Port, UserName, UserName);

                IReplicationProvider clientReplication
                    = new Db4oEmbeddedReplicationProvider(connectionForReplication);
                IReplicationProvider mobileRelicationPartner
                    = new Db4oEmbeddedReplicationProvider(mobileDatabase);

                IReplicationSession replicationSession =
                    Replication.Begin(clientReplication, mobileRelicationPartner);
                IObjectSet changesOnDesktop =
                    replicationSession.ProviderA().ObjectsChangedSinceLastReplication();

                // during the replication other clients store data on the server
                serverDbConnection.Store(new Pilot("Pilot 2"));
                serverDbConnection.Commit();

                foreach (object changedObjectOnDesktop in changesOnDesktop)
                {
                    replicationSession.Replicate(changedObjectOnDesktop);
                }

                replicationSession.Commit();

                serverDbConnection.Store(new Pilot("Pilot 3"));
                serverDbConnection.Commit();
            }

            // Pilot 2 is not replicated
            PrintPilots(mobileDatabase);


            {
                IObjectContainer connectionForReplication =
                    Db4oClientServer.OpenClient(Host, Port, UserName, UserName);

                IReplicationProvider clientRelicationPartner
                    = new Db4oEmbeddedReplicationProvider(connectionForReplication);
                IReplicationProvider mobileRelicationPartner
                    = new Db4oEmbeddedReplicationProvider(mobileDatabase);

                IReplicationSession replicationSession =
                    Replication.Begin(clientRelicationPartner, mobileRelicationPartner);
                IObjectSet changesOnDesktop =
                    replicationSession.ProviderA().ObjectsChangedSinceLastReplication();
                foreach (object changedOnDesktop in changesOnDesktop)
                {
                    replicationSession.Replicate(changedOnDesktop);
                }
                replicationSession.Commit();
            }

            // Pilot 2 is still not replicated
            PrintPilots(mobileDatabase);
            // #end example

            serverDatabase.Close();
            mobileDatabase.Close();
        }
Пример #19
0
 private static void ReplicateChanges(IReplicationSession replication, IReplicationProvider provider)
 {
     IObjectSet changes = provider.ObjectsChangedSinceLastReplication();
     foreach (object changedObject in changes)
     {
         replication.Replicate(changedObject);
     }
 }
Пример #20
0
		private void ReplicateAll(IReplicationSession replication, IEnumerator allObjects
			)
		{
			while (allObjects.MoveNext())
			{
				object changed = allObjects.Current;
				//System.out.println("changed = " + changed);
				replication.Replicate(changed);
			}
			replication.Commit();
		}