Repository: meilisearch/meilisearch
File: dump/src/reader/v4/mod.rs (Rust)

Code before:
        let meta = File::open(path.join("meta.json"))?;
        let meta: DumpMeta = serde_json::from_reader(meta)?;

        let mut created_at = None;
        let mut updated_at = None;

        for line in tasks.lines() {
            let task: Task = serde_json::from_str(&line?)?;

            if task.index_uid.to_string() == name {
                if updated_at.is_none() {
                    updated_at = match task.events.last() {
                        Some(TaskEvent::Created(ts)) => Some(*ts),
                        _ => None,
                    };
                }

                if created_at.is_none() {
                    created_at = match task.content {
                        TaskContent::IndexCreation { primary_key } => match task.events.first() {
                            Some(TaskEvent::Created(ts)) => Some(*ts),
                            _ => None,
                        },
                        _ => None,
                    };
                }

                if created_at.is_some() {
                    break;
                }
            }
        }

        let metadata = IndexMetadata {
            uid: name,
            primary_key: meta.primary_key,
            created_at: created_at.unwrap_or(OffsetDateTime::now_utc()),
            updated_at: updated_at.unwrap_or(OffsetDateTime::now_utc()),
        };

        let ret = V4IndexReader {
            metadata,
            settings: meta.settings.check(),
            documents: BufReader::new(File::open(path.join("documents.jsonl"))?),
        };

        Ok(ret)
    }

    pub fn metadata(&self) -> &IndexMetadata {
        &self.metadata

Review comment (bug):
hey @funilrys sorry, when I wrote my first review I totally forgot something about meilisearch.
And index can be created by multiple events:
- An indexCreation
- A documentAddition
- A settingsAddition

Thus this simple if actually doesn't work and finding the creation date is harder than I thought.
BUT, when we wrote this dump format back in the past we already thought about it and created a special struct to retrieve the `creation_task_id`.
Here is a print of the `index_uuids/data.jsonl` file:
```json
{
  "uid": "dnd_spells",
  "index_meta": {
    "uuid": "684b4662-e3c3-4440-8d57-07ee42b0b901",
    "creation_task_id": 6
  }
```

And in the code of the dumpV4 I'm already deserializing this structure:
```rust
#[derive(Deserialize, Debug)]
#[cfg_attr(test, derive(serde::Serialize))]
pub struct IndexUuid {
    pub uid: String,
    pub index_meta: IndexMeta,
}

#[derive(Deserialize, Debug)]
#[cfg_attr(test, derive(serde::Serialize))]
pub struct IndexMeta {
    pub uuid: Uuid,
    pub creation_task_id: usize,
}
```


Thus the idea for
1. Stopping the loop
2. Setting the creation date
Is to simply stop when we reach the `creation_task_id` of the index match the `task_id` of the task.

```suggestion
                if task.id == self.creation_task_id { // I have no idea of what fields are accessible sorry
                    created_at = task.processed_at(); // we'll probably need to write a function that extract the date whatever is the variant
                    break; // we don't need to check anything else and can stop right away
                }
```

Code after:
    pub fn new(
        name: String,
        path: &Path,
        index_metadata: &IndexMeta,
        tasks: BufReader<File>,
    ) -> Result<Self> {
        let meta = File::open(path.join("meta.json"))?;
        let meta: DumpMeta = serde_json::from_reader(meta)?;

        let mut created_at = None;
        let mut updated_at = None;

        for line in tasks.lines() {
            let task: Task = serde_json::from_str(&line?)?;

            if task.index_uid.to_string() == name {
                if updated_at.is_none() {
                    updated_at = task.updated_at()
                }

                if created_at.is_none() {
                    created_at = task.created_at()
                }

                if task.id as usize == index_metadata.creation_task_id {
                    created_at = task.processed_at();

                    break;
                }
            }
        }

        let metadata = IndexMetadata {
            uid: name,
            primary_key: meta.primary_key,
            created_at: created_at.unwrap_or_else(OffsetDateTime::now_utc),
            updated_at: updated_at.unwrap_or_else(OffsetDateTime::now_utc),
        };

        let ret = V4IndexReader {
            metadata,
            settings: meta.settings.check(),
            documents: BufReader::new(File::open(path.join("documents.jsonl"))?),
        };

        Ok(ret)
    }

    pub fn metadata(&self) -> &IndexMetadata {
        &self.metadata
    }