Example #1
0
        // Methods

        public (Byte[] Result, DeltaFileTransaction Transaction) Restore(FileNode node, LogEntry entry)
        {
            Byte[] result     = null;
            Byte[] lastRead   = null;
            var    logEntries = node.LogEntries.Where(x => x.CreatedAt <= entry.CreatedAt);
            var    strategy   = _fileHandlingStrategySelector.Select(node);

            foreach (var current in logEntries)
            {
                switch (current.Type)
                {
                case LogEntryTypes.Genesis:
                    result = _archiveManager.Read(node.FileName, node);
                    break;

                case LogEntryTypes.Normal:
                    var delta = _archiveManager.Read(current.DiffName, node);
                    lastRead = result;
                    result   = _deltaPatcher.Patch(result, delta);
                    break;

                default:
                    break;
                }
            }

            var newSignature = _signatureCreator.CreateSignature(result);
            var newDelta     = _deltaCreator.BuildDelta(newSignature, lastRead);
            var transaction  = new DeltaFileTransaction(node,
                                                        new Signature($"{Guid.NewGuid().ToString()}.signature", newSignature),
                                                        new Delta($"{Guid.NewGuid().ToString()}.delta", newDelta));

            return(result, transaction);
        }
        public DeltaFileTransaction CreateDelta(FileNode node)
        {
            var logEntry = node.LogEntries.First(x => x.Type == LogEntryTypes.Genesis);
            var original = _archiveManager.Read(node.FileName, node);

            var firstDelta = node.LogEntries.First(x => x.Type == LogEntryTypes.Normal);
            var delta      = _archiveManager.Read(firstDelta.DiffName, node);
            var patched    = _deltaPatcher.Patch(original, delta);

            foreach (var current in node.LogEntries.Where(x => x.Type == LogEntryTypes.Normal).Skip(1))
            {
                var rollingDelta = _archiveManager.Read(current.DiffName, node);
                patched = _deltaPatcher.Patch(patched, rollingDelta);
            }

            var deltaName          = $"{Guid.NewGuid().ToString()}.delta";
            var mostRecentLogEntry = node.LogEntries.OrderBy(x => x.CreatedAt).Last();
            var signature          = _archiveManager.Read(logEntry.SignatureEntry, node);
            var newDelta           = _deltaCreator.BuildDelta(signature, node);

            String newSignatureName = $"{Guid.NewGuid().ToString()}.signature";

            Byte[] newSignature = _signatureCreator.CreateSignature(node);


            var newLogEntry = new LogEntry
            {
                CreatedAt      = DateTime.Now,
                Type           = LogEntryTypes.Normal,
                DiffName       = deltaName,
                SignatureEntry = newSignatureName
            };

            node.LogEntries.Add(newLogEntry);

            var transaction = new DeltaFileTransaction(node,
                                                       new Signature(newSignatureName, newSignature),
                                                       new Delta(deltaName, newDelta));

            return(transaction);
        }