Skip to content

Commit 41ef055

Browse files
committed
ports from vitess/go/vt/sqlparser
1 parent 715d56e commit 41ef055

70 files changed

Lines changed: 27375 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

bytes2/buffer.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Copyright 2017 Google Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package bytes2
18+
19+
// Buffer implements a subset of the write portion of
20+
// bytes.Buffer, but more efficiently. This is meant to
21+
// be used in very high QPS operations, especially for
22+
// WriteByte, and without abstracting it as a Writer.
23+
// Function signatures contain errors for compatibility,
24+
// but they do not return errors.
25+
type Buffer struct {
26+
bytes []byte
27+
}
28+
29+
// NewBuffer is equivalent to bytes.NewBuffer.
30+
func NewBuffer(b []byte) *Buffer {
31+
return &Buffer{bytes: b}
32+
}
33+
34+
// Write is equivalent to bytes.Buffer.Write.
35+
func (buf *Buffer) Write(b []byte) (int, error) {
36+
buf.bytes = append(buf.bytes, b...)
37+
return len(b), nil
38+
}
39+
40+
// WriteString is equivalent to bytes.Buffer.WriteString.
41+
func (buf *Buffer) WriteString(s string) (int, error) {
42+
buf.bytes = append(buf.bytes, s...)
43+
return len(s), nil
44+
}
45+
46+
// WriteByte is equivalent to bytes.Buffer.WriteByte.
47+
func (buf *Buffer) WriteByte(b byte) error {
48+
buf.bytes = append(buf.bytes, b)
49+
return nil
50+
}
51+
52+
// Bytes is equivalent to bytes.Buffer.Bytes.
53+
func (buf *Buffer) Bytes() []byte {
54+
return buf.bytes
55+
}
56+
57+
// Strings is equivalent to bytes.Buffer.Strings.
58+
func (buf *Buffer) String() string {
59+
return string(buf.bytes)
60+
}
61+
62+
// Len is equivalent to bytes.Buffer.Len.
63+
func (buf *Buffer) Len() int {
64+
return len(buf.bytes)
65+
}

bytes2/buffer_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright 2017 Google Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package bytes2
18+
19+
import "testing"
20+
21+
func TestBuffer(t *testing.T) {
22+
b := NewBuffer(nil)
23+
b.Write([]byte("ab"))
24+
b.WriteString("cd")
25+
b.WriteByte('e')
26+
want := "abcde"
27+
if got := string(b.Bytes()); got != want {
28+
t.Errorf("b.Bytes(): %s, want %s", got, want)
29+
}
30+
if got := b.String(); got != want {
31+
t.Errorf("b.String(): %s, want %s", got, want)
32+
}
33+
if got := b.Len(); got != 5 {
34+
t.Errorf("b.Len(): %d, want 5", got)
35+
}
36+
}

hack/hack.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
Copyright 2017 Google Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package hack gives you some efficient functionality at the cost of
18+
// breaking some Go rules.
19+
package hack
20+
21+
import (
22+
"reflect"
23+
"unsafe"
24+
)
25+
26+
// StringArena lets you consolidate allocations for a group of strings
27+
// that have similar life length
28+
type StringArena struct {
29+
buf []byte
30+
str string
31+
}
32+
33+
// NewStringArena creates an arena of the specified size.
34+
func NewStringArena(size int) *StringArena {
35+
sa := &StringArena{buf: make([]byte, 0, size)}
36+
pbytes := (*reflect.SliceHeader)(unsafe.Pointer(&sa.buf))
37+
pstring := (*reflect.StringHeader)(unsafe.Pointer(&sa.str))
38+
pstring.Data = pbytes.Data
39+
pstring.Len = pbytes.Cap
40+
return sa
41+
}
42+
43+
// NewString copies a byte slice into the arena and returns it as a string.
44+
// If the arena is full, it returns a traditional go string.
45+
func (sa *StringArena) NewString(b []byte) string {
46+
if len(b) == 0 {
47+
return ""
48+
}
49+
if len(sa.buf)+len(b) > cap(sa.buf) {
50+
return string(b)
51+
}
52+
start := len(sa.buf)
53+
sa.buf = append(sa.buf, b...)
54+
return sa.str[start : start+len(b)]
55+
}
56+
57+
// SpaceLeft returns the amount of space left in the arena.
58+
func (sa *StringArena) SpaceLeft() int {
59+
return cap(sa.buf) - len(sa.buf)
60+
}
61+
62+
// String force casts a []byte to a string.
63+
// USE AT YOUR OWN RISK
64+
func String(b []byte) (s string) {
65+
if len(b) == 0 {
66+
return ""
67+
}
68+
pbytes := (*reflect.SliceHeader)(unsafe.Pointer(&b))
69+
pstring := (*reflect.StringHeader)(unsafe.Pointer(&s))
70+
pstring.Data = pbytes.Data
71+
pstring.Len = pbytes.Len
72+
return
73+
}
74+
75+
// StringPointer returns &s[0], which is not allowed in go
76+
func StringPointer(s string) unsafe.Pointer {
77+
pstring := (*reflect.StringHeader)(unsafe.Pointer(&s))
78+
return unsafe.Pointer(pstring.Data)
79+
}

