README 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. This directory contains an SQLite extension that implements a virtual
  2. table type that allows users to create, query and manipulate r-tree[1]
  3. data structures inside of SQLite databases. Users create, populate
  4. and query r-tree structures using ordinary SQL statements.
  5. 1. SQL Interface
  6. 1.1 Table Creation
  7. 1.2 Data Manipulation
  8. 1.3 Data Querying
  9. 1.4 Introspection and Analysis
  10. 2. Compilation and Deployment
  11. 3. References
  12. 1. SQL INTERFACE
  13. 1.1 Table Creation.
  14. All r-tree virtual tables have an odd number of columns between
  15. 3 and 11. Unlike regular SQLite tables, r-tree tables are strongly
  16. typed.
  17. The leftmost column is always the pimary key and contains 64-bit
  18. integer values. Each subsequent column contains a 32-bit real
  19. value. For each pair of real values, the first (leftmost) must be
  20. less than or equal to the second. R-tree tables may be
  21. constructed using the following syntax:
  22. CREATE VIRTUAL TABLE <name> USING rtree(<column-names>)
  23. For example:
  24. CREATE VIRTUAL TABLE boxes USING rtree(boxno, xmin, xmax, ymin, ymax);
  25. INSERT INTO boxes VALUES(1, 1.0, 3.0, 2.0, 4.0);
  26. Constructing a virtual r-tree table <name> creates the following three
  27. real tables in the database to store the data structure:
  28. <name>_node
  29. <name>_rowid
  30. <name>_parent
  31. Dropping or modifying the contents of these tables directly will
  32. corrupt the r-tree structure. To delete an r-tree from a database,
  33. use a regular DROP TABLE statement:
  34. DROP TABLE <name>;
  35. Dropping the main r-tree table automatically drops the automatically
  36. created tables.
  37. 1.2 Data Manipulation (INSERT, UPDATE, DELETE).
  38. The usual INSERT, UPDATE or DELETE syntax is used to manipulate data
  39. stored in an r-tree table. Please note the following:
  40. * Inserting a NULL value into the primary key column has the
  41. same effect as inserting a NULL into an INTEGER PRIMARY KEY
  42. column of a regular table. The system automatically assigns
  43. an unused integer key value to the new record. Usually, this
  44. is one greater than the largest primary key value currently
  45. present in the table.
  46. * Attempting to insert a duplicate primary key value fails with
  47. an SQLITE_CONSTRAINT error.
  48. * Attempting to insert or modify a record such that the value
  49. stored in the (N*2)th column is greater than that stored in
  50. the (N*2+1)th column fails with an SQLITE_CONSTRAINT error.
  51. * When a record is inserted, values are always converted to
  52. the required type (64-bit integer or 32-bit real) as if they
  53. were part of an SQL CAST expression. Non-numeric strings are
  54. converted to zero.
  55. 1.3 Queries.
  56. R-tree tables may be queried using all of the same SQL syntax supported
  57. by regular tables. However, some query patterns are more efficient
  58. than others.
  59. R-trees support fast lookup by primary key value (O(logN), like
  60. regular tables).
  61. Any combination of equality and range (<, <=, >, >=) constraints
  62. on spatial data columns may be used to optimize other queries. This
  63. is the key advantage to using r-tree tables instead of creating
  64. indices on regular tables.
  65. 1.4 Introspection and Analysis.
  66. TODO: Describe rtreenode() and rtreedepth() functions.
  67. 2. COMPILATION AND USAGE
  68. The easiest way to compile and use the RTREE extension is to build
  69. and use it as a dynamically loadable SQLite extension. To do this
  70. using gcc on *nix:
  71. gcc -shared rtree.c -o libSqliteRtree.so
  72. You may need to add "-I" flags so that gcc can find sqlite3ext.h
  73. and sqlite3.h. The resulting shared lib, libSqliteRtree.so, may be
  74. loaded into sqlite in the same way as any other dynamicly loadable
  75. extension.
  76. 3. REFERENCES
  77. [1] Atonin Guttman, "R-trees - A Dynamic Index Structure For Spatial
  78. Searching", University of California Berkeley, 1984.
  79. [2] Norbert Beckmann, Hans-Peter Kriegel, Ralf Schneider, Bernhard Seeger,
  80. "The R*-tree: An Efficient and Robust Access Method for Points and
  81. Rectangles", Universitaet Bremen, 1990.