1. EDB Sorting

I'm working with an EDB file and have the need to sort it by a particular column. I've looked at the documentation twice and don't find anything specifically about sorting an EDB file, just general sorting routines.

Does anybody have any thoughts on a efficient way to sort an EDB file? I've thought about retrieving the field to be sorted by along with the key field, sorting that and creating a new database (after renaming the original) in the correct order. I've also considered retrieving the entire database into an array, sorting the array and then replacing the existing database records. Neither excites me a lot and I can't help but think that someone may have already solved this issue.

And yes I could use SQLite but this is an effort to fully understand the Euphoria database system.

Thanks in advance for any thoughts or suggestions.

Tom

new topic     » topic index » view message » categorize

2. Re: EDB Sorting

tbohon said...

I'm working with an EDB file and have the need to sort it by a particular column.

Native EDB doesn't have a concept of columns. It's a key-value store. You can use custom_sort once you've loaded the data.

tbohon said...

Does anybody have any thoughts on a efficient way to sort an EDB file? I've thought about retrieving the field to be sorted by along with the key field, sorting that and creating a new database (after renaming the original) in the correct order. I've also considered retrieving the entire database into an array, sorting the array and then replacing the existing database records. Neither excites me a lot and I can't help but think that someone may have already solved this issue.

But now reading this, it sounds like you want it stored sorted? The format specifies that it's stored sorted by the key. There's really no alternative to this. I don't understand what you are trying to accomplish with the sorting.

Alternatively, you could try EuSQL, which adds some stuff to get a SQL-ish database using EDB as the underlying storage:

http://eusql.sourceforge.net/

Matt

new topic     » goto parent     » topic index » view message » categorize

3. Re: EDB Sorting

As Matt pointed out, you can use custom_sort() to do the sorting. You could also build just a sequence of keys sorted by column, if that's all you need. One drawback with this is that you'll be fetching the data values (the "columns") over and over again to do the comparison as you sort the sequence of keys. So this probably isn't the fastest method. But if you have a lot of data, this does save you from having to load all of the data into memory at once to sort the whole sequence.

include std/eds.e 
include std/filesys.e 
include std/sort.e 
 
function column_compare( object key_a, object key_b, integer column ) 
 
    sequence data_a = db_fetch_record( key_a ) 
    sequence data_b = db_fetch_record( key_b ) 
 
    return compare( data_a[column], data_b[column] ) 
end function 
 
function db_sort_keys( integer by_column, integer order = NORMAL_ORDER ) 
 
    integer count = db_table_size() 
    sequence keys = repeat( 0, count ) 
 
    for i = 1 to count do 
        keys[i] = db_record_key(i) 
    end for 
 
    return custom_sort( routine_id("column_compare"), keys, {by_column}, order ) 
end function 
 
procedure main() 
 
    db_create( "demo.edb" ) 
    db_create_table( "demo" ) 
 
    db_insert( 1, {"apple"} ) 
    db_insert( 2, {"orange"} ) 
    db_insert( 3, {"grape"} ) 
    db_insert( 4, {"banana"} ) 
    db_insert( 5, {"melon"} ) 
    db_insert( 6, {"pear"} ) 
 
    sequence keys = db_sort_keys( 1 ) 
 
    for i = 1 to length( keys ) do 
        sequence data = db_fetch_record( keys[i] ) 
        printf( 1, "%d : %s\n", {keys[i],data[1]} ) 
    end for 
 
    db_close() 
    delete_file( "demo.edb" ) 
 
end procedure 
 
main() 

1 : apple 
4 : banana 
3 : grape 
5 : melon 
2 : orange 
6 : pear 

-Greg

new topic     » goto parent     » topic index » view message » categorize

4. Re: EDB Sorting

Matt and Greg - thanks for clarifying a bunch of things in your replies. Should have realized the key-value pair concept, just a brain malfunction on my part.

What I'm trying to do is sort the data for display, i.e., by key value or first name or any other field. Greg's example shows me what to do and when I get a chance today (have a long, boring conference call to listen to) I'll start implementing it in my program.

The more I learn about OE the more I like it - to the point that it is going to be the only language on my new computer being built now after a Win10 crash-and-destroy took out my old system's hard drive beyond recovery.

Thanks guys. Have a great and very happy New Year!

Tom

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu