Example #1
0
        public void ShouldGet()
        {
            // Arrange
            string inputFolder      = "ShouldGet";
            string inputFolderPath  = Path.Combine(this.inputBasePath, inputFolder);
            string assertFolderPath = Path.Combine(this.assertBasePath, inputFolder);

            // Act
            using (var storage = new BinaryStorage(new StorageConfiguration()
            {
                WorkingFolder = inputFolderPath
            }))
            {
                Directory.EnumerateFiles(assertFolderPath, "*", SearchOption.AllDirectories)
                .AsParallel().WithDegreeOfParallelism(degreeOfParallelism).ForAll(s =>
                {
                    string fileName = Path.GetFileName(s);
                    using (var resultStream = storage.Get(fileName))
                    {
                        using (var sourceStream = new FileStream(s, FileMode.Open, FileAccess.Read))
                        {
                            if (!AreStreamsEqual(s, sourceStream, resultStream))
                            {
                                // Assert
                                Assert.IsTrue(false);
                            }
                        }
                    }
                });
            }
            // Assert
            Assert.IsTrue(true);
        }
Example #2
0
        internal static void Main(string[] args)
        {
            if (args.Length < 2 ||
                !Directory.Exists(args[0]) ||
                !Directory.Exists(args[1]))
            {
                Console.WriteLine("Usage: Lab.Interview.BinStorage.TestApp.exe InputFolder StorageFolder");
                return;
            }

            // Create storage and add data
            Console.WriteLine("Creating storage from " + args[0]);
            var stopwatch = Stopwatch.StartNew();

            using (var storage = new BinaryStorage(new StorageConfiguration()
            {
                WorkingFolder = args[1]
            })) {
                Directory.EnumerateFiles(args[0], "*", SearchOption.AllDirectories)
                .AsParallel().WithDegreeOfParallelism(4).ForAll(s => {
                    AddFile(storage, s);
                });
            }
            Console.WriteLine("Time to create: " + stopwatch.Elapsed);

            // Open storage and read data
            Console.WriteLine("Verifying data");
            stopwatch = Stopwatch.StartNew();
            using (var storage = new BinaryStorage(new StorageConfiguration()
            {
                WorkingFolder = args[1]
            })) {
                Directory.EnumerateFiles(args[0], "*", SearchOption.AllDirectories)
                .AsParallel().WithDegreeOfParallelism(4).ForAll(s => {
                    using (var ms1 = new MemoryStream()) {
                        storage.Get(s).CopyTo(ms1);
                        using (var ms2 = new MemoryStream()) {
                            using (var file = new FileStream(s, FileMode.Open)) {
                                {
                                    file.CopyTo(ms2);
                                    if (ms2.Length != ms1.Length)
                                    {
                                        throw new Exception();
                                    }
                                }
                            }
                        }
                    }
                });
            }
            Console.WriteLine("Time to verify: " + stopwatch.Elapsed);
        }
Example #3
0
        internal static void Main(string[] args)
        {
            if (args.Length < 2
                || !Directory.Exists(args[0])
                || !Directory.Exists(args[1])) {
                Console.WriteLine("Usage: Lab.Interview.BinStorage.TestApp.exe InputFolder StorageFolder");
                return;
            }

            // Create storage and add data
            Console.WriteLine("Creating storage from " + args[0]);
            var stopwatch = Stopwatch.StartNew();
            using (var storage = new BinaryStorage(new StorageConfiguration() { WorkingFolder = args[1] })) {
                Directory.EnumerateFiles(args[0], "*", SearchOption.AllDirectories)
                    .AsParallel().WithDegreeOfParallelism(4).ForAll(s => {
                        AddFile(storage, s);
                    });

            }
            Console.WriteLine("Time to create: " + stopwatch.Elapsed);

            // Open storage and read data
            Console.WriteLine("Verifying data");
            stopwatch = Stopwatch.StartNew();
            using (var storage = new BinaryStorage(new StorageConfiguration() { WorkingFolder = args[1] })) {
                Directory.EnumerateFiles(args[0], "*", SearchOption.AllDirectories)
                    .AsParallel().WithDegreeOfParallelism(4).ForAll(s => {
                        using (var ms1 = new MemoryStream()) {
                            storage.Get(s).CopyTo(ms1);
                            using (var ms2 = new MemoryStream()) {
                                using (var file = new FileStream(s, FileMode.Open)) {
                                    {
                                        file.CopyTo(ms2);
                                        if (ms2.Length != ms1.Length)
                                            throw new Exception();
                                    }
                                }
                            }
                        }
                    });
            }
            Console.WriteLine("Time to verify: " + stopwatch.Elapsed);
        }
Example #4
0
        public void ShouldGetAndNotDecompressIfAddedCompressed()
        {
            // Arrange
            string inputFolder     = "ShouldGetAndNotDecompressIfAddedCompressed";
            string inputFolderPath = Path.Combine(this.inputBasePath, inputFolder);
            string assertFileName  = "random_bytes.rar";
            string assertFilePath  = Path.Combine(this.assertBasePath, inputFolder, assertFileName);

            // Act
            using (var storage = new BinaryStorage(new StorageConfiguration()
            {
                WorkingFolder = inputFolderPath
            }))
            {
                using (var resultStream = storage.Get(assertFileName))
                {
                    using (var sourceStream = new FileStream(assertFilePath, FileMode.Open, FileAccess.Read))
                    {
                        // Assert
                        Assert.IsTrue(AreStreamsEqual(assertFileName, sourceStream, resultStream));
                    }
                }
            }
        }
