in reply to Message Board Mangling

Why bother with re-writing a file every time? Look at, say, BerkeleyDB, which maintains an efficient key/value pair database.

What you're talking about is being able to re-order existing records. The best strategy for this might be to use a single BerkeleyDB database, and re-define the keys from time to time to re-define the order of the posts. So in the value of each record, you maintain the time of the post (so you can get latest-first), and the ID or quality ranking of the poster, as well as the messsage. You generate your keys to the most significant few (say, 4) bytes supply a serial number that gives the number of re-order passes. This way, as you re-order records, they go to the end of the database, and you can then delete them from the beginning.

Each time you re-order, you do this:

increment the re-order sequence number For each record that was not re-ordered this time through, read the record re-compute the key based on the age and poster ID write the new key/value pair (with the new re-order number) delete the old key/value pair

The beauty of this is that BerkeleyDB will efficiently reclaim the file space with a minimum of work. When you have new messages, you just give them a key that will allow you to find them before the previous messages (cursors in BerkeleyDB can go forwards (DB_NEXT) or backwards (DB_PREV)).

Replies are listed 'Best First'.
Re: Re: Message Board Mangling
by achiles (Novice) on Jul 12, 2001 at 15:20 UTC
    I would, except the format isn't up to me (Unless I do some pretty major C hacking, or more code begging :p)
      What if the "OK Posters" is just anyone who puts "savepost" in their nick somewhere? Will
      my @ok_posters = qw ( \savepost\ );
      my $ok_regex = join "|", @ok_posters;
      my $ok_regex = qr/(?:$ok_regex)/;
      
      grab a nick of "John Luser savepost"?
        I think it would have save you alot of time if you'd set the store method up alot better.
        Maybe this way could have helped??

        SAVE THE POST
        --------------------------------------------------------
        Bring in data from FORM as usual
        Set usertype from form to be either "admin" or "guest" maybe as a type=hidden depending on whatever login rights you store.

        $message = join('|', $usertype, $username, $date, $comments); $message .= "\n"; open ("post", "/absolute/path/to/file") or print error; print post $message; close (post);
        READ THE POST(s)
        -------------------------------------------------------
        Choose to read admin only on this instance...
        $readtype = 'admin';

        open ("post", "/absolute/path/to/file") or print error; @post = <post>; close (post); foreach (@post) { ($usertype, $username, $date, $comments) = split ('[|]',$_); if ($readtype eq $usertype) { print qq| From $username ($date):<p> $comments<p> ______________________________________<br> |; } }
        This way you can pick the name off better in any future admin than having to search for it on a line.

        Maybe no help at all, but there you go.
        If its not, im sorry.
        It was good practice for my typing skills anyway :)

        ThAtH0M