Skip to content

Commit

Permalink
Merge pull request #874 from ydb-platform/xstring-buffer
Browse files Browse the repository at this point in the history
* Moved `internal/allocator.Buffers` to package `internal/xstring`
  • Loading branch information
asmyasnikov authored Oct 23, 2023
2 parents e1a1bb5 + 94565ea commit 15ba102
Show file tree
Hide file tree
Showing 25 changed files with 152 additions and 134 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* Moved `internal/allocator.Buffers` to package `internal/xstring`
* Bumped `golang.org/x/sync` to `v0.3.0`
* Bumped `google.golang.org/protobuf` to `v1.31.0`
* Bumped `google.golang.org/grpc` to `v1.57.1`
Expand Down
22 changes: 0 additions & 22 deletions internal/allocator/allocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
package allocator

import (
"bytes"
"sync"

"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Table"
)
Expand Down Expand Up @@ -192,22 +189,3 @@ func (a *Allocator) TableQueryID(id string) (v *Ydb_Table.Query_Id) {
Id: id,
}
}

var Buffers = &buffersPoolType{}

type buffersPoolType struct {
sync.Pool
}

func (p *buffersPoolType) Get() *bytes.Buffer {
v := p.Pool.Get()
if v == nil {
v = new(bytes.Buffer)
}
return v.(*bytes.Buffer)
}

func (p *buffersPoolType) Put(b *bytes.Buffer) {
b.Reset()
p.Pool.Put(b)
}
11 changes: 0 additions & 11 deletions internal/allocator/allocator_go1.18.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package allocator