Example #5
0
        static void Main(string[] args)
        {
            if (args.Length < 2 ||
                !Directory.Exists(args[0]) ||
                !Directory.Exists(args[1]))
            {
                Console.WriteLine("Usage: Zylab.Interview.BinStorage.TestApp.exe InputFolder StorageFolder");
                return;
            }

            // Create storage and add data
            Console.WriteLine("Creating storage from " + args[0]);
            Stopwatch sw = Stopwatch.StartNew();

            using (var storage = new BinaryStorage(new StorageConfiguration()
            {
                WorkingFolder = args[1]
            }))
            {
                Directory.EnumerateFiles(args[0], "*", SearchOption.AllDirectories)
                .AsParallel().WithDegreeOfParallelism(4).ForAll(s =>
                {
                    AddFile(storage, s);
                });
            }
            Console.WriteLine("Time to create: " + sw.Elapsed);

            // Open storage and read data
            Console.WriteLine("Verifying data");
            sw = Stopwatch.StartNew();
            using (var storage = new BinaryStorage(new StorageConfiguration()
            {
                WorkingFolder = args[1]
            }))
            {
                Directory.EnumerateFiles(args[0], "*", SearchOption.AllDirectories)
                .AsParallel().WithDegreeOfParallelism(4).ForAll(s =>
                {
                    using (var resultStream = storage.Get(s)) {
                        using (var sourceStream = new FileStream(s, FileMode.Open, FileAccess.Read)) {
                            if (sourceStream.Length != resultStream.Length)
                            {
                                throw new Exception(string.Format("Length did not match: Source - '{0}', Result - {1}", sourceStream.Length, resultStream.Length));
                            }

                            byte[] hash1, hash2;
                            using (MD5 md5 = MD5.Create()) {
                                hash1 = md5.ComputeHash(sourceStream);

                                md5.Initialize();
                                hash2 = md5.ComputeHash(resultStream);
                            }

                            if (!hash1.SequenceEqual(hash2))
                            {
                                throw new Exception(string.Format("Hashes do not match for file - '{0}'  ", s));
                            }
                        }
                    }
                });
            }
            Console.WriteLine("Time to verify: " + sw.Elapsed);
        }
Example #6
0
        static void Main(string[] args)
        {
            string sourceFolder        = @".\_input";
            string destFolder          = @".\_output";
            int    degreeOfParallelism = 12;
            int    count = 0;

            // Create storage and add data
            Console.WriteLine("Creating storage from " + sourceFolder);
            Stopwatch sw = Stopwatch.StartNew();

            using (var storage = new BinaryStorage(new StorageConfiguration()
            {
                WorkingFolder = destFolder
            }))
            {
                Directory.EnumerateFiles(sourceFolder, "*", SearchOption.AllDirectories)
                .AsParallel().WithDegreeOfParallelism(degreeOfParallelism).ForAll(s =>
                {
                    AddFile(storage, s);
                });
            }
            Console.WriteLine("Time to create: " + sw.Elapsed);

            // Open storage and read data
            Console.WriteLine("Verifying data");
            sw = Stopwatch.StartNew();
            using (var storage = new BinaryStorage(new StorageConfiguration()
            {
                WorkingFolder = destFolder
            }))
            {
                Directory.EnumerateFiles(sourceFolder, "*", SearchOption.AllDirectories)
                .AsParallel().WithDegreeOfParallelism(degreeOfParallelism).ForAll(s =>
                {
                    string key = Path.GetFileName(s);
                    using (var resultStream = storage.Get(key)) {
                        using (var sourceStream = new FileStream(s, FileMode.Open, FileAccess.Read)) {
                            if (sourceStream.Length != resultStream.Length)
                            {
                                throw new Exception(string.Format("Length did not match: Source - '{0}', Result - {1}", sourceStream.Length, resultStream.Length));
                            }

                            byte[] hash1, hash2;
                            using (MD5 md5 = MD5.Create()) {
                                hash1 = md5.ComputeHash(sourceStream);

                                md5.Initialize();
                                hash2 = md5.ComputeHash(resultStream);
                            }

                            if (!hash1.SequenceEqual(hash2))
                            {
                                throw new Exception(string.Format("Hashes do not match for file - '{0}'  ", s));
                            }
                        }
                    }
                    Interlocked.Add(ref count, 1);
                });
                Console.WriteLine("Time to verify: " + sw.Elapsed + ", count = " + count);

                Console.WriteLine("Verifying data 2nd time");
                sw    = Stopwatch.StartNew();
                count = 0;

                Directory.EnumerateFiles(sourceFolder, "*", SearchOption.AllDirectories)
                .AsParallel().WithDegreeOfParallelism(degreeOfParallelism).ForAll(s =>
                {
                    string key = Path.GetFileName(s);
                    using (var resultStream = storage.Get(key))
                    {
                        using (var sourceStream = new FileStream(s, FileMode.Open, FileAccess.Read))
                        {
                            if (sourceStream.Length != resultStream.Length)
                            {
                                throw new Exception(string.Format("Length did not match: Source - '{0}', Result - {1}", sourceStream.Length, resultStream.Length));
                            }

                            byte[] hash1, hash2;
                            using (MD5 md5 = MD5.Create())
                            {
                                hash1 = md5.ComputeHash(sourceStream);

                                md5.Initialize();
                                hash2 = md5.ComputeHash(resultStream);
                            }

                            if (!hash1.SequenceEqual(hash2))
                            {
                                throw new Exception(string.Format("Hashes do not match for file - '{0}'  ", s));
                            }
                        }
                    }
                    Interlocked.Add(ref count, 1);
                });
                Console.WriteLine("Time to verify: " + sw.Elapsed + ", count = " + count);
            }

            Console.ReadLine();
        }