Skip to content

Commit 921d082

Browse files
WIP
1 parent 280f0aa commit 921d082

File tree

15 files changed

+2689
-2003
lines changed

15 files changed

+2689
-2003
lines changed

cluster.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ package gocql
2727
import (
2828
"context"
2929
"errors"
30+
"github.com/gocql/gocql/consistency"
3031
"net"
3132
"time"
3233
)
@@ -114,7 +115,7 @@ type ClusterConfig struct {
114115

115116
// Default consistency level.
116117
// Default: Quorum
117-
Consistency Consistency
118+
Consistency consistency.Consistency
118119

119120
// Compression algorithm.
120121
// Default: nil
@@ -156,7 +157,7 @@ type ClusterConfig struct {
156157

157158
// Consistency for the serial part of queries, values can be either SERIAL or LOCAL_SERIAL.
158159
// Default: unset
159-
SerialConsistency SerialConsistency
160+
SerialConsistency consistency.SerialConsistency
160161

161162
// SslOpts configures TLS use when HostDialer is not set.
162163
// SslOpts is ignored if HostDialer is set.

compressor.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/golang/snappy"
2929
)
3030

31+
// Deprecated: use compressor.Compressor instead
3132
type Compressor interface {
3233
Name() string
3334
Encode(data []byte) ([]byte, error)

compressor/compressor.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package compressor
2+
3+
import "github.com/golang/snappy"
4+
5+
type Compressor interface {
6+
Name() string
7+
Encode(data []byte) ([]byte, error)
8+
Decode(data []byte) ([]byte, error)
9+
}
10+
11+
// SnappyCompressor implements the Compressor interface and can be used to
12+
// compress incoming and outgoing frames. The snappy compression algorithm
13+
// aims for very high speeds and reasonable compression.
14+
type SnappyCompressor struct{}
15+
16+
func (s SnappyCompressor) Name() string {
17+
return "snappy"
18+
}
19+
20+
func (s SnappyCompressor) Encode(data []byte) ([]byte, error) {
21+
return snappy.Encode(nil, data), nil
22+
}
23+
24+
func (s SnappyCompressor) Decode(data []byte) ([]byte, error) {
25+
return snappy.Decode(nil, data)
26+
}

conn.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1659,7 +1659,7 @@ func (c *Conn) querySystemPeers(ctx context.Context, version cassVersion) *Iter
16591659

16601660
err := iter.checkErrAndNotFound()
16611661
if err != nil {
1662-
if errFrame, ok := err.(errorFrame); ok && errFrame.code == ErrCodeInvalid { // system.peers_v2 not found, try system.peers
1662+
if errFrame, ok := err.(errorFrame); ok && errFrame.code == gocql_errors.ErrCodeInvalid { // system.peers_v2 not found, try system.peers
16631663
c.mu.Lock()
16641664
c.isSchemaV2 = false
16651665
c.mu.Unlock()

consistency/consistency.go

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package consistency
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
type Consistency uint16
9+
10+
const (
11+
Any Consistency = 0x00
12+
One Consistency = 0x01
13+
Two Consistency = 0x02
14+
Three Consistency = 0x03
15+
Quorum Consistency = 0x04
16+
All Consistency = 0x05
17+
LocalQuorum Consistency = 0x06
18+
EachQuorum Consistency = 0x07
19+
LocalOne Consistency = 0x0A
20+
)
21+
22+
func (c Consistency) String() string {
23+
switch c {
24+
case Any:
25+
return "ANY"
26+
case One:
27+
return "ONE"
28+
case Two:
29+
return "TWO"
30+
case Three:
31+
return "THREE"
32+
case Quorum:
33+
return "QUORUM"
34+
case All:
35+
return "ALL"
36+
case LocalQuorum:
37+
return "LOCAL_QUORUM"
38+
case EachQuorum:
39+
return "EACH_QUORUM"
40+
case LocalOne:
41+
return "LOCAL_ONE"
42+
default:
43+
return fmt.Sprintf("UNKNOWN_CONS_0x%x", uint16(c))
44+
}
45+
}
46+
47+
func (c Consistency) MarshalText() (text []byte, err error) {
48+
return []byte(c.String()), nil
49+
}
50+
51+
func (c *Consistency) UnmarshalText(text []byte) error {
52+
switch string(text) {
53+
case "ANY":
54+
*c = Any
55+
case "ONE":
56+
*c = One
57+
case "TWO":
58+
*c = Two
59+
case "THREE":
60+
*c = Three
61+
case "QUORUM":
62+
*c = Quorum
63+
case "ALL":
64+
*c = All
65+
case "LOCAL_QUORUM":
66+
*c = LocalQuorum
67+
case "EACH_QUORUM":
68+
*c = EachQuorum
69+
case "LOCAL_ONE":
70+
*c = LocalOne
71+
default:
72+
return fmt.Errorf("invalid consistency %q", string(text))
73+
}
74+
75+
return nil
76+
}
77+
78+
func ParseConsistency(s string) Consistency {
79+
var c Consistency
80+
if err := c.UnmarshalText([]byte(strings.ToUpper(s))); err != nil {
81+
panic(err)
82+
}
83+
return c
84+
}
85+
86+
// ParseConsistencyWrapper wraps gocql.ParseConsistency to provide an err
87+
// return instead of a panic
88+
func ParseConsistencyWrapper(s string) (consistency Consistency, err error) {
89+
err = consistency.UnmarshalText([]byte(strings.ToUpper(s)))
90+
return
91+
}
92+
93+
// MustParseConsistency is the same as ParseConsistency except it returns
94+
// an error (never). It is kept here since breaking changes are not good.
95+
// DEPRECATED: use ParseConsistency if you want a panic on parse error.
96+
func MustParseConsistency(s string) (Consistency, error) {
97+
c, err := ParseConsistencyWrapper(s)
98+
if err != nil {
99+
panic(err)
100+
}
101+
return c, nil
102+
}
103+
104+
type SerialConsistency uint16
105+
106+
const (
107+
Serial SerialConsistency = 0x08
108+
LocalSerial SerialConsistency = 0x09
109+
)
110+
111+
func (s SerialConsistency) String() string {
112+
switch s {
113+
case Serial:
114+
return "SERIAL"
115+
case LocalSerial:
116+
return "LOCAL_SERIAL"
117+
default:
118+
return fmt.Sprintf("UNKNOWN_SERIAL_CONS_0x%x", uint16(s))
119+
}
120+
}
121+
122+
func (s SerialConsistency) MarshalText() (text []byte, err error) {
123+
return []byte(s.String()), nil
124+
}
125+
126+
func (s *SerialConsistency) UnmarshalText(text []byte) error {
127+
switch string(text) {
128+
case "SERIAL":
129+
*s = Serial
130+
case "LOCAL_SERIAL":
131+
*s = LocalSerial
132+
default:
133+
return fmt.Errorf("invalid consistency %q", string(text))
134+
}
135+
136+
return nil
137+
}

0 commit comments

Comments
 (0)