github.com/matrixorigin/matrixone@v1.2.0/pkg/txn/storage/memorystorage/write.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 21 "github.com/matrixorigin/matrixone/pkg/pb/txn" 22 "github.com/matrixorigin/matrixone/pkg/vm/engine/memoryengine" 23 ) 24 25 func (s *Storage) Write(ctx context.Context, txnMeta txn.TxnMeta, op uint32, payload []byte) (result []byte, err error) { 26 27 switch op { 28 29 case memoryengine.OpCreateDatabase: 30 return handleWrite( 31 ctx, txnMeta, payload, 32 s.handler.HandleCreateDatabase, 33 ) 34 35 case memoryengine.OpDeleteDatabase: 36 return handleWrite( 37 ctx, txnMeta, payload, 38 s.handler.HandleDeleteDatabase, 39 ) 40 41 case memoryengine.OpCreateRelation: 42 return handleWrite( 43 ctx, txnMeta, payload, 44 s.handler.HandleCreateRelation, 45 ) 46 47 case memoryengine.OpDeleteRelation: 48 return handleWrite( 49 ctx, txnMeta, payload, 50 s.handler.HandleDeleteRelation, 51 ) 52 53 case memoryengine.OpTruncateRelation: 54 return handleWrite( 55 ctx, txnMeta, payload, 56 s.handler.HandleTruncateRelation, 57 ) 58 59 case memoryengine.OpAddTableDef: 60 return handleWrite( 61 ctx, txnMeta, payload, 62 s.handler.HandleAddTableDef, 63 ) 64 65 case memoryengine.OpDelTableDef: 66 return handleWrite( 67 ctx, txnMeta, payload, 68 s.handler.HandleDelTableDef, 69 ) 70 71 case memoryengine.OpDelete: 72 return handleWrite( 73 ctx, txnMeta, payload, 74 s.handler.HandleDelete, 75 ) 76 77 //case memoryengine.OpTruncate: 78 // return handleWrite( 79 // ctx, txnMeta, payload, 80 // s.handler.HandleTruncate, 81 // ) 82 83 case memoryengine.OpUpdate: 84 return handleWrite( 85 ctx, txnMeta, payload, 86 s.handler.HandleUpdate, 87 ) 88 89 case memoryengine.OpWrite: 90 return handleWrite( 91 ctx, txnMeta, payload, 92 s.handler.HandleWrite, 93 ) 94 95 } 96 97 return 98 } 99 100 func handleWrite[ 101 Req any, PReq interface { 102 // anonymous constraint 103 encoding.BinaryUnmarshaler 104 // make Req convertible to its pointer type 105 *Req 106 }, 107 Resp any, PResp interface { 108 // anonymous constraint 109 encoding.BinaryMarshaler 110 // make Resp convertible to its pointer type 111 *Resp 112 }, 113 ]( 114 ctx context.Context, 115 meta txn.TxnMeta, 116 payload []byte, 117 fn func( 118 ctx context.Context, 119 meta txn.TxnMeta, 120 preq PReq, 121 presp PResp, 122 ) ( 123 err error, 124 ), 125 ) ( 126 res []byte, 127 err error, 128 ) { 129 130 var preq PReq = new(Req) 131 if err := preq.UnmarshalBinary(payload); err != nil { 132 return nil, err 133 } 134 135 var presp PResp = new(Resp) 136 defer logReq("write", preq, meta, presp, &err)() 137 138 err = fn(ctx, meta, preq, presp) 139 if err != nil { 140 return nil, err 141 } 142 143 data, err := presp.MarshalBinary() 144 if err != nil { 145 return nil, err 146 } 147 res = data 148 149 return 150 }