00001 #ifndef Impala_Persistency_VideoSetsRepository_h
00002 #define Impala_Persistency_VideoSetsRepository_h
00003
00004 #include "Basis/Quids.h"
00005 #include "Basis/QuidObj.h"
00006 #include "Persistency/Locator.h"
00007 #include "Util/SimpleMap.h"
00008 #include "Util/StringParser.h"
00009 #include "Persistency/RepositoryInMonetDB.h"
00010 #include "Persistency/RepositoryInFileSystem.h"
00011
00012 namespace Impala
00013 {
00014
00015
00016 namespace Core
00017 {
00018 namespace VideoSet
00019 {
00020
00021 class VideoSet;
00022
00023 }
00024 }
00025
00026
00027 namespace Persistency
00028 {
00029
00030
00031 class VideoSetsRepository
00032 {
00033 public:
00034
00035 typedef Core::VideoSet::VideoSet VideoSet;
00036
00037 static VideoSetsRepository&
00038 GetInstance()
00039 {
00040 static VideoSetsRepository theVideoSetsRepository;
00041 return theVideoSetsRepository;
00042 }
00043
00044 bool
00045 Contains(CString setName) const
00046 {
00047 int id = 0;
00048 return mIdMap.GetIdx(FileNameBase(setName), id);
00049 }
00050
00051 int
00052 GetId(CString setName) const
00053 {
00054 int id = 0;
00055 mIdMap.GetIdx(FileNameBase(setName), id);
00056 return id;
00057 }
00058
00059 std::vector<int>
00060 GetAllId() const
00061 {
00062 return mIdMap.GetAllIdx();
00063 }
00064
00065 String
00066 GetSetName(int id) const
00067 {
00068 String res = "unknown";
00069 mIdMap.Get(id, res);
00070 return res;
00071 }
00072
00073 void
00074 Register(VideoSet* vs, String setName)
00075 {
00076 int id = GetId(setName);
00077 if (id == 0)
00078 {
00079 ILOG_ERROR("Register: Cannot find id for " << setName);
00080 return;
00081 }
00082 mSetMap.Add(id, vs);
00083 }
00084
00085 VideoSet*
00086 GetVideoSet(int id) const
00087 {
00088 VideoSet* res = 0;
00089 if (! mSetMap.Get(id, res))
00090 ILOG_INFO("Couldn't find VideoSet with id = " << id);
00091 return res;
00092 }
00093
00094 VideoSet*
00095 GetVideoSet(Quid quid) const
00096 {
00097 return GetVideoSet(QuidSet(quid));
00098 }
00099
00100 String
00101 QuidToString(Quid quid, bool elaborate)
00102 {
00103 QuidObj qObj(quid);
00104 if (!elaborate)
00105 return qObj.ToString();
00106 String setName = "unknown";
00107 mIdMap.Get(qObj.Set(), setName);
00108 return "Q(" + QuidClassToString(qObj.Class()) + "," + setName + "," +
00109 MakeString(qObj.Object()) + "," + MakeString(qObj.Id()) + ")";
00110 }
00111
00112 void
00113 CopyTo(const Locator& dstLoc) const
00114 {
00115 String protocol = dstLoc.GetProtocol();
00116 if (protocol == "mapi")
00117 {
00118 CopyToMonetDB(dstLoc);
00119 }
00120 else
00121 {
00122 ILOG_ERROR("Copy: unknown protocol: " << protocol);
00123 }
00124 }
00125
00126 void
00127 Dump() const
00128 {
00129 mIdMap.Dump("VideoSets: ");
00130 }
00131
00132 private:
00133
00134 typedef Link::Monet::Connection Connection;
00135
00136 VideoSetsRepository()
00137 {
00138 CmdOptions& options = CmdOptions::GetInstance();
00139 Locator loc("config", options);
00140 ILOG_DEBUG("ctor: loc = " << loc);
00141 String protocol = loc.GetProtocol();
00142 if ((protocol == "file") || (protocol == "dataServer"))
00143 {
00144 GetFromFileSystem(loc);
00145 }
00146 else if (protocol == "mapi")
00147 {
00148 GetFromMonetDB(loc);
00149 }
00150 else
00151 {
00152 ILOG_ERROR("Get: unknown protocol: " << protocol);
00153 }
00154 }
00155
00156 void
00157 GetFromFileSystem(const Locator& loc)
00158 {
00159 ILOG_DEBUG("GetFromFileSystem");
00160 CmdOptions& options = CmdOptions::GetInstance();
00161 String repName = options.GetString("videoSetsRepository");
00162 File f = RepositoryInFileSystem::GetInstance().GetFile(loc, repName,
00163 false, false);
00164 Util::IOBuffer* buf = f.GetReadBuffer();
00165 if (buf)
00166 {
00167 while (buf->Available())
00168 {
00169 String line = buf->ReadLine();
00170 if (line[0] && (line[0] != '#'))
00171 {
00172 Util::StringParser p(line);
00173 int setId = p.GetInt();
00174 String setName = p.GetString2(false);
00175 mIdMap.Add(setId, setName);
00176 }
00177 }
00178 delete buf;
00179 }
00180 }
00181
00182 void
00183 GetFromMonetDB(const Locator& loc)
00184 {
00185 ILOG_DEBUG("GetFromMonetDB");
00186 Connection* conn = RepositoryInMonetDB::GetInstance().GetConnection(loc);
00187
00188 String query =
00189 "select identifier, set_name \
00190 from video_sets \
00191 order by identifier;";
00192
00193 MapiHdl hdl = conn->QueryPartStart(query);
00194 if (hdl == 0)
00195 return;
00196
00197 int* id = 0;
00198 int idSize = 0;
00199 if (!conn->QueryPartFetchInt(hdl, 0, id, idSize))
00200 return;
00201
00202 std::vector<String> sName;
00203 if (!conn->QueryPartFetchString(hdl, 1, sName))
00204 return;
00205
00206 conn->QueryPartEnd(hdl);
00207
00208 for (int i=0 ; i<idSize ; i++)
00209 {
00210 mIdMap.Add(id[i], sName[i]);
00211 }
00212 delete id;
00213 }
00214
00215 void
00216 CopyToMonetDB(const Locator& loc) const
00217 {
00218 Connection* conn = RepositoryInMonetDB::GetInstance().GetConnection(loc);
00219
00220 std::vector<int> ids = GetAllId();
00221 for (int i=0 ; i<ids.size() ; i++)
00222 {
00223 String name = GetSetName(ids[i]);
00224 String q = "select i_add_video_set(" + MakeString(ids[i])
00225 + ", '" + name + "');";
00226 ILOG_INFO("q = [" + q + "]");
00227 conn->Query(q, false, false);
00228 }
00229 }
00230
00231 String mFileName;
00232 Util::SimpleMap<int, String> mIdMap;
00233 Util::SimpleMap<int, VideoSet*> mSetMap;
00234
00235 ILOG_VAR_DEC;
00236 };
00237
00238 ILOG_VAR_INIT(VideoSetsRepository, Impala.Persistency);
00239
00240 }
00241 }
00242
00243 #endif