Exemplo n.º 1
0
        /// <summary>
        /// Get offset from cached message.  Left to derived class, as only it knows how to get this from the cached message.
        /// </summary>
        public virtual string GetOffset(CachedMessage lastItemPurged)
        {
            // TODO figure out how to get this from the adapter
            int readOffset = 0;

            return(SegmentBuilder.ReadNextString(lastItemPurged.Segment, ref readOffset)); // read offset
        }
Exemplo n.º 2
0
        // Placed object message payload into a segment from a buffer pool.  When this get's too big, older blocks will be purged
        private ArraySegment <byte> SerializeMessageIntoPooledSegment(EventData queueMessage)
        {
            byte[] payloadBytes    = queueMessage.GetBytes();
            string streamNamespace = queueMessage.GetStreamNamespaceProperty();
            int    size            = SegmentBuilder.CalculateAppendSize(streamNamespace) +
                                     SegmentBuilder.CalculateAppendSize(queueMessage.Offset) +
                                     SegmentBuilder.CalculateAppendSize(payloadBytes);

            // get segment from current block
            ArraySegment <byte> segment;

            if (currentBuffer == null || !currentBuffer.TryGetSegment(size, out segment))
            {
                // no block or block full, get new block and try again
                currentBuffer = bufferPool.Allocate();
                currentBuffer.SetPurgeAction(PurgeAction);
                // if this fails with clean block, then requested size is too big
                if (!currentBuffer.TryGetSegment(size, out segment))
                {
                    string errmsg = String.Format(CultureInfo.InvariantCulture,
                                                  "Message size is to big. MessageSize: {0}", size);
                    throw new ArgumentOutOfRangeException("queueMessage", errmsg);
                }
            }
            // encode namespace, offset, and payload into segment
            int writeOffset = 0;

            SegmentBuilder.Append(segment, ref writeOffset, streamNamespace);
            SegmentBuilder.Append(segment, ref writeOffset, queueMessage.Offset);
            SegmentBuilder.Append(segment, ref writeOffset, payloadBytes);

            return(segment);
        }
Exemplo n.º 3
0
        // Placed object message payload into a segment.
        private ArraySegment <byte> EncodeMessageIntoSegment(StreamPosition streamPosition, EventData queueMessage)
        {
            byte[] propertiesBytes = queueMessage.SerializeProperties();
            byte[] payload         = queueMessage.GetBytes();
            // get size of namespace, offset, partitionkey, properties, and payload
            int size = SegmentBuilder.CalculateAppendSize(streamPosition.StreamIdentity.Namespace) +
                       SegmentBuilder.CalculateAppendSize(queueMessage.Offset) +
                       SegmentBuilder.CalculateAppendSize(queueMessage.PartitionKey) +
                       SegmentBuilder.CalculateAppendSize(propertiesBytes) +
                       SegmentBuilder.CalculateAppendSize(payload);

            // get segment
            ArraySegment <byte> segment = GetSegment(size);

            // encode namespace, offset, partitionkey, properties and payload into segment
            int writeOffset = 0;

            SegmentBuilder.Append(segment, ref writeOffset, streamPosition.StreamIdentity.Namespace);
            SegmentBuilder.Append(segment, ref writeOffset, queueMessage.Offset);
            SegmentBuilder.Append(segment, ref writeOffset, queueMessage.PartitionKey);
            SegmentBuilder.Append(segment, ref writeOffset, propertiesBytes);
            SegmentBuilder.Append(segment, ref writeOffset, payload);

            return(segment);
        }
Exemplo n.º 4
0
        protected override string GetOffset(CachedEventHubMessage lastItemPurged)
        {
            int readOffset = 0;

            SegmentBuilder.ReadNextString(lastItemPurged.Segment, ref readOffset);         // read namespace, not needed so throw away.
            return(SegmentBuilder.ReadNextString(lastItemPurged.Segment, ref readOffset)); // read offset
        }
