Using Database indexes to improve embedded software performance and efficiency

Source: Internet
Author: User

BKJIA database channel also recommends the database index and query topic. I believe this topic will help you better understand this article. In order to get information from a book, how can we make it more effective? Should I carefully read the content of each page or precisely determine the location of the information to be obtained based on the index?

Obviously, the latter is more effective, and embedded systems should be so intelligent. Today, embedded applications are managing the increasing amount of complex data. Finding the correct data, whether it is looking for the route destination node of the Network Package, calculating the distance between points on the map, and other computing targets, must be achieved under real-time performance requirements.

Fortunately, programmers can use data indexes to increase the search speed from a linear level to a logarithm level. As the finished Database Management System (DBMS) becomes increasingly important in the development of embedded systems, more indexes are used. Most embedded databases support at least one index type.

However, in many projects, the index is usually unique) B-tree. This is because only B-tree indexes are the most common among many existing index types. Without a doubt, Tree B can efficiently perform basic search operations, such as exact match, prefix, and range search. However, for the development of IP routing, ing, and pattern query algorithms, non-generic index types are more suitable, in addition, it can increase the speed and make more effective use of resources such as CPU clock cycles. See figure 1 ).

Figure 1: B-tree is not the only Index Choice for embedded applications

The following sections describe how to use two generic index types: R tree and Patricia trie

Spatial index and R tree

Ing (MAP) and other location-based functions are common in Mobile embedded applications, from battlefield support systems in combat vehicles to mobile phone applications used to find the nearest pizza shop. These applications are based on spatial query algorithms, such as finding objects closest to the current location, and finding all objects near the user.

B-tree indexes are one-dimensional: they cannot process 2D and 3D coordinates used for spatial search. The R tree, also known as the Gutman R tree, is named after the inventor) is a good alternative. It uses bounding box) to map objects in the space. If an object is represented by coordinate points X, Y), its boundary box is a square with a side length of 1.

For all ry Object Lines, polygon, and other shapes), the boundary box is a rectangle in which the coordinates in the upper left corner are less than or equal to any point in the object, the coordinate in the lower right corner is greater than or equal to any point in the object. In other words, a boundary rectangle is the smallest rectangle that can completely contain a given object.

The R-tree index is usually used to accelerate spatial search to find rectangles that contain a certain vertex, and to find all rectangles that overlap with a given rectangle, and so on ). With the help of the boundary box, applications can manage various shapes.

The database definition language of eXtremeDB can be described as follows:

 
 
  1. /* Indicates the structure of points on the map */
  2. Struct Point
  3. {
  4. Double latitude;
  5. Double longpolling;
  6. };
  7. Class Street {
  8. /* Indicates the dot vector of a street */
  9. Vector <Point> points;
  10. /* Street enclosed rectangle */
  11. Rect <double> wrap_rect;
  12. Rtree <wrap_rect> streets_idx;
  13. };

If we want to add a new street, the APP must store the street coordinates and calculate its boundary box.

 
 
  1. Street street;  
  2. mco_rect_t wr;  
  3. wr.l.x = DOUBLE_MAX;  
  4. wr.l.y = DOUBLE_MAX;  
  5. wr.r.x = DOUBLE_MIN;  
  6. wr.r.y = DOUBLE_MIN;  
  7. Street_new(trans, &street);  
  8. Street_points_alloc(&street, n_points);  
  9. for (i = 0; i < n_points; i++)   
  10. {  
  11. if (points[i].latitude < wr.l.latitude) {  
  12. wr.l.latitude = points[i].latitude;  
  13. }  
  14. if (points[i].longitude < wr.l.longitude) {  
  15. wr.l.longitude = points[i].longitude;  
  16. }  
  17. if (points[i].latitude > wr.r.latitude) {  
  18. wr.r.latitude = points[i].latitude;  
  19. }  
  20. if (points[i].longitude > wr.r.longitude) {  
  21. wr.r.longitude = points[i].longitude;  
  22. }  
  23. Street_points_put(&street, i, &points[i]);  
  24. }  
  25. Street_wrap_rect_put(&street, &wr); 

