I'm working on a small search engine in a Perl environment where I can't use databases, it has to be flat files.

The search engine structure I've come up with involves an index with a word,documentnumber,documentnumber structure on every line, like so:

alpha,0,1,2
bravo,2,3,4
charlie,3,4,5
(etc)

where the word "alpha" is in documents 0,1 and 2, the word "bravo" is in documents 2,3 and 4 and so on.

So the structure as it stands requires me to open a 500kb file with hundreds of words/lines in it, and process every line to see if it starts with the search term(s).

So it occured to me yesterday that it might actually be easier to have a structure where I actually had hundreds of files, where the keyword was the filename and the content was the list of documents, so rather than (pseudocode):

open(large file) for each line of the file { put it into an array see if item 0 of the array matches the search term grab the document list if it does }
I could just do something like:
if(a file exists called "/www/search/$searchterm"){ open it and grab the document list }
so am I crazy or what?

I guess the factors are:

It's obviously a very messy solution, in that I'd have a folder stuffed with a large number of very small files, but in terms of doing less file-reading, less I/O, are there any gains?


In reply to Poor Person's Database by Cody Pendant

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.