Esempio n. 1
0
        /// <summary>
        /// Restores the specified snapshot and measures time.
        /// </summary>
        /// <param name="snapshot">The snapshot.</param>
        public void Restore(FileCabinetServiceSnapshot snapshot)
        {
            string context = string.Format(CultureInfo.InvariantCulture, "{0} - Calling {1} with {2}\n", DateTime.Now, "Restore()", snapshot);

            File.AppendAllText("logger.txt", context);
            this.service.Restore(snapshot);
        }
Esempio n. 2
0
        /// <summary>
        /// Restore.
        /// </summary>
        /// <param name="snapshot">snapshot.</param>
        /// <returns>records count.</returns>
        public int Restore(FileCabinetServiceSnapshot snapshot)
        {
            if (snapshot is null)
            {
                throw new ArgumentNullException(nameof(snapshot), $"{nameof(snapshot)} is null");
            }

            IList <FileCabinetRecord> readRecords = snapshot.ReadRecords;

            foreach (var record in readRecords)
            {
                var index = this.list.FindIndex(x => x.Id == record.Id);
                if (index != -1)
                {
                    this.list[index] = record;
                    this.EditRecord(record);
                }
                else
                {
                    this.list.Add(record);
                    this.AddValueToDictionary(record.FirstName, this.firstNameDictionary, record);
                    this.AddValueToDictionary(record.LastName, this.lastNameDictionary, record);
                    this.AddValueToDictionary(record.DateOfBirth, this.dateOfBirthDictionary, record);
                }
            }

            return(readRecords.Count);
        }
        /// <summary>
        /// Recovers saved snapshot recordings.
        /// </summary>
        /// <param name="snapshot">Snapshot.</param>
        /// <returns>count of recovered records.</returns>
        public int Restore(FileCabinetServiceSnapshot snapshot)
        {
            int count = this.service.Restore(snapshot);

            this.WriteLogInFile(nameof(this.service.Restore), string.Empty);
            this.WriteLogReturnInFile(nameof(this.service.Restore), count.ToString(CultureInfo.InvariantCulture));
            return(count);
        }
Esempio n. 4
0
        /// <summary>
        /// Restores the specified snapshot and logs.
        /// </summary>
        /// <param name="snapshot">The snapshot.</param>
        public void Restore(FileCabinetServiceSnapshot snapshot)
        {
            var sw = Stopwatch.StartNew();

            this.service.Restore(snapshot);
            sw.Stop();
            Console.WriteLine("Restore method execution duration is {0} ticks", sw.ElapsedTicks);
        }
        /// <summary>
        /// Recovers saved snapshot recordings.
        /// </summary>
        /// <param name="snapshot">Snapshot.</param>
        /// <returns>count of recovered records.</returns>
        public int Restore(FileCabinetServiceSnapshot snapshot)
        {
            this.stopwatch.Restart();
            var count = this.service.Restore(snapshot);

            this.stopwatch.Stop();
            this.Information(nameof(this.service.Restore), this.stopwatch.ElapsedTicks);
            return(count);
        }
