/// <summary>
        /// Restores records.
        /// </summary>
        /// <param name="snapshot">Snapshot to restore.</param>
        public void Restore(FileCabinetRecordSnapshot snapshot)
        {
            var newRecords  = snapshot.GetRecords();
            var start       = newRecords[0].Id;
            var end         = newRecords[newRecords.Count - 1].Id;
            var index       = 0;
            var startInsert = 0;

            while (index < this.records.Count)
            {
                if (this.records[index].Id >= start && this.records[index].Id <= end)
                {
                    this.records.RemoveAt(index);
                }
                else
                {
                    index++;
                    startInsert++;
                }
            }

            if (this.records.Count == 0)
            {
                this.records.AddRange(newRecords);
            }
            else
            {
                this.records.InsertRange(startInsert, newRecords);
            }
        }
示例#2
0
        /// <summary>
        /// Restores records.
        /// </summary>
        /// <param name="snapshot">Snapshot to restore.</param>
        public void Restore(FileCabinetRecordSnapshot snapshot)
        {
            var  newRecords      = snapshot.GetRecords();
            var  start           = newRecords[0].Id;
            var  end             = newRecords[newRecords.Count - 1].Id;
            var  startInsert     = -1;
            int  currentId       = 0;
            int  currentPosition = 0;
            bool readRecords     = false;

            using (var reader = new BinaryReader(this.stream, Encoding.Unicode, true))
            {
                this.stream.Seek(SizeOfShort, SeekOrigin.Begin);

                if (reader.PeekChar() < 0)
                {
                    startInsert = 0;
                }

                while (reader.PeekChar() > -1)
                {
                    currentId = reader.ReadInt32();
                    this.stream.Seek(-SizeOfInt, SeekOrigin.Current);

                    if (currentId > start && startInsert == -1)
                    {
                        startInsert = currentPosition;
                    }

                    if (currentId < start)
                    {
                        startInsert = currentPosition + 1;
                    }

                    if (currentId > end)
                    {
                        readRecords = true;
                        break;
                    }

                    this.stream.Seek(SizeOfRecord, SeekOrigin.Current);
                }

                if (readRecords == true)
                {
                    this.stream.Seek(-SizeOfShort, SeekOrigin.Current);

                    while (reader.PeekChar() > -1)
                    {
                        newRecords.Add(ReadRecord(reader));
                    }
                }
            }

            using (var writer = new BinaryWriter(this.stream, Encoding.Unicode, true))
            {
                this.stream.Seek(startInsert * SizeOfRecord, SeekOrigin.Begin);

                foreach (var r in newRecords)
                {
                    this.stream.Seek(SizeOfShort, SeekOrigin.Current);
                    this.WriteRecordWithID(writer, r);
                }

                this.stream.Seek(0, SeekOrigin.Begin);
            }
        }