1616import NIO
1717
1818// TODO: add more config option per C++ cluster impl
19- public extension CassandraClient {
19+ extension CassandraClient {
2020 /// Configuration for the ``CassandraClient``.
21- struct Configuration : CustomStringConvertible {
21+ public struct Configuration : CustomStringConvertible {
2222 public typealias ContactPoints = [ String ]
2323
2424 /// Provides the initial `ContactPoints` of the Cassandra cluster.
2525 /// This can be a subset since each Cassandra instance is capable of discovering its peers.
2626 public var contactPointsProvider : ( @escaping ( Result < ContactPoints , Swift . Error > ) -> Void ) -> Void
27+
2728 public var port : Int32
2829 public var protocolVersion : ProtocolVersion
2930 public var username : String ?
@@ -37,7 +38,7 @@ public extension CassandraClient {
3738 public var coreConnectionsPerHost : UInt32 ?
3839 public var tcpNodelay : Bool ?
3940 public var tcpKeepalive : Bool ?
40- public var tcpKeepaliveDelaySeconds : UInt32
41+ public var tcpKeepaliveDelaySeconds : UInt32 = 0
4142 public var connectionHeartbeatInterval : UInt32 ?
4243 public var connectionIdleTimeout : UInt32 ?
4344 public var schema : Bool ?
@@ -47,6 +48,9 @@ public extension CassandraClient {
4748 public var prepareStrategy : PrepareStrategy ?
4849 public var compact : Bool ?
4950
51+ /// Sets the cluster's consistency level. Default is `.localOne`.
52+ public var consistency : CassandraClient . Consistency ?
53+
5054 public enum SpeculativeExecutionPolicy {
5155 case constant( delayInMillseconds: Int64 , maxExecutions: Int32 )
5256 case disabled
@@ -68,51 +72,11 @@ public extension CassandraClient {
6872 public init (
6973 contactPointsProvider: @escaping ( @escaping ( Result < ContactPoints , Swift . Error > ) -> Void ) -> Void ,
7074 port: Int32 ,
71- protocolVersion: ProtocolVersion ,
72- username: String ? = nil ,
73- password: String ? = nil ,
74- ssl: SSL ? = nil ,
75- keyspace: String ? = nil ,
76- numIOThreads: UInt32 ? = nil ,
77- connectTimeoutMillis: UInt32 ? = nil ,
78- requestTimeoutMillis: UInt32 ? = nil ,
79- resolveTimeoutMillis: UInt32 ? = nil ,
80- coreConnectionsPerHost: UInt32 ? = nil ,
81- tcpNodelay: Bool ? = nil ,
82- tcpKeepalive: Bool ? = nil ,
83- tcpKeepaliveDelaySeconds: UInt32 = 0 ,
84- connectionHeartbeatInterval: UInt32 ? = nil ,
85- connectionIdleTimeout: UInt32 ? = nil ,
86- schema: Bool ? = nil ,
87- hostnameResolution: Bool ? = nil ,
88- randomizedContactPoints: Bool ? = nil ,
89- speculativeExecutionPolicy: SpeculativeExecutionPolicy ? = nil ,
90- prepareStrategy: PrepareStrategy ? = nil ,
91- compact: Bool ? = nil
75+ protocolVersion: ProtocolVersion
9276 ) {
9377 self . contactPointsProvider = contactPointsProvider
9478 self . port = port
9579 self . protocolVersion = protocolVersion
96- self . username = username
97- self . password = password
98- self . ssl = ssl
99- self . keyspace = keyspace
100- self . numIOThreads = numIOThreads
101- self . connectTimeoutMillis = connectTimeoutMillis
102- self . requestTimeoutMillis = requestTimeoutMillis
103- self . resolveTimeoutMillis = resolveTimeoutMillis
104- self . coreConnectionsPerHost = coreConnectionsPerHost
105- self . tcpNodelay = tcpNodelay
106- self . tcpKeepalive = tcpKeepalive
107- self . tcpKeepaliveDelaySeconds = tcpKeepaliveDelaySeconds
108- self . connectionHeartbeatInterval = connectionHeartbeatInterval
109- self . connectionIdleTimeout = connectionIdleTimeout
110- self . schema = schema
111- self . hostnameResolution = hostnameResolution
112- self . randomizedContactPoints = randomizedContactPoints
113- self . speculativeExecutionPolicy = speculativeExecutionPolicy
114- self . prepareStrategy = prepareStrategy
115- self . compact = compact
11680 }
11781
11882 internal func makeCluster( on eventLoop: EventLoop ) -> EventLoopFuture < Cluster > {
@@ -167,40 +131,40 @@ public extension CassandraClient {
167131 if let ssl = self . ssl {
168132 try cluster. setSSL ( try ssl. makeSSLContext ( ) )
169133 }
170- if let value = numIOThreads {
134+ if let value = self . numIOThreads {
171135 try cluster. setNumThreadsIO ( value)
172136 }
173- if let value = connectTimeoutMillis {
137+ if let value = self . connectTimeoutMillis {
174138 try cluster. setConnectTimeout ( value)
175139 }
176- if let value = requestTimeoutMillis {
140+ if let value = self . requestTimeoutMillis {
177141 try cluster. setRequestTimeout ( value)
178142 }
179- if let value = resolveTimeoutMillis {
143+ if let value = self . resolveTimeoutMillis {
180144 try cluster. setResolveTimeout ( value)
181145 }
182- if let value = coreConnectionsPerHost {
146+ if let value = self . coreConnectionsPerHost {
183147 try cluster. setCoreConnectionsPerHost ( value)
184148 }
185- if let value = tcpNodelay {
149+ if let value = self . tcpNodelay {
186150 try cluster. setTcpNodelay ( value)
187151 }
188- if let value = tcpKeepalive {
152+ if let value = self . tcpKeepalive {
189153 try cluster. setTcpKeepalive ( value, delayInSeconds: self . tcpKeepaliveDelaySeconds)
190154 }
191- if let value = connectionHeartbeatInterval {
155+ if let value = self . connectionHeartbeatInterval {
192156 try cluster. setConnectionHeartbeatInterval ( value)
193157 }
194- if let value = connectionIdleTimeout {
158+ if let value = self . connectionIdleTimeout {
195159 try cluster. setConnectionIdleTimeout ( value)
196160 }
197- if let value = schema {
161+ if let value = self . schema {
198162 try cluster. setUseSchema ( value)
199163 }
200- if let value = hostnameResolution {
164+ if let value = self . hostnameResolution {
201165 try cluster. setUseHostnameResolution ( value)
202166 }
203- if let value = randomizedContactPoints {
167+ if let value = self . randomizedContactPoints {
204168 try cluster. setUseRandomizedContactPoints ( value)
205169 }
206170 switch self . speculativeExecutionPolicy {
@@ -219,9 +183,12 @@ public extension CassandraClient {
219183 case . none:
220184 break
221185 }
222- if let value = compact {
186+ if let value = self . compact {
223187 try cluster. setNoCompact ( !value)
224188 }
189+ if let value = self . consistency {
190+ try cluster. setConsistency ( value. cassConsistency)
191+ }
225192
226193 return cluster
227194 }
@@ -338,6 +305,10 @@ internal final class Cluster {
338305 try self . checkResult { cass_cluster_set_no_compact ( self . rawPointer, enabled ? cass_true : cass_false) }
339306 }
340307
308+ func setConsistency( _ consistency: CassConsistency ) throws {
309+ try self . checkResult { cass_cluster_set_consistency ( self . rawPointer, consistency) }
310+ }
311+
341312 func setSSL( _ ssl: SSLContext ) throws {
342313 cass_cluster_set_ssl ( self . rawPointer, ssl. rawPointer)
343314 }
@@ -352,8 +323,8 @@ internal final class Cluster {
352323
353324// MARK: - SSL
354325
355- public extension CassandraClient . Configuration {
356- struct SSL {
326+ extension CassandraClient . Configuration {
327+ public struct SSL {
357328 public var trustedCertificates : [ String ] ?
358329 public var verifyFlag : VerifyFlag ?
359330 public var cert : String ?
@@ -373,9 +344,7 @@ public extension CassandraClient.Configuration {
373344 case peerIdentityDNS
374345 }
375346
376- public init ( trustedCertificates: [ String ] ? ) {
377- self . trustedCertificates = trustedCertificates
378- }
347+ public init ( ) { }
379348
380349 func makeSSLContext( ) throws -> SSLContext {
381350 let sslContext = SSLContext ( )
0 commit comments