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