Home || Visual Search || Applications || Architecture || Important Messages || OGL || Src

VideoSetsRepository.h

Go to the documentation of this file.
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 // For forward declaration of VideoSet
00016 namespace Core
00017 {
00018 namespace VideoSet
00019 {
00020 
00021 class VideoSet;
00022 
00023 } // namespace VideoSet
00024 } // namespace Core
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         if (setName.empty())
00077         {
00078             ILOG_ERROR("Cannot find 'setName' because it is empty");
00079             return;
00080         }
00081         int id = GetId(setName);
00082         if (id == 0)
00083         {
00084             ILOG_ERROR("Register: Cannot find id for set named: " << setName);
00085             return;
00086         }
00087         mSetMap.Add(id, vs);
00088     }
00089 
00090     VideoSet*
00091     GetVideoSet(int id) const
00092     {
00093         VideoSet* res = 0;
00094         if (! mSetMap.Get(id, res))
00095             ILOG_INFO("Couldn't find VideoSet with id = " << id);
00096         return res;
00097     }
00098 
00099     VideoSet*
00100     GetVideoSet(Quid quid) const
00101     {
00102         return GetVideoSet(QuidSet(quid));
00103     }
00104 
00105     String
00106     QuidToString(Quid quid, bool elaborate)
00107     {
00108         QuidObj qObj(quid);
00109         if (!elaborate)
00110             return qObj.ToString();
00111         String setName = "unknown";
00112         mIdMap.Get(qObj.Set(), setName);
00113         return "Q(" + QuidClassToString(qObj.Class()) + "," + setName + "," +
00114             MakeString(qObj.Object()) + "," + MakeString(qObj.Id()) + ")";
00115     }
00116 
00117     void
00118     CopyTo(const Locator& dstLoc) const
00119     {
00120         String protocol = dstLoc.GetProtocol();
00121         if (protocol == "mapi")
00122         {
00123             CopyToMonetDB(dstLoc);
00124         }
00125         else
00126         {
00127             ILOG_ERROR("Copy: unknown protocol: " << protocol);
00128         }
00129     }
00130 
00131     void
00132     Dump() const
00133     {
00134         mIdMap.Dump("VideoSets: ");
00135     }
00136 
00137 private:
00138 
00139     typedef Link::Monet::Connection Connection;
00140 
00141     VideoSetsRepository()
00142     {
00143         CmdOptions& options = CmdOptions::GetInstance();
00144         Locator loc("config", options);
00145         ILOG_DEBUG("ctor: loc = " << loc);
00146         String protocol = loc.GetProtocol();
00147         if ((protocol == "file") || (protocol == "dataServer"))
00148         {
00149             GetFromFileSystem(loc);
00150         }
00151         else if (protocol == "mapi")
00152         {
00153             GetFromMonetDB(loc);
00154         }
00155         else if (protocol == "inMem")
00156         {
00157             mIdMap.Add(1, "volatile"); // repos contains one and only one video set
00158         }
00159         else
00160         {
00161             ILOG_ERROR("Get: unknown protocol: " << protocol);
00162         }
00163     }
00164 
00165     void
00166     GetFromFileSystem(const Locator& loc)
00167     {
00168         ILOG_DEBUG("GetFromFileSystem");
00169         CmdOptions& options = CmdOptions::GetInstance();
00170         String repName = options.GetString("videoSetsRepository");
00171         File f = RepositoryInFileSystem::GetInstance().GetFile(loc, repName,
00172                                                                false, false);
00173         Util::IOBuffer* buf = f.GetReadBuffer();
00174         if (buf)
00175         {
00176             while (buf->Available())
00177             {
00178                 String line = buf->ReadLine();
00179                 if (line[0] && (line[0] != '#'))
00180                 {
00181                     Util::StringParser p(line);
00182                     int setId = p.GetInt();
00183                     if ((setId <= 0) || (setId > 255))
00184                         ILOG_ERROR("setId out of range [0-255]");
00185                     String setName = p.GetString2(false);
00186                     mIdMap.Add(setId, setName);
00187                 }
00188             }
00189             delete buf;
00190         }
00191     }
00192 
00193     void
00194     GetFromMonetDB(const Locator& loc)
00195     {
00196         ILOG_DEBUG("GetFromMonetDB");
00197         Connection* conn = RepositoryInMonetDB::GetInstance().GetConnection(loc);
00198 
00199         String query =
00200             "select identifier, set_name \
00201              from   video_sets \
00202              order by identifier;";
00203 
00204         MapiHdl hdl = conn->QueryPartStart(query);
00205         if (hdl == 0)
00206             return;
00207 
00208         int* id = 0;
00209         int idSize = 0;
00210         if (!conn->QueryPartFetchInt(hdl, 0, id, idSize))
00211             return;
00212 
00213         std::vector<String> sName;
00214         if (!conn->QueryPartFetchString(hdl, 1, sName))
00215             return;
00216 
00217         conn->QueryPartEnd(hdl);
00218 
00219         for (int i=0 ; i<idSize ; i++)
00220         {
00221             mIdMap.Add(id[i], sName[i]);
00222         }
00223         delete id;
00224     }
00225 
00226     void
00227     CopyToMonetDB(const Locator& loc) const
00228     {
00229         Connection* conn = RepositoryInMonetDB::GetInstance().GetConnection(loc);
00230 
00231         std::vector<int> ids = GetAllId();
00232         for (int i=0 ; i<ids.size() ; i++)
00233         {
00234             String name = GetSetName(ids[i]);
00235             String q = "select i_add_video_set(" + MakeString(ids[i])
00236                 + ", '" + name + "');";
00237             ILOG_INFO("q = [" + q + "]");
00238             conn->Query(q, false, false);
00239         }
00240     }
00241 
00242     String                          mFileName;
00243     Util::SimpleMap<int, String>    mIdMap;
00244     Util::SimpleMap<int, VideoSet*> mSetMap;
00245 
00246     ILOG_VAR_DEC;
00247 };
00248 
00249 ILOG_VAR_INIT(VideoSetsRepository, Impala.Persistency);
00250 
00251 } // namespace Persistency
00252 } // namespace Impala
00253 
00254 #endif

Generated on Thu Jan 13 09:05:13 2011 for ImpalaSrc by  doxygen 1.5.1