import (
"bytes"
"sync"

"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
Expand Down Expand Up @@ -880,17 +879,7 @@ func (p *Pool[T]) Put(t *T) {
(*sync.Pool)(p).Put(t)
}

type buffersPoolType struct {
Pool[bytes.Buffer]
}

func (p *buffersPoolType) Put(b *bytes.Buffer) {
b.Reset()
p.Pool.Put(b)
}

var (
Buffers = &buffersPoolType{}
allocatorPool Pool[Allocator]
valuePool Pool[Ydb.Value]
typePool Pool[Ydb.Type]
Expand Down
6 changes: 3 additions & 3 deletions internal/bind/auto_declare.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package bind
import (
"sort"

"github.com/ydb-platform/ydb-go-sdk/v3/internal/allocator"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xstring"
)

type AutoDeclare struct{}
Expand All @@ -27,9 +27,9 @@ func (m AutoDeclare) RewriteQuery(query string, args ...interface{}) (

var (
declares = make([]string, 0, len(params))
buffer = allocator.Buffers.Get()
buffer = xstring.Buffer()
)
defer allocator.Buffers.Put(buffer)
defer buffer.Free()

buffer.WriteString("-- bind declares\n")

Expand Down
6 changes: 3 additions & 3 deletions internal/bind/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package bind
import (
"sort"

"github.com/ydb-platform/ydb-go-sdk/v3/internal/allocator"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xstring"
"github.com/ydb-platform/ydb-go-sdk/v3/table"
)

Expand Down Expand Up @@ -38,8 +38,8 @@ func (bindings Bindings) RewriteQuery(query string, args ...interface{}) (
return query, table.NewQueryParameters(params...), nil
}

buffer := allocator.Buffers.Get()
defer allocator.Buffers.Put(buffer)
buffer := xstring.Buffer()
defer buffer.Free()

for i := range bindings {
query, args, err = bindings[len(bindings)-1-i].RewriteQuery(query, args...)
Expand Down
6 changes: 3 additions & 3 deletions internal/bind/numeric_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"strconv"
"unicode/utf8"

"github.com/ydb-platform/ydb-go-sdk/v3/internal/allocator"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xstring"
"github.com/ydb-platform/ydb-go-sdk/v3/table"
)

Expand All @@ -30,10 +30,10 @@ func (m NumericArgs) RewriteQuery(sql string, args ...interface{}) (
}

var (
buffer = allocator.Buffers.Get()
buffer = xstring.Buffer()
param table.ParameterOption
)
defer allocator.Buffers.Put(buffer)
defer buffer.Free()

if len(args) > 0 {
newArgs = make([]interface{}, len(args))
Expand Down
6 changes: 3 additions & 3 deletions internal/bind/positional_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"strconv"
"unicode/utf8"

"github.com/ydb-platform/ydb-go-sdk/v3/internal/allocator"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xstring"
"github.com/ydb-platform/ydb-go-sdk/v3/table"
)

Expand All @@ -30,11 +30,11 @@ func (m PositionalArgs) RewriteQuery(sql string, args ...interface{}) (
}

var (
buffer = allocator.Buffers.Get()
buffer = xstring.Buffer()
position = 0
param table.ParameterOption
)
defer allocator.Buffers.Put(buffer)
defer buffer.Free()

for _, p := range l.parts {
switch p := p.(type) {
Expand Down
6 changes: 3 additions & 3 deletions internal/bind/table_path_prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"path"
"strings"

"github.com/ydb-platform/ydb-go-sdk/v3/internal/allocator"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xstring"
)

type TablePathPrefix string
Expand All @@ -27,8 +27,8 @@ func (tablePathPrefix TablePathPrefix) NormalizePath(folderOrTable string) strin
func (tablePathPrefix TablePathPrefix) RewriteQuery(query string, args ...interface{}) (
yql string, newArgs []interface{}, err error,
) {
buffer := allocator.Buffers.Get()
defer allocator.Buffers.Put(buffer)
buffer := xstring.Buffer()
defer buffer.Free()

buffer.WriteString("-- bind TablePathPrefix\n")
buffer.WriteString("PRAGMA TablePathPrefix(\"")
Expand Down
42 changes: 21 additions & 21 deletions internal/credentials/access_error.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package credentials

import (
"bytes"
"fmt"
"io"
"reflect"
"strconv"

"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
grpcCodes "google.golang.org/grpc/codes"

"github.com/ydb-platform/ydb-go-sdk/v3/internal/allocator"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xstring"
)

type authErrorOption interface {
applyAuthErrorOption(buffer *bytes.Buffer)
applyAuthErrorOption(w io.Writer)
}

var (
Expand All @@ -26,9 +26,9 @@ var (

type addressAuthErrorOption string

func (address addressAuthErrorOption) applyAuthErrorOption(buffer *bytes.Buffer) {
buffer.WriteString("address:")
fmt.Fprintf(buffer, "%q", address)
func (address addressAuthErrorOption) applyAuthErrorOption(w io.Writer) {
fmt.Fprint(w, "address:")
fmt.Fprintf(w, "%q", address)
}

func WithAddress(address string) addressAuthErrorOption {
Expand All @@ -37,9 +37,9 @@ func WithAddress(address string) addressAuthErrorOption {

type endpointAuthErrorOption string

func (endpoint endpointAuthErrorOption) applyAuthErrorOption(buffer *bytes.Buffer) {
buffer.WriteString("endpoint:")
fmt.Fprintf(buffer, "%q", endpoint)
func (endpoint endpointAuthErrorOption) applyAuthErrorOption(w io.Writer) {
fmt.Fprint(w, "endpoint:")
fmt.Fprintf(w, "%q", endpoint)
}

func WithEndpoint(endpoint string) endpointAuthErrorOption {
Expand All @@ -48,9 +48,9 @@ func WithEndpoint(endpoint string) endpointAuthErrorOption {

type databaseAuthErrorOption string

func (address databaseAuthErrorOption) applyAuthErrorOption(buffer *bytes.Buffer) {
buffer.WriteString("database:")
fmt.Fprintf(buffer, "%q", address)
func (address databaseAuthErrorOption) applyAuthErrorOption(w io.Writer) {
fmt.Fprint(w, "database:")
fmt.Fprintf(w, "%q", address)
}

func WithDatabase(database string) databaseAuthErrorOption {
Expand All @@ -59,9 +59,9 @@ func WithDatabase(database string) databaseAuthErrorOption {

type nodeIDAuthErrorOption uint32

func (id nodeIDAuthErrorOption) applyAuthErrorOption(buffer *bytes.Buffer) {
buffer.WriteString("nodeID:")
buffer.WriteString(strconv.FormatUint(uint64(id), 10))
func (id nodeIDAuthErrorOption) applyAuthErrorOption(w io.Writer) {
fmt.Fprint(w, "nodeID:")
fmt.Fprint(w, strconv.FormatUint(uint64(id), 10))
}

func WithNodeID(id uint32) authErrorOption {
Expand All @@ -72,13 +72,13 @@ type credentialsUnauthenticatedErrorOption struct {
credentials Credentials
}

func (opt credentialsUnauthenticatedErrorOption) applyAuthErrorOption(buffer *bytes.Buffer) {
buffer.WriteString("credentials:")
func (opt credentialsUnauthenticatedErrorOption) applyAuthErrorOption(w io.Writer) {
fmt.Fprint(w, "credentials:")
if stringer, has := opt.credentials.(fmt.Stringer); has {
fmt.Fprintf(buffer, "%q", stringer.String())
fmt.Fprintf(w, "%q", stringer.String())
} else {
t := reflect.TypeOf(opt.credentials)
fmt.Fprintf(buffer, "%q", t.PkgPath()+"."+t.Name())
fmt.Fprintf(w, "%q", t.PkgPath()+"."+t.Name())
}
}

Expand All @@ -89,8 +89,8 @@ func WithCredentials(credentials Credentials) credentialsUnauthenticatedErrorOpt
}

func AccessError(msg string, err error, opts ...authErrorOption) error {
buffer := allocator.Buffers.Get()
defer allocator.Buffers.Put(buffer)
buffer := xstring.Buffer()
defer buffer.Free()
buffer.WriteString(msg)
buffer.WriteString(" (")
for i, opt := range opts {
Expand Down
6 changes: 3 additions & 3 deletions internal/credentials/access_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"context"
"fmt"

"github.com/ydb-platform/ydb-go-sdk/v3/internal/allocator"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/secret"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xstring"
)

var (
Expand Down Expand Up @@ -44,8 +44,8 @@ func (c AccessToken) Token(_ context.Context) (string, error) {

// Token implements Credentials.
func (c AccessToken) String() string {
buffer := allocator.Buffers.Get()
defer allocator.Buffers.Put(buffer)
buffer := xstring.Buffer()
defer buffer.Free()
buffer.WriteString("AccessToken(token:")
fmt.Fprintf(buffer, "%q", secret.Token(c.token))
if c.sourceInfo != "" {
Expand Down
6 changes: 3 additions & 3 deletions internal/credentials/anonymous.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"fmt"

"github.com/ydb-platform/ydb-go-sdk/v3/internal/allocator"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xstring"
)

var (
Expand Down Expand Up @@ -40,8 +40,8 @@ func (c Anonymous) Token(_ context.Context) (string, error) {

// Token implements Credentials.
func (c Anonymous) String() string {
buffer := allocator.Buffers.Get()
defer allocator.Buffers.Put(buffer)
buffer := xstring.Buffer()
defer buffer.Free()
buffer.WriteString("Anonymous(")
if c.sourceInfo != "" {
buffer.WriteString("from:")
Expand Down
6 changes: 3 additions & 3 deletions internal/credentials/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Operations"
"google.golang.org/grpc"

"github.com/ydb-platform/ydb-go-sdk/v3/internal/allocator"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/secret"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xstring"
)

var (
Expand Down Expand Up @@ -145,8 +145,8 @@ func parseExpiresAt(raw string) (expiresAt time.Time, err error) {
}

func (c *Static) String() string {
buffer := allocator.Buffers.Get()
defer allocator.Buffers.Put(buffer)
buffer := xstring.Buffer()
defer buffer.Free()
buffer.WriteString("Static(user:")
fmt.Fprintf(buffer, "%q", c.user)
buffer.WriteString(",password:")
Expand Down
6 changes: 3 additions & 3 deletions internal/stack/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"runtime"
"strings"

"github.com/ydb-platform/ydb-go-sdk/v3/internal/allocator"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xstring"
)

type recordOptions struct {
Expand Down Expand Up @@ -109,8 +109,8 @@ func Record(depth int, opts ...recordOption) string {
structName = split[1]
}

buffer := allocator.Buffers.Get()
defer allocator.Buffers.Put(buffer)
buffer := xstring.Buffer()
defer buffer.Free()
if optionsHolder.packagePath {
buffer.WriteString(pkgPath)
}
Expand Down
5 changes: 2 additions & 3 deletions internal/table/scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"

"github.com/ydb-platform/ydb-go-sdk/v3/internal/allocator"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/value"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xstring"
Expand Down Expand Up @@ -251,8 +250,8 @@ func (s *scanner) reset(set *Ydb.ResultSet, columnNames ...string) {
}

func (s *scanner) path() string {
buf := allocator.Buffers.Get()
defer allocator.Buffers.Put(buf)
buf := xstring.Buffer()
defer buf.Free()
_, _ = s.writePathTo(buf)
return buf.String()
}
Expand Down
Loading

0 comments on commit 15ba102

Please sign in to comment.