コード例 #1
0
ファイル: MessageReader.cs プロジェクト: junxie6/messageVault
        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;
                        }
                    }
                }
            }
        }
コード例 #2
0
ファイル: MessageReader.cs プロジェクト: junxie6/messageVault
        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));
        }
コード例 #3
0
ファイル: MessageReader.cs プロジェクト: junxie6/messageVault
        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);
                }
            }
        }
コード例 #4
0
        public AppendResult Append(ICollection <Message> messages)
        {
            if (messages.Count == 0)
            {
                throw new ArgumentException("Must provide non-empty array", "messages");
            }
            var ids = new List <MessageId>(messages.Count);

            foreach (var item in messages)
            {
                if (item.Value.Length > Constants.MaxValueSize)
                {
                    string message = "Each message must be smaller than " + Constants.MaxValueSize;
                    throw new InvalidOperationException(message);
                }

                if (item.Key.Length > Constants.MaxKeySize)
                {
                    var message = "Each contract must be shorter than " + Constants.MaxKeySize;
                    throw new InvalidOperationException(message);
                }

                var sizeEstimate = StorageFormat.EstimateSize(item);

                var availableInBuffer = _stream.Length - _stream.Position;
                if (sizeEstimate > availableInBuffer)
                {
                    FlushBuffer();
                }

                var offset = VirtualPosition();
                var id     = MessageId.CreateNew(offset);
                StorageFormat.Write(_binary, id, item);
                ids.Add(id);
            }
            FlushBuffer();

            _positionWriter.Update(_position);

            return(new AppendResult(ids, _position));
        }