Commit commits the transaction, following these steps: 1. check the length of pendingWrites.If there are no writes, return immediately. 2. check if the ActiveFile has not enough space to store entry. if not, call rotateActiveFile function. 3. write pendingWrites to disk, if a non-nil error,return
()
| 174 | // |
| 175 | // 6. Unlock the database and clear the db field. |
| 176 | func (tx *Tx) Commit() (err error) { |
| 177 | defer func() { |
| 178 | if err != nil { |
| 179 | tx.handleErr(err) |
| 180 | } |
| 181 | |
| 182 | tx.unlock() |
| 183 | |
| 184 | // Ensure the transaction is unregistered so active counts stay accurate |
| 185 | tx.db.transactionMgr.UnregisterTx(tx.id) |
| 186 | |
| 187 | tx.db = nil |
| 188 | |
| 189 | tx.pendingWrites = nil |
| 190 | }() |
| 191 | |
| 192 | if tx.isClosed() { |
| 193 | return ErrCannotCommitAClosedTx |
| 194 | } |
| 195 | |
| 196 | if tx.db == nil { |
| 197 | tx.setStatusClosed() |
| 198 | return ErrDBClosed |
| 199 | } |
| 200 | |
| 201 | var curWriteCount int64 |
| 202 | |
| 203 | // If the database is closing/closed, abort early to avoid touching released resources. |
| 204 | if tx.db.statusMgr.isClosed() { |
| 205 | return ErrDBClosed |
| 206 | } |
| 207 | |
| 208 | if tx.db.opt.MaxWriteRecordCount > 0 { |
| 209 | curWriteCount, err = tx.getNewAddRecordCount() |
| 210 | if err != nil { |
| 211 | return err |
| 212 | } |
| 213 | |
| 214 | // judge all write records is whether more than the MaxWriteRecordCount |
| 215 | if tx.db.RecordCount+curWriteCount > tx.db.opt.MaxWriteRecordCount { |
| 216 | return ErrTxnExceedWriteLimit |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | tx.setStatusCommitting() |
| 221 | defer tx.setStatusClosed() |
| 222 | |
| 223 | writesBucketLen := len(tx.pendingBucketList) |
| 224 | if tx.pendingWrites.size == 0 && writesBucketLen == 0 { |
| 225 | return nil |
| 226 | } |
| 227 | |
| 228 | buff := tx.allocCommitBuffer() |
| 229 | defer tx.db.commitBuffer.Reset() |
| 230 | |
| 231 | var records []*core.Record |
| 232 | |
| 233 | pendingWriteList := tx.pendingWrites.toList() |