Browse Source

make SerialConsistency a distinct type

Chris Bannister 10 years ago
parent
commit
87288fa4a1
4 changed files with 59 additions and 44 deletions
  1. 19 19
      cluster.go
  2. 34 20
      frame.go
  3. 4 4
      session.go
  4. 2 1
      session_test.go

+ 19 - 19
cluster.go

@@ -55,25 +55,25 @@ type DiscoveryConfig struct {
 // behavior to fit the most common use cases. Applications that requre a
 // behavior to fit the most common use cases. Applications that requre a
 // different setup must implement their own cluster.
 // different setup must implement their own cluster.
 type ClusterConfig struct {
 type ClusterConfig struct {
-	Hosts             []string      // addresses for the initial connections
-	CQLVersion        string        // CQL version (default: 3.0.0)
-	ProtoVersion      int           // version of the native protocol (default: 2)
-	Timeout           time.Duration // connection timeout (default: 600ms)
-	Port              int           // port (default: 9042)
-	Keyspace          string        // initial keyspace (optional)
-	NumConns          int           // number of connections per host (default: 2)
-	NumStreams        int           // number of streams per connection (default: max per protocol, either 128 or 32768)
-	Consistency       Consistency   // default consistency level (default: Quorum)
-	Compressor        Compressor    // compression algorithm (default: nil)
-	Authenticator     Authenticator // authenticator (default: nil)
-	RetryPolicy       RetryPolicy   // Default retry policy to use for queries (default: 0)
-	SocketKeepalive   time.Duration // The keepalive period to use, enabled if > 0 (default: 0)
-	ConnPoolType      NewPoolFunc   // The function used to create the connection pool for the session (default: NewSimplePool)
-	DiscoverHosts     bool          // If set, gocql will attempt to automatically discover other members of the Cassandra cluster (default: false)
-	MaxPreparedStmts  int           // Sets the maximum cache size for prepared statements globally for gocql (default: 1000)
-	MaxRoutingKeyInfo int           // Sets the maximum cache size for query info about statements for each session (default: 1000)
-	PageSize          int           // Default page size to use for created sessions (default: 0)
-	SerialConsistency Consistency   // Sets the consistency for the serial part of queries, values can be either SERIAL or LOCAL_SERIAL (default: unset)
+	Hosts             []string          // addresses for the initial connections
+	CQLVersion        string            // CQL version (default: 3.0.0)
+	ProtoVersion      int               // version of the native protocol (default: 2)
+	Timeout           time.Duration     // connection timeout (default: 600ms)
+	Port              int               // port (default: 9042)
+	Keyspace          string            // initial keyspace (optional)
+	NumConns          int               // number of connections per host (default: 2)
+	NumStreams        int               // number of streams per connection (default: max per protocol, either 128 or 32768)
+	Consistency       Consistency       // default consistency level (default: Quorum)
+	Compressor        Compressor        // compression algorithm (default: nil)
+	Authenticator     Authenticator     // authenticator (default: nil)
+	RetryPolicy       RetryPolicy       // Default retry policy to use for queries (default: 0)
+	SocketKeepalive   time.Duration     // The keepalive period to use, enabled if > 0 (default: 0)
+	ConnPoolType      NewPoolFunc       // The function used to create the connection pool for the session (default: NewSimplePool)
+	DiscoverHosts     bool              // If set, gocql will attempt to automatically discover other members of the Cassandra cluster (default: false)
+	MaxPreparedStmts  int               // Sets the maximum cache size for prepared statements globally for gocql (default: 1000)
+	MaxRoutingKeyInfo int               // Sets the maximum cache size for query info about statements for each session (default: 1000)
+	PageSize          int               // Default page size to use for created sessions (default: 0)
+	SerialConsistency SerialConsistency // Sets the consistency for the serial part of queries, values can be either SERIAL or LOCAL_SERIAL (default: unset)
 	Discovery         DiscoveryConfig
 	Discovery         DiscoveryConfig
 	SslOpts           *SslOptions
 	SslOpts           *SslOptions
 }
 }

+ 34 - 20
frame.go

@@ -135,16 +135,14 @@ type Consistency uint16
 
 
 const (
 const (
 	Any         Consistency = 0x00
 	Any         Consistency = 0x00
-	One                     = 0x01
-	Two                     = 0x02
-	Three                   = 0x03
-	Quorum                  = 0x04
-	All                     = 0x05
-	LocalQuorum             = 0x06
-	EachQuorum              = 0x07
-	Serial                  = 0x08
-	LocalSerial             = 0x09
-	LocalOne                = 0x0A
+	One         Consistency = 0x01
+	Two         Consistency = 0x02
+	Three       Consistency = 0x03
+	Quorum      Consistency = 0x04
+	All         Consistency = 0x05
+	LocalQuorum Consistency = 0x06
+	EachQuorum  Consistency = 0x07
+	LocalOne    Consistency = 0x0A
 )
 )
 
 
 func (c Consistency) String() string {
 func (c Consistency) String() string {
@@ -165,14 +163,28 @@ func (c Consistency) String() string {
 		return "LOCAL_QUORUM"
 		return "LOCAL_QUORUM"
 	case EachQuorum:
 	case EachQuorum:
 		return "EACH_QUORUM"
 		return "EACH_QUORUM"
+	case LocalOne:
+		return "LOCAL_ONE"
+	default:
+		return fmt.Sprintf("UNKNOWN_CONS_0x%x", uint16(c))
+	}
+}
+
+type SerialConsistency uint16
+
+const (
+	Serial      SerialConsistency = 0x08
+	LocalSerial SerialConsistency = 0x09
+)
+
+func (s SerialConsistency) String() string {
+	switch s {
 	case Serial:
 	case Serial:
 		return "SERIAL"
 		return "SERIAL"
 	case LocalSerial:
 	case LocalSerial:
 		return "LOCAL_SERIAL"
 		return "LOCAL_SERIAL"
-	case LocalOne:
-		return "LOCAL_ONE"
 	default:
 	default:
-		return fmt.Sprintf("UNKNOWN_CONS_0x%x", uint16(c))
+		return fmt.Sprintf("UNKNOWN_SERIAL_CONS_0x%x", uint16(s))
 	}
 	}
 }
 }
 
 
