Repository: TabbyML/tabby
File: crates/tabby-index/src/indexer.rs (Rust)

Code before:
                let source_id = source_id.clone();

                yield tokio::spawn(async move {
                    let built_chunk_attributes_result = task.await?;
                    let (tokens, chunk_attributes) = built_chunk_attributes_result?;

                    let mut doc = doc! {
                        schema.field_id => id,
                        schema.field_source_id => source_id,
                        schema.field_corpus => kind,
                        schema.field_updated_at => updated_at,
                        schema.field_chunk_id => format!("{}-{}", id, chunk_id),
                        schema.field_chunk_attributes => chunk_attributes,
                    };

                    for token in &tokens {
                        doc.add_text(schema.field_chunk_tokens, token);
                    }

                    Ok(doc)
                });
            }
        }
    }

    pub async fn backfill_attribute(&self, origin: &TantivyDocument, doc: &T) -> TantivyDocument {
        let schema = IndexSchema::instance();
        let mut doc = doc! {
            schema.field_id => get_text(origin, schema.field_id),
            schema.field_source_id => get_text(origin, schema.field_source_id).to_string(),
            schema.field_corpus => get_text(origin, schema.field_corpus).to_string(),
            schema.field_attributes => self.builder.build_attributes(doc).await,
            schema.field_updated_at => get_date(origin, schema.field_updated_at),
        };
        if let Some(failed_chunks) = get_number_optional(origin, schema.field_failed_chunks_count) {
            doc.add_u64(schema.field_failed_chunks_count, failed_chunks as u64);
        }

        doc
    }
}

pub struct Indexer {
    corpus: String,
    searcher: Searcher,
    writer: IndexWriter,
}

impl Indexer {
    pub fn new(corpus: &str) -> Self {
        let doc = IndexSchema::instance();

Review comment (suggestion):
```suggestion
    pub async fn backfill_doc_attributes(&self, origin: &TantivyDocument, doc: &T) -> TantivyDocument {
```

Code after:
                let source_id = source_id.clone();

                yield tokio::spawn(async move {
                    let built_chunk_attributes_result = task.await?;
                    let (tokens, chunk_attributes) = built_chunk_attributes_result?;

                    let mut doc = doc! {
                        schema.field_id => id,
                        schema.field_source_id => source_id,
                        schema.field_corpus => kind,
                        schema.field_updated_at => updated_at,
                        schema.field_chunk_id => format!("{}-{}", id, chunk_id),
                        schema.field_chunk_attributes => chunk_attributes,
                    };

                    for token in &tokens {
                        doc.add_text(schema.field_chunk_tokens, token);
                    }

                    Ok(doc)
                });
            }
        }
    }

    pub async fn backfill_doc_attributes(
        &self,
        origin: &TantivyDocument,
        doc: &T,
    ) -> TantivyDocument {
        let schema = IndexSchema::instance();
        let mut doc = doc! {
            schema.field_id => get_text(origin, schema.field_id),
            schema.field_source_id => get_text(origin, schema.field_source_id).to_string(),
            schema.field_corpus => get_text(origin, schema.field_corpus).to_string(),
            schema.field_attributes => self.builder.build_attributes(doc).await,
            schema.field_updated_at => get_date(origin, schema.field_updated_at),
        };
        if let Some(failed_chunks) = get_number_optional(origin, schema.field_failed_chunks_count) {
            doc.add_u64(schema.field_failed_chunks_count, failed_chunks as u64);
        }

        doc
    }
}

pub struct Indexer {
    corpus: String,
    searcher: Searcher,
    writer: IndexWriter,
}