]>
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){ |
6720fa1f | 64 | AliInfo(Form("gGrid = %p; fGridUrl = %s; gGrid->GridUrl() = %s",gGrid,fGridUrl.Data(), gGrid->GridUrl())); |
024cf675 | 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 " | |
d2a0a483 | 125 | "to ShrinkCacheFileDir: cacheSize = %lld, cleanupInterval = %ld !", |
c75fabed | 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++){ |
0e10c8fd | 236 | //TString cmd("find -d "); |
237 | //cmd += Form("%s ",dirName); | |
238 | //cmd += | |
239 | //gGrid->Command(cmd.Data()); | |
4667c116 | 240 | dirName+=Form("%s/",id.GetPathLevel(i).Data()); |
9e1ceb13 | 241 | dirExist=gGrid->Cd(dirName,0); |
242 | if (!dirExist) { | |
b05400be | 243 | AliDebug(2,Form("Creating new folder <%s> ...",dirName.Data())); |
9e1ceb13 | 244 | if(!gGrid->Mkdir(dirName,"",0)){ |
245 | AliError(Form("Cannot create directory <%s> !",dirName.Data())); | |
246 | gGrid->Cd(initDir.Data()); | |
247 | return kFALSE; | |
248 | } | |
4667c116 | 249 | |
250 | // if folders are new add tags to them | |
251 | if(i == 1) { | |
252 | // TODO Currently disabled | |
c75fabed | 253 | // add short lived tag! |
4667c116 | 254 | // AliInfo("Tagging level 1 folder with \"ShortLived\" tag"); |
255 | // if(!AddTag(dirName,"ShortLived_try")){ | |
256 | // AliError(Form("Could not tag folder %s !", dirName.Data())); | |
257 | // if(!gGrid->Rmdir(dirName.Data())){ | |
258 | // AliError(Form("Unexpected: could not remove %s directory!", dirName.Data())); | |
259 | // } | |
260 | // return 0; | |
261 | //} | |
262 | ||
263 | } else if(i == 2) { | |
c75fabed | 264 | AliDebug(2,"Tagging level 2 folder with \"CDB\" and \"CDB_MD\" tag"); |
4667c116 | 265 | if(!AddTag(dirName,"CDB")){ |
266 | AliError(Form("Could not tag folder %s !", dirName.Data())); | |
267 | if(!gGrid->Rmdir(dirName.Data())){ | |
268 | AliError(Form("Unexpected: could not remove %s directory!", dirName.Data())); | |
269 | } | |
270 | return 0; | |
271 | } | |
272 | if(!AddTag(dirName,"CDB_MD")){ | |
273 | AliError(Form("Could not tag folder %s !", dirName.Data())); | |
274 | if(!gGrid->Rmdir(dirName.Data())){ | |
275 | AliError(Form("Unexpected: could not remove %s directory!", dirName.Data())); | |
276 | } | |
277 | return 0; | |
278 | } | |
279 | ||
4667c116 | 280 | // TODO Currently disabled |
c75fabed | 281 | // add short lived tag! |
4667c116 | 282 | // TString path=id.GetPath(); |
283 | // if(AliCDBManager::Instance()->IsShortLived(path.Data())) { | |
284 | // AliInfo(Form("Tagging %s as short lived", dirName.Data())); | |
285 | // if(!TagShortLived(dirName, kTRUE)){ | |
286 | // AliError(Form("Could not tag folder %s !", dirName.Data())); | |
287 | // if(!gGrid->Rmdir(dirName.Data())){ | |
288 | // AliError(Form("Unexpected: could not remove %s directory!", dirName.Data())); | |
289 | // } | |
290 | // return 0; | |
291 | // } | |
292 | // } else { | |
293 | // AliInfo(Form("Tagging %s as long lived", dirName.Data())); | |
294 | // if(!TagShortLived(dirName, kFALSE)){ | |
295 | // AliError(Form("Could not tag folder %s !", dirName.Data())); | |
296 | // if(!gGrid->Rmdir(dirName.Data())){ | |
297 | // AliError(Form("Unexpected: could not remove %s directory!", dirName.Data())); | |
298 | // } | |
299 | // return 0; | |
300 | // } | |
301 | // } | |
302 | } | |
9e1ceb13 | 303 | } |
304 | } | |
9e1ceb13 | 305 | gGrid->Cd(initDir,0); |
306 | ||
62032124 | 307 | TString filename; |
308 | AliCDBId anId; // the id got from filename | |
9e1ceb13 | 309 | AliCDBRunRange lastRunRange(-1,-1); // highest runRange found |
9e1ceb13 | 310 | Int_t lastVersion=0; // highest version found |
311 | ||
312 | TGridResult *res = gGrid->Ls(dirName); | |
313 | ||
314 | //loop on the files in the directory, look for highest version | |
315 | for(int i=0; i < res->GetEntries(); i++){ | |
62032124 | 316 | filename=res->GetFileNamePath(i); |
317 | if (!FilenameToId(filename, anId)) continue; | |
318 | if (anId.GetAliCDBRunRange().Overlaps(id.GetAliCDBRunRange()) && anId.GetVersion() > lastVersion) { | |
319 | lastVersion = anId.GetVersion(); | |
320 | lastRunRange = anId.GetAliCDBRunRange(); | |
9e1ceb13 | 321 | } |
322 | ||
323 | } | |
4005d0b5 | 324 | delete res; |
325 | ||
be1b1008 | 326 | // GRP entries with explicitly set version escape default incremental versioning |
327 | if(id.GetPath().Contains("GRP") && id.HasVersion() && lastVersion!=0) | |
328 | { | |
329 | AliDebug(5,Form("Entry %s won't be put in the destination OCDB", id.ToString().Data())); | |
330 | return kFALSE; | |
1ac347b2 | 331 | } |
05ca3c19 | 332 | |
333 | id.SetVersion(lastVersion + 1); | |
4005d0b5 | 334 | id.SetSubVersion(0); |
9e1ceb13 | 335 | |
336 | TString lastStorage = id.GetLastStorage(); | |
337 | if(lastStorage.Contains(TString("new"), TString::kIgnoreCase) && id.GetVersion() > 1 ){ | |
84090f85 | 338 | AliDebug(2, Form("A NEW object is being stored with version %d", |
9e1ceb13 | 339 | id.GetVersion())); |
84090f85 | 340 | AliDebug(2, Form("and it will hide previously stored object with version %d!", |
9e1ceb13 | 341 | id.GetVersion()-1)); |
342 | } | |
343 | ||
4005d0b5 | 344 | if(!lastRunRange.IsAnyRange() && !(lastRunRange.IsEqual(&id.GetAliCDBRunRange()))) |
9e1ceb13 | 345 | AliWarning(Form("Run range modified w.r.t. previous version (Run%d_%d_v%d)", |
346 | lastRunRange.GetFirstRun(), lastRunRange.GetLastRun(), id.GetVersion())); | |
4005d0b5 | 347 | |
9e1ceb13 | 348 | return kTRUE; |
349 | } | |
350 | ||
351 | //_____________________________________________________________________________ | |
6dc56e97 | 352 | AliCDBId* AliCDBGrid::GetId(const TObjArray& validFileIds, const AliCDBId& query) { |
62032124 | 353 | // look for the Id that matches query's requests (highest or exact version) |
9e1ceb13 | 354 | |
98873e41 | 355 | if(validFileIds.GetEntriesFast() < 1) |
62032124 | 356 | return NULL; |
9e1ceb13 | 357 | |
62032124 | 358 | TIter iter(&validFileIds); |
9e1ceb13 | 359 | |
62032124 | 360 | AliCDBId *anIdPtr=0; |
361 | AliCDBId* result=0; | |
9e1ceb13 | 362 | |
62032124 | 363 | while((anIdPtr = dynamic_cast<AliCDBId*> (iter.Next()))){ |
62032124 | 364 | if(anIdPtr->GetPath() != query.GetPath()) continue; |
365 | ||
366 | //if(!CheckVersion(query, anIdPtr, result)) return NULL; | |
9e1ceb13 | 367 | |
368 | if (!query.HasVersion()){ // look for highest version | |
62032124 | 369 | if(result && result->GetVersion() > anIdPtr->GetVersion()) continue; |
370 | if(result && result->GetVersion() == anIdPtr->GetVersion()) { | |
4667c116 | 371 | AliError(Form("More than one object valid for run %d, version %d!", |
62032124 | 372 | query.GetFirstRun(), anIdPtr->GetVersion())); |
373 | return NULL; | |
9e1ceb13 | 374 | } |
62032124 | 375 | result = anIdPtr; |
9e1ceb13 | 376 | } else { // look for specified version |
62032124 | 377 | if(query.GetVersion() != anIdPtr->GetVersion()) continue; |
378 | if(result && result->GetVersion() == anIdPtr->GetVersion()){ | |
4667c116 | 379 | AliError(Form("More than one object valid for run %d, version %d!", |
62032124 | 380 | query.GetFirstRun(), anIdPtr->GetVersion())); |
381 | return NULL; | |
9e1ceb13 | 382 | } |
62032124 | 383 | result = anIdPtr; |
9e1ceb13 | 384 | } |
62032124 | 385 | |
386 | } | |
c75fabed | 387 | |
388 | if (!result) return NULL; | |
62032124 | 389 | |
4667c116 | 390 | return dynamic_cast<AliCDBId*> (result->Clone()); |
62032124 | 391 | } |
392 | ||
9e1ceb13 | 393 | //_____________________________________________________________________________ |
4667c116 | 394 | AliCDBId* AliCDBGrid::GetEntryId(const AliCDBId& queryId) { |
395 | // get AliCDBId from the database | |
396 | // User must delete returned object | |
9e1ceb13 | 397 | |
62032124 | 398 | AliCDBId* dataId=0; |
399 | ||
400 | AliCDBId selectedId(queryId); | |
401 | if (!selectedId.HasVersion()) { | |
9e1ceb13 | 402 | // if version is not specified, first check the selection criteria list |
4b5e0dce | 403 | GetSelection(&selectedId); |
62032124 | 404 | } |
405 | ||
6dc56e97 | 406 | TObjArray validFileIds; |
62032124 | 407 | validFileIds.SetOwner(1); |
408 | ||
409 | // look for file matching query requests (path, runRange, version) | |
410 | if(selectedId.GetFirstRun() == fRun && | |
c3a7b59a | 411 | fPathFilter.Comprises(selectedId.GetAliCDBPath()) && fVersion < 0 && !fMetaDataFilter){ |
62032124 | 412 | // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds() |
98873e41 | 413 | AliDebug(2, Form("List of files valid for run %d was loaded. Looking there for fileids valid for path %s!", |
62032124 | 414 | selectedId.GetFirstRun(), selectedId.GetPath().Data())); |
415 | dataId = GetId(fValidFileIds, selectedId); | |
416 | ||
9e1ceb13 | 417 | } else { |
62032124 | 418 | // List of files valid for reqested run was not loaded. Looking directly into CDB |
98873e41 | 419 | AliDebug(2, Form("List of files valid for run %d was not loaded. Looking directly into CDB for fileids valid for path %s!", |
62032124 | 420 | selectedId.GetFirstRun(), selectedId.GetPath().Data())); |
421 | ||
422 | TString filter; | |
6dc56e97 | 423 | MakeQueryFilter(selectedId.GetFirstRun(), selectedId.GetLastRun(), 0, filter); |
62032124 | 424 | |
6dc56e97 | 425 | TString pattern = Form("%s/Run*", selectedId.GetPath().Data()); |
0e10c8fd | 426 | TString optionQuery = "-y"; |
427 | if(selectedId.GetVersion() >= 0) { | |
428 | pattern += Form("_v%d*",selectedId.GetVersion()); | |
429 | optionQuery = ""; | |
430 | } | |
431 | ||
6dc56e97 | 432 | pattern += ".root"; |
62ea513e | 433 | TString det = pattern(0,4); |
434 | TString patternCopy(pattern); | |
435 | patternCopy.Remove(0,4); | |
436 | TString folderCopy(fDBFolder); | |
437 | folderCopy += det; | |
6dc56e97 | 438 | |
0e10c8fd | 439 | if (optionQuery == "-y"){ |
440 | AliInfo("Only latest version will be returned"); | |
441 | } | |
442 | ||
62ea513e | 443 | AliDebug(2,Form("** fDBFolder = %s, pattern = %s, filter = %s",folderCopy.Data(), patternCopy.Data(), filter.Data())); |
444 | TGridResult *res = gGrid->Query(folderCopy, patternCopy, filter, optionQuery.Data()); | |
ecae96f8 | 445 | if (res) { |
446 | AliCDBId validFileId; | |
447 | for(int i=0; i<res->GetEntries(); i++){ | |
448 | TString filename = res->GetKey(i, "lfn"); | |
449 | if(filename == "") continue; | |
450 | if(FilenameToId(filename, validFileId)) | |
451 | validFileIds.AddLast(validFileId.Clone()); | |
452 | } | |
453 | delete res; | |
454 | } | |
62032124 | 455 | dataId = GetId(validFileIds, selectedId); |
9e1ceb13 | 456 | } |
457 | ||
4667c116 | 458 | return dataId; |
459 | } | |
460 | ||
461 | //_____________________________________________________________________________ | |
462 | AliCDBEntry* AliCDBGrid::GetEntry(const AliCDBId& queryId) { | |
463 | // get AliCDBEntry from the database | |
464 | ||
465 | AliCDBId* dataId = GetEntryId(queryId); | |
466 | ||
62032124 | 467 | if (!dataId) return NULL; |
9e1ceb13 | 468 | |
469 | TString filename; | |
62032124 | 470 | if (!IdToFilename(*dataId, filename)) { |
024cf675 | 471 | AliDebug(2,Form("Bad data ID encountered! Subnormal error!")); |
4667c116 | 472 | delete dataId; |
9e1ceb13 | 473 | return NULL; |
474 | } | |
475 | ||
62032124 | 476 | AliCDBEntry* anEntry = GetEntryFromFile(filename, dataId); |
477 | ||
4667c116 | 478 | delete dataId; |
62032124 | 479 | return anEntry; |
480 | } | |
481 | ||
482 | //_____________________________________________________________________________ | |
8e245d15 | 483 | AliCDBEntry* AliCDBGrid::GetEntryFromFile(TString& filename, AliCDBId* dataId){ |
62032124 | 484 | // Get AliCBEntry object from file "filename" |
9e1ceb13 | 485 | |
024cf675 | 486 | AliDebug(2,Form("Opening file: %s",filename.Data())); |
62032124 | 487 | |
488 | filename.Prepend("/alien"); | |
c75fabed | 489 | |
490 | // if option="CACHEREAD" TFile will use the local caching facility! | |
491 | TString option="READ"; | |
492 | if(fCacheFolder != ""){ | |
493 | ||
494 | // Check if local cache folder was changed in the meanwhile | |
495 | TString origCache(TFile::GetCacheFileDir()); | |
496 | if(fCacheFolder != origCache) { | |
497 | AliWarning(Form("Local cache folder has been overwritten!! fCacheFolder = %s origCache = %s", | |
498 | fCacheFolder.Data(), origCache.Data())); | |
499 | TFile::SetCacheFileDir(fCacheFolder.Data(), fOperateDisconnected); | |
500 | TFile::ShrinkCacheFileDir(fCacheSize, fCleanupInterval); | |
501 | } | |
502 | ||
503 | option.Prepend("CACHE"); | |
504 | } | |
505 | ||
506 | AliDebug(2, Form("Option: %s", option.Data())); | |
507 | ||
508 | TFile *file = TFile::Open(filename, option); | |
9e1ceb13 | 509 | if (!file) { |
024cf675 | 510 | AliDebug(2,Form("Can't open file <%s>!", filename.Data())); |
9e1ceb13 | 511 | return NULL; |
512 | } | |
513 | ||
514 | // get the only AliCDBEntry object from the file | |
515 | // the object in the file is an AliCDBEntry entry named "AliCDBEntry" | |
516 | ||
62032124 | 517 | AliCDBEntry* anEntry = dynamic_cast<AliCDBEntry*> (file->Get("AliCDBEntry")); |
9e1ceb13 | 518 | |
62032124 | 519 | if (!anEntry) { |
520 | AliDebug(2,Form("Bad storage data: file does not contain an AliCDBEntry object!")); | |
521 | file->Close(); | |
9e1ceb13 | 522 | return NULL; |
523 | } | |
524 | ||
9e1ceb13 | 525 | // The object's Id is not reset during storage |
526 | // If object's Id runRange or version do not match with filename, | |
527 | // it means that someone renamed file by hand. In this case a warning msg is issued. | |
62032124 | 528 | |
529 | if(anEntry){ | |
530 | AliCDBId entryId = anEntry->GetId(); | |
4005d0b5 | 531 | Int_t tmpSubVersion = dataId->GetSubVersion(); |
532 | dataId->SetSubVersion(entryId.GetSubVersion()); // otherwise filename and id may mismatch | |
8e245d15 | 533 | if(!entryId.IsEqual(dataId)){ |
b8ec52f6 | 534 | AliWarning(Form("Mismatch between file name and object's Id!")); |
535 | AliWarning(Form("File name: %s", dataId->ToString().Data())); | |
536 | AliWarning(Form("Object's Id: %s", entryId.ToString().Data())); | |
62032124 | 537 | } |
4005d0b5 | 538 | dataId->SetSubVersion(tmpSubVersion); |
9e1ceb13 | 539 | } |
540 | ||
62032124 | 541 | anEntry->SetLastStorage("grid"); |
542 | ||
145f45a2 | 543 | // Check whether entry contains a TTree. In case load the tree in memory! |
544 | LoadTreeFromFile(anEntry); | |
545 | ||
9e1ceb13 | 546 | // close file, return retieved entry |
547 | file->Close(); delete file; file=0; | |
62032124 | 548 | |
549 | return anEntry; | |
9e1ceb13 | 550 | } |
551 | ||
552 | //_____________________________________________________________________________ | |
62032124 | 553 | TList* AliCDBGrid::GetEntries(const AliCDBId& queryId) { |
9e1ceb13 | 554 | // multiple request (AliCDBStorage::GetAll) |
555 | ||
62032124 | 556 | TList* result = new TList(); |
557 | result->SetOwner(); | |
9e1ceb13 | 558 | |
6dc56e97 | 559 | TObjArray validFileIds; |
62032124 | 560 | validFileIds.SetOwner(1); |
9e1ceb13 | 561 | |
62032124 | 562 | Bool_t alreadyLoaded = kFALSE; |
9e1ceb13 | 563 | |
62032124 | 564 | // look for file matching query requests (path, runRange) |
565 | if(queryId.GetFirstRun() == fRun && | |
c3a7b59a | 566 | fPathFilter.Comprises(queryId.GetAliCDBPath()) && fVersion < 0 && !fMetaDataFilter){ |
62032124 | 567 | // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds() |
568 | AliDebug(2,Form("List of files valid for run %d and for path %s was loaded. Looking there!", | |
569 | queryId.GetFirstRun(), queryId.GetPath().Data())); | |
9e1ceb13 | 570 | |
62032124 | 571 | alreadyLoaded = kTRUE; |
9e1ceb13 | 572 | |
62032124 | 573 | } else { |
574 | // List of files valid for reqested run was not loaded. Looking directly into CDB | |
575 | AliDebug(2,Form("List of files valid for run %d and for path %s was not loaded. Looking directly into CDB!", | |
576 | queryId.GetFirstRun(), queryId.GetPath().Data())); | |
577 | ||
578 | TString filter; | |
6dc56e97 | 579 | MakeQueryFilter(queryId.GetFirstRun(), queryId.GetLastRun(), 0, filter); |
580 | ||
581 | TString pattern = Form("%s/Run*.root", queryId.GetPath().Data()); | |
582 | AliDebug(2,Form("pattern: %s", pattern.Data())); | |
583 | ||
0e10c8fd | 584 | TString optionQuery("-y"); |
585 | if(queryId.GetVersion() >= 0) optionQuery = ""; | |
586 | ||
587 | if (optionQuery == "-y"){ | |
588 | AliInfo("Only latest version will be returned"); | |
589 | } | |
590 | ||
591 | TGridResult *res = gGrid->Query(fDBFolder, pattern, filter, optionQuery.Data()); | |
62032124 | 592 | |
62032124 | 593 | AliCDBId validFileId; |
594 | for(int i=0; i<res->GetEntries(); i++){ | |
595 | TString filename = res->GetKey(i, "lfn"); | |
c3a7b59a | 596 | if(filename == "") continue; |
62032124 | 597 | if(FilenameToId(filename, validFileId)) |
598 | validFileIds.AddLast(validFileId.Clone()); | |
599 | } | |
600 | delete res; | |
9e1ceb13 | 601 | } |
602 | ||
62032124 | 603 | TIter *iter=0; |
604 | if(alreadyLoaded){ | |
605 | iter = new TIter(&fValidFileIds); | |
606 | } else { | |
607 | iter = new TIter(&validFileIds); | |
608 | } | |
9e1ceb13 | 609 | |
6dc56e97 | 610 | TObjArray selectedIds; |
62032124 | 611 | selectedIds.SetOwner(1); |
612 | ||
613 | // loop on list of valid Ids to select the right version to get. | |
614 | // According to query and to the selection criteria list, version can be the highest or exact | |
615 | AliCDBPath pathCopy; | |
616 | AliCDBId* anIdPtr=0; | |
617 | AliCDBId* dataId=0; | |
618 | AliCDBPath queryPath = queryId.GetAliCDBPath(); | |
619 | while((anIdPtr = dynamic_cast<AliCDBId*> (iter->Next()))){ | |
620 | AliCDBPath thisCDBPath = anIdPtr->GetAliCDBPath(); | |
621 | if(!(queryPath.Comprises(thisCDBPath)) || pathCopy.GetPath() == thisCDBPath.GetPath()) continue; | |
622 | pathCopy = thisCDBPath; | |
623 | ||
624 | // check the selection criteria list for this query | |
625 | AliCDBId thisId(*anIdPtr); | |
626 | thisId.SetVersion(queryId.GetVersion()); | |
627 | if(!thisId.HasVersion()) GetSelection(&thisId); | |
628 | ||
629 | if(alreadyLoaded){ | |
630 | dataId = GetId(fValidFileIds, thisId); | |
631 | } else { | |
632 | dataId = GetId(validFileIds, thisId); | |
9e1ceb13 | 633 | } |
4667c116 | 634 | if(dataId) selectedIds.Add(dataId); |
9e1ceb13 | 635 | } |
9e1ceb13 | 636 | |
62032124 | 637 | delete iter; iter=0; |
9e1ceb13 | 638 | |
62032124 | 639 | // selectedIds contains the Ids of the files matching all requests of query! |
640 | // All the objects are now ready to be retrieved | |
641 | iter = new TIter(&selectedIds); | |
642 | while((anIdPtr = dynamic_cast<AliCDBId*> (iter->Next()))){ | |
643 | TString filename; | |
644 | if (!IdToFilename(*anIdPtr, filename)) { | |
645 | AliDebug(2,Form("Bad data ID encountered! Subnormal error!")); | |
646 | continue; | |
647 | } | |
9e1ceb13 | 648 | |
62032124 | 649 | AliCDBEntry* anEntry = GetEntryFromFile(filename, anIdPtr); |
9e1ceb13 | 650 | |
62032124 | 651 | if(anEntry) result->Add(anEntry); |
9e1ceb13 | 652 | |
62032124 | 653 | } |
654 | delete iter; iter=0; | |
655 | ||
656 | return result; | |
9e1ceb13 | 657 | } |
658 | ||
659 | //_____________________________________________________________________________ | |
660 | Bool_t AliCDBGrid::PutEntry(AliCDBEntry* entry) { | |
661 | // put an AliCDBEntry object into the database | |
62032124 | 662 | |
9e1ceb13 | 663 | AliCDBId& id = entry->GetId(); |
664 | ||
665 | // set version for the entry to be stored | |
62032124 | 666 | if (!PrepareId(id)) return kFALSE; |
9e1ceb13 | 667 | |
668 | // build filename from entry's id | |
669 | TString filename; | |
62032124 | 670 | if (!IdToFilename(id, filename)) { |
9e1ceb13 | 671 | AliError("Bad ID encountered! Subnormal error!"); |
672 | return kFALSE; | |
62032124 | 673 | } |
674 | ||
675 | TString folderToTag = Form("%s%s", | |
676 | fDBFolder.Data(), | |
677 | id.GetPath().Data()); | |
678 | ||
9e1ceb13 | 679 | TDirectory* saveDir = gDirectory; |
680 | ||
fb33abde | 681 | TString fullFilename = Form("/alien%s", filename.Data()); |
62032124 | 682 | // specify SE to filename |
fb33abde | 683 | if (fSE != "default") fullFilename += Form("?se=%s",fSE.Data()); |
62032124 | 684 | |
9e1ceb13 | 685 | // open file |
145f45a2 | 686 | TFile *file = TFile::Open(fullFilename,"CREATE"); |
687 | if(!file || !file->IsWritable()){ | |
688 | AliError(Form("Can't open file <%s>!", filename.Data())); | |
689 | if(file && !file->IsWritable()) file->Close(); delete file; file=0; | |
690 | return kFALSE; | |
691 | } | |
62032124 | 692 | |
693 | file->cd(); | |
9e1ceb13 | 694 | |
ed343d5e | 695 | //SetTreeToFile(entry, file); |
145f45a2 | 696 | |
9e1ceb13 | 697 | entry->SetVersion(id.GetVersion()); |
698 | ||
699 | // write object (key name: "AliCDBEntry") | |
145f45a2 | 700 | Bool_t result = (file->WriteTObject(entry, "AliCDBEntry") != 0); |
62032124 | 701 | if (!result) AliError(Form("Can't write entry to file <%s>!", filename.Data())); |
9e1ceb13 | 702 | |
703 | ||
704 | if (saveDir) saveDir->cd(); else gROOT->cd(); | |
705 | file->Close(); delete file; file=0; | |
c3a7b59a | 706 | |
9e1ceb13 | 707 | if(result) { |
c75fabed | 708 | |
c3a7b59a | 709 | if(!TagFileId(filename, &id)){ |
710 | AliInfo(Form("CDB tagging failed. Deleting file %s!",filename.Data())); | |
711 | if(!gGrid->Rm(filename.Data())) | |
712 | AliError("Can't delete file!"); | |
713 | return kFALSE; | |
714 | } | |
715 | ||
716 | TagFileMetaData(filename, entry->GetMetaData()); | |
9e1ceb13 | 717 | } |
62032124 | 718 | |
c75fabed | 719 | AliInfo(Form("CDB object stored into file %s", filename.Data())); |
720 | AliInfo(Form("Storage Element: %s", fSE.Data())); | |
62032124 | 721 | return result; |
722 | } | |
723 | //_____________________________________________________________________________ | |
724 | Bool_t AliCDBGrid::AddTag(TString& folderToTag, const char* tagname){ | |
725 | // add "tagname" tag (CDB or CDB_MD) to folder where object will be stored | |
726 | ||
727 | Bool_t result = kTRUE; | |
728 | AliDebug(2, Form("adding %s tag to folder %s", tagname, folderToTag.Data())); | |
729 | TString addTag = Form("addTag %s %s", folderToTag.Data(), tagname); | |
730 | TGridResult *gridres = gGrid->Command(addTag.Data()); | |
731 | const char* resCode = gridres->GetKey(0,"__result__"); // '1' if success | |
732 | if(resCode[0] != '1') { | |
733 | AliError(Form("Couldn't add %s tags to folder %s !", | |
734 | tagname, folderToTag.Data())); | |
735 | result = kFALSE; | |
736 | } | |
737 | delete gridres; | |
9e1ceb13 | 738 | return result; |
739 | } | |
740 | ||
62032124 | 741 | //_____________________________________________________________________________ |
c3a7b59a | 742 | Bool_t AliCDBGrid::TagFileId(TString& filename, const AliCDBId* id){ |
62032124 | 743 | // tag stored object in CDB table using object Id's parameters |
744 | ||
0e10c8fd | 745 | |
746 | TString dirname(filename); | |
747 | Int_t dirNumber = gGrid->Mkdir(dirname.Remove(dirname.Last('/')),"-d"); | |
748 | ||
f430a9ce | 749 | TString addTagValue1 = Form("addTagValue %s CDB ", filename.Data()); |
750 | TString addTagValue2 = Form("first_run=%d last_run=%d version=%d ", | |
62032124 | 751 | id->GetFirstRun(), |
752 | id->GetLastRun(), | |
753 | id->GetVersion()); | |
0e10c8fd | 754 | TString addTagValue3 = Form("path_level_0=\"%s\" path_level_1=\"%s\" path_level_2=\"%s\" ", |
4667c116 | 755 | id->GetPathLevel(0).Data(), |
756 | id->GetPathLevel(1).Data(), | |
757 | id->GetPathLevel(2).Data()); | |
0e10c8fd | 758 | //TString addTagValue4 = Form("version_path=\"%s\" dir_number=%d",Form("%d_%s",id->GetVersion(),filename.Data()),dirNumber); |
759 | TString addTagValue4 = Form("version_path=\"%09d%s\" dir_number=%d",id->GetVersion(),filename.Data(),dirNumber); | |
760 | TString addTagValue = Form("%s%s%s%s", | |
f430a9ce | 761 | addTagValue1.Data(), |
762 | addTagValue2.Data(), | |
0e10c8fd | 763 | addTagValue3.Data(), |
764 | addTagValue4.Data()); | |
62032124 | 765 | |
c3a7b59a | 766 | Bool_t result = kFALSE; |
62032124 | 767 | AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data())); |
768 | TGridResult* res = gGrid->Command(addTagValue.Data()); | |
769 | const char* resCode = res->GetKey(0,"__result__"); // '1' if success | |
770 | if(resCode[0] != '1') { | |
c3a7b59a | 771 | AliError(Form("Couldn't add CDB tag value to file %s !", |
62032124 | 772 | filename.Data())); |
c3a7b59a | 773 | result = kFALSE; |
62032124 | 774 | } else { |
c75fabed | 775 | AliDebug(2, "Object successfully tagged."); |
c3a7b59a | 776 | result = kTRUE; |
62032124 | 777 | } |
778 | delete res; | |
c3a7b59a | 779 | return result; |
62032124 | 780 | |
781 | } | |
782 | ||
4667c116 | 783 | //_____________________________________________________________________________ |
784 | Bool_t AliCDBGrid::TagShortLived(TString& filename, Bool_t value){ | |
785 | // tag folder with ShortLived tag | |
786 | ||
787 | TString addTagValue = Form("addTagValue %s ShortLived_try value=%d", filename.Data(), value); | |
788 | ||
789 | Bool_t result = kFALSE; | |
790 | AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data())); | |
791 | TGridResult* res = gGrid->Command(addTagValue.Data()); | |
792 | const char* resCode = res->GetKey(0,"__result__"); // '1' if success | |
793 | if(resCode[0] != '1') { | |
794 | AliError(Form("Couldn't add ShortLived tag value to file %s !", filename.Data())); | |
795 | result = kFALSE; | |
796 | } else { | |
c75fabed | 797 | AliDebug(2,"Object successfully tagged."); |
4667c116 | 798 | result = kTRUE; |
799 | } | |
800 | delete res; | |
801 | return result; | |
802 | ||
803 | } | |
804 | ||
62032124 | 805 | //_____________________________________________________________________________ |
c3a7b59a | 806 | Bool_t AliCDBGrid::TagFileMetaData(TString& filename, const AliCDBMetaData* md){ |
62032124 | 807 | // tag stored object in CDB table using object Id's parameters |
808 | ||
f430a9ce | 809 | TString addTagValue1 = Form("addTagValue %s CDB_MD ", filename.Data()); |
810 | TString addTagValue2 = Form("object_classname=\"%s\" responsible=\"%s\" beam_period=%d ", | |
62032124 | 811 | md->GetObjectClassName(), |
812 | md->GetResponsible(), | |
813 | md->GetBeamPeriod()); | |
f430a9ce | 814 | TString addTagValue3 = Form("aliroot_version=\"%s\" comment=\"%s\"", |
62032124 | 815 | md->GetAliRootVersion(), |
816 | md->GetComment()); | |
817 | TString addTagValue = Form("%s%s%s", | |
f430a9ce | 818 | addTagValue1.Data(), |
819 | addTagValue2.Data(), | |
820 | addTagValue3.Data()); | |
62032124 | 821 | |
c3a7b59a | 822 | Bool_t result = kFALSE; |
62032124 | 823 | AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data())); |
824 | TGridResult* res = gGrid->Command(addTagValue.Data()); | |
825 | const char* resCode = res->GetKey(0,"__result__"); // '1' if success | |
826 | if(resCode[0] != '1') { | |
827 | AliWarning(Form("Couldn't add CDB_MD tag value to file %s !", | |
828 | filename.Data())); | |
c3a7b59a | 829 | result = kFALSE; |
62032124 | 830 | } else { |
c75fabed | 831 | AliDebug(2,"Object successfully tagged."); |
c3a7b59a | 832 | result = kTRUE; |
62032124 | 833 | } |
c3a7b59a | 834 | return result; |
62032124 | 835 | } |
836 | ||
b05400be | 837 | //_____________________________________________________________________________ |
838 | TList* AliCDBGrid::GetIdListFromFile(const char* fileName){ | |
6dc56e97 | 839 | |
b05400be | 840 | TString turl(fileName); |
841 | turl.Prepend("/alien" + fDBFolder); | |
62032124 | 842 | turl += "?se="; turl += fSE.Data(); |
b05400be | 843 | TFile *file = TFile::Open(turl); |
844 | if (!file) { | |
845 | AliError(Form("Can't open selection file <%s>!", turl.Data())); | |
846 | return NULL; | |
847 | } | |
848 | ||
849 | TList *list = new TList(); | |
850 | list->SetOwner(); | |
851 | int i=0; | |
852 | TString keycycle; | |
62032124 | 853 | |
b05400be | 854 | AliCDBId *id; |
855 | while(1){ | |
856 | i++; | |
857 | keycycle = "AliCDBId;"; | |
858 | keycycle+=i; | |
859 | ||
860 | id = (AliCDBId*) file->Get(keycycle); | |
861 | if(!id) break; | |
862 | list->AddFirst(id); | |
863 | } | |
864 | file->Close(); delete file; file=0; | |
865 | ||
866 | return list; | |
867 | ||
868 | ||
869 | } | |
870 | ||
871 | //_____________________________________________________________________________ | |
872 | Bool_t AliCDBGrid::Contains(const char* path) const{ | |
873 | // check for path in storage's DBFolder | |
874 | ||
875 | TString initDir(gGrid->Pwd(0)); | |
876 | TString dirName(fDBFolder); | |
877 | dirName += path; // dirName = fDBFolder/path | |
878 | Bool_t result=kFALSE; | |
879 | if (gGrid->Cd(dirName,0)) result=kTRUE; | |
880 | gGrid->Cd(initDir.Data(),0); | |
881 | return result; | |
882 | } | |
883 | ||
62032124 | 884 | //_____________________________________________________________________________ |
885 | void AliCDBGrid::QueryValidFiles() | |
886 | { | |
887 | // Query the CDB for files valid for AliCDBStorage::fRun | |
888 | // fills list fValidFileIds with AliCDBId objects created from file name | |
889 | ||
890 | TString filter; | |
6dc56e97 | 891 | MakeQueryFilter(fRun, fRun, fMetaDataFilter, filter); |
892 | ||
893 | TString pattern = Form("%s/Run*", fPathFilter.GetPath().Data()); | |
0e10c8fd | 894 | TString optionQuery = "-y"; |
895 | if(fVersion >= 0) { | |
896 | pattern += Form("_v%d*", fVersion); | |
897 | optionQuery = ""; | |
898 | } | |
6dc56e97 | 899 | pattern += ".root"; |
900 | AliDebug(2,Form("pattern: %s", pattern.Data())); | |
901 | ||
0e10c8fd | 902 | AliInfo(Form("fDBFolder = %s, pattern = %s, filter = %s",fDBFolder.Data(), pattern.Data(), filter.Data())); |
903 | ||
904 | if (optionQuery == "-y"){ | |
905 | AliInfo("Only latest version will be returned"); | |
906 | } | |
907 | ||
908 | TGridResult *res = gGrid->Query(fDBFolder, pattern, filter, optionQuery.Data()); | |
62032124 | 909 | |
b7e6419d | 910 | if (!res) { |
911 | AliError("Grid query failed"); | |
912 | return; | |
913 | } | |
914 | ||
b68a24b3 | 915 | TIter next(res); |
916 | TMap *map; | |
917 | while ((map = (TMap*)next())) { | |
918 | TObjString *entry; | |
919 | if ((entry = (TObjString *) ((TMap *)map)->GetValue("lfn"))) { | |
920 | TString& filename = entry->String(); | |
921 | if(filename.IsNull()) continue; | |
922 | AliDebug(2,Form("Found valid file: %s", filename.Data())); | |
923 | AliCDBId *validFileId = new AliCDBId; | |
924 | Bool_t result = FilenameToId(filename, *validFileId); | |
925 | if(result) { | |
926 | fValidFileIds.AddLast(validFileId); | |
927 | } | |
928 | else { | |
929 | delete validFileId; | |
930 | } | |
931 | } | |
62032124 | 932 | } |
933 | delete res; | |
934 | ||
935 | } | |
936 | ||
937 | //_____________________________________________________________________________ | |
c3a7b59a | 938 | void AliCDBGrid::MakeQueryFilter(Int_t firstRun, Int_t lastRun, |
62032124 | 939 | const AliCDBMetaData* md, TString& result) const |
940 | { | |
941 | // create filter for file query | |
942 | ||
c3a7b59a | 943 | result = Form("CDB:first_run<=%d and CDB:last_run>=%d", firstRun, lastRun); |
62032124 | 944 | |
6dc56e97 | 945 | // if(version >= 0) { |
946 | // result += Form(" and CDB:version=%d", version); | |
947 | // } | |
948 | // if(pathFilter.GetLevel0() != "*") { | |
949 | // result += Form(" and CDB:path_level_0=\"%s\"", pathFilter.GetLevel0().Data()); | |
950 | // } | |
951 | // if(pathFilter.GetLevel1() != "*") { | |
952 | // result += Form(" and CDB:path_level_1=\"%s\"", pathFilter.GetLevel1().Data()); | |
953 | // } | |
954 | // if(pathFilter.GetLevel2() != "*") { | |
955 | // result += Form(" and CDB:path_level_2=\"%s\"", pathFilter.GetLevel2().Data()); | |
956 | // } | |
62032124 | 957 | |
958 | if(md){ | |
959 | if(md->GetObjectClassName()[0] != '\0') { | |
960 | result += Form(" and CDB_MD:object_classname=\"%s\"", md->GetObjectClassName()); | |
961 | } | |
962 | if(md->GetResponsible()[0] != '\0') { | |
963 | result += Form(" and CDB_MD:responsible=\"%s\"", md->GetResponsible()); | |
964 | } | |
965 | if(md->GetBeamPeriod() != 0) { | |
966 | result += Form(" and CDB_MD:beam_period=%d", md->GetBeamPeriod()); | |
967 | } | |
968 | if(md->GetAliRootVersion()[0] != '\0') { | |
969 | result += Form(" and CDB_MD:aliroot_version=\"%s\"", md->GetAliRootVersion()); | |
970 | } | |
971 | if(md->GetComment()[0] != '\0') { | |
972 | result += Form(" and CDB_MD:comment=\"%s\"", md->GetComment()); | |
973 | } | |
974 | } | |
975 | AliDebug(2, Form("filter: %s",result.Data())); | |
976 | ||
977 | } | |
978 | ||
c3a7b59a | 979 | //_____________________________________________________________________________ |
980 | Int_t AliCDBGrid::GetLatestVersion(const char* path, Int_t run){ | |
981 | // get last version found in the database valid for run and path | |
982 | ||
c3a7b59a | 983 | AliCDBPath aCDBPath(path); |
984 | if(!aCDBPath.IsValid() || aCDBPath.IsWildcard()) { | |
985 | AliError(Form("Invalid path in request: %s", path)); | |
986 | return -1; | |
987 | } | |
988 | AliCDBId query(path, run, run, -1, -1); | |
c3a7b59a | 989 | |
0e10c8fd | 990 | AliCDBId* dataId = 0; |
c3a7b59a | 991 | // look for file matching query requests (path, runRange, version) |
6dc56e97 | 992 | if(run == fRun && fPathFilter.Comprises(aCDBPath) && fVersion < 0){ |
c3a7b59a | 993 | // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds() |
994 | AliDebug(2, Form("List of files valid for run %d and for path %s was loaded. Looking there!", | |
995 | run, path)); | |
996 | dataId = GetId(fValidFileIds, query); | |
997 | if (!dataId) return -1; | |
4667c116 | 998 | Int_t version = dataId->GetVersion(); |
999 | delete dataId; | |
1000 | return version; | |
c3a7b59a | 1001 | |
1002 | } | |
1003 | // List of files valid for reqested run was not loaded. Looking directly into CDB | |
1004 | AliDebug(2, Form("List of files valid for run %d and for path %s was not loaded. Looking directly into CDB!", | |
1005 | run, path)); | |
1006 | ||
4667c116 | 1007 | TObjArray validFileIds; |
1008 | validFileIds.SetOwner(1); | |
1009 | ||
c3a7b59a | 1010 | TString filter; |
6dc56e97 | 1011 | MakeQueryFilter(run, run, 0, filter); |
1012 | ||
1013 | TString pattern = Form("%s/Run*.root", path); | |
1014 | AliDebug(2,Form("pattern: %s", pattern.Data())); | |
c3a7b59a | 1015 | |
0e10c8fd | 1016 | TGridResult *res = gGrid->Query(fDBFolder, pattern, filter, "-y"); |
c3a7b59a | 1017 | AliCDBId validFileId; |
0e10c8fd | 1018 | if (res->GetEntries()>1){ |
1019 | AliWarning("Number of found entries >1, even if option -y was used"); | |
1020 | for(int i=0; i<res->GetEntries(); i++){ | |
1021 | TString filename = res->GetKey(i, "lfn"); | |
1022 | if(filename == "") continue; | |
1023 | if(FilenameToId(filename, validFileId)) | |
c3a7b59a | 1024 | validFileIds.AddLast(validFileId.Clone()); |
0e10c8fd | 1025 | } |
1026 | dataId = GetId(validFileIds, query); | |
1027 | if (!dataId) return -1; | |
1028 | ||
1029 | Int_t version = dataId->GetVersion(); | |
1030 | delete dataId; | |
1031 | return version; | |
1032 | } | |
1033 | else if (res->GetEntries()==1){ | |
1034 | TString filename = res->GetKey(0, "lfn"); | |
1035 | if(filename == "") { | |
1036 | AliError("The only entry found has filename empty"); | |
1037 | return -1; | |
1038 | } | |
1039 | if(FilenameToId(filename, validFileId)) return validFileId.GetVersion(); | |
1040 | else{ | |
1041 | AliError("Impossible to get FileId from filename"); | |
1042 | return -1; | |
1043 | } | |
1044 | } | |
1045 | else { | |
1046 | AliError("No entries found"); | |
1047 | return -1; | |
c3a7b59a | 1048 | } |
c3a7b59a | 1049 | |
0e10c8fd | 1050 | delete res; |
62ea513e | 1051 | |
c3a7b59a | 1052 | |
1053 | } | |
1054 | ||
1055 | //_____________________________________________________________________________ | |
1056 | Int_t AliCDBGrid::GetLatestSubVersion(const char* /*path*/, Int_t /*run*/, Int_t /*version*/){ | |
1057 | // get last subversion found in the database valid for run and path | |
1058 | AliError("Objects in GRID storage have no sub version!"); | |
1059 | return -1; | |
1060 | } | |
1061 | ||
1062 | ||
9e1ceb13 | 1063 | ///////////////////////////////////////////////////////////////////////////////////////////////// |
1064 | // // | |
1065 | // AliCDBGrid factory // | |
1066 | // // | |
1067 | ///////////////////////////////////////////////////////////////////////////////////////////////// | |
1068 | ||
1069 | ClassImp(AliCDBGridFactory) | |
1070 | ||
1071 | //_____________________________________________________________________________ | |
1072 | Bool_t AliCDBGridFactory::Validate(const char* gridString) { | |
1073 | // check if the string is valid Grid URI | |
1074 | ||
b05400be | 1075 | TRegexp gridPattern("^alien://.+$"); |
9e1ceb13 | 1076 | |
27cac7e3 | 1077 | return TString(gridString).Contains(gridPattern); |
9e1ceb13 | 1078 | } |
1079 | ||
1080 | //_____________________________________________________________________________ | |
1081 | AliCDBParam* AliCDBGridFactory::CreateParameter(const char* gridString) { | |
1082 | // create AliCDBGridParam class from the URI string | |
1083 | ||
1084 | if (!Validate(gridString)) { | |
1085 | return NULL; | |
1086 | } | |
c75fabed | 1087 | |
b05400be | 1088 | TString buffer(gridString); |
c3a7b59a | 1089 | |
1090 | TString gridUrl = "alien://"; | |
b05400be | 1091 | TString user = ""; |
4667c116 | 1092 | TString dbFolder = ""; |
fb33abde | 1093 | TString se = "default"; |
c75fabed | 1094 | TString cacheFolder = ""; |
1095 | Bool_t operateDisconnected = kTRUE; | |
1096 | Long64_t cacheSize = (UInt_t) 1024*1024*1024; // 1GB | |
1097 | Long_t cleanupInterval = 0; | |
b05400be | 1098 | |
1099 | TObjArray *arr = buffer.Tokenize('?'); | |
1100 | TIter iter(arr); | |
62032124 | 1101 | TObjString *str = 0; |
4667c116 | 1102 | |
b05400be | 1103 | while((str = (TObjString*) iter.Next())){ |
1104 | TString entry(str->String()); | |
1105 | Int_t indeq = entry.Index('='); | |
1106 | if(indeq == -1) { | |
1107 | if(entry.BeginsWith("alien://")) { // maybe it's a gridUrl! | |
1108 | gridUrl = entry; | |
1109 | continue; | |
1110 | } else { | |
1111 | AliError(Form("Invalid entry! %s",entry.Data())); | |
1112 | continue; | |
1113 | } | |
1114 | } | |
1115 | ||
1116 | TString key = entry(0,indeq); | |
1117 | TString value = entry(indeq+1,entry.Length()-indeq); | |
1118 | ||
1119 | if(key.Contains("grid",TString::kIgnoreCase)) { | |
1120 | gridUrl += value; | |
c75fabed | 1121 | } |
b05400be | 1122 | else if (key.Contains("user",TString::kIgnoreCase)){ |
1123 | user = value; | |
1124 | } | |
b05400be | 1125 | else if (key.Contains("se",TString::kIgnoreCase)){ |
1126 | se = value; | |
1127 | } | |
d7744171 | 1128 | else if (key.Contains("cacheF",TString::kIgnoreCase)){ |
c75fabed | 1129 | cacheFolder = value; |
f8703115 | 1130 | if (!cacheFolder.IsNull() && !cacheFolder.EndsWith("/")) |
c75fabed | 1131 | cacheFolder += "/"; |
1132 | } | |
d7744171 | 1133 | else if (key.Contains("folder",TString::kIgnoreCase)){ |
1134 | dbFolder = value; | |
1135 | } | |
c75fabed | 1136 | else if (key.Contains("operateDisc",TString::kIgnoreCase)){ |
1137 | if(value == "kTRUE") { | |
1138 | operateDisconnected = kTRUE; | |
1139 | } else if (value == "kFALSE") { | |
1140 | operateDisconnected = kFALSE; | |
1141 | } else if (value == "0" || value == "1") { | |
1142 | operateDisconnected = (Bool_t) value.Atoi(); | |
1143 | } else { | |
1144 | AliError(Form("Invalid entry! %s",entry.Data())); | |
1145 | return NULL; | |
1146 | } | |
1147 | } | |
d7744171 | 1148 | else if (key.Contains("cacheS",TString::kIgnoreCase)){ |
c75fabed | 1149 | if(value.IsDigit()) { |
1150 | cacheSize = value.Atoi(); | |
1151 | } else { | |
1152 | AliError(Form("Invalid entry! %s",entry.Data())); | |
1153 | return NULL; | |
1154 | } | |
1155 | } | |
1156 | else if (key.Contains("cleanupInt",TString::kIgnoreCase)){ | |
1157 | if(value.IsDigit()) { | |
1158 | cleanupInterval = value.Atoi(); | |
1159 | } else { | |
1160 | AliError(Form("Invalid entry! %s",entry.Data())); | |
1161 | return NULL; | |
1162 | } | |
1163 | } | |
b05400be | 1164 | else{ |
1165 | AliError(Form("Invalid entry! %s",entry.Data())); | |
c75fabed | 1166 | return NULL; |
b05400be | 1167 | } |
1168 | } | |
1169 | delete arr; arr=0; | |
c75fabed | 1170 | |
1171 | AliDebug(2, Form("gridUrl: %s", gridUrl.Data())); | |
1172 | AliDebug(2, Form("user: %s", user.Data())); | |
1173 | AliDebug(2, Form("dbFolder: %s", dbFolder.Data())); | |
1174 | AliDebug(2, Form("s.e.: %s", se.Data())); | |
1175 | AliDebug(2, Form("local cache folder: %s", cacheFolder.Data())); | |
1176 | AliDebug(2, Form("local cache operate disconnected: %d", operateDisconnected)); | |
d2a0a483 | 1177 | AliDebug(2, Form("local cache size: %lld", cacheSize)); |
6720fa1f | 1178 | AliDebug(2, Form("local cache cleanup interval: %ld", cleanupInterval)); |
9e1ceb13 | 1179 | |
27cac7e3 | 1180 | if(dbFolder == ""){ |
c75fabed | 1181 | AliError("Base folder must be specified!"); |
4667c116 | 1182 | return NULL; |
1183 | } | |
1184 | ||
c75fabed | 1185 | return new AliCDBGridParam(gridUrl.Data(), user.Data(), |
1186 | dbFolder.Data(), se.Data(), cacheFolder.Data(), | |
1187 | operateDisconnected, cacheSize, cleanupInterval); | |
9e1ceb13 | 1188 | } |
1189 | ||
1190 | //_____________________________________________________________________________ | |
1191 | AliCDBStorage* AliCDBGridFactory::Create(const AliCDBParam* param) { | |
1192 | // create AliCDBGrid storage instance from parameters | |
1193 | ||
62032124 | 1194 | AliCDBGrid *grid = 0; |
9e1ceb13 | 1195 | if (AliCDBGridParam::Class() == param->IsA()) { |
62032124 | 1196 | |
9e1ceb13 | 1197 | const AliCDBGridParam* gridParam = (const AliCDBGridParam*) param; |
62032124 | 1198 | grid = new AliCDBGrid(gridParam->GridUrl().Data(), |
1199 | gridParam->GetUser().Data(), | |
1200 | gridParam->GetDBFolder().Data(), | |
c75fabed | 1201 | gridParam->GetSE().Data(), |
1202 | gridParam->GetCacheFolder().Data(), | |
1203 | gridParam->GetOperateDisconnected(), | |
1204 | gridParam->GetCacheSize(), | |
1205 | gridParam->GetCleanupInterval()); | |
9e1ceb13 | 1206 | |
9e1ceb13 | 1207 | } |
1208 | ||
c3a7b59a | 1209 | if(!gGrid && grid) { |
1210 | delete grid; grid=0; | |
1211 | } | |
1212 | ||
62032124 | 1213 | return grid; |
9e1ceb13 | 1214 | } |
1215 | ||
1216 | ///////////////////////////////////////////////////////////////////////////////////////////////// | |
1217 | // // | |
c75fabed | 1218 | // AliCDBGrid Parameter class // // |
9e1ceb13 | 1219 | // // |
1220 | ///////////////////////////////////////////////////////////////////////////////////////////////// | |
1221 | ||
1222 | ClassImp(AliCDBGridParam) | |
1223 | ||
1224 | //_____________________________________________________________________________ | |
62032124 | 1225 | AliCDBGridParam::AliCDBGridParam(): |
1226 | AliCDBParam(), | |
1227 | fGridUrl(), | |
1228 | fUser(), | |
1229 | fDBFolder(), | |
c75fabed | 1230 | fSE(), |
1231 | fCacheFolder(), | |
1232 | fOperateDisconnected(), | |
1233 | fCacheSize(), | |
1234 | fCleanupInterval() | |
1235 | ||
62032124 | 1236 | { |
9e1ceb13 | 1237 | // default constructor |
1238 | ||
1239 | } | |
1240 | ||
1241 | //_____________________________________________________________________________ | |
c75fabed | 1242 | AliCDBGridParam::AliCDBGridParam(const char* gridUrl, const char* user, const char* dbFolder, |
1243 | const char* se, const char* cacheFolder, Bool_t operateDisconnected, | |
1244 | Long64_t cacheSize, Long_t cleanupInterval): | |
62032124 | 1245 | AliCDBParam(), |
b05400be | 1246 | fGridUrl(gridUrl), |
9e1ceb13 | 1247 | fUser(user), |
b05400be | 1248 | fDBFolder(dbFolder), |
c75fabed | 1249 | fSE(se), |
1250 | fCacheFolder(cacheFolder), | |
1251 | fOperateDisconnected(operateDisconnected), | |
1252 | fCacheSize(cacheSize), | |
1253 | fCleanupInterval(cleanupInterval) | |
9e1ceb13 | 1254 | { |
1255 | // constructor | |
c75fabed | 1256 | |
9e1ceb13 | 1257 | SetType("alien"); |
1258 | ||
c75fabed | 1259 | TString uri = Form("%s?User=%s?DBFolder=%s?SE=%s?CacheFolder=%s" |
d2a0a483 | 1260 | "?OperateDisconnected=%d?CacheSize=%lld?CleanupInterval=%ld", |
62032124 | 1261 | fGridUrl.Data(), fUser.Data(), |
c75fabed | 1262 | fDBFolder.Data(), fSE.Data(), fCacheFolder.Data(), |
1263 | fOperateDisconnected, fCacheSize, fCleanupInterval); | |
62032124 | 1264 | |
1265 | SetURI(uri.Data()); | |
9e1ceb13 | 1266 | } |
1267 | ||
1268 | //_____________________________________________________________________________ | |
1269 | AliCDBGridParam::~AliCDBGridParam() { | |
1270 | // destructor | |
1271 | ||
1272 | } | |
1273 | ||
1274 | //_____________________________________________________________________________ | |
1275 | AliCDBParam* AliCDBGridParam::CloneParam() const { | |
1276 | // clone parameter | |
1277 | ||
62032124 | 1278 | return new AliCDBGridParam(fGridUrl.Data(), fUser.Data(), |
c75fabed | 1279 | fDBFolder.Data(), fSE.Data(), fCacheFolder.Data(), |
1280 | fOperateDisconnected, fCacheSize, fCleanupInterval); | |
9e1ceb13 | 1281 | } |
1282 | ||
1283 | //_____________________________________________________________________________ | |
1284 | ULong_t AliCDBGridParam::Hash() const { | |
1285 | // return Hash function | |
1286 | ||
c75fabed | 1287 | return fGridUrl.Hash()+fUser.Hash()+fDBFolder.Hash()+fSE.Hash()+fCacheFolder.Hash(); |
9e1ceb13 | 1288 | } |
1289 | ||
1290 | //_____________________________________________________________________________ | |
1291 | Bool_t AliCDBGridParam::IsEqual(const TObject* obj) const { | |
1292 | // check if this object is equal to AliCDBParam obj | |
1293 | ||
1294 | if (this == obj) { | |
1295 | return kTRUE; | |
1296 | } | |
1297 | ||
1298 | if (AliCDBGridParam::Class() != obj->IsA()) { | |
1299 | return kFALSE; | |
1300 | } | |
1301 | ||
1302 | AliCDBGridParam* other = (AliCDBGridParam*) obj; | |
1303 | ||
b05400be | 1304 | if(fGridUrl != other->fGridUrl) return kFALSE; |
9e1ceb13 | 1305 | if(fUser != other->fUser) return kFALSE; |
b05400be | 1306 | if(fDBFolder != other->fDBFolder) return kFALSE; |
9e1ceb13 | 1307 | if(fSE != other->fSE) return kFALSE; |
c75fabed | 1308 | if(fCacheFolder != other->fCacheFolder) return kFALSE; |
1309 | if(fOperateDisconnected != other->fOperateDisconnected) return kFALSE; | |
1310 | if(fCacheSize != other->fCacheSize) return kFALSE; | |
1311 | if(fCleanupInterval != other->fCleanupInterval) return kFALSE; | |
9e1ceb13 | 1312 | return kTRUE; |
1313 | } | |
1314 |