13.23Sorting a database. 4tH 4tH Forth compiler
โฆษณา
โฆษณา
CHAPTER 13. SPECIAL LIBRARIES 235 db.first
db.key Firstname s" Hans" db.find
This will return you the first record containing ”Hans”. In order to find the next record simply issue: db.key Firstname s" Hans" db.find
If there isn’t a second ”Hans”, the database will return an End of file error. You can query the database by using db.error: db.error EDB.EOF =
You’ll find a full list of error codes in the source. You can print the error message by using db.message
: db.error db.message
If you want to delete the current record, simply issue: db.delete
Note the record is invalidated at that moment and you will have to navigate away from it.
If you want to change the current record, simply make the changes to the buffer and issue db.update
: s" Chuck" db.buffer -> Firstname place db.update
Finally, you can switch between tables at all times. E.g. you can make changes to a
”Profession” table and return to the ”Person” table at any time. The ”Person” table will be untouched:
Profession db.use
( Here we consult or change the ”Profession” table)
Person db.use
( As if we never left it..)
Note that db.close closes the entire database system, not just the current table. However, if we want to revive it all you have to do is to db.use it. You will start at the first record though.
13.23
Sorting a database
Sometimes neither random nor sequential access is sufficient: you want to retrieve the records in order. 4tH does not support indexes, but you can sort a table. Note is is not a good idea to sort a table with thousands of records
19 that way, because it will take a considerable amount of time. Having said that, it is not very difficult.
First you have to decide how many records have to be sorted. If the default allocation is not enough, you can raise the number of entries by defining ”/sort-index” before including the library file. Second, which sorting routine do you want to use. Quicksort is slightly faster, but heapsort has better properties for a database sort. You can also use any sorting algorithm
20 if you want to, the compiler will figure it out:
19
10,000 records is the upper limit.
20
See section 12.11.
CHAPTER 13. SPECIAL LIBRARIES 236
8192 constant /sort-index include lib/heapsort.4th
include lib/dbmsort.4th
Next, you have to assign your sorting routine to db.sorting:
’ heapsort is db.sorting
Now open your database as usual:
(Person) /Person s" Persons.dbm" db.declare to Person
Person db.use
Then you have to scan your database. As long as you don’t insert, delete or modify any records or scan another table you only have to do this once: db.scan
Finally, sort it: db.key Firstname db.sort
What actually happened is that 4tH sorted all the rowids in the proper order and stored them in an array called db.sorted. You can access it by supplying the index e.g.:
0 db.sorted @
Gets you the first rowid. So in order to fill the buffer with the first record issue:
0 db.sorted @ db.goto
The subsequent record can be reached by issuing:
1 db.sorted @
And so on. But how do you know you’ve reached the last record? Easy, the total number of records is stored in db.norows. So printing a records in the proper order is as easy as: db.norows 0 do i db.sorted @ db.goto
db.buffer -> Firstname count type space db.buffer -> Lastname count type cr loop db.sortkey
is a special field which holds the currently sorted field. E.g. this is perfectly valid: db.buffer -> db.sortkey count type cr
In this example it would print the ”Firstname”. Note you can sort on another field or use another table without having to scan the table again. However, if you want to return to a previous sort after an invocation of db.scan you either have to save and restore the array yourself or repeat the entire procedure. This is always required after deleting, modifying or inserting a record.
ดาวน์โหลด
โฆษณา