Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 223 lines (179 sloc) 6.214 kb
e4efc4c @nberardi added support for session based configurations, as well as dynamic prope...
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;
e4efc4c @nberardi added support for session based configurations, as well as dynamic prope...
nberardi authored
6
7 namespace FluentCassandra
8 {
9 public class CassandraSession : IDisposable
10 {
11 [ThreadStatic]
12 private static CassandraSession _current;
13
14 public static CassandraSession Current
15 {
16 get { return _current; }
17 internal set { _current = value; }
18 }
19
50d830b @nberardi added thrift from latest build
nberardi authored
20 #region Cassandra System For Server
21
7ba4ce5 @nberardi added support for a couple more features that were added to the client i...
nberardi authored
22 public static KsDef GetKeyspace(Server server, string keyspace)
23 {
24 using (var session = new CassandraSession(new ConnectionBuilder(keyspace, server.Host, server.Port)))
25 return session.GetClient().describe_keyspace(keyspace);
26 }
27
50d830b @nberardi added thrift from latest build
nberardi authored
28 public static string AddKeyspace(Server server, KsDef definition)
29 {
ce017c8 @nberardi all tests working for 0.7
nberardi authored
30 using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port)))
31 return session.GetClient(setKeyspace: false).system_add_keyspace(definition);
7ba4ce5 @nberardi added support for a couple more features that were added to the client i...
nberardi authored
32 }
33
34 public static string UpdateKeyspace(Server server, KsDef definition)
35 {
36 using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port)))
37 return session.GetClient(setKeyspace: false).system_update_keyspace(definition);
50d830b @nberardi added thrift from latest build
nberardi authored
38 }
39
40 public static string DropKeyspace(Server server, string keyspace)
41 {
ce017c8 @nberardi all tests working for 0.7
nberardi authored
42 using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port)))
43 return session.GetClient(setKeyspace: false).system_drop_keyspace(keyspace);
50d830b @nberardi added thrift from latest build
nberardi authored
44 }
45
46 #endregion
47
1651f02 @nberardi added descriptive opperations so that you can query the server for infor...
nberardi authored
48 #region Cassandra Descriptions For Server
49
ce017c8 @nberardi all tests working for 0.7
nberardi authored
50 public static bool KeyspaceExists(Server server, string keyspaceName)
51 {
52 return DescribeKeyspaces(server).Any(keyspace => keyspace.KeyspaceName == keyspaceName);
53 }
54
50d830b @nberardi added thrift from latest build
nberardi authored
55 public static IEnumerable<CassandraKeyspace> DescribeKeyspaces(Server server)
1651f02 @nberardi added descriptive opperations so that you can query the server for infor...
nberardi authored
56 {
ce017c8 @nberardi all tests working for 0.7
nberardi authored
57 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 infor...
nberardi authored
58 {
74b23c0 @nberardi now compiles against 0.7 beta-2 thrift interface
nberardi authored
59 IEnumerable<KsDef> keyspaces = session.GetClient(setKeyspace: false).describe_keyspaces();
1651f02 @nberardi added descriptive opperations so that you can query the server for infor...
nberardi authored
60
61 foreach (var keyspace in keyspaces)
62 yield return new CassandraKeyspace(keyspace);
63 }
64 }
65
50d830b @nberardi added thrift from latest build
nberardi authored
66 public static string DescribeClusterName(Server server)
1651f02 @nberardi added descriptive opperations so that you can query the server for infor...
nberardi authored
67 {
ce017c8 @nberardi all tests working for 0.7
nberardi authored
68 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 infor...
nberardi authored
69 {
ce017c8 @nberardi all tests working for 0.7
nberardi authored
70 string response = session.GetClient(setKeyspace: false).describe_cluster_name();
1651f02 @nberardi added descriptive opperations so that you can query the server for infor...
nberardi authored
71 return response;
72 }
73 }
74
7ba4ce5 @nberardi added support for a couple more features that were added to the client i...
nberardi authored
75 public static Dictionary<string, List<string>> DescribeSchemaVersions(Server server)
76 {
77 using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port)))
78 {
79 var response = session.GetClient(setKeyspace: false).describe_schema_versions();
80 return response;
81 }
82 }
83
50d830b @nberardi added thrift from latest build
nberardi authored
84 public static string DescribeVersion(Server server)
1651f02 @nberardi added descriptive opperations so that you can query the server for infor...
nberardi authored
85 {
ce017c8 @nberardi all tests working for 0.7
nberardi authored
86 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 infor...
nberardi authored
87 {
ce017c8 @nberardi all tests working for 0.7
nberardi authored
88 string response = session.GetClient(setKeyspace: false).describe_version();
1651f02 @nberardi added descriptive opperations so that you can query the server for infor...
nberardi authored
89 return response;
90 }
91 }
92
50d830b @nberardi added thrift from latest build
nberardi authored
93 public static string DescribePartitioner(Server server)
1651f02 @nberardi added descriptive opperations so that you can query the server for infor...
nberardi authored
94 {
ce017c8 @nberardi all tests working for 0.7
nberardi authored
95 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 infor...
nberardi authored
96 {
ce017c8 @nberardi all tests working for 0.7
nberardi authored
97 string response = session.GetClient(setKeyspace: false).describe_partitioner();
1651f02 @nberardi added descriptive opperations so that you can query the server for infor...
nberardi authored
98 return response;
99 }
100 }
101
7ba4ce5 @nberardi added support for a couple more features that were added to the client i...
nberardi authored
102 public static string DescribeSnitch(Server server)
103 {
104 using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port)))
105 {
106 string response = session.GetClient(setKeyspace: false).describe_snitch();
107 return response;
108 }
109 }
110
1651f02 @nberardi added descriptive opperations so that you can query the server for infor...
nberardi authored
111 #endregion
e4efc4c @nberardi added support for session based configurations, as well as dynamic prope...
nberardi authored
112
113 private bool _disposed;
114 private IConnection _connection;
115
116 public CassandraSession()
117 : this(CassandraContext.CurrentConnectionBuilder) { }
118
119 public CassandraSession(ConnectionBuilder connectionBuilder)
120 : this(ConnectionProviderFactory.Get(connectionBuilder), connectionBuilder.ReadConsistency, connectionBuilder.WriteConsistency) { }
121
122 public CassandraSession(ConsistencyLevel read, ConsistencyLevel write)
123 : this(ConnectionProviderFactory.Get(CassandraContext.CurrentConnectionBuilder), read, write) { }
124
125 public CassandraSession(IConnectionProvider connectionProvider, ConsistencyLevel read, ConsistencyLevel write)
126 {
127 if (Current != null)
128 throw new CassandraException("Cannot create a new session while there is one already active.");
129
130 ConnectionProvider = connectionProvider;
131 ReadConsistency = read;
132 WriteConsistency = write;
365b683 @nberardi still working on getting the app to compile after the breaking changes i...
nberardi authored
133 Keyspace = new CassandraKeyspace(connectionProvider.Builder.Keyspace);
e4efc4c @nberardi added support for session based configurations, as well as dynamic prope...
nberardi authored
134
e93edc7 @nberardi improved the login API, and allowed it to be set in the connection strin...
nberardi authored
135 IsAuthenticated = false;
e4efc4c @nberardi added support for session based configurations, as well as dynamic prope...
nberardi authored
136 Current = this;
137 }
138
365b683 @nberardi still working on getting the app to compile after the breaking changes i...
nberardi authored
139 public IConnectionProvider ConnectionProvider { get; private set; }
140
e4efc4c @nberardi added support for session based configurations, as well as dynamic prope...
nberardi authored
141 public ConsistencyLevel ReadConsistency { get; private set; }
142
143 public ConsistencyLevel WriteConsistency { get; private set; }
144
365b683 @nberardi still working on getting the app to compile after the breaking changes i...
nberardi authored
145 public CassandraKeyspace Keyspace { get; private set; }
e4efc4c @nberardi added support for session based configurations, as well as dynamic prope...
nberardi authored
146
e93edc7 @nberardi improved the login API, and allowed it to be set in the connection strin...
nberardi authored
147 public bool IsAuthenticated { get; private set; }
148
ce017c8 @nberardi all tests working for 0.7
nberardi authored
149 internal Cassandra.Client GetClient(bool setKeyspace = true)
e4efc4c @nberardi added support for session based configurations, as well as dynamic prope...
nberardi authored
150 {
50d830b @nberardi added thrift from latest build
nberardi authored
151 if (_connection == null)
e4efc4c @nberardi added support for session based configurations, as well as dynamic prope...
nberardi authored
152 _connection = ConnectionProvider.Open();
50d830b @nberardi added thrift from latest build
nberardi authored
153
154 if (!_connection.IsOpen)
155 _connection.Open();
e4efc4c @nberardi added support for session based configurations, as well as dynamic prope...
nberardi authored
156
ce017c8 @nberardi all tests working for 0.7
nberardi authored
157 if (setKeyspace)
23a0cbf @nberardi fixed build error
nberardi authored
158 _connection.SetKeyspace(Keyspace.KeyspaceName);
ce017c8 @nberardi all tests working for 0.7
nberardi authored
159
e4efc4c @nberardi added support for session based configurations, as well as dynamic prope...
nberardi authored
160 return _connection.Client;
161 }
162
e93edc7 @nberardi improved the login API, and allowed it to be set in the connection strin...
nberardi authored
163 public void Login(string username, string password)
7ba4ce5 @nberardi added support for a couple more features that were added to the client i...
nberardi authored
164 {
e93edc7 @nberardi improved the login API, and allowed it to be set in the connection strin...
nberardi authored
165 var auth = new AuthenticationRequest {
166 Credentials = new Dictionary<string, string> { { "username", username }, { "password", password } }
167 };
168
169 try
170 {
171 GetClient().login(auth);
172 IsAuthenticated = true;
173 }
174 catch (Exception exc)
175 {
176 IsAuthenticated = false;
177 throw new CassandraException("Login failed.", exc);
178 }
7ba4ce5 @nberardi added support for a couple more features that were added to the client i...
nberardi authored
179 }
180
e4efc4c @nberardi added support for session based configurations, as well as dynamic prope...
nberardi authored
181 #region IDisposable Members
182
183 /// <summary>
184 ///
185 /// </summary>
186 public void Dispose()
187 {
188 Dispose(true);
046825e @nberardi updated connections to have a more standarding naming connvetion with ot...
nberardi authored
189 GC.SuppressFinalize(this);
e4efc4c @nberardi added support for session based configurations, as well as dynamic prope...
nberardi authored
190 }
191
192 /// <summary>
193 /// The dispose.
194 /// </summary>
195 /// <param name="disposing">
196 /// The disposing.
197 /// </param>
198 protected virtual void Dispose(bool disposing)
199 {
365b683 @nberardi still working on getting the app to compile after the breaking changes i...
nberardi authored
200 if (!_disposed && disposing)
e4efc4c @nberardi added support for session based configurations, as well as dynamic prope...
nberardi authored
201 {
365b683 @nberardi still working on getting the app to compile after the breaking changes i...
nberardi authored
202 if (_connection != null)
203 ConnectionProvider.Close(_connection);
e4efc4c @nberardi added support for session based configurations, as well as dynamic prope...
nberardi authored
204
205 if (Current == this)
206 Current = null;
207 }
208
365b683 @nberardi still working on getting the app to compile after the breaking changes i...
nberardi authored
209 _disposed = true;
e4efc4c @nberardi added support for session based configurations, as well as dynamic prope...
nberardi authored
210 }
211
212 /// <summary>
365b683 @nberardi still working on getting the app to compile after the breaking changes i...
nberardi authored
213 /// Finalizes an instance of the <see cref="CassandraSession"/> class.
e4efc4c @nberardi added support for session based configurations, as well as dynamic prope...
nberardi authored
214 /// </summary>
215 ~CassandraSession()
216 {
365b683 @nberardi still working on getting the app to compile after the breaking changes i...
nberardi authored
217 Dispose(false);
e4efc4c @nberardi added support for session based configurations, as well as dynamic prope...
nberardi authored
218 }
219
220 #endregion
221 }
222 }
Something went wrong with that request. Please try again.