@@ -878,7 +890,7 @@ type queryParams struct {
 	values            []queryValues
 	values            []queryValues
 	pageSize          int
 	pageSize          int
 	pagingState       []byte
 	pagingState       []byte
-	serialConsistency Consistency
+	serialConsistency SerialConsistency
 	// v3+
 	// v3+
 	timestamp *time.Time
 	timestamp *time.Time
 }
 }
@@ -946,7 +958,7 @@ func (f *framer) writeQueryParams(opts *queryParams) {
 	}
 	}
 
 
 	if opts.serialConsistency > 0 {
 	if opts.serialConsistency > 0 {
-		f.writeConsistency(opts.serialConsistency)
+		f.writeConsistency(Consistency(opts.serialConsistency))
 	}
 	}
 
 
 	if f.proto > protoVersion2 && opts.timestamp != nil {
 	if f.proto > protoVersion2 && opts.timestamp != nil {
@@ -1022,10 +1034,12 @@ type batchStatment struct {
 }
 }
 
 
 type writeBatchFrame struct {
 type writeBatchFrame struct {
-	typ               BatchType
-	statements        []batchStatment
-	consistency       Consistency
-	serialConsistency Consistency
+	typ         BatchType
+	statements  []batchStatment
+	consistency Consistency
+
+	// v3+
+	serialConsistency SerialConsistency
 	defaultTimestamp  bool
 	defaultTimestamp  bool
 }
 }
 
 
@@ -1078,7 +1092,7 @@ func (f *framer) writeBatchFrame(streamID int, w *writeBatchFrame) error {
 		f.writeByte(flags)
 		f.writeByte(flags)
 
 
 		if w.serialConsistency > 0 {
 		if w.serialConsistency > 0 {
-			f.writeConsistency(w.serialConsistency)
+			f.writeConsistency(Consistency(w.serialConsistency))
 		}
 		}
 		if w.defaultTimestamp {
 		if w.defaultTimestamp {
 			now := time.Now().UnixNano() / 1000
 			now := time.Now().UnixNano() / 1000

+ 4 - 4
session.go

@@ -372,7 +372,7 @@ type Query struct {
 	binding      func(q *QueryInfo) ([]interface{}, error)
 	binding      func(q *QueryInfo) ([]interface{}, error)
 	attempts     int
 	attempts     int
 	totalLatency int64
 	totalLatency int64
-	serialCons   Consistency
+	serialCons   SerialConsistency
 }
 }
 
 
 //Attempts returns the number of times the query was executed.
 //Attempts returns the number of times the query was executed.
@@ -523,7 +523,7 @@ func (q *Query) Bind(v ...interface{}) *Query {
 // either SERIAL or LOCAL_SERIAL and if not present, it defaults to
 // either SERIAL or LOCAL_SERIAL and if not present, it defaults to
 // SERIAL. This option will be ignored for anything else that a
 // SERIAL. This option will be ignored for anything else that a
 // conditional update/insert.
 // conditional update/insert.
-func (q *Query) SerialConsistency(cons Consistency) *Query {
+func (q *Query) SerialConsistency(cons SerialConsistency) *Query {
 	q.serialCons = cons
 	q.serialCons = cons
 	return q
 	return q
 }
 }
@@ -698,7 +698,7 @@ type Batch struct {
 	rt           RetryPolicy
 	rt           RetryPolicy
 	attempts     int
 	attempts     int
 	totalLatency int64
 	totalLatency int64
-	serialCons   Consistency
+	serialCons   SerialConsistency
 }
 }
 
 
 // NewBatch creates a new batch operation without defaults from the cluster
 // NewBatch creates a new batch operation without defaults from the cluster
@@ -760,7 +760,7 @@ func (b *Batch) Size() int {
 // conditional update/insert.
 // conditional update/insert.
 //
 //
 // Only available for protocol 3 and above
 // Only available for protocol 3 and above
-func (b *Batch) SerialConsistency(cons Consistency) *Batch {
+func (b *Batch) SerialConsistency(cons SerialConsistency) *Batch {
 	b.serialCons = cons
 	b.serialCons = cons
 	return b
 	return b
 }
 }

+ 2 - 1
session_test.go

@@ -3,6 +3,7 @@
 package gocql
 package gocql
 
 
 import (
 import (
+	"fmt"
 	"testing"
 	"testing"
 )
 )
 
 
@@ -221,7 +222,7 @@ func TestBatchBasicAPI(t *testing.T) {
 }
 }
 
 
 func TestConsistencyNames(t *testing.T) {
 func TestConsistencyNames(t *testing.T) {
-	names := map[Consistency]string{
+	names := map[fmt.Stringer]string{
 		Any:         "ANY",
 		Any:         "ANY",
 		One:         "ONE",
 		One:         "ONE",
 		Two:         "TWO",
 		Two:         "TWO",