Example #1
0
        private static void DeletionsReplication()
        {
            DeleteDatabases();
            StoreObjectsIn(DesktopDatabaseName);
            IObjectContainer desktopDatabase = OpenDatabase(DesktopDatabaseName);
            IObjectContainer mobileDatabase  = OpenDatabase(MobileDatabaseName);

            Replicate(desktopDatabase, mobileDatabase);

            Car carToDelete = desktopDatabase.Query <Car>()[0];

            desktopDatabase.Delete(carToDelete);
            desktopDatabase.Commit();

            PrintCars(mobileDatabase);

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

            // #example: Replicate deletions
            IReplicationSession replicationSession
                = Replication.Begin(dektopReplicationProvider, mobileReplicationProvider);

            replicationSession.ReplicateDeletions(typeof(Car));
            replicationSession.Commit();
            // #end example

            PrintCars(mobileDatabase);

            CloseDBs(desktopDatabase, mobileDatabase);
        }
Example #2
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);
        }
Example #3
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);
        }
        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);
        }
Example #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);
        }
        private static void ReplicationConflictTakeLatestChange()
        {
            DeleteDatabases();

            IObjectContainer desktopDatabase = OpenDatabase(DesktopDatabaseName);

            desktopDatabase.Store(new Pilot("Max"));
            IObjectContainer mobileDatabase = OpenDatabase(MobileDatabaseName);

            ReplicateBidirectional(desktopDatabase, mobileDatabase);


            UpdateObject(desktopDatabase);
            UpdateObject(mobileDatabase);

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

            // #example: Take latest change
            IReplicationSession replicationSession =
                Replication.Begin(desktopRelicationPartner, mobileRelicationPartner,
                                  new TakeLatestModificationOnConflictListener());

            // #end example

            ReplicateBidirectional(replicationSession);
            replicationSession.Commit();

            CloseDBs(desktopDatabase, mobileDatabase);
        }
        private static void ReplicationConflicts()
        {
            DeleteDatabases();

            IObjectContainer desktopDatabase = OpenDatabase(DesktopDatabaseName);

            desktopDatabase.Store(new Pilot("Max"));
            IObjectContainer mobileDatabase = OpenDatabase(MobileDatabaseName);

            ReplicateBidirectional(desktopDatabase, mobileDatabase);


            UpdateObject(desktopDatabase);
            UpdateObject(mobileDatabase);

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

            // #example: Deal with conflicts
            IReplicationSession replicationSession =
                Replication.Begin(desktopRelicationPartner, mobileRelicationPartner,
                                  new SimpleConflictResolvingListener());

            // #end example

            ReplicateBidirectional(replicationSession);
            replicationSession.Commit();

            CloseDBs(desktopDatabase, mobileDatabase);
        }
        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();
            }
        }
Example #9
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);
        }
Example #10
0
        protected virtual void ReplicateAll(ITestableReplicationProviderInside from, ITestableReplicationProviderInside
                                            to, IReplicationEventListener listener)
        {
            IReplicationSession replication = Replication.Begin(from, to, listener, _fixtures
                                                                .reflector);

            ReplicateAll(replication, from.ObjectsChangedSinceLastReplication().GetEnumerator
                             ());
        }
Example #11
0
        protected virtual void ReplicateClass(ITestableReplicationProviderInside providerA
                                              , ITestableReplicationProviderInside providerB, Type clazz)
        {
            IReplicationSession replication = Replication.Begin(providerA, providerB, null, _fixtures
                                                                .reflector);
            IEnumerator allObjects = providerA.ObjectsChangedSinceLastReplication(clazz).GetEnumerator
                                         ();

            ReplicateAll(replication, allObjects);
        }
Example #12
0
        protected virtual void ReplicateAll(ITestableReplicationProviderInside providerFrom
                                            , ITestableReplicationProviderInside providerTo)
        {
            IReplicationSession replication = Replication.Begin(providerFrom, providerTo, _fixtures
                                                                .reflector);
            IObjectSet changedSet = providerFrom.ObjectsChangedSinceLastReplication();

            if (changedSet.Count == 0)
            {
                throw new Exception("Can't find any objects to replicate");
            }
            ReplicateAll(replication, changedSet.GetEnumerator());
        }
        private static void ReplicateBidirectional(IObjectContainer desktopDatabase, IObjectContainer mobileDatabase)
        {
            IReplicationProvider desktopRelicationPartner
                = new Db4oEmbeddedReplicationProvider(desktopDatabase);
            IReplicationProvider mobileRelicationPartner
                = new Db4oEmbeddedReplicationProvider(mobileDatabase);

            IReplicationSession replicationSession
                = Replication.Begin(desktopRelicationPartner, mobileRelicationPartner);

            ReplicateBidirectional(replicationSession);
            replicationSession.Commit();
        }
Example #14
0
        private static void Replicate(IObjectContainer desktopDatabase, IObjectContainer mobileDatabase)
        {
            IReplicationProvider dektopReplicationProvider
                = new Db4oEmbeddedReplicationProvider(desktopDatabase);
            IReplicationProvider mobileReplicationProvider
                = new Db4oEmbeddedReplicationProvider(mobileDatabase);

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

            ReplicateChanges(replicationSession, replicationSession.ProviderA());
            ReplicateChanges(replicationSession, replicationSession.ProviderB());
            replicationSession.Commit();
        }
Example #15
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();
        }
Example #16
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);
        }
Example #17
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);
        }
        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);
        }
Example #19
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 static void EventExample()
        {
            DeleteDatabases();
            StoreObjectsIn(DesktopDatabaseName);

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

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

            // #example: Register a listener for information about the replication process
            IReplicationSession replicationSession
                = Replication.Begin(desktopRelicationPartner, mobileRelicationPartner, new LogReplicationListener());

            // #end example
            ReplicateBidirectional(replicationSession);

            replicationSession.Commit();
            CloseDBs(desktopDatabase, mobileDatabase);
        }
        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();
        }