Collation algorithm example

Here is an example of a collation algorithm that uses the data pointer arg to compare two items. The DLL would export the following entries:

my_sort_ctx_t my_sort_ctx = { ... };

static int my_compare_func(void *arg,
                           int l1, const void *s1,
                           int l2, const void *s2)
{
    /* Custom code for comparing two items */
}

static int my_setup_func(void *arg, const void *data, char **errmsg)
{
    /* Custom code for initializing collation based on setup data
       in my_sort_ctx (which is referred to by arg) */
    return errno;
}

struct qdb_collation my_sort = {
    .name="my_sort", .encoding=SQLITE_UTF8, .arg=&my_sort_ctx,
    .compare=my_compare_func, .setup=my_setup_func };

Here, the collation routine is described by a structure named my_sort and calls the my_compare_func() function to do the actual sorting. This function is given data that is stored in the my_sort_ctx structure and passed in the arg argument. The same data is also passed in to the setup function, my_setup_func().

You would install this routine to QDB in the configuration object with this setting:

Collation::my_sort@/usr/lib/libqdb_mysort.so