-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstRTree.h
More file actions
1003 lines (887 loc) · 29.7 KB
/
Copy pathstRTree.h
File metadata and controls
1003 lines (887 loc) · 29.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* File: stRTree.h
* Author: aocsa
*
* Created on January 16, 2007, 3:07 PM
*/
#ifndef _stRTree_H
#define _stRTree_H
#include <arboretum/stPageManager.h>
#include <arboretum/stTypes.h>
#include <arboretum/stUtil.h>
#include <arboretum/stResult.h>
#include <arboretum/stGenericPriorityQueue.h>
#include "stRNode.h"
#include "stRLogicNode.h"
#include "stSubTreeInfo.h"
#define R_ORDER(A, B) (floor(((A-NODE_HEAD_SIZE)/double(ENTRY_SIZE+B))) - 1)
// this is used to set the initial size of the dynamic queue
#define STARTVALUEQUEUE 200
// this is used to set the increment size of the dynamic queue
#define INCREMENTVALUEQUEUE 200
//=============================================================================
// Class template stRTree
//-----------------------------------------------------------------------------
/**
* @author Alexander Ocsa . (alexocsa@unsa.edu.pe)
* @todo More documentation.??
* @todo Finish the implementation.??
* @version 1.0
*/
template<class ObjectType>
class stRTree{
public:
typedef stResult<ObjectType> tResult;
typedef ObjectType tObject;
typedef stSubTreeInfo<ObjectType> tSubTreeInfo;
public:
stRTree(stPageManager* pageManager);
virtual ~stRTree();
bool Add(tObject* data);
bool Remove(tObject* obj);
bool UpdateData(tObject* oldValue, tObject* newValue);
tResult* IntersectionQuery(tObject* data);
tResult* NearestQuery(tObject * sample, stCount k, bool tie = false);
void Print(std::ostream& out) {
if(GetRoot() != 0) {
out << "----------------------------------------\n";
this->Print(out, GetRoot(), 0);
out << "----------------------------------------\n\n";
}
}
void Print(std::ostream& out, stPageID currPageID, int level);
stCount GetNumberOfObjects() {
return HeaderInfo->ObjectCount;
}
protected:
enum stInsertAction{ NO_ACT,
CHANGE_REP,
PROMOTION,
REMOVE_FARTHEST,
REMOVE_NODE,
NOT_FOUND
};
typedef stRLogicNode<ObjectType> tLogicNode;
typedef stRPriorityQueue < stDistance, stCount > tPriorityQueue;
typedef stDynamicRPriorityQueue < stDistance, stQueryPriorityQueueValue > tDynamicPriorityQueue;
typedef stGenericPriorityHeap < ObjectType > tPGenericHeap;
private:
void WriteHeader();
void LoadHeader();
void DefaultHeader();
bool RecursiveUpdateData(stPageID currNodeID, tObject* oldValue, tObject* newValue, tSubTreeInfo* promo);
int RecursiveInsert(stPageID currNodeID, tSubTreeInfo& newSubTree, ObjectType* repObj, tSubTreeInfo* promo);
int RecursiveRemove(stPageID currNodeID, tObject* obj, tSubTreeInfo* promo);
void AddNewRoot(tSubTreeInfo* promo);
void Split(stRNode* currNode, tSubTreeInfo* promo);
int ChooseSubTree(stRNode* node, tSubTreeInfo &newSubtree);
void NearestQuery(tResult * result,
ObjectType * sample, stDistance rangeK, stCount k,
tDynamicPriorityQueue * queue);
stPageID GetRoot() {
return this->HeaderInfo->Root;
}
void SetRoot(stPageID id) {
this->HeaderInfo->Root = id;
this->HeaderUpdate = true;
}
stPage* GetNewPage() {
this->HeaderInfo->NodeCount++;
return this->myPageManager->GetNewPage();
}
stSize GetOrder() {
return this->HeaderInfo->Order;
}
stSize GetNumberOfClusters() {
return this->HeaderInfo->NumberOfClusters;
}
private:
struct stRTreeHeader {
int Order;
ObjectType* TreeMBR;
int NumberOfClusters;
int SplitMethod;
int ChooseMethod;
stPageID Root;
stCount MaxOccupation;
stCount ObjectCount;
stSize NodeCount;
bool ReInsertObjects;
stCount Height;
};
public:
enum tChooseMethod {
ORIGINAL_RTREE_CHOOSE
};
enum tSplitMethod {
ORIGINAL_RTREE_SPLIT
};
private:
stPageManager* myPageManager;
stRTreeHeader* HeaderInfo;
stPage* HeaderPage;
bool HeaderUpdate;
};
//-------------------------------------------------------------------------------------
template<class ObjectType>
stRTree<ObjectType>::stRTree(stPageManager* pageManager)
{
myPageManager = pageManager;
HeaderInfo = NULL;
HeaderPage = NULL;
LoadHeader();
if( this->myPageManager->IsEmpty()) {
DefaultHeader();
}
// Allocate Collection for ReInsert
}
template<class ObjectType>
stRTree<ObjectType>::~stRTree()
{
if(this->HeaderPage != NULL) {
if( this->HeaderInfo != NULL)
this->WriteHeader();
this->myPageManager->ReleasePage(this->HeaderPage);
}
}
template<class ObjectType>
void stRTree<ObjectType>::WriteHeader()
{
if(this->HeaderUpdate) {
this->myPageManager->WriteHeaderPage(this->HeaderPage);
this->HeaderUpdate = false;
}
}
template<class ObjectType>
void stRTree<ObjectType>::LoadHeader()
{
if( this->HeaderPage != NULL ) {
myPageManager->ReleasePage(this->HeaderPage);
}
this->HeaderPage = myPageManager->GetHeaderPage();
this->HeaderInfo = (stRTreeHeader*) this->HeaderPage->GetData();
if( this->myPageManager->GetMinimumPageSize() <= sizeof(stRTreeHeader) ) {
throw page_size_error("The Page Header size is too small");
}
this->HeaderUpdate = false;
}
template<class ObjectType>
void stRTree<ObjectType>::DefaultHeader()
{
this->HeaderPage->Clear();
this->HeaderInfo->TreeMBR = NULL;
this->HeaderInfo->Height = 0;
this->HeaderInfo->ChooseMethod = 0;
this->HeaderInfo->MaxOccupation = 0;
this->HeaderInfo->NodeCount = 0;
this->HeaderInfo->ObjectCount = 0;
this->HeaderInfo->NumberOfClusters = 2;
this->HeaderInfo->ReInsertObjects = false;
this->HeaderInfo->Root = 0;
this->HeaderInfo->SplitMethod = 0;
this->HeaderUpdate = true;
}
template<class ObjectType>
bool stRTree<ObjectType>::UpdateData(tObject* oldValue, tObject* newValue)
{
bool result;
if ( this->GetRoot() == 0) {
result = false;
}
else {
tSubTreeInfo* promo = new tSubTreeInfo[2];
promo[0].Rep = NULL;
promo[1].Rep = NULL;
if(! this->RecursiveUpdateData( this->GetRoot(), oldValue, newValue, promo) ) {
result = false;
}
else {
this->HeaderInfo->TreeMBR = promo[0].Rep;
result = true;
}
delete []promo;
}
return result;
}
template<class ObjectType>
bool stRTree<ObjectType>::RecursiveUpdateData(stPageID currNodeID, tObject* oldValue, tObject* newValue, tSubTreeInfo* promo)
{
int idxSubTree;
int idx;
bool resultAction;
stPage* currPage;
stRNode* currNode;
stMBR* mbr;
ObjectType* tmpObj;
currPage = this->myPageManager->GetPage(currNodeID);
currNode = new stRNode(currPage);
tSubTreeInfo newSubTree;
newSubTree.Rep = oldValue;
idxSubTree = this->ChooseSubTree(currNode, newSubTree);
if( idxSubTree >= 0) {
if( this->RecursiveUpdateData(currNode->GetEntry(idxSubTree).PageID, oldValue, newValue, promo) ) {
currNode->SetEntry(idxSubTree, promo[0].Rep->GetSerializedSize(), promo[0].Rep->Serialize(), promo[0].RootID);
currNode->SetNENtries(idxSubTree, promo[0].NObjects);
tmpObj = new ObjectType();
tmpObj->Unserialize( currNode->GetObject(0), currNode->GetObjectSize(0));
mbr = tmpObj->Clone();
for (idx = 1; idx < currNode->GetNumberOfEntries(); idx++) {
tmpObj->Unserialize( currNode->GetObject(idx), currNode->GetObjectSize(idx));
mbr = mbr->GetUnionMBR( (stMBR*)tmpObj );
}
delete tmpObj;
if( promo[0].Rep )
delete promo[0].Rep;
promo[0].Rep = (ObjectType*)mbr;
promo[0].NObjects = currNode->GetTotalObjectCount();
promo[0].RootID = currNode->GetPageID();
resultAction = true;
}
else {
resultAction = false;
}
}
else {
tmpObj = new ObjectType();
for (idx = 0; idx < currNode->GetNumberOfEntries(); idx++) {
tmpObj->Unserialize( currNode->GetObject(idx), currNode->GetObjectSize(idx));
if( tmpObj->IsEqual(oldValue) ) {
#ifdef __stDEBUG__
std::cout << "found!\n";
#endif
break;
}
}
if( idx < currNode->GetNumberOfEntries()) {
//found
#ifdef __stDEBUG__
std::cout << "update...idx = " << idx << "\n";
#endif
currNode->SetEntry(idx, newValue->GetSerializedSize(), newValue->Serialize(), 0);
tmpObj->Unserialize(currNode->GetObject(0), currNode->GetObjectSize(0));
mbr = tmpObj->Clone();
for (idx = 1; idx < currNode->GetNumberOfEntries(); idx++) {
tmpObj->Unserialize( currNode->GetObject(idx), currNode->GetObjectSize(idx));
mbr = mbr->GetUnionMBR( (stMBR*)tmpObj );
}
#ifdef __stDEBUG__
newValue->Print(std::cout);
std::cin.get();
#endif
delete tmpObj;
promo[0].Rep = (ObjectType*)mbr;
promo[0].NObjects = currNode->GetTotalObjectCount();
promo[0].RootID = currNode->GetPageID();
resultAction = true;
}
else {
//!found
resultAction = false;
}
}
if(resultAction)
this->myPageManager->WritePage(currPage);
delete currNode;
this->myPageManager->ReleasePage(currPage);
return resultAction;
}
template<class ObjectType>
bool stRTree<ObjectType>::Add(ObjectType* data)
{
int insertIdx;
stPage* rootPage;
stRNode* rootNode;
if ( this->GetRoot() == 0) {
#ifdef __stDEBUG__
std::cout << "first object\n";
#endif
this->HeaderInfo->Order = R_ORDER(PAGE_SIZE, data->GetSerializedSize());
rootPage = this->GetNewPage();
rootNode = new stRNode(rootPage, this->GetOrder());
insertIdx = rootNode->AddEntry(data->GetSerializedSize(), data->Serialize(), 0);
#ifdef __stDEBUG__
if(insertIdx < 0) {
throw page_size_error("The page size is too small to store the first object");
}
#endif // __stDEBUG__
rootNode->GetEntry(0).PageID = 0;
this->SetRoot(rootPage->GetPageID());
this->HeaderInfo->Height = 1;
this->myPageManager->WritePage(rootPage);
delete rootNode;
this->myPageManager->ReleasePage(rootPage);
}
else {
tSubTreeInfo subTree;
subTree.Rep = data;
subTree.Height = 0;
subTree.NObjects = 0;
subTree.RootID = 0;
tSubTreeInfo* promo = new tSubTreeInfo[2];
promo[0].Rep = NULL;
promo[1].Rep = NULL;
#ifdef __stDEBUG__
std::cout << "first, recursive insert\n";
#endif
if( PROMOTION == this->RecursiveInsert( this->GetRoot(), subTree, NULL, promo) ){
this->AddNewRoot(promo); //this->HeaderInfo->Height++;
this->HeaderUpdate = true;
}
this->HeaderInfo->TreeMBR = promo[0].Rep;
delete []promo;
}
this->HeaderInfo->ObjectCount++;
return true;
}
template<class ObjectType>
int stRTree<ObjectType>::RecursiveInsert(stPageID currNodeID, tSubTreeInfo& newSubTree, ObjectType* repObj, tSubTreeInfo* promo)
{
int idxSubTree, insertIdx;
int idx, resultAction;
ObjectType* subTreeObj;
stPage* currPage;
stRNode* currNode;
stMBR* mbr;
ObjectType* tmpObj;
currPage = this->myPageManager->GetPage(currNodeID);
currNode = new stRNode(currPage);
idxSubTree = this->ChooseSubTree(currNode, newSubTree);
if( idxSubTree >= 0) {
subTreeObj = new ObjectType();
subTreeObj->Unserialize(newSubTree.Rep->Serialize(), newSubTree.Rep->GetSerializedSize());
#ifdef __stDEBUG__
std::cout << "recursive insert\n";
#endif
switch( this->RecursiveInsert(currNode->GetEntry(idxSubTree).PageID, newSubTree, subTreeObj, promo) ) {
case NO_ACT:
#ifdef __stDEBUG__
std::cout << "update\n";
#endif
currNode->SetEntry(idxSubTree, promo[0].Rep->GetSerializedSize(), promo[0].Rep->Serialize(), promo[0].RootID);
currNode->SetNENtries(idxSubTree, promo[0].NObjects);
tmpObj = new ObjectType();
tmpObj->Unserialize( currNode->GetObject(0), currNode->GetObjectSize(0));
mbr = tmpObj->Clone();
for (idx = 1; idx < currNode->GetNumberOfEntries(); idx++) {
tmpObj->Unserialize( currNode->GetObject(idx), currNode->GetObjectSize(idx));
mbr = mbr->GetUnionMBR( (stMBR*)tmpObj );
}
delete tmpObj;
if( promo[0].Rep )
delete promo[0].Rep;
promo[0].Rep = (ObjectType*)mbr;
promo[0].NObjects = currNode->GetTotalObjectCount();
promo[0].RootID = currNode->GetPageID();
resultAction = NO_ACT;
break;
case PROMOTION:
#ifdef __stDEBUG__
std::cout << "promotion\n";
#endif
bool stop;
idx = 0;
currNode->SetEntry( idxSubTree, promo[idx].Rep->GetSerializedSize(),
promo[idx].Rep->Serialize(),
promo[idx].RootID);
currNode->SetNENtries(idxSubTree, promo[idx].NObjects);
delete promo[idx].Rep;
promo[idx].Rep = NULL;
idx++;
stop = (idx == this->GetNumberOfClusters());
while (!stop) {
if(promo[idx].Rep != NULL) {
insertIdx = currNode->AddEntry(promo[idx].Rep->GetSerializedSize(),
promo[idx].Rep->Serialize(),
promo[idx].RootID);
if(insertIdx < 0) {
stop = true;
}
else {
currNode->SetNENtries(insertIdx, promo[idx].NObjects);
delete promo[idx].Rep;
promo[idx].Rep = NULL;
}
}
idx++;
stop = (stop || idx == this->GetNumberOfClusters());
}
if( currNode->IsOverflow()) {
this->Split(currNode, promo);
resultAction = PROMOTION;
}
else {
tmpObj = new ObjectType();
tmpObj->Unserialize( currNode->GetObject(0), currNode->GetObjectSize(0));
mbr = tmpObj->Clone();
for (idx = 1; idx < currNode->GetNumberOfEntries(); idx++) {
tmpObj->Unserialize( currNode->GetObject(idx), currNode->GetObjectSize(idx));
mbr = mbr->GetUnionMBR( (stMBR*)tmpObj );
}
delete tmpObj;
if( promo[0].Rep )
delete promo[0].Rep;
promo[0].Rep = (ObjectType*)mbr;
promo[0].NObjects = currNode->GetTotalObjectCount();
promo[0].RootID = currNode->GetPageID();
resultAction = NO_ACT;
}
break;
//case
}
}
else {
#ifdef __stDEBUG__
std::cout << "add object\n";
#endif
insertIdx = currNode->AddEntry(newSubTree.Rep->GetSerializedSize(), newSubTree.Rep->Serialize(), 0);
currNode->SetNENtries(insertIdx, 0);
if( currNode->IsOverflow()) {
this->Split(currNode, promo);
resultAction = PROMOTION;
}
else {
tmpObj = new ObjectType();
tmpObj->Unserialize( currNode->GetObject(0), currNode->GetObjectSize(0));
mbr = tmpObj->Clone();
for (idx = 1; idx < currNode->GetNumberOfEntries(); idx++) {
tmpObj->Unserialize( currNode->GetObject(idx), currNode->GetObjectSize(idx));
mbr = mbr->GetUnionMBR( (stMBR*)tmpObj );
}
delete tmpObj;
promo[0].Rep = (ObjectType*)mbr;
promo[0].NObjects = currNode->GetTotalObjectCount();
promo[0].RootID = currNode->GetPageID();
resultAction = NO_ACT;
}
}
#ifdef __stDEBUG__
std::cout << "resultAction "<< resultAction << "\n";
#endif
this->myPageManager->WritePage(currPage);
delete currNode;
this->myPageManager->ReleasePage(currPage);
return resultAction;
}
template<class ObjectType>
void stRTree<ObjectType>::AddNewRoot(tSubTreeInfo* promo)
{
int idx, insertIdx;
stPage* rootPage;
stRNode* rootNode;
rootPage = this->GetNewPage();
rootNode = new stRNode(rootPage, this->GetOrder());
for (idx = 0; idx < this->GetNumberOfClusters(); idx++) {
if(promo[idx].Rep) {
insertIdx = rootNode->AddEntry(promo[idx].Rep->GetSerializedSize(), promo[idx].Rep->Serialize(), promo[idx].RootID);
rootNode->SetNENtries(insertIdx, promo[idx].NObjects);
delete promo[idx].Rep;
promo[idx].Rep = NULL;
}
}
this->HeaderInfo->Height++;
this->SetRoot(rootPage->GetPageID());
ObjectType* tmpObj = new ObjectType();
tmpObj->Unserialize( rootNode->GetObject(0), rootNode->GetObjectSize(0));
stMBR* mbr = tmpObj->Clone();
for (idx = 1; idx < rootNode->GetNumberOfEntries(); idx++) {
tmpObj->Unserialize( rootNode->GetObject(idx), rootNode->GetObjectSize(idx));
mbr = mbr->GetUnionMBR( (stMBR*)tmpObj );
}
delete tmpObj;
promo[0].Rep = (ObjectType*)mbr;
this->myPageManager->WritePage(rootPage);
delete rootNode;
this->myPageManager->ReleasePage(rootPage);
}
template<class ObjectType>
void stRTree<ObjectType>::Split(stRNode* oldNode,tSubTreeInfo* promo)
{
stPage* newPage;
stRNode* newNode;
tLogicNode* logicNode = new tLogicNode( oldNode->GetNumberOfEntries() );
newPage = this->GetNewPage();
newNode = new stRNode(newPage, GetOrder());
logicNode->AddNode(oldNode);
oldNode->RemoveAll();
logicNode->Distribute(oldNode, newNode, promo);
this->myPageManager->WritePage(oldNode->GetPage());///????
this->myPageManager->WritePage(newPage);
delete newNode;
this->myPageManager->ReleasePage(newPage);
}
template<class ObjectType>
int stRTree<ObjectType>::ChooseSubTree(stRNode* node, tSubTreeInfo &newSubtree)
{
if(node->GetNumberOfEntries() == node->GetNumberOfFreeObjects() )
return -1;
int subTree = -1;
int i;
stDistance increased;
stDistance min = MAXDOUBLE;
ObjectType* tmpObj = new ObjectType();
for (i = 0; i < node->GetNumberOfEntries(); i++) {
tmpObj->Unserialize(node->GetObject(i), node->GetObjectSize(i));
stMBR* a = tmpObj->GetUnionMBR( newSubtree.Rep );
increased = a->GetArea() - tmpObj->GetArea();
//increased = tmpObj->IncreaseRequired(newSubtree->Rep);
if( increased < min ) {
min = increased;
subTree = i;
}
delete a;
}
delete tmpObj;
return subTree;
}
template<class ObjectType>
void stRTree<ObjectType>::Print(std::ostream& out, stPageID currPageID, int level)
{
int idx;
stPage* currPage;
stRNode* currNode;
ObjectType* tmpObj = new ObjectType;
currPage = this->myPageManager->GetPage(currPageID);
currNode = new stRNode(currPage);
//out << "*" << currNode->GetNumberOfEntries() << "*\n";
for (idx = currNode->GetNumberOfEntries() - 1; idx >= 0; idx--) {
if(currNode->GetEntry(idx).PageID) {
this->Print(out, currNode->GetEntry(idx).PageID, level + 1);
}
tmpObj->Unserialize(currNode->GetObject(idx), currNode->GetObjectSize(idx));
for (int i = 0; i < DIMENSION;i++) {
for (int k = 0; k < level ; k++) {
out << " ";
}
out << tmpObj->GetBegin(i) << " - " << tmpObj->GetEnd(i) << "\n";
}
for (int k = 0; k < level ; k++) {
out << " ";
}
out << tmpObj->GetOID() << "|\n";
out << "\n";
//out << << ;
}
delete tmpObj;
delete currNode;
this->myPageManager->ReleasePage(currPage);
}
template<class ObjectType>
bool stRTree<ObjectType>::Remove(tObject* obj)
{
bool result;
if ( this->GetRoot() == 0) {
result = false;
}
else {
tSubTreeInfo* promo = new tSubTreeInfo[2];
promo[0].Rep = NULL;
promo[1].Rep = NULL;
if( this->RecursiveRemove( this->GetRoot(), obj, promo) == NOT_FOUND) {
result = false;
}
else {
this->HeaderInfo->TreeMBR = promo[0].Rep;
result = true;
}
delete []promo;
}
return result;
}
template<class ObjectType>
int stRTree<ObjectType>::RecursiveRemove(stPageID currPageID, tObject* obj, tSubTreeInfo* promo)
{
int idxSubTree;
int idx;
int resultAction;
stPage* currPage;
stRNode* currNode;
stMBR* mbr;
ObjectType* tmpObj;
currPage = this->myPageManager->GetPage(currPageID);
currNode = new stRNode(currPage);
tSubTreeInfo newSubTree;
newSubTree.Rep = obj;
idxSubTree = this->ChooseSubTree(currNode, newSubTree);
if( idxSubTree >= 0) {
switch( this->RecursiveRemove(currNode->GetEntry(idxSubTree).PageID, obj, promo) ) {
case NO_ACT:
currNode->SetEntry(idxSubTree, promo[0].Rep->GetSerializedSize(), promo[0].Rep->Serialize(), promo[0].RootID);
currNode->SetNENtries(idxSubTree, promo[0].NObjects);
tmpObj = new ObjectType();
tmpObj->Unserialize( currNode->GetObject(0), currNode->GetObjectSize(0));
mbr = tmpObj->Clone();
for (idx = 1; idx < currNode->GetNumberOfEntries(); idx++) {
tmpObj->Unserialize( currNode->GetObject(idx), currNode->GetObjectSize(idx));
mbr = mbr->GetUnionMBR( (stMBR*)tmpObj );
}
delete tmpObj;
if( promo[0].Rep )
delete promo[0].Rep;
promo[0].Rep = (ObjectType*)mbr;
promo[0].NObjects = currNode->GetTotalObjectCount();
promo[0].RootID = currNode->GetPageID();
resultAction = NO_ACT;
break;
case REMOVE_NODE:
currNode->RemoveEntry(idxSubTree);
if( currNode->GetNumberOfEntries() == 0) {
resultAction = REMOVE_NODE;
}
else {
tmpObj = new ObjectType();
tmpObj->Unserialize(currNode->GetObject(0), currNode->GetObjectSize(0));
mbr = tmpObj->Clone();
for (idx = 1; idx < currNode->GetNumberOfEntries(); idx++) {
tmpObj->Unserialize( currNode->GetObject(idx), currNode->GetObjectSize(idx));
mbr = mbr->GetUnionMBR( (stMBR*)tmpObj );
}
delete tmpObj;
promo[0].Rep = (ObjectType*)mbr;
promo[0].NObjects = currNode->GetTotalObjectCount();
promo[0].RootID = currNode->GetPageID();
resultAction = NO_ACT;
}
break;
case NOT_FOUND:
resultAction = NOT_FOUND;
}
}
else {
tmpObj = new ObjectType();
for (idx = 0; idx < currNode->GetNumberOfEntries(); idx++) {
tmpObj->Unserialize( currNode->GetObject(idx), currNode->GetObjectSize(idx));
if( tmpObj->IsEqual(obj) ) {
#ifdef __stDEBUG__
std::cout << "found!\n";
#endif
break;
}
}
if( idx < currNode->GetNumberOfEntries()) {
//found
currNode->RemoveEntry(idx);
if( currNode->GetNumberOfEntries() == 0) {
resultAction = REMOVE_NODE;
}
else {
tmpObj->Unserialize(currNode->GetObject(0), currNode->GetObjectSize(0));
mbr = tmpObj->Clone();
for (idx = 1; idx < currNode->GetNumberOfEntries(); idx++) {
tmpObj->Unserialize( currNode->GetObject(idx), currNode->GetObjectSize(idx));
mbr = mbr->GetUnionMBR( (stMBR*)tmpObj );
}
delete tmpObj;
promo[0].Rep = (ObjectType*)mbr;
promo[0].NObjects = currNode->GetTotalObjectCount();
promo[0].RootID = currNode->GetPageID();
resultAction = NO_ACT;
}
}
else {
//!found
resultAction = NOT_FOUND;
}
}
if(resultAction != NOT_FOUND)
this->myPageManager->WritePage(currPage);
delete currNode;
this->myPageManager->ReleasePage(currPage);
return resultAction;
}
template<class ObjectType>
stResult<ObjectType>* stRTree<ObjectType>::IntersectionQuery(tObject* data)
{
return NULL;
}
template<class ObjectType>
stResult<ObjectType>* stRTree<ObjectType>::NearestQuery(tObject * sample, stCount k, bool tie )
{
tResult * result = new tResult(); // Create result
ObjectType tmpObj;
stPage * currPage;
stRNode * currNode;
stDistance distance, distanceRepres;
stDistance rangeK = MAXDOUBLE;
stCount numberOfEntries, idx;
stCount idxRep;
stQueryPriorityQueueValue pqCurrValue;
stQueryPriorityQueueValue pqTmpValue;
tDynamicPriorityQueue * queue;
// Set information for this query
result->SetQueryInfo(sample->Clone(), tResult::KNEARESTQUERY, k, MAXDOUBLE, tie);
// Let's search
if (this->GetRoot() != 0){
// Create the Global Priority Queue.
queue = new tDynamicPriorityQueue(STARTVALUEQUEUE, INCREMENTVALUEQUEUE);
// Read node...
currPage = this->myPageManager->GetPage(this->GetRoot());
currNode = new stRNode(currPage);
// Get the number of entries
numberOfEntries = currNode->GetNumberOfEntries();
// Rebuild the object
//tmpObj.Unserialize(HeaderInfo->TreeMBR->Serialize(),
// HeaderInfo->TreeMBR->GetSerializedSize());
// Evaluate the distance.
distanceRepres = MAXDOUBLE;
// Is there any free objects in this node?
if (currNode->GetNumberOfFreeObjects() > 0){
// Now do it all free objects.
for (idx = 0; idx < numberOfEntries; idx++) {
// tests if this is a subtree.
if (!currNode->GetEntry(idx).PageID) {
// use of the triangle inequality to cut a subtree
//if (fabs(distanceRepres - currNode->GetEntry(idx).Distance) <= rangeK){
// Rebuild the object
tmpObj.Unserialize(currNode->GetObject(idx),
currNode->GetObjectSize(idx));
// is it a Representative?
//if (currNode->GetEntry(idx).Distance != 0) {
// No, it is not a representative. Evaluate distance
distance = tmpObj.GetMinDist(sample);
//}else{
// distance = distanceRepres;
//}//end if
if (distance <= rangeK){
// No, there is not a subtree. But this object qualifies.
// Add the object.
result->AddPair(tmpObj.Clone(), distance);
// there is more than k elements?
if (result->GetNumOfEntries() >= k){
//cut if there is more than k elements
result->Cut(k);
//may I use this for performance?
rangeK = result->GetMaximumDistance();
}//end if
}//end if
//}//end if
}//end if
}//end for
}//end if
// Now do it for subtrees.
for (idx = 0; idx < numberOfEntries; idx++) {
// tests if this is a subtree.
if (currNode->GetEntry(idx).PageID){
// use of the triangle inequality to cut a subtree
//if (fabs(distanceRepres - currNode->GetEntry(idx).Distance) <=
// rangeK + currNode->GetRadius(idx)){
// Rebuild the object
tmpObj.Unserialize(currNode->GetObject(idx), currNode->GetObjectSize(idx));
// is it a Representative?
if (idx != idxRep) {
// No, it is not a representative. Evaluate distance
distance = tmpObj.GetMinDist( sample);
}else{
distance = distanceRepres;
}//end if
if (distance <= rangeK /*+ currNode->GetRadius(idx)*/){
// Yes! I'm qualified! Put it in the queue.
pqTmpValue.PageID = currNode->GetEntry(idx).PageID;
//pqTmpValue.Radius = currNode->GetRadius(idx);
// this is a subtree, put in the queue.
queue->Add(distance, pqTmpValue);
}//end if
//}//end if
}//end if
}//end for
// Free it all
delete currNode;
this->myPageManager->ReleasePage(currPage);
// If there is something in the queue.
if (queue->GetSize() > 0){
this->NearestQuery(result, sample, rangeK, k, queue);
}//end if
// Release the Global Priority Queue.
delete queue;
}//end if
return result;
}
template <class ObjectType>
void stRTree<ObjectType>::NearestQuery(tResult * result,
ObjectType * sample, stDistance rangeK, stCount k,
tDynamicPriorityQueue * queue){
stCount idx;
stPage * currPage;
stRNode * currNode;
ObjectType tmpObj;
stDistance distance;
stDistance distanceRepres = 0;
stCount numberOfEntries;
stQueryPriorityQueueValue pqCurrValue;
stQueryPriorityQueueValue pqTmpValue;
bool stop;
queue->Get(distance, pqCurrValue);
distanceRepres = distance;
stDistance dOkQ;
tObject h;
int kIndex = -1;
// Let's search
while (pqCurrValue.PageID != 0){
// Read node...
currPage = this->myPageManager->GetPage(pqCurrValue.PageID);
currNode = new stRNode(currPage);
// Get the number of entries
numberOfEntries = currNode->GetNumberOfEntries();
//Reset values for Lemma1
dOkQ = -1.0f;
kIndex = -1;
// Is there any free objects in this node?
if (currNode->GetNumberOfFreeObjects() > 0){
// Now do it all free objects.
for (idx = 0; idx < numberOfEntries; idx++) {
// tests if this is a subtree.
if (!currNode->GetEntry(idx).PageID){
tmpObj.Unserialize(currNode->GetObject(idx),
currNode->GetObjectSize(idx));
distance = tmpObj.GetMinDist(sample);
if (distance <= rangeK) {
// No, there is not a subtree. But this object qualifies.
// Add the object.
result->AddPair(tmpObj.Clone(), distance);
// there is more than k elements?
if (result->GetNumOfEntries() >= k){
//cut if there is more than k elements
result->Cut(k);
//may I use this for performance?
rangeK = result->GetMaximumDistance();
}//end if
}//end if
}//end if
}//end for
}//end if
// Now do it for subtrees.
for (idx = 0; idx < numberOfEntries; idx++) {
// tests if this is a subtree.
if (currNode->GetEntry(idx).PageID){
tmpObj.Unserialize(currNode->GetObject(idx), currNode->GetObjectSize(idx));
distance = tmpObj.GetMinDist(sample);
if (distance <= rangeK /*+ currNode->GetRadius(idx)*/){
pqTmpValue.PageID = currNode->GetEntry(idx).PageID;
queue->Add(distance, pqTmpValue);
}//end if
}//end if
}//end for
// Free it all
delete currNode;
this->myPageManager->ReleasePage(currPage);
// Go to next node
stop = false;
do{
if (queue->Get(distance, pqCurrValue)){
// Qualified if distance <= rangeK
if (distance <= rangeK ){
// Yes, get the pageID and the distance from the representative
// and the query object.
distanceRepres = distance;
// Break the while.
stop = true;
}//end if
}else{
// the queue is empty!
pqCurrValue.PageID = 0;
// Break the while.
stop = true;
}//end if
}while (!stop);
}// end while