Esempio n. 1
0
        // parameters:
        //      strBackupFileName   备份文件名。扩展名应为 .dp2bak
        // return:
        //      -1  出错
        //      0   处理被中断
        //      1   成功
        int BackupDatabase(
            RmsChannel channel,
            string strRecPathFileName,
            BreakPointInfo info,
            string strBackupFileName,
            out BreakPointInfo breakpoint,
            out string strError)
        {
            strError = "";

            breakpoint = new BreakPointInfo();
            breakpoint.BackupFileName = strBackupFileName;

            this._stop.BeginLoop();
            try
            {
                ExportUtil export_util = new ExportUtil();
                export_util.SafeMode = true;
                export_util.TempDir  = this.App.TempDir;
                int nRet = export_util.Begin(null,
                                             strBackupFileName,
                                             out strError);
                if (nRet == -1)
                {
                    return(-1);
                }

                try
                {
                    List <string> lines = new List <string>();
                    using (StreamReader sr = new StreamReader(strRecPathFileName, Encoding.UTF8))
                    {
                        long lTotalLength = sr.BaseStream.Length;

                        // 跳过断点位置前,以前已经处理过的行
                        if (info != null &&
                            string.IsNullOrEmpty(info.DbName) == false &&
                            string.IsNullOrEmpty(info.RecID) == false)
                        {
                            while (true)
                            {
                                if (this.Stopped == true)
                                {
                                    strError = "中断";
                                    WriteStateFile(strBackupFileName, "abort");  // 表示文件创建过程被中断。文件内容不完整
                                    return(0);
                                }

                                string line = sr.ReadLine();
                                if (line == null)
                                {
                                    break;
                                }
                                string strDbName = ResPath.GetDbName(line);
                                string strID     = ResPath.GetRecordId(line);

                                if (info.DbName == strDbName && info.RecID == strID)
                                {
                                    break;
                                }
                            }
                        }

                        this.AppendResultText("开始写入大备份文件" + strBackupFileName + "\r\n");

                        while (true)
                        {
                            if (this.Stopped == true)
                            {
                                strError = "中断";
                                WriteStateFile(strBackupFileName, "abort");  // 表示文件创建过程被中断。文件内容不完整
                                return(0);
                            }

                            string line = sr.ReadLine();
                            if (line != null)
                            {
                                lines.Add(line);
                            }

                            if (lines.Count >= BATCH_SIZE ||
                                (line == null && lines.Count > 0))
                            {
                                RmsBrowseLoader loader = new RmsBrowseLoader();
                                loader.Channel  = channel;
                                loader.Format   = "id,xml,timestamp,metadata";
                                loader.RecPaths = lines;

                                foreach (Record record in loader)
                                {
                                    if (this.Stopped == true)
                                    {
                                        strError = "中断";
                                        WriteStateFile(strBackupFileName, "abort");  // 表示文件创建过程被中断。文件内容不完整
                                        return(0);
                                    }

                                    // TODO: 检查 RecordBody 是否为 null
                                    nRet = export_util.ExportOneRecord(
                                        channel,
                                        this._stop,
                                        this.App.WsUrl,
                                        record.Path,
                                        record.RecordBody.Xml,
                                        record.RecordBody.Metadata,
                                        record.RecordBody.Timestamp,
                                        out strError);
                                    if (nRet == -1)
                                    {
                                        WriteStateFile(strBackupFileName, "error");  // 表示文件创建过程出错
                                        return(-1);
                                    }

                                    breakpoint.DbName = ResPath.GetDbName(record.Path);
                                    breakpoint.RecID  = ResPath.GetRecordId(record.Path);

                                    long lCurrent = sr.BaseStream.Position;

                                    SetProgressText(m_nRecordCount.ToString() + " " + record.Path + " " + GetPercent((double)lCurrent, lTotalLength));

                                    // 每 100 条显示一行
                                    if ((m_nRecordCount % 100) == 0)
                                    {
                                        this.AppendResultText("已输出记录 " + record.Path + "  " + (m_nRecordCount + 1).ToString() + "\r\n");
                                    }
                                    m_nRecordCount++;
                                }

                                lines.Clear();
                            }

                            if (line == null)
                            {
                                break;
                            }
                        }

                        Debug.Assert(lines.Count == 0, "");
                        breakpoint = null;
                    }
                }
                finally
                {
                    export_util.End();
                }

                WriteStateFile(strBackupFileName, "finish");  // 表示文件已经创建完成
                return(1);
            }
            catch (Exception ex)
            {
                strError = "BackupDatabase() 出现异常: " + ExceptionUtil.GetDebugText(ex);
                WriteStateFile(strBackupFileName, "error");  // 表示文件创建过程出错
                return(-1);
            }
            finally
            {
                this._stop.EndLoop();
            }
        }