Esempio n. 6
0
        /// <summary>
        /// Restores the specified snapshot.
        /// </summary>
        /// <param name="snapshot">The snapshot.</param>
        public void Restore(FileCabinetServiceSnapshot snapshot)
        {
            if (snapshot is null)
            {
                throw new ArgumentNullException(nameof(snapshot));
            }

            foreach (FileCabinetRecord record in snapshot.Records)
            {
                try
                {
                    this.validator.ValidateCabinetRecord(RecordToParams(record));
                    if (this.dictionaryId.ContainsKey(record.Id))
                    {
                        this.EditRecord(record.Id, RecordToParams(record));
                    }
                    else
                    {
                        this.fileStream.Position = this.fileStream.Length;
                        this.dictionaryId.Add(record.Id, this.fileStream.Position);
                        AddToDictionary<string, long>(this.firstNameDictionary, record.FirstName.ToUpperInvariant(), this.fileStream.Position);
                        AddToDictionary<string, long>(this.lastNameDictionary, record.LastName.ToUpperInvariant(), this.fileStream.Position);
                        AddToDictionary<DateTime, long>(this.dateOfBirthDictionary, record.DateOfBirth, this.fileStream.Position);

                        byte[] tempFirstName = Encoding.Default.GetBytes(record.FirstName);
                        byte[] tempLastName = Encoding.Default.GetBytes(record.LastName);
                        byte[] firstName = new byte[120];
                        byte[] lastName = new byte[120];
                        ToBytesDecimal toBytesDecimal = new ToBytesDecimal(record.Salary);
                        byte[] bytesSalary = BitConverter.GetBytes(toBytesDecimal.Bytes1).Concat(BitConverter.GetBytes(toBytesDecimal.Bytes2)).ToArray();
                        Array.Copy(tempFirstName, 0, firstName, 0, tempFirstName.Length);
                        Array.Copy(tempLastName, 0, lastName, 0, tempLastName.Length);
                        this.fileStream.Write(BitConverter.GetBytes(record.Department));
                        this.fileStream.Write(BitConverter.GetBytes(record.Id));
                        this.fileStream.Write(firstName);
                        this.fileStream.Write(lastName);
                        this.fileStream.Write(BitConverter.GetBytes(record.DateOfBirth.Year));
                        this.fileStream.Write(BitConverter.GetBytes(record.DateOfBirth.Month));
                        this.fileStream.Write(BitConverter.GetBytes(record.DateOfBirth.Day));
                        this.fileStream.Write(bytesSalary);
                        this.fileStream.Write(BitConverter.GetBytes(record.Class));
                        this.fileStream.Write(new byte[] { 0 });
                    }

                    this.id = Math.Max(this.id, record.Id);
                }
                catch (ArgumentException e)
                {
                    Console.WriteLine($"{record.Id}: {e.Message}");
                }
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Restores the specified snapshot.
        /// </summary>
        /// <param name="snapshot">The snapshot.</param>
        /// <returns>
        /// count of restored records.
        /// </returns>
        public int Restore(FileCabinetServiceSnapshot snapshot)
        {
            if (snapshot is null)
            {
                throw new ArgumentNullException(nameof(snapshot), $"{nameof(snapshot)} is null");
            }

            IList <FileCabinetRecord> readRecords = snapshot.ReadRecords;

            foreach (var record in readRecords)
            {
                if (this.recordIndexPosition.ContainsKey(record.Id))
                {
                    this.EditRecord(record);
                }
                else
                {
                    this.CreateRecord(record);
                }
            }

            return(readRecords.Count);
        }
Esempio n. 8
0
        /// <summary>
        /// Remove deleted records from file.
        /// </summary>
        /// <param name="snapshot">Snapshot.</param>
        public void Restore(FileCabinetServiceSnapshot snapshot)
        {
            if (snapshot is null)
            {
                throw new ArgumentNullException($"{nameof(snapshot)} cannot be null.");
            }

            foreach (var record in snapshot.Records)
            {
                try
                {
                    int id = record.Id;
                    if (id <= 0)
                    {
                        throw new ArgumentOutOfRangeException($"{nameof(id)} must be positive.");
                    }

                    if (id <= this.list.Count)
                    {
                        var data = new RecordData();
                        data.FirstName    = record.FirstName;
                        data.LastName     = record.LastName;
                        data.DateOfBirth  = record.DateOfBirth;
                        data.Balance      = record.Balance;
                        data.Experience   = record.Experience;
                        data.EnglishLevel = record.EnglishLevel;
                        this.EditRecord(id, data);
                    }
                    else
                    {
                        this.list.Add(record);

                        if (this.firstNameDictionary.ContainsKey(record.FirstName.ToUpper(CultureInfo.InvariantCulture)))
                        {
                            this.firstNameDictionary[record.FirstName.ToUpper(CultureInfo.InvariantCulture)].Add(record);
                        }
                        else
                        {
                            this.firstNameDictionary.Add(record.FirstName.ToUpper(CultureInfo.InvariantCulture), new List <FileCabinetRecord> {
                                record
                            });
                        }

                        if (this.lastNameDictionary.ContainsKey(record.LastName.ToUpper(CultureInfo.InvariantCulture)))
                        {
                            this.lastNameDictionary[record.LastName.ToUpper(CultureInfo.InvariantCulture)].Add(record);
                        }
                        else
                        {
                            this.lastNameDictionary.Add(record.LastName.ToUpper(CultureInfo.InvariantCulture), new List <FileCabinetRecord> {
                                record
                            });
                        }

                        if (this.dateOfBirthDictionary.ContainsKey(record.DateOfBirth))
                        {
                            this.dateOfBirthDictionary[record.DateOfBirth].Add(record);
                        }
                        else
                        {
                            this.dateOfBirthDictionary.Add(record.DateOfBirth, new List <FileCabinetRecord> {
                                record
                            });
                        }
                    }
                }
                catch (IndexOutOfRangeException indexOutOfRangeException)
                {
                    snapshot.NotImported.Add($"Import record with id {record.Id} failed: {indexOutOfRangeException.Message}");
                }
            }
        }