AlexAltea / milli-py

Python bindings for Milli, the embeddable Rust-based search engine powering Meilisearch
MIT License
123 stars 2 forks source link

Implement Drop for PyIndex #2

Closed AlexAltea closed 1 year ago

AlexAltea commented 1 year ago

We should implement the Drop trait for PyIndex:

impl Drop for PyIndex {
    fn drop(&mut self) {
        //self.index.prepare_for_closing().wait();
    }
}

This would allow us to explicitly close the database, which is needed if we want to programmatically remove the underlying directory where an Index has been created, e.g. after del index or during Index's __enter__ and __exit__.

The issue with this is that Index.prepare_for_closing() takes the ownership of the value but drop(&mut self) is taking a reference. (Not sure if this sentence makes sense, I'm not a Rust programmer; just expressing my vague intuition).

In Rust words:

[...] cannot move out of `self.index` which is behind a mutable reference
AlexAltea commented 1 year ago

Found a simple solution at: https://cs.github.com/meilisearch/meilisearch/blob/d833e62282e1b10f02962f749b90f8918b1d4b8e/meilisearch-lib/src/index/index.rs?q=prepare_for_closing

AlexAltea commented 1 year ago

Fixed at https://github.com/AlexAltea/milli-py/commit/4d46d9431fe1df5c8b0346d3b79e35f2d97d11e5.