Repository: meilisearch/meilisearch
File: milli/src/update/index_documents/mod.rs (Rust)

Code before:
                }
            }

            Ok(())
        }).map_err(InternalError::from)??;

        // We write the field distribution into the main database
        self.index.put_field_distribution(self.wtxn, &field_distribution)?;

        // We write the primary key field id into the main database
        self.index.put_primary_key(self.wtxn, &primary_key)?;
        let number_of_documents = self.index.number_of_documents(self.wtxn)?;
        let mut rng = rand::rngs::StdRng::seed_from_u64(42);

        for (embedder_name, dimension) in dimension {
            let wtxn = &mut *self.wtxn;
            let vector_arroy = self.index.vector_arroy;

            let embedder_index = self.index.embedder_category_id.get(wtxn, &embedder_name)?.ok_or(
                InternalError::DatabaseMissingEntry { db_name: "embedder_category_id", key: None },
            )?;

            pool.install(|| {
                for k in crate::vector::arroy_db_range_for_embedder(embedder_index) {
                    let writer = arroy::Writer::new(vector_arroy, k, dimension);
                    if !writer.need_build(wtxn)? && writer.is_empty(wtxn)? {
                        break;
                    }
                    writer.build(wtxn, &mut rng, None)?;
                }
                Result::Ok(())
            })
            .map_err(InternalError::from)??;
        }

        self.execute_prefix_databases(
            word_docids.map(MergerBuilder::build),
            exact_word_docids.map(MergerBuilder::build),
            word_position_docids.map(MergerBuilder::build),
            word_fid_docids.map(MergerBuilder::build),
        )?;

        Ok(number_of_documents)
    }

    #[tracing::instrument(
        level = "trace",
        skip_all,
        target = "indexing::prefix",
        name = "index_documents_prefix_databases"
    )]

Review comment (suggestion):
hello, do we still need the `writer.is_empty` part? I would expect `need_build` to suffice?

it sounds strange to me to have `no writer doesn't need build` and then do it anyway...

Code after:
                }
            }

            Ok(())
        }).map_err(InternalError::from)??;

        // We write the field distribution into the main database
        self.index.put_field_distribution(self.wtxn, &field_distribution)?;

        // We write the primary key field id into the main database
        self.index.put_primary_key(self.wtxn, &primary_key)?;
        let number_of_documents = self.index.number_of_documents(self.wtxn)?;
        let mut rng = rand::rngs::StdRng::seed_from_u64(42);

        for (embedder_name, dimension) in dimension {
            let wtxn = &mut *self.wtxn;
            let vector_arroy = self.index.vector_arroy;

            let embedder_index = self.index.embedder_category_id.get(wtxn, &embedder_name)?.ok_or(
                InternalError::DatabaseMissingEntry { db_name: "embedder_category_id", key: None },
            )?;

            pool.install(|| {
                for k in crate::vector::arroy_db_range_for_embedder(embedder_index) {
                    let writer = arroy::Writer::new(vector_arroy, k, dimension);
                    if writer.need_build(wtxn)? {
                        writer.build(wtxn, &mut rng, None)?;
                    } else if writer.is_empty(wtxn)? {
                        break;
                    }
                }
                Result::Ok(())
            })
            .map_err(InternalError::from)??;
        }

        self.execute_prefix_databases(
            word_docids.map(MergerBuilder::build),
            exact_word_docids.map(MergerBuilder::build),
            word_position_docids.map(MergerBuilder::build),
            word_fid_docids.map(MergerBuilder::build),
        )?;

        Ok(number_of_documents)
    }

    #[tracing::instrument(
        level = "trace",
        skip_all,
        target = "indexing::prefix",
        name = "index_documents_prefix_databases"