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

Code before:
            indexer_config: Arc::new(indexer_config),
        })
    }

    /// 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, created: Option<time::OffsetDateTime>, updated: Option<time::OffsetDateTime>) -> Result<Index> {
        let mut options = EnvOpenOptions::new();
        options.map_size(clamp_to_page_size(self.index_size));
        options.max_readers(1024);

        let created_at;
        if created == None {
            created_at = time::OffsetDateTime::now_utc();
        } else {
            created_at = created.unwrap(); 
        }

        let updated_at;
        if updated == None {
            updated_at = time::OffsetDateTime::now_utc();
        } else {
            updated_at = updated.unwrap();
        }

        Ok(Index::new_with_creation_dates(options, path, created_at, updated_at)?)
    }

    /// Get or create the index.
    pub fn create_index(&self, mut wtxn: RwTxn, name: &str, created_at: Option<time::OffsetDateTime>, updated_at: Option<time::OffsetDateTime>) -> Result<Index> {
        match self.index(&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 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)?;

                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()))
                {

Review comment (suggestion):
Here if the dates are not available, we should simply call `Index::new` and let milli handle the generation of the dates.

Code after:
            index_size,
            indexer_config: Arc::new(indexer_config),
        })
    }

    /// 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,
        date: Option<(OffsetDateTime, OffsetDateTime)>,
    ) -> Result<Index> {
        let mut options = EnvOpenOptions::new();
        options.map_size(clamp_to_page_size(self.index_size));
        options.max_readers(1024);

        if let Some((created, updated)) = date {
            Ok(Index::new_with_creation_dates(options, path, created, updated)?)
        } else {
            Ok(Index::new(options, path)?)
        }
    }

    /// Get or create the index.
    pub fn create_index(
        &self,
        mut wtxn: RwTxn,
        name: &str,
        date: Option<(OffsetDateTime, OffsetDateTime)>,
    ) -> Result<Index> {
        match self.index(&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, date)?;

                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.");
                }