Exemplo n.º 5
0
        private void OnPurged(CachedEventHubMessage lastItemPurged)
        {
            int readOffset = 0;

            SegmentBuilder.ReadNextString(lastItemPurged.Segment, ref readOffset);                 // read namespace, not needed so throw away.
            string offset = SegmentBuilder.ReadNextString(lastItemPurged.Segment, ref readOffset); // read offset

            checkpoint.Update(offset, DateTime.UtcNow);
        }
Exemplo n.º 6
0
        public IBatchContainer GetBatchContainer(ref CachedEventHubMessage cachedMessage)
        {
            int    readOffset           = 0;
            string streamNamespace      = SegmentBuilder.ReadNextString(cachedMessage.Segment, ref readOffset);
            string offset               = SegmentBuilder.ReadNextString(cachedMessage.Segment, ref readOffset);
            ArraySegment <byte> payload = SegmentBuilder.ReadNextBytes(cachedMessage.Segment, ref readOffset);

            return(new EventHubBatchContainer(cachedMessage.StreamGuid, streamNamespace, offset, cachedMessage.SequenceNumber, payload.ToArray()));
        }
Exemplo n.º 7
0
        public bool IsInStream(ref CachedEventHubMessage cachedMessage, Guid streamGuid, string streamNamespace)
        {
            // fail out early if guids does not match.  Don't incur cost of decoding namespace unless necessary.
            if (cachedMessage.StreamGuid != streamGuid)
            {
                return(false);
            }
            int    readOffset             = 0;
            string decodedStreamNamespace = SegmentBuilder.ReadNextString(cachedMessage.Segment, ref readOffset);

            return(decodedStreamNamespace == streamNamespace);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Duplicate of EventHub's EventData class.
        /// </summary>
        /// <param name="cachedMessage"></param>
        public EventHubMessage(CachedEventHubMessage cachedMessage)
        {
            int readOffset = 0;

            StreamIdentity = new StreamIdentity(cachedMessage.StreamGuid, SegmentBuilder.ReadNextString(cachedMessage.Segment, ref readOffset));
            Offset         = SegmentBuilder.ReadNextString(cachedMessage.Segment, ref readOffset);
            PartitionKey   = SegmentBuilder.ReadNextString(cachedMessage.Segment, ref readOffset);
            SequenceNumber = cachedMessage.SequenceNumber;
            EnqueueTimeUtc = cachedMessage.EnqueueTimeUtc;
            DequeueTimeUtc = cachedMessage.DequeueTimeUtc;
            Properties     = SegmentBuilder.ReadNextBytes(cachedMessage.Segment, ref readOffset).DeserializeProperties();
            Payload        = SegmentBuilder.ReadNextBytes(cachedMessage.Segment, ref readOffset).ToArray();
        }
Exemplo n.º 9
0
        public bool Equals(CachedEventHubMessage cachedMessage, IStreamIdentity streamIdentity)
        {
            int result = cachedMessage.StreamGuid.CompareTo(streamIdentity.Guid);

            if (result != 0)
            {
                return(false);
            }

            int    readOffset             = 0;
            string decodedStreamNamespace = SegmentBuilder.ReadNextString(cachedMessage.Segment, ref readOffset);

            return(string.Compare(decodedStreamNamespace, streamIdentity.Namespace, StringComparison.Ordinal) == 0);
        }
Exemplo n.º 10
0
        public EventHubMessage(CachedEventHubMessage cachedMessage)
        {
            int readOffset = 0;

            StreamIdentity = new StreamIdentity(cachedMessage.StreamGuid, SegmentBuilder.ReadNextString(cachedMessage.Segment, ref readOffset));
            Offset         = SegmentBuilder.ReadNextString(cachedMessage.Segment, ref readOffset);
            SequenceNumber = cachedMessage.SequenceNumber;
            EnqueueTimeUtc = cachedMessage.EnqueueTimeUtc;
            DequeueTimeUtc = cachedMessage.DequeueTimeUtc;
            Properties     = SegmentBuilder.ReadNextBytes(cachedMessage.Segment, ref readOffset).DeserializeProperties();
            object offsetObj;

            PartitionKey = Properties.TryGetValue("PartitionKey", out offsetObj)
                ? offsetObj as string
                : default(string);
            Payload = SegmentBuilder.ReadNextBytes(cachedMessage.Segment, ref readOffset).ToArray();
        }
Exemplo n.º 11
0
        // Placed object message payload into a segment.
        private ArraySegment <byte> EncodeMessageIntoSegment(StreamPosition streamPosition, EventData queueMessage)
        {
            byte[] propertiesBytes = queueMessage.SerializeProperties(this.serializationManager);
#if !BUILD_FLAVOR_LEGACY
            byte[] payload = queueMessage.Body.Array;
#else
            byte[] payload = queueMessage.GetBytes();
#endif
            // get size of namespace, offset, partitionkey, properties, and payload
            int size = SegmentBuilder.CalculateAppendSize(streamPosition.StreamIdentity.Namespace) +
#if !BUILD_FLAVOR_LEGACY
                       SegmentBuilder.CalculateAppendSize(queueMessage.SystemProperties.Offset) +
                       SegmentBuilder.CalculateAppendSize(queueMessage.SystemProperties.PartitionKey) +
#else
                       SegmentBuilder.CalculateAppendSize(queueMessage.Offset) +
                       SegmentBuilder.CalculateAppendSize(queueMessage.PartitionKey) +
#endif
                       SegmentBuilder.CalculateAppendSize(propertiesBytes) +
                       SegmentBuilder.CalculateAppendSize(payload);

            // get segment
            ArraySegment <byte> segment = GetSegment(size);

            // encode namespace, offset, partitionkey, properties and payload into segment
            int writeOffset = 0;
            SegmentBuilder.Append(segment, ref writeOffset, streamPosition.StreamIdentity.Namespace);
#if !BUILD_FLAVOR_LEGACY
            SegmentBuilder.Append(segment, ref writeOffset, queueMessage.SystemProperties.Offset);
            SegmentBuilder.Append(segment, ref writeOffset, queueMessage.SystemProperties.PartitionKey);
#else
            SegmentBuilder.Append(segment, ref writeOffset, queueMessage.Offset);
            SegmentBuilder.Append(segment, ref writeOffset, queueMessage.PartitionKey);
#endif
            SegmentBuilder.Append(segment, ref writeOffset, propertiesBytes);
            SegmentBuilder.Append(segment, ref writeOffset, payload);

            return(segment);
        }
Exemplo n.º 12
0
        // Placed object message payload into a segment.
        protected virtual ArraySegment <byte> EncodeMessageIntoSegment(EventData queueMessage, Func <int, ArraySegment <byte> > getSegment)
        {
            byte[] propertiesBytes = queueMessage.SerializeProperties(this.serializer);
            var    payload         = queueMessage.Body;
            // get size of namespace, offset, partitionkey, properties, and payload
            int size = SegmentBuilder.CalculateAppendSize(queueMessage.Offset.ToString()) +
                       SegmentBuilder.CalculateAppendSize(queueMessage.PartitionKey) +
                       SegmentBuilder.CalculateAppendSize(propertiesBytes) +
                       SegmentBuilder.CalculateAppendSize(payload);

            // get segment
            ArraySegment <byte> segment = getSegment(size);

            // encode namespace, offset, partitionkey, properties and payload into segment
            int writeOffset = 0;

            SegmentBuilder.Append(segment, ref writeOffset, queueMessage.Offset.ToString());
            SegmentBuilder.Append(segment, ref writeOffset, queueMessage.PartitionKey);
            SegmentBuilder.Append(segment, ref writeOffset, propertiesBytes);
            SegmentBuilder.Append(segment, ref writeOffset, payload);

            return(segment);
        }