As far as I know, there is no way to do better than O (n) for a generic glob search.
However, for special cases of prefix and suffix search, you can sort indexes yourself to perform binary searches, as a result of which O (log n) is used to search for the prefix and suffix. The prefix index will be sorted based on the first character, then the second, etc. The suffix index will be sorted based on the last character, then the second, etc. (Reverse lines).
I would do as you suggest in your post, and do a two-step search, find the prefix or suffix indices, and then do a brute-force search using the abbreviated list provided from the first phase using a regular expression made from glob.
Since string length comparisons are faster than regular expressions, I would also pre-filter for the minimum length of a matching string or fixed-length string for example. doc.
From the sound of your original message, the indexes will also have to reference the full path of each record so that you can display it after the final results are found.
Sarah happy
source share