Dr. Dobb's Journal - December 2008 - (Page 41) d12kri_p3ds 10/10/08 10:38 AM Page 41 if (points[i].longitude > wr.r.longitude) { wr.r.longitude = points[i].longitude; } Street_points_put(&street, i, &points[i]); } Street_wrap_rect_put(&street, &wr); For instance, assume a user is searching for a location using mapping software. The application must present the result (streets) in a window that corresponds to a map rectangle with coordinates min_longitude, min_latitude, max_longitude, max_latitude. mco_rect_t r; mco_cursor_t c; MCO_RET rc; r.l.x = min_longitude; r.l.y = min_latitude; r.r.x = max_longitude; r.r.y = max_latitude; if (Street_streets_idx_search(trans, MCO_EQ, &c, (double*)&r) == MCO_S_OK) { for (; rc == MCO_S_OK; rc = mco_cursor_next(trans, &c)) { Street street; Street_from_cursor(trans, &c, &street); // display it } } Many applications and services need efficient algorithms to perform spatial searches 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; }; 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)); } 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 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], 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), KDTrees (another spatial index), T-Trees for in-memory data access and storage, and Hash tables for quickly locating a single unique index entry. DDJ December 2008 l www.ddj.com l Dr. Dobb’s Journal 41 http://www.ddj.com/architect/208800854 http://www.ddj.com
Table of Contents Feed for the Digital Edition of Dr. Dobb's Journal - December 2008 Dr. Dobb's Journal - December 2008 Contents Friday Night Fish Fry Alia Vox Developer Diaries Conversations The Man Who Sold the Sky Performance on Rails LINQ-to-SQL and T-SQL A Remote Java RMI Registry Beyond B-Trees File Descriptors and Multithreaded Programs Effective Concurrency The Agile Edge Swaine's Flames Dr. Dobb's Journal - December 2008 Dr. Dobb's Journal - December 2008 - Dr. Dobb's Journal - December 2008 (Page Cover1) Dr. Dobb's Journal - December 2008 - Dr. Dobb's Journal - December 2008 (Page Cover2) Dr. Dobb's Journal - December 2008 - Dr. Dobb's Journal - December 2008 (Page 1) Dr. Dobb's Journal - December 2008 - Dr. Dobb's Journal - December 2008 (Page 2) Dr. Dobb's Journal - December 2008 - Dr. Dobb's Journal - December 2008 (Page 3) Dr. Dobb's Journal - December 2008 - Contents (Page 4) Dr. Dobb's Journal - December 2008 - Contents (Page 5) Dr. Dobb's Journal - December 2008 - Friday Night Fish Fry (Page 6) Dr. Dobb's Journal - December 2008 - Friday Night Fish Fry (Page 7) Dr. Dobb's Journal - December 2008 - Friday Night Fish Fry (Page 8) Dr. Dobb's Journal - December 2008 - Friday Night Fish Fry (Page 9) Dr. Dobb's Journal - December 2008 - Alia Vox (Page 10) Dr. Dobb's Journal - December 2008 - Alia Vox (Page 11) Dr. Dobb's Journal - December 2008 - Developer Diaries (Page 12) Dr. Dobb's Journal - December 2008 - Developer Diaries (Page 13) Dr. Dobb's Journal - December 2008 - Conversations (Page 14) Dr. Dobb's Journal - December 2008 - Conversations (Page 15) Dr. Dobb's Journal - December 2008 - The Man Who Sold the Sky (Page 16) Dr. Dobb's Journal - December 2008 - The Man Who Sold the Sky (Page 17) Dr. Dobb's Journal - December 2008 - The Man Who Sold the Sky (Page 18) Dr. Dobb's Journal - December 2008 - The Man Who Sold the Sky (Page 19) Dr. Dobb's Journal - December 2008 - Performance on Rails (Page 20) Dr. Dobb's Journal - December 2008 - Performance on Rails (Page 21) Dr. Dobb's Journal - December 2008 - Performance on Rails (Page 22) Dr. Dobb's Journal - December 2008 - Performance on Rails (Page 23) Dr. Dobb's Journal - December 2008 - Performance on Rails (Page 24) Dr. Dobb's Journal - December 2008 - Performance on Rails (Page 25) Dr. Dobb's Journal - December 2008 - Performance on Rails (Page 26) Dr. Dobb's Journal - December 2008 - Performance on Rails (Page 27) Dr. Dobb's Journal - December 2008 - Performance on Rails (Page 28) Dr. Dobb's Journal - December 2008 - LINQ-to-SQL and T-SQL (Page 29) Dr. Dobb's Journal - December 2008 - LINQ-to-SQL and T-SQL (Page 30) Dr. Dobb's Journal - December 2008 - LINQ-to-SQL and T-SQL (Page 31) Dr. Dobb's Journal - December 2008 - LINQ-to-SQL and T-SQL (Page 32) Dr. Dobb's Journal - December 2008 - LINQ-to-SQL and T-SQL (Page 33) Dr. Dobb's Journal - December 2008 - LINQ-to-SQL and T-SQL (Page 34) Dr. Dobb's Journal - December 2008 - A Remote Java RMI Registry (Page 35) Dr. Dobb's Journal - December 2008 - A Remote Java RMI Registry (Page 36) Dr. Dobb's Journal - December 2008 - A Remote Java RMI Registry (Page 37) Dr. Dobb's Journal - December 2008 - A Remote Java RMI Registry (Page 38) Dr. Dobb's Journal - December 2008 - A Remote Java RMI Registry (Page 39) Dr. Dobb's Journal - December 2008 - Beyond B-Trees (Page 40) Dr. Dobb's Journal - December 2008 - Beyond B-Trees (Page 41) Dr. Dobb's Journal - December 2008 - File Descriptors and Multithreaded Programs (Page 42) Dr. Dobb's Journal - December 2008 - File Descriptors and Multithreaded Programs (Page 43) Dr. Dobb's Journal - December 2008 - File Descriptors and Multithreaded Programs (Page 44) Dr. Dobb's Journal - December 2008 - File Descriptors and Multithreaded Programs (Page 45) Dr. Dobb's Journal - December 2008 - Effective Concurrency (Page 46) Dr. Dobb's Journal - December 2008 - Effective Concurrency (Page 47) Dr. Dobb's Journal - December 2008 - Effective Concurrency (Page 48) Dr. Dobb's Journal - December 2008 - The Agile Edge (Page 49) Dr. Dobb's Journal - December 2008 - The Agile Edge (Page 50) Dr. Dobb's Journal - December 2008 - The Agile Edge (Page 51) Dr. Dobb's Journal - December 2008 - Swaine's Flames (Page 52) Dr. Dobb's Journal - December 2008 - Swaine's Flames (Page Cover3) Dr. Dobb's Journal - December 2008 - Swaine's Flames (Page Cover4)
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.