1 module cassandra.client; 2 3 public import cassandra.keyspace; 4 5 import cassandra.cql.connection; 6 import cassandra.internal.utils; 7 8 import std..string : format; 9 10 11 class CassandraClient { 12 private { 13 string m_host; 14 ushort m_port; 15 version (Have_vibe_d) { 16 import vibe.core.connectionpool : ConnectionPool; 17 ConnectionPool!Connection m_connections; 18 } else { 19 Connection m_connection; 20 } 21 } 22 23 this(string host, ushort port = Connection.defaultPort) 24 { 25 m_host = host; 26 m_port = port; 27 28 version (Have_vibe_d) { 29 m_connections = new ConnectionPool!Connection(&createConnection); 30 } else m_connection = createConnection(); 31 } 32 33 CassandraKeyspace getKeyspace(string name) { return CassandraKeyspace(this, name); } 34 35 CassandraKeyspace createKeyspace(string name/*, ...*/) 36 { 37 enforceValidIdentifier(name); 38 auto conn = lockConnection(); 39 conn.query(conn, format(`CREATE KEYSPACE %s WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}`, name), Consistency.any); 40 return getKeyspace(name); 41 } 42 43 version (Have_vibe_d) { 44 package auto lockConnection() { return m_connections.lockConnection(); } 45 } else { 46 package auto lockConnection() { return m_connection; } 47 } 48 49 private Connection createConnection() 50 { 51 auto ret = new Connection(m_host, m_port); 52 ret.connect(); 53 return ret; 54 } 55 }