hack/hack_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
Copyright 2017 Google Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package hack
18+
19+
import "testing"
20+
21+
func TestStringArena(t *testing.T) {
22+
sarena := NewStringArena(10)
23+
24+
s0 := sarena.NewString(nil)
25+
checkint(t, len(sarena.buf), 0)
26+
checkint(t, sarena.SpaceLeft(), 10)
27+
checkstring(t, s0, "")
28+
29+
s1 := sarena.NewString([]byte("01234"))
30+
checkint(t, len(sarena.buf), 5)
31+
checkint(t, sarena.SpaceLeft(), 5)
32+
checkstring(t, s1, "01234")
33+
34+
s2 := sarena.NewString([]byte("5678"))
35+
checkint(t, len(sarena.buf), 9)
36+
checkint(t, sarena.SpaceLeft(), 1)
37+
checkstring(t, s2, "5678")
38+
39+
// s3 will be allocated outside of sarena
40+
s3 := sarena.NewString([]byte("ab"))
41+
checkint(t, len(sarena.buf), 9)
42+
checkint(t, sarena.SpaceLeft(), 1)
43+
checkstring(t, s3, "ab")
44+
45+
// s4 should still fit in sarena
46+
s4 := sarena.NewString([]byte("9"))
47+
checkint(t, len(sarena.buf), 10)
48+
checkint(t, sarena.SpaceLeft(), 0)
49+
checkstring(t, s4, "9")
50+
51+
sarena.buf[0] = 'A'
52+
checkstring(t, s1, "A1234")
53+
54+
sarena.buf[5] = 'B'
55+
checkstring(t, s2, "B678")
56+
57+
sarena.buf[9] = 'C'
58+
// s3 will not change
59+
checkstring(t, s3, "ab")
60+
checkstring(t, s4, "C")
61+
checkstring(t, sarena.str, "A1234B678C")
62+
}
63+
64+
func checkstring(t *testing.T, actual, expected string) {
65+
if actual != expected {
66+
t.Errorf("received %s, expecting %s", actual, expected)
67+
}
68+
}
69+
70+
func checkint(t *testing.T, actual, expected int) {
71+
if actual != expected {
72+
t.Errorf("received %d, expecting %d", actual, expected)
73+
}
74+
}
75+
76+
func TestByteToString(t *testing.T) {
77+
v1 := []byte("1234")
78+
if s := String(v1); s != "1234" {
79+
t.Errorf("String(\"1234\"): %q, want 1234", s)
80+
}
81+
82+
v1 = []byte("")
83+
if s := String(v1); s != "" {
84+
t.Errorf("String(\"\"): %q, want empty", s)
85+
}
86+
87+
v1 = nil
88+
if s := String(v1); s != "" {
89+
t.Errorf("String(\"\"): %q, want empty", s)
90+
}
91+
}

logutil/console_logger.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
Copyright 2017 Google Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreedto in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package logutil
18+
19+
import (
20+
"fmt"
21+
22+
log "github.com/golang/glog"
23+
)
24+
25+
// ConsoleLogger is a Logger that uses glog directly to log, at the right level.
26+
//
27+
// Note that methods on ConsoleLogger must use pointer receivers,
28+
// because otherwise an autogenerated conversion method will be inserted in the
29+
// call stack when ConsoleLogger is used via TeeLogger, making the log depth
30+
// incorrect.
31+
type ConsoleLogger struct{}
32+
33+
// NewConsoleLogger returns a simple ConsoleLogger.
34+
func NewConsoleLogger() *ConsoleLogger {
35+
return &ConsoleLogger{}
36+
}
37+
38+
// Infof is part of the Logger interface
39+
func (cl *ConsoleLogger) Infof(format string, v ...interface{}) {
40+
cl.InfoDepth(1, fmt.Sprintf(format, v...))
41+
}
42+
43+
// Warningf is part of the Logger interface
44+
func (cl *ConsoleLogger) Warningf(format string, v ...interface{}) {
45+
cl.WarningDepth(1, fmt.Sprintf(format, v...))
46+
}
47+
48+
// Errorf is part of the Logger interface
49+
func (cl *ConsoleLogger) Errorf(format string, v ...interface{}) {
50+
cl.ErrorDepth(1, fmt.Sprintf(format, v...))
51+
}
52+
53+
// Printf is part of the Logger interface
54+
func (cl *ConsoleLogger) Printf(format string, v ...interface{}) {
55+
fmt.Printf(format, v...)
56+
}
57+
58+
// InfoDepth is part of the Logger interface.
59+
func (cl *ConsoleLogger) InfoDepth(depth int, s string) {
60+
log.InfoDepth(1+depth, s)
61+
}
62+
63+
// WarningDepth is part of the Logger interface.
64+
func (cl *ConsoleLogger) WarningDepth(depth int, s string) {
65+
log.WarningDepth(1+depth, s)
66+
}
67+
68+
// ErrorDepth is part of the Logger interface.
69+
func (cl *ConsoleLogger) ErrorDepth(depth int, s string) {
70+
log.ErrorDepth(1+depth, s)
71+
}

0 commit comments

Comments
 (0)