Skip to content

Commit b773465

Browse files
WIP
1 parent 280f0aa commit b773465

24 files changed

+3682
-2569
lines changed

cassandra_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"context"
3333
"errors"
3434
"fmt"
35+
"github.com/gocql/gocql/protocol"
3536
"github.com/stretchr/testify/require"
3637
"io"
3738
"math"
@@ -519,23 +520,23 @@ func TestDurationType(t *testing.T) {
519520
t.Fatal("create:", err)
520521
}
521522

522-
durations := []Duration{
523-
Duration{
523+
durations := []protocol.Duration{
524+
protocol.Duration{
524525
Months: 250,
525526
Days: 500,
526527
Nanoseconds: 300010001,
527528
},
528-
Duration{
529+
protocol.Duration{
529530
Months: -250,
530531
Days: -500,
531532
Nanoseconds: -300010001,
532533
},
533-
Duration{
534+
protocol.Duration{
534535
Months: 0,
535536
Days: 128,
536537
Nanoseconds: 127,
537538
},
538-
Duration{
539+
protocol.Duration{
539540
Months: 0x7FFFFFFF,
540541
Days: 0x7FFFFFFF,
541542
Nanoseconds: 0x7FFFFFFFFFFFFFFF,
@@ -547,7 +548,7 @@ func TestDurationType(t *testing.T) {
547548
}
548549

549550
var id int
550-
var duration Duration
551+
var duration protocol.Duration
551552
if err := session.Query(`SELECT k, v FROM gocql_test.duration_table`).Scan(&id, &duration); err != nil {
552553
t.Fatal(err)
553554
}

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: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"crypto/tls"
3131
"errors"
3232
"fmt"
33+
"github.com/gocql/gocql/internal"
3334
"io"
3435
"io/ioutil"
3536
"net"
@@ -1276,7 +1277,7 @@ func (c *Conn) prepareStatement(ctx context.Context, stmt string, tracer Tracer)
12761277
flight.preparedStatment = &preparedStatment{
12771278
// defensively copy as we will recycle the underlying buffer after we
12781279
// return.
1279-
id: copyBytes(x.preparedID),
1280+
id: internal.CopyBytes(x.preparedID),
12801281
// the type info's should _not_ have a reference to the framers read buffer,
12811282
// therefore we can just copy them directly.
12821283
request: x.reqMeta,
@@ -1431,7 +1432,7 @@ func (c *Conn) executeQuery(ctx context.Context, qry *Query) *Iter {
14311432
if params.skipMeta {
14321433
if info != nil {
14331434
iter.meta = info.response
1434-
iter.meta.pagingState = copyBytes(x.meta.pagingState)
1435+
iter.meta.pagingState = internal.CopyBytes(x.meta.pagingState)
14351436
} else {
14361437
return &Iter{framer: framer, err: errors.New("gocql: did not receive metadata but prepared info is nil")}
14371438
}
@@ -1442,7 +1443,7 @@ func (c *Conn) executeQuery(ctx context.Context, qry *Query) *Iter {
14421443
if x.meta.morePages() && !qry.disableAutoPage {
14431444
newQry := new(Query)
14441445
*newQry = *qry
1445-
newQry.pageState = copyBytes(x.meta.pagingState)
1446+
newQry.pageState = internal.CopyBytes(x.meta.pagingState)
14461447
newQry.metrics = &queryMetrics{m: make(map[string]*hostMetrics)}
14471448

14481449
iter.next = &nextIter{
@@ -1659,7 +1660,7 @@ func (c *Conn) querySystemPeers(ctx context.Context, version cassVersion) *Iter
16591660

16601661
err := iter.checkErrAndNotFound()
16611662
if err != nil {
1662-
if errFrame, ok := err.(errorFrame); ok && errFrame.code == ErrCodeInvalid { // system.peers_v2 not found, try system.peers
1663+
if errFrame, ok := err.(errorFrame); ok && errFrame.code == gocql_errors.ErrCodeInvalid { // system.peers_v2 not found, try system.peers
16631664
c.mu.Lock()
16641665
c.isSchemaV2 = false
16651666
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)