]>
Commit | Line | Data |
---|---|---|
9e1ceb13 | 1 | /************************************************************************** |
2 | * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. * | |
3 | * * | |
4 | * Author: The ALICE Off-line Project. * | |
5 | * Contributors are mentioned in the code where appropriate. * | |
6 | * * | |
7 | * Permission to use, copy, modify and distribute this software and its * | |
8 | * documentation strictly for non-commercial purposes is hereby granted * | |
9 | * without fee, provided that the above copyright notice appears in all * | |
10 | * copies and that both the copyright notice and this permission notice * | |
11 | * appear in the supporting documentation. The authors make no claims * | |
12 | * about the suitability of this software for any purpose. It is * | |
13 | * provided "as is" without express or implied warranty. * | |
14 | **************************************************************************/ | |
15 | ||
16 | ///////////////////////////////////////////////////////////////////////////////////////////////// | |
17 | // // | |
18 | // AliCDBGrid // | |
19 | // access class to a DataBase in an AliEn storage // | |
20 | // // | |
21 | ///////////////////////////////////////////////////////////////////////////////////////////////// | |
22 | ||
b09247a2 | 23 | #include <cstdlib> |
9e1ceb13 | 24 | #include <TGrid.h> |
25 | #include <TGridResult.h> | |
26 | #include <TFile.h> | |
6dc56e97 | 27 | #include <TKey.h> |
9e1ceb13 | 28 | #include <TROOT.h> |
4667c116 | 29 | #include <TList.h> |
9e1ceb13 | 30 | #include <TObjArray.h> |
31 | #include <TObjString.h> | |
32 | #include <TRegexp.h> | |
33 | ||
34 | #include "AliLog.h" | |
fdf65bb5 | 35 | #include "AliCDBEntry.h" |
9e1ceb13 | 36 | #include "AliCDBGrid.h" |
4667c116 | 37 | #include "AliCDBManager.h" |
9e1ceb13 | 38 | |
39 | ||
40 | ClassImp(AliCDBGrid) | |
41 | ||
42 | //_____________________________________________________________________________ | |
c75fabed | 43 | AliCDBGrid::AliCDBGrid(const char *gridUrl, const char *user, const char *dbFolder, |
44 | const char *se, const char* cacheFolder, Bool_t operateDisconnected, | |
45 | Long64_t cacheSize, Long_t cleanupInterval) : | |
9e1ceb13 | 46 | AliCDBStorage(), |
b05400be | 47 | fGridUrl(gridUrl), |
9e1ceb13 | 48 | fUser(user), |
b05400be | 49 | fDBFolder(dbFolder), |
c75fabed | 50 | fSE(se), |
51 | fCacheFolder(cacheFolder), | |
52 | fOperateDisconnected(operateDisconnected), | |
53 | fCacheSize(cacheSize), | |
54 | fCleanupInterval(cleanupInterval) | |
9e1ceb13 | 55 | { |
56 | // constructor // | |
57 | ||
9e1ceb13 | 58 | // if the same Grid is alreay active, skip connection |
4667c116 | 59 | if (!gGrid || fGridUrl != gGrid->GridUrl() |
024cf675 | 60 | || (( fUser != "" ) && ( fUser != gGrid->GetUser() )) ) { |
9e1ceb13 | 61 | // connection to the Grid |
62032124 | 62 | AliInfo("Connection to the Grid..."); |
024cf675 | 63 | if(gGrid){ |
64 | AliInfo(Form("gGrid = %x; fGridUrl = %s; gGrid->GridUrl() = %s",gGrid,fGridUrl.Data(), gGrid->GridUrl())); | |
65 | AliInfo(Form("fUser = %s; gGrid->GetUser() = %s",fUser.Data(), gGrid->GetUser())); | |
66 | } | |
b05400be | 67 | TGrid::Connect(fGridUrl.Data(),fUser.Data()); |
9e1ceb13 | 68 | } |
69 | ||
70 | if(!gGrid) { | |
71 | AliError("Connection failed!"); | |
72 | return; | |
73 | } | |
74 | ||
75 | TString initDir(gGrid->Pwd(0)); | |
b05400be | 76 | if (fDBFolder[0] != '/') { |
77 | fDBFolder.Prepend(initDir); | |
9e1ceb13 | 78 | } |
79 | ||
80 | // check DBFolder: trying to cd to DBFolder; if it does not exist, create it | |
b05400be | 81 | if(!gGrid->Cd(fDBFolder.Data(),0)){ |
82 | AliDebug(2,Form("Creating new folder <%s> ...",fDBFolder.Data())); | |
d7744171 | 83 | TGridResult* res = gGrid->Command(Form("mkdir -p %s",fDBFolder.Data())); |
84 | TString result = res->GetKey(0,"__result__"); | |
85 | if(result == "0"){ | |
86 | AliFatal(Form("Cannot create folder <%s> !",fDBFolder.Data())); | |
87 | return; | |
9e1ceb13 | 88 | } |
89 | } else { | |
b05400be | 90 | AliDebug(2,Form("Folder <%s> found",fDBFolder.Data())); |
9e1ceb13 | 91 | } |
92 | ||
93 | // removes any '/' at the end of path, then append one '/' | |
b05400be | 94 | while(fDBFolder.EndsWith("/")) fDBFolder.Remove(fDBFolder.Last('/')); |
95 | fDBFolder+="/"; | |
9e1ceb13 | 96 | |
62032124 | 97 | fType="alien"; |
98 | fBaseFolder = fDBFolder; | |
99 | ||
c75fabed | 100 | // Setting the cache |
101 | ||
102 | // Check if local cache folder is already defined | |
103 | TString origCache(TFile::GetCacheFileDir()); | |
104 | if(fCacheFolder.Length() > 0) { | |
105 | if(origCache.Length() == 0) { | |
106 | AliInfo(Form("Setting local cache to: %s", fCacheFolder.Data())); | |
107 | } else if(fCacheFolder != origCache) { | |
108 | AliWarning(Form("Local cache folder was already defined, changing it to: %s", | |
109 | fCacheFolder.Data())); | |
110 | } | |
111 | ||
112 | // default settings are: operateDisconnected=kTRUE, forceCacheread = kFALSE | |
113 | if(!TFile::SetCacheFileDir(fCacheFolder.Data(), fOperateDisconnected)) { | |
114 | AliError(Form("Could not set cache folder %s !", fCacheFolder.Data())); | |
115 | fCacheFolder = ""; | |
116 | } else { | |
117 | // reset fCacheFolder because the function may have | |
118 | // slightly changed the folder name (e.g. '/' added) | |
119 | fCacheFolder = TFile::GetCacheFileDir(); | |
120 | } | |
121 | ||
122 | // default settings are: cacheSize=1GB, cleanupInterval = 0 | |
123 | if(!TFile::ShrinkCacheFileDir(fCacheSize, fCleanupInterval)) { | |
124 | AliError(Form("Could not set following values " | |
125 | "to ShrinkCacheFileDir: cacheSize = %d, cleanupInterval = %d !", | |
126 | fCacheSize, fCleanupInterval)); | |
127 | } | |
128 | } | |
129 | ||
9e1ceb13 | 130 | // return to the initial directory |
131 | gGrid->Cd(initDir.Data(),0); | |
132 | } | |
133 | ||
134 | //_____________________________________________________________________________ | |
135 | AliCDBGrid::~AliCDBGrid() | |
136 | { | |
137 | // destructor | |
62032124 | 138 | delete gGrid; gGrid=0; |
9e1ceb13 | 139 | |
140 | } | |
141 | ||
142 | //_____________________________________________________________________________ | |
62032124 | 143 | Bool_t AliCDBGrid::FilenameToId(TString& filename, AliCDBId& id) { |
4005d0b5 | 144 | // build AliCDBId from full path filename (fDBFolder/path/Run#x_#y_v#z_s0.root) |
62032124 | 145 | |
146 | if(filename.Contains(fDBFolder)){ | |
147 | filename = filename(fDBFolder.Length(),filename.Length()-fDBFolder.Length()); | |
148 | } | |
149 | ||
150 | TString idPath = filename(0,filename.Last('/')); | |
151 | id.SetPath(idPath); | |
152 | if(!id.IsValid()) return kFALSE; | |
153 | ||
154 | filename=filename(idPath.Length()+1,filename.Length()-idPath.Length()); | |
9e1ceb13 | 155 | |
156 | Ssiz_t mSize; | |
4005d0b5 | 157 | // valid filename: Run#firstRun_#lastRun_v#version_s0.root |
158 | TRegexp keyPattern("^Run[0-9]+_[0-9]+_v[0-9]+_s0.root$"); | |
9e1ceb13 | 159 | keyPattern.Index(filename, &mSize); |
160 | if (!mSize) { | |
4005d0b5 | 161 | |
162 | // TODO backward compatibility ... maybe remove later! | |
163 | Ssiz_t oldmSize; | |
164 | TRegexp oldKeyPattern("^Run[0-9]+_[0-9]+_v[0-9]+.root$"); | |
165 | oldKeyPattern.Index(filename, &oldmSize); | |
166 | if(!oldmSize) { | |
167 | AliDebug(2,Form("Bad filename <%s>.", filename.Data())); | |
168 | return kFALSE; | |
169 | } else { | |
170 | AliDebug(2,Form("Old filename format <%s>.", filename.Data())); | |
171 | id.SetSubVersion(-11); // TODO trick to ensure backward compatibility | |
172 | } | |
173 | ||
174 | } else { | |
175 | id.SetSubVersion(-1); // TODO trick to ensure backward compatibility | |
176 | } | |
9e1ceb13 | 177 | |
62032124 | 178 | filename.Resize(filename.Length() - sizeof(".root") + 1); |
9e1ceb13 | 179 | |
62032124 | 180 | TObjArray* strArray = (TObjArray*) filename.Tokenize("_"); |
9e1ceb13 | 181 | |
182 | TString firstRunString(((TObjString*) strArray->At(0))->GetString()); | |
62032124 | 183 | id.SetFirstRun(atoi(firstRunString.Data() + 3)); |
184 | id.SetLastRun(atoi(((TObjString*) strArray->At(1))->GetString())); | |
185 | ||
9e1ceb13 | 186 | TString verString(((TObjString*) strArray->At(2))->GetString()); |
62032124 | 187 | id.SetVersion(atoi(verString.Data() + 1)); |
9e1ceb13 | 188 | |
189 | delete strArray; | |
190 | ||
191 | return kTRUE; | |
192 | } | |
193 | ||
194 | //_____________________________________________________________________________ | |
c3a7b59a | 195 | Bool_t AliCDBGrid::IdToFilename(const AliCDBId& id, TString& filename) const { |
62032124 | 196 | // build file name from AliCDBId (path, run range, version) and fDBFolder |
9e1ceb13 | 197 | |
62032124 | 198 | if (!id.GetAliCDBRunRange().IsValid()) { |
199 | AliDebug(2,Form("Invalid run range <%d, %d>.", | |
200 | id.GetFirstRun(), id.GetLastRun())); | |
9e1ceb13 | 201 | return kFALSE; |
202 | } | |
203 | ||
62032124 | 204 | if (id.GetVersion() < 0) { |
205 | AliDebug(2,Form("Invalid version <%d>.", id.GetVersion())); | |
9e1ceb13 | 206 | return kFALSE; |
207 | } | |
62032124 | 208 | |
4005d0b5 | 209 | filename = Form("Run%d_%d_v%d", |
62032124 | 210 | id.GetFirstRun(), |
211 | id.GetLastRun(), | |
212 | id.GetVersion()); | |
213 | ||
4005d0b5 | 214 | if (id.GetSubVersion() != -11) filename += "_s0"; // TODO to ensure backward compatibility |
215 | filename += ".root"; | |
216 | ||
62032124 | 217 | filename.Prepend(fDBFolder + id.GetPath() + '/'); |
9e1ceb13 | 218 | |
219 | return kTRUE; | |
220 | } | |
221 | ||
222 | //_____________________________________________________________________________ | |
223 | Bool_t AliCDBGrid::PrepareId(AliCDBId& id) { | |
224 | // prepare id (version) of the object that will be stored (called by PutEntry) | |
225 | ||
226 | TString initDir(gGrid->Pwd(0)); | |
9e1ceb13 | 227 | |
b05400be | 228 | TString dirName(fDBFolder); |
9e1ceb13 | 229 | |
230 | Bool_t dirExist=kFALSE; | |
4005d0b5 | 231 | |
4667c116 | 232 | |
233 | ||
9e1ceb13 | 234 | // go to the path; if directory does not exist, create it |
4667c116 | 235 | for(int i=0;i<3;i++){ |
236 | dirName+=Form("%s/",id.GetPathLevel(i).Data()); | |
9e1ceb13 | 237 | dirExist=gGrid->Cd(dirName,0); |
238 | if (!dirExist) { | |
b05400be | 239 | AliDebug(2,Form("Creating new folder <%s> ...",dirName.Data())); |
9e1ceb13 | 240 | if(!gGrid->Mkdir(dirName,"",0)){ |
241 | AliError(Form("Cannot create directory <%s> !",dirName.Data())); | |
242 | gGrid->Cd(initDir.Data()); | |
243 | return kFALSE; | |
244 | } | |
4667c116 | 245 | |
246 | // if folders are new add tags to them | |
247 | if(i == 1) { | |
248 | // TODO Currently disabled | |
c75fabed | 249 | // add short lived tag! |
4667c116 | 250 | // AliInfo("Tagging level 1 folder with \"ShortLived\" tag"); |
251 | // if(!AddTag(dirName,"ShortLived_try")){ | |
252 | // AliError(Form("Could not tag folder %s !", dirName.Data())); | |
253 | // if(!gGrid->Rmdir(dirName.Data())){ | |
254 | // AliError(Form("Unexpected: could not remove %s directory!", dirName.Data())); | |
255 | // } | |
256 | // return 0; | |
257 | //} | |
258 | ||
259 | } else if(i == 2) { | |
c75fabed | 260 | AliDebug(2,"Tagging level 2 folder with \"CDB\" and \"CDB_MD\" tag"); |
4667c116 | 261 | if(!AddTag(dirName,"CDB")){ |
262 | AliError(Form("Could not tag folder %s !", dirName.Data())); | |
263 | if(!gGrid->Rmdir(dirName.Data())){ | |
264 | AliError(Form("Unexpected: could not remove %s directory!", dirName.Data())); | |
265 | } | |
266 | return 0; | |
267 | } | |
268 | if(!AddTag(dirName,"CDB_MD")){ | |
269 | AliError(Form("Could not tag folder %s !", dirName.Data())); | |
270 | if(!gGrid->Rmdir(dirName.Data())){ | |
271 | AliError(Form("Unexpected: could not remove %s directory!", dirName.Data())); | |
272 | } | |
273 | return 0; | |
274 | } | |
275 | ||
4667c116 | 276 | // TODO Currently disabled |
c75fabed | 277 | // add short lived tag! |
4667c116 | 278 | // TString path=id.GetPath(); |
279 | // if(AliCDBManager::Instance()->IsShortLived(path.Data())) { | |
280 | // AliInfo(Form("Tagging %s as short lived", dirName.Data())); | |
281 | // if(!TagShortLived(dirName, kTRUE)){ | |
282 | // AliError(Form("Could not tag folder %s !", dirName.Data())); | |
283 | // if(!gGrid->Rmdir(dirName.Data())){ | |
284 | // AliError(Form("Unexpected: could not remove %s directory!", dirName.Data())); | |
285 | // } | |
286 | // return 0; | |
287 | // } | |
288 | // } else { | |
289 | // AliInfo(Form("Tagging %s as long lived", dirName.Data())); | |
290 | // if(!TagShortLived(dirName, kFALSE)){ | |
291 | // AliError(Form("Could not tag folder %s !", dirName.Data())); | |
292 | // if(!gGrid->Rmdir(dirName.Data())){ | |
293 | // AliError(Form("Unexpected: could not remove %s directory!", dirName.Data())); | |
294 | // } | |
295 | // return 0; | |
296 | // } | |
297 | // } | |
298 | } | |
9e1ceb13 | 299 | } |
300 | } | |
9e1ceb13 | 301 | gGrid->Cd(initDir,0); |
302 | ||
62032124 | 303 | TString filename; |
304 | AliCDBId anId; // the id got from filename | |
9e1ceb13 | 305 | AliCDBRunRange lastRunRange(-1,-1); // highest runRange found |
9e1ceb13 | 306 | Int_t lastVersion=0; // highest version found |
307 | ||
308 | TGridResult *res = gGrid->Ls(dirName); | |
309 | ||
310 | //loop on the files in the directory, look for highest version | |
311 | for(int i=0; i < res->GetEntries(); i++){ | |
62032124 | 312 | filename=res->GetFileNamePath(i); |
313 | if (!FilenameToId(filename, anId)) continue; | |
314 | if (anId.GetAliCDBRunRange().Overlaps(id.GetAliCDBRunRange()) && anId.GetVersion() > lastVersion) { | |
315 | lastVersion = anId.GetVersion(); | |
316 | lastRunRange = anId.GetAliCDBRunRange(); | |
9e1ceb13 | 317 | } |
318 | ||
319 | } | |
4005d0b5 | 320 | delete res; |
321 | ||
be1b1008 | 322 | // GRP entries with explicitly set version escape default incremental versioning |
323 | if(id.GetPath().Contains("GRP") && id.HasVersion() && lastVersion!=0) | |
324 | { | |
325 | AliDebug(5,Form("Entry %s won't be put in the destination OCDB", id.ToString().Data())); | |
326 | return kFALSE; | |
1ac347b2 | 327 | } |
05ca3c19 | 328 | |
329 | id.SetVersion(lastVersion + 1); | |
4005d0b5 | 330 | id.SetSubVersion(0); |
9e1ceb13 | 331 | |
332 | TString lastStorage = id.GetLastStorage(); | |
333 | if(lastStorage.Contains(TString("new"), TString::kIgnoreCase) && id.GetVersion() > 1 ){ | |
84090f85 | 334 | AliDebug(2, Form("A NEW object is being stored with version %d", |
9e1ceb13 | 335 | id.GetVersion())); |
84090f85 | 336 | AliDebug(2, Form("and it will hide previously stored object with version %d!", |
9e1ceb13 | 337 | id.GetVersion()-1)); |
338 | } | |
339 | ||
4005d0b5 | 340 | if(!lastRunRange.IsAnyRange() && !(lastRunRange.IsEqual(&id.GetAliCDBRunRange()))) |
9e1ceb13 | 341 | AliWarning(Form("Run range modified w.r.t. previous version (Run%d_%d_v%d)", |
342 | lastRunRange.GetFirstRun(), lastRunRange.GetLastRun(), id.GetVersion())); | |
4005d0b5 | 343 | |
9e1ceb13 | 344 | return kTRUE; |
345 | } | |
346 | ||
347 | //_____________________________________________________________________________ | |
6dc56e97 | 348 | AliCDBId* AliCDBGrid::GetId(const TObjArray& validFileIds, const AliCDBId& query) { |
62032124 | 349 | // look for the Id that matches query's requests (highest or exact version) |
9e1ceb13 | 350 | |
98873e41 | 351 | if(validFileIds.GetEntriesFast() < 1) |
62032124 | 352 | return NULL; |
9e1ceb13 | 353 | |
62032124 | 354 | TIter iter(&validFileIds); |
9e1ceb13 | 355 | |
62032124 | 356 | AliCDBId *anIdPtr=0; |
357 | AliCDBId* result=0; | |
9e1ceb13 | 358 | |
62032124 | 359 | while((anIdPtr = dynamic_cast<AliCDBId*> (iter.Next()))){ |
62032124 | 360 | if(anIdPtr->GetPath() != query.GetPath()) continue; |
361 | ||
362 | //if(!CheckVersion(query, anIdPtr, result)) return NULL; | |
9e1ceb13 | 363 | |
364 | if (!query.HasVersion()){ // look for highest version | |
62032124 | 365 | if(result && result->GetVersion() > anIdPtr->GetVersion()) continue; |
366 | if(result && result->GetVersion() == anIdPtr->GetVersion()) { | |
4667c116 | 367 | AliError(Form("More than one object valid for run %d, version %d!", |
62032124 | 368 | query.GetFirstRun(), anIdPtr->GetVersion())); |
369 | return NULL; | |
9e1ceb13 | 370 | } |
62032124 | 371 | result = anIdPtr; |
9e1ceb13 | 372 | } else { // look for specified version |
62032124 | 373 | if(query.GetVersion() != anIdPtr->GetVersion()) continue; |
374 | if(result && result->GetVersion() == anIdPtr->GetVersion()){ | |
4667c116 | 375 | AliError(Form("More than one object valid for run %d, version %d!", |
62032124 | 376 | query.GetFirstRun(), anIdPtr->GetVersion())); |
377 | return NULL; | |
9e1ceb13 | 378 | } |
62032124 | 379 | result = anIdPtr; |
9e1ceb13 | 380 | } |
62032124 | 381 | |
382 | } | |
c75fabed | 383 | |
384 | if (!result) return NULL; | |
62032124 | 385 | |
4667c116 | 386 | return dynamic_cast<AliCDBId*> (result->Clone()); |
62032124 | 387 | } |
388 | ||
9e1ceb13 | 389 | //_____________________________________________________________________________ |
4667c116 | 390 | AliCDBId* AliCDBGrid::GetEntryId(const AliCDBId& queryId) { |
391 | // get AliCDBId from the database | |
392 | // User must delete returned object | |
9e1ceb13 | 393 | |
62032124 | 394 | AliCDBId* dataId=0; |
395 | ||
396 | AliCDBId selectedId(queryId); | |
397 | if (!selectedId.HasVersion()) { | |
9e1ceb13 | 398 | // if version is not specified, first check the selection criteria list |
4b5e0dce | 399 | GetSelection(&selectedId); |
62032124 | 400 | } |
401 | ||
6dc56e97 | 402 | TObjArray validFileIds; |
62032124 | 403 | validFileIds.SetOwner(1); |
404 | ||
405 | // look for file matching query requests (path, runRange, version) | |
406 | if(selectedId.GetFirstRun() == fRun && | |
c3a7b59a | 407 | fPathFilter.Comprises(selectedId.GetAliCDBPath()) && fVersion < 0 && !fMetaDataFilter){ |
62032124 | 408 | // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds() |
98873e41 | 409 | AliDebug(2, Form("List of files valid for run %d was loaded. Looking there for fileids valid for path %s!", |
62032124 | 410 | selectedId.GetFirstRun(), selectedId.GetPath().Data())); |
411 | dataId = GetId(fValidFileIds, selectedId); | |
412 | ||
9e1ceb13 | 413 | } else { |
62032124 | 414 | // List of files valid for reqested run was not loaded. Looking directly into CDB |
98873e41 | 415 | AliDebug(2, Form("List of files valid for run %d was not loaded. Looking directly into CDB for fileids valid for path %s!", |
62032124 | 416 | selectedId.GetFirstRun(), selectedId.GetPath().Data())); |
417 | ||
418 | TString filter; | |
6dc56e97 | 419 | MakeQueryFilter(selectedId.GetFirstRun(), selectedId.GetLastRun(), 0, filter); |
62032124 | 420 | |
6dc56e97 | 421 | TString pattern = Form("%s/Run*", selectedId.GetPath().Data()); |
4005d0b5 | 422 | if(selectedId.GetVersion() >= 0) pattern += Form("_v%d*",selectedId.GetVersion()); |
6dc56e97 | 423 | pattern += ".root"; |
424 | AliDebug(2,Form("pattern: %s", pattern.Data())); | |
425 | ||
426 | TGridResult *res = gGrid->Query(fDBFolder, pattern, filter, ""); | |
ecae96f8 | 427 | if (res) { |
428 | AliCDBId validFileId; | |
429 | for(int i=0; i<res->GetEntries(); i++){ | |
430 | TString filename = res->GetKey(i, "lfn"); | |
431 | if(filename == "") continue; | |
432 | if(FilenameToId(filename, validFileId)) | |
433 | validFileIds.AddLast(validFileId.Clone()); | |
434 | } | |
435 | delete res; | |
436 | } | |
62032124 | 437 | dataId = GetId(validFileIds, selectedId); |
9e1ceb13 | 438 | } |
439 | ||
4667c116 | 440 | return dataId; |
441 | } | |
442 | ||
443 | //_____________________________________________________________________________ | |
444 | AliCDBEntry* AliCDBGrid::GetEntry(const AliCDBId& queryId) { | |
445 | // get AliCDBEntry from the database | |
446 | ||
447 | AliCDBId* dataId = GetEntryId(queryId); | |
448 | ||
62032124 | 449 | if (!dataId) return NULL; |
9e1ceb13 | 450 | |
451 | TString filename; | |
62032124 | 452 | if (!IdToFilename(*dataId, filename)) { |
024cf675 | 453 | AliDebug(2,Form("Bad data ID encountered! Subnormal error!")); |
4667c116 | 454 | delete dataId; |
9e1ceb13 | 455 | return NULL; |
456 | } | |
457 | ||
62032124 | 458 | AliCDBEntry* anEntry = GetEntryFromFile(filename, dataId); |
459 | ||
4667c116 | 460 | delete dataId; |
62032124 | 461 | return anEntry; |
462 | } | |
463 | ||
464 | //_____________________________________________________________________________ | |
8e245d15 | 465 | AliCDBEntry* AliCDBGrid::GetEntryFromFile(TString& filename, AliCDBId* dataId){ |
62032124 | 466 | // Get AliCBEntry object from file "filename" |
9e1ceb13 | 467 | |
024cf675 | 468 | AliDebug(2,Form("Opening file: %s",filename.Data())); |
62032124 | 469 | |
470 | filename.Prepend("/alien"); | |
c75fabed | 471 | |
472 | // if option="CACHEREAD" TFile will use the local caching facility! | |
473 | TString option="READ"; | |
474 | if(fCacheFolder != ""){ | |
475 | ||
476 | // Check if local cache folder was changed in the meanwhile | |
477 | TString origCache(TFile::GetCacheFileDir()); | |
478 | if(fCacheFolder != origCache) { | |
479 | AliWarning(Form("Local cache folder has been overwritten!! fCacheFolder = %s origCache = %s", | |
480 | fCacheFolder.Data(), origCache.Data())); | |
481 | TFile::SetCacheFileDir(fCacheFolder.Data(), fOperateDisconnected); | |
482 | TFile::ShrinkCacheFileDir(fCacheSize, fCleanupInterval); | |
483 | } | |
484 | ||
485 | option.Prepend("CACHE"); | |
486 | } | |
487 | ||
488 | AliDebug(2, Form("Option: %s", option.Data())); | |
489 | ||
490 | TFile *file = TFile::Open(filename, option); | |
9e1ceb13 | 491 | if (!file) { |
024cf675 | 492 | AliDebug(2,Form("Can't open file <%s>!", filename.Data())); |
9e1ceb13 | 493 | return NULL; |
494 | } | |
495 | ||
496 | // get the only AliCDBEntry object from the file | |
497 | // the object in the file is an AliCDBEntry entry named "AliCDBEntry" | |
498 | ||
62032124 | 499 | AliCDBEntry* anEntry = dynamic_cast<AliCDBEntry*> (file->Get("AliCDBEntry")); |
9e1ceb13 | 500 | |
62032124 | 501 | if (!anEntry) { |
502 | AliDebug(2,Form("Bad storage data: file does not contain an AliCDBEntry object!")); | |
503 | file->Close(); | |
9e1ceb13 | 504 | return NULL; |
505 | } | |
506 | ||
9e1ceb13 | 507 | // The object's Id is not reset during storage |
508 | // If object's Id runRange or version do not match with filename, | |
509 | // it means that someone renamed file by hand. In this case a warning msg is issued. | |
62032124 | 510 | |
511 | if(anEntry){ | |
512 | AliCDBId entryId = anEntry->GetId(); | |
4005d0b5 | 513 | Int_t tmpSubVersion = dataId->GetSubVersion(); |
514 | dataId->SetSubVersion(entryId.GetSubVersion()); // otherwise filename and id may mismatch | |
8e245d15 | 515 | if(!entryId.IsEqual(dataId)){ |
b8ec52f6 | 516 | AliWarning(Form("Mismatch between file name and object's Id!")); |
517 | AliWarning(Form("File name: %s", dataId->ToString().Data())); | |
518 | AliWarning(Form("Object's Id: %s", entryId.ToString().Data())); | |
62032124 | 519 | } |
4005d0b5 | 520 | dataId->SetSubVersion(tmpSubVersion); |
9e1ceb13 | 521 | } |
522 | ||
62032124 | 523 | anEntry->SetLastStorage("grid"); |
524 | ||
145f45a2 | 525 | // Check whether entry contains a TTree. In case load the tree in memory! |
526 | LoadTreeFromFile(anEntry); | |
527 | ||
9e1ceb13 | 528 | // close file, return retieved entry |
529 | file->Close(); delete file; file=0; | |
62032124 | 530 | |
531 | return anEntry; | |
9e1ceb13 | 532 | } |
533 | ||
534 | //_____________________________________________________________________________ | |
62032124 | 535 | TList* AliCDBGrid::GetEntries(const AliCDBId& queryId) { |
9e1ceb13 | 536 | // multiple request (AliCDBStorage::GetAll) |
537 | ||
62032124 | 538 | TList* result = new TList(); |
539 | result->SetOwner(); | |
9e1ceb13 | 540 | |
6dc56e97 | 541 | TObjArray validFileIds; |
62032124 | 542 | validFileIds.SetOwner(1); |
9e1ceb13 | 543 | |
62032124 | 544 | Bool_t alreadyLoaded = kFALSE; |
9e1ceb13 | 545 | |
62032124 | 546 | // look for file matching query requests (path, runRange) |
547 | if(queryId.GetFirstRun() == fRun && | |
c3a7b59a | 548 | fPathFilter.Comprises(queryId.GetAliCDBPath()) && fVersion < 0 && !fMetaDataFilter){ |
62032124 | 549 | // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds() |
550 | AliDebug(2,Form("List of files valid for run %d and for path %s was loaded. Looking there!", | |
551 | queryId.GetFirstRun(), queryId.GetPath().Data())); | |
9e1ceb13 | 552 | |
62032124 | 553 | alreadyLoaded = kTRUE; |
9e1ceb13 | 554 | |
62032124 | 555 | } else { |
556 | // List of files valid for reqested run was not loaded. Looking directly into CDB | |
557 | AliDebug(2,Form("List of files valid for run %d and for path %s was not loaded. Looking directly into CDB!", | |
558 | queryId.GetFirstRun(), queryId.GetPath().Data())); | |
559 | ||
560 | TString filter; | |
6dc56e97 | 561 | MakeQueryFilter(queryId.GetFirstRun(), queryId.GetLastRun(), 0, filter); |
562 | ||
563 | TString pattern = Form("%s/Run*.root", queryId.GetPath().Data()); | |
564 | AliDebug(2,Form("pattern: %s", pattern.Data())); | |
565 | ||
566 | TGridResult *res = gGrid->Query(fDBFolder, pattern, filter, ""); | |
62032124 | 567 | |
62032124 | 568 | AliCDBId validFileId; |
569 | for(int i=0; i<res->GetEntries(); i++){ | |
570 | TString filename = res->GetKey(i, "lfn"); | |
c3a7b59a | 571 | if(filename == "") continue; |
62032124 | 572 | if(FilenameToId(filename, validFileId)) |
573 | validFileIds.AddLast(validFileId.Clone()); | |
574 | } | |
575 | delete res; | |
9e1ceb13 | 576 | } |
577 | ||
62032124 | 578 | TIter *iter=0; |
579 | if(alreadyLoaded){ | |
580 | iter = new TIter(&fValidFileIds); | |
581 | } else { | |
582 | iter = new TIter(&validFileIds); | |
583 | } | |
9e1ceb13 | 584 | |
6dc56e97 | 585 | TObjArray selectedIds; |
62032124 | 586 | selectedIds.SetOwner(1); |
587 | ||
588 | // loop on list of valid Ids to select the right version to get. | |
589 | // According to query and to the selection criteria list, version can be the highest or exact | |
590 | AliCDBPath pathCopy; | |
591 | AliCDBId* anIdPtr=0; | |
592 | AliCDBId* dataId=0; | |
593 | AliCDBPath queryPath = queryId.GetAliCDBPath(); | |
594 | while((anIdPtr = dynamic_cast<AliCDBId*> (iter->Next()))){ | |
595 | AliCDBPath thisCDBPath = anIdPtr->GetAliCDBPath(); | |
596 | if(!(queryPath.Comprises(thisCDBPath)) || pathCopy.GetPath() == thisCDBPath.GetPath()) continue; | |
597 | pathCopy = thisCDBPath; | |
598 | ||
599 | // check the selection criteria list for this query | |
600 | AliCDBId thisId(*anIdPtr); | |
601 | thisId.SetVersion(queryId.GetVersion()); | |
602 | if(!thisId.HasVersion()) GetSelection(&thisId); | |
603 | ||
604 | if(alreadyLoaded){ | |
605 | dataId = GetId(fValidFileIds, thisId); | |
606 | } else { | |
607 | dataId = GetId(validFileIds, thisId); | |
9e1ceb13 | 608 | } |
4667c116 | 609 | if(dataId) selectedIds.Add(dataId); |
9e1ceb13 | 610 | } |
9e1ceb13 | 611 | |
62032124 | 612 | delete iter; iter=0; |
9e1ceb13 | 613 | |
62032124 | 614 | // selectedIds contains the Ids of the files matching all requests of query! |
615 | // All the objects are now ready to be retrieved | |
616 | iter = new TIter(&selectedIds); | |
617 | while((anIdPtr = dynamic_cast<AliCDBId*> (iter->Next()))){ | |
618 | TString filename; | |
619 | if (!IdToFilename(*anIdPtr, filename)) { | |
620 | AliDebug(2,Form("Bad data ID encountered! Subnormal error!")); | |
621 | continue; | |
622 | } | |
9e1ceb13 | 623 | |
62032124 | 624 | AliCDBEntry* anEntry = GetEntryFromFile(filename, anIdPtr); |
9e1ceb13 | 625 | |
62032124 | 626 | if(anEntry) result->Add(anEntry); |
9e1ceb13 | 627 | |
62032124 | 628 | } |
629 | delete iter; iter=0; | |
630 | ||
631 | return result; | |
9e1ceb13 | 632 | } |
633 | ||
634 | //_____________________________________________________________________________ | |
635 | Bool_t AliCDBGrid::PutEntry(AliCDBEntry* entry) { | |
636 | // put an AliCDBEntry object into the database | |
62032124 | 637 | |
9e1ceb13 | 638 | AliCDBId& id = entry->GetId(); |
639 | ||
640 | // set version for the entry to be stored | |
62032124 | 641 | if (!PrepareId(id)) return kFALSE; |
9e1ceb13 | 642 | |
643 | // build filename from entry's id | |
644 | TString filename; | |
62032124 | 645 | if (!IdToFilename(id, filename)) { |
9e1ceb13 | 646 | AliError("Bad ID encountered! Subnormal error!"); |
647 | return kFALSE; | |
62032124 | 648 | } |
649 | ||
650 | TString folderToTag = Form("%s%s", | |
651 | fDBFolder.Data(), | |
652 | id.GetPath().Data()); | |
653 | ||
9e1ceb13 | 654 | TDirectory* saveDir = gDirectory; |
655 | ||
fb33abde | 656 | TString fullFilename = Form("/alien%s", filename.Data()); |
62032124 | 657 | // specify SE to filename |
fb33abde | 658 | if (fSE != "default") fullFilename += Form("?se=%s",fSE.Data()); |
62032124 | 659 | |
9e1ceb13 | 660 | // open file |
145f45a2 | 661 | TFile *file = TFile::Open(fullFilename,"CREATE"); |
662 | if(!file || !file->IsWritable()){ | |
663 | AliError(Form("Can't open file <%s>!", filename.Data())); | |
664 | if(file && !file->IsWritable()) file->Close(); delete file; file=0; | |
665 | return kFALSE; | |
666 | } | |
62032124 | 667 | |
668 | file->cd(); | |
9e1ceb13 | 669 | |
ed343d5e | 670 | //SetTreeToFile(entry, file); |
145f45a2 | 671 | |
9e1ceb13 | 672 | entry->SetVersion(id.GetVersion()); |
673 | ||
674 | // write object (key name: "AliCDBEntry") | |
145f45a2 | 675 | Bool_t result = (file->WriteTObject(entry, "AliCDBEntry") != 0); |
62032124 | 676 | if (!result) AliError(Form("Can't write entry to file <%s>!", filename.Data())); |
9e1ceb13 | 677 | |
678 | ||
679 | if (saveDir) saveDir->cd(); else gROOT->cd(); | |
680 | file->Close(); delete file; file=0; | |
c3a7b59a | 681 | |
9e1ceb13 | 682 | if(result) { |
c75fabed | 683 | |
c3a7b59a | 684 | if(!TagFileId(filename, &id)){ |
685 | AliInfo(Form("CDB tagging failed. Deleting file %s!",filename.Data())); | |
686 | if(!gGrid->Rm(filename.Data())) | |
687 | AliError("Can't delete file!"); | |
688 | return kFALSE; | |
689 | } | |
690 | ||
691 | TagFileMetaData(filename, entry->GetMetaData()); | |
9e1ceb13 | 692 | } |
62032124 | 693 | |
c75fabed | 694 | AliInfo(Form("CDB object stored into file %s", filename.Data())); |
695 | AliInfo(Form("Storage Element: %s", fSE.Data())); | |
62032124 | 696 | return result; |
697 | } | |
698 | //_____________________________________________________________________________ | |
699 | Bool_t AliCDBGrid::AddTag(TString& folderToTag, const char* tagname){ | |
700 | // add "tagname" tag (CDB or CDB_MD) to folder where object will be stored | |
701 | ||
702 | Bool_t result = kTRUE; | |
703 | AliDebug(2, Form("adding %s tag to folder %s", tagname, folderToTag.Data())); | |
704 | TString addTag = Form("addTag %s %s", folderToTag.Data(), tagname); | |
705 | TGridResult *gridres = gGrid->Command(addTag.Data()); | |
706 | const char* resCode = gridres->GetKey(0,"__result__"); // '1' if success | |
707 | if(resCode[0] != '1') { | |
708 | AliError(Form("Couldn't add %s tags to folder %s !", | |
709 | tagname, folderToTag.Data())); | |
710 | result = kFALSE; | |
711 | } | |
712 | delete gridres; | |
9e1ceb13 | 713 | return result; |
714 | } | |
715 | ||
62032124 | 716 | //_____________________________________________________________________________ |
c3a7b59a | 717 | Bool_t AliCDBGrid::TagFileId(TString& filename, const AliCDBId* id){ |
62032124 | 718 | // tag stored object in CDB table using object Id's parameters |
719 | ||
f430a9ce | 720 | TString addTagValue1 = Form("addTagValue %s CDB ", filename.Data()); |
721 | TString addTagValue2 = Form("first_run=%d last_run=%d version=%d ", | |
62032124 | 722 | id->GetFirstRun(), |
723 | id->GetLastRun(), | |
724 | id->GetVersion()); | |
f430a9ce | 725 | TString addTagValue3 = Form("path_level_0=\"%s\" path_level_1=\"%s\" path_level_2=\"%s\"", |
4667c116 | 726 | id->GetPathLevel(0).Data(), |
727 | id->GetPathLevel(1).Data(), | |
728 | id->GetPathLevel(2).Data()); | |
62032124 | 729 | TString addTagValue = Form("%s%s%s", |
f430a9ce | 730 | addTagValue1.Data(), |
731 | addTagValue2.Data(), | |
732 | addTagValue3.Data()); | |
62032124 | 733 | |
c3a7b59a | 734 | Bool_t result = kFALSE; |
62032124 | 735 | AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data())); |
736 | TGridResult* res = gGrid->Command(addTagValue.Data()); | |
737 | const char* resCode = res->GetKey(0,"__result__"); // '1' if success | |
738 | if(resCode[0] != '1') { | |
c3a7b59a | 739 | AliError(Form("Couldn't add CDB tag value to file %s !", |
62032124 | 740 | filename.Data())); |
c3a7b59a | 741 | result = kFALSE; |
62032124 | 742 | } else { |
c75fabed | 743 | AliDebug(2, "Object successfully tagged."); |
c3a7b59a | 744 | result = kTRUE; |
62032124 | 745 | } |
746 | delete res; | |
c3a7b59a | 747 | return result; |
62032124 | 748 | |
749 | } | |
750 | ||
4667c116 | 751 | //_____________________________________________________________________________ |
752 | Bool_t AliCDBGrid::TagShortLived(TString& filename, Bool_t value){ | |
753 | // tag folder with ShortLived tag | |
754 | ||
755 | TString addTagValue = Form("addTagValue %s ShortLived_try value=%d", filename.Data(), value); | |
756 | ||
757 | Bool_t result = kFALSE; | |
758 | AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data())); | |
759 | TGridResult* res = gGrid->Command(addTagValue.Data()); | |
760 | const char* resCode = res->GetKey(0,"__result__"); // '1' if success | |
761 | if(resCode[0] != '1') { | |
762 | AliError(Form("Couldn't add ShortLived tag value to file %s !", filename.Data())); | |
763 | result = kFALSE; | |
764 | } else { | |
c75fabed | 765 | AliDebug(2,"Object successfully tagged."); |
4667c116 | 766 | result = kTRUE; |
767 | } | |
768 | delete res; | |
769 | return result; | |
770 | ||
771 | } | |
772 | ||
62032124 | 773 | //_____________________________________________________________________________ |
c3a7b59a | 774 | Bool_t AliCDBGrid::TagFileMetaData(TString& filename, const AliCDBMetaData* md){ |
62032124 | 775 | // tag stored object in CDB table using object Id's parameters |
776 | ||
f430a9ce | 777 | TString addTagValue1 = Form("addTagValue %s CDB_MD ", filename.Data()); |
778 | TString addTagValue2 = Form("object_classname=\"%s\" responsible=\"%s\" beam_period=%d ", | |
62032124 | 779 | md->GetObjectClassName(), |
780 | md->GetResponsible(), | |
781 | md->GetBeamPeriod()); | |
f430a9ce | 782 | TString addTagValue3 = Form("aliroot_version=\"%s\" comment=\"%s\"", |
62032124 | 783 | md->GetAliRootVersion(), |
784 | md->GetComment()); | |
785 | TString addTagValue = Form("%s%s%s", | |
f430a9ce | 786 | addTagValue1.Data(), |
787 | addTagValue2.Data(), | |
788 | addTagValue3.Data()); | |
62032124 | 789 | |
c3a7b59a | 790 | Bool_t result = kFALSE; |
62032124 | 791 | AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data())); |
792 | TGridResult* res = gGrid->Command(addTagValue.Data()); | |
793 | const char* resCode = res->GetKey(0,"__result__"); // '1' if success | |
794 | if(resCode[0] != '1') { | |
795 | AliWarning(Form("Couldn't add CDB_MD tag value to file %s !", | |
796 | filename.Data())); | |
c3a7b59a | 797 | result = kFALSE; |
62032124 | 798 | } else { |
c75fabed | 799 | AliDebug(2,"Object successfully tagged."); |
c3a7b59a | 800 | result = kTRUE; |
62032124 | 801 | } |
c3a7b59a | 802 | return result; |
62032124 | 803 | } |
804 | ||
b05400be | 805 | //_____________________________________________________________________________ |
806 | TList* AliCDBGrid::GetIdListFromFile(const char* fileName){ | |
6dc56e97 | 807 | |
b05400be | 808 | TString turl(fileName); |
809 | turl.Prepend("/alien" + fDBFolder); | |
62032124 | 810 | turl += "?se="; turl += fSE.Data(); |
b05400be | 811 | TFile *file = TFile::Open(turl); |
812 | if (!file) { | |
813 | AliError(Form("Can't open selection file <%s>!", turl.Data())); | |
814 | return NULL; | |
815 | } | |
816 | ||
817 | TList *list = new TList(); | |
818 | list->SetOwner(); | |
819 | int i=0; | |
820 | TString keycycle; | |
62032124 | 821 | |
b05400be | 822 | AliCDBId *id; |
823 | while(1){ | |
824 | i++; | |
825 | keycycle = "AliCDBId;"; | |
826 | keycycle+=i; | |
827 | ||
828 | id = (AliCDBId*) file->Get(keycycle); | |
829 | if(!id) break; | |
830 | list->AddFirst(id); | |
831 | } | |
832 | file->Close(); delete file; file=0; | |
833 | ||
834 | return list; | |
835 | ||
836 | ||
837 | } | |
838 | ||
839 | //_____________________________________________________________________________ | |
840 | Bool_t AliCDBGrid::Contains(const char* path) const{ | |
841 | // check for path in storage's DBFolder | |
842 | ||
843 | TString initDir(gGrid->Pwd(0)); | |
844 | TString dirName(fDBFolder); | |
845 | dirName += path; // dirName = fDBFolder/path | |
846 | Bool_t result=kFALSE; | |
847 | if (gGrid->Cd(dirName,0)) result=kTRUE; | |
848 | gGrid->Cd(initDir.Data(),0); | |
849 | return result; | |
850 | } | |
851 | ||
62032124 | 852 | //_____________________________________________________________________________ |
853 | void AliCDBGrid::QueryValidFiles() | |
854 | { | |
855 | // Query the CDB for files valid for AliCDBStorage::fRun | |
856 | // fills list fValidFileIds with AliCDBId objects created from file name | |
857 | ||
858 | TString filter; | |
6dc56e97 | 859 | MakeQueryFilter(fRun, fRun, fMetaDataFilter, filter); |
860 | ||
861 | TString pattern = Form("%s/Run*", fPathFilter.GetPath().Data()); | |
4005d0b5 | 862 | if(fVersion >= 0) pattern += Form("_v%d*", fVersion); |
6dc56e97 | 863 | pattern += ".root"; |
864 | AliDebug(2,Form("pattern: %s", pattern.Data())); | |
865 | ||
866 | TGridResult *res = gGrid->Query(fDBFolder, pattern, filter, ""); | |
62032124 | 867 | |
b7e6419d | 868 | if (!res) { |
869 | AliError("Grid query failed"); | |
870 | return; | |
871 | } | |
872 | ||
b68a24b3 | 873 | TIter next(res); |
874 | TMap *map; | |
875 | while ((map = (TMap*)next())) { | |
876 | TObjString *entry; | |
877 | if ((entry = (TObjString *) ((TMap *)map)->GetValue("lfn"))) { | |
878 | TString& filename = entry->String(); | |
879 | if(filename.IsNull()) continue; | |
880 | AliDebug(2,Form("Found valid file: %s", filename.Data())); | |
881 | AliCDBId *validFileId = new AliCDBId; | |
882 | Bool_t result = FilenameToId(filename, *validFileId); | |
883 | if(result) { | |
884 | fValidFileIds.AddLast(validFileId); | |
885 | } | |
886 | else { | |
887 | delete validFileId; | |
888 | } | |
889 | } | |
62032124 | 890 | } |
891 | delete res; | |
892 | ||
893 | } | |
894 | ||
895 | //_____________________________________________________________________________ | |
c3a7b59a | 896 | void AliCDBGrid::MakeQueryFilter(Int_t firstRun, Int_t lastRun, |
62032124 | 897 | const AliCDBMetaData* md, TString& result) const |
898 | { | |
899 | // create filter for file query | |
900 | ||
c3a7b59a | 901 | result = Form("CDB:first_run<=%d and CDB:last_run>=%d", firstRun, lastRun); |
62032124 | 902 | |
6dc56e97 | 903 | // if(version >= 0) { |
904 | // result += Form(" and CDB:version=%d", version); | |
905 | // } | |
906 | // if(pathFilter.GetLevel0() != "*") { | |
907 | // result += Form(" and CDB:path_level_0=\"%s\"", pathFilter.GetLevel0().Data()); | |
908 | // } | |
909 | // if(pathFilter.GetLevel1() != "*") { | |
910 | // result += Form(" and CDB:path_level_1=\"%s\"", pathFilter.GetLevel1().Data()); | |
911 | // } | |
912 | // if(pathFilter.GetLevel2() != "*") { | |
913 | // result += Form(" and CDB:path_level_2=\"%s\"", pathFilter.GetLevel2().Data()); | |
914 | // } | |
62032124 | 915 | |
916 | if(md){ | |
917 | if(md->GetObjectClassName()[0] != '\0') { | |
918 | result += Form(" and CDB_MD:object_classname=\"%s\"", md->GetObjectClassName()); | |
919 | } | |
920 | if(md->GetResponsible()[0] != '\0') { | |
921 | result += Form(" and CDB_MD:responsible=\"%s\"", md->GetResponsible()); | |
922 | } | |
923 | if(md->GetBeamPeriod() != 0) { | |
924 | result += Form(" and CDB_MD:beam_period=%d", md->GetBeamPeriod()); | |
925 | } | |
926 | if(md->GetAliRootVersion()[0] != '\0') { | |
927 | result += Form(" and CDB_MD:aliroot_version=\"%s\"", md->GetAliRootVersion()); | |
928 | } | |
929 | if(md->GetComment()[0] != '\0') { | |
930 | result += Form(" and CDB_MD:comment=\"%s\"", md->GetComment()); | |
931 | } | |
932 | } | |
933 | AliDebug(2, Form("filter: %s",result.Data())); | |
934 | ||
935 | } | |
936 | ||
c3a7b59a | 937 | //_____________________________________________________________________________ |
938 | Int_t AliCDBGrid::GetLatestVersion(const char* path, Int_t run){ | |
939 | // get last version found in the database valid for run and path | |
940 | ||
c3a7b59a | 941 | AliCDBPath aCDBPath(path); |
942 | if(!aCDBPath.IsValid() || aCDBPath.IsWildcard()) { | |
943 | AliError(Form("Invalid path in request: %s", path)); | |
944 | return -1; | |
945 | } | |
946 | AliCDBId query(path, run, run, -1, -1); | |
947 | AliCDBId* dataId = 0; | |
948 | ||
949 | // look for file matching query requests (path, runRange, version) | |
6dc56e97 | 950 | if(run == fRun && fPathFilter.Comprises(aCDBPath) && fVersion < 0){ |
c3a7b59a | 951 | // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds() |
952 | AliDebug(2, Form("List of files valid for run %d and for path %s was loaded. Looking there!", | |
953 | run, path)); | |
954 | dataId = GetId(fValidFileIds, query); | |
955 | if (!dataId) return -1; | |
4667c116 | 956 | Int_t version = dataId->GetVersion(); |
957 | delete dataId; | |
958 | return version; | |
c3a7b59a | 959 | |
960 | } | |
961 | // List of files valid for reqested run was not loaded. Looking directly into CDB | |
962 | AliDebug(2, Form("List of files valid for run %d and for path %s was not loaded. Looking directly into CDB!", | |
963 | run, path)); | |
964 | ||
4667c116 | 965 | TObjArray validFileIds; |
966 | validFileIds.SetOwner(1); | |
967 | ||
c3a7b59a | 968 | TString filter; |
6dc56e97 | 969 | MakeQueryFilter(run, run, 0, filter); |
970 | ||
971 | TString pattern = Form("%s/Run*.root", path); | |
972 | AliDebug(2,Form("pattern: %s", pattern.Data())); | |
c3a7b59a | 973 | |
6dc56e97 | 974 | TGridResult *res = gGrid->Query(fDBFolder, pattern, filter, ""); |
c3a7b59a | 975 | AliCDBId validFileId; |
976 | for(int i=0; i<res->GetEntries(); i++){ | |
977 | TString filename = res->GetKey(i, "lfn"); | |
978 | if(filename == "") continue; | |
979 | if(FilenameToId(filename, validFileId)) | |
980 | validFileIds.AddLast(validFileId.Clone()); | |
981 | } | |
982 | delete res; | |
983 | ||
984 | dataId = GetId(validFileIds, query); | |
985 | if (!dataId) return -1; | |
986 | ||
4667c116 | 987 | Int_t version = dataId->GetVersion(); |
988 | delete dataId; | |
989 | return version; | |
c3a7b59a | 990 | |
991 | } | |
992 | ||
993 | //_____________________________________________________________________________ | |
994 | Int_t AliCDBGrid::GetLatestSubVersion(const char* /*path*/, Int_t /*run*/, Int_t /*version*/){ | |
995 | // get last subversion found in the database valid for run and path | |
996 | AliError("Objects in GRID storage have no sub version!"); | |
997 | return -1; | |
998 | } | |
999 | ||
1000 | ||
9e1ceb13 | 1001 | ///////////////////////////////////////////////////////////////////////////////////////////////// |
1002 | // // | |
1003 | // AliCDBGrid factory // | |
1004 | // // | |
1005 | ///////////////////////////////////////////////////////////////////////////////////////////////// | |
1006 | ||
1007 | ClassImp(AliCDBGridFactory) | |
1008 | ||
1009 | //_____________________________________________________________________________ | |
1010 | Bool_t AliCDBGridFactory::Validate(const char* gridString) { | |
1011 | // check if the string is valid Grid URI | |
1012 | ||
b05400be | 1013 | TRegexp gridPattern("^alien://.+$"); |
9e1ceb13 | 1014 | |
27cac7e3 | 1015 | return TString(gridString).Contains(gridPattern); |
9e1ceb13 | 1016 | } |
1017 | ||
1018 | //_____________________________________________________________________________ | |
1019 | AliCDBParam* AliCDBGridFactory::CreateParameter(const char* gridString) { | |
1020 | // create AliCDBGridParam class from the URI string | |
1021 | ||
1022 | if (!Validate(gridString)) { | |
1023 | return NULL; | |
1024 | } | |
c75fabed | 1025 | |
b05400be | 1026 | TString buffer(gridString); |
c3a7b59a | 1027 | |
1028 | TString gridUrl = "alien://"; | |
b05400be | 1029 | TString user = ""; |
4667c116 | 1030 | TString dbFolder = ""; |
fb33abde | 1031 | TString se = "default"; |
c75fabed | 1032 | TString cacheFolder = ""; |
1033 | Bool_t operateDisconnected = kTRUE; | |
1034 | Long64_t cacheSize = (UInt_t) 1024*1024*1024; // 1GB | |
1035 | Long_t cleanupInterval = 0; | |
b05400be | 1036 | |
1037 | TObjArray *arr = buffer.Tokenize('?'); | |
1038 | TIter iter(arr); | |
62032124 | 1039 | TObjString *str = 0; |
4667c116 | 1040 | |
b05400be | 1041 | while((str = (TObjString*) iter.Next())){ |
1042 | TString entry(str->String()); | |
1043 | Int_t indeq = entry.Index('='); | |
1044 | if(indeq == -1) { | |
1045 | if(entry.BeginsWith("alien://")) { // maybe it's a gridUrl! | |
1046 | gridUrl = entry; | |
1047 | continue; | |
1048 | } else { | |
1049 | AliError(Form("Invalid entry! %s",entry.Data())); | |
1050 | continue; | |
1051 | } | |
1052 | } | |
1053 | ||
1054 | TString key = entry(0,indeq); | |
1055 | TString value = entry(indeq+1,entry.Length()-indeq); | |
1056 | ||
1057 | if(key.Contains("grid",TString::kIgnoreCase)) { | |
1058 | gridUrl += value; | |
c75fabed | 1059 | } |
b05400be | 1060 | else if (key.Contains("user",TString::kIgnoreCase)){ |
1061 | user = value; | |
1062 | } | |
b05400be | 1063 | else if (key.Contains("se",TString::kIgnoreCase)){ |
1064 | se = value; | |
1065 | } | |
d7744171 | 1066 | else if (key.Contains("cacheF",TString::kIgnoreCase)){ |
c75fabed | 1067 | cacheFolder = value; |
1068 | if (!cacheFolder.EndsWith("/")) | |
1069 | cacheFolder += "/"; | |
1070 | } | |
d7744171 | 1071 | else if (key.Contains("folder",TString::kIgnoreCase)){ |
1072 | dbFolder = value; | |
1073 | } | |
c75fabed | 1074 | else if (key.Contains("operateDisc",TString::kIgnoreCase)){ |
1075 | if(value == "kTRUE") { | |
1076 | operateDisconnected = kTRUE; | |
1077 | } else if (value == "kFALSE") { | |
1078 | operateDisconnected = kFALSE; | |
1079 | } else if (value == "0" || value == "1") { | |
1080 | operateDisconnected = (Bool_t) value.Atoi(); | |
1081 | } else { | |
1082 | AliError(Form("Invalid entry! %s",entry.Data())); | |
1083 | return NULL; | |
1084 | } | |
1085 | } | |
d7744171 | 1086 | else if (key.Contains("cacheS",TString::kIgnoreCase)){ |
c75fabed | 1087 | if(value.IsDigit()) { |
1088 | cacheSize = value.Atoi(); | |
1089 | } else { | |
1090 | AliError(Form("Invalid entry! %s",entry.Data())); | |
1091 | return NULL; | |
1092 | } | |
1093 | } | |
1094 | else if (key.Contains("cleanupInt",TString::kIgnoreCase)){ | |
1095 | if(value.IsDigit()) { | |
1096 | cleanupInterval = value.Atoi(); | |
1097 | } else { | |
1098 | AliError(Form("Invalid entry! %s",entry.Data())); | |
1099 | return NULL; | |
1100 | } | |
1101 | } | |
b05400be | 1102 | else{ |
1103 | AliError(Form("Invalid entry! %s",entry.Data())); | |
c75fabed | 1104 | return NULL; |
b05400be | 1105 | } |
1106 | } | |
1107 | delete arr; arr=0; | |
c75fabed | 1108 | |
1109 | AliDebug(2, Form("gridUrl: %s", gridUrl.Data())); | |
1110 | AliDebug(2, Form("user: %s", user.Data())); | |
1111 | AliDebug(2, Form("dbFolder: %s", dbFolder.Data())); | |
1112 | AliDebug(2, Form("s.e.: %s", se.Data())); | |
1113 | AliDebug(2, Form("local cache folder: %s", cacheFolder.Data())); | |
1114 | AliDebug(2, Form("local cache operate disconnected: %d", operateDisconnected)); | |
1115 | AliDebug(2, Form("local cache size: %d", cacheSize)); | |
1116 | AliDebug(2, Form("local cache cleanup interval: %d", cleanupInterval)); | |
9e1ceb13 | 1117 | |
27cac7e3 | 1118 | if(dbFolder == ""){ |
c75fabed | 1119 | AliError("Base folder must be specified!"); |
4667c116 | 1120 | return NULL; |
1121 | } | |
1122 | ||
c75fabed | 1123 | return new AliCDBGridParam(gridUrl.Data(), user.Data(), |
1124 | dbFolder.Data(), se.Data(), cacheFolder.Data(), | |
1125 | operateDisconnected, cacheSize, cleanupInterval); | |
9e1ceb13 | 1126 | } |
1127 | ||
1128 | //_____________________________________________________________________________ | |
1129 | AliCDBStorage* AliCDBGridFactory::Create(const AliCDBParam* param) { | |
1130 | // create AliCDBGrid storage instance from parameters | |
1131 | ||
62032124 | 1132 | AliCDBGrid *grid = 0; |
9e1ceb13 | 1133 | if (AliCDBGridParam::Class() == param->IsA()) { |
62032124 | 1134 | |
9e1ceb13 | 1135 | const AliCDBGridParam* gridParam = (const AliCDBGridParam*) param; |
62032124 | 1136 | grid = new AliCDBGrid(gridParam->GridUrl().Data(), |
1137 | gridParam->GetUser().Data(), | |
1138 | gridParam->GetDBFolder().Data(), | |
c75fabed | 1139 | gridParam->GetSE().Data(), |
1140 | gridParam->GetCacheFolder().Data(), | |
1141 | gridParam->GetOperateDisconnected(), | |
1142 | gridParam->GetCacheSize(), | |
1143 | gridParam->GetCleanupInterval()); | |
9e1ceb13 | 1144 | |
9e1ceb13 | 1145 | } |
1146 | ||
c3a7b59a | 1147 | if(!gGrid && grid) { |
1148 | delete grid; grid=0; | |
1149 | } | |
1150 | ||
62032124 | 1151 | return grid; |
9e1ceb13 | 1152 | } |
1153 | ||
1154 | ///////////////////////////////////////////////////////////////////////////////////////////////// | |
1155 | // // | |
c75fabed | 1156 | // AliCDBGrid Parameter class // // |
9e1ceb13 | 1157 | // // |
1158 | ///////////////////////////////////////////////////////////////////////////////////////////////// | |
1159 | ||
1160 | ClassImp(AliCDBGridParam) | |
1161 | ||
1162 | //_____________________________________________________________________________ | |
62032124 | 1163 | AliCDBGridParam::AliCDBGridParam(): |
1164 | AliCDBParam(), | |
1165 | fGridUrl(), | |
1166 | fUser(), | |
1167 | fDBFolder(), | |
c75fabed | 1168 | fSE(), |
1169 | fCacheFolder(), | |
1170 | fOperateDisconnected(), | |
1171 | fCacheSize(), | |
1172 | fCleanupInterval() | |
1173 | ||
62032124 | 1174 | { |
9e1ceb13 | 1175 | // default constructor |
1176 | ||
1177 | } | |
1178 | ||
1179 | //_____________________________________________________________________________ | |
c75fabed | 1180 | AliCDBGridParam::AliCDBGridParam(const char* gridUrl, const char* user, const char* dbFolder, |
1181 | const char* se, const char* cacheFolder, Bool_t operateDisconnected, | |
1182 | Long64_t cacheSize, Long_t cleanupInterval): | |
62032124 | 1183 | AliCDBParam(), |
b05400be | 1184 | fGridUrl(gridUrl), |
9e1ceb13 | 1185 | fUser(user), |
b05400be | 1186 | fDBFolder(dbFolder), |
c75fabed | 1187 | fSE(se), |
1188 | fCacheFolder(cacheFolder), | |
1189 | fOperateDisconnected(operateDisconnected), | |
1190 | fCacheSize(cacheSize), | |
1191 | fCleanupInterval(cleanupInterval) | |
9e1ceb13 | 1192 | { |
1193 | // constructor | |
c75fabed | 1194 | |
9e1ceb13 | 1195 | SetType("alien"); |
1196 | ||
c75fabed | 1197 | TString uri = Form("%s?User=%s?DBFolder=%s?SE=%s?CacheFolder=%s" |
1198 | "?OperateDisconnected=%d?CacheSize=%d?CleanupInterval=%d", | |
62032124 | 1199 | fGridUrl.Data(), fUser.Data(), |
c75fabed | 1200 | fDBFolder.Data(), fSE.Data(), fCacheFolder.Data(), |
1201 | fOperateDisconnected, fCacheSize, fCleanupInterval); | |
62032124 | 1202 | |
1203 | SetURI(uri.Data()); | |
9e1ceb13 | 1204 | } |
1205 | ||
1206 | //_____________________________________________________________________________ | |
1207 | AliCDBGridParam::~AliCDBGridParam() { | |
1208 | // destructor | |
1209 | ||
1210 | } | |
1211 | ||
1212 | //_____________________________________________________________________________ | |
1213 | AliCDBParam* AliCDBGridParam::CloneParam() const { | |
1214 | // clone parameter | |
1215 | ||
62032124 | 1216 | return new AliCDBGridParam(fGridUrl.Data(), fUser.Data(), |
c75fabed | 1217 | fDBFolder.Data(), fSE.Data(), fCacheFolder.Data(), |
1218 | fOperateDisconnected, fCacheSize, fCleanupInterval); | |
9e1ceb13 | 1219 | } |
1220 | ||
1221 | //_____________________________________________________________________________ | |
1222 | ULong_t AliCDBGridParam::Hash() const { | |
1223 | // return Hash function | |
1224 | ||
c75fabed | 1225 | return fGridUrl.Hash()+fUser.Hash()+fDBFolder.Hash()+fSE.Hash()+fCacheFolder.Hash(); |
9e1ceb13 | 1226 | } |
1227 | ||
1228 | //_____________________________________________________________________________ | |
1229 | Bool_t AliCDBGridParam::IsEqual(const TObject* obj) const { | |
1230 | // check if this object is equal to AliCDBParam obj | |
1231 | ||
1232 | if (this == obj) { | |
1233 | return kTRUE; | |
1234 | } | |
1235 | ||
1236 | if (AliCDBGridParam::Class() != obj->IsA()) { | |
1237 | return kFALSE; | |
1238 | } | |
1239 | ||
1240 | AliCDBGridParam* other = (AliCDBGridParam*) obj; | |
1241 | ||
b05400be | 1242 | if(fGridUrl != other->fGridUrl) return kFALSE; |
9e1ceb13 | 1243 | if(fUser != other->fUser) return kFALSE; |
b05400be | 1244 | if(fDBFolder != other->fDBFolder) return kFALSE; |
9e1ceb13 | 1245 | if(fSE != other->fSE) return kFALSE; |
c75fabed | 1246 | if(fCacheFolder != other->fCacheFolder) return kFALSE; |
1247 | if(fOperateDisconnected != other->fOperateDisconnected) return kFALSE; | |
1248 | if(fCacheSize != other->fCacheSize) return kFALSE; | |
1249 | if(fCleanupInterval != other->fCleanupInterval) return kFALSE; | |
9e1ceb13 | 1250 | return kTRUE; |
1251 | } | |
1252 |