Skip to content
Newer
Older
100644 265 lines (219 sloc) 6.94 KB
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using Apache.Cassandra;
b6643b0 @nberardi added round robin server manager to connection provider
nberardi authored
5 using FluentCassandra.Connections;
321da49 @nberardi refactoring of way operations happen, and created a wrapper for the c…
nberardi authored
6 using FluentCassandra.Operations;
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
7
8 namespace FluentCassandra
9 {
10 public class CassandraSession : IDisposable
11 {
12 [ThreadStatic]
13 private static CassandraSession _current;
14
15 public static CassandraSession Current
16 {
17 get { return _current; }
18 internal set { _current = value; }
19 }
20
50d830b @nberardi added thrift from latest build
nberardi authored
21 #region Cassandra System For Server
22
7ba4ce5 @nberardi added support for a couple more features that were added to the clien…
nberardi authored
23 public static KsDef GetKeyspace(Server server, string keyspace)
24 {
25 using (var session = new CassandraSession(new ConnectionBuilder(keyspace, server.Host, server.Port)))
26 return session.GetClient().describe_keyspace(keyspace);
27 }
28
50d830b @nberardi added thrift from latest build
nberardi authored
29 public static string AddKeyspace(Server server, KsDef definition)
30 {
ce017c8 @nberardi all tests working for 0.7
nberardi authored
31 using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port)))
32 return session.GetClient(setKeyspace: false).system_add_keyspace(definition);
7ba4ce5 @nberardi added support for a couple more features that were added to the clien…
nberardi authored
33 }
34
35 public static string UpdateKeyspace(Server server, KsDef definition)
36 {
37 using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port)))
38 return session.GetClient(setKeyspace: false).system_update_keyspace(definition);
50d830b @nberardi added thrift from latest build
nberardi authored
39 }
40
41 public static string DropKeyspace(Server server, string keyspace)
42 {
ce017c8 @nberardi all tests working for 0.7
nberardi authored
43 using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port)))
44 return session.GetClient(setKeyspace: false).system_drop_keyspace(keyspace);
50d830b @nberardi added thrift from latest build
nberardi authored
45 }
46
47 #endregion
48
1651f02 @nberardi added descriptive opperations so that you can query the server for in…
nberardi authored
49 #region Cassandra Descriptions For Server
50
ce017c8 @nberardi all tests working for 0.7
nberardi authored
51 public static bool KeyspaceExists(Server server, string keyspaceName)
52 {
53 return DescribeKeyspaces(server).Any(keyspace => keyspace.KeyspaceName == keyspaceName);
54 }
55
50d830b @nberardi added thrift from latest build
nberardi authored
56 public static IEnumerable<CassandraKeyspace> DescribeKeyspaces(Server server)
1651f02 @nberardi added descriptive opperations so that you can query the server for in…
nberardi authored
57 {
ce017c8 @nberardi all tests working for 0.7
nberardi authored
58 using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port)))
1651f02 @nberardi added descriptive opperations so that you can query the server for in…
nberardi authored
59 {
74b23c0 @nberardi now compiles against 0.7 beta-2 thrift interface
nberardi authored
60 IEnumerable<KsDef> keyspaces = session.GetClient(setKeyspace: false).describe_keyspaces();
8c2e9f5 @nberardi composite types are now working
nberardi authored
61 return keyspaces.Select(keyspace => new CassandraKeyspace(keyspace)).ToList();
1651f02 @nberardi added descriptive opperations so that you can query the server for in…
nberardi authored
62 }
63 }
64
50d830b @nberardi added thrift from latest build
nberardi authored
65 public static string DescribeClusterName(Server server)
1651f02 @nberardi added descriptive opperations so that you can query the server for in…
nberardi authored
66 {
ce017c8 @nberardi all tests working for 0.7
nberardi authored
67 using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port)))
1651f02 @nberardi added descriptive opperations so that you can query the server for in…
nberardi authored
68 {
ce017c8 @nberardi all tests working for 0.7
nberardi authored
69 string response = session.GetClient(setKeyspace: false).describe_cluster_name();
1651f02 @nberardi added descriptive opperations so that you can query the server for in…
nberardi authored
70 return response;
71 }
72 }
73
7ba4ce5 @nberardi added support for a couple more features that were added to the clien…
nberardi authored
74 public static Dictionary<string, List<string>> DescribeSchemaVersions(Server server)
75 {
76 using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port)))
77 {
78 var response = session.GetClient(setKeyspace: false).describe_schema_versions();
79 return response;
80 }
81 }
82
50d830b @nberardi added thrift from latest build
nberardi authored
83 public static string DescribeVersion(Server server)
1651f02 @nberardi added descriptive opperations so that you can query the server for in…
nberardi authored
84 {
ce017c8 @nberardi all tests working for 0.7
nberardi authored
85 using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port)))
1651f02 @nberardi added descriptive opperations so that you can query the server for in…
nberardi authored
86 {
ce017c8 @nberardi all tests working for 0.7
nberardi authored
87 string response = session.GetClient(setKeyspace: false).describe_version();
1651f02 @nberardi added descriptive opperations so that you can query the server for in…
nberardi authored
88 return response;
89 }
90 }
91
50d830b @nberardi added thrift from latest build
nberardi authored
92 public static string DescribePartitioner(Server server)
1651f02 @nberardi added descriptive opperations so that you can query the server for in…
nberardi authored
93 {
ce017c8 @nberardi all tests working for 0.7
nberardi authored
94 using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port)))
1651f02 @nberardi added descriptive opperations so that you can query the server for in…
nberardi authored
95 {
ce017c8 @nberardi all tests working for 0.7
nberardi authored
96 string response = session.GetClient(setKeyspace: false).describe_partitioner();
1651f02 @nberardi added descriptive opperations so that you can query the server for in…
nberardi authored
97 return response;
98 }
99 }
100
7ba4ce5 @nberardi added support for a couple more features that were added to the clien…
nberardi authored
101 public static string DescribeSnitch(Server server)
102 {
103 using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port)))
104 {
105 string response = session.GetClient(setKeyspace: false).describe_snitch();
106 return response;
107 }
108 }
109
1651f02 @nberardi added descriptive opperations so that you can query the server for in…
nberardi authored
110 #endregion
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
111
112 private IConnection _connection;
113
114 public CassandraSession(ConnectionBuilder connectionBuilder)
115 : this(ConnectionProviderFactory.Get(connectionBuilder), connectionBuilder.ReadConsistency, connectionBuilder.WriteConsistency) { }
116
2645bc7 @nberardi cleaned up the use of sessions in the code
nberardi authored
117 public CassandraSession(ConnectionBuilder connectionBuilder, ConsistencyLevel read, ConsistencyLevel write)
118 : this(ConnectionProviderFactory.Get(connectionBuilder), read, write) { }
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
119
120 public CassandraSession(IConnectionProvider connectionProvider, ConsistencyLevel read, ConsistencyLevel write)
121 {
122 if (Current != null)
123 throw new CassandraException("Cannot create a new session while there is one already active.");
124
125 ConnectionProvider = connectionProvider;
126 ReadConsistency = read;
127 WriteConsistency = write;
365b683 @nberardi still working on getting the app to compile after the breaking change…
nberardi authored
128 Keyspace = new CassandraKeyspace(connectionProvider.Builder.Keyspace);
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
129
e93edc7 @nberardi improved the login API, and allowed it to be set in the connection st…
nberardi authored
130 IsAuthenticated = false;
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
131 Current = this;
132 }
133
2645bc7 @nberardi cleaned up the use of sessions in the code
nberardi authored
134 /// <summary>
135 ///
136 /// </summary>
365b683 @nberardi still working on getting the app to compile after the breaking change…
nberardi authored
137 public IConnectionProvider ConnectionProvider { get; private set; }
138
2645bc7 @nberardi cleaned up the use of sessions in the code
nberardi authored
139 /// <summary>
140 ///
141 /// </summary>
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
142 public ConsistencyLevel ReadConsistency { get; private set; }
143
2645bc7 @nberardi cleaned up the use of sessions in the code
nberardi authored
144 /// <summary>
145 ///
146 /// </summary>
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
147 public ConsistencyLevel WriteConsistency { get; private set; }
148
2645bc7 @nberardi cleaned up the use of sessions in the code
nberardi authored
149 /// <summary>
150 ///
151 /// </summary>
365b683 @nberardi still working on getting the app to compile after the breaking change…
nberardi authored
152 public CassandraKeyspace Keyspace { get; private set; }
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
153
2645bc7 @nberardi cleaned up the use of sessions in the code
nberardi authored
154 /// <summary>
155 ///
156 /// </summary>
e93edc7 @nberardi improved the login API, and allowed it to be set in the connection st…
nberardi authored
157 public bool IsAuthenticated { get; private set; }
158
2645bc7 @nberardi cleaned up the use of sessions in the code
nberardi authored
159 /// <summary>
160 ///
161 /// </summary>
162 /// <param name="setKeyspace"></param>
163 /// <returns></returns>
321da49 @nberardi refactoring of way operations happen, and created a wrapper for the c…
nberardi authored
164 internal CassandraClientWrapper GetClient(bool setKeyspace = true)
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
165 {
50d830b @nberardi added thrift from latest build
nberardi authored
166 if (_connection == null)
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
167 _connection = ConnectionProvider.Open();
50d830b @nberardi added thrift from latest build
nberardi authored
168
169 if (!_connection.IsOpen)
170 _connection.Open();
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
171
ce017c8 @nberardi all tests working for 0.7
nberardi authored
172 if (setKeyspace)
23a0cbf @nberardi fixed build error
nberardi authored
173 _connection.SetKeyspace(Keyspace.KeyspaceName);
ce017c8 @nberardi all tests working for 0.7
nberardi authored
174
321da49 @nberardi refactoring of way operations happen, and created a wrapper for the c…
nberardi authored
175 return new CassandraClientWrapper(_connection.Client);
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
176 }
177
2645bc7 @nberardi cleaned up the use of sessions in the code
nberardi authored
178 /// <summary>
179 ///
180 /// </summary>
181 public void Login()
182 {
183 var builder = ConnectionProvider.Builder;
184
185 if (String.IsNullOrWhiteSpace(builder.Username) || String.IsNullOrWhiteSpace(builder.Password))
186 throw new CassandraException("No username and/or password was set in the connection string, please use Login(username, password) method.");
187
188 Login(builder.Username, builder.Password);
189 }
190
191 /// <summary>
192 ///
193 /// </summary>
194 /// <param name="username"></param>
195 /// <param name="password"></param>
e93edc7 @nberardi improved the login API, and allowed it to be set in the connection st…
nberardi authored
196 public void Login(string username, string password)
7ba4ce5 @nberardi added support for a couple more features that were added to the clien…
nberardi authored
197 {
e93edc7 @nberardi improved the login API, and allowed it to be set in the connection st…
nberardi authored
198 var auth = new AuthenticationRequest {
199 Credentials = new Dictionary<string, string> { { "username", username }, { "password", password } }
200 };
201
202 try
203 {
204 GetClient().login(auth);
205 IsAuthenticated = true;
206 }
207 catch (Exception exc)
208 {
209 IsAuthenticated = false;
210 throw new CassandraException("Login failed.", exc);
211 }
7ba4ce5 @nberardi added support for a couple more features that were added to the clien…
nberardi authored
212 }
213
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
214 #region IDisposable Members
215
216 /// <summary>
217 ///
218 /// </summary>
2645bc7 @nberardi cleaned up the use of sessions in the code
nberardi authored
219 public bool WasDisposed
220 {
221 get;
222 private set;
223 }
224
225 /// <summary>
226 ///
227 /// </summary>
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
228 public void Dispose()
229 {
230 Dispose(true);
046825e @nberardi updated connections to have a more standarding naming connvetion with…
nberardi authored
231 GC.SuppressFinalize(this);
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
232 }
233
234 /// <summary>
235 /// The dispose.
236 /// </summary>
237 /// <param name="disposing">
238 /// The disposing.
239 /// </param>
240 protected virtual void Dispose(bool disposing)
241 {
2645bc7 @nberardi cleaned up the use of sessions in the code
nberardi authored
242 if (!WasDisposed && disposing)
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
243 {
365b683 @nberardi still working on getting the app to compile after the breaking change…
nberardi authored
244 if (_connection != null)
245 ConnectionProvider.Close(_connection);
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
246
247 if (Current == this)
248 Current = null;
249 }
250
2645bc7 @nberardi cleaned up the use of sessions in the code
nberardi authored
251 WasDisposed = true;
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
252 }
253
254 /// <summary>
365b683 @nberardi still working on getting the app to compile after the breaking change…
nberardi authored
255 /// Finalizes an instance of the <see cref="CassandraSession"/> class.
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
256 /// </summary>
257 ~CassandraSession()
258 {
365b683 @nberardi still working on getting the app to compile after the breaking change…
nberardi authored
259 Dispose(false);
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
260 }
261
262 #endregion
263 }
264 }
Something went wrong with that request. Please try again.