github.com/matrixorigin/matrixone@v1.2.0/pkg/pb/query/query.go (about)

     1  // Copyright 2021 - 2023 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package query
    16  
    17  import (
    18  	"bytes"
    19  	"context"
    20  	"fmt"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    23  )
    24  
    25  type WrappedResponse struct {
    26  	*Response
    27  	ReleaseFunc func()
    28  }
    29  
    30  func (r *WrappedResponse) Reset() {
    31  	if r.ReleaseFunc != nil {
    32  		r.ReleaseFunc()
    33  	}
    34  	if r.Response != nil {
    35  		r.Response.Reset()
    36  	}
    37  }
    38  
    39  // SetID implements the morpc.Message interface.
    40  func (m *Request) SetID(id uint64) {
    41  	m.RequestID = id
    42  }
    43  
    44  // GetID implements the morpc.Message interface.
    45  func (m *Request) GetID() uint64 {
    46  	return m.RequestID
    47  }
    48  
    49  // GetMethod implements the morpc.MethodBasedMessage interface.
    50  func (m *Request) GetMethod() CmdMethod {
    51  	return m.GetCmdMethod()
    52  }
    53  
    54  // Method implements the morpc.MethodBasedMessage interface.
    55  func (m *Request) Method() uint32 {
    56  	return uint32(m.CmdMethod)
    57  }
    58  
    59  // SetMethod implements the morpc.MethodBasedMessage interface.
    60  func (m *Request) SetMethod(v uint32) {
    61  	m.CmdMethod = CmdMethod(v)
    62  }
    63  
    64  // DebugString implements the morpc.Message interface.
    65  func (m *Request) DebugString() string {
    66  	var buffer bytes.Buffer
    67  	buffer.WriteString(fmt.Sprintf("%d: ", m.RequestID))
    68  	buffer.WriteString(m.CmdMethod.String())
    69  	buffer.WriteString("/")
    70  	return buffer.String()
    71  }
    72  
    73  // WrapError implements the morpc.MethodBasedMessage interface.
    74  func (m *Request) WrapError(err error) {
    75  	panic("not supported")
    76  }
    77  
    78  // UnwrapError implements the morpc.MethodBasedMessage interface.
    79  func (m *Request) UnwrapError() error {
    80  	panic("not supported")
    81  }
    82  
    83  // SetID implements the morpc.Message interface.
    84  func (m *Response) SetID(id uint64) {
    85  	m.RequestID = id
    86  }
    87  
    88  // GetID implements the morpc.Message interface.
    89  func (m *Response) GetID() uint64 {
    90  	return m.RequestID
    91  }
    92  
    93  // Method implements the morpc.MethodBasedMessage interface.
    94  func (m *Response) Method() uint32 {
    95  	return uint32(m.CmdMethod)
    96  }
    97  
    98  // SetMethod implements the morpc.MethodBasedMessage interface.
    99  func (m *Response) SetMethod(v uint32) {
   100  	m.CmdMethod = CmdMethod(v)
   101  }
   102  
   103  // DebugString implements the morpc.Message interface.
   104  func (m *Response) DebugString() string {
   105  	var buffer bytes.Buffer
   106  	buffer.WriteString(fmt.Sprintf("%d: ", m.RequestID))
   107  	buffer.WriteString(m.CmdMethod.String())
   108  	buffer.WriteString("/")
   109  	return buffer.String()
   110  }
   111  
   112  // WrapError implements the morpc.MethodBasedMessage interface.
   113  func (m *Response) WrapError(err error) {
   114  	me := moerr.ConvertGoError(context.TODO(), err).(*moerr.Error)
   115  	data, e := me.MarshalBinary()
   116  	if e != nil {
   117  		panic(e)
   118  	}
   119  	m.Error = data
   120  }
   121  
   122  // UnwrapError implements the morpc.MethodBasedMessage interface.
   123  func (m *Response) UnwrapError() error {
   124  	if len(m.Error) == 0 {
   125  		return nil
   126  	}
   127  	err := &moerr.Error{}
   128  	if e := err.UnmarshalBinary(m.Error); e != nil {
   129  		panic(e)
   130  	}
   131  	return err
   132  }