Back to index

nordugrid-arc-nox  1.1.0~rc6
DataPointIndex.cpp
Go to the documentation of this file.
00001 // -*- indent-tabs-mode: nil -*-
00002 
00003 #ifdef HAVE_CONFIG_H
00004 #include <config.h>
00005 #endif
00006 
00007 #include <list>
00008 
00009 #include <arc/Logger.h>
00010 #include <arc/data/DataPointIndex.h>
00011 
00012 namespace Arc {
00013 
00014   DataPointIndex::DataPointIndex(const URL& url, const UserConfig& usercfg)
00015     : DataPoint(url, usercfg),
00016       registered(false),
00017       h(NULL) {
00018     location = locations.end();
00019   }
00020 
00021   DataPointIndex::~DataPointIndex() {
00022     if (h)
00023       delete h;
00024   }
00025 
00026   const URL& DataPointIndex::CurrentLocation() const {
00027     static const URL empty;
00028     if (locations.end() == location)
00029       return empty;
00030     return *location;
00031   }
00032 
00033   const std::string& DataPointIndex::CurrentLocationMetadata() const {
00034     static const std::string empty;
00035     if (locations.end() == location)
00036       return empty;
00037     return location->Name();
00038   }
00039 
00040   DataStatus DataPointIndex::CompareLocationMetadata() const {
00041     if (h && *h) {
00042       DataStatus res = (*h)->Check();
00043       if (!res.Passed())
00044         return res;
00045       if (!CompareMeta(*(*h)))
00046         return DataStatus::InconsistentMetadataError;
00047     }
00048     return DataStatus::Success;
00049   }  
00050     
00051   bool DataPointIndex::HaveLocations() const {
00052     return (locations.size() != 0);
00053   }
00054 
00055   bool DataPointIndex::LocationValid() const {
00056     if (triesleft <= 0)
00057       return false;
00058     if (locations.end() == location)
00059       return false;
00060     return true;
00061   }
00062 
00063   void DataPointIndex::SetHandle(void) {
00064     // TODO: pass various options from old handler to new
00065     if (h)
00066       delete h;
00067     if (locations.end() != location)
00068       h = new DataHandle(*location, usercfg);
00069     else
00070       h = NULL;
00071   }
00072 
00073   bool DataPointIndex::NextLocation() {
00074     if (!LocationValid()) {
00075       --triesleft;
00076       return false;
00077     }
00078     ++location;
00079     if (locations.end() == location)
00080       if (--triesleft > 0)
00081         location = locations.begin();
00082     SetHandle();
00083     return LocationValid();
00084   }
00085 
00086   DataStatus DataPointIndex::RemoveLocation() {
00087     if (locations.end() == location)
00088       return DataStatus::NoLocationError;
00089     location = locations.erase(location);
00090     if (locations.end() == location)
00091       location = locations.begin();
00092     SetHandle();
00093     return DataStatus::Success;
00094   }
00095 
00096   DataStatus DataPointIndex::RemoveLocations(const DataPoint& p_) {
00097     if (!p_.IsIndex())
00098       return DataStatus::Success;
00099     const DataPointIndex& p = dynamic_cast<const DataPointIndex&>(p_);
00100     std::list<URLLocation>::iterator p_int;
00101     std::list<URLLocation>::const_iterator p_ext;
00102     for (p_ext = p.locations.begin(); p_ext != p.locations.end(); ++p_ext)
00103       for (p_int = locations.begin(); p_int != locations.end();)
00104         // Compare URLs
00105         if (*p_int == *p_ext)
00106           if (location == p_int) {
00107             p_int = locations.erase(p_int);
00108             location = p_int;
00109           }
00110           else
00111             p_int = locations.erase(p_int);
00112         else
00113           ++p_int;
00114     if (locations.end() == location)
00115       location = locations.begin();
00116     SetHandle();
00117     return DataStatus::Success;
00118   }
00119 
00120   DataStatus DataPointIndex::AddLocation(const URL& url,
00121                                          const std::string& meta) {
00122     logger.msg(DEBUG, "Add location: url: %s", url.str());
00123     logger.msg(DEBUG, "Add location: metadata: %s", meta);
00124     for (std::list<URLLocation>::iterator i = locations.begin();
00125          i != locations.end(); ++i)
00126       if ((i->Name() == meta) && (url == (*i)))
00127         return DataStatus::LocationAlreadyExistsError;
00128     locations.push_back(URLLocation(url, meta));
00129     if(locations.end() == location) {
00130       location = locations.begin();
00131       SetHandle();
00132     }
00133     return DataStatus::Success;
00134   }
00135 
00136   void DataPointIndex::SetTries(const int n) {
00137     triesleft = std::max(0, n);
00138     if (triesleft == 0)
00139       location = locations.end();
00140     else if (locations.end() == location)
00141       location = locations.begin();
00142     SetHandle();
00143   }
00144 
00145   bool DataPointIndex::IsIndex() const {
00146     return true;
00147   }
00148 
00149   bool DataPointIndex::AcceptsMeta() {
00150     return true;
00151   }
00152 
00153   bool DataPointIndex::ProvidesMeta() {
00154     return true;
00155   }
00156 
00157   bool DataPointIndex::Registered() const {
00158     return registered;
00159   }
00160 
00161   DataStatus DataPointIndex::StartReading(DataBuffer& buffer) {
00162     if (!h || !*h)
00163       return DataStatus::NoLocationError;
00164     return (*h)->StartReading(buffer);
00165   }
00166 
00167   DataStatus DataPointIndex::StartWriting(DataBuffer& buffer,
00168                                           DataCallback *cb) {
00169     if (!h || !*h)
00170       return DataStatus::NoLocationError;
00171     return (*h)->StartWriting(buffer, cb);
00172   }
00173 
00174   DataStatus DataPointIndex::StopReading() {
00175     if (!h || !*h)
00176       return DataStatus::NoLocationError;
00177     return (*h)->StopReading();
00178   }
00179 
00180   DataStatus DataPointIndex::StopWriting() {
00181     if (!h || !*h)
00182       return DataStatus::NoLocationError;
00183     return (*h)->StopWriting();
00184   }
00185 
00186   DataStatus DataPointIndex::Check() {
00187     if (!h || !*h)
00188       return DataStatus::NoLocationError;
00189     return (*h)->Check();
00190   }
00191 
00192   long long int DataPointIndex::BufSize() const {
00193     if (!h || !*h)
00194       return -1;
00195     return (*h)->BufSize();
00196   }
00197 
00198   int DataPointIndex::BufNum() const {
00199     if (!h || !*h)
00200       return 1;
00201     return (*h)->BufNum();
00202   }
00203 
00204   bool DataPointIndex::Local() const {
00205     if (!h || !*h)
00206       return false;
00207     return (*h)->Local();
00208   }
00209 
00210   bool DataPointIndex::ReadOnly() const {
00211     if (!h || !*h)
00212       return true;
00213     return (*h)->ReadOnly();
00214   }
00215 
00216   DataStatus DataPointIndex::Remove() {
00217     if (!h || !*h)
00218       return DataStatus::NoLocationError;
00219     return (*h)->Remove();
00220   }
00221 
00222   void DataPointIndex::ReadOutOfOrder(bool v) {
00223     if (h && *h)
00224       (*h)->ReadOutOfOrder(v);
00225   }
00226 
00227   bool DataPointIndex::WriteOutOfOrder() {
00228     if (!h || !*h)
00229       return false;
00230     return (*h)->WriteOutOfOrder();
00231   }
00232 
00233   void DataPointIndex::SetAdditionalChecks(bool v) {
00234     if (h && *h)
00235       (*h)->SetAdditionalChecks(v);
00236   }
00237 
00238   bool DataPointIndex::GetAdditionalChecks() const {
00239     if (!h || !*h)
00240       return false;
00241     return (*h)->GetAdditionalChecks();
00242   }
00243 
00244   void DataPointIndex::SetSecure(bool v) {
00245     if (h && *h)
00246       (*h)->SetSecure(v);
00247   }
00248 
00249   bool DataPointIndex::GetSecure() const {
00250     if (!h || !*h)
00251       return false;
00252     return (*h)->GetSecure();
00253   }
00254 
00255   void DataPointIndex::Passive(bool v) {
00256     if (h && *h)
00257       (*h)->Passive(v);
00258   }
00259 
00260   void DataPointIndex::Range(unsigned long long int start,
00261                              unsigned long long int end) {
00262     if (h && *h)
00263       (*h)->Range(start, end);
00264   }
00265 
00266 } // namespace Arc