A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: editing a flatfile database

  1. #1
    Senior Member
    Join Date
    Sep 2000
    Posts
    183
    hi
    my flatfile db for my search engine is something like this:

    &name1=Flashkit&link1=http://www.flashkit.com&descr1=great site flash

    where the number in front of the' = ' increases with each site added.
    I would like to add a rating system where each site would have a number out of five.
    what i would like to know is,is it possible to edit the rating of a site (once the person has voted) in database with a perl script?
    ie is it possible to go to a specific line in the database and edit it.

    hope yuo can help

    Ash

  2. #2
    Registered User
    Join Date
    Feb 2001
    Posts
    13,041
    Hi,

    no, you cannot edit file...
    All these scripts will lock the file, read the entire file, and then write a modified file back.
    This puts some sort of limit on the number of entries the script could handle, but you will probably not reach it, anyway. If somehow the server crashes during the process, your file may be lost

    sample code - very simplified - without error checking:
    open F, $filename;
    @data = <F>;
    close F;
    foreach $line (@data)
    { $line =~s/vote$number=\d+/vote$number=$newvote/ if($line =~ /descr$number=/);
    }
    open F, ">$filename";
    print F, @data;
    close F;

    Musicman

  3. #3
    Senior Member
    Join Date
    Sep 2000
    Posts
    183
    Hi
    so it can't be done at all, because the server will crash as it will have too much demand?

    what if i use this code i found in my perl book:
    Code:
    flock(LOG, 2)
    print "balbabnl"
    flock(LOG, 8)
    it says that this will get exclusive access to the file.
    will this help, or will it just make the txt file very slow to access.

    what i was thinking was when the user rates a site information about the site name and adress were sent . Then when this info was matched in a certain line, i could change that line accordingly.
    But i wasn't sure how to alter the line and put it back in its place. Because i don't want the lines all over the place. (eg record 2 is on the 14h line).

    Any ideas

    Ash

  4. #4
    Registered User
    Join Date
    Feb 2001
    Posts
    13,041
    Hi,

    the code I have shown takes care of handling the entire file - it will locate a line identified by a specific number and replace the vote value in the line...
    The flock stuff is there to make sure two people cannot update the file at the same time, so you will need both.
    If the server is known to crash often, you could make some script that mails you a daily backup...

    Musicman

  5. #5
    Senior Member
    Join Date
    Sep 2000
    Posts
    183
    hi
    so i should be able to get it to work quite reliabally on a paid server?
    in your script what does this part do, i can't understand it:

    $line =~s/vote$number=\d+/vote$number=$newvote/ if($line =~ /descr$number=/

    If you could just explain what each bit did it in english not perl, i would be very grateful.

    Cheers
    ash

  6. #6
    Registered User
    Join Date
    Feb 2001
    Posts
    13,041
    Hi Ash,

    if the line matches a pattern like descr23= (put the equals sign there so the code would not change item 233 as well)
    replace the part of the line that reads vote23=(some digits) by vote23=(new value) - this assumes that the flash movie sends the item number and the updated vote value
    If you wanted to just add 1 to the vote value, the first half of the line would be
    $line =~s/vote$number=(\d+)/"vote$number=" . ($1+1)/e
    Here the /e bit tells perl that the replacement is an expression (consisting of a "string" as well as a number calculated by adding 1 to the number found in the original line
    Perl has two ways to write a simple if statement:
    if(something) { statement; } ## { } required here
    statement if(something);

    Musicman

  7. #7
    Registered User
    Join Date
    Feb 2001
    Posts
    13,041
    Hi Ash,

    if the line matches abpattern like descr23= (put the equals sign there so the code would not change item 233 as well)
    replace the part of the line that reads vote23=(some digits) by vote23=(new value) - this assumes that the flash movie sends the item number and the updated vote value
    If you wanted to just add 1 to the vote value, the first half of the line would be
    $line =~s/vote$number=(\d+)/"vote$number=" . ($1+1)/e
    Here the /e bit tells perl that the replacement is an expression (consisting of a "string" as well as a number calculated by adding 1 to the number found in the original line
    Perl has two ways to write a simple if statement:
    if(something) { statement; } ## { } required here
    statement if(something);

    Musicman

  8. #8
    Senior Member
    Join Date
    Sep 2000
    Posts
    183
    hi
    will this work...

    Code:
    #!/usr/bin/perl
    
    use CGI;
    
    $query = new CGI;
    $rating = $query->param('rating');
    
    open DB, $filename; 
    flock(DB, 2);
    @data = <F>; 
    flock(DB, 8);
    close DB; 
    
    $number=1;
    foreach $line (@data) { 
    $line =~s/number_of_vote$number=\d+/"number_of_vote$number=" . ($1+1)/ if($line =~ /descr$number=/);
    $line =~s/total$number=\d+/"total$number=" . ($1+$rating)/ if($line =~ /descr$number=/);
    $number++;
    } 
    
    open DB, ">$filename"; 
    flock(DB, 2);
    print DB, @data; 
    flock(DB, 8);
    close DB; 
    
    print "Content-type:text/html\n\n";
    print "status=ok";
    how would i do the working out of the rating (ie. number_of_votes / total). would this be easier done in flash?

    what is the best way to test it quickly..

    hope you can help

    Ash

  9. #9
    Registered User
    Join Date
    Feb 2001
    Posts
    13,041
    Hi Ash,

    please do not change $number during the for loop ... it is meant to be the number of the item the user is voting on, so it is the other value passed in from the flash interface.

    There is another thing I do not like with your code - you are locking the file for reading, unlock it, lock it again for writing. So it is possible one is reading the file, next one is reading the file, first one writes back with a vote added for one item, second write back original file with a vote for another item.
    I checked the book but there is no function to lock a file that is not open. But if this is meant to work on a unix server, try this code for locking
    Code:
    $cnt = 0;
    while(!link $filename, "$filename.lock" && $cnt < 20) {sleep 1; $cnt++}
    ## if file locked for 20 seconds, it must be very heavily used or something is broken
    if ($cnt < 20)
    {	open DB, $filename;
    	@data = <DB>;
    	close DB;
    	## process file now, slightly optimized code
    	foreach $line (@data)
    	{	if($line =~/descr$number=/)
    		{	$line =~s/number_of_vote$number=\d+/"number_of_vote$number=" . ($1+1)/;
    			$line =~s/total$number=\d+/"total$number=" . ($1+$rating)/;
    			last;
    		}
    	}
    	open DB, ">$filename";
    	print DB, @data;
    	close DB;
    	$status = 'ok';
    }
    else
    {	$status = 'lock error';
    }
    # if some process was stopped with the lock set, this will eventually free the lock.
    unlink "$filename.lock";
    print "Content-type: text/html\n\n";
    print "status=$status";
    Now this code is not exactly safe - and you should not use it if you expect 20 people voting every second.
    The other way to handle the situation would be to lock the file after opening for read and releasing the lock before the final close - but since the manual does not tell whether the lock will be maintained, this is not exactly safe either.

    Musicman

  10. #10
    Senior Member
    Join Date
    Sep 2000
    Posts
    183
    hi
    thanks for the effort with the code. greatly appreciated!
    just one thing
    what do i need to send when accessing the code. I know i need to send the rating but do i need to send the description (descr) as well?
    IS there anything i should send to the script?

    also what does this statement mean
    if($line =~/descr$number=/)

    Cheers
    Ash

  11. #11
    Registered User
    Join Date
    Feb 2001
    Posts
    13,041
    Hi,

    you should send the item number, so if you want to rate
    descr15=www.macromedia.com, you would send number=15
    I am assuming you have already sent a list of items to the movie, so the movie would show the descrition and save the number

    Musicman

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center