@@ -288,6 +288,12 @@ where
288
288
value : Option < V > ,
289
289
) -> Result < ( SeqMarked < V > , SeqMarked < V > ) , io:: Error > {
290
290
let old_value = self . get ( space, key. clone ( ) ) . await ?;
291
+
292
+ if old_value. is_not_found ( ) && value. is_none ( ) {
293
+ // No such entry at all, no need to create a tombstone for delete
294
+ return Ok ( ( old_value, SeqMarked :: new_tombstone ( 0 ) ) ) ;
295
+ }
296
+
291
297
let order_key = self . set ( space, key, value. clone ( ) ) ;
292
298
let new_value = match value {
293
299
Some ( v) => order_key. map ( |_| v) ,
@@ -1387,4 +1393,43 @@ mod tests {
1387
1393
SeqMarked :: new_normal( 11 , value( "resurrected" ) )
1388
1394
) ;
1389
1395
}
1396
+
1397
+ #[ tokio:: test]
1398
+ async fn test_fetch_and_set_delete_nonexistent ( ) {
1399
+ let mut view = create_view ( create_base_view ( ) ) ;
1400
+
1401
+ // Try to delete a key that doesn't exist
1402
+ let ( old_value, new_value) = view
1403
+ . fetch_and_set ( TestSpace :: Space1 , key ( "nonexistent_key" ) , None )
1404
+ . await
1405
+ . unwrap ( ) ;
1406
+
1407
+ // Should return not_found for old value
1408
+ assert_eq ! ( old_value, SeqMarked :: new_not_found( ) ) ;
1409
+ // Should return tombstone with seq 0 (no tombstone created)
1410
+ assert_eq ! ( new_value, SeqMarked :: new_tombstone( 0 ) ) ;
1411
+
1412
+ // Verify no tombstone was actually created in the changes
1413
+ let key_exists_in_table = view
1414
+ . changes
1415
+ . get ( & TestSpace :: Space1 )
1416
+ . map ( |table| {
1417
+ table
1418
+ . inner
1419
+ . keys ( )
1420
+ . any ( |( k, _) | k == & key ( "nonexistent_key" ) )
1421
+ } )
1422
+ . unwrap_or ( false ) ;
1423
+ assert ! ( !key_exists_in_table) ;
1424
+
1425
+ // Verify the key still doesn't exist
1426
+ let current_value = view
1427
+ . get ( TestSpace :: Space1 , key ( "nonexistent_key" ) )
1428
+ . await
1429
+ . unwrap ( ) ;
1430
+ assert_eq ! ( current_value, SeqMarked :: new_not_found( ) ) ;
1431
+
1432
+ // Verify last_seq was not incremented
1433
+ assert_eq ! ( view. last_seq, InternalSeq :: new( 10 ) ) ;
1434
+ }
1390
1435
}
0 commit comments