Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Harshit Gangal <[email protected]>
  • Loading branch information
harshit-gangal committed Nov 4, 2024
1 parent ed6e267 commit 9fafcb1
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 3 deletions.
73 changes: 73 additions & 0 deletions go/mysql/result.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
Copyright 2024 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package mysql

import (
"vitess.io/vitess/go/sqltypes"
querypb "vitess.io/vitess/go/vt/proto/query"
)

// ParseResult converts the raw packets in a QueryResult to a sqltypes.Result.
func ParseResult(qr *querypb.QueryResult, wantfields bool) (*sqltypes.Result, error) {
if qr.RawPackets == nil {
return sqltypes.Proto3ToResult(qr), nil
}

var colcount int
for i, p := range qr.RawPackets {
if len(p) == 0 {
colcount = i
break
}
}

var err error
fieldArray := make([]querypb.Field, colcount)
fieldPackets := qr.RawPackets[:colcount]
rowPackets := qr.RawPackets[colcount+1:]

result := &sqltypes.Result{
RowsAffected: qr.RowsAffected,
InsertID: qr.InsertId,
SessionStateChanges: qr.SessionStateChanges,
Info: qr.Info,
Fields: make([]*querypb.Field, len(fieldPackets)),
Rows: make([]sqltypes.Row, 0, len(rowPackets)),
}

for i, fieldpkt := range fieldPackets {
result.Fields[i] = &fieldArray[i]
if wantfields {
err = parseColumnDefinition(fieldpkt, result.Fields[i], i)
} else {
err = parseColumnDefinitionType(fieldpkt, result.Fields[i], i)
}
if err != nil {
return nil, err
}
}

for _, rowpkt := range rowPackets {
r, err := parseRow(rowpkt, result.Fields, readLenEncStringAsBytes, nil)
if err != nil {
return nil, err
}
result.Rows = append(result.Rows, r)
}

return result, nil
}
17 changes: 17 additions & 0 deletions go/vt/proto/query/query_membuffer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions go/vt/servenv/grpc_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ type vtprotoMessage interface {
SizeVT() int
}

type vtprotoMemBuffer interface {
MarshalToMemBufferVT() (mem.BufferSlice, error)
}

type Codec struct {
fallback encoding.CodecV2
}
Expand All @@ -43,7 +47,8 @@ func (Codec) Name() string { return Name }
var defaultBufferPool = mem.DefaultBufferPool()

func (c *Codec) Marshal(v any) (mem.BufferSlice, error) {
if m, ok := v.(vtprotoMessage); ok {
switch m := v.(type) {
case vtprotoMessage:
size := m.SizeVT()
if mem.IsBelowBufferPoolingThreshold(size) {
buf := make([]byte, size)
Expand All @@ -58,9 +63,11 @@ func (c *Codec) Marshal(v any) (mem.BufferSlice, error) {
return nil, err
}
return mem.BufferSlice{mem.NewBuffer(buf, defaultBufferPool)}, nil
case vtprotoMemBuffer:
return m.MarshalToMemBufferVT()
default:
return c.fallback.Marshal(v)
}

return c.fallback.Marshal(v)
}

func (c *Codec) Unmarshal(data mem.BufferSlice, v any) error {
Expand Down

0 comments on commit 9fafcb1

Please sign in to comment.