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