Esempio n. 1
0
        /// <inheritdoc/>
        public int Restore(FileCabinetServiceSnapshot fileCabinetServiceSnapshot)
        {
            if (fileCabinetServiceSnapshot == null)
            {
                throw new ArgumentNullException(nameof(fileCabinetServiceSnapshot));
            }

            int numberOfRestoredRecords;

            try
            {
                numberOfRestoredRecords = this.service.Restore(fileCabinetServiceSnapshot);
            }
            catch (Exception ex) when(ex is ArgumentNullException || ex is ArgumentException)
            {
                Log($"Method {nameof(this.service.Restore)} finished with exception: " +
                    $"Message - {ex.Message}");
                throw;
            }

            Log($"Calling {nameof(this.service.Restore)}()");
            Log($"{nameof(this.service.Restore)}() returned '{numberOfRestoredRecords}'");

            return(numberOfRestoredRecords);
        }
Esempio n. 2
0
        /// <inheritdoc/>
        public int Restore(FileCabinetServiceSnapshot fileCabinetServiceSnapshot)
        {
            if (fileCabinetServiceSnapshot == null)
            {
                throw new ArgumentNullException(nameof(fileCabinetServiceSnapshot));
            }

            var loadedRecords        = fileCabinetServiceSnapshot.Records;
            var importedRecordsCount = 0;

            foreach (var importedRecord in loadedRecords)
            {
                var inputValidator   = this.InputValidator;
                var validationResult = inputValidator.ValidateParameters(importedRecord);
                if (validationResult.Item1)
                {
                    importedRecordsCount++;
                    try
                    {
                        this.EditRecord(importedRecord.Id, importedRecord);
                    }
                    catch (ArgumentException)
                    {
                        this.CreateRecord(importedRecord, true);
                    }
                }
                else
                {
                    Console.WriteLine($"Error. Record #{importedRecord.Id} is not imported. Invalid field: {validationResult.Item2}.");
                }
            }

            return(importedRecordsCount);
        }
Esempio n. 3
0
        /// <summary>
        /// Restores statement from snapshot and measure execution time.
        /// </summary>
        /// <param name="snapshot">Snapshot that represent statement to restore.</param>
        /// <returns>Amount of new records added.</returns>
        /// <exception cref="ArgumentNullException">Thrown when snapshot is null.</exception>
        public int Restore(FileCabinetServiceSnapshot snapshot)
        {
            var stopWatch = Stopwatch.StartNew();
            var result    = this.service.Restore(snapshot);

            Console.WriteLine($"Restore method execution duration is {stopWatch.ElapsedTicks} ticks.");
            return(result);
        }
Esempio n. 4
0
        /// <inheritdoc/>
        public int Restore(FileCabinetServiceSnapshot snapshot)
        {
            var watch  = Stopwatch.StartNew();
            var result = this.service.Restore(snapshot);

            this.ShowTime(Source.Resource.GetString("restoreTime", CultureInfo.InvariantCulture), watch.ElapsedTicks);
            return(result);
        }
Esempio n. 5
0
        /// <summary>
        /// Restores statement from snapshot and writes info to log file.
        /// </summary>
        /// <param name="snapshot">Snapshot that represent statement to restore.</param>
        /// <returns>Amount of new records added.</returns>
        /// <exception cref="ArgumentNullException">Thrown when snapshot is null.</exception>
        public int Restore(FileCabinetServiceSnapshot snapshot)
        {
            this.writer.WriteLine($"{DateTime.Now} Calling Restore.");
            var result = this.service.Restore(snapshot);

            this.writer.WriteLine($"{DateTime.Now} Restore imported {result} record(s).");
            return(result);
        }
Esempio n. 6
0
        /// <summary>
        /// Restores the specified snapshot.
        /// </summary>
        /// <param name="snapshot">The snapshot.</param>
        /// <param name="exceptions">The exceptions.</param>
        public void Restore(FileCabinetServiceSnapshot snapshot, out Dictionary <int, string> exceptions)
        {
            if (snapshot == null)
            {
                throw new ArgumentNullException(nameof(snapshot));
            }

            this.service.Restore(snapshot, out exceptions);
        }
Esempio n. 7
0
        /// <summary>
        /// Restores the specified snapshot.
        /// </summary>
        /// <param name="snapshot">The snapshot.</param>
        /// <param name="exceptions">The exceptions.</param>
        public void Restore(FileCabinetServiceSnapshot snapshot, out Dictionary <int, string> exceptions)
        {
            if (snapshot == null)
            {
                throw new ArgumentNullException(nameof(snapshot));
            }

            this.stopWatch.Reset();
            this.stopWatch.Start();
            this.service.Restore(snapshot, out exceptions);

            this.stopWatch.Stop();
            this.ticks = this.stopWatch.ElapsedTicks;

            this.write($"Restore method execution duration is {this.ticks} ticks.");
        }
Esempio n. 8
0
        /// <inheritdoc/>
        public int Restore(FileCabinetServiceSnapshot fileCabinetServiceSnapshot)
        {
            if (fileCabinetServiceSnapshot == null)
            {
                throw new ArgumentNullException(nameof(fileCabinetServiceSnapshot));
            }

            this.watch.Reset();
            this.watch.Start();

            var numberOfRestoredRecords = this.service.Restore(fileCabinetServiceSnapshot);

            this.watch.Stop();
            Console.WriteLine($"Restore method execution duration is {this.watch.ElapsedTicks} ticks.");
            Console.WriteLine();

            return(numberOfRestoredRecords);
        }
