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

Code before:
    /// Create or open an index in the specified path.
    /// The path *must* exists or an error will be thrown.
    fn create_or_open_index(&self, path: &Path) -> Result<Index> {
        let mut options = EnvOpenOptions::new();
        options.map_size(self.index_size);
        options.max_readers(1024);
        Ok(Index::new(options, path)?)
    }

    /// Get or create the index.
    pub fn create_index(&self, mut wtxn: RwTxn, name: &str) -> Result<Index> {
        match self.index(&mut wtxn, name) {
            Ok(index) => {
                wtxn.commit()?;
                Ok(index)
            }
            Err(Error::IndexNotFound(_)) => {
                let uuid = Uuid::new_v4();
                self.index_mapping.put(&mut wtxn, name, &uuid)?;

                let index_path = self.base_path.join(uuid.to_string());
                fs::create_dir_all(&index_path)?;
                let index = self.create_or_open_index(&index_path)?;

                wtxn.commit()?;
                // TODO: it would be better to lazyly create the index. But we need an Index::open function for milli.
                if let Some(BeingDeleted) =
                    self.index_map.write().unwrap().insert(uuid, Available(index.clone()))
                {
                    panic!("Uuid v4 conflict.");
                }

                Ok(index)
            }
            error => error,
        }
    }

    /// Removes the index from the mapping table and the in-memory index map
    /// but keeps the associated tasks.
    pub fn delete_index(&self, mut wtxn: RwTxn, name: &str) -> Result<()> {
        let uuid = self
            .index_mapping
            .get(&wtxn, name)?
            .ok_or_else(|| Error::IndexNotFound(name.to_string()))?;

        // Once we retrieved the UUID of the index we remove it from the mapping table.
        assert!(self.index_mapping.delete(&mut wtxn, name)?);

        wtxn.commit()?;
        // We remove the index from the in-memory index map.

Review comment (suggestion):
```suggestion
                // TODO: it would be better to lazily create the index. But we need an Index::open function for milli.
```

Code after:
    /// Create or open an index in the specified path.
    /// The path *must* exists or an error will be thrown.
    fn create_or_open_index(&self, path: &Path) -> Result<Index> {
        let mut options = EnvOpenOptions::new();
        options.map_size(self.index_size);
        options.max_readers(1024);
        Ok(Index::new(options, path)?)
    }

    /// Get or create the index.
    pub fn create_index(&self, mut wtxn: RwTxn, name: &str) -> Result<Index> {
        match self.index(&mut wtxn, name) {
            Ok(index) => {
                wtxn.commit()?;
                Ok(index)
            }
            Err(Error::IndexNotFound(_)) => {
                let uuid = Uuid::new_v4();
                self.index_mapping.put(&mut wtxn, name, &uuid)?;

                let index_path = self.base_path.join(uuid.to_string());
                fs::create_dir_all(&index_path)?;
                let index = self.create_or_open_index(&index_path)?;

                wtxn.commit()?;
                // TODO: it would be better to lazily create the index. But we need an Index::open function for milli.
                if let Some(BeingDeleted) =
                    self.index_map.write().unwrap().insert(uuid, Available(index.clone()))
                {
                    panic!("Uuid v4 conflict.");
                }

                Ok(index)
            }
            error => error,
        }
    }

    /// Removes the index from the mapping table and the in-memory index map
    /// but keeps the associated tasks.
    pub fn delete_index(&self, mut wtxn: RwTxn, name: &str) -> Result<()> {
        let uuid = self
            .index_mapping
            .get(&wtxn, name)?
            .ok_or_else(|| Error::IndexNotFound(name.to_string()))?;

        // Once we retrieved the UUID of the index we remove it from the mapping table.
        assert!(self.index_mapping.delete(&mut wtxn, name)?);

        wtxn.commit()?;
        // We remove the index from the in-memory index map.