Beispiel #1
0
        public void ReadMessages(long from, long till, int maxCount, Action <MessageWithId> action)
        {
            Require.ZeroOrGreater("from", from);
            Require.ZeroOrGreater("maxOffset", till);
            Require.Positive("maxCount", maxCount);

            var count = 0;

            using (var prs = new PageReadStream(Messages, from, till, _buffer))
            {
                using (var bin = new BinaryReader(prs))
                {
                    while (prs.Position < prs.Length)
                    {
                        var message = StorageFormat.Read(bin);
                        action(message);
                        count += 1;
                        if (count >= maxCount)
                        {
                            break;
                        }
                    }
                }
            }
        }
Beispiel #2
0
        public MessageResult ReadMessages(long from, long till, int maxCount)
        {
            Require.ZeroOrGreater("from", from);
            Require.ZeroOrGreater("maxOffset", till);
            Require.Positive("maxCount", maxCount);

            var list     = new List <MessageWithId>(maxCount);
            var position = from;

            using (var prs = new PageReadStream(Messages, from, till, _buffer)) {
                using (var bin = new BinaryReader(prs)) {
                    while (prs.Position < prs.Length)
                    {
                        var message = StorageFormat.Read(bin);
                        list.Add(message);
                        position = prs.Position;
                        if (list.Count >= maxCount)
                        {
                            break;
                        }
                    }
                }
            }
            return(new MessageResult(list, position));
        }
Beispiel #3
0
        void RunSubscription(
            Subscription sub,
            long position,
            CancellationToken ct,
            int bufferSize,
            int cacheSize
            )
        {
            sub.DebugStartPosition = position;
            var buffer = new byte[bufferSize];

            // forever try
            while (!ct.IsCancellationRequested)
            {
                try {
                    // read current max length
                    var length = Position.Read();
                    sub.DebugKnownMaxOffset = length;
                    using (var prs = new PageReadStream(Messages, position, length, buffer)) {
                        using (var bin = new BinaryReader(prs)) {
                            while (prs.Position < prs.Length)
                            {
                                var message = StorageFormat.Read(bin);
                                sub.Buffer.Enqueue(message);
                                sub.DebugEnqueuedOffset = prs.Position;
                                position = prs.Position;

                                while (sub.Buffer.Count >= cacheSize)
                                {
                                    ct.WaitHandle.WaitOne(500);
                                }
                            }
                        }
                    }
                    // wait till we get chance to advance
                    while (Position.Read() == position)
                    {
                        if (ct.WaitHandle.WaitOne(1000))
                        {
                            return;
                        }
                    }
                }
                catch (ForbiddenException) {
                    throw;
                }
                catch (Exception ex) {
                    Debug.Print("Exception {0}", ex);
                    ct.WaitHandle.WaitOne(1000 * 5);
                }
            }
        }
        public MessageResult ReadMessages(long from, long till, int maxCount) {
            Require.ZeroOrGreater("from", from);
            Require.ZeroOrGreater("maxOffset", till);
            Require.Positive("maxCount", maxCount);

            var list = new List<MessageWithId>(maxCount);
            var position = from;

            using (var prs = new PageReadStream(_messages, from, till, _buffer)) {
                using (var bin = new BinaryReader(prs)) {
                    while (prs.Position < prs.Length) {
                        var message = StorageFormat.Read(bin);
                        list.Add(message);
                        position = prs.Position;
                        if (list.Count >= maxCount) {
                            break;
                        }
                    }
                }
            }
            return new MessageResult(list, position);
        }
        void RunSubscription(
            Subscription sub,
            long position,
            CancellationToken ct,
            int bufferSize,
            int cacheSize
            ) {

	        sub.DebugStartPosition = position;
            var buffer = new byte[bufferSize];
            // forever try
            while (!ct.IsCancellationRequested) {
                try {
                    // read current max length
                    var length = _position.Read();
	                sub.DebugKnownMaxOffset = length;
                    using (var prs = new PageReadStream(_messages, position, length, buffer)) {
                        using (var bin = new BinaryReader(prs)) {
                            while (prs.Position < prs.Length) {
                                var message = StorageFormat.Read(bin);
                                sub.Buffer.Enqueue(message);
	                            sub.DebugEnqueuedOffset = prs.Position;
                                position = prs.Position;

                                while (sub.Buffer.Count >= cacheSize) {
                                    ct.WaitHandle.WaitOne(500);
                                }
                            }
                        }
                    }
                    // wait till we get chance to advance
                    while (_position.Read() == position)
                    {
                        if (ct.WaitHandle.WaitOne(1000)) {
                            return;
                        }
                    }
                }
                catch (Exception ex) {
                    Debug.Print("Exception {0}", ex);
                    ct.WaitHandle.WaitOne(1000*5);
                }
            }
        }