Exemplo n.º 1
0
        public async Task <FileVersionerResults> DoRestore(Models.Restore restore, LinkedList <CustomVersionedFile> files, bool newRestore)
        {
            Assert.IsNotNull(restore);
            Assert.AreEqual(TransferStatus.RUNNING, restore.Status);
            Assert.IsNotNull(files);

            Results.Reset();

            await ExecuteOnBackround(() =>
            {
                RestoreRepository daoRestore = new RestoreRepository();
                Restore = daoRestore.Get(restore.Id);

                RestorePlanFileRepository daoRestorePlanFile = new RestorePlanFileRepository();
                AllFilesFromPlan = daoRestorePlanFile.GetAllByPlan(restore.RestorePlan).ToDictionary <Models.RestorePlanFile, string>(p => p.Path);

                Execute(restore, files, newRestore);

                Save();
            }, CancellationToken);

            return(Results);
        }
Exemplo n.º 2
0
        //
        // Summary:
        // 1. Create `RestorePlanFile`s and `RestoredFile`s as necessary and add them to the `Restore`.
        // 2. Insert/Update `Restore` and its `RestorededFile`s into the database, also saving
        //	  the `RestorePlanFile`s instances that may have been changed by step 1.2.
        // 3. Create versioned files and remove files that won't belong to this restore.
        //
        public void Save()
        {
            Assert.IsFalse(IsSaved);

            ISession session = NHibernateHelper.GetSession();

            RestoreRepository            daoRestore            = new RestoreRepository(session);
            RestorePlanFileRepository    daoRestorePlanFile    = new RestorePlanFileRepository(session);
            RestoredFileRepository       daoRestoredFile       = new RestoredFileRepository(session);
            BackupPlanPathNodeRepository daoBackupPlanPathNode = new BackupPlanPathNodeRepository(session);

            var FilesToTrack          = SuppliedFiles;
            var FilesToInsertOrUpdate = FilesToTrack;

            BlockPerfStats stats = new BlockPerfStats();

            using (ITransaction tx = session.BeginTransaction())
            {
                try
                {
                    // ------------------------------------------------------------------------------------

                    stats.Begin("STEP 1");

                    // 1. Create `RestorePlanFile`s and `RestoredFile`s as necessary and add them to the `Restore`.
                    foreach (Models.RestorePlanFile entry in FilesToInsertOrUpdate)
                    {
                        // Throw if the operation was canceled.
                        CancellationToken.ThrowIfCancellationRequested();

                        // 1.1 - Insert/Update RestorePlanFile's and RestoredFile's if they don't exist yet.

                        // IMPORTANT: It's important that we guarantee the referenced `RestorePlanFile` has a valid `Id`
                        // before we reference it elsewhere, otherwise NHibernate won't have a valid value to put on
                        // the `restore_plan_file_id` column.
                        daoRestorePlanFile.InsertOrUpdate(tx, entry);                         // Guarantee it's saved

                        Models.RestoredFile restoredFile = daoRestoredFile.GetByRestoreAndPath(Restore, entry.Path);
                        if (restoredFile == null)                         // If we're resuming, this should already exist.
                        {
                            // Create `RestoredFile`.
                            Models.BackupedFile backupedFile = entry.VersionedFile.UserData as Models.BackupedFile;
                            restoredFile = new Models.RestoredFile(Restore, entry, backupedFile);
                        }
                        restoredFile.UpdatedAt = DateTime.UtcNow;
                        daoRestoredFile.InsertOrUpdate(tx, restoredFile);

                        Restore.Files.Add(restoredFile);
                        //daoRestore.Update(tx, Restore);

                        ProcessBatch(session);
                    }

                    ProcessBatch(session, true);
                    stats.End();

                    // ------------------------------------------------------------------------------------

                    stats.Begin("STEP 2");

                    // 2. Insert/Update `Restore` and its `RestorededFile`s into the database, also saving
                    //	  the `RestorePlanFile`s instances that may have been changed by step 1.2.
                    {
                        daoRestore.Update(tx, Restore);
                    }

                    ProcessBatch(session, true);
                    stats.End();

                    // ------------------------------------------------------------------------------------

                    tx.Commit();
                }
                catch (OperationCanceledException)
                {
                    tx.Rollback();                     // Rollback the transaction
                    throw;
                }
                catch (Exception)
                {
                    tx.Rollback();                     // Rollback the transaction
                    throw;
                }
                finally
                {
                    //session.Close();
                    if (session.IsConnected)
                    {
                        session.Disconnect();
                    }
                }
            }

            IsSaved = true;

            // 3. Create versioned files and remove files that won't belong to this restore.
            TransferSet.Files = GetFilesToTransfer(Restore, SuppliedFiles);
        }