void WriteIntegerMessage(IByteBufferAllocator allocator, IntegerRedisMessage msg, List <object> output) { IByteBuffer buf = allocator.Buffer(RedisConstants.TypeLength + RedisConstants.LongMaxLength + RedisConstants.EndOfLineLength); RedisMessageType.Integer.WriteTo(buf); buf.WriteBytes(this.NumberToBytes(msg.Value)); buf.WriteShort(RedisConstants.EndOfLineShort); output.Add(buf); }
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); }
IntegerRedisMessage GetIntegerMessage(IByteBuffer byteBuffer) { IntegerRedisMessage message; if (!this.messagePool.TryGetMessage(byteBuffer, out message)) { message = new IntegerRedisMessage(this.ParseNumber(byteBuffer)); } return(message); }
public void EncodeInteger() { var msg = new IntegerRedisMessage(1234L); Assert.True(this.channel.WriteOutbound(msg)); IByteBuffer written = ReadAll(this.channel); Assert.Equal(BytesOf(":1234\r\n"), BytesOf(written)); written.Release(); }
public void EncodeInteger(long value) { var channel = new EmbeddedChannel(new RedisEncoder()); var message = new IntegerRedisMessage(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); } }
void Write(IByteBufferAllocator allocator, IntegerRedisMessage message, ICollection <object> output) { Contract.Requires(allocator != null); Contract.Requires(message != null); Contract.Requires(output != null); IByteBuffer buffer = allocator.Buffer( RedisConstants.TypeLength + RedisConstants.LongValueMaximumLength + RedisConstants.EndOfLineLength); // Header buffer.WriteByte((char)RedisMessageType.Integer); // Content byte[] bytes = this.NumberToBytes(message.Value); buffer.WriteBytes(bytes); // EOL buffer.WriteShort(RedisConstants.EndOfLine); output.Add(buffer); }
public bool TryGetInteger(IByteBuffer content, out IntegerRedisMessage message) => this.byteBufToIntegers.TryGetValue(content, out message);
public bool TryGetInteger(long value, out IntegerRedisMessage message) => this.longToIntegers.TryGetValue(value, out message);