28 namespace Experimental {
34 class TFileStorageInterface {
36 virtual void Flush() = 0;
37 virtual void Close() = 0;
38 virtual ~TFileStorageInterface() =
default;
39 virtual void WriteMemoryWithType(std::string_view
name,
const void *address,
47 TFileSharedPtrCtor(std::unique_ptr<TFileStorageInterface>&& storage):
48 TFile(std::move(storage)) {}
61 static void AddFilesToClose(std::weak_ptr<ROOT::Experimental::TFile> pFile) {
63 std::vector<std::weak_ptr<ROOT::Experimental::TFile>> fFiles;
66 for (
auto& wFile: fFiles) {
67 if (
auto sFile = wFile.lock()) {
73 static CloseFiles_t closer;
75 std::lock_guard<std::mutex> lock(closer.fMutex);
76 closer.fFiles.emplace_back(pFile);
83 class TV6Storage:
public ROOT::Experimental::Internal::TFileStorageInterface {
87 TV6Storage(
const std::string&
name,
const std::string& mode):
88 fOldFile(::
TFile::Open(name.c_str(), mode.c_str())) {
91 void Flush()
final { fOldFile->
Flush(); }
93 void Close()
final { fOldFile->
Close(); }
99 void WriteMemoryWithType(std::string_view name,
const void* address,
TClass* cl)
final {
100 fOldFile->WriteObjectAny(address, cl, std::string(name).c_str());
108 AddFilesToClose(
fFile);
112 static std::string GetV6TFileOpts(
const char* mode,
114 std::string ret(mode);
116 ret +=
" CACHEREAD ";
118 ret +=
" TIMEOUT=" + std::to_string(opts.
fAsyncTimeout) +
" ";
122 static std::mutex& GetCacheDirMutex() {
123 static std::mutex sMutex;
127 static std::unique_ptr<ROOT::Experimental::Internal::TFileStorageInterface>
128 OpenV6TFile(std::string_view
name,
const char* mode,
132 struct SetCacheDirRAII_t {
133 std::string fOldCacheDir;
134 std::lock_guard<std::mutex> fLock;
136 SetCacheDirRAII_t(
bool need): fLock(GetCacheDirMutex()) {
141 ~SetCacheDirRAII_t() {
142 if (!fOldCacheDir.empty())
147 auto v6storage = std::make_unique<TV6Storage>(std::string(name), GetV6TFileOpts(mode, opts));
150 return std::unique_ptr<TFileStorageInterface>{std::move(v6storage)};
158 using namespace Internal;
159 auto file = std::make_shared<TFileSharedPtrCtor>(OpenV6TFile(name,
"READ", opts));
167 using namespace Internal;
168 auto file = std::make_shared<TFileSharedPtrCtor>(OpenV6TFile(name,
"CREATE", opts));
176 using namespace Internal;
177 auto file = std::make_shared<TFileSharedPtrCtor>(OpenV6TFile(name,
"RECREATE", opts));
185 using namespace Internal;
186 auto file = std::make_shared<TFileSharedPtrCtor>(OpenV6TFile(name,
"UPDATE", opts));
191 std::lock_guard<std::mutex> lock(GetCacheDirMutex());
199 std::lock_guard<std::mutex> lock(GetCacheDirMutex());
200 return ::TFile::GetCacheFileDir();
206 fStorage(
std::move(storage)) {}
217 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 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.
static Bool_t SetCacheFileDir(const char *cacheDir, Bool_t operateDisconnected=kTRUE, Bool_t forceCacheread=kFALSE)
Sets the directory where to locally stage/cache remote files.
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.