replaceRow removes all duplicate rows for one row, then inserts it.
(ctx context.Context, r toBeCheckedRow, dupKeyCheck table.DupKeyCheckMode)
| 64 | |
| 65 | // replaceRow removes all duplicate rows for one row, then inserts it. |
| 66 | func (e *ReplaceExec) replaceRow(ctx context.Context, r toBeCheckedRow, dupKeyCheck table.DupKeyCheckMode) error { |
| 67 | txn, err := e.Ctx().Txn(true) |
| 68 | if err != nil { |
| 69 | return err |
| 70 | } |
| 71 | |
| 72 | if r.handleKey != nil { |
| 73 | handle, err := tablecodec.DecodeRowKey(r.handleKey.newKey) |
| 74 | if err != nil { |
| 75 | return err |
| 76 | } |
| 77 | |
| 78 | if _, err := txn.Get(ctx, r.handleKey.newKey); err == nil { |
| 79 | rowUnchanged, err := e.removeRow(ctx, txn, handle, r, true) |
| 80 | if err != nil { |
| 81 | return err |
| 82 | } |
| 83 | if rowUnchanged { |
| 84 | return nil |
| 85 | } |
| 86 | } else { |
| 87 | if !kv.IsErrNotFound(err) { |
| 88 | return err |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // Keep on removing duplicated rows. |
| 94 | for { |
| 95 | rowUnchanged, foundDupKey, err := e.removeIndexRow(ctx, txn, r) |
| 96 | if err != nil { |
| 97 | return err |
| 98 | } |
| 99 | if rowUnchanged { |
| 100 | return nil |
| 101 | } |
| 102 | if foundDupKey { |
| 103 | continue |
| 104 | } |
| 105 | break |
| 106 | } |
| 107 | |
| 108 | // No duplicated rows now, insert the row. |
| 109 | err = e.addRecord(ctx, r.row, dupKeyCheck) |
| 110 | if err != nil { |
| 111 | return err |
| 112 | } |
| 113 | return nil |
| 114 | } |
| 115 | |
| 116 | // removeIndexRow removes the row which has a duplicated key. |
| 117 | // the return values: |
no test coverage detected