public int WriteFrame(short streamId, MemoryStream stream, Serializer serializer) { var wb = new FrameWriter(stream, serializer); wb.WriteFrameHeader(0x00, streamId, OpCode); wb.WriteBytes(_token); return wb.Close(); }
public int WriteFrame(short streamId, MemoryStream stream, Serializer serializer) { var wb = new FrameWriter(stream, serializer); wb.WriteFrameHeader(0x00, streamId, OpCode); wb.WriteStringList(_eventTypes); return wb.Close(); }
public FrameWriter(MemoryStream stream, Serializer serializer) { _stream = stream; _serializer = serializer; _offset = stream.Position; _version = serializer.ProtocolVersion; }
internal Session(ICluster cluster, Configuration configuration, string keyspace, Serializer serializer) { _serializer = serializer; Cluster = cluster; Configuration = configuration; Keyspace = keyspace; UserDefinedTypes = new UdtMappingDefinitions(this, serializer); _connectionPool = new ConcurrentDictionary<IPEndPoint, HostConnectionPool>(); }
public int WriteFrame(short streamId, MemoryStream stream, Serializer serializer) { var wb = new FrameWriter(stream, serializer); wb.WriteFrameHeader((byte)_headerFlags, streamId, OpCode); if (Payload != null) { wb.WriteBytesMap(Payload); } wb.WriteLongString(Query); return wb.Close(); }
public HostConnectionPool(Host host, HostDistance distance, Configuration config, Serializer serializer) { _host = host; _host.CheckedAsDown += OnHostCheckedAsDown; _host.Down += OnHostDown; _host.Up += OnHostUp; _host.Remove += OnHostRemoved; _distance = distance; _config = config; _serializer = serializer; _timer = config.Timer; }
public void Should_Allow_Custom_Udt_Serializers() { var typeSerializer = new UdtSerializerWrapper(); var serializer = new Serializer(4, new ITypeSerializer[] { typeSerializer }); var buffer = serializer.Serialize(new object()); CollectionAssert.AreEqual(Encoding.UTF8.GetBytes("DUMMY UDT SERIALIZED"), buffer); CollectionAssert.AreEqual(buffer, (IEnumerable)serializer.Deserialize(buffer, ColumnTypeCode.Udt, new UdtColumnInfo("ks1.udt1"))); //Check that other serializers are still working CollectionAssert.AreEqual(new byte[] { 0, 0, 0, 10 }, serializer.Serialize(10)); CollectionAssert.AreEqual(new byte[] { 0x61, 0x62 }, serializer.Serialize("ab")); Assert.AreEqual(1, typeSerializer.DeserializationCounter); Assert.AreEqual(1, typeSerializer.SerializationCounter); }
public void Should_Allow_Custom_Cql_Type_Serializers() { var typeSerializer = new DummyCustomTypeSerializer(); var serializer = new Serializer(4, new ITypeSerializer[] { typeSerializer }); var value = new DummyCustomType(new byte[] { 1, 2 }); var buffer = serializer.Serialize(value); CollectionAssert.AreEqual(new byte[] { 1, 2 }, buffer); var deserializedValue = serializer.Deserialize(buffer, ColumnTypeCode.Custom, typeSerializer.TypeInfo); Assert.IsInstanceOf<DummyCustomType>(deserializedValue); var deserializedCustom = (DummyCustomType)deserializedValue; CollectionAssert.AreEqual(value.Buffer, deserializedCustom.Buffer); //Check that other serializers are still working CollectionAssert.AreEqual(new byte[] { 0, 0, 0, 10 }, serializer.Serialize(10)); CollectionAssert.AreEqual(new byte[] { 0x61 }, serializer.Serialize("a")); }
public void Should_Allow_Custom_Primitive_Serializers() { var serializer = new Serializer(4, new [] {new BigDecimalSerializer()}); var value = new BigDecimal(5, 1); var buffer = serializer.Serialize(value); CollectionAssert.AreEqual(new byte[] { 0, 0, 0, 5, 1 }, buffer); var deserializedValue = serializer.Deserialize(buffer, ColumnTypeCode.Decimal, null); Assert.IsInstanceOf<BigDecimal>(deserializedValue); var deserializedDecimal = (BigDecimal) deserializedValue; Assert.AreEqual("0.00001", deserializedDecimal.ToString()); Assert.AreEqual(value.Scale, deserializedDecimal.Scale); Assert.AreEqual(value.UnscaledValue, deserializedDecimal.UnscaledValue); //Check that other serializers are still working CollectionAssert.AreEqual(new byte[] { 0, 0, 0, 10 }, serializer.Serialize(10)); CollectionAssert.AreEqual(new byte[] { 0x61 }, serializer.Serialize("a")); }
public int WriteFrame(short streamId, MemoryStream stream, Serializer serializer) { if (serializer.ProtocolVersion > 1) { throw new NotSupportedException("Credentials request is only supported in C* = 1.2.x"); } var wb = new FrameWriter(stream, serializer); wb.WriteFrameHeader(0x00, streamId, OpCode); wb.WriteUInt16((ushort) _credentials.Count); foreach (var kv in _credentials) { wb.WriteString(kv.Key); wb.WriteString(kv.Value); } return wb.Close(); }
public int WriteFrame(short streamId, MemoryStream stream, Serializer serializer) { var wb = new FrameWriter(stream, serializer); if (Payload != null) { _headerFlags |= FrameHeader.HeaderFlag.CustomPayload; } wb.WriteFrameHeader((byte)_headerFlags, streamId, OpCode); if (Payload != null) { //A custom payload for this request wb.WriteBytesMap(Payload); } wb.WriteShortBytes(_id); _queryOptions.Write(wb, true); return wb.Close(); }
public Frame(FrameHeader header, Stream body, Serializer serializer) { if (header == null) { throw new ArgumentNullException("header"); } if (body == null) { throw new ArgumentNullException("body"); } if (serializer == null) { throw new ArgumentNullException("serializer"); } _header = header; _body = body; _serializer = serializer; }
public int WriteFrame(short streamId, MemoryStream stream, Serializer serializer) { //protocol v2: <type><n><query_1>...<query_n><consistency> //protocol v3: <type><n><query_1>...<query_n><consistency><flags>[<serial_consistency>][<timestamp>] var protocolVersion = serializer.ProtocolVersion; var wb = new FrameWriter(stream, serializer); if (Payload != null) { _headerFlags |= FrameHeader.HeaderFlag.CustomPayload; } wb.WriteFrameHeader((byte)_headerFlags, streamId, OpCode); if (Payload != null) { //A custom payload for this request wb.WriteBytesMap(Payload); } wb.WriteByte((byte) _type); wb.WriteInt16((short) _requests.Count); foreach (var br in _requests) { br.WriteToBatch(wb); } wb.WriteInt16((short) Consistency); if (protocolVersion >= 3) { wb.WriteByte((byte)_batchFlags); } if (_serialConsistency != null) { wb.WriteInt16((short)_serialConsistency.Value); } if (_timestamp != null) { //Expressed in microseconds wb.WriteLong(TypeSerializer.SinceUnixEpoch(_timestamp.Value).Ticks / 10); } return wb.Close(); }
internal ControlConnection(byte initialProtocolVersion, Configuration config, Metadata metadata) { _metadata = metadata; _reconnectionPolicy = config.Policies.ReconnectionPolicy; _reconnectionSchedule = _reconnectionPolicy.NewSchedule(); _reconnectionTimer = new Timer(_ => Reconnect(), null, Timeout.Infinite, Timeout.Infinite); _config = config; _serializer = new Serializer(initialProtocolVersion, config.TypeSerializers); }
public Connection(Serializer serializer, IPEndPoint endpoint, Configuration configuration) { if (serializer == null) { throw new ArgumentNullException("serializer"); } if (configuration == null) { throw new ArgumentNullException("configuration"); } if (configuration.BufferPool == null) { throw new ArgumentNullException(null, "BufferPool can not be null"); } _serializer = serializer; Configuration = configuration; _tcpSocket = new TcpSocket(endpoint, configuration.SocketOptions, configuration.ProtocolOptions.SslOptions); _idleTimer = new Timer(IdleTimeoutHandler, null, Timeout.Infinite, Timeout.Infinite); }
public void PreparedStatement_Bind_SetsRoutingKey_Multiple() { const int protocolVersion = 2; var metadata = new RowSetMetadata(null) { Columns = new[] { new CqlColumn { Name = "id2" }, new CqlColumn { Name = "id1" } } }; var ps = GetPrepared("SELECT * FROM tbl1 WHERE id2 = ? and id1", metadata, protocolVersion); ps.SetPartitionKeys(new[] { new TableColumn() { Name = "id1" }, new TableColumn() { Name = "id2" } }); //The routing key is formed by the parameters at position 1 and 0 CollectionAssert.AreEqual(new[] { 1, 0 }, ps.RoutingIndexes); Assert.Null(ps.RoutingKey); var bound = ps.Bind(2001, 1001); Assert.NotNull(bound.RoutingKey); var serializer = new Serializer(protocolVersion); var expectedRoutingKey = new byte[0] .Concat(new byte[] {0, 4}) .Concat(serializer.Serialize(1001)) .Concat(new byte[] {0}) .Concat(new byte[] {0, 4}) .Concat(serializer.Serialize(2001)) .Concat(new byte[] {0}); CollectionAssert.AreEqual(expectedRoutingKey, bound.RoutingKey.RawRoutingKey); }
public void Create_With_Frozen_Udt() { string createQuery = null; var serializer = new Serializer(4); var sessionMock = GetSessionMock(serializer); sessionMock .Setup(s => s.Execute(It.IsAny<string>())) .Returns(() => new RowSet()) .Callback<string>(q => createQuery = q); var definition = new Map<UdtAndTuplePoco>() .PartitionKey(c => c.Id1) .Column(c => c.Id1, cm => cm.WithName("id")) .Column(c => c.Udt1, cm => cm.WithName("my_udt").WithDbType<Song>().AsFrozen()) .ExplicitColumns() .TableName("tbl1"); var udtInfo = new UdtColumnInfo("song"); udtInfo.Fields.Add(new ColumnDesc { Name = "title", TypeCode = ColumnTypeCode.Ascii }); udtInfo.Fields.Add(new ColumnDesc { Name = "releasedate", TypeCode = ColumnTypeCode.Timestamp }); var udtMap = UdtMap.For<Song>(); udtMap.SetSerializer(serializer); udtMap.Build(udtInfo); serializer.SetUdtMap("song", udtMap); var table = GetTable<UdtAndTuplePoco>(sessionMock.Object, definition); table.Create(); Assert.AreEqual("CREATE TABLE tbl1 (id uuid, my_udt frozen<song>, PRIMARY KEY (id))", createQuery); }
/// <summary> /// Initializes once (Thread-safe) the control connection and metadata associated with the Cluster instance /// </summary> private void Init() { if (_initialized) { //It was already initialized return; } lock (_initLock) { if (_initialized) { //It was initialized when waiting on the lock return; } if (_initException != null) { //There was an exception that is not possible to recover from throw _initException; } var protocolVersion = (byte) MaxProtocolVersion; if (Configuration.ProtocolOptions.MaxProtocolVersion != null && Configuration.ProtocolOptions.MaxProtocolVersion < MaxProtocolVersion ) { protocolVersion = Configuration.ProtocolOptions.MaxProtocolVersion.Value; } //create the buffer pool with 16KB for small buffers and 256Kb for large buffers. Configuration.BufferPool = new RecyclableMemoryStreamManager(16 * 1024, 256 * 1024, ProtocolOptions.MaximumFrameLength); _controlConnection = new ControlConnection(protocolVersion, Configuration, _metadata); _metadata.ControlConnection = _controlConnection; try { _controlConnection.Init(); _serializer = _controlConnection.Serializer; //Initialize policies Configuration.Policies.LoadBalancingPolicy.Initialize(this); Configuration.Policies.SpeculativeExecutionPolicy.Initialize(this); } catch (NoHostAvailableException) { //No host available now, maybe later it can recover from throw; } catch (Exception ex) { //There was an error that the driver is not able to recover from //Store the exception for the following times _initException = ex; //Throw the actual exception for the first time throw; } Configuration.Timer = new HashedWheelTimer(); _logger.Info("Cluster Connected using binary protocol version: [" + _serializer.ProtocolVersion + "]"); _initialized = true; _metadata.Hosts.Added += OnHostAdded; _metadata.Hosts.Removed += OnHostRemoved; } }
public FrameReader(Stream stream, Serializer serializer) { _stream = stream; _serializer = serializer; }
internal PreparedStatement(RowSetMetadata metadata, byte[] id, string cql, string keyspace, Serializer serializer) { Metadata = metadata; Id = id; Cql = cql; Keyspace = keyspace; _serializer = serializer; }
internal BoundStatement(PreparedStatement statement, Serializer serializer) : this(statement) { _serializer = serializer; }
private static Mock<ISession> GetSessionMock(Serializer serializer = null) { if (serializer == null) { serializer = new Serializer(4); } var sessionMock = new Mock<ISession>(MockBehavior.Strict); var config = new Configuration(); var metadata = new Metadata(config); var ccMock = new Mock<IMetadataQueryProvider>(MockBehavior.Strict); ccMock.Setup(cc => cc.Serializer).Returns(serializer); metadata.ControlConnection = ccMock.Object; var clusterMock = new Mock<ICluster>(); clusterMock.Setup(c => c.Metadata).Returns(metadata); sessionMock.Setup(s => s.Cluster).Returns(clusterMock.Object); return sessionMock; }