00001 #ifndef Impala_Persistency_ImageSetsRepository_h
00002 #define Impala_Persistency_ImageSetsRepository_h
00003
00004 #include "Basis/Quids.h"
00005 #include "Basis/QuidObj.h"
00006 #include "Util/SimpleMap.h"
00007 #include "Util/StringParser.h"
00008 #include "Persistency/RepositoryInFileSystem.h"
00009
00010 namespace Impala
00011 {
00012
00013
00014 namespace Core
00015 {
00016 namespace ImageSet
00017 {
00018
00019 class ImageSet;
00020
00021 }
00022 }
00023
00024
00025 namespace Persistency
00026 {
00027
00028
00029 class ImageSetsRepository
00030 {
00031 public:
00032
00033 typedef Core::ImageSet::ImageSet ImageSet;
00034
00035 static ImageSetsRepository&
00036 GetInstance()
00037 {
00038 static ImageSetsRepository theImageSetsRepository;
00039 return theImageSetsRepository;
00040 }
00041
00042 bool
00043 Contains(CString setName)
00044 {
00045 int id = 0;
00046 return mIdMap.GetIdx(GetMapName(setName), id);
00047 }
00048
00049 int
00050 GetId(String setName)
00051 {
00052 int id = 0;
00053 mIdMap.GetIdx(GetMapName(setName), id);
00054 return id;
00055 }
00056
00057 String
00058 GetSetName(int id)
00059 {
00060 String res = "unknown";
00061 mIdMap.Get(id, res);
00062 return res;
00063 }
00064
00065 void
00066 Register(ImageSet* vs, String setName)
00067 {
00068 if (setName.empty())
00069 {
00070 ILOG_ERROR("Cannot find setName because it is empty");
00071 return;
00072 }
00073 int id = GetId(setName);
00074 if (id == 0)
00075 {
00076 ILOG_ERROR("Cannot find " << setName);
00077 return;
00078 }
00079 mSetMap.Add(id, vs);
00080 }
00081
00082 ImageSet*
00083 GetImageSet(int id)
00084 {
00085 ImageSet* res = 0;
00086 if (! mSetMap.Get(id, res))
00087 ILOG_INFO("Couldn't find ImageSet with id = " << id);
00088 return res;
00089 }
00090
00091 ImageSet*
00092 GetImageSet(Quid quid)
00093 {
00094 return GetImageSet(QuidSet(quid));
00095 }
00096
00097 String
00098 QuidToString(Quid quid, bool elaborate)
00099 {
00100 QuidObj qObj(quid);
00101 if (!elaborate)
00102 return qObj.ToString();
00103 String setName = "unknown";
00104 mIdMap.Get(qObj.Set(), setName);
00105 return "Q(" + QuidClassToString(qObj.Class()) + "," + setName + "," +
00106 MakeString(qObj.Object()) + "," + MakeString(qObj.Id()) + ")";
00107 }
00108
00109 void
00110 Dump() const
00111 {
00112 mIdMap.Dump("ImageSets: ");
00113 }
00114
00115 private:
00116
00117 ImageSetsRepository()
00118 {
00119 CmdOptions& options = CmdOptions::GetInstance();
00120 Locator loc("config", options);
00121 String protocol = loc.GetProtocol();
00122 if ((protocol == "file") || (protocol == "dataServer"))
00123 {
00124 GetFromFileSystem(loc);
00125 }
00126 else
00127 {
00128 ILOG_ERROR("Get: unknown protocol: " << loc.GetProtocol());
00129 }
00130 }
00131
00132 void
00133 GetFromFileSystem(const Locator& loc)
00134 {
00135 CmdOptions& options = CmdOptions::GetInstance();
00136 String repName = options.GetString("imageSetsRepository");
00137 File f = RepositoryInFileSystem::GetInstance().GetFile(loc, repName,
00138 false, false);
00139 Util::IOBuffer* buf = f.GetReadBuffer();
00140 if (buf)
00141 {
00142 while (buf->Available())
00143 {
00144 String line = buf->ReadLine();
00145 if (line[0] && (line[0] != '#'))
00146 {
00147 Util::StringParser p(line);
00148 int setId = p.GetInt();
00149 if ((setId <= 0) || (setId > 255))
00150 ILOG_ERROR("setId out of range [0-255]");
00151 String setName = p.GetString2(false);
00152 mIdMap.Add(setId, setName);
00153 }
00154 }
00155 delete buf;
00156 }
00157 }
00158
00159 String
00160 GetMapName(CString setName)
00161 {
00162 String base = FileNameBase(setName);
00163 if (StringEndsWith(base, "keyframes"))
00164 return "keyframes";
00165 if (StringEndsWith(base, "thumbnails"))
00166 return "thumbnails";
00167 if (StringEndsWith(base, "stills"))
00168 return "stills";
00169 return base;
00170 }
00171
00172 String mFileName;
00173 Util::SimpleMap<int, String> mIdMap;
00174 Util::SimpleMap<int, ImageSet*> mSetMap;
00175
00176 ILOG_VAR_DEC;
00177 };
00178
00179 ILOG_VAR_INIT(ImageSetsRepository, Impala.Persistency);
00180
00181 }
00182 }
00183
00184 #endif