public static KeyValuePair <Schema, RelationVal> LoadData(String MemoryDatabaseFile, Firefly.Mapping.Binary.BinarySerializer bs) { Schema s; RelationVal Value; using (var fs = Streams.OpenReadable(MemoryDatabaseFile)) { if (fs.ReadSimpleString(8) != "KRUSDATA") { throw new InvalidOperationException("FileFormatMismatch"); } fs.ReadUInt64(); var SchemaLength = fs.ReadInt64(); if (SchemaLength == 0) { throw new InvalidOperationException("FileContainsNoSchema"); } var SchemaPosition = fs.Position; s = bs.Read <Schema>(fs); if (fs.Position != SchemaPosition + SchemaLength) { throw new InvalidOperationException(); } var DataLength = fs.ReadInt64(); var DataPosition = fs.Position; var rvs = new RelationValueSerializer(s); Value = rvs.Read(fs); if (fs.Position != DataPosition + DataLength) { throw new InvalidOperationException(); } } return(new KeyValuePair <Schema, RelationVal>(s, Value)); }
public static RelationVal LoadData(Schema s, String[] DataDirOrMemoryDatabaseFiles) { var Entities = s.Types.Where(t => t.OnEntity).Select(t => t.Entity).ToList(); var DuplicatedCollectionNames = Entities.GroupBy(e => e.CollectionName).Where(g => g.Count() > 1).Select(g => g.Key).ToList(); if (DuplicatedCollectionNames.Count > 0) { throw new InvalidOperationException("DuplicatedCollectionNames: {0}".Formats(String.Join(" ", DuplicatedCollectionNames))); } var CollectionNameToEntity = Entities.ToDictionary(e => e.CollectionName, StringComparer.OrdinalIgnoreCase); var SchemaHash = s.Hash(); var Files = new Dictionary <String, Byte[]>(); var MemoryFiles = new List <RelationVal>(); foreach (var DataDirOrMemoryDatabaseFile in DataDirOrMemoryDatabaseFiles) { if (Directory.Exists(DataDirOrMemoryDatabaseFile)) { foreach (var f in Directory.GetFiles(DataDirOrMemoryDatabaseFile, "*.tree", SearchOption.AllDirectories).OrderBy(ff => ff, StringComparer.OrdinalIgnoreCase)) { Files.Add(f, File.ReadAllBytes(f)); } } else { RelationVal FileValue; using (var fs = Streams.OpenReadable(DataDirOrMemoryDatabaseFile)) { if (fs.ReadSimpleString(8) != "KRUSDATA") { throw new InvalidOperationException("FileFormatMismatch"); } var Hash = fs.ReadUInt64(); if (Hash != SchemaHash) { throw new InvalidOperationException("DatabaseSchemaVersionMismatch"); } var SchemaLength = fs.ReadInt64(); fs.Position += SchemaLength; var DataLength = fs.ReadInt64(); var DataPosition = fs.Position; var rvs = new RelationValueSerializer(s); FileValue = rvs.Read(fs); if (fs.Position != DataPosition + DataLength) { throw new InvalidOperationException(); } } MemoryFiles.Add(FileValue); } } var Forests = Files.AsParallel().Select(p => { using (var bas = new ByteArrayStream(p.Value)) { using (var sr = Txt.CreateTextReader(bas.AsNewReading(), Firefly.TextEncoding.TextEncoding.Default)) { return(TreeFile.ReadDirect(sr, p.Key, new TreeFormatParseSetting(), new TreeFormatEvaluateSetting()).Value.Nodes); } } }).ToList(); var Tables = new Dictionary <String, List <Node> >(); foreach (var Forest in Forests) { foreach (var n in Forest) { if (!n.OnStem) { continue; } var Name = n.Stem.Name; if (CollectionNameToEntity.ContainsKey(Name)) { Name = CollectionNameToEntity[Name].Name; } if (!Tables.ContainsKey(Name)) { Tables.Add(Name, new List <Node>()); } var t = Tables[Name]; t.AddRange(n.Stem.Children); } } var Missings = Entities.Select(e => e.Name).Except(Tables.Keys, StringComparer.OrdinalIgnoreCase).ToList(); foreach (var Name in Missings) { Tables.Add(Name, new List <Node>()); } var rvts = new RelationValueTreeSerializer(s); var Value = rvts.Read(Tables); foreach (var FileValue in MemoryFiles) { for (int k = 0; k < FileValue.Tables.Count; k += 1) { Value.Tables[k].Rows.AddRange(FileValue.Tables[k].Rows); } } return(Value); }