Skip to content
This repository
Newer
Older
100644 260 lines (221 sloc) 4.894 kb
1adec35a »
2010-04-17 connections now are object oriented, and basic keyspace and column fa…
1 using System;
2 using Thrift.Transport;
3 using Thrift.Protocol;
4 using Apache.Cassandra;
5
b6643b0c »
2010-11-15 added round robin server manager to connection provider
6 namespace FluentCassandra.Connections
1adec35a »
2010-04-17 connections now are object oriented, and basic keyspace and column fa…
7 {
a7e66afe »
2010-04-17 connections now are object oriented, and basic keyspace and column fa…
8 /// <summary>
9 ///
10 /// </summary>
11 /// <remarks>Borrowed much of the layout from NoRM, I just couldn't resist it was very elegant in its design.</remarks>
12 /// <see href="http://github.com/robconery/NoRM/tree/master/NoRM/Connections/"/>
1adec35a »
2010-04-17 connections now are object oriented, and basic keyspace and column fa…
13 public class Connection : IConnection, IDisposable
14 {
6851535d »
2012-06-20 rounded up a few things for issue #40
15 private TTransport _transport;
16 private Cassandra.Client _client;
17 private readonly object _lock = new object();
1adec35a »
2010-04-17 connections now are object oriented, and basic keyspace and column fa…
18
19 /// <summary>
20 ///
21 /// </summary>
6851535d »
2012-06-20 rounded up a few things for issue #40
22 /// <param name="server"></param>
1adec35a »
2010-04-17 connections now are object oriented, and basic keyspace and column fa…
23 /// <param name="builder"></param>
6851535d »
2012-06-20 rounded up a few things for issue #40
24 public Connection(Server server, IConnectionBuilder builder)
25 : this(server, builder.ConnectionType, builder.BufferSize) { }
494c679f »
2012-05-07 fixed an odd NullReferenceException that was occuring in Connection o…
26
6851535d »
2012-06-20 rounded up a few things for issue #40
27 /// <summary>
28 ///
29 /// </summary>
30 /// <param name="server"></param>
31 /// <param name="connectionType"></param>
32 /// <param name="bufferSize"></param>
33 public Connection(Server server, ConnectionType connectionType, int bufferSize)
34 {
f4b065ba »
2012-05-23 moved around some code from issues #28 and #30 and used DateTimePreci…
35 Created = DateTime.UtcNow;
f5aac51f »
2010-05-30 added support for failover connection provider, to satisfy issue #1.
36 Server = server;
6851535d »
2012-06-20 rounded up a few things for issue #40
37 ConnectionType = connectionType;
38 BufferSize = bufferSize;
1adec35a »
2010-04-17 connections now are object oriented, and basic keyspace and column fa…
39
6851535d »
2012-06-20 rounded up a few things for issue #40
40 InitTransportAndClient();
41 }
42
43 /// <summary>
44 ///
45 /// </summary>
46 private void InitTransportAndClient()
47 {
48 var socket = new TSocket(Server.Host, Server.Port, Server.Timeout * 1000);
1a298c12 »
2011-06-21 added support for simple, buffered, and framed connections, buffered …
49
6851535d »
2012-06-20 rounded up a few things for issue #40
50 switch (ConnectionType)
1a298c12 »
2011-06-21 added support for simple, buffered, and framed connections, buffered …
51 {
52 case ConnectionType.Simple:
53 _transport = socket;
54 break;
55
56 case ConnectionType.Buffered:
6851535d »
2012-06-20 rounded up a few things for issue #40
57 _transport = new TBufferedTransport(socket, BufferSize);
1a298c12 »
2011-06-21 added support for simple, buffered, and framed connections, buffered …
58 break;
59
60 case ConnectionType.Framed:
61 _transport = new TFramedTransport(socket);
62 break;
b6b4d572 »
2012-06-20 still having _transport is null issues. starting to think it is part …
63
64 default:
65 goto case ConnectionType.Framed;
1a298c12 »
2011-06-21 added support for simple, buffered, and framed connections, buffered …
66 }
50d830b2 »
2010-09-04 added thrift from latest build
67
6851535d »
2012-06-20 rounded up a few things for issue #40
68 var protocol = new TBinaryProtocol(_transport);
69 _client = new Cassandra.Client(protocol);
1adec35a »
2010-04-17 connections now are object oriented, and basic keyspace and column fa…
70 }
71
222aa246 »
2010-05-31 connections can now failover, pool, and load balance
72 /// <summary>
73 ///
74 /// </summary>
75 public DateTime Created
76 {
77 get;
78 private set;
79 }
80
81 /// <summary>
82 ///
83 /// </summary>
1adec35a »
2010-04-17 connections now are object oriented, and basic keyspace and column fa…
84 public Server Server
85 {
86 get;
87 private set;
88 }
89
90 /// <summary>
91 ///
92 /// </summary>
6851535d »
2012-06-20 rounded up a few things for issue #40
93 public ConnectionType ConnectionType
94 {
95 get;
96 private set;
97 }
98
99 /// <summary>
100 ///
101 /// </summary>
102 public int BufferSize
103 {
104 get;
105 private set;
106 }
107
108 /// <summary>
109 ///
110 /// </summary>
1adec35a »
2010-04-17 connections now are object oriented, and basic keyspace and column fa…
111 public bool IsOpen
112 {
494c679f »
2012-05-07 fixed an odd NullReferenceException that was occuring in Connection o…
113 get
114 {
b6b4d572 »
2012-06-20 still having _transport is null issues. starting to think it is part …
115 if (_transport == null)
116 return false;
117
6851535d »
2012-06-20 rounded up a few things for issue #40
118 lock (_lock)
119 return _transport.IsOpen;
494c679f »
2012-05-07 fixed an odd NullReferenceException that was occuring in Connection o…
120 }
1adec35a »
2010-04-17 connections now are object oriented, and basic keyspace and column fa…
121 }
122
123 /// <summary>
124 ///
125 /// </summary>
126 public void Open()
127 {
38b12437 »
2012-06-20 added some disposed exceptions to the connection. hopefully this will…
128 CheckWasDisposed();
129
bc3811ba »
2010-06-15 minor bug fixes around handling of exceptions
130 if (IsOpen)
131 return;
132
6851535d »
2012-06-20 rounded up a few things for issue #40
133 if (_transport == null)
134 InitTransportAndClient();
135
136 lock (_lock)
494c679f »
2012-05-07 fixed an odd NullReferenceException that was occuring in Connection o…
137 _transport.Open();
1adec35a »
2010-04-17 connections now are object oriented, and basic keyspace and column fa…
138 }
139
140 /// <summary>
141 ///
142 /// </summary>
143 public void Close()
144 {
38b12437 »
2012-06-20 added some disposed exceptions to the connection. hopefully this will…
145 CheckWasDisposed();
146
bc3811ba »
2010-06-15 minor bug fixes around handling of exceptions
147 if (!IsOpen)
148 return;
149
6851535d »
2012-06-20 rounded up a few things for issue #40
150 lock (_lock)
151 {
494c679f »
2012-05-07 fixed an odd NullReferenceException that was occuring in Connection o…
152 _transport.Close();
6851535d »
2012-06-20 rounded up a few things for issue #40
153 _transport = null;
154 _client = null;
155 }
1adec35a »
2010-04-17 connections now are object oriented, and basic keyspace and column fa…
156 }
157
ea9226f5 »
2012-05-24 added support for CQL 3 and selecting the CQL version
158 /// <summary>
159 ///
160 /// </summary>
161 /// <param name="keyspace"></param>
494c679f »
2012-05-07 fixed an odd NullReferenceException that was occuring in Connection o…
162 public void SetKeyspace(string keyspace)
50d830b2 »
2010-09-04 added thrift from latest build
163 {
6851535d »
2012-06-20 rounded up a few things for issue #40
164 CheckWasDisposed();
165
166 if (!IsOpen)
167 throw new CassandraConnectionException("A connection to Cassandra has not been opened.");
168
50d830b2 »
2010-09-04 added thrift from latest build
169 Client.set_keyspace(keyspace);
170 }
171
1adec35a »
2010-04-17 connections now are object oriented, and basic keyspace and column fa…
172 /// <summary>
173 ///
174 /// </summary>
ea9226f5 »
2012-05-24 added support for CQL 3 and selecting the CQL version
175 /// <param name="cqlVersion"></param>
176 public void SetCqlVersion(string cqlVersion)
177 {
6851535d »
2012-06-20 rounded up a few things for issue #40
178 CheckWasDisposed();
179
180 if (!IsOpen)
181 throw new CassandraConnectionException("A connection to Cassandra has not been opened.");
182
ea9226f5 »
2012-05-24 added support for CQL 3 and selecting the CQL version
183 Client.set_cql_version(cqlVersion);
184 }
185
186 /// <summary>
187 ///
188 /// </summary>
1adec35a »
2010-04-17 connections now are object oriented, and basic keyspace and column fa…
189 public Cassandra.Client Client
190 {
6851535d »
2012-06-20 rounded up a few things for issue #40
191 get
192 {
193 lock(_lock)
194 return _client;
38b12437 »
2012-06-20 added some disposed exceptions to the connection. hopefully this will…
195 }
1adec35a »
2010-04-17 connections now are object oriented, and basic keyspace and column fa…
196 }
197
198 /// <summary>
50d830b2 »
2010-09-04 added thrift from latest build
199 ///
200 /// </summary>
201 public override string ToString()
202 {
203 return String.Format("{0}/{1}", Server, Created);
204 }
205
494c679f »
2012-05-07 fixed an odd NullReferenceException that was occuring in Connection o…
206 #region IDisposable Members
207
208 /// <summary>
209 ///
38b12437 »
2012-06-20 added some disposed exceptions to the connection. hopefully this will…
210 /// </summary>
211 private void CheckWasDisposed()
212 {
213 if (WasDisposed)
214 throw new ObjectDisposedException("connection has been disposed of");
215 }
216
217 /// <summary>
218 ///
494c679f »
2012-05-07 fixed an odd NullReferenceException that was occuring in Connection o…
219 /// </summary>
220 public bool WasDisposed
221 {
222 get;
223 private set;
224 }
225
50d830b2 »
2010-09-04 added thrift from latest build
226 /// <summary>
1adec35a »
2010-04-17 connections now are object oriented, and basic keyspace and column fa…
227 /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
228 /// </summary>
229 public void Dispose()
230 {
231 Dispose(true);
046825e7 »
2011-05-23 updated connections to have a more standarding naming connvetion with…
232 GC.SuppressFinalize(this);
1adec35a »
2010-04-17 connections now are object oriented, and basic keyspace and column fa…
233 }
234
235 /// <summary>
236 /// Releases unmanaged and - optionally - managed resources
237 /// </summary>
238 /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
239 protected virtual void Dispose(bool disposing)
240 {
ee333716 »
2012-06-20 cleaned up some of the disposal code in hopes of correcting issue #40
241 if (!WasDisposed && disposing && _transport != null)
242 {
243 Close();
244 }
1adec35a »
2010-04-17 connections now are object oriented, and basic keyspace and column fa…
245
494c679f »
2012-05-07 fixed an odd NullReferenceException that was occuring in Connection o…
246 WasDisposed = true;
1adec35a »
2010-04-17 connections now are object oriented, and basic keyspace and column fa…
247 }
248
249 /// <summary>
250 /// Releases unmanaged resources and performs other cleanup operations before the
251 /// <see cref="Connection"/> is reclaimed by garbage collection.
252 /// </summary>
253 ~Connection()
254 {
255 Dispose(false);
256 }
494c679f »
2012-05-07 fixed an odd NullReferenceException that was occuring in Connection o…
257
258 #endregion
1adec35a »
2010-04-17 connections now are object oriented, and basic keyspace and column fa…
259 }
494c679f »
2012-05-07 fixed an odd NullReferenceException that was occuring in Connection o…
260 }
Something went wrong with that request. Please try again.