Main Page Namespace List Class Hierarchy Compound List File List Namespace Members Compound Members File Members Related Pages

# DAGSearchDatabase.cpp

Go to the documentation of this file.

    00001
    00040 #include "DAGSearchDatabase.h"
    00041 #include "Debug.h"
    00042
    00043 DAGSearchDatabase::DAGSearchDatabase()
    00044 {
    00045         nnd.name = "";
    00046         nnd.data = HnSRTreeFile::null;
    00047         nnd.dimension = 0;
    00048 }
    00049
    00050 DAGSearchDatabase::~DAGSearchDatabase()
    00051 {
    00052   if(nnd.data!=HnSRTreeFile::null)
    00053     {
    00054       nnd.data.close();
    00055     }
    00056 }
    00057
    00058 void DAGSearchDatabase::Add(ID id, const DAG& dag)
    00059 {
    00060         leda_node v;
    00061         DAGSearchRecord record;
    00062         TSV h;
    00063         HnPoint pn;
    00064
    00065         //Check if database has been activated
    00066         if(nnd.data!=HnSRTreeFile::null)
    00067         {
    00068                 record.id = id;
    00069                 record.offset = dag.GetFileOffset();
    00070                 record.totalVote = dag.GetTotalTSVSum();
    00071
    00072                 //for each node in the dag class record its TSV to the NND
    00073                 forall_nodes(v, dag)
    00074             {
    00075                         if (dag.GetNodeTSVNorm(v) > 0)
    00076                         {
    00077                                 record.node = dag.GetNodeIndex(v);
    00078                                 record.nodeLbl = dag.NodeType(v);
    00079                                 h = dag.GetNodeTSV(v, nnd.dimension);
    00080                                 pn = TSVtoHnPoint(h);
    00081                                 nnd.data.store(pn, new_HnDataItem((void*)(&record),sizeof(DAGSearchRecord)));
    00082                 }
    00083                 }
    00084         }
    00085 }
    00086
    00087 void  DAGSearchDatabase::Remove(ID id, DAG& dag)
    00088 {
    00089         leda_node v;
    00090         TSV h;
    00091         HnPoint pn;
    00092
    00093         DAGSearchRecord record;
    00094         record.id = id;
    00095
    00096         //Check if database has been activated
    00097         if(nnd.data!=HnSRTreeFile::null)
    00098         {
    00099           forall_nodes(v, dag)
    00100           {
    00101                   record.node = dag.GetNodeIndex(v);
    00102                   h = dag.GetNodeTSV(v, nnd.dimension);
    00103                   pn = TSVtoHnPoint(h);
    00104
    00105                   // I am not sure if I should allocate mdemory for each search record
    00106                   nnd.data.remove(pn, new_HnDataItem((void*)(&record),sizeof(DAGSearchRecord)));
    00107           }
    00108         }
    00109 }
    00110
    00118 SmartArray<DAGSearchRecordEx> DAGSearchDatabase::GetKClosest(const DAG& dag, const leda_node& querynode, int k)
    00119 {
    00120         SmartArray<DAGSearchRecordEx> closestDAGs;
    00121
    00122         //Check if database has been activated
    00123         if(nnd.data != HnSRTreeFile::null && k >= 1)
    00124         {
    00125                 int i, size;
    00126                 HnPoint pp;
    00127                 TSV tsv;
    00128                 DAGSearchRecordEx sr;
    00129
    00130                 HnPointVector points;
    00131                 HnDataItemVector dataItems;
    00132
    00133                 tsv = dag.GetNodeTSV(querynode, nnd.dimension);
    00134
    00135                 pp = TSVtoHnPoint(tsv);
    00136
    00137                 nnd.data.getNeighbors(pp, k, &points, &dataItems);
    00138
    00139                 closestDAGs.ReSize(size = dataItems.size());
    00140
    00141                 for(i = 0; i < size; i++)
    00142                 {
    00143                         sr = *(DAGSearchRecord*)(dataItems.elementAt(i).toCharArray());
    00144                         sr.nodeTSV = HnPointToTSV(points.elementAt(i));
    00145                         closestDAGs.Add(sr);
    00146                 }
    00147         }
    00148
    00149         return closestDAGs;
    00150 }
    00151
    00152 SmartArray<DAGSearchRecordEx> DAGSearchDatabase::GetClosest(const DAG& dag, const leda_node& querynode,
    00153                                                                                                                         const double& range)
    00154 {
    00155         SmartArray<DAGSearchRecordEx> closestDAGs;
    00156
    00157         ASSERT(range >= 0 && range <= 1);
    00158
    00159         //Check if database has been activated
    00160         if(nnd.data != HnSRTreeFile::null)
    00161         {
    00162                 int i, size;
    00163                 double delta, val;
    00164                 HnRect rect;
    00165                 TSV tsv;
    00166                 DAGSearchRecordEx sr;
    00167
    00168                 HnPointVector points;
    00169                 HnDataItemVector dataItems;
    00170
    00171                 tsv = dag.GetNodeTSV(querynode, nnd.dimension);
    00172                 rect = new_HnRect(nnd.dimension);
    00173
    00174                 for(i = 0; i < tsv.GetSize(); i++)
    00175                 {
    00176                         val = tsv[i];
    00177                         delta = val * range;
    00178                         rect.setRangeAt(val - delta, val + delta, i);
    00179                 }
    00180
    00181                 nnd.data.getInRect(rect, &points, &dataItems);
    00182
    00183                 closestDAGs.ReSize(size = dataItems.size());
    00184
    00185                 for(i = 0; i < size; i++)
    00186                 {
    00187                         sr = *(DAGSearchRecord*)(dataItems.elementAt(i).toCharArray());
    00188                         sr.nodeTSV = HnPointToTSV(points.elementAt(i));
    00189                         closestDAGs.Add(sr);
    00190                 }
    00191         }
    00192
    00193         return closestDAGs;
    00194 }
    00195
    00196
    00197 bool DAGSearchDatabase::Create(std::string name, int dim)
    00198 {
    00199     //check if the dimensions are larger or equal to the minimal dimensions allowed
    00200     if(dim < Min_NND_Dimension) return false;
    00201
    00202     //Check if database has been activated
    00203         if(nnd.data==HnSRTreeFile::null)
    00204         {
    00205           //create the near neighbor database
    00206           nnd.data = new_HnSRTreeFile(name.c_str(), dim, sizeof(DAGSearchRecord), HnProperties::null);
    00207
    00208           //check if the database was created successful
    00209           if(nnd.data == HnSRTreeFile::null)
    00210             {
    00211               //return false if an error occured
    00212               return false;
    00213             }
    00214           else
    00215             {
    00216               nnd.name = name;
    00217               nnd.dimension = dim;
    00218             }
    00219         }
    00220         else
    00221         {
    00222           return false;
    00223         }
    00224         return true;
    00225 }
    00226
    00227 bool DAGSearchDatabase::Open(std::string name)
    00228 {
    00229   //close the indexing database
    00230   if(nnd.data!=HnSRTreeFile::null)
    00231   {
    00232     nnd.data.close();
    00233   }
    00234
    00235   nnd.data = new_HnSRTreeFile(name.c_str(), "rw");
    00236   if(nnd.data == HnSRTreeFile::null) return false;
    00237
    00238   nnd.name = name;
    00239   nnd.dimension = nnd.data.getDimension();
    00240   return true;
    00241 }
    00242
    00243 void DAGSearchDatabase::Close()
    00244 {
    00245         nnd.data.close();
    00246         nnd.data = HnSRTreeFile::null;
    00247 }
    00248
    00249 std::istream&  DAGSearchDatabase::Read(std::istream& is)
    00250 {
    00251   char* namestr;
    00252   size_t t;
    00253   std::string name;
    00254   int dim;
    00255
    00256   //close the indexing database
    00257   if(nnd.data!=HnSRTreeFile::null)
    00258   {
    00259     nnd.data.close();
    00260   }
    00261
    00262   is.read((char*)&t,sizeof(t));
    00263   namestr = new char[t];
    00264
    00265   is.read(namestr, t);
    00266   name = std::string(namestr);
    00267   delete [] namestr;
    00268
    00269   is.read((char*)&dim,sizeof(dim));
    00270
    00271   //load the near neighbor database
    00272   nnd.data = new_HnSRTreeFile(name.c_str(), "rw");
    00273
    00274   //check if the database was created successfuly
    00275   if(nnd.data == HnSRTreeFile::null)
    00276     {
    00277       //if error occured set stream state to fail and return it
    00278       is.setstate(ios::failbit);
    00279       return is;
    00280     }
    00281
    00282      nnd.name = name;
    00283      nnd.dimension = dim;
    00284
    00285      return is;
    00286 }
    00287
    00288 std::ostream&  DAGSearchDatabase::Write(std::ostream& os)
    00289 {
    00290   size_t t;
    00291
    00292   t = nnd.name.length() + 1;
    00293
    00294   os.write((char*)&t,sizeof(t));
    00295   os.write(nnd.name.c_str(), t);
    00296   os.write((char*)&nnd.dimension,sizeof(nnd.dimension));
    00297
    00298   return os;
    00299 }
    00300
    00301 HnPoint DAGSearchDatabase::TSVtoHnPoint(const TSV& tsv)
    00302 {
    00303         int d = tsv.GetSize();
    00304         HnPoint hp = new_HnPoint(d);
    00305
    00306         for(int i = 0; i < d; i++)
    00307                 hp.setCoordAt(tsv[i], i);
    00308
    00309         return hp;
    00310 }
    00311
    00312 TSV DAGSearchDatabase::HnPointToTSV(const HnPoint& pt)
    00313 {
    00314         int d = pt.getDimension();
    00315         TSV tsv(d);
    00316
    00317         for(int i = 0; i < d; i++)
    00318                 tsv[i] = pt.getCoordAt(i);
    00319
    00320         return tsv;
    00321 }