Repository: meilisearch/meilisearch
File: index-scheduler/src/index_mapper.rs (Rust)

Code before:
        let uuid = self
            .index_mapping
            .get(rtxn, name)?
            .ok_or_else(|| Error::IndexNotFound(name.to_string()))?;

        // we clone here to drop the lock before entering the match
        let index = self.index_map.read().unwrap().get(&uuid).cloned();
        let index = match index {
            Some(Available(index)) => index,
            Some(BeingDeleted) => return Err(Error::IndexNotFound(name.to_string())),
            // since we're lazy, it's possible that the index has not been opened yet.
            None => {
                let mut index_map = self.index_map.write().unwrap();
                // between the read lock and the write lock it's not impossible
                // that someone already opened the index (eg if two search happens
                // at the same time), thus before opening it we check a second time
                // if it's not already there.
                // Since there is a good chance it's not already there we can use
                // the entry method.
                match index_map.entry(uuid) {
                    Entry::Vacant(entry) => {
                        let index_path = self.base_path.join(uuid.to_string());

                        let created_at = Some(time::OffsetDateTime::now_utc());
                        let updated_at = Some(time::OffsetDateTime::now_utc());
                        let index = self.create_or_open_index(&index_path, created_at, updated_at)?;
                        entry.insert(Available(index.clone()));
                        index
                    }
                    Entry::Occupied(entry) => match entry.get() {
                        Available(index) => index.clone(),
                        BeingDeleted => return Err(Error::IndexNotFound(name.to_string())),
                    },
                }
            }
        };

        Ok(index)
    }

    /// Return all indexes, may open them if they weren't already opened.
    pub fn indexes(&self, rtxn: &RoTxn) -> Result<Vec<(String, Index)>> {
        self.index_mapping
            .iter(rtxn)?
            .map(|ret| {
                ret.map_err(Error::from).and_then(|(name, _)| {
                    self.index(rtxn, name).map(|index| (name.to_string(), index))
                })
            })
            .collect()
    }

Review comment (suggestion):
```suggestion
                        let index = self.create_or_open_index(&index_path)?;
```

Here there is no reason to override the date if one was already set

Code after:
            .get(rtxn, name)?
            .ok_or_else(|| Error::IndexNotFound(name.to_string()))?;

        // we clone here to drop the lock before entering the match
        let index = self.index_map.read().unwrap().get(&uuid).cloned();
        let index = match index {
            Some(Available(index)) => index,
            Some(BeingDeleted) => return Err(Error::IndexNotFound(name.to_string())),
            // since we're lazy, it's possible that the index has not been opened yet.
            None => {
                let mut index_map = self.index_map.write().unwrap();
                // between the read lock and the write lock it's not impossible
                // that someone already opened the index (eg if two search happens
                // at the same time), thus before opening it we check a second time
                // if it's not already there.
                // Since there is a good chance it's not already there we can use
                // the entry method.
                match index_map.entry(uuid) {
                    Entry::Vacant(entry) => {
                        let index_path = self.base_path.join(uuid.to_string());

                        let index = self.create_or_open_index(&index_path, None)?;
                        entry.insert(Available(index.clone()));
                        index
                    }
                    Entry::Occupied(entry) => match entry.get() {
                        Available(index) => index.clone(),
                        BeingDeleted => return Err(Error::IndexNotFound(name.to_string())),
                    },
                }
            }
        };

        Ok(index)
    }

    /// Return all indexes, may open them if they weren't already opened.
    pub fn indexes(&self, rtxn: &RoTxn) -> Result<Vec<(String, Index)>> {
        self.index_mapping
            .iter(rtxn)?
            .map(|ret| {
                ret.map_err(Error::from).and_then(|(name, _)| {
                    self.index(rtxn, name).map(|index| (name.to_string(), index))
                })
            })
            .collect()
    }

    /// Swap two index names.
    pub fn swap(&self, wtxn: &mut RwTxn, lhs: &str, rhs: &str) -> Result<()> {
        let lhs_uuid = self