github.com/matrixorigin/matrixone@v1.2.0/pkg/txn/storage/memorystorage/read.go (about) 1 // Copyright 2022 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 memorystorage 16 17 import ( 18 "context" 19 "encoding" 20 "io" 21 22 "github.com/matrixorigin/matrixone/pkg/pb/txn" 23 "github.com/matrixorigin/matrixone/pkg/txn/storage" 24 "github.com/matrixorigin/matrixone/pkg/vm/engine/memoryengine" 25 ) 26 27 func (s *Storage) Read(ctx context.Context, txnMeta txn.TxnMeta, op uint32, payload []byte) (res storage.ReadResult, err error) { 28 29 switch op { 30 31 case memoryengine.OpOpenDatabase: 32 return handleRead( 33 ctx, txnMeta, payload, 34 s.handler.HandleOpenDatabase, 35 ) 36 37 case memoryengine.OpGetDatabases: 38 return handleRead( 39 ctx, txnMeta, payload, 40 s.handler.HandleGetDatabases, 41 ) 42 43 case memoryengine.OpOpenRelation: 44 return handleRead( 45 ctx, txnMeta, payload, 46 s.handler.HandleOpenRelation, 47 ) 48 49 case memoryengine.OpGetRelations: 50 return handleRead( 51 ctx, txnMeta, payload, 52 s.handler.HandleGetRelations, 53 ) 54 55 case memoryengine.OpGetPrimaryKeys: 56 return handleRead( 57 ctx, txnMeta, payload, 58 s.handler.HandleGetPrimaryKeys, 59 ) 60 61 case memoryengine.OpGetTableDefs: 62 return handleRead( 63 ctx, txnMeta, payload, 64 s.handler.HandleGetTableDefs, 65 ) 66 67 case memoryengine.OpGetHiddenKeys: 68 return handleRead( 69 ctx, txnMeta, payload, 70 s.handler.HandleGetHiddenKeys, 71 ) 72 73 case memoryengine.OpNewTableIter: 74 return handleRead( 75 ctx, txnMeta, payload, 76 s.handler.HandleNewTableIter, 77 ) 78 79 case memoryengine.OpRead: 80 return handleRead( 81 ctx, txnMeta, payload, 82 s.handler.HandleRead, 83 ) 84 85 case memoryengine.OpCloseTableIter: 86 return handleRead( 87 ctx, txnMeta, payload, 88 s.handler.HandleCloseTableIter, 89 ) 90 91 case memoryengine.OpTableStats: 92 return handleRead( 93 ctx, txnMeta, payload, 94 s.handler.HandleTableStats, 95 ) 96 97 case memoryengine.OpGetTableColumns: 98 return handleRead( 99 ctx, txnMeta, payload, 100 s.handler.HandleGetTableColumns, 101 ) 102 103 } 104 105 return 106 } 107 108 func handleRead[ 109 Req any, PReq interface { 110 // anonymous constraint 111 encoding.BinaryUnmarshaler 112 // make Req convertible to its pointer type 113 *Req 114 }, 115 Resp any, PResp interface { 116 encoding.BinaryMarshaler 117 *Resp 118 }, 119 ]( 120 ctx context.Context, 121 txnMeta txn.TxnMeta, 122 payload []byte, 123 fn func( 124 ctx context.Context, 125 meta txn.TxnMeta, 126 preq PReq, 127 presp PResp, 128 ) ( 129 err error, 130 ), 131 ) ( 132 res storage.ReadResult, 133 err error, 134 ) { 135 136 var preq PReq = new(Req) 137 if err := preq.UnmarshalBinary(payload); err != nil { 138 return nil, err 139 } 140 141 var presp PResp = new(Resp) 142 defer logReq("read", preq, txnMeta, presp, &err)() 143 defer func() { 144 if closer, ok := (any)(presp).(io.Closer); ok { 145 _ = closer.Close() 146 } 147 }() 148 149 err = fn(ctx, txnMeta, preq, presp) 150 if err != nil { 151 return nil, err 152 } 153 154 data, err := presp.MarshalBinary() 155 if err != nil { 156 return nil, err 157 } 158 res = &readResult{ 159 payload: data, 160 } 161 162 return res, nil 163 }