Esempio n. 9
0
        /// <inheritdoc/>
        public int Restore(FileCabinetServiceSnapshot snapshot)
        {
            if (snapshot is null || snapshot.Records is null)
            {
                throw new ArgumentNullException(nameof(snapshot));
            }

            int count      = 0;
            var importData = snapshot.Records;

            foreach (var record in importData)
            {
                var validationResult = this.recordValidator.ValidateParameters(record);
                if (!validationResult.Item1)
                {
                    Console.WriteLine(Source.Resource.GetString("importFailValidation", CultureInfo.InvariantCulture), record.Id, validationResult.Item2);
                    continue;
                }

                if (this.idсache.Contains(record.Id))
                {
                    var temp = this.list.Find(x => x.Id == record.Id);
                    temp.FirstName = record.FirstName;
                    temp.LastName  = record.LastName;
                    temp.Sex       = record.Sex;
                    temp.Weight    = record.Weight;
                    temp.Height    = record.Height;
                    Console.WriteLine(Source.Resource.GetString("importReplaceRecord", CultureInfo.InvariantCulture), record.Id);
                    count++;
                }
                else
                {
                    this.idсache.Add(record.Id);
                    this.list.Add(record);
                    count++;
                }
            }

            return(count);
        }
Esempio n. 10
0
        /// <inheritdoc/>
        public int Restore(FileCabinetServiceSnapshot snapshot)
        {
            if (snapshot is null || snapshot.Records is null)
            {
                throw new ArgumentNullException(nameof(snapshot));
            }

            var list       = new List <FileCabinetRecord>();
            var importData = snapshot.Records;
            int count      = 0;

            foreach (var record in importData)
            {
                var validationResult = this.recordValidator.ValidateParameters(record);
                if (!validationResult.Item1)
                {
                    Console.WriteLine(Source.Resource.GetString("importFailValidation", CultureInfo.InvariantCulture), record.Id, validationResult.Item2);
                    continue;
                }

                if (this.idсache.ContainsKey(record.Id))
                {
                    this.WriteToFile(record, this.idсache[record.Id]);
                    Console.WriteLine(Source.Resource.GetString("importReplaceRecord", CultureInfo.InvariantCulture), record.Id);
                    count++;
                }
                else
                {
                    this.idсache.Add(record.Id, this.offset);
                    this.WriteToFile(record, this.offset);
                    this.offset += RecordSize;
                    count++;
                }
            }

            return(count);
        }
        /// <summary>
        /// Restores the specified snapshot.
        /// </summary>
        /// <param name="snapshot">The snapshot.</param>
        /// <param name="exceptions">The exceptions.</param>
        /// <exception cref="ArgumentNullException">Snapshot is null.</exception>
        public void Restore(FileCabinetServiceSnapshot snapshot, out Dictionary <int, string> exceptions)
        {
            if (snapshot == null)
            {
                throw new ArgumentNullException(nameof(snapshot));
            }

            long position;

            exceptions = new Dictionary <int, string>();
            var recordsFromFile = snapshot.FileCabinetRecords.ToList();

            using (BinaryReader binaryReader = new BinaryReader(this.fileStream, Encoding.Unicode, true))
            {
                this.FillAllDictionaries();

                bool flag    = false;
                int  existId = -1;
                foreach (var record in recordsFromFile)
                {
                    flag = false;
                    foreach (var idkey in this.idpositionPairs.Keys)
                    {
                        if (record.Id == idkey)
                        {
                            flag    = true;
                            existId = idkey;
                            break;
                        }
                    }

                    if (flag)
                    {
                        long existRecordPosition = this.idpositionPairs[existId];
                        this.fileStream.Seek(existRecordPosition, SeekOrigin.Begin);
                    }
                    else
                    {
                        this.fileStream.Seek(0, SeekOrigin.End);
                    }

                    try
                    {
                        if (record.Id <= 0)
                        {
                            throw new ArgumentException(nameof(record.Id));
                        }

                        var recordParameters = new RecordParameters(
                            record.FirstName, record.LastName, record.DateOfBirth, record.Gender, record.Office, record.Salary);
                        this.validator.ValidateParameters(recordParameters);
                        if (this.idpositionPairs.ContainsKey(record.Id))
                        {
                            this.RemoveFromDictionaries(record.Id);
                            this.idpositionPairs.Remove(record.Id);
                        }

                        if (!this.idpositionPairs.ContainsKey(record.Id))
                        {
                            position = this.fileStream.Position;
                            this.idpositionPairs.Add(record.Id, position);
                            this.AddToDictionaries(recordParameters, position);
                        }

                        this.WriteToTheBinaryFile(record);
                    }
                    catch (ArgumentException ex)
                    {
                        exceptions.Add(record.Id, ex.Message);
                    }
                }
            }
        }
        /// <summary>
        /// Makes the snapshot.
        /// </summary>
        /// <returns>The file cabinet service snapshot.</returns>
        public FileCabinetServiceSnapshot MakeSnapshot()
        {
            var snapshot = new FileCabinetServiceSnapshot(this.GetRecords().ToList());

            return(snapshot);
        }