|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | + |
| 7 | +namespace Cassandra.Serialization |
| 8 | +{ |
| 9 | + internal class CollectionSerializer : TypeSerializer<IEnumerable> |
| 10 | + { |
| 11 | + private Serializer _serializer; |
| 12 | + |
| 13 | + public override ColumnTypeCode CqlType |
| 14 | + { |
| 15 | + get { throw new NotSupportedException("CollectionSerializer does not map to a single cql type"); } |
| 16 | + } |
| 17 | + |
| 18 | + internal void SetChildSerializer(Serializer serializer) |
| 19 | + { |
| 20 | + _serializer = serializer; |
| 21 | + } |
| 22 | + |
| 23 | + private byte[] SerializeChild(object obj) |
| 24 | + { |
| 25 | + if (_serializer == null) |
| 26 | + { |
| 27 | + throw new NullReferenceException("Child serializer can not be null"); |
| 28 | + } |
| 29 | + return _serializer.Serialize(obj); |
| 30 | + } |
| 31 | + |
| 32 | + private object DeserializeChild(byte[] buffer, ColumnTypeCode typeCode, IColumnInfo typeInfo) |
| 33 | + { |
| 34 | + if (_serializer == null) |
| 35 | + { |
| 36 | + throw new NullReferenceException("Child serializer can not be null"); |
| 37 | + } |
| 38 | + return _serializer.Deserialize(buffer, typeCode, typeInfo); |
| 39 | + } |
| 40 | + |
| 41 | + public override IEnumerable Deserialize(ushort protocolVersion, byte[] buffer, IColumnInfo typeInfo) |
| 42 | + { |
| 43 | + throw new NotImplementedException(); |
| 44 | + } |
| 45 | + |
| 46 | + public override byte[] Serialize(ushort protocolVersion, IEnumerable value) |
| 47 | + { |
| 48 | + throw new NotImplementedException(); |
| 49 | + } |
| 50 | + |
| 51 | + private IEnumerable DeserializeCollection(int protocolVersion, Type childType, ColumnTypeCode childTypeCode, IColumnInfo childTypeInfo, byte[] buffer) |
| 52 | + { |
| 53 | + var index = 0; |
| 54 | + var count = DecodeCollectionLength(protocolVersion, buffer, ref index); |
| 55 | + var result = Array.CreateInstance(childType, count); |
| 56 | + for (var i = 0; i < count; i++) |
| 57 | + { |
| 58 | + var valueBufferLength = DecodeCollectionLength(protocolVersion, buffer, ref index); |
| 59 | + var itemBuffer = new byte[valueBufferLength]; |
| 60 | + Buffer.BlockCopy(buffer, index, itemBuffer, 0, valueBufferLength); |
| 61 | + index += valueBufferLength; |
| 62 | + result.SetValue(DeserializeChild(itemBuffer, childTypeCode, childTypeInfo), i); |
| 63 | + } |
| 64 | + return result; |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Decodes length for collection types depending on the protocol version |
| 69 | + /// </summary> |
| 70 | + private static int DecodeCollectionLength(int protocolVersion, byte[] buffer, ref int index) |
| 71 | + { |
| 72 | + int result; |
| 73 | + if (protocolVersion < 3) |
| 74 | + { |
| 75 | + //length is a short |
| 76 | + result = BeConverter.ToInt16(buffer, index); |
| 77 | + index += 2; |
| 78 | + } |
| 79 | + else |
| 80 | + { |
| 81 | + //length is expressed in int |
| 82 | + result = BeConverter.ToInt32(buffer, index); |
| 83 | + index += 4; |
| 84 | + } |
| 85 | + return result; |
| 86 | + } |
| 87 | + |
| 88 | + } |
| 89 | +} |
0 commit comments