Global HashMap creation

I'm trying to create a global HashMap that on program startup will loop through a directory finding tantivy indexes and if they open will store a writer and reader for the indexes in the map.

All requests coming in then check the map to see if the index exists and if so creates a searcher off the reader or uses the writer.

I'm struggling to find a way to do this in rust. I'm assuming I need to create a HashMap and wrap it in a Mutex and Arc so its thread safe to access it. like this.

let index_map = Arc::new(Mutex::new(HashMap::new()));

But no idea how to make it global or if this is the rust way of doing this, would creating a single thread that does this and then use channels to communicate to it be better ?

To make a global (static in this case) with some initialization lazy_static is often used.
I suppose the writer is mutating stuff, in that case you'll have to wrap your HashMap in a Mutex or RwLock.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.