public DMSFileMergeResult MergeDMSFile(DMSFile fileToMerge, bool dedupRows = true) { DMSFileMergeResult result = new DMSFileMergeResult(); if (this.Equals(fileToMerge)) { result.Success = false; return(result); } foreach (var newTable in fileToMerge.Tables) { Console.WriteLine("Merging table: " + newTable.Name); if (Tables.Select(t => t.Name).Contains(newTable.Name)) { /* make sure we can merge these rows */ var existingColumns = Tables.Where(t => t.Name.Equals(newTable.Name)).First().Columns.Select(c => c.Name); if (newTable.Columns.Select(c => c.Name).SequenceEqual(existingColumns)) { if (dedupRows) { DedupRows(newTable); } if (newTable.Rows.Count > 0) { Tables.Add(newTable); result.SuccessfulMerge.Add(newTable); } else { result.NoOpMerge.Add(newTable); } } else { result.FailedMerge.Add(newTable); } } else { /* no collision, just add it to the list */ if (dedupRows) { DedupRows(newTable); } if (newTable.Rows.Count > 0) { Tables.Add(newTable); result.SuccessfulMerge.Add(newTable); } else { result.NoOpMerge.Add(newTable); } } } result.Success = true; return(result); }
public static void Write(string path, DMSFile file, bool saveOnlyDiffs = false, List <DMSTable> selectedTables = null) { if (File.Exists(path)) { File.Delete(path); } using (StreamWriter sw = new StreamWriter(File.OpenWrite(path))) { file.WriteToStream(sw, saveOnlyDiffs, selectedTables); } }
public static DMSFile Read(string path) { var memSizeBefore = Process.GetCurrentProcess().PrivateMemorySize64; DMSFile file = null; DMSRowDecoder rowDecoder = new DMSRowDecoder(); Stopwatch sw = new Stopwatch(); sw.Start(); if (File.Exists(path)) { file = new DMSFile(); using (StreamReader sr = new StreamReader(File.OpenRead(path))) { /* Read the version */ var firstLine = sr.ReadLine(); /* ensure the first line is what we expect it to look like */ if (firstLine.StartsWith("SET VERSION_DAM") == false) { throw new FormatException(path + " appears to be an invalid DMS DAT file."); } file.Version = firstLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)[2]; /* Blank line */ file.BlankLine = sr.ReadLine(); file.Endian = sr.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)[2]; file.BaseLanguage = sr.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)[2]; file.Database = sr.ReadLine().Replace("REM Database: ", ""); file.Started = sr.ReadLine().Replace("REM Started: ", ""); /* Read the EXPORT RECROD/SPACE.x */ sr.ReadLine(); /* Read namespaces */ var currentLine = ""; while (currentLine != "/") { currentLine = sr.ReadLine(); if (currentLine != "/") { file.Namespaces.Add(currentLine); } } StringBuilder sb = new StringBuilder(); /* Read big metadata blob */ MemoryStream ms = new MemoryStream(); byte[] lineBytes = null; currentLine = ""; while (currentLine != "/") { lineBytes = DMSDecoder.DecodeString(currentLine); ms.Write(lineBytes, 0, lineBytes.Length); currentLine = sr.ReadLine(); } var ddlBlob = ms.ToArray(); file.DDLs = new DDLDefaults(ddlBlob, file.Endian == "LE"); ms.Close(); currentLine = sr.ReadLine(); while (currentLine.StartsWith("EXPORT")) { /* Handle a table export */ DMSTable table = new DMSTable(); file.Tables.Add(table); /* Record name */ var match = tableNameRegex.Match(currentLine); table.Name = match.Groups[1].Value; table.DBName = match.Groups[2].Value; table.WhereClause = ""; /* Record where clause */ currentLine = sr.ReadLine(); while (currentLine != "/") { if (table.WhereClause.Length > 0) { table.WhereClause += "\r\n"; } table.WhereClause += currentLine; currentLine = sr.ReadLine(); } /* Record metadata */ sb.Clear(); currentLine = sr.ReadLine(); ms = new MemoryStream(); while (currentLine != "/") { lineBytes = DMSDecoder.DecodeString(currentLine); ms.Write(lineBytes, 0, lineBytes.Length); currentLine = sr.ReadLine(); } table.Metadata = new DMSRecordMetadata(ms.ToArray(), file.Endian == "LE"); ms.Close(); /* Record Columns */ currentLine = sr.ReadLine(); sb.Clear(); while (currentLine != "/") { sb.Append(currentLine); currentLine = sr.ReadLine(); } /* Parse the columns */ var matches = columnRegex.Matches(sb.ToString()); foreach (Match m in matches) { DMSColumn col = new DMSColumn(); col.Name = m.Groups[2].Value; col.Type = m.Groups[3].Value; col.Size = m.Groups[4].Value; if (m.Groups.Count == 6) { col.Size += m.Groups[5].Value; } table.Columns.Add(col); } currentLine = sr.ReadLine(); DMSRow row = new DMSRow(); sb.Clear(); while (currentLine != "/") { if (currentLine == "//") { var nextRow = new DMSRow(); rowDecoder.Finish(nextRow); nextRow.OwningTable = table; nextRow.GenerateHashes(); table.Rows.Add(nextRow); rowDecoder.Reset(); } else { rowDecoder.DecodeLine(currentLine); } currentLine = sr.ReadLine(); } currentLine = sr.ReadLine(); } /* Parse "Ended" */ file.Ended = currentLine.Replace("REM Ended: ", ""); } } sw.Stop(); Console.WriteLine("Total Read Time: " + sw.Elapsed.TotalSeconds + " seconds."); Console.WriteLine("DAT File Size: " + new FileInfo(path).Length / 1024.0 / 1024.0 + "MB"); Console.WriteLine("Memory Size Increase: " + ((Process.GetCurrentProcess().PrivateMemorySize64 - memSizeBefore) / 1024.0) / 1024.0 + "MB"); var totalRows = file.Tables.Sum(t => t.Rows.Count); Console.WriteLine("Total Row Count: " + totalRows); /* Set the filename parameter */ file.FileName = new FileInfo(path).Name; return(file); }