MCPcopy Index your code
hub / github.com/CovenantSQL/CovenantSQL / Commit

Method Commit

storage/storage.go:173–230  ·  view source on GitHub ↗

Commit implements commit method of two-phase commit worker.

(ctx context.Context, wb twopc.WriteBatch)

Source from the content-addressed store, hash-verified

171
172// Commit implements commit method of two-phase commit worker.
173func (s *Storage) Commit(ctx context.Context, wb twopc.WriteBatch) (result interface{}, err error) {
174 el, ok := wb.(*ExecLog)
175
176 if !ok {
177 err = errors.New("unexpected WriteBatch type")
178 return
179 }
180
181 s.Lock()
182 defer s.Unlock()
183
184 if s.tx != nil {
185 if equalTxID(&s.id, &TxID{el.ConnectionID, el.SeqNo, el.Timestamp}) {
186 // get last insert id and affected rows result
187 execResult := ExecResult{}
188
189 for _, q := range s.queries {
190 // convert arguments types
191 args := make([]interface{}, len(q.Args))
192
193 for i, v := range q.Args {
194 args[i] = v
195 }
196
197 var res sql.Result
198 res, err = s.tx.ExecContext(ctx, q.Pattern, args...)
199
200 if err != nil {
201 log.WithError(err).Debug("commit query failed")
202 s.tx.Rollback()
203 s.tx = nil
204 s.queries = nil
205 return
206 }
207
208 lastInsertID, _ := res.LastInsertId()
209 rowsAffected, _ := res.RowsAffected()
210
211 execResult.LastInsertID = lastInsertID
212 execResult.RowsAffected += rowsAffected
213 }
214
215 s.tx.Commit()
216 s.tx = nil
217 s.queries = nil
218 result = execResult
219
220 return
221 }
222
223 err = fmt.Errorf("twopc: inconsistent state, currently in tx: "+
224 "conn = %d, seq = %d, time = %d", s.id.ConnectionID, s.id.SeqNo, s.id.Timestamp)
225 return
226 }
227
228 err = errors.New("twopc: tx not prepared")
229 return
230}

Callers

nothing calls this directly

Calls 10

WithErrorFunction · 0.92
equalTxIDFunction · 0.85
DebugMethod · 0.80
ErrorfMethod · 0.80
NewMethod · 0.65
ExecContextMethod · 0.65
RollbackMethod · 0.65
CommitMethod · 0.65
LastInsertIdMethod · 0.45
RowsAffectedMethod · 0.45

Tested by

no test coverage detected