Ejemplo n.º 1
0
        public void ScatterGatherStreamFactory_CreateScatterGatherStream_WhereCapacityIsNegative_Throws()
        {
            var scatterStream = default(Stream);
            var gatherStream  = default(Stream);

            ScatterGatherStreamFactory.CreateScatterGatherStreams(-1, out scatterStream, out gatherStream);
        }
Ejemplo n.º 2
0
        public void ScatterGatherStreamFactory_CreateScatterGatherStream_WhereTimeoutIsNegative_Throws()
        {
            var scatterStream = default(Stream);
            var gatherStream  = default(Stream);

            ScatterGatherStreamFactory.CreateScatterGatherStreams(100, TimeSpan.FromMilliseconds(-2), out scatterStream, out gatherStream);
        }
        public void ScatterGatherStreamFactory_CreateScatterGatherStreams_WhereGatherStreamsIsNumm_Throws()
        {
            var scatterStream = default(Stream);
            var gatherStreams = default(Stream[]);

            ScatterGatherStreamFactory.CreateScatterGatherStreams(100, out scatterStream, gatherStreams);
        }
Ejemplo n.º 4
0
        public NtStatus SetAllocationSize(string fileName, long length, DokanFileInfo info)
        {
            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }

            var context = (StreamContext)info.Context;

            if (length > 0 && context.Stream == null)
            {
                var scatterStream = default(Stream);
                var gatherStream  = default(Stream);
                ScatterGatherStreamFactory.CreateScatterGatherStreams((int)length, out scatterStream, out gatherStream);

                context.Stream = scatterStream;

                context.Task = Task.Run(() => {
                    try {
                        context.File.SetContent(drive, gatherStream);
                    } catch (Exception ex) {
                        if (!(ex is UnauthorizedAccessException))
                        {
                            context.File.Remove(drive);
                        }
                        logger.Trace($"{nameof(context.File.SetContent)} failed on file '{fileName}' with {ex.GetType().Name} '{ex.Message}'".ToString(CultureInfo.CurrentCulture));
                        throw;
                    }
                })
                               .ContinueWith(t => logger.Trace($"{nameof(context.File.SetContent)} finished on file '{fileName}'".ToString(CultureInfo.CurrentCulture)), TaskContinuationOptions.OnlyOnRanToCompletion);
            }

            return(Trace(nameof(SetAllocationSize), fileName, info, DokanResult.Success, length.ToString(CultureInfo.InvariantCulture)));
        }
            public void WriteBufferConcurrently(byte[] sourceBuffer, byte[] targetBuffer, int writeChunkSize, TimeSpan writeDelay, TimeSpan timeout, out Stream gatherStream)
            {
                var scatterStream = default(Stream);

                ScatterGatherStreamFactory.CreateScatterGatherStreams(targetBuffer.Length, timeout, out scatterStream, out gatherStream);
                var writeTask = WriteAsync(scatterStream, sourceBuffer, TimeSpan.Zero, writeChunkSize, writeDelay);

                writeTask.Wait(timeout);
            }
            public byte[] ReadBufferConcurrently(byte[] sourceBuffer, int readChunkSize, TimeSpan readDelay, TimeSpan timeout, out Stream scatterStream)
            {
                var targetBuffer = new byte[sourceBuffer.Length];

                var gatherStream = default(Stream);

                ScatterGatherStreamFactory.CreateScatterGatherStreams(sourceBuffer.Length, timeout, out scatterStream, out gatherStream);
                var readTask = ReadAsync(gatherStream, targetBuffer, TimeSpan.Zero, readChunkSize, readDelay);

                readTask.Wait(timeout);

                return(targetBuffer);
            }
        public void ScatterStream_SetCapacity_SetsMatchingCapacityOnAllGatherStreams()
        {
            var size = 1000;

            var scatterStream = default(Stream);
            var gatherStreams = new Stream[5];

            ScatterGatherStreamFactory.CreateScatterGatherStreams(size, out scatterStream, gatherStreams);

            var changedSize = size / 4;

            ((ScatterStream)scatterStream).Capacity = changedSize;

            Assert.IsTrue(gatherStreams.Cast <GatherStream>().All(s => s.Capacity == changedSize));
        }
        public void ScatterStream_SetCapacity_SetsMatchingCapacityOnGatherStream()
        {
            var size = 1000;

            var scatterStream = default(Stream);
            var gatherStream  = default(Stream);

            ScatterGatherStreamFactory.CreateScatterGatherStreams(size, out scatterStream, out gatherStream);

            var changedSize = size / 4;

            ((ScatterStream)scatterStream).Capacity = changedSize;

            Assert.AreEqual(changedSize, ((GatherStream)gatherStream).Capacity);
        }
            public byte[] CopyBufferConcurrentlyByPermutation(byte[] sourceBuffer, TimeSpan initialWriteDelay, int writeChunkSize, TimeSpan writeDelay, int[] writePermutation, TimeSpan initialReadDelay, int readChunkSize, TimeSpan readDelay, int[] readPermutation, bool flush = true)
            {
                var targetBuffer = new byte[sourceBuffer.Length];

                var scatterStream = default(Stream);
                var gatherStream  = default(Stream);

                ScatterGatherStreamFactory.CreateScatterGatherStreams(sourceBuffer.Length, out scatterStream, out gatherStream);

                var writeTask = WriteAsync(scatterStream, sourceBuffer, initialWriteDelay, writeChunkSize, writeDelay, writePermutation, flush);
                var readTask  = ReadAsync(gatherStream, targetBuffer, initialReadDelay, readChunkSize, readDelay, readPermutation);

                Task.WaitAll(new Task[] { writeTask, readTask });

                return(targetBuffer);
            }
            public byte[][] CopyBuffersConcurrently(byte[] sourceBuffer, int results, TimeSpan initialWriteDelay, int writeChunkSize, TimeSpan writeDelay, TimeSpan initialReadDelay, int readChunkSize, TimeSpan readDelay, bool flush = true)
            {
                var targetBuffers = Enumerable.Range(0, results).Select(i => new byte[sourceBuffer.Length]).ToArray();

                var scatterStream = default(Stream);
                var gatherStreams = new Stream[results];

                ScatterGatherStreamFactory.CreateScatterGatherStreams(sourceBuffer.Length, out scatterStream, gatherStreams);

                var writeTask = WriteAsync(scatterStream, sourceBuffer, initialWriteDelay, writeChunkSize, writeDelay, flush: flush);
                var readTasks = Enumerable.Range(0, results).Select(i => ReadAsync(gatherStreams[i], targetBuffers[i], initialReadDelay, readChunkSize, readDelay));

                Task.WaitAll(new Task[] { writeTask }.Concat(readTasks).ToArray());

                return(targetBuffers);
            }
            public static byte[][] CopyBuffersConcurrentlyByPermutation(byte[] sourceBuffer, BlockMap.Block[] writePermutation, BlockMap.Block[][] readPermutations)
            {
                var targetBuffers = Enumerable.Range(0, readPermutations.Length).Select(i => new byte[sourceBuffer.Length]).ToArray();

                var scatterStream = default(Stream);
                var gatherStreams = new Stream[readPermutations.Length];

                ScatterGatherStreamFactory.CreateScatterGatherStreams(sourceBuffer.Length, out scatterStream, gatherStreams);

                var writeTask = WriteAsync(scatterStream, writePermutation, sourceBuffer);
                var readTasks = Enumerable.Range(0, readPermutations.Length).Select(i => ReadAsync(gatherStreams[i], readPermutations[i], targetBuffers[i]));

                Task.WaitAll(new Task[] { writeTask }.Concat(readTasks).ToArray());

                return(targetBuffers);
            }
            public static byte[] CopyBufferConcurrentlyByPermutation(byte[] sourceBuffer, BlockMap.Block[] writePermutation, BlockMap.Block[] readPermutation)
            {
                var targetBuffer = new byte[sourceBuffer.Length];

                var scatterStream = default(Stream);
                var gatherStream  = default(Stream);

                ScatterGatherStreamFactory.CreateScatterGatherStreams(sourceBuffer.Length, out scatterStream, out gatherStream);

                var writeTask = WriteAsync(scatterStream, writePermutation, sourceBuffer);
                var readTask  = ReadAsync(gatherStream, readPermutation, targetBuffer);

                Task.WaitAll(new Task[] { writeTask, readTask });

                return(targetBuffer);
            }
