Skip to content

Commit f8a086e

Browse files
committed
txn: add CheckNotExist mutation when delete-your-writes happens
Signed-off-by: Ziyi Yan <ziyi.yan@foxmail.com>
1 parent d3f63fe commit f8a086e

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/transaction/buffer.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,20 @@ impl Buffer {
211211

212212
/// Mark a value as deleted.
213213
pub async fn delete(&self, key: Key) {
214-
self.mutations.lock().await.insert(key, BufferEntry::Del);
214+
let mut mutations = self.mutations.lock().await;
215+
let value = mutations
216+
.entry_map
217+
.entry(key.clone())
218+
.or_insert(BufferEntry::Del);
219+
220+
let new_value: BufferEntry;
221+
if let BufferEntry::Insert(_) = value {
222+
new_value = BufferEntry::CheckNotExist
223+
} else {
224+
new_value = BufferEntry::Del
225+
}
226+
227+
mutations.insert(key, new_value);
215228
}
216229

217230
/// Converts the buffered mutations to the proto buffer version
@@ -257,6 +270,9 @@ impl Buffer {
257270
Some(BufferEntry::Insert(v)) => {
258271
assert!(value.as_ref() == Some(v))
259272
}
273+
Some(BufferEntry::CheckNotExist) => {
274+
assert!(value.is_none());
275+
}
260276
}
261277
}
262278
}
@@ -268,6 +284,7 @@ impl Buffer {
268284
// - `Put`
269285
// - `Del`
270286
// - `Insert`
287+
// - `CheckNotExist`, a constraint to ensure the key doesn't exist. See https://github.com/pingcap/tidb/pull/14968.
271288
// Cache of read requests:
272289
// - `Cached`, generated by normal read requests
273290
// - `ReadLockCached`, generated by lock commands (`lock_keys`, `get_for_update`) and optionally read requests
@@ -297,6 +314,8 @@ enum BufferEntry {
297314
Del,
298315
// Key should be check not exists before.
299316
Insert(Value),
317+
// Key should be check not exists before.
318+
CheckNotExist,
300319
}
301320

302321
impl BufferEntry {
@@ -314,6 +333,7 @@ impl BufferEntry {
314333
pb.set_op(kvrpcpb::Op::Insert);
315334
pb.set_value(v.clone());
316335
}
336+
BufferEntry::CheckNotExist => pb.set_op(kvrpcpb::Op::CheckNotExists),
317337
};
318338
pb.set_key(key.clone().into());
319339
Some(pb)
@@ -327,6 +347,7 @@ impl BufferEntry {
327347
BufferEntry::Locked(None) => MutationValue::Undetermined,
328348
BufferEntry::Locked(Some(value)) => MutationValue::Determined(value.clone()),
329349
BufferEntry::Insert(value) => MutationValue::Determined(Some(value.clone())),
350+
BufferEntry::CheckNotExist => MutationValue::Determined(None),
330351
}
331352
}
332353
}

0 commit comments

Comments
 (0)