Represents an object either being retrieved from the cache or being sent to the cache.
Пример #1
0
        public object Deserialize(CacheItem item)
        {
            var jsonSerializer = new JsonSerializer
                {
                    TypeNameHandling = TypeNameHandling.All
                };

            var data = new byte[item.Data.Count];
            Array.Copy(item.Data.Array, item.Data.Offset, data, 0, data.Length);
            using (var memoryStream = new MemoryStream(data))
            {
                using (var zipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
                {
                    using (var streamReader = new StreamReader(zipStream))
                    {
                        var textReader = new JsonTextReader(streamReader);
                        try
                        {
                            return jsonSerializer.Deserialize(textReader);
                        }
                        catch (JsonSerializationException)
                        {
                            //This exception occurs if whatever is in memcached is impossible to deserialize. It's a tricky case, but we'll have to report back that nothing is in there.
                            return null;
                        }
                    }
                }
            }
        }
Пример #2
0
        public object Deserialize(CacheItem item)
        {
            var jsonSerializer = new JsonSerializer
            {
                TypeNameHandling = TypeNameHandling.All
            };

            var data = new byte[item.Data.Count];
            Array.Copy(item.Data.Array, item.Data.Offset, data, 0, data.Length);

            using (var memoryStream = new MemoryStream(data))
            {
                using (var streamReader = new StreamReader(memoryStream))
                {
                    var textReader = new JsonTextReader(streamReader);
                    try
                    {
                        var dynamicEntity = jsonSerializer.Deserialize(textReader);

                        return dynamicEntity;
                    }
                    catch (JsonReaderException)
                    {
                        return null;
                    }
                    catch (JsonSerializationException)
                    {
                        return null;
                    }
                }
            }
        }
Пример #3
0
        public object Deserialize(CacheItem item)
        {
            var jsonSerializer = new JsonSerializer
            {
                TypeNameHandling = TypeNameHandling.All
            };

            using (var memoryStream = new MemoryStream(item.Data.Array))
            {
                using (var streamReader = new StreamReader(memoryStream))
                {
                    var textReader = new JsonTextReader(streamReader);
                    try
                    {
                        var dynamicEntity = jsonSerializer.Deserialize(textReader);

                        return dynamicEntity;
                    }
                    catch(JsonReaderException e)
                    {
                        return new object();
                    }
                }
            }
        }
Пример #4
0
        protected virtual object Deserialize(CacheItem item)
        {
            if (item.Data.Array == null)
                return null;

            if (item.Flags == RawDataFlag) {
                var tmp = item.Data;

                if (tmp.Count == tmp.Array.Length)
                    return tmp.Array;

                // we should never arrive here, but it's better to be safe than sorry
                var retval = new byte[tmp.Count];

                Array.Copy(tmp.Array, tmp.Offset, retval, 0, tmp.Count);

                return retval;
            }

            var code = (TypeCode)(item.Flags & 0xff);

            var data = item.Data;

            switch (code) {
                // incrementing a non-existing key then getting it
                // returns as a string, but the flag will be 0
                // so treat all 0 flagged items as string
                // this may help inter-client data management as well
                //
                // however we store 'null' as Empty + an empty array,
                // so this must special-cased for compatibilty with
                // earlier versions. we introduced DBNull as null marker in emc2.6
                case TypeCode.Empty:
                    return (data.Array == null || data.Count == 0)
                            ? null
                            : DeserializeString(data);

                case TypeCode.DBNull: return null;
                case TypeCode.String: return this.DeserializeString(data);
                case TypeCode.Boolean: return this.DeserializeBoolean(data);
                case TypeCode.Int16: return this.DeserializeInt16(data);
                case TypeCode.Int32: return this.DeserializeInt32(data);
                case TypeCode.Int64: return this.DeserializeInt64(data);
                case TypeCode.UInt16: return this.DeserializeUInt16(data);
                case TypeCode.UInt32: return this.DeserializeUInt32(data);
                case TypeCode.UInt64: return this.DeserializeUInt64(data);
                case TypeCode.Char: return this.DeserializeChar(data);
                case TypeCode.DateTime: return this.DeserializeDateTime(data);
                case TypeCode.Double: return this.DeserializeDouble(data);
                case TypeCode.Single: return this.DeserializeSingle(data);

                // backward compatibility
                // earlier versions serialized decimals with TypeCode.Decimal
                // even though they were saved by BinaryFormatter
                case TypeCode.Decimal:
                case TypeCode.Object: return this.DeserializeObject(data);
                default: throw new InvalidOperationException("Unknown TypeCode was returned: " + code);
            }
        }
		public IStoreOperation Store(StoreMode mode, Key key, CacheItem value, uint expires, ulong cas)
		{
			return new StoreOperation(allocator, mode, key, value)
			{
				Cas = cas,
				Expires = expires
			};
		}
		public object Deserialize(CacheItem item)
		{
			if (item.Segment.Array == null)
				return null;

			if (item.Flags == RawDataFlag || (item.Flags & 0x1ff) != item.Flags)
			{
				var tmp = item.Segment;
				if (tmp.Count == tmp.Array.Length)
					return tmp.Array;

				// TODO improve memcpy
				var retval = new byte[tmp.Count];
				Buffer.BlockCopy(tmp.Array, 0, retval, 0, tmp.Count);

				return retval;
			}

			var code = (TypeCode)(item.Flags & 0xff);
			var data = item.Segment;

			switch (code)
			{
				case TypeCode.DBNull: return null;

				// incrementing a non-existing key then getting it
				// returns as a string, but the flag will be 0
				// so treat all 0 flagged items as string
				// this may help inter-client data management as well
				case TypeCode.Empty:
				case TypeCode.String: return DeserializeString(data);

				case TypeCode.Byte: return data.Array[0];
				case TypeCode.SByte: return (sbyte)data.Array[0];

				case TypeCode.Boolean: return PooledBitConverter.ToBoolean(data);
				case TypeCode.Char: return PooledBitConverter.ToChar(data);

				case TypeCode.Int16: return PooledBitConverter.ToInt16(data);
				case TypeCode.Int32: return PooledBitConverter.ToInt32(data);
				case TypeCode.Int64: return PooledBitConverter.ToInt64(data);
				case TypeCode.UInt16: return PooledBitConverter.ToUInt16(data);
				case TypeCode.UInt32: return PooledBitConverter.ToUInt32(data);
				case TypeCode.UInt64: return PooledBitConverter.ToUInt64(data);
				case TypeCode.DateTime: return DateTime.FromBinary(PooledBitConverter.ToInt64(data));

				case TypeCode.Single: return PooledBitConverter.ToSingle(data);
				case TypeCode.Double: return PooledBitConverter.ToDouble(data);
				case TypeCode.Decimal: return PooledBitConverter.ToDecimal(data);

				case TypeCode.Object: return DeserializeObject(data);
				default: throw new InvalidOperationException("Unknown TypeCode was returned: " + code);
			}
		}
Пример #7
0
 object ITranscoder.Deserialize(CacheItem item)
 {
     switch (item.Flag)
     {
         case ProtoIdentifier:
             var segment = item.Data;
             byte[] raw = segment.Array;
             int count = segment.Count, offset = segment.Offset;
             Type type = ReadType(raw, ref offset, ref count);
             using (var ms = new MemoryStream(raw, offset, count))
             {
                 return Serializer.NonGeneric.Deserialize(type, ms);
             }
         default:
             return inner.Deserialize(item);
     }
 }
Пример #8
0
 public bool Equals(CacheItem obj)
 {
     return(obj.flags == flags && obj.data.Equals(data));
 }
Пример #9
0
		object ITranscoder.Deserialize(CacheItem item)
		{
			if (item.Data == null || item.Data.Array == null)
				return null;

			if (item.Flags == RawDataFlag)
			{
				ArraySegment<byte> tmp = item.Data;

				if (tmp.Count == tmp.Array.Length)
					return tmp.Array;

				// we should never arrive here, but it's better to be safe than sorry
				byte[] retval = new byte[tmp.Count];

				Array.Copy(tmp.Array, tmp.Offset, retval, 0, tmp.Count);

				return retval;
			}

			TypeCode code = (TypeCode)(item.Flags & 0x00ff);

			byte[] data = item.Data.Array;
			int offset = item.Data.Offset;
			int count = item.Data.Count;

			switch (code)
			{
				// incrementing a non-existing key then getting it
				// returns as a string, but the flag will be 0
				// so treat all 0 flagged items as string
				// this may help inter-memcachedClient data management as well
				//
				// however we store 'null' as Empty + an empty array, 
				// so this must special-cased for compatibilty with 
				// earlier versions. we introduced DBNull as null marker in emc2.6
				case TypeCode.Empty:
					return (data == null || count == 0)
							? null
							: Encoding.UTF8.GetString(data, offset, count);

				case TypeCode.DBNull:
					return null;

				case TypeCode.String:
					return Encoding.UTF8.GetString(data, offset, count);

				case TypeCode.Boolean:
					return BitConverter.ToBoolean(data, offset);

				case TypeCode.Int16:
					return BitConverter.ToInt16(data, offset);

				case TypeCode.Int32:
					return BitConverter.ToInt32(data, offset);

				case TypeCode.Int64:
					return BitConverter.ToInt64(data, offset);

				case TypeCode.UInt16:
					return BitConverter.ToUInt16(data, offset);

				case TypeCode.UInt32:
					return BitConverter.ToUInt32(data, offset);

				case TypeCode.UInt64:
					return BitConverter.ToUInt64(data, offset);

				case TypeCode.Char:
					return BitConverter.ToChar(data, offset);

				case TypeCode.DateTime:
					return DateTime.FromBinary(BitConverter.ToInt64(data, offset));

				case TypeCode.Double:
					return BitConverter.ToDouble(data, offset);

				case TypeCode.Single:
					return BitConverter.ToSingle(data, offset);

				case TypeCode.Object:
					using (MemoryStream ms = new MemoryStream(data, offset, count))
					{
						return new BinaryFormatter().Deserialize(ms);
					}

				default: throw new InvalidOperationException("Unknown TypeCode was returned: " + code);
			}
		}
        public GetResponse(string key, ushort flags, ulong casValue, byte[] data, int offset, int count)
        {
            this.Key = key;
            this.CasValue = casValue;

            this.Item = new CacheItem(flags, new ArraySegment<byte>(data, offset, count));
        }
Пример #11
0
        object ITranscoder.Deserialize(CacheItem item)
        {
            if (item.Data == null || item.Data.Array == null)
                return null;

            if (item.Flags == RawDataFlag)
            {
                ArraySegment<byte> tmp = item.Data;

                if (tmp.Count == tmp.Array.Length)
                    return tmp.Array;

                // we should never arrive here, but it's better to be safe than sorry
                byte[] retval = new byte[tmp.Count];

                Array.Copy(tmp.Array, tmp.Offset, retval, 0, tmp.Count);

                return retval;
            }

            TypeCode code = (TypeCode)(item.Flags & 0x00ff);

            byte[] data = item.Data.Array;
            int offset = item.Data.Offset;
            int count = item.Data.Count;

            switch (code)
            {
                // incrementing a non-existing key then getting it
                // returns as a string, but the flag will be 0
                // so treat all 0 flagged items as string
                // this may help inter-client data management as well
                //
                // however we store 'null' as Empty + an empty array,
                // so this must special-cased for compatibilty with
                // earlier versions. we introduced DBNull as null marker in emc2.6
                case TypeCode.Empty:
                    return (data == null || count == 0)
                            ? null
                            : Encoding.UTF8.GetString(data, offset, count);

                case TypeCode.DBNull:
                    return null;

                case TypeCode.String:
                    return Encoding.UTF8.GetString(data, offset, count);

                case TypeCode.Boolean:
                    return BitConverter.ToBoolean(data, offset);

                case TypeCode.Int16:
                    return BitConverter.ToInt16(data, offset);

                case TypeCode.Int32:
                    return BitConverter.ToInt32(data, offset);

                case TypeCode.Int64:
                    return BitConverter.ToInt64(data, offset);

                case TypeCode.UInt16:
                    return BitConverter.ToUInt16(data, offset);

                case TypeCode.UInt32:
                    return BitConverter.ToUInt32(data, offset);

                case TypeCode.UInt64:
                    return BitConverter.ToUInt64(data, offset);

                case TypeCode.Char:
                    return BitConverter.ToChar(data, offset);

                case TypeCode.DateTime:
                    return DateTime.FromBinary(BitConverter.ToInt64(data, offset));

                case TypeCode.Double:
                    return BitConverter.ToDouble(data, offset);

                case TypeCode.Single:
                    return BitConverter.ToSingle(data, offset);

                case TypeCode.Object:
                    using (MemoryStream ms = new MemoryStream(data, offset, count))
                    {
                        return new BinaryFormatter().Deserialize(ms);
                    }

                default: throw new InvalidOperationException("Unknown TypeCode was returned: " + code);
            }
        }
Пример #12
0
        protected virtual object Deserialize(CacheItem item)
        {
            if (item.Data.Array == null)
            {
                return(null);
            }

            if (item.Flags == RawDataFlag)
            {
                var tmp = item.Data;

                if (tmp.Count == tmp.Array.Length)
                {
                    return(tmp.Array);
                }

                // we should never arrive here, but it's better to be safe than sorry
                var retval = new byte[tmp.Count];

                Array.Copy(tmp.Array, tmp.Offset, retval, 0, tmp.Count);

                return(retval);
            }

            var code = (TypeCode)(item.Flags & 0xff);

            var data = item.Data;

            switch (code)
            {
            // incrementing a non-existing key then getting it
            // returns as a string, but the flag will be 0
            // so treat all 0 flagged items as string
            // this may help inter-client data management as well
            //
            // however we store 'null' as Empty + an empty array,
            // so this must special-cased for compatibilty with
            // earlier versions. we introduced DBNull as null marker in emc2.6
            case TypeCode.Empty:
                return((data.Array == null || data.Count == 0)
                                                        ? null
                                                        : DeserializeString(data));

            case TypeCode.DBNull: return(null);

            case TypeCode.String: return(this.DeserializeString(data));

            case TypeCode.Boolean: return(this.DeserializeBoolean(data));

            case TypeCode.Int16: return(this.DeserializeInt16(data));

            case TypeCode.Int32: return(this.DeserializeInt32(data));

            case TypeCode.Int64: return(this.DeserializeInt64(data));

            case TypeCode.UInt16: return(this.DeserializeUInt16(data));

            case TypeCode.UInt32: return(this.DeserializeUInt32(data));

            case TypeCode.UInt64: return(this.DeserializeUInt64(data));

            case TypeCode.Char: return(this.DeserializeChar(data));

            case TypeCode.DateTime: return(this.DeserializeDateTime(data));

            case TypeCode.Double: return(this.DeserializeDouble(data));

            case TypeCode.Single: return(this.DeserializeSingle(data));

            case TypeCode.Byte: return(this.DeserializeByte(data));

            case TypeCode.SByte: return(this.DeserializeSByte(data));

            // backward compatibility
            // earlier versions serialized decimals with TypeCode.Decimal
            // even though they were saved by BinaryFormatter
            case TypeCode.Decimal:
            case TypeCode.Object: return(this.DeserializeObject(data));

            default: throw new InvalidOperationException("Unknown TypeCode was returned: " + code);
            }
        }
        object ITranscoder.Deserialize(CacheItem item)
        {
            if (item.Flag == RawDataFlag)
            {
                ArraySegment<byte> tmp = item.Data;

                if (tmp.Count == tmp.Array.Length)
                    return tmp.Array;

                // we should never arrive here, but it's better to be safe than sorry
                byte[] retval = new byte[tmp.Count];

                Array.Copy(tmp.Array, tmp.Offset, retval, 0, tmp.Count);

                return retval;
            }

            TypeCode code = (TypeCode)(item.Flag & 0x00ff);

            if (code == TypeCode.Empty)
                return null;

            byte[] data = item.Data.Array;
            int offset = item.Data.Offset;
            int count = item.Data.Count;

            switch (code)
            {
                case TypeCode.String:
                    return Encoding.UTF8.GetString(data, offset, count);

                case TypeCode.Boolean:
                    return BitConverter.ToBoolean(data, offset);

                case TypeCode.Int16:
                    return BitConverter.ToInt16(data, offset);

                case TypeCode.Int32:
                    return BitConverter.ToInt32(data, offset);

                case TypeCode.Int64:
                    return BitConverter.ToInt64(data, offset);

                case TypeCode.UInt16:
                    return BitConverter.ToUInt16(data, offset);

                case TypeCode.UInt32:
                    return BitConverter.ToUInt32(data, offset);

                case TypeCode.UInt64:
                    return BitConverter.ToUInt64(data, offset);

                case TypeCode.Char:
                    return BitConverter.ToChar(data, offset);

                case TypeCode.DateTime:
                    return DateTime.FromBinary(BitConverter.ToInt64(data, offset));

                case TypeCode.Double:
                    return BitConverter.ToDouble(data, offset);

                case TypeCode.Single:
                    return BitConverter.ToSingle(data, offset);

                case TypeCode.Object:
                    using (MemoryStream ms = new MemoryStream(data, offset, count))
                    {
                        return new BinaryFormatter().Deserialize(ms);
                    }

                default: throw new InvalidOperationException("Unknown TypeCode was returned: " + code);
            }
        }
Пример #14
0
		public bool Equals(CacheItem obj)
		{
			return obj.flags == flags && obj.segment.Equals(segment);
		}
Пример #15
0
 object ITranscoder.Deserialize(CacheItem item)
 {
     return(this.Deserialize(item));
 }
 public VBStore(VBucketNodeLocator locator, StoreMode mode, string key, CacheItem value, uint expires)
     : base(mode, key, value, expires)
 {
     this.locator = locator;
 }
Пример #17
0
 object ITranscoder.Deserialize(CacheItem item)
 {
     switch (item.Flags)
     {
         case ProtoIdentifier:
             var segment = item.Data;
             byte[] raw = segment.Array;
             int count = segment.Count, offset = segment.Offset;
             Type type = ReadType(raw, ref offset, ref count);
             using (var ms = new MemoryStream(raw, offset, count))
             {
                 Interlocked.Increment(ref deserializedCount);
                 return model.Deserialize(ms, null, type);
             }
         default:
             return inner.Deserialize(item);
     }
 }
Пример #18
0
 object ITranscoder.Deserialize(CacheItem item)
 => this.Deserialize(item);
        public object Deserialize(CacheItem item)
        {
            if (item.Segment.Array == null)
            {
                return(null);
            }

            if (item.Flags == RawDataFlag || (item.Flags & 0x1ff) != item.Flags)
            {
                var tmp = item.Segment;
                if (tmp.Count == tmp.Array.Length)
                {
                    return(tmp.Array);
                }

                // TODO improve memcpy
                var retval = new byte[tmp.Count];
                Buffer.BlockCopy(tmp.Array, 0, retval, 0, tmp.Count);

                return(retval);
            }

            var code = (TypeCode)(item.Flags & 0xff);
            var data = item.Segment;

            switch (code)
            {
            case TypeCode.DBNull: return(null);

            // incrementing a non-existing key then getting it
            // returns as a string, but the flag will be 0
            // so treat all 0 flagged items as string
            // this may help inter-client data management as well
            case TypeCode.Empty:
            case TypeCode.String: return(DeserializeString(data));

            case TypeCode.Byte: return(data.Array[0]);

            case TypeCode.SByte: return((sbyte)data.Array[0]);

            case TypeCode.Boolean: return(PooledBitConverter.ToBoolean(data));

            case TypeCode.Char: return(PooledBitConverter.ToChar(data));

            case TypeCode.Int16: return(PooledBitConverter.ToInt16(data));

            case TypeCode.Int32: return(PooledBitConverter.ToInt32(data));

            case TypeCode.Int64: return(PooledBitConverter.ToInt64(data));

            case TypeCode.UInt16: return(PooledBitConverter.ToUInt16(data));

            case TypeCode.UInt32: return(PooledBitConverter.ToUInt32(data));

            case TypeCode.UInt64: return(PooledBitConverter.ToUInt64(data));

            case TypeCode.DateTime: return(DateTime.FromBinary(PooledBitConverter.ToInt64(data)));

            case TypeCode.Single: return(PooledBitConverter.ToSingle(data));

            case TypeCode.Double: return(PooledBitConverter.ToDouble(data));

            case TypeCode.Decimal: return(PooledBitConverter.ToDecimal(data));

            case TypeCode.Object: return(DeserializeObject(data));

            default: throw new InvalidOperationException("Unknown TypeCode was returned: " + code);
            }
        }
Пример #20
0
        object ITranscoder.Deserialize(CacheItem item)
        {
            if (item.Flag == RawDataFlag)
            {
                ArraySegment <byte> tmp = item.Data;

                if (tmp.Count == tmp.Array.Length)
                {
                    return(tmp.Array);
                }

                // we should never arrive here, but it's better to be safe than sorry
                byte[] retval = new byte[tmp.Count];

                Array.Copy(tmp.Array, tmp.Offset, retval, 0, tmp.Count);

                return(retval);
            }

            TypeCode code = (TypeCode)(item.Flag & 0x00ff);

            if (code == TypeCode.Empty)
            {
                return(null);
            }

            byte[] data   = item.Data.Array;
            int    offset = item.Data.Offset;
            int    count  = item.Data.Count;

            switch (code)
            {
            case TypeCode.String:
                return(Encoding.UTF8.GetString(data, offset, count));

            case TypeCode.Boolean:
                return(BitConverter.ToBoolean(data, offset));

            case TypeCode.Int16:
                return(BitConverter.ToInt16(data, offset));

            case TypeCode.Int32:
                return(BitConverter.ToInt32(data, offset));

            case TypeCode.Int64:
                return(BitConverter.ToInt64(data, offset));

            case TypeCode.UInt16:
                return(BitConverter.ToUInt64(data, offset));

            case TypeCode.UInt32:
                return(BitConverter.ToUInt32(data, offset));

            case TypeCode.UInt64:
                return(BitConverter.ToUInt64(data, offset));

            case TypeCode.Char:
                return(BitConverter.ToChar(data, offset));

            case TypeCode.DateTime:
                return(DateTime.FromBinary(BitConverter.ToInt64(data, offset)));

            case TypeCode.Double:
                return(BitConverter.ToDouble(data, offset));

            case TypeCode.Single:
                return(BitConverter.ToSingle(data, offset));

            case TypeCode.Object:
                using (MemoryStream ms = new MemoryStream(data, offset, count))
                {
                    return(new BinaryFormatter().Deserialize(ms));
                }

            default: throw new InvalidOperationException("Unknown TypeCode was returned: " + code);
            }
        }
 IStoreOperation IOperationFactory.Store(StoreMode mode, string key, CacheItem value, uint expires, ulong cas)
 {
     return new VBStore(locator, mode, key, value, expires) { Cas = cas };
 }
Пример #22
0
		public IStoreOperation Store(StoreMode mode, Key key, CacheItem value, uint expires, ulong cas)
		{
			throw new NotImplementedException();
		}
Пример #23
0
 object ITranscoder.Deserialize(CacheItem item)
 {
     return this.Deserialize(item);
 }
Пример #24
0
		public object Deserialize(CacheItem item)
		{
			throw new NotImplementedException();
		}