Gossamer Forum
Home : Products : DBMan : Customization :

Sorting the database file

Quote Reply
Sorting the database file
Is there a mod ( I have not found one yet ) that actually sorts the database? I want to make sure the db file stays sorted whenever it is modified/updated. My db fields start with YEAR, MONTH, DAY and TIME. I need the actual database file to stay sorted because I have other applications that read the db files and I don't want to have to add extra logic the the other apps to sort on-the-fly. It's too inefficient. If I keep the db sorted it will only have to sort once when the db is changed.

What I did in the past was add two extra line to the beginning of my external apps:

# Use event.db.sorted instead of event.db
# Sorting the events database
$result = `sort event.db > event.db.sorted`;

This was quick and dirty but it works. However, this happens every time someone views the events page. There's no reason to sort the database everytime someone views the events page when all that needs to happen is to sort the database once when it changes.
Quote Reply
Re: [acravens] Sorting the database file In reply to
I just realized that some databases will need to be sorted ascending and others will be sorted descending. So, I'll need to put something in each database's cfg file to let db.cgi know it needs to be sorted and whether it's ascending or descending.

I was thinking about doing the following:

1) Near the top of db.cgi, initialize a variable called $sort_order and make it a null value.

2) In each cfg file that requires the database to be sorted, set $sort_order to "ascending" or "descending" If the database does not need to be sorted then no mods are needed to the cfg file. The $sort_order variable was still initialized to null in db.cgi.

3) Create a new subroutine in db.cgi called sort_db_file

sub sort_db_file {
# If $sort_order is null nothing will be done
# If $sort_order is "ascending" then sort ascending
# Call OS to do sorting
if ($sort_order eq "ascending") {
`sort $db_file_name > $db_file_name.sorted`;
`mv $db_file_name.sorted $db_file_name`;
}
# If $sort_order is "descending" then sort descending
if ($sort_order eq = "descending") {
`sort -r #db_file_name > $db_file_name.sorted`;
'mv $db_file_name.sorted $db_file_name';
}
}

The sort_db_file routine would be placed in add_record and modify_record just before the &html_add_success and &html_modify_success lines.

Can I set the variable $sort_order in db.cfg? As you can see, I'm calling the OS's sort command to sort, bypassing any file locking. Is this OK?
Quote Reply
Re: [acravens] Sorting the database file In reply to
Looks like dbman already uses $sort_order

I'll have to use a different name
Quote Reply
Re: [acravens] Sorting the database file In reply to
One of the basic tenets of database creation and management is that the order in which the records are stored in the database is unimportant. I know it seems inefficient, but it's an absolute backbone of databases.

You could write a separate file, I guess, whenever a record is added or modified and you would put the code for that in the appropriate subroutines in the db.cgi file.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.