public void Read_ZeroLength_ReturnsEmptyArray()
        {
            var stream    = new MemoryStream(new byte[10]);
            var container = new StreamContainer(stream);

            CollectionAssert.AreEqual(container.Read(0, 0), new byte[0]);
        }
        public void Read_TooCloseToEnd_ReturnsTruncatedArray()
        {
            var stream    = new MemoryStream(new byte[10]);
            var container = new StreamContainer(stream);

            CollectionAssert.AreEqual(container.Read(5, 10), new byte[5]);
        }
        public void AddOrUpdateStream(StreamInfo streamInfo, int maxItemsInQueue)
        {
            streamInfo.Validate();
            StreamContainer container = new StreamContainer(streamInfo, maxItemsInQueue);

            m_containers[streamInfo.ID] = container;
        }
        public void Length_ReturnsLengthOfGivenArray(int length)
        {
            var stream    = new MemoryStream(new byte[length]);
            var container = new StreamContainer(stream);

            Assert.That(container.Length, Is.EqualTo(length));
        }
        public void Read_NegativeOffset_ThrowsArgumentOutOfRangeException()
        {
            var stream    = new MemoryStream();
            var container = new StreamContainer(stream);

            Assert.Throws <ArgumentOutOfRangeException>(() => container.Read(-1, 0));
        }
        public void Read_BeyondEnd_ReturnsEmptyArray()
        {
            var stream    = new MemoryStream(new byte[10]);
            var container = new StreamContainer(stream);

            CollectionAssert.AreEqual(container.Read(20, 10), new byte[0]);
        }
        public void Read_WithBogusStream_ReturnsEmptyArray()
        {
            var stream    = new BogusStream();
            var container = new StreamContainer(stream);

            CollectionAssert.AreEqual(container.Read(0, 10), new byte[0]);
        }
            public void AddReader(StreamReader reader)
            {
                var head = new StreamContainer()
                {
                    Reader = reader, Container = Container
                };

                Container = head;
            }
Exemplo n.º 9
0
        public Client(StreamContainer container)
        {
            if (container == null)
            {
                ArgumentNullException ex = new ArgumentNullException();
                Logger.Instance.WriteLog("Failed at client init: " + ex.ToString());
                throw ex;
            }

            streamContainer = container;
        }
Exemplo n.º 10
0
        public Client(IClientListener listener, StreamContainer container)
        {
            if (listener == null || container == null)
            {
                ArgumentNullException ex = new ArgumentNullException();
                Logger.Instance.WriteLog("Failed at client init: " + ex.ToString());
                throw ex;
            }

            this.listener   = listener;
            streamContainer = container;
        }
        public void MergeChunks(string outputFilePath, string[] chunkFiles)
        {
            Console.WriteLine(nameof(SortedDictionaryMergeStrategy));
            using (var writer = File.CreateText(outputFilePath))
            {
                List <StreamReader> readers = null;
                try
                {
                    readers = chunkFiles.Select(s => new StreamReader(s, Encoding.UTF8)).ToList();
                    SortedDictionary <string, StreamContainer> kWayMerger
                        = new SortedDictionary <string, StreamContainer>(_comparer);

                    // Here SortedDictionary is used as a priority queue for k-Way merge. The key is string, and the value
                    // is a linked list of readers this string came from. Approach is similar as in PriorityQueueMergeStrategy,
                    // but the difference is that here there could be multiple readers for the same line.
                    // It could be beneficial if there are many identical strings
                    // At first read a line from each stream
                    readers.ForEach(reader => AddLineForMerging(kWayMerger, reader, reader.ReadLine()));


                    while (kWayMerger.Count > 0)
                    {
                        var pair = kWayMerger.First();
                        kWayMerger.Remove(pair.Key);

                        string          lineToWrite    = pair.Key;
                        StreamContainer readersForLine = pair.Value;

                        // Write minimal line from the dictionary to the output for each reader in the list
                        do
                        {
                            StreamReader reader = readersForLine.Reader;
                            readersForLine = readersForLine.Container;

                            writer.WriteLine(lineToWrite);
                            AddLineForMerging(kWayMerger, reader, reader.ReadLine());
                        }while (readersForLine != null);
                    }
                }
                finally
                {
                    readers?.ForEach(r => r.Dispose());
                }
            }

            foreach (var path in chunkFiles)
            {
                File.Delete(path);
            }
        }
