Esempio n. 1
0
        private WriteEntry ReadWriteEntry()
        {
            WriteEntry writeEntry  = default(WriteEntry);
            WriteEntry writeEntry2 = writeEntry;

            writeEntry2.FilePosition = reader.ReadInt32();
            writeEntry2.Size         = reader.ReadInt32();
            writeEntry = writeEntry2;
            if ((long)writeEntry.Size <= 32768L)
            {
                int num  = writeEntry.Size;
                int num2 = 0;
                do
                {
                    int num3 = reader.Read(buffer, num2, num);
                    num  -= num3;
                    num2 += num3;
                }while (num > 0);
                writeEntry.Bytes = buffer;
            }
            else
            {
                writeEntry.Bytes = reader.ReadBytes(writeEntry.Size);
            }
            return(writeEntry);
        }
Esempio n. 2
0
        async Task Write(WriteEntry entry)
        {
            var requests = entry.Requests.OrderBy(o => o.ProjectionStreamSequence).ToList();

            if (!requests.Select(e => e.ProjectionStreamSequence).IsSequential())
                throw new MissingIndexEntryException();

            var firstSequence = requests.First().ProjectionStreamSequence;
            var globalSequences = requests.Select(e => e.GlobalSequence).ToList();

            await _writer.WriteProjectionIndexAsync(entry.Stream, firstSequence - 1, globalSequences);
        }
Esempio n. 3
0
        async Task Write(WriteEntry entry)
        {
            var requests = entry.Requests.OrderBy(o => o.ProjectionStreamSequence).ToList();

            if (!requests.Select(e => e.ProjectionStreamSequence).IsSequential())
            {
                throw new MissingIndexEntryException();
            }

            var firstSequence   = requests.First().ProjectionStreamSequence;
            var globalSequences = requests.Select(e => e.GlobalSequence).ToList();

            await _writer.WriteProjectionIndexAsync(entry.Stream, firstSequence - 1, globalSequences);
        }
Esempio n. 4
0
        private void ApplyEntries(int curEntryIndex, int numEntries)
        {
            for (int i = curEntryIndex; i < numEntries; i++)
            {
                JournalEntryType journalEntryType = (JournalEntryType)reader.ReadByte();
                switch (journalEntryType)
                {
                case JournalEntryType.Resize:
                {
                    string      path3             = reader.ReadString();
                    Stream      targetFileStream3 = fileSystem.OpenFileStream(path3);
                    ResizeEntry entry3            = ReadResizeEntry();
                    ApplyResizeEntry(targetFileStream3, entry3);
                    break;
                }

                case JournalEntryType.Write:
                {
                    string     path2             = reader.ReadString();
                    Stream     targetFileStream2 = fileSystem.OpenFileStream(path2);
                    WriteEntry entry2            = ReadWriteEntry();
                    ApplyWriteEntry(targetFileStream2, entry2);
                    break;
                }

                case JournalEntryType.Copy:
                {
                    string    path             = reader.ReadString();
                    Stream    targetFileStream = fileSystem.OpenFileStream(path);
                    CopyEntry entry            = ReadCopyEntry();
                    ApplyCopyEntry(targetFileStream, entry);
                    break;
                }

                default:
                    throw new CorruptionException("Unhandled entry type: " + journalEntryType);
                }
                curEntryIndex++;
                SetCurEntryIndex(curEntryIndex);
            }
        }
Esempio n. 5
0
        public override void Commit()
        {
            if (_committed)
            {
                return;
            }

            bool isOpen = _fileIndex.IsOpen;

            if (!isOpen)
            {
                _fileIndex.Open();
            }

            string workingDirectory = Path.GetDirectoryName(_fileIndex.IndexPath);

            string tmpIdx = Path.Combine(workingDirectory, string.Format("{0}.tmp", _fileIndex.IndexPath));
            string tmpMul = Path.Combine(workingDirectory, string.Format("{0}.tmp", _fileIndex.MulPath));

            _transactionWrites.Sort((l, r) => l.Index.CompareTo(r.Index));

            byte[] buffer = new byte[10 * 1024 * 1024]; // 10mb

            var writeBegin = WriteBegin;

            if (writeBegin != null)
            {
                writeBegin(this, EventArgs.Empty);
            }

            int totalIndices = _fileIndex.Length;

            if (File.Exists(tmpIdx))
            {
                File.Delete(tmpIdx);
            }

            if (File.Exists(tmpMul))
            {
                File.Delete(tmpMul);
            }

            int lastIndex = 0;

            using (BinaryWriter idxWriter = new BinaryWriter(File.Open(tmpIdx, FileMode.Create)))
                using (BinaryWriter mulWriter = new BinaryWriter(File.Open(tmpMul, FileMode.Create)))
                {
                    for (int i = 0; i < _transactionWrites.Count; i++)
                    {
                        WriteEntry entry = _transactionWrites[i];

                        for (int j = i; j < entry.Index; j++)
                        {
                            WriteIndex(j, buffer, idxWriter, mulWriter);
                            OnWriteProgress(j, totalIndices);
                        }

                        idxWriter.Write((int)mulWriter.BaseStream.Position);
                        idxWriter.Write(entry.Data.Length);
                        idxWriter.Write(entry.Extra);

                        mulWriter.Write(entry.Data, 0, entry.Data.Length);
                        OnWriteProgress(i, totalIndices);

                        lastIndex = entry.Index;
                    }

                    for (int i = lastIndex + 1; i < _fileIndex.Length; i++)
                    {
                        WriteIndex(i, buffer, idxWriter, mulWriter);
                        OnWriteProgress(i, totalIndices);
                    }
                }

            OnWriteProgress(totalIndices, totalIndices);

            buffer = null;
            _fileIndex.Close();

            DateTime now = DateTime.Now;

            string bakIdx = Path.Combine(workingDirectory, string.Format("{0}.{1:MM-dd-yy HH-mm-ss}.bak", _fileIndex.IndexPath, now));
            string bakMul = Path.Combine(workingDirectory, string.Format("{0}.{1:MM-dd-yy HH-mm-ss}.bak", _fileIndex.MulPath, now));

            int count = 1;

            while (File.Exists(bakIdx))
            {
                bakIdx = Path.Combine(workingDirectory, string.Format("{0}.{1:MM-dd-yy HH-mm-ss} {2}.bak", _fileIndex.IndexPath, now, count++));
            }

            count = 1;
            while (File.Exists(bakMul))
            {
                bakMul = Path.Combine(workingDirectory, string.Format("{0}.{1:MM-dd-yy HH-mm-ss} {2}.bak", _fileIndex.MulPath, now, count++));
            }

            File.Copy(_fileIndex.IndexPath, bakIdx);
            File.Copy(_fileIndex.MulPath, bakMul);

            File.Copy(tmpIdx, _fileIndex.IndexPath, true);
            File.Copy(tmpMul, _fileIndex.MulPath, true);

            File.Delete(tmpIdx);
            File.Delete(tmpMul);

            var writeEnd = WriteEnd;

            if (writeEnd != null)
            {
                writeEnd(this, EventArgs.Empty);
            }

            if (isOpen)
            {
                _fileIndex.Open();
            }

            _committed = true;
        }
Esempio n. 6
0
 private static void ApplyWriteEntry(Stream targetFileStream, WriteEntry entry)
 {
     targetFileStream.Seek(entry.FilePosition, SeekOrigin.Begin);
     targetFileStream.Write(entry.Bytes, 0, entry.Size);
     targetFileStream.Flush();
 }