FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
Database
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
TABLE OF CONTENTS
November 07, 2008

Beyond B-Trees

(Page 2 of 2)

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.

Previous Page | 1 Spatial Index | 2 User-Defined Indexes
TOP 5 ARTICLES
No Top Articles.



MICROSITES
FEATURED TOPIC

ADDITIONAL TOPICS

INFO-LINK