Ejemplo n.º 13
0
        public NtStatus SetAllocationSize(string fileName, long length, DokanFileInfo info)
        {
            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }

            var context = (StreamContext)info.Context;

            if (length > 0)
            {
                if (context.Stream == null)
                {
                    var gatherStreams = new Stream[2];
                    ScatterGatherStreamFactory.CreateScatterGatherStreams((int)length, out Stream scatterStream, gatherStreams);

                    context.Stream = new ReadWriteSegregatingStream(scatterStream, gatherStreams[1]);

                    context.Task = Task.Run(() => {
                        try
                        {
                            context.File.SetContent(_drive, gatherStreams[0]);
                        }
                        catch (Exception ex)
                        {
                            if (!(ex is UnauthorizedAccessException))
                            {
                                context.File.Remove(_drive);
                            }
                            _logger.Error($"{nameof(context.File.SetContent)} failed on file '{fileName}' with {ex.GetType().Name} '{ex.Message}'".ToString(CultureInfo.CurrentCulture));
                            throw;
                        }
                    })
                                   .ContinueWith(t => _logger.Debug($"{nameof(context.File.SetContent)} finished on file '{fileName}'".ToString(CultureInfo.CurrentCulture)), TaskContinuationOptions.OnlyOnRanToCompletion);
                }
                else
                {
                    if ((context.Stream as ReadWriteSegregatingStream)?.WriteStream is ScatterStream scatterStream)
                    {
                        scatterStream.Capacity = (int)length;
                    }
                }
            }

            return(AsDebug(nameof(SetAllocationSize), fileName, info, DokanResult.Success, length.ToString(CultureInfo.InvariantCulture)));
        }