If a user searches for a location, the map application returns the result Street) in the window as a map rectangle with the coordinates of |.

 
 
  1. mco_rect_t r;  
  2. mco_cursor_t c;  
  3. MCO_RET rc;  
  4. r.l.x = min_longitude;  
  5. r.l.y = min_latitude;  
  6. r.r.x = max_longitude;  
  7. r.r.y = max_latitude;  
  8. if (Street_streets_idx_search(trans, MCO_EQ, &c, (double*)&r) == MCO_S_OK)  
  9. {  
  10. for (; rc == MCO_S_OK; rc = mco_cursor_next(trans, &c))  
  11. {  
  12. Street street;  
  13. Street_from_cursor(trans, &c, &street);  
  14. // display it  
  15. }  

Patricia trie

Tree B can use the specified prefix to locate the keyword, for example, to find a company whose name starts with "AAA. However, some applications must search for keywords that represent the longest prefix of a specific value. Tree B can meet this requirement, but it must start from the longest prefix for multiple iterations and query different given prefix.

A more effective prefix search index is a practical algorithm used to query letter-number encoding, that is, Patricia trie. Patricia trie is a kind of deformation of Binary Trees. It is usually used for telephone routing and IP filtering. In the first case, the application must select the correct contact number for the access phone number and an operator table with a known prefix. In the second case, a valid/rejected domain IP mask is given, and the received (one) HTTP request should be classified as accept, reject, forward, and so on. The following database mode defines a route table with a mask represented by a bit vector.

 
 
  1. class Route  
  2. {  
  3. Vector<bool> dest;  
  4. uint4 gateway;  
  5. uint4 interf;  
  6. uint2 metric;  
  7. unique patricia  by_dest;  
  8. }; 

In order to find a suitable forwarding target for the received IP address, the application uses Patricia trie to query eXtremeDB as follows:

 
 
  1. Mco_cursor_t csr;
  2. If (MCO_S_ OK = Route_by_dest_index_cursor (trans, & csr )){
  3. Uint1 mask [4];
  4. Make_mask (mask, ip, 32 );
  5. /* Find routes which mask match this IP address */
  6. /* Find the forwarding node that matches the mask and IP address */
  7. If (MCO_S_ OK = Route_by_dest_prefix_match (trans, & csr, mask, 32 );
  8. Route route;
  9. Route_from_cursor (trans, & csr, & route );
  10. ...
  11. }
  12. }

The following code converts an integer representing an IP address to a bitvector:

 
 
  1. void make_mask(uint1* mask, uint4 val, int bitnum)  
  2. {  
  3. int i;  
  4. val = val >> (32-bitnum);  
  5. memset(mask, 0, 4);  
  6. for (i = 0; i < bitnum; i++, val = val >> 1)  
  7. {  
  8. mask[i >> 3] |= (val&1) << (i&7);  
  9. }  

The more available indexes, the better the result.

Using dedicated indexes such as the R tree and Patricia trie accelerates code development, improves code efficiency, and enables applications to process more complex data structures. Although the famous B-tree is sufficient to handle a large number of common query tasks, the less famous index types can be better done in specialized applications and data types. The R tree can efficiently process ing and geospatial data, while Patricia trie is an ideal solution for telephone routing, IP filtering, and other tasks that must be completed by matching the maximum prefix of a specific value.

Steve Graves is one of the CEOS and founders of McObject. McObject focuses on Embedded Database System software. Prior to McObject, Steve was the president of Centura Solutions and vice president of global consulting at Centura Software Inc. Nasdaq stock code: CNTR. He is also the president and chief operating officer of Raima. You can contact Steve through the steve.graves@mcobject.com.

Konstantin Knizhnik is a software engineer at McObject and participates in the development of the embedded database system eXtremeDB. He is also the author of multiple excellent open-source projects, including Java-based database management systems Perst and Perst Lite, it also provides extension tools for multiple programming languages, including Java, C ++, and C. You can contact Konstantin through a knizhnik@mcobject.com. If you have any questions, contact McObject China.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.