00001 #ifndef Impala_Persistency_RepositoryInFileSystem_h
00002 #define Impala_Persistency_RepositoryInFileSystem_h
00003
00004 #include "Persistency/File.h"
00005 #include "Persistency/FileLocator.h"
00006 #include "Util/SimpleMap.h"
00007
00008 namespace Impala
00009 {
00010 namespace Persistency
00011 {
00012
00013
00016 class RepositoryInFileSystem
00017 {
00018 public:
00019
00020 static RepositoryInFileSystem&
00021 GetInstance()
00022 {
00023 static RepositoryInFileSystem theRepositoryInFileSystem;
00024 return theRepositoryInFileSystem;
00025 }
00026
00027
00028 File
00029 GetFile(const Locator& loc, CString fileName, bool toWrite, bool silent)
00030 {
00031 FileSystem* fs = GetFS(loc);
00032 String path = fs->GetFilePath(loc.GetDataSet(), fileName, toWrite,
00033 silent);
00034 return File(path, fs, toWrite);
00035 }
00036
00037 File
00038 GetFile(const Locator& loc, CString dir, CString file, bool toWrite,
00039 bool silent)
00040 {
00041 FileSystem* fs = GetFS(loc);
00042 if (toWrite && !silent)
00043 fs->MakeDir(dir);
00044 String path = fs->GetFilePath(dir, file, toWrite, silent);
00045 return File(path, fs, toWrite);
00046 }
00047
00048 File
00049 GetFile(const FileLocator& loc, bool toWrite, bool silent)
00050 {
00051 FileSystem* fs = GetFS(loc);
00052 if (toWrite && !silent)
00053 fs->MakeDir(loc.GetDir());
00054 String path = fs->GetFilePath("", loc.GetName(), toWrite, silent);
00055 return File(path, fs, toWrite);
00056 }
00057
00058 void
00059 CopyFile(FileLocator srcLoc, FileLocator dstLoc)
00060 {
00061 File dstFile = GetFile(dstLoc, false, true);
00062 if (dstFile.Valid())
00063 return;
00064
00065 dstFile = GetFile(dstLoc, true, false);
00066 File srcFile = GetFile(srcLoc, false, false);
00067 dstFile.CopyFrom(srcFile);
00068 }
00069
00070 void
00071 DeleteDir(const Locator& loc, CString dir)
00072 {
00073 FileSystem* fs = GetFS(loc);
00074 fs->DeleteDir(dir);
00075 }
00076
00077 private:
00078
00079 RepositoryInFileSystem()
00080 {
00081 }
00082
00083 FileSystem*
00084 GetFS(const Locator& loc)
00085 {
00086 FileSystem* fs = 0;
00087 if (mMap.Get(loc.ToString(), fs))
00088 return fs;
00089
00090 CmdOptions& options = CmdOptions::GetInstance();
00091 String setName = loc.GetDataSet();
00092 String data("");
00093 if ((setName == "dot") || (setName == ".") || (setName == "abs") ||
00094 (setName == "direct"))
00095 {
00096 data = "";
00097 ILOG_INFO_HEADNODE("'data' for " << setName << " is <empty>");
00098 }
00099 else if (setName == "config")
00100 {
00101 data = options.GetString("config");
00102 ILOG_INFO_HEADNODE("'data' for " << setName << " is " << data);
00103 }
00104 else
00105 {
00106 data = StringReplaceAll(options.GetString("data"), ":", ";", false);
00107 String additionalData = options.GetString("data::"+setName, "");
00108 if (additionalData.empty() && !setName.empty())
00109 additionalData = "../" + FileNameBase(setName);
00110 if (!additionalData.empty())
00111 {
00112 data = additionalData + ";" + data;
00113 ILOG_INFO_HEADNODE("'data' for " << setName << " is " << data);
00114 }
00115 }
00116
00117 fs = new FileSystem(data, loc.GetHost());
00118 mMap.Add(loc.ToString(), fs);
00119 return fs;
00120 }
00121
00122 Util::SimpleMapDelete<String, FileSystem*> mMap;
00123
00124 ILOG_VAR_DEC;
00125
00126 };
00127
00128 ILOG_VAR_INIT(RepositoryInFileSystem, Impala.Persistency);
00129
00130
00131 static File
00132 RepositoryGetFile(const FileLocator& loc, bool toWrite, bool silent)
00133 {
00134 return RepositoryInFileSystem::GetInstance().GetFile(loc, toWrite, silent);
00135 }
00136
00137 static File
00138 RepositoryGetFile(CString fileName, bool toWrite, bool silent)
00139 {
00140 FileLocator loc(fileName);
00141 return RepositoryInFileSystem::GetInstance().GetFile(loc, toWrite, silent);
00142 }
00143
00144
00145
00146 }
00147 }
00148
00149 #endif