Exemplo n.º 12
0
        public async Task WritesReadsNewStream()
        {
            var hydra     = CreateHydra();
            var container = new StreamContainer(hydra);
            var subject   = new Stream(container);

            var id        = Guid.NewGuid().ToString();
            var eventData = "{ \"name\": \"john\" }";

            await subject.WriteEventAsync("key", id, eventData);

            var persisted = await subject.ReadEventsAsync("key", id);

            Assert.Equal(eventData, persisted.First());
        }
Exemplo n.º 13
0
        public void Read_Contents_ReturnsCorrectContents()
        {
            var stream = new MemoryStream(
                new byte[]
            {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10
            });
            var container = new StreamContainer(stream);

            CollectionAssert.AreEqual(
                container.Read(5, 10), new byte[]
            {
                6, 7, 8, 9, 10
            });
        }
Exemplo n.º 14
0
        public object OpenStreamChannel(ConnectionBase connection, ExecuteCommand executeCommand, Type parameterType)
        {
            StreamContainer streamContainer = new StreamContainer(connection.Id, parameterType);

            Guid streamId = Guid.NewGuid();

            streamContainers.TryAdd(streamId, streamContainer);

            _ = connection.Send(new InitStreamResponse()
            {
                ReferenceId = executeCommand.ReferenceId,
                Id          = streamId
            });

            CloseOldStreamChannels();

            return(streamContainer.AsyncEnumerableValue);
        }
        private static void AddLineForMerging(SortedDictionary <string, StreamContainer> kWayMerger, StreamReader reader, string line)
        {
            if (line == null)
            {
                return;
            }

            if (kWayMerger.ContainsKey(line))
            {
                kWayMerger[line].AddReader(reader);
            }
            else
            {
                kWayMerger[line] = new StreamContainer()
                {
                    Reader = reader
                };
            }
        }
Exemplo n.º 16
0
        public Stream GetStream()
        {
            if (disposed != 0)
            {
                throw new ObjectDisposedException("StreamFactory");
            }
            int threadId = Thread.CurrentThread.ManagedThreadId;

            StreamContainer res = containers.FirstOrDefault(c => c.Lock(threadId));

            if (res == null)
            {
                res = new StreamContainer(CreateNew());
                res.Lock(threadId);
                containers.Add(res);
            }

            return(res);
        }
Exemplo n.º 17
0
        public async Task WritesReadsExistingStream()
        {
            var hydra     = CreateHydra();
            var container = new StreamContainer(hydra);
            var subject   = new Stream(container);

            var id         = Guid.NewGuid().ToString();
            var eventData1 = "{ \"name\": \"john\" }";
            var eventData2 = "{ \"lastname\": \"doe\" }";

            await subject.WriteEventAsync("key", id, eventData1);

            await subject.WriteEventAsync("key", id, eventData2);

            var persisted = await subject.ReadEventsAsync("key", id);

            Assert.Equal(eventData1, persisted[0]);
            Assert.Equal(eventData2, persisted[1]);
        }
        public static void Run()
        {
            // To get proper output please apply a valid Aspose.Imaging License. You can purchase full license or get 30 day temporary license from http:// Www.aspose.com/purchase/default.aspx."

            // ExStart: SyncRootProperty

            // Create an instance of Memory stream class.
            using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
            {
                // Create an instance of Stream container class and assign memory stream object.
                using (StreamContainer streamContainer = new StreamContainer(memoryStream))
                {
                    // check if the access to the stream source is synchronized.
                    lock (streamContainer.SyncRoot)
                    {
                        // do work
                        // now access to source MemoryStream is synchronized
                    }
                }
            }

            // ExEnd: SyncRootProperty
        }
Exemplo n.º 19
0
        public static void Run()
        {
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_PSD();

            //ExStart:SyncRoot

            // Create an instance of Memory stream class.
            using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
            {
                // Create an instance of Stream container class and assign memory stream object.
                using (StreamContainer streamContainer = new StreamContainer(memoryStream))
                {
                    // check if the access to the stream source is synchronized.
                    lock (streamContainer.SyncRoot)
                    {
                        // do work
                        // now access to source MemoryStream is synchronized
                    }
                }
            }

            //ExEnd:SyncRoot
        }
Exemplo n.º 20
0
 public void AddStreamController(StreamContainer container) => streamContainer = container;