Skip to content

Commit e14dad0

Browse files

File tree

13 files changed

+1013
-981
lines changed

13 files changed

+1013
-981
lines changed

conn.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,7 +1276,7 @@ func (c *Conn) prepareStatement(ctx context.Context, stmt string, tracer Tracer)
12761276
flight.preparedStatment = &preparedStatment{
12771277
// defensively copy as we will recycle the underlying buffer after we
12781278
// return.
1279-
id: copyBytes(x.preparedID),
1279+
id: internal.CopyBytes(x.preparedID),
12801280
// the type info's should _not_ have a reference to the framers read buffer,
12811281
// therefore we can just copy them directly.
12821282
request: x.reqMeta,
@@ -1308,7 +1308,7 @@ func marshalQueryValue(typ TypeInfo, value interface{}, dst *queryValues) error
13081308
value = named.value
13091309
}
13101310

1311-
if _, ok := value.(unsetColumn); !ok {
1311+
if _, ok := value.(internal.UnsetColumn); !ok {
13121312
val, err := Marshal(typ, value)
13131313
if err != nil {
13141314
return err
@@ -1431,7 +1431,7 @@ func (c *Conn) executeQuery(ctx context.Context, qry *Query) *Iter {
14311431
if params.skipMeta {
14321432
if info != nil {
14331433
iter.meta = info.response
1434-
iter.meta.pagingState = copyBytes(x.meta.pagingState)
1434+
iter.meta.pagingState = internal.CopyBytes(x.meta.pagingState)
14351435
} else {
14361436
return &Iter{framer: framer, err: errors.New("gocql: did not receive metadata but prepared info is nil")}
14371437
}
@@ -1442,7 +1442,7 @@ func (c *Conn) executeQuery(ctx context.Context, qry *Query) *Iter {
14421442
if x.meta.morePages() && !qry.disableAutoPage {
14431443
newQry := new(Query)
14441444
*newQry = *qry
1445-
newQry.pageState = copyBytes(x.meta.pagingState)
1445+
newQry.pageState = internal.CopyBytes(x.meta.pagingState)
14461446
newQry.metrics = &queryMetrics{m: make(map[string]*hostMetrics)}
14471447

14481448
iter.next = &nextIter{

control.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func init() {
5050
panic(fmt.Sprintf("unable to seed random number generator: %v", err))
5151
}
5252

53-
randr = rand.New(rand.NewSource(int64(readInt(b))))
53+
randr = rand.New(rand.NewSource(int64(internal.ReadInt(b))))
5454
}
5555

5656
const (

frame.go

Lines changed: 11 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"context"
2929
"errors"
3030
"fmt"
31+
"github.com/gocql/gocql/internal"
3132
"io"
3233
"io/ioutil"
3334
"net"
@@ -36,16 +37,14 @@ import (
3637
"time"
3738
)
3839

39-
type unsetColumn struct{}
40-
4140
// UnsetValue represents a value used in a query binding that will be ignored by Cassandra.
4241
//
4342
// By setting a field to the unset value Cassandra will ignore the write completely.
4443
// The main advantage is the ability to keep the same prepared statement even when you don't
4544
// want to update some fields, where before you needed to make another prepared statement.
4645
//
4746
// UnsetValue is only available when using the version 4 of the protocol.
48-
var UnsetValue = unsetColumn{}
47+
var UnsetValue = internal.UnsetColumn{}
4948

5049
type namedValue struct {
5150
name string
@@ -331,10 +330,6 @@ var (
331330

332331
const maxFrameHeaderSize = 9
333332

334-
func readInt(p []byte) int32 {
335-
return int32(p[0])<<24 | int32(p[1])<<16 | int32(p[2])<<8 | int32(p[3])
336-
}
337-
338333
type frameHeader struct {
339334
version protoVersion
340335
flags byte
@@ -474,15 +469,15 @@ func readHeader(r io.Reader, p []byte) (head frameHeader, err error) {
474469

475470
head.stream = int(int16(p[2])<<8 | int16(p[3]))
476471
head.op = frameOp(p[4])
477-
head.length = int(readInt(p[5:]))
472+
head.length = int(internal.ReadInt(p[5:]))
478473
} else {
479474
if len(p) != 8 {
480475
return frameHeader{}, fmt.Errorf("not enough bytes to read header require 8 got: %d", len(p))
481476
}
482477

483478
head.stream = int(int8(p[2]))
484479
head.op = frameOp(p[3])
485-
head.length = int(readInt(p[4:]))
480+
head.length = int(internal.ReadInt(p[4:]))
486481
}
487482

488483
return head, nil
@@ -647,7 +642,7 @@ func (f *framer) parseErrorFrame() frame {
647642
stmtId := f.readShortBytes()
648643
return &RequestErrUnprepared{
649644
errorFrame: errD,
650-
StatementId: copyBytes(stmtId), // defensively copy
645+
StatementId: internal.CopyBytes(stmtId), // defensively copy
651646
}
652647
case ErrCodeReadFailure:
653648
res := &RequestErrReadFailure{
@@ -969,7 +964,7 @@ func (f *framer) parsePreparedMetadata() preparedMetadata {
969964
}
970965

971966
if meta.flags&flagHasMorePages == flagHasMorePages {
972-
meta.pagingState = copyBytes(f.readBytes())
967+
meta.pagingState = internal.CopyBytes(f.readBytes())
973968
}
974969

975970
if meta.flags&flagNoMetaData == flagNoMetaData {
@@ -1057,7 +1052,7 @@ func (f *framer) parseResultMetadata() resultMetadata {
10571052
meta.actualColCount = meta.colCount
10581053

10591054
if meta.flags&flagHasMorePages == flagHasMorePages {
1060-
meta.pagingState = copyBytes(f.readBytes())
1055+
meta.pagingState = internal.CopyBytes(f.readBytes())
10611056
}
10621057

10631058
if meta.flags&flagNoMetaData == flagNoMetaData {
@@ -1940,49 +1935,6 @@ func (f *framer) writeByte(b byte) {
19401935
f.buf = append(f.buf, b)
19411936
}
19421937

1943-
func appendBytes(p []byte, d []byte) []byte {
1944-
if d == nil {
1945-
return appendInt(p, -1)
1946-
}
1947-
p = appendInt(p, int32(len(d)))
1948-
p = append(p, d...)
1949-
return p
1950-
}
1951-
1952-
func appendShort(p []byte, n uint16) []byte {
1953-
return append(p,
1954-
byte(n>>8),
1955-
byte(n),
1956-
)
1957-
}
1958-
1959-
func appendInt(p []byte, n int32) []byte {
1960-
return append(p, byte(n>>24),
1961-
byte(n>>16),
1962-
byte(n>>8),
1963-
byte(n))
1964-
}
1965-
1966-
func appendUint(p []byte, n uint32) []byte {
1967-
return append(p, byte(n>>24),
1968-
byte(n>>16),
1969-
byte(n>>8),
1970-
byte(n))
1971-
}
1972-
1973-
func appendLong(p []byte, n int64) []byte {
1974-
return append(p,
1975-
byte(n>>56),
1976-
byte(n>>48),
1977-
byte(n>>40),
1978-
byte(n>>32),
1979-
byte(n>>24),
1980-
byte(n>>16),
1981-
byte(n>>8),
1982-
byte(n),
1983-
)
1984-
}
1985-
19861938
func (f *framer) writeCustomPayload(customPayload *map[string][]byte) {
19871939
if len(*customPayload) > 0 {
19881940
if f.proto < protoVersion4 {
@@ -1994,19 +1946,19 @@ func (f *framer) writeCustomPayload(customPayload *map[string][]byte) {
19941946

19951947
// these are protocol level binary types
19961948
func (f *framer) writeInt(n int32) {
1997-
f.buf = appendInt(f.buf, n)
1949+
f.buf = internal.AppendInt(f.buf, n)
19981950
}
19991951

20001952
func (f *framer) writeUint(n uint32) {
2001-
f.buf = appendUint(f.buf, n)
1953+
f.buf = internal.AppendUint(f.buf, n)
20021954
}
20031955

20041956
func (f *framer) writeShort(n uint16) {
2005-
f.buf = appendShort(f.buf, n)
1957+
f.buf = internal.AppendShort(f.buf, n)
20061958
}
20071959

20081960
func (f *framer) writeLong(n int64) {
2009-
f.buf = appendLong(f.buf, n)
1961+
f.buf = internal.AppendLong(f.buf, n)
20101962
}
20111963

20121964
func (f *framer) writeString(s string) {

helpers.go

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ package gocql
2626

2727
import (
2828
"fmt"
29+
"github.com/gocql/gocql/internal"
2930
"math/big"
3031
"net"
3132
"reflect"
@@ -176,7 +177,7 @@ func getCassandraType(name string, logger StdLogger) TypeInfo {
176177
Elem: getCassandraType(strings.TrimPrefix(name[:len(name)-1], "list<"), logger),
177178
}
178179
} else if strings.HasPrefix(name, "map<") {
179-
names := splitCompositeTypes(strings.TrimPrefix(name[:len(name)-1], "map<"))
180+
names := internal.SplitCompositeTypes(strings.TrimPrefix(name[:len(name)-1], "map<"))
180181
if len(names) != 2 {
181182
logger.Printf("Error parsing map type, it has %d subelements, expecting 2\n", len(names))
182183
return NativeType{
@@ -189,7 +190,7 @@ func getCassandraType(name string, logger StdLogger) TypeInfo {
189190
Elem: getCassandraType(names[1], logger),
190191
}
191192
} else if strings.HasPrefix(name, "tuple<") {
192-
names := splitCompositeTypes(strings.TrimPrefix(name[:len(name)-1], "tuple<"))
193+
names := internal.SplitCompositeTypes(strings.TrimPrefix(name[:len(name)-1], "tuple<"))
193194
types := make([]TypeInfo, len(names))
194195

195196
for i, name := range names {
@@ -207,34 +208,6 @@ func getCassandraType(name string, logger StdLogger) TypeInfo {
207208
}
208209
}
209210

210-
func splitCompositeTypes(name string) []string {
211-
if !strings.Contains(name, "<") {
212-
return strings.Split(name, ", ")
213-
}
214-
var parts []string
215-
lessCount := 0
216-
segment := ""
217-
for _, char := range name {
218-
if char == ',' && lessCount == 0 {
219-
if segment != "" {
220-
parts = append(parts, strings.TrimSpace(segment))
221-
}
222-
segment = ""
223-
continue
224-
}
225-
segment += string(char)
226-
if char == '<' {
227-
lessCount++
228-
} else if char == '>' {
229-
lessCount--
230-
}
231-
}
232-
if segment != "" {
233-
parts = append(parts, strings.TrimSpace(segment))
234-
}
235-
return parts
236-
}
237-
238211
func apacheToCassandraType(t string) string {
239212
t = strings.Replace(t, apacheCassandraTypePrefix, "", -1)
240213
t = strings.Replace(t, "(", "<", -1)
@@ -451,12 +424,6 @@ func (iter *Iter) MapScan(m map[string]interface{}) bool {
451424
return false
452425
}
453426

454-
func copyBytes(p []byte) []byte {
455-
b := make([]byte, len(p))
456-
copy(b, p)
457-
return b
458-
}
459-
460427
var failDNS = false
461428

462429
func LookupIP(host string) ([]net.IP, error) {

internal/frame.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package internal
2+
3+
type UnsetColumn struct{}
4+
5+
func ReadInt(p []byte) int32 {
6+
return int32(p[0])<<24 | int32(p[1])<<16 | int32(p[2])<<8 | int32(p[3])
7+
}
8+
9+
func AppendBytes(p []byte, d []byte) []byte {
10+
if d == nil {
11+
return AppendInt(p, -1)
12+
}
13+
p = AppendInt(p, int32(len(d)))
14+
p = append(p, d...)
15+
return p
16+
}
17+
18+
func AppendShort(p []byte, n uint16) []byte {
19+
return append(p,
20+
byte(n>>8),
21+
byte(n),
22+
)
23+
}
24+
25+
func AppendInt(p []byte, n int32) []byte {
26+
return append(p, byte(n>>24),
27+
byte(n>>16),
28+
byte(n>>8),
29+
byte(n))
30+
}
31+
32+
func AppendUint(p []byte, n uint32) []byte {
33+
return append(p, byte(n>>24),
34+
byte(n>>16),
35+
byte(n>>8),
36+
byte(n))
37+
}
38+
39+
func AppendLong(p []byte, n int64) []byte {
40+
return append(p,
41+
byte(n>>56),
42+
byte(n>>48),
43+
byte(n>>40),
44+
byte(n>>32),
45+
byte(n>>24),
46+
byte(n>>16),
47+
byte(n>>8),
48+
byte(n),
49+
)
50+
}

internal/helpers.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package internal
2+
3+
import "strings"
4+
5+
func SplitCompositeTypes(name string) []string {
6+
if !strings.Contains(name, "<") {
7+
return strings.Split(name, ", ")
8+
}
9+
var parts []string
10+
lessCount := 0
11+
segment := ""
12+
for _, char := range name {
13+
if char == ',' && lessCount == 0 {
14+
if segment != "" {
15+
parts = append(parts, strings.TrimSpace(segment))
16+
}
17+
segment = ""
18+
continue
19+
}
20+
segment += string(char)
21+
if char == '<' {
22+
lessCount++
23+
} else if char == '>' {
24+
lessCount--
25+
}
26+
}
27+
if segment != "" {
28+
parts = append(parts, strings.TrimSpace(segment))
29+
}
30+
return parts
31+
}
32+
33+
func CopyBytes(p []byte) []byte {
34+
b := make([]byte, len(p))
35+
copy(b, p)
36+
return b
37+
}

0 commit comments

Comments
 (0)