GetMessageSetSize() public static method

Gives the size of a list of messages
public static GetMessageSetSize ( IEnumerable messages ) : int
messages IEnumerable /// The messages. ///
return int
Esempio n. 1
0
        public static Message Compress(IEnumerable <Message> messages, CompressionCodecs compressionCodec)
        {
            switch (compressionCodec)
            {
            case CompressionCodecs.DefaultCompressionCodec:
            case CompressionCodecs.GZIPCompressionCodec:
                using (MemoryStream outputStream = new MemoryStream())
                {
                    using (GZipStream gZipStream = new GZipStream(outputStream, CompressionMode.Compress))
                    {
                        if (Logger.IsDebugEnabled)
                        {
                            Logger.DebugFormat(
                                CultureInfo.CurrentCulture,
                                "Allocating BufferedMessageSet of size = {0}",
                                MessageSet.GetMessageSetSize(messages));
                        }

                        var bufferedMessageSet = new BufferedMessageSet(messages);
                        using (MemoryStream inputStream = new MemoryStream(bufferedMessageSet.SetSize))
                        {
                            bufferedMessageSet.WriteTo(inputStream);
                            inputStream.Position = 0;
                            try
                            {
                                gZipStream.Write(inputStream.ToArray(), 0, inputStream.ToArray().Length);
                                gZipStream.Close();
                            }
                            catch (IOException ex)
                            {
                                Logger.Error("Error while writing to the GZIP stream", ex);
                                throw;
                            }
                        }

                        Message oneCompressedMessage = new Message(outputStream.ToArray(), compressionCodec);
                        return(oneCompressedMessage);
                    }
                }

            default:
                throw new UnknownCodecException(String.Format(CultureInfo.CurrentCulture, "Unknown Codec: {0}", compressionCodec));
            }
        }
Esempio n. 2
0
        public static Message Compress(IEnumerable <Message> messages, CompressionCodecs compressionCodec, int partition)
        {
            switch (compressionCodec)
            {
            case CompressionCodecs.DefaultCompressionCodec:
            case CompressionCodecs.GZIPCompressionCodec:
                using (var outputStream = new MemoryStream())
                {
                    using (var gZipStream = new GZipStream(outputStream, CompressionMode.Compress))
                    {
                        //if (Logger.IsDebugEnabled)
                        {
                            Logger.DebugFormat(
                                "Allocating BufferedMessageSet of size = {0}",
                                MessageSet.GetMessageSetSize(messages));
                        }

                        var bufferedMessageSet = new BufferedMessageSet(messages, partition);
                        using (var inputStream = new MemoryStream(bufferedMessageSet.SetSize))
                        {
                            bufferedMessageSet.WriteTo(inputStream);
                            inputStream.Position = 0;
                            try
                            {
                                gZipStream.Write(inputStream.ToArray(), 0, inputStream.ToArray().Length);
                                gZipStream.Close();
                            }
                            catch (IOException ex)
                            {
                                Logger.ErrorFormat("Error while writing to the GZIP stream {0}",
                                                   ex.FormatException());
                                throw;
                            }
                        }

                        var oneCompressedMessage = new Message(outputStream.ToArray(), compressionCodec)
                        {
                            PartitionId = partition
                        };
                        return(oneCompressedMessage);
                    }
                }

            case CompressionCodecs.SnappyCompressionCodec:
                Logger.DebugFormat(
                    "Allocating BufferedMessageSet of size = {0}",
                    MessageSet.GetMessageSetSize(messages));

                var messageSet = new BufferedMessageSet(messages, partition);
                using (var inputStream = new MemoryStream(messageSet.SetSize))
                {
                    messageSet.WriteTo(inputStream);
                    inputStream.Position = 0;

                    try
                    {
                        return(new Message(SnappyHelper.Compress(inputStream.GetBuffer()), compressionCodec)
                        {
                            PartitionId = partition
                        });
                    }
                    catch (Exception ex)
                    {
                        Logger.ErrorFormat("Error while writing to the Snappy stream {0}", ex.FormatException());
                        throw;
                    }
                }

            default:
                throw new UnknownCodecException(string.Format(CultureInfo.CurrentCulture, "Unknown Codec: {0}",
                                                              compressionCodec));
            }
        }