public ReliableFileWriteStream(String FilePath)
 {
     AbsoluteFilePath = FileNameHandling.GetAbsolutePath(FilePath, System.Environment.CurrentDirectory);
     if (File.Exists(AbsoluteFilePath + ".new"))
     {
         throw new InvalidOperationException("FileExists: {0}".Formats(AbsoluteFilePath + ".new"));
     }
     if (File.Exists(AbsoluteFilePath))
     {
         if (File.Exists(AbsoluteFilePath + ".old"))
         {
             throw new InvalidOperationException("FileExists: {0}".Formats(AbsoluteFilePath + ".old"));
         }
     }
     FileStream = Streams.CreateResizable(AbsoluteFilePath + ".new");
 }
Example #2
0
        public static void ApplySchemaDiff(String MemoryDatabaseFileOld, String MemoryDatabaseFileNew, String SchemaDiffFile, String MemoryDatabaseFileOutput)
        {
            var    bs = Niveum.ObjectSchema.BinarySerializerWithString.Create();
            Schema SchemaOld;
            var    SchemaNew = LoadSchema(MemoryDatabaseFileNew, bs);

            var Loader = new RelationSchemaDiffLoader(SchemaNew);

            Loader.LoadType(SchemaDiffFile);
            var l = Loader.GetResult();

            using (var fs = Streams.OpenReadable(MemoryDatabaseFileOld))
            {
                if (fs.ReadSimpleString(8) != "KRUSDATA")
                {
                    throw new InvalidOperationException("FileFormatMismatch");
                }
                fs.ReadUInt64();
                var OldSchemaLength = fs.ReadInt64();
                if (OldSchemaLength == 0)
                {
                    throw new InvalidOperationException("FileContainsNoSchema");
                }
                var OldSchemaPosition = fs.Position;
                SchemaOld = bs.Read <Schema>(fs);
                if (fs.Position != OldSchemaPosition + OldSchemaLength)
                {
                    throw new InvalidOperationException();
                }
                fs.ReadInt64();

                RelationSchemaDiffVerifier.Verifiy(SchemaOld, SchemaNew, l);
                var orvs = new RelationValueSerializer(SchemaOld);
                var nrvs = new RelationValueSerializer(SchemaNew);

                var OldEntities          = SchemaOld.Types.Where(t => t.OnEntity).Select(t => t.Entity).ToList();
                var OldEntityNameToIndex = OldEntities.Select((e, i) => new KeyValuePair <String, int>(e.Name, i)).ToDictionary(p => p.Key, p => p.Value);
                var NewEntities          = SchemaNew.Types.Where(t => t.OnEntity).Select(t => t.Entity).ToList();
                var dt = new RelationSchemaDiffTranslator(SchemaOld, SchemaNew, l);

                var Dir = FileNameHandling.GetFileDirectory(MemoryDatabaseFileOutput);
                if (Dir != "" && !Directory.Exists(Dir))
                {
                    Directory.CreateDirectory(Dir);
                }

                var OldEntityCount = OldEntities.Count;

                var Positions = new Dictionary <int, Int64>();
                var CurrentEntityTableIndex = 0;
                var CurrentCount            = 0;
                if (OldEntityCount > 0)
                {
                    Positions.Add(0, fs.Position);
                    CurrentCount = fs.ReadInt32();
                }
                Action <int> AdvanceTo = EntityTableIndex =>
                {
                    if (Positions.ContainsKey(EntityTableIndex))
                    {
                        fs.Position             = Positions[EntityTableIndex];
                        CurrentEntityTableIndex = EntityTableIndex;
                        CurrentCount            = fs.ReadInt32();
                    }
                    else
                    {
                        if ((EntityTableIndex < 0) || (EntityTableIndex >= OldEntityCount))
                        {
                            throw new InvalidOperationException();
                        }
                        while (CurrentEntityTableIndex < EntityTableIndex)
                        {
                            var rr = orvs.GetRowReader(OldEntities[CurrentEntityTableIndex]);
                            while (CurrentCount > 0)
                            {
                                rr(fs);
                                CurrentCount -= 1;
                            }
                            CurrentEntityTableIndex += 1;
                            if (CurrentEntityTableIndex >= OldEntityCount)
                            {
                                break;
                            }
                            Positions.Add(CurrentEntityTableIndex, fs.Position);
                            CurrentCount = fs.ReadInt32();
                        }
                    }
                };

                using (var fso = Streams.CreateResizable(MemoryDatabaseFileOutput))
                {
                    fso.WriteSimpleString("KRUSDATA", 8);
                    fso.WriteUInt64(SchemaNew.Hash());
                    var SchemaLengthPosition = fso.Position;
                    fso.WriteInt64(0);
                    var SchemaPosition = fso.Position;
                    bs.Write(SchemaNew, fso);
                    var SchemaLength       = fso.Position - SchemaPosition;
                    var DataLengthPosition = fso.Position;
                    fso.WriteInt64(0);
                    var DataPosition = fso.Position;

                    foreach (var ne in NewEntities)
                    {
                        var oOldEntityName = dt.GetOldEntityName(ne.Name);
                        if (oOldEntityName.OnNone)
                        {
                            fso.WriteInt32(0);
                            continue;
                        }
                        var Index = OldEntityNameToIndex[oOldEntityName.Some];
                        var rr    = orvs.GetRowReader(OldEntities[Index]);
                        var rw    = nrvs.GetRowWriter(ne);
                        var t     = dt.GetTranslator(ne.Name);
                        AdvanceTo(Index);

                        fso.WriteInt32(CurrentCount);
                        while (CurrentCount > 0)
                        {
                            rw(fso, t(rr(fs)));
                            CurrentCount -= 1;
                        }
                    }

                    var DataLength = fso.Position - DataPosition;
                    fso.Position = SchemaLengthPosition;
                    fso.WriteInt64(SchemaLength);
                    fso.Position = DataLengthPosition;
                    fso.WriteInt64(DataLength);
                }
            }
        }