Channels ▼
RSS

Database

Beyond B-Trees


User-Defined Indexes

eXtremeDB's user-defined index is not an index in its own right; rather, it extends a B-Tree to accept the user's specified comparison function. This is needed because applications use different rules for sorting string values. For example, an application might want to ignore a character's case, or compare values using the rules of a particular language.

By default, eXtremeDB uses "raw comparison" by comparing strings as sequences of bytes. Consider the class Person containing first and last names of the person:


class Person {
    string first_name;    
    string last_name;
};


If a case-insensitive index for Person's name is desired, it can be declared in the eXtremeDB schema description file using the userdef keyword:


class Person {
    string first_name;    
    string last_name;
    userdef tree <last_name,first_name> name_index;
};


Next, the comparison function is defined in a C file. This actually requires two functions—one to compare two objects, and another to compare a given key value to the index field value(s) of an object. The eXtremeDB schema compiler generates function prototypes—programmers must only provide function bodies:


/* object-to-object user-defined compare function */
int2 Person_name_index_compare_obj(Person* handle1, Person* handle2)
{
    char buf[2][MAX_NAME_LEN];
    uint2 len;
    int diff;
    /* extract last name component from objects*/
    Person_last_name_get(handle1, buf[0], MAX_NAME_LEN, &len);
    Person_last_name_get(handle2, buf[1], 
      MAX_NAME_LEN, &len);
    /* compare last names */
    diff = stricmp(buf[0], buf[1]);
    if (diff != 0) { /* if them not equal, return 
                       difference */
        return diff;
    }
    /* extract first names */
    Person_first_name_get(handle1, buf[0], 
      MAX_NAME_LEN, &len);
    Person_first_name_get(handle2, buf[1], 
      MAX_NAME_LEN, &len);
    /* compare first names */
    return stricmp(buf[0], buf[1]);
}
/* object-to-key user-defined compare function */
int2  Person_name_index__compare_ext(Person* handle, 
      void** key)
{
    char buf[MAX_NAME_LEN];
    uint2 len;
    int diff;
    /* extract last name component from the object */
    Person_last_name_get(handle, buf, MAX_NAME_LEN, 
      &len);
    /* compare last names */
    diff = stricmp(buf[0], 
      Person_name_index_extkey_last_name(key));
    if (diff != 0) { 
        return diff;
    }
    /* extract first names */
    Person_first_name_get(handle, buf, MAX_NAME_LEN, 
      &len);
    /* compare first names */
    return stricmp(buf, 
      Person_name_index_extkey_first_name(key));
}

The user-defined function must be registered in the database before use. Another valuable application of user-defined functions is an implementation of the soundex algorithm to search based on the sound of a word rather than its exact spelling—"wear," "where," and "ware," for instance.

Conclusion

Knowing something about specialized indexes enables faster development, more efficient code, and the ability to work with more complex data structures. Other "nontraditional" indexes worth exploring include the Patrice Trie (www.ddj.com/architect/208800854), KD-Trees (another spatial index), T-Trees for in-memory data access and storage, and Hash tables for quickly locating a single unique index entry.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video