This repository has been archived by the owner on May 25, 2021. It is now read-only.
/
Connection.cs
193 lines (165 loc) · 3.6 KB
/
Connection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using System;
using Thrift.Transport;
using Thrift.Protocol;
using Apache.Cassandra;
namespace FluentCassandra.Connections
{
/// <summary>
///
/// </summary>
/// <remarks>Borrowed much of the layout from NoRM, I just couldn't resist it was very elegant in its design.</remarks>
/// <see href="http://github.com/robconery/NoRM/tree/master/NoRM/Connections/"/>
public class Connection : IConnection, IDisposable
{
private readonly ConnectionType _connectionType;
private readonly int _bufferSize;
private readonly TTransport _transport;
private readonly TProtocol _protocol;
private readonly Cassandra.Client _client;
/// <summary>
///
/// </summary>
/// <param name="builder"></param>
internal Connection(Server server, ConnectionBuilder builder)
{
_connectionType = builder.ConnectionType;
_bufferSize = builder.BufferSize;
Created = DateTime.UtcNow;
Server = server;
var socket = new TSocket(server.Host, server.Port, server.Timeout * 1000);
switch (_connectionType)
{
case ConnectionType.Simple:
_transport = socket;
break;
case ConnectionType.Buffered:
_transport = new TBufferedTransport(socket, _bufferSize);
break;
case ConnectionType.Framed:
_transport = new TFramedTransport(socket);
break;
}
_protocol = new TBinaryProtocol(_transport);
_client = new Cassandra.Client(_protocol);
}
/// <summary>
///
/// </summary>
public DateTime Created
{
get;
private set;
}
/// <summary>
///
/// </summary>
public Server Server
{
get;
private set;
}
/// <summary>
///
/// </summary>
public bool IsOpen
{
get
{
lock (_transport)
{
if (_transport == null)
return false;
try { return _transport.IsOpen; }
catch { return false; }
}
}
}
/// <summary>
///
/// </summary>
public void Open()
{
if (IsOpen)
return;
lock (_transport)
_transport.Open();
}
/// <summary>
///
/// </summary>
public void Close()
{
if (!IsOpen)
return;
lock (_transport)
_transport.Close();
}
/// <summary>
///
/// </summary>
/// <param name="keyspace"></param>
public void SetKeyspace(string keyspace)
{
Client.set_keyspace(keyspace);
}
/// <summary>
///
/// </summary>
/// <param name="cqlVersion"></param>
public void SetCqlVersion(string cqlVersion)
{
Client.set_cql_version(cqlVersion);
}
/// <summary>
///
/// </summary>
public Cassandra.Client Client
{
get { return _client; }
}
/// <summary>
///
/// </summary>
public override string ToString()
{
return String.Format("{0}/{1}", Server, Created);
}
#region IDisposable Members
/// <summary>
///
/// </summary>
public bool WasDisposed
{
get;
private set;
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
if (WasDisposed)
return;
Close();
WasDisposed = true;
}
/// <summary>
/// Releases unmanaged resources and performs other cleanup operations before the
/// <see cref="Connection"/> is reclaimed by garbage collection.
/// </summary>
~Connection()
{
Dispose(false);
}
#endregion
}
}