30 namespace Experimental {
36 class TFileStorageInterface {
38 virtual void Flush() = 0;
39 virtual void Close() = 0;
40 virtual ~TFileStorageInterface() =
default;
48 TFileSharedPtrCtor(std::unique_ptr<TFileStorageInterface> &&storage):
TFile(std::move(storage)) {}
60 static void AddFilesToClose(std::weak_ptr<ROOT::Experimental::TFile> pFile)
63 std::vector<std::weak_ptr<ROOT::Experimental::TFile>> fFiles;
67 for (
auto &wFile: fFiles) {
68 if (
auto sFile = wFile.lock()) {
74 static CloseFiles_t closer;
76 std::lock_guard<std::mutex> lock(closer.fMutex);
77 closer.fFiles.emplace_back(pFile);
83 class TV6Storage:
public ROOT::Experimental::Internal::TFileStorageInterface {
87 TV6Storage(
const std::string &
name,
const std::string &mode): fOldFile(::
TFile::Open(name.c_str(), mode.c_str())) {}
89 void Flush()
final { fOldFile->
Flush(); }
91 void Close()
final { fOldFile->
Close(); }
93 ~TV6Storage() {
delete fOldFile; }
97 fOldFile->WriteObjectAny(address, cl, std::string(name).c_str());
104 AddFilesToClose(
fFile);
110 std::string ret(mode);
112 ret +=
" CACHEREAD ";
114 ret +=
" TIMEOUT=" + std::to_string(opts.
fAsyncTimeout) +
" ";
118 static std::mutex &GetCacheDirMutex()
120 static std::mutex sMutex;
124 static std::unique_ptr<ROOT::Experimental::Internal::TFileStorageInterface>
129 struct SetCacheDirRAII_t {
130 std::string fOldCacheDir;
131 std::lock_guard<std::mutex> fLock;
133 SetCacheDirRAII_t(
bool need): fLock(GetCacheDirMutex())
141 if (!fOldCacheDir.empty())
146 auto v6storage = std::make_unique<TV6Storage>(std::string(name), GetV6TFileOpts(mode, opts));
149 return std::unique_ptr<TFileStorageInterface>{std::move(v6storage)};
157 using namespace Internal;
158 auto file = std::make_shared<TFileSharedPtrCtor>(OpenV6TFile(name,
"READ", opts));
166 using namespace Internal;
167 auto file = std::make_shared<TFileSharedPtrCtor>(OpenV6TFile(name,
"CREATE", opts));
175 using namespace Internal;
176 auto file = std::make_shared<TFileSharedPtrCtor>(OpenV6TFile(name,
"RECREATE", opts));
184 using namespace Internal;
185 auto file = std::make_shared<TFileSharedPtrCtor>(OpenV6TFile(name,
"UPDATE", opts));
191 std::lock_guard<std::mutex> lock(GetCacheDirMutex());
200 std::lock_guard<std::mutex> lock(GetCacheDirMutex());
201 return ::TFile::GetCacheFileDir();
207 : fStorage(
std::move(storage))
224 fStorage->WriteMemoryWithType(name, address, cl);
int fAsyncTimeout
Timeout for asynchronous opening.
std::unique_ptr< Internal::TFileStorageInterface > fStorage
Storage backend.
Namespace for new ROOT classes and functions.
~TFile()
Must not call Write() of all attached objects: some might not be needed to be written or writing migh...
Options for TFile construction.
static TFilePtr OpenForUpdate(std::string_view name, const Options_t &opts=Options_t())
Open an existing file with name for reading and writing.
static Bool_t SetCacheFileDir(ROOT::Internal::TStringView cacheDir, Bool_t operateDisconnected=kTRUE, Bool_t forceCacheread=kFALSE)
static TFilePtr Create(std::string_view name, const Options_t &opts=Options_t())
Open a file with name for reading and writing.
static std::string GetCacheDir()
Get the directory used for cached reads.
void Flush()
Save all objects associated with this directory (including file header) to the storage medium...
static TDirectory & Heap()
Dedicated, process-wide TDirectory.
bool fCachedRead
Whether the file should be cached before reading.
static TFilePtr Open(std::string_view name, const Options_t &opts=Options_t())
Open a file with name for reading.
The ROOT global object gROOT contains a list of all defined classes.
static TFilePtr Recreate(std::string_view name, const Options_t &opts=Options_t())
Open a file with name for reading and writing.
std::shared_ptr< TFile > fFile
void WriteMemoryWithType(std::string_view name, const void *address, TClass *cl)
Serialize the object at address, using the object's TClass.
Points to an object that stores or reads objects in ROOT's binary format.
basic_string_view< char > string_view
Key/value store of objects.
static std::string SetCacheDir(std::string_view path)
Set the new directory used for cached reads, returns the old directory.
void Close()
Flush() and make the file non-writable: close it.
TFile(std::unique_ptr< Internal::TFileStorageInterface > &&storage)
TFilePtr(std::shared_ptr< TFile > &&)
Constructed by Open etc.
static const char * GetCacheFileDir()
Get the directory where to locally stage/cache remote files.
bool fAsynchronousOpen
Whether the file should be opened asynchronously, if available.