Repository: tikv/tikv
File: components/txn_types/src/lock.rs (Rust)

Code before:
                    either.as_ref().right()
                }
                Either::Right(lock) => Some(lock),
            }
        } else {
            None
        }
    }

    fn put_lock(&mut self, ts: TimeStamp, lock: Lock) -> Option<Lock> {
        let previous = self.txn_info_segments.insert(ts, Either::Right(lock));

        match previous {
            Some(either) => match either {
                Either::Left(encoded) => {
                    // Previously stored as encoded lock info; decode it back to a Lock.
                    Some(Lock::parse(&encoded).expect("failed to parse shared lock txn info"))
                }
                Either::Right(lock) => Some(lock),
            },
            None => None,
        }
    }

    #[inline]
    pub fn put_shared_lock(&mut self, lock: Lock) -> Result<()> {
        let lock_type = lock.lock_type;
        match lock_type {
            LockType::Lock | LockType::Pessimistic => {}
            _ => unreachable!(),
        }
        if self.is_shrink_only() {
            return Err(Error::from(ErrorInner::SharedLocksAreShrinkOnly));
        }
        let ts = lock.ts;
        let old = self.put_lock(ts, lock);
        if lock_type == LockType::Lock {
            debug_assert!(
                old.is_some(),
                "shared lock should be prewritten over pessimistic lock"
            );
        }
        Ok(())
    }

    pub fn to_bytes(&self) -> Vec<u8> {
        let mut b = Vec::with_capacity(self.pre_allocate_size());
        b.push(LockType::Shared.to_u8());
        if !self.flags.is_empty() {
            b.push(SHARED_LOCK_FLAGS_PREFIX);
            b.push(self.flags.bits());

Review comment (suggestion):
FYI, I renamed this function to `put_lock` in another pending PR.

Code after:
                    either.as_ref().right()
                }
                Either::Right(lock) => Some(lock),
            }
        } else {
            None
        }
    }

    pub fn insert_lock(&mut self, lock: Lock) -> Result<()> {
        let lock_type = lock.lock_type;
        assert!(matches!(lock_type, LockType::Lock | LockType::Pessimistic));
        if self.is_shrink_only() {
            return Err(ErrorInner::InvalidOperation(format!(
                "cannot insert new lock #{} into shrink-only SharedLocks",
                lock.ts.into_inner()
            ))
            .into());
        } else if self.contains_start_ts(lock.ts) {
            return Err(ErrorInner::InvalidOperation(format!(
                "lock #{} already exists in SharedLocks",
                lock.ts.into_inner()
            ))
            .into());
        }
        self.txn_info_segments.insert(lock.ts, Either::Right(lock));
        Ok(())
    }

    pub fn update_lock(&mut self, lock: Lock) -> Result<()> {
        let lock_type = lock.lock_type;
        assert!(matches!(lock_type, LockType::Lock | LockType::Pessimistic));
        if !self.contains_start_ts(lock.ts) {
            return Err(ErrorInner::InvalidOperation(format!(
                "lock #{} does not exist in SharedLocks",
                lock.ts.into_inner()
            ))
            .into());
        }
        self.txn_info_segments.insert(lock.ts, Either::Right(lock));
        Ok(())
    }

    pub fn to_bytes(&self) -> Vec<u8> {
        let mut b = Vec::with_capacity(self.pre_allocate_size());
        b.push(LockType::Shared.to_u8());
        if !self.flags.is_empty() {
            b.push(SHARED_LOCK_FLAGS_PREFIX);
            b.push(self.flags.bits());
        }
        b.push(SHARED_LOCK_TXNS_INFO_PREFIX);