// Copyright 2020 TiKV Project Authors. Licensed under Apache-2.0.
// Modifications Copyright 2022 PingCAP, Inc.
//! This file contains the "actions" we perform on a
//! [`crate::storage::mvcc::MvccTxn`] and related tests. "Actions" here means a
//! group of more basic operations, eg.
//! [`crate::storage::mvcc::MvccReader::load_lock`],
//! [`crate::storage::mvcc::MvccTxn::put_write`], which are methods on
//! [`crate::storage::mvcc::MvccTxn`], for archiving a certain target.

pub mod acquire_pessimistic_lock;
pub mod check_data_constraint;
pub mod check_txn_status;
pub mod cleanup;
pub mod commit;
pub mod prewrite;
pub mod tests;

use txn_types::{Key, Lock, LockType};

use crate::storage::{
    metrics::TXN_FILE_LOCK_REJECT_IN_NORMAL_PATH_COUNTER,
    mvcc::{ErrorInner, Result},
};

fn reject_txn_file_lock(lock: &Lock, key: &Key) -> Result<()> {
    if lock.is_txn_file {
        warn!(
            "reject txn file lock in normal txn path";
            "start_ts" => lock.ts,
            "key" => ?key,
        );
        TXN_FILE_LOCK_REJECT_IN_NORMAL_PATH_COUNTER.inc();
        return Err(ErrorInner::LockTypeNotMatch {
            start_ts: lock.ts,
            key: key.clone().into_raw().unwrap_or_default(),
            pessimistic: lock.lock_type == LockType::Pessimistic,
            is_txn_file: true,
        }
        .into());
    }
    Ok(())
}
