indexf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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 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 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 Rect *r,
  43. SearchHitCallback *shcb, void *cbarg)
  44. {
  45. struct 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, cbarg)) {
  87. /* callback wants to terminate search early */
  88. return hitCount;
  89. }
  90. }
  91. }
  92. }
  93. top--;
  94. }
  95. }
  96. return hitCount;
  97. }
  98. /*
  99. * Inserts a new data rectangle into the index structure.
  100. * Non-recursively descends tree, propagates splits back up.
  101. * Returns 0 if node was not split. Old node updated.
  102. * If node was split, returns 1 and sets the pointer pointed to by
  103. * new_node to point to the new node. Old node updated to become one of two.
  104. * The level argument specifies the number of steps up from the leaf
  105. * level to insert; e.g. a data rectangle goes in at level = 0.
  106. */
  107. static int RTreeInsertRect2F(struct Rect *r, union Child child, int level,
  108. struct Node *newnode, off_t *newnode_pos,
  109. struct RTree *t,
  110. struct ListBranch **ee, int *overflow)
  111. {
  112. int i, currlevel;
  113. struct Branch b;
  114. struct Rect nr, *cover;
  115. struct Node *n, *n2, nn;
  116. struct fstack s[MAXLEVEL];
  117. int top = 0, down = 0;
  118. int result;
  119. n2 = &nn;
  120. /* add root node position to stack */
  121. currlevel = t->rootlevel;
  122. s[top].pos = t->rootpos;
  123. RTreeGetNode(&(s[top].sn), s[top].pos, currlevel, t);
  124. /* go down to level of insertion */
  125. while (s[top].sn.level > level) {
  126. n = &(s[top].sn);
  127. i = RTreePickBranch(r, n, t);
  128. s[top++].branch_id = i;
  129. /* add next node to stack */
  130. s[top].pos = n->branch[i].child.pos;
  131. currlevel--;
  132. RTreeGetNode(&(s[top].sn), s[top].pos, currlevel, t);
  133. assert(s[top].sn.level == currlevel);
  134. }
  135. /* Have reached level for insertion. Add rect, split if necessary */
  136. if (s[top].sn.level == level) {
  137. b.rect = *r;
  138. /* child field of leaves contains tid of data record */
  139. b.child = child;
  140. /* add branch, may split node or remove branches */
  141. if (top)
  142. cover = &(s[top - 1].sn.branch[s[top - 1].branch_id].rect);
  143. else
  144. cover = NULL;
  145. result = RTreeAddBranch(&b, &(s[top].sn), &n2, ee, cover, overflow, t);
  146. /* write out new node if node was split */
  147. if (result == 1) {
  148. *newnode_pos = RTreeGetNodePos(t);
  149. RTreeWriteNode(n2, t);
  150. t->n_nodes++;
  151. }
  152. /* update node */
  153. RTreePutNode(&(s[top].sn), s[top].pos, t);
  154. }
  155. else {
  156. /* Not supposed to happen */
  157. assert(s[top].sn.level == level);
  158. return 0;
  159. }
  160. /* go back up */
  161. while (top) {
  162. down = top--;
  163. i = s[top].branch_id;
  164. if (result == 0) { /* branch was added */
  165. nr = RTreeCombineRect(r, &(s[top].sn.branch[i].rect), t);
  166. /* rewrite rect */
  167. if (!RTreeCompareRect(&nr, &(s[top].sn.branch[i].rect), t)) {
  168. RTreeUpdateRect(&nr, &(s[top].sn), s[top].pos, i, t);
  169. }
  170. }
  171. else if (result == 2) { /* branches were removed */
  172. /* get node cover of previous node */
  173. nr = RTreeNodeCover(&(s[down].sn), t);
  174. /* rewrite rect */
  175. if (!RTreeCompareRect(&nr, &(s[top].sn.branch[i].rect), t)) {
  176. RTreeUpdateRect(&nr, &(s[top].sn), s[top].pos, i, t);
  177. }
  178. }
  179. else if (result == 1) { /* node was split */
  180. /* get node cover of previous node */
  181. s[top].sn.branch[i].rect = RTreeNodeCover(&(s[down].sn), t);
  182. /* add new branch for new node previously added by RTreeAddBranch() */
  183. b.child.pos = *newnode_pos;
  184. b.rect = RTreeNodeCover(n2, t);
  185. /* add branch, may split node or remove branches */
  186. if (top)
  187. cover = &(s[top - 1].sn.branch[s[top - 1].branch_id].rect);
  188. else
  189. cover = NULL;
  190. result = RTreeAddBranch(&b, &(s[top].sn), &n2, ee, cover, overflow, t);
  191. /* write out new node if node was split */
  192. if (result == 1) {
  193. *newnode_pos = RTreeGetNodePos(t);
  194. RTreeWriteNode(n2, t);
  195. t->n_nodes++;
  196. }
  197. /* update node */
  198. RTreePutNode(&(s[top].sn), s[top].pos, t);
  199. }
  200. }
  201. *newnode = *n2;
  202. return result;
  203. }
  204. /*
  205. * Insert a data rectangle into an index structure.
  206. * RTreeInsertRect1 provides for splitting the root;
  207. * returns 1 if root was split, 0 if it was not.
  208. * The level argument specifies the number of steps up from the leaf
  209. * level to insert; e.g. a data rectangle goes in at level = 0.
  210. * RTreeInsertRect2 does the actual insertion.
  211. */
  212. int RTreeInsertRectF(struct Rect *r, union Child child, int level,
  213. struct RTree *t)
  214. {
  215. struct Node oldroot, newroot, newnode;
  216. struct Branch b;
  217. struct ListBranch *e, *reInsertList = NULL;
  218. int result;
  219. int i, overflow[MAXLEVEL];
  220. off_t newnode_pos = -1;
  221. /* R*-tree forced reinsertion: for each level only once */
  222. for (i = 0; i < MAXLEVEL; i++)
  223. overflow[i] = 1;
  224. result =
  225. RTreeInsertRect2F(r, child, level, &newnode, &newnode_pos, t,
  226. &reInsertList, overflow);
  227. if (result == 1) { /* root split */
  228. RTreeGetNode(&oldroot, t->rootpos, t->rootlevel, t);
  229. /* grow a new root, & tree taller */
  230. t->rootlevel++;
  231. RTreeInitNode(&newroot, NODETYPE(t->rootlevel, t->fd));
  232. newroot.level = t->rootlevel;
  233. /* branch for old root */
  234. b.rect = RTreeNodeCover(&oldroot, t);
  235. b.child.pos = t->rootpos;
  236. RTreeAddBranch(&b, &newroot, NULL, NULL, NULL, NULL, t);
  237. /* branch for new node created by RTreeInsertRect2F() */
  238. b.rect = RTreeNodeCover(&newnode, t);
  239. b.child.pos = newnode_pos; /* offset to new node as returned by RTreeInsertRect2F() */
  240. RTreeAddBranch(&b, &newroot, NULL, NULL, NULL, NULL, t);
  241. /* write new root node */
  242. t->rootpos = RTreeGetNodePos(t);
  243. RTreeWriteNode(&newroot, t);
  244. t->n_nodes++;
  245. }
  246. else if (result == 2) { /* branches were removed */
  247. while (reInsertList) {
  248. /* get next branch in list */
  249. b = reInsertList->b;
  250. level = reInsertList->level;
  251. e = reInsertList;
  252. reInsertList = reInsertList->next;
  253. RTreeFreeListBranch(e);
  254. /* reinsert branches */
  255. result =
  256. RTreeInsertRect2F(&(b.rect), b.child, level, &newnode, &newnode_pos, t,
  257. &reInsertList, overflow);
  258. if (result == 1) { /* root split */
  259. RTreeGetNode(&oldroot, t->rootpos, t->rootlevel, t);
  260. /* grow a new root, & tree taller */
  261. t->rootlevel++;
  262. RTreeInitNode(&newroot, NODETYPE(t->rootlevel, t->fd));
  263. newroot.level = t->rootlevel;
  264. /* branch for old root */
  265. b.rect = RTreeNodeCover(&oldroot, t);
  266. b.child.pos = t->rootpos;
  267. RTreeAddBranch(&b, &newroot, NULL, NULL, NULL, NULL, t);
  268. /* branch for new node created by RTreeInsertRect2F() */
  269. b.rect = RTreeNodeCover(&newnode, t);
  270. b.child.pos = newnode_pos;
  271. RTreeAddBranch(&b, &newroot, NULL, NULL, NULL, NULL, t);
  272. /* write new root node */
  273. t->rootpos = RTreeGetNodePos(t);
  274. RTreeWriteNode(&newroot, t);
  275. t->n_nodes++;
  276. }
  277. }
  278. }
  279. return result;
  280. }
  281. /*
  282. * Delete a rectangle from non-root part of an index structure.
  283. * Called by RTreeDeleteRect. Descends tree non-recursively,
  284. * merges branches on the way back up.
  285. * Returns 1 if record not found, 0 if success.
  286. */
  287. static int
  288. RTreeDeleteRect2F(struct Rect *r, union Child child, struct RTree *t,
  289. struct ListNode **ee)
  290. {
  291. int i, notfound = 1, currlevel;
  292. struct Node *n;
  293. struct Rect nr;
  294. struct fstack s[MAXLEVEL];
  295. int top = 0, down = 0;
  296. int minfill;
  297. assert(ee);
  298. /* add root node position to stack */
  299. currlevel = t->rootlevel;
  300. s[top].pos = t->rootpos;
  301. RTreeGetNode(&(s[top].sn), s[top].pos, currlevel, t);
  302. s[top].branch_id = 0;
  303. while (notfound) {
  304. /* go down to level 0, remember path */
  305. if (s[top].sn.level > 0) {
  306. n = &(s[top].sn);
  307. currlevel = s[top].sn.level - 1;
  308. for (i = s[top].branch_id; i < t->nodecard; i++) {
  309. if (n->branch[i].child.pos > -1 && RTreeOverlap(r, &(n->branch[i].rect), t)) {
  310. s[top++].branch_id = i + 1;
  311. /* add next node to stack */
  312. s[top].pos = n->branch[i].child.pos;
  313. RTreeGetNode(&(s[top].sn), s[top].pos, currlevel, t);
  314. s[top].branch_id = 0;
  315. notfound = 0;
  316. break;
  317. }
  318. }
  319. if (notfound) {
  320. /* nothing else found, go back up */
  321. s[top].branch_id = t->nodecard;
  322. top--;
  323. }
  324. else /* found a way down but not yet the item */
  325. notfound = 1;
  326. }
  327. else {
  328. for (i = 0; i < t->leafcard; i++) {
  329. if (s[top].sn.branch[i].child.id &&
  330. s[top].sn.branch[i].child.id == child.id) { /* found item */
  331. RTreeDisconnectBranch(&(s[top].sn), i, t);
  332. RTreePutNode(&(s[top].sn), s[top].pos, t);
  333. t->n_leafs--;
  334. notfound = 0;
  335. break;
  336. }
  337. }
  338. if (notfound) /* continue searching */
  339. top--;
  340. }
  341. }
  342. if (notfound) {
  343. return notfound;
  344. }
  345. /* go back up */
  346. while (top) {
  347. down = top--;
  348. i = s[top].branch_id - 1;
  349. assert(s[down].sn.level == s[top].sn.level - 1);
  350. minfill = (s[down].sn.level ? t->min_node_fill : t->min_leaf_fill);
  351. if (s[down].sn.count >= minfill) {
  352. /* just update node cover */
  353. nr = RTreeNodeCover(&(s[down].sn), t);
  354. /* rewrite rect */
  355. if (!RTreeCompareRect(&nr, &(s[top].sn.branch[i].rect), t)) {
  356. RTreeUpdateRect(&nr, &(s[top].sn), s[top].pos, i, t);
  357. }
  358. }
  359. else {
  360. /* not enough entries in child, eliminate child node */
  361. assert(s[top].sn.branch[i].child.pos == s[down].pos);
  362. n = RTreeNewNode(t, s[down].sn.level);
  363. memcpy(n, &(s[down].sn), t->nodesize);
  364. RTreeAddNodePos(s[top].sn.branch[i].child.pos, s[down].sn.level, t);
  365. RTreeReInsertNode(n, ee);
  366. RTreeDisconnectBranch(&(s[top].sn), i, t);
  367. RTreePutNode(&(s[top].sn), s[top].pos, t);
  368. }
  369. }
  370. return notfound;
  371. }
  372. /*
  373. * should be called by RTreeDeleteRect() only
  374. *
  375. * Delete a data rectangle from an index structure.
  376. * Pass in a pointer to a Rect, the tid of the record, ptr RTree.
  377. * Returns 1 if record not found, 0 if success.
  378. * RTreeDeleteRect1 provides for eliminating the root.
  379. */
  380. int RTreeDeleteRectF(struct Rect *r, union Child child, struct RTree *t)
  381. {
  382. int i;
  383. struct Node *n, rn;
  384. struct ListNode *e, *reInsertList = NULL;
  385. if (!RTreeDeleteRect2F(r, child, t, &reInsertList)) {
  386. /* found and deleted a data item */
  387. /* reinsert any branches from eliminated nodes */
  388. while (reInsertList) {
  389. t->n_nodes--;
  390. n = reInsertList->node;
  391. if (n->level > 0) { /* reinsert node branches */
  392. for (i = 0; i < t->nodecard; i++) {
  393. if (n->branch[i].child.pos > -1) {
  394. RTreeInsertRectF(&(n->branch[i].rect),
  395. n->branch[i].child, n->level, t);
  396. }
  397. }
  398. }
  399. else { /* reinsert leaf branches */
  400. for (i = 0; i < t->leafcard; i++) {
  401. if (n->branch[i].child.id) {
  402. RTreeInsertRectF(&(n->branch[i].rect),
  403. n->branch[i].child, n->level, t);
  404. }
  405. }
  406. }
  407. e = reInsertList;
  408. reInsertList = reInsertList->next;
  409. RTreeFreeNode(e->node);
  410. RTreeFreeListNode(e);
  411. }
  412. /* check for redundant root (not leaf, 1 child) and eliminate */
  413. RTreeGetNode(&rn, t->rootpos, t->rootlevel, t);
  414. if (rn.count == 1 && rn.level > 0) {
  415. for (i = 0; i < t->nodecard; i++) {
  416. if (rn.branch[i].child.pos > -1)
  417. break;
  418. }
  419. RTreeAddNodePos(t->rootpos, t->rootlevel, t);
  420. t->rootpos = rn.branch[i].child.pos;
  421. t->rootlevel--;
  422. }
  423. return 0;
  424. }
  425. else {
  426. return 1;
  427. }
  428. }