Repository: tikv/tikv
File: src/storage/mvcc/reader/reader.rs (Rust)

Code before:
            Some(Key::from_raw(b"k3\x00")),
            0,
            &visible_locks[..3],
            false,
        );
        check_scan_lock(
            None,
            Some(Key::from_raw(b"k3\x00")),
            3,
            &visible_locks[..3],
            true,
        );
        check_scan_lock(
            None,
            Some(Key::from_raw(b"k3\x00")),
            2,
            &visible_locks[..2],
            true,
        );
    }

    #[test]
    fn test_scan_locks_from_storage_shared_locks_limit() {
        // This test demonstrates that scan_locks_from_storage counts KEYS, not
        // individual locks. When SharedLocks are present on a key, the total
        // number of individual locks returned can exceed the limit value.
        let path = tempfile::Builder::new()
            .prefix("_test_scan_locks_shared_limit")
            .tempdir()
            .unwrap();
        let path = path.path().to_str().unwrap();
        let region = make_region(1, vec![], vec![]);
        let db = open_db(path, true);
        let mut engine = RegionEngine::new(&db, &region);

        // Create a SharedLocks with 5 individual locks on key "k1"
        let mut shared_locks = SharedLocks::new();
        for i in 1..=5u64 {
            let lock = Lock::new(
                LockType::Pessimistic,
                b"k1".to_vec(),
                i.into(),
                100,
                None,
                TimeStamp::zero(),
                0,
                TimeStamp::zero(),
                false,
            );
            shared_locks.insert_lock(lock).unwrap();
        }

Review comment (bug):
The test comment is misleading. It states "This test demonstrates that scan_locks_from_storage counts KEYS, not individual locks" which describes the old buggy behavior. Since this PR fixes the issue, the comment should instead describe what the test is verifying: that scan_locks_from_storage now correctly counts individual locks and respects the limit, not keys.

Code after:
            Some(Key::from_raw(b"k3\x00")),
            0,
            &visible_locks[..3],
            false,
        );
        check_scan_lock(
            None,
            Some(Key::from_raw(b"k3\x00")),
            3,
            &visible_locks[..3],
            true,
        );
        check_scan_lock(
            None,
            Some(Key::from_raw(b"k3\x00")),
            2,
            &visible_locks[..2],
            true,
        );
    }

    #[test]
    fn test_scan_locks_from_storage_shared_locks_limit() {
        // This test guarantees that the limit parameter take effect on shared locks as
        // well.
        let path = tempfile::Builder::new()
            .prefix("_test_storage_mvcc_reader_scan_locks_from_storage_shared_locks_limit")
            .tempdir()
            .unwrap();
        let path = path.path().to_str().unwrap();
        let region = make_region(1, vec![], vec![]);
        let db = open_db(path, true);
        let mut engine = RegionEngine::new(&db, &region);

        // Create a SharedLocks with 5 individual locks on key "k1"
        let mut shared_locks = SharedLocks::new();
        for i in 1..=5u64 {
            let lock = Lock::new(
                LockType::Pessimistic,
                format!("k{}", i).into_bytes(),
                i.into(),
                100,
                None,
                TimeStamp::zero(),
                0,
                TimeStamp::zero(),
                false,
            );
            shared_locks.insert_lock(lock).unwrap();
        }