morgon has asked for the wisdom of the Perl Monks concerning the following question:
assume I have a large collection of strings (let's say a million of them) each associated with a timestamp.
I now want to be able to query this collection for all strings matching a given regex, possibly constrained by upper and/or lower limits on the associated timestamp, so e.g. a query would be "find all strings matching /abc.*/", another one would be "find all strings matching /x*y/ where the associated timestamps are of last week".
Evidently I could put all the data into a database and use SQL for the queries but I wonder if there is a good algorithm to build a suitable index for such queries and do all the querying in pure perl - in such a way of course that answering a query should not take more than a few seconds.
If building an index that supports arbirary regexes is too difficult I could make do with shell-style globbing.
Any ideas?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Creating an index on a string-collection
by moritz (Cardinal) on Feb 08, 2009 at 19:01 UTC | |
But you could just pull it all into a database, don't reinvent the wheel, drink a $beverage_of_your_choice and be happy. | [reply] |
Re: Creating an index on a string-collection
by samtregar (Abbot) on Feb 08, 2009 at 21:35 UTC | |
On my system the searches are very fast once the database is loaded.
You'd need to do more work if you wanted fast "foo*bar" searches or other syntax. Also worth noting is that it uses quite a lot of memory, but hey, that's Perl for you! Thanks for the question - that was fun! -sam | [reply] [d/l] [select] |
by AnomalousMonk (Archbishop) on Feb 09, 2009 at 02:14 UTC | |
Shouldn't '*otomy' (and '*botomy', for that matter) match against something like 'phlebotomy'? What is the significance of the position of the wildcard character in the search string? | [reply] |
by samtregar (Abbot) on Feb 09, 2009 at 03:23 UTC | |
-sam | [reply] |
by AnomalousMonk (Archbishop) on Feb 09, 2009 at 08:00 UTC | |
by samtregar (Abbot) on Feb 09, 2009 at 08:07 UTC | |
Re: Creating an index on a string-collection
by perrin (Chancellor) on Feb 08, 2009 at 21:38 UTC | |
You can build an index IF you know the regexes to index in advance. You can't build an index that will work for any random regex that comes along. If you want to do it, just look up information on building an inverted word index, but use regexes instead of words. (In short, you run every regex against every record and build a hash from regexes to a list of records that match them.) You could probably even adapt some of the text search tools on CPAN if you chose to. | [reply] |
Re: Creating an index on a string-collection
by CountZero (Bishop) on Feb 08, 2009 at 22:51 UTC | |
A possible strategy could be to open the file(s) and upon reading each string immediately checking whether the string matches your query, discarding the string if not and keeping it if it does. This probably the fastest way, but will only work for one query. Each query would suffer again the cost of opening the file(s) and reading the strings. If you need to run multiple queries, you must invent a data-structure to hold all the strings and timestamps (a hash of arrays or an array of arrays spring to mind). The set-up of these will also have a one-time cost and there is of course the memory issue to look into: too much memory needed and your machine will start swapping, which will degrade the performance of your queries. Fortunately memory if cheap, so adding another GigaByte of RAM will solve a lot of problems in that respect. But you will still be re-inventing a lot of wheels and rather than playing at the "(re-)invent-a-database" game, why not dumping the data into a simple SQLite database and then running the usual SQL-queries against this database? CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James | [reply] |
Re: Creating an index on a string-collection
by BrowserUk (Patriarch) on Feb 09, 2009 at 04:13 UTC | |
Try this. It uses ~300MB to store and index 1 million (randomly generated) words (14MB on disk). It allows full regex searching (with some adaptions) and never seems to take more than 2 seconds:
A few examples:
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |