indexf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /****************************************************************************
  2. * MODULE: R-Tree library
  3. *
  4. * AUTHOR(S): Antonin Guttman - original code
  5. * Daniel Green (green@superliminal.com) - major clean-up
  6. * and implementation of bounding spheres
  7. * Markus Metz - file-based and memory-based R*-tree
  8. *
  9. * PURPOSE: Multidimensional index
  10. *
  11. * COPYRIGHT: (C) 2001 by the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General Public
  14. * License (>=v2). Read the file COPYING that comes with GRASS
  15. * for details.
  16. *****************************************************************************/
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <sys/types.h>
  21. #include <assert.h>
  22. #include <grass/config.h>
  23. #include <grass/gis.h>
  24. #include "index.h"
  25. #include "card.h"
  26. /* stack used for non-recursive insertion/deletion */
  27. struct fstack
  28. {
  29. struct RTree_Node sn; /* stack node */
  30. int branch_id; /* branch no to follow down */
  31. off_t pos; /* file position of stack node */
  32. };
  33. int RTreeValidChildF(union RTree_Child *child)
  34. {
  35. return (child->pos > -1);
  36. }
  37. /*
  38. * Search in an index tree for all data retangles that
  39. * overlap the argument rectangle.
  40. * Return the number of qualifying data rects.
  41. */
  42. int RTreeSearchF(struct RTree *t, struct RTree_Rect *r,
  43. SearchHitCallback *shcb, void *cbarg)
  44. {
  45. struct RTree_Node *n;
  46. int hitCount = 0, found, currlevel;
  47. int i;
  48. struct fstack s[MAXLEVEL];
  49. int top = 0;
  50. /* stack size of t->rootlevel + 1 is enough because of depth first search */
  51. /* only one node per level on stack at any given time */
  52. /* add root node position to stack */
  53. currlevel = t->rootlevel;
  54. s[top].pos = t->rootpos;
  55. RTreeGetNode(&(s[top].sn), s[top].pos, currlevel, t);
  56. s[top].branch_id = i = 0;
  57. while (top >= 0) {
  58. if (s[top].sn.level > 0) { /* this is an internal node in the tree */
  59. n = &(s[top].sn);
  60. found = 1;
  61. currlevel = s[top].sn.level - 1;
  62. for (i = s[top].branch_id; i < t->nodecard; i++) {
  63. if (s[top].sn.branch[i].child.pos > -1 &&
  64. RTreeOverlap(r, &(s[top].sn.branch[i].rect), t)) {
  65. s[top++].branch_id = i + 1;
  66. /* add next node to stack */
  67. s[top].pos = n->branch[i].child.pos;
  68. RTreeGetNode(&(s[top].sn), s[top].pos, currlevel, t);
  69. s[top].branch_id = 0;
  70. found = 0;
  71. break;
  72. }
  73. }
  74. if (found) {
  75. /* nothing else found, go back up */
  76. s[top].branch_id = t->nodecard;
  77. top--;
  78. }
  79. }
  80. else { /* this is a leaf node */
  81. for (i = 0; i < t->leafcard; i++) {
  82. if (s[top].sn.branch[i].child.id &&
  83. RTreeOverlap(r, &(s[top].sn.branch[i].rect), t)) {
  84. hitCount++;
  85. if (shcb) { /* call the user-provided callback */
  86. if (!shcb(s[top].sn.branch[i].child.id,
  87. s[top].sn.branch[i].rect, cbarg)) {
  88. /* callback wants to terminate search early */
  89. return hitCount;
  90. }
  91. }
  92. }
  93. }
  94. top--;
  95. }
  96. }
  97. return hitCount;
  98. }
  99. /*
  100. * Inserts a new data rectangle into the index structure.
  101. * Non-recursively descends tree, propagates splits back up.
  102. * Returns 0 if node was not split. Old node updated.
  103. * If node was split, returns 1 and sets the pointer pointed to by
  104. * new_node to point to the new node. Old node updated to become one of two.
  105. * The level argument specifies the number of steps up from the leaf
  106. * level to insert; e.g. a data rectangle goes in at level = 0.
  107. */
  108. static int RTreeInsertRect2F(struct RTree_Rect *r, union RTree_Child child, int level,
  109. struct RTree_Node *newnode, off_t *newnode_pos,
  110. struct RTree *t,
  111. struct RTree_ListBranch **ee, int *overflow)
  112. {
  113. int i, currlevel;
  114. struct RTree_Branch b;
  115. struct RTree_Rect nr, *cover;
  116. struct RTree_Node *n, *n2, nn;
  117. struct fstack s[MAXLEVEL];
  118. int top = 0, down = 0;
  119. int result;
  120. n2 = &nn;
  121. /* add root node position to stack */
  122. currlevel = t->rootlevel;
  123. s[top].pos = t->rootpos;
  124. RTreeGetNode(&(s[top].sn), s[top].pos, currlevel, t);
  125. /* go down to level of insertion */
  126. while (s[top].sn.level > level) {
  127. n = &(s[top].sn);
  128. i = RTreePickBranch(r, n, t);
  129. s[top++].branch_id = i;
  130. /* add next node to stack */
  131. s[top].pos = n->branch[i].child.pos;
  132. currlevel--;
  133. RTreeGetNode(&(s[top].sn), s[top].pos, currlevel, t);
  134. assert(s[top].sn.level == currlevel);
  135. }
  136. /* Have reached level for insertion. Add rect, split if necessary */
  137. if (s[top].sn.level == level) {
  138. b.rect = *r;
  139. /* child field of leaves contains tid of data record */
  140. b.child = child;
  141. /* add branch, may split node or remove branches */
  142. if (top)
  143. cover = &(s[top - 1].sn.branch[s[top - 1].branch_id].rect);
  144. else
  145. cover = NULL;
  146. result = RTreeAddBranch(&b, &(s[top].sn), &n2, ee, cover, overflow, t);
  147. /* write out new node if node was split */
  148. if (result == 1) {
  149. *newnode_pos = RTreeGetNodePos(t);
  150. RTreeWriteNode(n2, t);
  151. t->n_nodes++;
  152. }
  153. /* update node */
  154. RTreePutNode(&(s[top].sn), s[top].pos, t);
  155. }
  156. else {
  157. /* Not supposed to happen */
  158. assert(s[top].sn.level == level);
  159. return 0;
  160. }
  161. /* go back up */
  162. while (top) {
  163. down = top--;
  164. i = s[top].branch_id;
  165. if (result == 0) { /* branch was added */
  166. nr = RTreeCombineRect(r, &(s[top].sn.branch[i].rect), t);
  167. /* rewrite rect */
  168. if (!RTreeCompareRect(&nr, &(s[top].sn.branch[i].rect), t)) {
  169. RTreeUpdateRect(&nr, &(s[top].sn), s[top].pos, i, t);
  170. }
  171. }
  172. else if (result == 2) { /* branches were removed */
  173. /* get node cover of previous node */
  174. nr = RTreeNodeCover(&(s[down].sn), t);
  175. /* rewrite rect */
  176. if (!RTreeCompareRect(&nr, &(s[top].sn.branch[i].rect), t)) {
  177. RTreeUpdateRect(&nr, &(s[top].sn), s[top].pos, i, t);
  178. }
  179. }
  180. else if (result == 1) { /* node was split */
  181. /* get node cover of previous node */
  182. s[top].sn.branch[i].rect = RTreeNodeCover(&(s[down].sn), t);
  183. /* add new branch for new node previously added by RTreeAddBranch() */
  184. b.child.pos = *newnode_pos;
  185. b.rect = RTreeNodeCover(n2, t);
  186. /* add branch, may split node or remove branches */
  187. if (top)
  188. cover = &(s[top - 1].sn.branch[s[top - 1].branch_id].rect);
  189. else
  190. cover = NULL;
  191. result = RTreeAddBranch(&b, &(s[top].sn), &n2, ee, cover, overflow, t);
  192. /* write out new node if node was split */
  193. if (result == 1) {
  194. *newnode_pos = RTreeGetNodePos(t);
  195. RTreeWriteNode(n2, t);
  196. t->n_nodes++;
  197. }
  198. /* update node */
  199. RTreePutNode(&(s[top].sn), s[top].pos, t);
  200. }
  201. }
  202. *newnode = *n2;
  203. return result;
  204. }
  205. /*
  206. * Insert a data rectangle into an index structure.
  207. * RTreeInsertRect1 provides for splitting the root;
  208. * returns 1 if root was split, 0 if it was not.
  209. * The level argument specifies the number of steps up from the leaf
  210. * level to insert; e.g. a data rectangle goes in at level = 0.
  211. * RTreeInsertRect2 does the actual insertion.
  212. */
  213. int RTreeInsertRectF(struct RTree_Rect *r, union RTree_Child child, int level,
  214. struct RTree *t)
  215. {
  216. struct RTree_Node oldroot, newroot, newnode;
  217. struct RTree_Branch b;
  218. struct RTree_ListBranch *e, *reInsertList = NULL;
  219. int result;
  220. int i, overflow[MAXLEVEL];
  221. off_t newnode_pos = -1;
  222. /* R*-tree forced reinsertion: for each level only once */
  223. for (i = 0; i < MAXLEVEL; i++)
  224. overflow[i] = 1;
  225. result =
  226. RTreeInsertRect2F(r, child, level, &newnode, &newnode_pos, t,
  227. &reInsertList, overflow);
  228. if (result == 1) { /* root split */
  229. RTreeGetNode(&oldroot, t->rootpos, t->rootlevel, t);
  230. /* grow a new root, & tree taller */
  231. t->rootlevel++;
  232. RTreeInitNode(&newroot, NODETYPE(t->rootlevel, t->fd));
  233. newroot.level = t->rootlevel;
  234. /* branch for old root */
  235. b.rect = RTreeNodeCover(&oldroot, t);
  236. b.child.pos = t->rootpos;
  237. RTreeAddBranch(&b, &newroot, NULL, NULL, NULL, NULL, t);
  238. /* branch for new node created by RTreeInsertRect2F() */
  239. b.rect = RTreeNodeCover(&newnode, t);
  240. b.child.pos = newnode_pos; /* offset to new node as returned by RTreeInsertRect2F() */
  241. RTreeAddBranch(&b, &newroot, NULL, NULL, NULL, NULL, t);
  242. /* write new root node */
  243. t->rootpos = RTreeGetNodePos(t);
  244. RTreeWriteNode(&newroot, t);
  245. t->n_nodes++;
  246. }
  247. else if (result == 2) { /* branches were removed */
  248. while (reInsertList) {
  249. /* get next branch in list */
  250. b = reInsertList->b;
  251. level = reInsertList->level;
  252. e = reInsertList;
  253. reInsertList = reInsertList->next;
  254. RTreeFreeListBranch(e);
  255. /* reinsert branches */
  256. result =
  257. RTreeInsertRect2F(&(b.rect), b.child, level, &newnode, &newnode_pos, t,
  258. &reInsertList, overflow);
  259. if (result == 1) { /* root split */
  260. RTreeGetNode(&oldroot, t->rootpos, t->rootlevel, t);
  261. /* grow a new root, & tree taller */
  262. t->rootlevel++;
  263. RTreeInitNode(&newroot, NODETYPE(t->rootlevel, t->fd));
  264. newroot.level = t->rootlevel;
  265. /* branch for old root */
  266. b.rect = RTreeNodeCover(&oldroot, t);
  267. b.child.pos = t->rootpos;
  268. RTreeAddBranch(&b, &newroot, NULL, NULL, NULL, NULL, t);
  269. /* branch for new node created by RTreeInsertRect2F() */
  270. b.rect = RTreeNodeCover(&newnode, t);
  271. b.child.pos = newnode_pos;
  272. RTreeAddBranch(&b, &newroot, NULL, NULL, NULL, NULL, t);
  273. /* write new root node */
  274. t->rootpos = RTreeGetNodePos(t);
  275. RTreeWriteNode(&newroot, t);
  276. t->n_nodes++;
  277. }
  278. }
  279. }
  280. return result;
  281. }
  282. /*
  283. * Delete a rectangle from non-root part of an index structure.
  284. * Called by RTreeDeleteRect. Descends tree non-recursively,
  285. * merges branches on the way back up.
  286. * Returns 1 if record not found, 0 if success.
  287. */
  288. static int
  289. RTreeDeleteRect2F(struct RTree_Rect *r, union RTree_Child child, struct RTree *t,
  290. struct RTree_ListNode **ee)
  291. {
  292. int i, notfound = 1, currlevel;
  293. struct RTree_Node *n;
  294. struct RTree_Rect nr;
  295. struct fstack s[MAXLEVEL];
  296. int top = 0, down = 0;
  297. int minfill;
  298. assert(ee);
  299. /* add root node position to stack */
  300. currlevel = t->rootlevel;
  301. s[top].pos = t->rootpos;
  302. RTreeGetNode(&(s[top].sn), s[top].pos, currlevel, t);
  303. s[top].branch_id = 0;
  304. while (notfound) {
  305. /* go down to level 0, remember path */
  306. if (s[top].sn.level > 0) {
  307. n = &(s[top].sn);
  308. currlevel = s[top].sn.level - 1;
  309. for (i = s[top].branch_id; i < t->nodecard; i++) {
  310. if (n->branch[i].child.pos > -1 && RTreeOverlap(r, &(n->branch[i].rect), t)) {
  311. s[top++].branch_id = i + 1;
  312. /* add next node to stack */
  313. s[top].pos = n->branch[i].child.pos;
  314. RTreeGetNode(&(s[top].sn), s[top].pos, currlevel, t);
  315. s[top].branch_id = 0;
  316. notfound = 0;
  317. break;
  318. }
  319. }
  320. if (notfound) {
  321. /* nothing else found, go back up */
  322. s[top].branch_id = t->nodecard;
  323. top--;
  324. }
  325. else /* found a way down but not yet the item */
  326. notfound = 1;
  327. }
  328. else {
  329. for (i = 0; i < t->leafcard; i++) {
  330. if (s[top].sn.branch[i].child.id &&
  331. s[top].sn.branch[i].child.id == child.id) { /* found item */
  332. RTreeDisconnectBranch(&(s[top].sn), i, t);
  333. RTreePutNode(&(s[top].sn), s[top].pos, t);
  334. t->n_leafs--;
  335. notfound = 0;
  336. break;
  337. }
  338. }
  339. if (notfound) /* continue searching */
  340. top--;
  341. }
  342. }
  343. if (notfound) {
  344. return notfound;
  345. }
  346. /* go back up */
  347. while (top) {
  348. down = top--;
  349. i = s[top].branch_id - 1;
  350. assert(s[down].sn.level == s[top].sn.level - 1);
  351. minfill = (s[down].sn.level ? t->min_node_fill : t->min_leaf_fill);
  352. if (s[down].sn.count >= minfill) {
  353. /* just update node cover */
  354. nr = RTreeNodeCover(&(s[down].sn), t);
  355. /* rewrite rect */
  356. if (!RTreeCompareRect(&nr, &(s[top].sn.branch[i].rect), t)) {
  357. RTreeUpdateRect(&nr, &(s[top].sn), s[top].pos, i, t);
  358. }
  359. }
  360. else {
  361. /* not enough entries in child, eliminate child node */
  362. assert(s[top].sn.branch[i].child.pos == s[down].pos);
  363. n = RTreeNewNode(t, s[down].sn.level);
  364. memcpy(n, &(s[down].sn), t->nodesize);
  365. RTreeAddNodePos(s[top].sn.branch[i].child.pos, s[down].sn.level, t);
  366. RTreeReInsertNode(n, ee);
  367. RTreeDisconnectBranch(&(s[top].sn), i, t);
  368. RTreePutNode(&(s[top].sn), s[top].pos, t);
  369. }
  370. }
  371. return notfound;
  372. }
  373. /*
  374. * should be called by RTreeDeleteRect() only
  375. *
  376. * Delete a data rectangle from an index structure.
  377. * Pass in a pointer to a Rect, the tid of the record, ptr RTree.
  378. * Returns 1 if record not found, 0 if success.
  379. * RTreeDeleteRect1 provides for eliminating the root.
  380. */
  381. int RTreeDeleteRectF(struct RTree_Rect *r, union RTree_Child child, struct RTree *t)
  382. {
  383. int i;
  384. struct RTree_Node *n, rn;
  385. struct RTree_ListNode *e, *reInsertList = NULL;
  386. if (!RTreeDeleteRect2F(r, child, t, &reInsertList)) {
  387. /* found and deleted a data item */
  388. /* reinsert any branches from eliminated nodes */
  389. while (reInsertList) {
  390. t->n_nodes--;
  391. n = reInsertList->node;
  392. if (n->level > 0) { /* reinsert node branches */
  393. for (i = 0; i < t->nodecard; i++) {
  394. if (n->branch[i].child.pos > -1) {
  395. RTreeInsertRectF(&(n->branch[i].rect),
  396. n->branch[i].child, n->level, t);
  397. }
  398. }
  399. }
  400. else { /* reinsert leaf branches */
  401. for (i = 0; i < t->leafcard; i++) {
  402. if (n->branch[i].child.id) {
  403. RTreeInsertRectF(&(n->branch[i].rect),
  404. n->branch[i].child, n->level, t);
  405. }
  406. }
  407. }
  408. e = reInsertList;
  409. reInsertList = reInsertList->next;
  410. RTreeFreeNode(e->node);
  411. RTreeFreeListNode(e);
  412. }
  413. /* check for redundant root (not leaf, 1 child) and eliminate */
  414. RTreeGetNode(&rn, t->rootpos, t->rootlevel, t);
  415. if (rn.count == 1 && rn.level > 0) {
  416. for (i = 0; i < t->nodecard; i++) {
  417. if (rn.branch[i].child.pos > -1)
  418. break;
  419. }
  420. RTreeAddNodePos(t->rootpos, t->rootlevel, t);
  421. t->rootpos = rn.branch[i].child.pos;
  422. t->rootlevel--;
  423. }
  424. return 0;
  425. }
  426. else {
  427. return 1;
  428. }
  429. }