-
Notifications
You must be signed in to change notification settings - Fork 0
/
MStarTree.h
2309 lines (2014 loc) · 72.3 KB
/
MStarTree.h
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
#ifndef __stMStarTree_h__
#define __stMStarTree_h__
#include <algorithm>
#include <cstdlib>
#include <cassert>
#include <deque>
#include <iostream>
#include "arboretum/stUtil.h"
#include "arboretum/stTypes.h"
#include "arboretum/stMetricTree.h"
#include "arboretum/stPageManager.h"
#include "arboretum/stGenericPriorityQueue.h"
#include "arboretum/stDBMCollection.h"
#include "arboretum/stDBMNode.h "
// A <- PageSize
// B <- ObjectSize
// C <- EntrySize
// D <- MStarNodeHeadSize
#define MSTAR_ORDER(A, B) ( floor(((A-NODE_HEAD_SIZE)/double(ENTRY_SIZE+B))) - 1)
#define max(a,b) (((a) > (b)) ? (a) : (b))
#define STARTVALUEQUEUE 200
#define INCREMENTVALUEQUEUE 200
#include "MStarNode.h"
#include "stMStarLogicNode.h"
#include "stMStarMSTSplitter.h"
//=============================================================================
// Class template stMStarTree
//-----------------------------------------------------------------------------
/**
* @author Alexander Ocsa M. ([email protected])
* @todo More documentation.??
* @todo Finish the implementation.??
* @version 1.0
*/
template<class ObjectType, class EvaluatorType>
class stMStarTree : public stMetricTree<ObjectType, EvaluatorType>{
public:
typedef struct stSubtreeInfo{
ObjectType * Rep;
stDistance dOrepOnew;
stDistance Radius;
stPageID RootID;
stCount NObjects;
stCount NEntries;
} tSubtreeInfo;
private:
struct stMStarHeader{
int SplitMethod;
int ChooseMethod;
int idxRoot;
stDistance TreeRadius;
stPageID Root;
/**
* Minimum number of objects in a node.
*/
stCount MaxOccupation;
/**
* Minimum percentage of objects in a node.
*/
double MinOccupation;
stCount ObjectCount;
stSize NodeCount;
/**
* If true, the reinsertion of object is done.
*/
bool ReInsertObjects;
/**
* If true, remove farthest object to put new ones is perfomed.
*/
bool RemoveFarthestObject;
/**
* The maximum number of promoted representatives.
*/
stCount NumberOfClusters;
/**
* The height of the MSTAR-tree
*/
stCount Height;
stSize Order;
};
public:
/**
* These constants are used to define the choose sub tree method.
*/
enum tChooseMethod{
MINGDIST,
cmMINDIST,
cmMINDISTWITHCOVER,
/**
* Density Based Hybrid ChooseSubTree.
*/
cmDBHCS
};
enum tSplitMethod{
smMIN_MAX,
smN_SPLIT,
MST_SLIM
};
/**
* This is the class that abstracs an result set.
*/
typedef stResult <ObjectType> tResult;
typedef ObjectType tObject;
/**
* This type is a memory node.
*/
//typedef stMSTARMemNode < ObjectType > tMSTARMemNode;
stMStarTree(stPageManager* pageman);
virtual ~stMStarTree();
virtual bool Add(tObject* obj);
virtual stCount GetHeight(){
return m_Header->Height;
}
/**
* Returns the radius of the tree.
*/
stDistance GetTreeRadius(){
return m_Header->TreeRadius;
}//end GetTreeRadius
/**
* Returns if the split method has already been called.
*/
bool IsMinOccupationMapped(){
return m_MinOccupationMapped;
}//end IsMinOccupationMapped
/**
* Returns the MaxOccupation of the nodes.
*/
stCount GetMaxOccupation(){
return MSTAR_ORDER(PAGE_SIZE, OBJECT_SIZE);
}//end GetMaxOccupation
/**
* Returns the MaxOccupation of the nodes.
*/
void SetMaxOccupation(stCount newValue){
if (newValue > m_Header->MaxOccupation){
m_Header->MaxOccupation = newValue;
}//end if
}//end SetMaxOccupation
/**
* Returns the MinOccupation of the nodes.
*/
virtual double GetMinOccupation(){
return m_Header->MinOccupation;
}//end GetMinOccupation
/**
* Sets the MinOccupation of the node.
*/
virtual void SetMinOccupation(double MinOccup){
m_Header->MinOccupation = MinOccup;
// the occupation has already mapped.
this->m_MinOccupationMapped = true;
m_HeaderUpdate = true;
}//end SetMinOccupation
virtual long GetNodeCount(){
return m_Header->NodeCount;
}
/**
* Returns the number of objetcs of this tree.
*
* @return The number of objects indexed by the tree or -1 if this information is not available.
*/
long GetNumberOfObjects(){
return m_Header->ObjectCount;
}//end GetNumberOfObjects
void SetSplitMethod(enum tSplitMethod method){
m_Header->SplitMethod = method;
m_HeaderUpdate = true;
}
int GetSplitMethod(){
return m_Header->SplitMethod;
}
void SetChooseMethod(enum tChooseMethod method){
m_Header->ChooseMethod = method;
m_HeaderUpdate = true;
}
int GetChooseMethod(){
return m_Header->ChooseMethod;
}
stCount GetNumberOfClusters(){
return m_Header->NumberOfClusters;
}
bool IsRemoveFarthest(){
return m_Header->RemoveFarthestObject;
}//end IsRemoveFarthest
bool IsReInsert(){
return m_Header->ReInsertObjects;
}//end IsReInsert
void WriteHeader() {
if(m_HeaderUpdate == true) {
this->myPageManager->WriteHeaderPage(m_HeaderPage);
m_HeaderUpdate = false;
}
}
virtual tResult * RangeQuery(tObject * sample, stDistance range);
virtual tResult * NearestQuery(tObject * sample, stCount k, bool tie = false);
/*virtual tResult * PointQuery(tObject * sample);
virtual bool Delete(tObject * obj);*/
virtual void Optimize() {
}
void SetReInsert(bool flag) {
m_Header->ReInsertObjects = flag;
m_HeaderUpdate = true;
}
void SetRemoveFarthest(bool flag) {
m_Header->RemoveFarthestObject = flag;
m_HeaderUpdate = true;
}
void Show() {
Show(GetRoot(), 0);
}
void Show(stPageID pageID, stCount level);
bool CheckNode(stPageID pageID);
bool CheckAllDistances() {
//printf("first, %d \n", GetRoot());
return CheckAllDistances( m_Header->TreeRadius, GetRoot());
}
double GetDistanceLimit(){
if(CheckAllDistances()) {
return 10.0;
}
else {
return -1.0;
}
}
bool CheckDistribution(stPageID pageID);
bool CheckDistribution(stMStarNode* node);
bool CheckAllDistances(stDistance radius, stPageID pageID);
private:
void LoadHeader();
void DefaultHeader();
int InsertRecursive(stPageID currNodeID, tSubtreeInfo& newSubTree,
ObjectType * repObj, tSubtreeInfo & farthest,
tSubtreeInfo * promo);
stPageID GetRoot(){
return m_Header->Root;
}
void SetRoot(stPageID pageID){
m_Header->Root = pageID;
}
void UpdateObjectCounter(int inc){
m_Header->ObjectCount += inc;
m_HeaderUpdate = true;
}
stDistance Infinite() {
return MAXDOUBLE;
}
stSize GetOrder() {
return m_Header->Order;
}
stPage* NewPage() {
++m_Header->NodeCount;
return this->myPageManager->GetNewPage();
}
bool RemoveFarthest(stMStarNode* currNode, tSubtreeInfo& newSubTree,tSubtreeInfo& farthest);
void ReInsert(stMStarNode* node);
int ChooseSubTree(stMStarNode* node, tSubtreeInfo& newSubTree);
void AddNewRoot(tSubtreeInfo* promo);
void Split(stMStarNode* node, tSubtreeInfo* promo);
/**
* Updates the distances of the objects from the new representative.
*/
void UpdateDistances(stMStarNode* node, ObjectType * repObj, int repObjIdx);
/**
* Updates the distances of the objects from the new representative.
*/
stCount ChooseRepresentative(stMStarNode* node);
void PrintNode(stMStarNode* node);
private:
/**
* This type defines the logic node for this class.
*/
typedef stMStarLogicNode < ObjectType, EvaluatorType > tLogicNode;
/**
* This type defines the MST splitter for this class.
*/
typedef stMStarSplitter < ObjectType, EvaluatorType > tMSTSplitter;
/**
* This type defines the MST splitter for this class.
*/
// typedef stMSTARNSplitter < ObjectType, EvaluatorType > tMSTARNSplitter;
typedef stDBMMemNode < ObjectType > tDBMMemNode;
typedef stDBMCollection<ObjectType> tDBMCollection;
typedef stRPriorityQueue < stDistance, stCount > tPriorityQueue;
typedef stDynamicRPriorityQueue < stDistance, stQueryPriorityQueueValue > tDynamicPriorityQueue;
typedef stGenericPriorityHeap < ObjectType > tPGenericHeap;
enum stInsertAction{
/**
* No action required. Just update the radius.
*/
NO_ACT,
/**
* Replace representative.
*/
CHANGE_REP,
/**
* Split occured. Update subtrees.
*/
PROMOTION,
/**
* Farthest removed.
*/
REMOVE_FARTHEST,
/**
* The node was removed.
*/
REMOVED_NODE
};//end stInsertAction
enum stCloseness {
DBHCS_HIGH,
DBHCS_MIDDLE,
DBHCS_VERY_LOW
};
/**
* If true, the split method has already been called.
*/
bool m_MinOccupationMapped;
/**
* If true, the m_Header mus be written to the page manager.
*/
bool m_HeaderUpdate;
/**
* The MStarTree m_Header. This variable points to data in the HeaderPage.
*/
stMStarHeader * m_Header;
stPage * m_HeaderPage;
tDBMCollection* returnCollection;
tDBMCollection* reInsertCollection;
/**
* Updates all distances between representatives and all objects in this
* node. It returns the number of distances calculated.
*
* @param metricEvaluator The metric evaluator to be used.
*/
stCount UpdateDistances(EvaluatorType * metricEvaluator);
void MinMaxRadiusPromoteSlim(tLogicNode * node) ;
void UpdateDCMatrix(stMStarNode* node, stCount idxReplacedObj);
void UpdateDCMatrix(stMStarNode* node);
bool CheckLemma1(stMStarNode* node,const stCount &i, int &k, stDistance &dOrepQ, stDistance & radius, stDistance& dOkQ, ObjectType* query);
void RangeQuery(stPageID currPageID, ObjectType* sample, stDistance &range, stDistance& dOrepOj, tResult *&queryResult);
void NearestQuery(tResult * result,
ObjectType * sample, stDistance rangeK, stCount k,
tDynamicPriorityQueue * queue);
};
//////////////////////////////////////////////////////////////////////////
template<class ObjectType, class EvaluatorType>
stMStarTree<ObjectType, EvaluatorType>::stMStarTree(stPageManager* pageman)
: stMetricTree<ObjectType, EvaluatorType>(pageman)
{
m_Header = NULL;
m_HeaderPage = NULL;
LoadHeader();
if(this->myPageManager->IsEmpty()) {
DefaultHeader();
}
// Allocate the collections.
this->returnCollection = new tDBMCollection();
if(this->IsReInsert()) {
this->reInsertCollection = new tDBMCollection();
}
else {
this->reInsertCollection = NULL;
}
}
template<class ObjectType, class EvaluatorType>
void stMStarTree<ObjectType, EvaluatorType>::LoadHeader()
{
if(m_HeaderPage != NULL)
this->myPageManager->ReleasePage(m_HeaderPage);
m_HeaderPage = this->myPageManager->GetHeaderPage();
if( m_HeaderPage->GetPageSize() <= sizeof(stMStarHeader))
throw page_size_error("The page size id too small.");
m_Header = (stMStarHeader *)m_HeaderPage->GetData();
m_HeaderUpdate = false;
}
template<class ObjectType, class EvaluatorType>
void stMStarTree<ObjectType, EvaluatorType>::DefaultHeader()
{
m_HeaderPage->Clear();
m_Header->SplitMethod = smMIN_MAX;
m_Header->ChooseMethod = stMStarTree::cmDBHCS;
//m_Header->ChooseMethod = cmMINDISTWITHCOVER;
this->SetReInsert(false);
this->SetRemoveFarthest(false);
m_Header->Root = 0;
m_Header->MinOccupation = 0.25;
m_Header->Height = 0;
m_Header->ObjectCount = 0;
m_Header->TreeRadius = 0.0;
m_Header->idxRoot = -1;
m_Header->NumberOfClusters = 2;
// Notify modifications
m_HeaderUpdate = true;
}
template<class ObjectType, class EvaluatorType>
stMStarTree<ObjectType, EvaluatorType>::~stMStarTree()
{
if(m_HeaderPage != NULL){
if(m_Header != NULL)
WriteHeader();
this->myPageManager->ReleasePage(m_HeaderPage);
}
delete this->returnCollection;
if( this->IsReInsert() ) {
delete this->reInsertCollection;
}
}
template<class ObjectType, class EvaluatorType>
bool stMStarTree<ObjectType, EvaluatorType>::Add(tObject* obj)
{
#ifdef __stDEBUB__
printf("**enter, add, rootID = %d \n", GetRoot());
cout << *obj << endl;
#endif
tSubtreeInfo * promo;
tSubtreeInfo newSubTree;
tSubtreeInfo farthest;
stMStarNode* rootNode;
stPage* rootPage;
int insertIdx;
int idxPromo, idxReturn;
if(GetRoot() == 0) {
m_Header->Order = MSTAR_ORDER(PAGE_SIZE, obj->GetSerializedSize()); // Set order of the tree
//printf("order tree: %d\n", m_Header->Order);
rootPage = this->NewPage();
rootNode = new stMStarNode(rootPage, m_Header->Order);
insertIdx = rootNode->AddEntry(obj->GetSerializedSize(), obj->Serialize(), 0);
#ifdef __stDEBUB__
if ( insertIdx < 0) {
throw page_size_error("The page size is too small to store the first object");
}
#endif //__stDEBUB__
rootNode->GetEntry(insertIdx).Distance = 0.0;
rootNode->SetRadius(insertIdx, 0);
rootNode->SetNEntries(insertIdx, 0);
this->SetRoot(rootPage->GetPageID());
m_Header->idxRoot = 0;
m_Header->Height = 1; // Update Height
m_Header->TreeRadius = 0.0;
m_Header->idxRoot = 0;
this->myPageManager->WritePage(rootPage);
delete rootNode;
this->myPageManager->ReleasePage(rootPage);
#ifdef __stDEBUB__
printf("first object\n");
#endif
}
else {
newSubTree.Rep = obj;
newSubTree.Radius = 0.0;
newSubTree.RootID = 0;
newSubTree.NObjects = 0;
//newSubTree.Height = 0;
newSubTree.dOrepOnew = 0.0;
promo = new tSubtreeInfo[GetNumberOfClusters()];
for (idxPromo = 0; idxPromo < GetNumberOfClusters(); idxPromo++){
promo[idxPromo].Rep = NULL;
}
#ifdef __stDEBUB__
printf("recursive insert\n");
#endif
if (this->InsertRecursive(this->GetRoot(), newSubTree, NULL, farthest, promo) == PROMOTION){
#ifdef __stDEBUB__
printf("add new root\n");
#endif
this->AddNewRoot(promo);
}
if( this->IsReInsert() ) {
idxReturn = reInsertCollection->GetNumberOfEntries();
// While there is object...
while (idxReturn > 0) {
newSubTree.Rep = (* reInsertCollection)[idxReturn-1]->GetObject();
newSubTree.Radius = (* reInsertCollection)[idxReturn-1]->GetRadius();
newSubTree.RootID = (* reInsertCollection)[idxReturn-1]->GetPageID();
// newSubTree.NObjects= (* reInsertCollection)[idxReturn-1]->GetNEntries();
(* reInsertCollection)[idxReturn-1]->SetMine(false);
reInsertCollection->RemoveLast();
#ifdef __stDEBUB__
cout << "reinsert object\n";
CheckNode(GetRoot());
#endif
if (InsertRecursive(this->GetRoot(), newSubTree, NULL, farthest, promo) == PROMOTION){
// Split occurred! We must create a new root because it is required.
// The tree will acquire a new root.
AddNewRoot(promo);
}//end if
// Clean.
delete newSubTree.Rep;
// update the number of entries.
idxReturn = reInsertCollection->GetNumberOfEntries();
}//end while
}//end if
m_Header->TreeRadius = promo[0].Radius;
delete[] promo;
}
#ifdef __stDEBUB__
cout << "edn_add::root_node\n";
CheckNode(GetRoot());
#endif
UpdateObjectCounter(1);
m_HeaderUpdate = true;
return true;
}
template<class ObjectType, class EvaluatorType>
int stMStarTree<ObjectType, EvaluatorType>::InsertRecursive( stPageID currNodeID,
tSubtreeInfo& newSubTree,
ObjectType * repObj,
tSubtreeInfo & farthest,
tSubtreeInfo * promo)
{
int result;
int insertIdx;
int subTree;
stPage* currPage;
stMStarNode* currNode;
stCount idx;
currPage = this->myPageManager->GetPage( currNodeID );
currNode = new stMStarNode(currPage);
subTree = ChooseSubTree(currNode, newSubTree);
if (subTree >= 0) {
ObjectType* subRep = new ObjectType();
subRep->Unserialize( currNode->GetObject(subTree), currNode->GetObjectSize(subTree));
// Is this a SubTree?
if ( currNode->IsSubTree(subTree) ) {
//Yes, It is a SubTree Entry
#ifdef __stDEBUB__
printf("recursive insert\n");
#endif
//cout << "before, insertRecursive: subtree = " << subRep->GetStr() << "\n";
switch( InsertRecursive( currNode->GetEntry(subTree).PageID, newSubTree, subRep, farthest, promo) ) {
case NO_ACT:
#ifdef __stDEBUB__
printf("no act\n");
#endif
currNode->SetRadius(subTree, promo[0].Radius);
currNode->SetNEntries(subTree, promo[0].NObjects);
this->ReInsert(currNode);
promo[0].Radius = currNode->GetMinimumRadius();
promo[0].NObjects = currNode->GetTotalObjectCount();
#ifdef __stDEBUB__
CheckNode(currNodeID);
#endif
result = NO_ACT;
break;
case PROMOTION:
#ifdef __stDEBUB__
printf("promotion\n");
cout << " *****currSubTree: " << subTree << "\n";
cout << " -before promotion\n";
CheckDistribution(currNode);
#endif
//stDistance distanceOjOidxRoot;
idx = 1;
bool stop;
currNode->SetEntry( promo[0].Rep->Serialize(),
promo[0].Rep->GetSerializedSize(),
promo[0].RootID,
subTree );
currNode->GetEntry(subTree).Distance = -1.0; //in this moment has invalid distance
currNode->SetRadius(subTree, promo[0].Radius);
currNode->SetNEntries(subTree, promo[0].NObjects);
delete promo[0].Rep;
promo[0].Rep = NULL;
stop = (idx == GetNumberOfClusters());
// Add the promo object to the current node.
while (!stop) {
if (promo[idx].Rep) {
insertIdx = currNode->AddEntry( promo[idx].Rep->GetSerializedSize(),
promo[idx].Rep->Serialize(),
promo[idx].RootID);
///////// xQxQxQ?????????????????///////////////////////////////////////
UpdateDCMatrix(currNode, insertIdx);
if ( insertIdx >= 0 ) {
currNode->SetRadius(insertIdx, promo[idx].Radius);
currNode->SetNEntries(insertIdx, promo[idx].NObjects);
delete promo[idx].Rep;
promo[idx].Rep = NULL;
idx++;
}
else {
stop = true;
}
}
else {
idx++;
}
stop = (idx == GetNumberOfClusters()); // stop criteria
}
// Test if the current node is overflow
if ( !currNode->IsOverflow() ) {
// No, these node is not overflow
// Is the root node?
if (repObj != NULL) {
// No, it isn't the root node.
//Check, if the subRep is equal to the representative of this node
if (repObj->IsEqual(subRep)) {
// The representative was replaced.
// Choose a new representative and calculate the distances.
insertIdx = ChooseRepresentative(currNode);
promo[0].Rep = new ObjectType();
// Propagate the changes.
promo[0].Rep->Unserialize(currNode->GetObject(insertIdx),
currNode->GetObjectSize(insertIdx));
//UpdateDCMatrix(currNode, subTree);
// Report the action!
result = CHANGE_REP;
}
else {
UpdateDistances(currNode, repObj, currNode->GetRepresentativeIndex());
result =NO_ACT;
}
}
else {
// Yes, it is the root node.
m_Header->idxRoot = currNode->GetRepresentativeIndex();
if (m_Header->idxRoot >= 0) {
// the representative wasn´t replaced
repObj = new ObjectType();
repObj->Unserialize( currNode->GetObject(m_Header->idxRoot),
currNode->GetObjectSize(m_Header->idxRoot));
UpdateDistances(currNode, repObj, m_Header->idxRoot);
result = NO_ACT;
delete repObj;
repObj = NULL;
}
else {
// the representative was replaced
// Choose a new representative and calculate the distances.
m_Header->idxRoot = ChooseRepresentative(currNode);
repObj = new ObjectType();
repObj->Unserialize( currNode->GetObject(m_Header->idxRoot),
currNode->GetObjectSize(m_Header->idxRoot));
delete repObj;
repObj = NULL;
}
result = NO_ACT;
}
UpdateDCMatrix(currNode, subTree);
//currNode->ClearMatrix();
/////////////////////////
promo[0].Radius = currNode->GetMinimumRadius();
promo[0].RootID = currNode->GetPageID();
promo[0].NObjects = currNode->GetTotalObjectCount();
}
else {
// Yes, these node is overflow
/////////////////////////////////////////////////////////////////////
//promo[0].Rep = NULL;
//promo[0].Radius = currNode->GetMinimumRadius();
//promo[0].NObjects = currNode->GetTotalObjectCount();
//UpdateDistances(currNode, repObj, m_Header->idxRoot);
/////////////////////////////////////////////////////////////////////
//is the root node?
if( repObj != NULL) {
// no it is not the root node
/*if( repObj->IsEqual(subRep) ) {
//The representative object was replaced
UpdateDCMatrix(currNode, subTree);
}*/
}
else {
m_Header->idxRoot = currNode->GetRepresentativeIndex();
/*if (m_Header->idxRoot < 0) {
// The representative object was replaced
UpdateDCMatrix(currNode, subTree);
}*/
}//endif
//*-**********************************************
UpdateDCMatrix(currNode, subTree);
//currNode->ClearMatrix();
#ifdef __stDEBUB__
CheckNode(currNodeID);
#endif
Split(currNode, promo);
result = PROMOTION;
}
#ifdef __stDEBUB__
CheckNode(currNodeID);
cout << " -after promotion\n";
CheckDistribution(currNode);
#endif
break;
case CHANGE_REP: //lack check
//idx = 1;
//bool stop = (idx == GetNumberOfClusters());
#ifdef __stDEBUB__
printf("change rep\n");
cout << " *****currSubTree: " << subTree << "\n";
cout << " -before change_rep\n";
CheckDistribution(currNode);
#endif
currNode->SetEntry( promo[0].Rep->Serialize(),
promo[0].Rep->GetSerializedSize(),
promo[0].RootID,
subTree );
currNode->GetEntry(subTree).Distance = -1.0; //in this moment is invalid distance
currNode->SetRadius(subTree, promo[0].Radius);
currNode->SetNEntries(subTree, promo[0].NObjects);
//delete promo[0].Rep;
//promo[0].Rep = NULL;
// Is the root node?
if (repObj == NULL) {
// Yes, it´s the root node.
m_Header->idxRoot = currNode->GetRepresentativeIndex();
if (m_Header->idxRoot >= 0) {
// the representative wasn´t replaced
repObj = new ObjectType();
repObj->Unserialize( currNode->GetObject(m_Header->idxRoot),
currNode->GetObjectSize(m_Header->idxRoot));
UpdateDistances(currNode, repObj, m_Header->idxRoot);
delete repObj;
repObj = NULL;
}
else {
// the representative was replaced
// Choose a new representative and calculate the distances.
m_Header->idxRoot = ChooseRepresentative(currNode);
}
result = NO_ACT;
delete promo[0].Rep;
promo[0].Rep = NULL;
}
else {
//No, it isn´t the root node
if(repObj->IsEqual(subRep)) {
// The representative of this node was replaced
insertIdx = ChooseRepresentative(currNode);
promo[0].Rep->Unserialize(currNode->GetObject(insertIdx), currNode->GetObjectSize(insertIdx));
result = CHANGE_REP;
}
else {
UpdateDistances(currNode, repObj, currNode->GetRepresentativeIndex());
result = NO_ACT;
delete promo[0].Rep;
promo[0].Rep = NULL;
}
}
UpdateDCMatrix(currNode, subTree);
#ifdef __stDEBUB__
CheckNode(currNodeID);
cout << " -after change_rep\n";
CheckDistribution(currNode);
#endif
//assert(currNode->GetRepresentativeIndex() >= 0);
promo[0].Radius = currNode->GetMinimumRadius();
promo[0].RootID = currNode->GetPageID();
promo[0].NObjects = currNode->GetTotalObjectCount();
break;
}//end swhich
delete subRep;
subRep = NULL;
}
else {
newSubTree.dOrepOnew = 0.0;
// No, It isn´t a SubTree. This entry is a free Object.
// Create a new node and grown down the tree, with the new object.
stPage* newPage = this->NewPage();
stMStarNode* newNode = new stMStarNode(newPage, this->GetOrder() );
insertIdx = newNode->AddEntry(subRep->GetSerializedSize(),
subRep->Serialize(),
0 );
newNode->GetEntry(insertIdx).Distance = 0.0;
newNode->SetRadius(insertIdx, 0.0);
newNode->SetNEntries(insertIdx, 0);
// Add the new object
insertIdx = newNode->AddEntry(newSubTree.Rep->GetSerializedSize(),
newSubTree.Rep->Serialize(),
0);
newNode->GetEntry(insertIdx).Distance = this->myMetricEvaluator->GetDistance(subRep, newSubTree.Rep);
newNode->SetRadius(insertIdx, newSubTree.Radius);
newNode->SetNEntries(insertIdx, newSubTree.NObjects);
UpdateDCMatrix(newNode, insertIdx);
currNode->SetEntry( subRep->Serialize(),
subRep->GetSerializedSize(),
newNode->GetPageID(),
subTree);
currNode->GetEntry(subTree).Distance = -1.0; //in this moment is invalid distance
currNode->SetRadius(subTree, newNode->GetMinimumRadius());
currNode->SetNEntries(subTree, 2);
// Is the root node?
if ( repObj != NULL) {
// No, it isn´t the root node.
if( repObj->IsEqual(subRep) ) {
idx = this->ChooseRepresentative(currNode);
promo[0].Rep = new ObjectType;
promo[0].Rep->Unserialize( currNode->GetObject(idx),
currNode->GetObjectSize(idx));
result = CHANGE_REP;
}
else {
currNode->GetEntry(subTree).Distance = this->myMetricEvaluator->GetDistance(repObj, subRep );
result = NO_ACT;
}
}
else {
// Yes, it is the root node.
this->m_Header->idxRoot = currNode->GetRepresentativeIndex();
if( m_Header->idxRoot >= 0) {
repObj = new ObjectType;
repObj->Unserialize(currNode->GetObject(m_Header->idxRoot),
currNode->GetObjectSize(m_Header->idxRoot));
//***************************++ test..!!!!!!
//currNode->GetEntry(subTree).Distance = this->myMetricEvaluator->GetDistance(repObj, subRep);
UpdateDistances(currNode, repObj, m_Header->idxRoot);
delete repObj;
repObj = NULL;
}
else {
m_Header->idxRoot = ChooseRepresentative(currNode);
}
result = NO_ACT;
}
UpdateDCMatrix(currNode, subTree);
promo[0].Radius = currNode->GetMinimumRadius();
promo[0].RootID = currNode->GetPageID();
promo[0].NObjects = currNode->GetNumberOfEntries();
// Write node.
this->myPageManager->WritePage(newPage);
//clean the house
delete newNode;
this->myPageManager->ReleasePage(newPage);
}
}
else {
newSubTree.dOrepOnew = 0.0;
//No, there is a subTree that qualifies
//Insert in this node
#ifdef __stDEBUB__
cout << " -before add_entry\n";
//CheckNode(currNodeID);
CheckDistribution(currNode);
#endif
insertIdx = currNode->AddEntry( newSubTree.Rep->GetSerializedSize(),
newSubTree.Rep->Serialize(),
0);
//Is the root node?
if (repObj == NULL) {
// Yes, it´s the root node
repObj = new ObjectType();
repObj->Unserialize( currNode->GetObject(m_Header->idxRoot),
currNode->GetObjectSize(m_Header->idxRoot));
currNode->GetEntry(insertIdx).Distance = this->myMetricEvaluator->GetDistance(repObj, newSubTree.Rep);
delete repObj;
//cout << "repIdx: " << m_Header->idxRoot << endl;
repObj = NULL;
}
else {
//No, it isn´t the root node
currNode->GetEntry(insertIdx).Distance = this->myMetricEvaluator->GetDistance(repObj, newSubTree.Rep);
//(*currNode)(currNode->GetRepresentativeIndex(), insertIdx) = currNode->GetEntry(insertIdx).Distance;
//cout << "repIdx: " << currNode->GetRepresentativeIndex() << endl;
}
//cout << "insertIdx: " << insertIdx << endl;
UpdateDCMatrix(currNode, insertIdx);
//this->PrintNode(currNode);
currNode->SetRadius(insertIdx, 0.0);
currNode->SetNEntries(insertIdx, 0);
#ifdef __stDEBUB__
//CheckNode(currNodeID);
cout << " -after add_entry\n";
CheckDistribution(currNode);
#endif
if (currNode->IsOverflow()) {
// Is it possible to remove the farthest object?