static void Write(IByteBufferAllocator allocator, ErrorRedisMessage message, ICollection <object> output) { Contract.Requires(allocator != null); Contract.Requires(message != null); Contract.Requires(output != null); IByteBuffer buffer = WriteString(allocator, RedisMessageType.Error, message.Content); output.Add(buffer); }
FixedRedisMessagePool() { var stringToSimpleStringValues = new Dictionary <string, SimpleStringRedisMessage>(); var byteBufferToSimpleStringValues = new Dictionary <IByteBuffer, SimpleStringRedisMessage>(); foreach (string simpleString in SimpleStrings) { IByteBuffer key = Unpooled .WrappedBuffer(Encoding.UTF8.GetBytes(simpleString)) .Unreleasable(); var redisMessage = new SimpleStringRedisMessage(simpleString); stringToSimpleStringValues.Add(simpleString, redisMessage); byteBufferToSimpleStringValues.Add(key, redisMessage); } this.stringToSimpleStringMessages = new ReadOnlyDictionary <string, SimpleStringRedisMessage>(stringToSimpleStringValues); this.byteBufferToSimpleStringMessages = new ReadOnlyDictionary <IByteBuffer, SimpleStringRedisMessage>(byteBufferToSimpleStringValues); var errorToErrorValues = new Dictionary <string, ErrorRedisMessage>(); var byteBufferToErrorValues = new Dictionary <IByteBuffer, ErrorRedisMessage>(); foreach (string error in Errors) { IByteBuffer key = Unpooled .WrappedBuffer(Encoding.UTF8.GetBytes(error)) .Unreleasable(); var redisMessage = new ErrorRedisMessage(error); errorToErrorValues.Add(error, redisMessage); byteBufferToErrorValues.Add(key, redisMessage); } this.stringToErrorMessages = new ReadOnlyDictionary <string, ErrorRedisMessage>(errorToErrorValues); this.byteBufferToErrorMessages = new ReadOnlyDictionary <IByteBuffer, ErrorRedisMessage>(byteBufferToErrorValues); var longToIntegerValues = new Dictionary <long, IntegerRedisMessage>(); var longToByteBufferValues = new Dictionary <long, byte[]>(); var byteBufferToIntegerValues = new Dictionary <IByteBuffer, IntegerRedisMessage>(); for (long value = MinimumCachedIntegerNumber; value < MaximumCachedIntegerNumber; value++) { byte[] bytes = RedisCodecUtil.LongToAsciiBytes(value); IByteBuffer key = Unpooled .WrappedBuffer(bytes) .Unreleasable(); var redisMessage = new IntegerRedisMessage(value); longToIntegerValues.Add(value, redisMessage); longToByteBufferValues.Add(value, bytes); byteBufferToIntegerValues.Add(key, redisMessage); } this.longToIntegerMessages = new ReadOnlyDictionary <long, IntegerRedisMessage>(longToIntegerValues); this.longToBytes = new ReadOnlyDictionary <long, byte[]>(longToByteBufferValues); this.byteBufferToIntegerMessages = new ReadOnlyDictionary <IByteBuffer, IntegerRedisMessage>(byteBufferToIntegerValues); }
public void EncodeError() { var msg = new ErrorRedisMessage("error1"); Assert.True(this.channel.WriteOutbound(msg)); IByteBuffer written = ReadAll(this.channel); Assert.Equal(BytesOf("-error1\r\n"), BytesOf(written)); written.Release(); }
ErrorRedisMessage GetErrorMessage(IByteBuffer byteBuffer) { Contract.Requires(byteBuffer != null); ErrorRedisMessage message; if (!this.messagePool.TryGetMessage(byteBuffer, out message)) { message = new ErrorRedisMessage(byteBuffer.ToString(Encoding.UTF8)); } return(message); }
public void EncodeError(string value) { var channel = new EmbeddedChannel(new RedisEncoder()); var message = new ErrorRedisMessage(value); Assert.True(channel.WriteOutbound(message)); IByteBuffer written = ReadAll(channel); byte[] output = written.Bytes(); byte[] expected = $"-{value}\r\n".Bytes(); Assert.Equal(expected.Length, output.Length); Assert.True(output.SequenceEqual(expected)); written.Release(); }
FixedRedisMessagePool() { this.byteBufToSimpleStrings = new Dictionary <IByteBuffer, SimpleStringRedisMessage>(DefaultSimpleStrings.Length); this.stringToSimpleStrings = new Dictionary <string, SimpleStringRedisMessage>(DefaultSimpleStrings.Length); foreach (string simpleString in DefaultSimpleStrings) { IByteBuffer key = Unpooled.UnreleasableBuffer( Unpooled.WrappedBuffer(Encoding.UTF8.GetBytes(simpleString))); var cached = new SimpleStringRedisMessage(simpleString); this.byteBufToSimpleStrings.Add(key, cached); this.stringToSimpleStrings.Add(simpleString, cached); } this.byteBufToErrors = new Dictionary <IByteBuffer, ErrorRedisMessage>(DefaultErrors.Length); this.stringToErrors = new Dictionary <string, ErrorRedisMessage>(DefaultErrors.Length); foreach (string error in DefaultErrors) { IByteBuffer key = Unpooled.UnreleasableBuffer( Unpooled.WrappedBuffer(Encoding.UTF8.GetBytes(error))); var cached = new ErrorRedisMessage(error); this.byteBufToErrors.Add(key, cached); this.stringToErrors.Add(error, cached); } this.byteBufToIntegers = new Dictionary <IByteBuffer, IntegerRedisMessage>(); this.longToIntegers = new Dictionary <long, IntegerRedisMessage>(SizeCachedIntegerNumber); this.longToByteBufs = new Dictionary <long, byte[]>(SizeCachedIntegerNumber); for (long value = MinimumCachedIntegerNumber; value < MaximumCachedIntegerNumber; value++) { byte[] keyBytes = RedisCodecUtil.LongToAsciiBytes(value); IByteBuffer keyByteBuf = Unpooled.UnreleasableBuffer( Unpooled.WrappedBuffer(keyBytes)); var cached = new IntegerRedisMessage(value); this.byteBufToIntegers.Add(keyByteBuf, cached); this.longToIntegers.Add(value, cached); this.longToByteBufs.Add(value, keyBytes); } }
public bool TryGetError(IByteBuffer content, out ErrorRedisMessage message) => this.byteBufToErrors.TryGetValue(content, out message);
public bool TryGetError(string content, out ErrorRedisMessage message) => this.stringToErrors.TryGetValue(content, out message);
static void WriteErrorMessage(IByteBufferAllocator allocator, ErrorRedisMessage msg, List <object> output) => WriteString(allocator, RedisMessageType.Error, msg.Content, output);