public void EncodeDecodeSingleValuesFactoryTest() { var initialValues = new object[] { new object[] { "just utf8 text olé!", ColumnTypeCode.Text }, new object[] { "just ascii text", ColumnTypeCode.Ascii }, new object[] { 123, ColumnTypeCode.Int }, new object[] { Int64.MinValue + 100, ColumnTypeCode.Bigint }, new object[] { 44F, ColumnTypeCode.Float }, new object[] { -320D, ColumnTypeCode.Double }, new object[] { 99.89770M, ColumnTypeCode.Decimal }, new object[] { Decimal.MaxValue, ColumnTypeCode.Decimal }, new object[] { new DateTime(2010, 4, 29), ColumnTypeCode.Timestamp }, new object[] { new DateTimeOffset(new DateTime(2010, 4, 29)), ColumnTypeCode.Timestamp }, new object[] { new IPAddress(new byte[] { 10, 0, 5, 5 }), ColumnTypeCode.Inet }, new object[] { Guid.NewGuid(), ColumnTypeCode.Uuid }, new object[] { false, ColumnTypeCode.Boolean }, new object[] { new byte [] { 1, 2 }, ColumnTypeCode.Blob } }; foreach (var version in _protocolVersions) { foreach (object[] value in initialValues) { byte[] encoded = TypeCodec.Encode(version, value[0]); Assert.AreEqual(value[0], TypeCodec.Decode(version, encoded, (ColumnTypeCode)value[1], null, value[0].GetType())); } } }
public void EncodeDecodeSingleValuesDefaultsFactory() { var initialValues = new object[] { new object[] { "just utf8 text olé!", ColumnTypeCode.Text }, new object[] { 123, ColumnTypeCode.Int }, new object[] { Int64.MinValue + 100, ColumnTypeCode.Bigint }, new object[] { -144F, ColumnTypeCode.Float }, new object[] { 1120D, ColumnTypeCode.Double }, new object[] { -9999.89770M, ColumnTypeCode.Decimal }, new object[] { -256M, ColumnTypeCode.Decimal }, new object[] { new DateTimeOffset(new DateTime(2010, 4, 29)), ColumnTypeCode.Timestamp }, new object[] { new IPAddress(new byte[] { 10, 0, 5, 5 }), ColumnTypeCode.Inet }, new object[] { Guid.NewGuid(), ColumnTypeCode.Uuid }, new object[] { true, ColumnTypeCode.Boolean }, new object[] { new byte [] { 255, 128, 64, 32, 16, 9, 9 }, ColumnTypeCode.Blob } }; foreach (var version in _protocolVersions) { foreach (object[] value in initialValues) { byte[] encoded = TypeCodec.Encode(version, value[0]); //Set object as the target CSharp type, it should get the default value Assert.AreEqual(value[0], TypeCodec.Decode(version, encoded, (ColumnTypeCode)value[1], null, typeof(object))); } } }
public void EncodeDecodeListSetFactoryTest() { var initialValues = new object[] { new object[] { new List <int>(new [] { 1, 2, 1000 }), ColumnTypeCode.List, new ListColumnInfo() { ValueTypeCode = ColumnTypeCode.Int } }, new object[] { new List <double>(new [] { -1D, 2.333D, 1.2D }), ColumnTypeCode.List, new ListColumnInfo() { ValueTypeCode = ColumnTypeCode.Double } }, new object[] { new List <decimal>(new [] { -1M, 2.333M, 1.2M, 256M }), ColumnTypeCode.Set, new SetColumnInfo() { KeyTypeCode = ColumnTypeCode.Decimal } } }; foreach (var version in _protocolVersions) { foreach (object[] value in initialValues) { var valueToEncode = (IList)value[0]; var encoded = TypeCodec.Encode(version, valueToEncode); var decoded = (IList)TypeCodec.Decode(version, encoded, (ColumnTypeCode)value[1], (IColumnInfo)value[2], value[0].GetType()); Assert.AreEqual(valueToEncode.Count, decoded.Count); Assert.AreEqual(valueToEncode, decoded); } } }
public void EncodeDecodeNestedSet() { var initialValues = new object[] { new object[] { new SortedSet <IEnumerable <int> > { new SortedSet <int>(new [] { 1, 2, 1000 }) }, ColumnTypeCode.Set, GetNestedSetColumnInfo(1, ColumnTypeCode.Int) }, new object[] { new SortedSet <IEnumerable <IEnumerable <int> > > { new SortedSet <IEnumerable <int> > { new SortedSet <int>(new [] { 1, 2, 1000 }) } }, ColumnTypeCode.Set, GetNestedSetColumnInfo(2, ColumnTypeCode.Int) } }; foreach (var version in _protocolVersions) { foreach (object[] value in initialValues) { var originalType = value[0].GetType(); var valueToEncode = (IEnumerable)value[0]; var encoded = TypeCodec.Encode(version, valueToEncode); var decoded = (IEnumerable)TypeCodec.Decode(version, encoded, (ColumnTypeCode)value[1], (IColumnInfo)value[2], originalType); //The return type is not respected CollectionAssert.AreEqual(valueToEncode, decoded); } } }
public override Task <AggregateMetadata> GetAggregate(string keyspaceName, string aggregateName, string signatureString) { var query = string.Format(SelectAggregates, keyspaceName, aggregateName, signatureString); return(Cc .QueryAsync(query, true) .ContinueSync(rs => { var row = rs.FirstOrDefault(); if (row == null) { return null; } var emptyArray = new string[0]; var aggregate = new AggregateMetadata { Name = row.GetValue <string>("aggregate_name"), KeyspaceName = row.GetValue <string>("keyspace_name"), Signature = row.GetValue <string[]>("signature") ?? emptyArray, StateFunction = row.GetValue <string>("state_func"), StateType = TypeCodec.ParseFqTypeName(row.GetValue <string>("state_type")), FinalFunction = row.GetValue <string>("final_func"), ReturnType = TypeCodec.ParseFqTypeName(row.GetValue <string>("return_type")), ArgumentTypes = (row.GetValue <string[]>("argument_types") ?? emptyArray).Select(s => TypeCodec.ParseFqTypeName(s)).ToArray(), }; var initConditionRaw = TypeCodec.Decode(Cc.ProtocolVersion, row.GetValue <byte[]>("initcond"), aggregate.StateType.TypeCode, aggregate.StateType.TypeInfo); if (initConditionRaw != null) { aggregate.InitialCondition = initConditionRaw.ToString(); } return aggregate; })); }
public void Encode_Decode_Nested_List() { var initialValues = new object[] { new object[] { new List <IEnumerable <int> > { new List <int>(new [] { 1, 2, 1000 }) }, ColumnTypeCode.List, GetNestedListColumnInfo(1, ColumnTypeCode.Int) }, new object[] { new List <IEnumerable <IEnumerable <int> > > { new List <IEnumerable <int> > { new List <int>(new [] { 1, 2, 1000 }) } }, ColumnTypeCode.List, GetNestedListColumnInfo(2, ColumnTypeCode.Int) } }; foreach (var version in _protocolVersions) { foreach (object[] value in initialValues) { var originalType = value[0].GetType(); var valueToEncode = (IEnumerable)value[0]; var encoded = TypeCodec.Encode(version, valueToEncode); var decoded = (IEnumerable)TypeCodec.Decode(version, encoded, (ColumnTypeCode)value[1], (IColumnInfo)value[2], originalType); Assert.IsInstanceOf(originalType, decoded); CollectionAssert.AreEqual(valueToEncode, decoded); } } }
public void TestDecodeEmptyString() { TypeCodec coder = FastType.Ascii.GetCodec(Operator.None, false); Assert.AreEqual("", coder.Decode(BitStream("10000000")).ToString()); Assert.AreEqual("\u0000", coder.Decode(BitStream("00000000 10000000")).ToString()); try { coder.Decode(BitStream("00000000 11000001")); Assert.Fail(); } catch (RepErrorException e) { Assert.AreEqual(RepError.StringOverlong, e.Error); } }
public void EncodeDecodeTupleAsSubtypeFactoryTest() { const int version = 3; var initialValues = new object[] { new object[] { new List <Tuple <string> > { new Tuple <string>("val1") }, ColumnTypeCode.List, new ListColumnInfo { ValueTypeCode = ColumnTypeCode.Tuple, ValueTypeInfo = new TupleColumnInfo() { Elements = new List <ColumnDesc>() { new ColumnDesc() { TypeCode = ColumnTypeCode.Text } } } } }, new object[] { new List <Tuple <string, int> > { new Tuple <string, int>("val2ZZ", 0) }, ColumnTypeCode.List, new ListColumnInfo { ValueTypeCode = ColumnTypeCode.Tuple, ValueTypeInfo = new TupleColumnInfo() { Elements = new List <ColumnDesc>() { new ColumnDesc() { TypeCode = ColumnTypeCode.Text }, new ColumnDesc() { TypeCode = ColumnTypeCode.Int } } } } } }; foreach (object[] value in initialValues) { var valueToEncode = (IList)value[0]; var encoded = TypeCodec.Encode(version, valueToEncode); var decoded = (IList)TypeCodec.Decode(version, encoded, (ColumnTypeCode)value[1], (IColumnInfo)value[2]); Assert.AreEqual(valueToEncode, decoded); } }
public void EncodeDecodeNestedMap() { var initialValues = new object[] { new object[] { new SortedDictionary <string, IEnumerable <int> > { { "first", new List <int>(new [] { 1, 2, 1000 }) } }, ColumnTypeCode.Map, new MapColumnInfo { KeyTypeCode = ColumnTypeCode.Text, ValueTypeCode = ColumnTypeCode.List, ValueTypeInfo = new ListColumnInfo { ValueTypeCode = ColumnTypeCode.Int } } }, new object[] { new SortedDictionary <int, IEnumerable <string> > { { 120, new SortedSet <string>(new [] { "a", "b", "c" }) } }, ColumnTypeCode.Map, new MapColumnInfo { KeyTypeCode = ColumnTypeCode.Int, ValueTypeCode = ColumnTypeCode.Set, ValueTypeInfo = new SetColumnInfo { KeyTypeCode = ColumnTypeCode.Text } } }, new object[] { new SortedDictionary <string, IDictionary <string, int> > { { "first-b", new SortedDictionary <string, int> { { "A", 1 }, { "B", 2 } } } }, ColumnTypeCode.Map, new MapColumnInfo { KeyTypeCode = ColumnTypeCode.Text, ValueTypeCode = ColumnTypeCode.Map, ValueTypeInfo = new MapColumnInfo { KeyTypeCode = ColumnTypeCode.Text, ValueTypeCode = ColumnTypeCode.Int } } } }; foreach (var version in _protocolVersions) { foreach (object[] value in initialValues) { var originalType = value[0].GetType(); var valueToEncode = (IEnumerable)value[0]; var encoded = TypeCodec.Encode(version, valueToEncode); var decoded = (IEnumerable)TypeCodec.Decode(version, encoded, (ColumnTypeCode)value[1], (IColumnInfo)value[2], originalType); Assert.IsInstanceOf(originalType, decoded); CollectionAssert.AreEqual(valueToEncode, decoded); } } }
public void EncodeDecodeTupleFactoryTest() { const int version = 3; var initialValues = new object[] { new object[] { new Tuple <string>("val1"), ColumnTypeCode.Tuple, new TupleColumnInfo() { Elements = new List <ColumnDesc>() { new ColumnDesc() { TypeCode = ColumnTypeCode.Text } } } }, new object[] { new Tuple <string, int>("val2", 2), ColumnTypeCode.Tuple, new TupleColumnInfo() { Elements = new List <ColumnDesc>() { new ColumnDesc() { TypeCode = ColumnTypeCode.Text }, new ColumnDesc() { TypeCode = ColumnTypeCode.Int } } } }, new object[] { new Tuple <string, int>(null, -1234), ColumnTypeCode.Tuple, new TupleColumnInfo() { Elements = new List <ColumnDesc>() { new ColumnDesc() { TypeCode = ColumnTypeCode.Text }, new ColumnDesc() { TypeCode = ColumnTypeCode.Int } } } } }; foreach (object[] value in initialValues) { var valueToEncode = (IStructuralEquatable)value[0]; var encoded = TypeCodec.Encode(version, valueToEncode); var decoded = (IStructuralEquatable)TypeCodec.Decode(version, encoded, (ColumnTypeCode)value[1], (IColumnInfo)value[2]); Assert.AreEqual(valueToEncode, decoded); } }
public void EncodeDecodeListSetFactoryTest() { var initialValues = new object[] { //Lists new object[] { new List <int>(new [] { 1, 2, 1000 }), ColumnTypeCode.List, new ListColumnInfo() { ValueTypeCode = ColumnTypeCode.Int } }, new object[] { new List <double>(new [] { -1D, 2.333D, 1.2D }), ColumnTypeCode.List, new ListColumnInfo() { ValueTypeCode = ColumnTypeCode.Double } }, new object[] { new double[] { 5D, 4.333D, 1.2D }, ColumnTypeCode.List, new ListColumnInfo() { ValueTypeCode = ColumnTypeCode.Double } }, //Sets new object[] { new List <decimal>(new [] { -1M, 2.333M, 1.2M, 256M }), ColumnTypeCode.Set, new SetColumnInfo() { KeyTypeCode = ColumnTypeCode.Decimal } }, new object[] { new SortedSet <string>(new [] { "a", "b", "c" }), ColumnTypeCode.Set, new SetColumnInfo() { KeyTypeCode = ColumnTypeCode.Text } }, new object[] { new HashSet <string>(new [] { "ADADD", "AA", "a" }), ColumnTypeCode.Set, new SetColumnInfo() { KeyTypeCode = ColumnTypeCode.Text } }, new object[] { new string[] { "ADADD", "AA", "a" }, ColumnTypeCode.Set, new SetColumnInfo() { KeyTypeCode = ColumnTypeCode.Text } } }; foreach (var version in _protocolVersions) { foreach (object[] value in initialValues) { var originalType = value[0].GetType(); var valueToEncode = (IEnumerable)value[0]; var encoded = TypeCodec.Encode(version, valueToEncode); var decoded = (IEnumerable)TypeCodec.Decode(version, encoded, (ColumnTypeCode)value[1], (IColumnInfo)value[2], originalType); Assert.IsInstanceOf(originalType, decoded); CollectionAssert.AreEqual(valueToEncode, decoded); } } }
public void EncodeDecodeMapFactoryTest() { var initialValues = new object[] { new object[] { new SortedDictionary <string, string>(), ColumnTypeCode.Map, new MapColumnInfo() { KeyTypeCode = ColumnTypeCode.Text, ValueTypeCode = ColumnTypeCode.Text } }, new object[] { new SortedDictionary <string, string> { { "key100", "value100" } }, ColumnTypeCode.Map, new MapColumnInfo() { KeyTypeCode = ColumnTypeCode.Text, ValueTypeCode = ColumnTypeCode.Text } }, new object[] { new SortedDictionary <string, string> { { "key1", "value1" }, { "key2", "value2" } }, ColumnTypeCode.Map, new MapColumnInfo() { KeyTypeCode = ColumnTypeCode.Text, ValueTypeCode = ColumnTypeCode.Text } }, new object[] { new SortedDictionary <string, int> { { "key1", 1 }, { "key2", 2 } }, ColumnTypeCode.Map, new MapColumnInfo() { KeyTypeCode = ColumnTypeCode.Text, ValueTypeCode = ColumnTypeCode.Int } }, new object[] { new SortedDictionary <Guid, string> { { Guid.NewGuid(), "value1" }, { Guid.NewGuid(), "value2" } }, ColumnTypeCode.Map, new MapColumnInfo() { KeyTypeCode = ColumnTypeCode.Uuid, ValueTypeCode = ColumnTypeCode.Text } }, }; foreach (var version in _protocolVersions) { foreach (object[] value in initialValues) { var valueToEncode = (IDictionary)value[0]; var encoded = TypeCodec.Encode(version, valueToEncode); var decoded = (IDictionary)TypeCodec.Decode(version, encoded, (ColumnTypeCode)value[1], (IColumnInfo)value[2], typeof(IDictionary)); CollectionAssert.AreEquivalent(valueToEncode, decoded); } } }
/// <summary> /// Creates a new instance of function metadata based on a schema_function row. /// </summary> internal static AggregateMetadata Build(int protocolVersion, Row row) { var emptyArray = new string[0]; var aggregate = new AggregateMetadata { Name = row.GetValue <string>("aggregate_name"), KeyspaceName = row.GetValue <string>("keyspace_name"), Signature = row.GetValue <string[]>("signature") ?? emptyArray, StateFunction = row.GetValue <string>("state_func"), StateType = TypeCodec.ParseDataType(row.GetValue <string>("state_type")), FinalFunction = row.GetValue <string>("final_func"), ReturnType = TypeCodec.ParseDataType(row.GetValue <string>("return_type")), ArgumentTypes = (row.GetValue <string[]>("argument_types") ?? emptyArray).Select(s => TypeCodec.ParseDataType(s)).ToArray(), }; aggregate.InitialCondition = TypeCodec.Decode(protocolVersion, row.GetValue <byte[]>("initcond"), aggregate.StateType.TypeCode, aggregate.StateType.TypeInfo); return(aggregate); }
internal virtual Row ProcessRowItem(FrameReader reader) { var rowValues = new object[_metadata.Columns.Length]; for (var i = 0; i < _metadata.Columns.Length; i++) { var c = _metadata.Columns[i]; var length = reader.ReadInt32(); if (length < 0) { rowValues[i] = null; continue; } var buffer = GetBuffer(length, c.TypeCode); reader.Read(buffer, 0, length); rowValues[i] = TypeCodec.Decode(_protocolVersion, buffer, c.TypeCode, c.TypeInfo); } return(new Row(rowValues, _metadata.Columns, _metadata.ColumnIndexes)); }
public override IFieldValue Decode(Stream inStream, Group decodeTemplate, Context context, BitVectorReader presenceMapReader) { try { ScalarValue priorValue = null; IDictionary dict = null; QName key = Key; ScalarValue value; int pmapIndex = presenceMapReader.Index; if (IsPresent(presenceMapReader)) { if (context.TraceEnabled) { inStream = new RecordingInputStream(inStream); } if (!_operatorCodec.ShouldDecodeType) { return(_operatorCodec.DecodeValue(null, null, this)); } if (_operatorCodec.DecodeNewValueNeedsPrevious) { dict = context.GetDictionary(Dictionary); priorValue = context.Lookup(dict, decodeTemplate, key); ValidateDictionaryTypeAgainstFieldType(priorValue, _fastType); } ScalarValue decodedValue = _typeCodec.Decode(inStream); value = _operatorCodec.DecodeValue(decodedValue, priorValue, this); if (context.TraceEnabled) { context.DecodeTrace.Field(this, value, decodedValue, ((RecordingInputStream)inStream).Buffer, pmapIndex); } } else { if (_operatorCodec.DecodeEmptyValueNeedsPrevious) { dict = context.GetDictionary(Dictionary); priorValue = context.Lookup(dict, decodeTemplate, key); ValidateDictionaryTypeAgainstFieldType(priorValue, _fastType); } value = _operatorCodec.DecodeEmptyValue(priorValue, this); } ValidateDecodedValueIsCorrectForType(value, _fastType); #warning TODO: Review if this previous "if" statement is needed. // if (Operator != Template.Operator.Operator.DELTA || value != null) if (value != null && (_operatorCodec.DecodeNewValueNeedsPrevious || _operatorCodec.DecodeEmptyValueNeedsPrevious)) { context.Store(dict ?? context.GetDictionary(Dictionary), decodeTemplate, key, value); } return(value); } catch (DynErrorException e) { throw new DynErrorException(e, e.Error, "Error occurred while decoding {0}", this); } }
private object ConvertToObject(int i, byte[] buffer, Type cSharpType) { return(TypeCodec.Decode(ProtocolVersion, buffer, Columns[i].TypeCode, Columns[i].TypeInfo, cSharpType)); }
public override IFieldValue Decode(Stream inStream, Group decodeTemplate, Context context, BitVectorReader presenceMapReader) { try { ScalarValue priorValue = null; IDictionary dict = null; QName key = Key; ScalarValue value; int pmapIndex = presenceMapReader.Index; if (IsPresent(presenceMapReader)) { if (context.TraceEnabled) { inStream = new RecordingInputStream(inStream); } if (!OperatorCodec.ShouldDecodeType) { return(OperatorCodec.DecodeValue(null, null, this)); } if (OperatorCodec.DecodeNewValueNeedsPrevious) { dict = context.GetDictionary(Dictionary); priorValue = context.Lookup(dict, decodeTemplate, key); ValidateDictionaryTypeAgainstFieldType(priorValue, FastType); } ScalarValue decodedValue = TypeCodec.Decode(inStream); value = OperatorCodec.DecodeValue(decodedValue, priorValue, this); if (context.TraceEnabled) { context.DecodeTrace.Field(this, value, decodedValue, ((RecordingInputStream)inStream).Buffer, pmapIndex); } } else { if (OperatorCodec.DecodeEmptyValueNeedsPrevious) { dict = context.GetDictionary(Dictionary); priorValue = context.Lookup(dict, decodeTemplate, key); ValidateDictionaryTypeAgainstFieldType(priorValue, FastType); } value = OperatorCodec.DecodeEmptyValue(priorValue, this); } ValidateDecodedValueIsCorrectForType(value, FastType); // Delta the only operator that ALWAYS return true on IsPresent(presenceMapReader) (UsesPresenceMapBit from AlwaysPresentOperatorCodec always return false) and need previous if ((value != null || !(Operator is Operator.DeltaOperator)) && (OperatorCodec.DecodeNewValueNeedsPrevious || OperatorCodec.DecodeEmptyValueNeedsPrevious)) { context.Store(dict ?? context.GetDictionary(Dictionary), decodeTemplate, key, value); } return(value); } catch (DynErrorException e) { throw new DynErrorException(e, e.Error, "Error occurred while decoding {0}", this); } }
protected static void AssertEncodeDecode(ScalarValue value, String bitString, TypeCodec type) { Assert.AreEqual(bitString, type.Encode(value ?? ScalarValue.NULL)); Assert.AreEqual(value, type.Decode(ByteUtil.CreateByteStream(bitString))); }
protected static void AssertEncodeDecode(ScalarValue value, String bitString, TypeCodec type) { Assert.AreEqual(ByteUtil.ConvertBitStringToFastByteArray(bitString), type.Encode(value ?? ScalarValue.Null)); Assert.AreEqual(value, type.Decode(ByteUtil.CreateByteStream(bitString))); }