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

Code before:
                        &index,
                        self.index_mapper.indexer_config(),
                    );
                    builder.set_primary_key(primary_key);
                    builder.execute(|_| ())?;
                    index_wtxn.commit()?;
                }

                task.status = Status::Succeeded;
                task.details = Some(Details::IndexInfo { primary_key });

                Ok(vec![task])
            }
            Batch::IndexDeletion {
                index_uid,
                mut tasks,
            } => {
                let wtxn = self.env.write_txn()?;
                // The write transaction is directly owned and commited inside.
                self.index_mapper.delete_index(wtxn, &index_uid)?;

                // We set all the tasks details to the default value.
                for task in &mut tasks {
                    task.status = Status::Succeeded;
                    // TODO should we put a details = None, here?
                    // TODO we are putting Details::IndexInfo with a primary_key = None, this is not cool bro'
                    task.details = task.kind.default_details();
                }

                Ok(tasks)
            }
        }
    }

    fn apply_index_operation<'txn, 'i>(
        &self,
        index_wtxn: &'txn mut RwTxn<'i, '_>,
        index: &'i milli::Index,
        operation: IndexOperation,
    ) -> Result<Vec<Task>> {
        match operation {
            IndexOperation::DocumentClear { mut tasks, .. } => {
                let result = milli::update::ClearDocuments::new(index_wtxn, index).execute();
                for task in &mut tasks {
                    match result {
                        Ok(deleted_documents) => {
                            task.status = Status::Succeeded;
                            task.details = Some(Details::ClearAll {
                                deleted_documents: Some(deleted_documents),
                            });
                        }

Review comment (performance):
bro' we shouldn't be able to batch an IndexCreation / IndexUpdate with an IndexDeletion bro'
Thus bro', the `IndexInfo` should never be used bro'

But to be a real bro' you should get the number of documents in the index (and thus open it?), match the `kind` of the task and insert the Details only in the case of the IndexDeletion, for everything else the `default_details` is good

Code after:
                    let mut builder = milli::update::Settings::new(
                        &mut index_wtxn,
                        &index,
                        self.index_mapper.indexer_config(),
                    );
                    builder.set_primary_key(primary_key);
                    builder.execute(|_| ())?;
                    index_wtxn.commit()?;
                }

                task.status = Status::Succeeded;
                task.details = Some(Details::IndexInfo { primary_key });

                Ok(vec![task])
            }
            Batch::IndexDeletion {
                index_uid,
                mut tasks,
            } => {
                let wtxn = self.env.write_txn()?;

                let number_of_documents = {
                    let index = self.index_mapper.index(&wtxn, &index_uid)?;
                    let index_rtxn = index.read_txn()?;
                    index.number_of_documents(&index_rtxn)?
                };

                // The write transaction is directly owned and commited inside.
                self.index_mapper.delete_index(wtxn, &index_uid)?;

                // We set all the tasks details to the default value.
                for task in &mut tasks {
                    task.status = Status::Succeeded;
                    task.details = match &task.kind {
                        KindWithContent::IndexDeletion { .. } => Some(Details::ClearAll {
                            deleted_documents: Some(number_of_documents),
                        }),
                        otherwise => otherwise.default_details(),
                    };
                }

                Ok(tasks)
            }
        }
    }

    fn apply_index_operation<'txn, 'i>(
        &self,
        index_wtxn: &'txn mut RwTxn<'i, '_>,
        index: &'i milli::Index,
        operation: IndexOperation,