00001 #ifndef Impala_Persistency_SimilarityTableSetRepositoryInMonet_h
00002 #define Impala_Persistency_SimilarityTableSetRepositoryInMonet_h
00003
00004 #include "Persistency/VideoSetRepository.h"
00005 #include "Persistency/KeywordListRepository.h"
00006 #include "Persistency/RepositoryInMonetDB.h"
00007 #include "Core/Table/SimilarityTableSet.h"
00008 #include "Persistency/SimilarityTableSetLocator.h"
00009
00010 namespace Impala
00011 {
00012 namespace Persistency
00013 {
00014
00015
00016 class SimilarityTableSetRepositoryInMonet
00017 {
00018 public:
00019
00020 typedef Link::Monet::Connection Connection;
00021 typedef Core::Table::SimilarityTableSet SimilarityTableSet;
00022 typedef Core::Table::SimilarityTableSet::SimTableType SimTableType;
00023 typedef Core::Table::QuidTable QuidTable;
00024 typedef Core::Table::KeywordList KeywordList;
00025
00026 SimilarityTableSetRepositoryInMonet()
00027 {
00028 }
00029
00030 SimilarityTableSet*
00031 Get(const SimilarityTableSetLocator& loc)
00032 {
00033 if (loc.GetInIndex())
00034 {
00035 ILOG_ERROR("Index not implemented (yet)");
00036 return 0;
00037 }
00038 if (loc.GetWalkType() != "Frames")
00039 {
00040 ILOG_ERROR("Only implemented for Frames");
00041 return 0;
00042 }
00043
00044 Connection* conn = RepMonet().GetConnection(loc);
00045
00046 KeywordListLocator keyLoc(loc, loc.GetConceptSet());
00047 KeywordList keyList = *(KeywordListRepository().Get(keyLoc));
00048 if (keyList.size() == 0)
00049 {
00050 ILOG_ERROR("Could find keywords");
00051 return 0;
00052 }
00053
00054 SimilarityTableSet* res = new SimilarityTableSet(keyList, 0);
00055 res->SetDescription(loc.GetFeature());
00056
00057 LoadSims(res, loc);
00058 res->ComputeRanks(true);
00059 return res;
00060 }
00061
00062 void
00063 Add(const SimilarityTableSetLocator& loc, SimilarityTableSet* simSet)
00064 {
00065 if (loc.GetInIndex())
00066 {
00067 ILOG_ERROR("Index not implemented (yet)");
00068 return;
00069 }
00070 if (loc.GetWalkType() != "Frames")
00071 {
00072 ILOG_ERROR("Only implemented for Frames");
00073 return;
00074 }
00075
00076 SaveSims(simSet, loc);
00077 }
00078
00079 private:
00080
00081 RepositoryInMonetDB&
00082 RepMonet()
00083 {
00084 return RepositoryInMonetDB::GetInstance();
00085 }
00086
00087 void
00088 LoadSim(SimilarityTableSet* res, int tableIdx,
00089 const SimilarityTableSetLocator& loc)
00090 {
00091 Connection* conn = RepMonet().GetConnection(loc);
00092
00093 String vidSetBase = FileNameBase(loc.GetDataSet());
00094 String conceptSetBase = FileNameBase(loc.GetConceptSet());
00095 String concept = res->GetName(tableIdx);
00096
00097 Core::VideoSet::VideoSet* vidSet = VideoSetRepository().Get(loc);
00098 int fileIdx = vidSet->GetFileId(loc.GetContainer());
00099 Quid vidQuid = vidSet->GetQuidVideo(fileIdx, false);
00100
00101 String query =
00102 "select fr.fragment_start, s.confidence \
00103 from video_sets vss, video_set vs, keyword_sets kss, \
00104 keyword_set ks, keyword k, model_sets_name msn, \
00105 model_sets mss, model_ref mr, \
00106 file f, fragment fr, score s \
00107 where vss.set_name = '" + vidSetBase + "' and \
00108 kss.set_name = '" + conceptSetBase + "' and \
00109 kss.video_sets_id = vss.video_sets_id and \
00110 msn.set_name = '" + loc.GetFeature() + "' and \
00111 mss.model_sets_name_id = msn.model_sets_name_id and \
00112 k.keyword_name = '" + concept + "' and \
00113 ks.keyword_sets_id = kss.keyword_sets_id and \
00114 ks.keyword_id = k.keyword_id and \
00115 mr.model_name = '" + loc.GetModel() + "' and \
00116 mr.keyword_set_id = ks.keyword_set_id and \
00117 mr.model_ref_id = mss.model_ref_id and \
00118 vs.video_sets_id = vss.video_sets_id and \
00119 f.file_id = vs.file_id and \
00120 f.quid = " + MakeString(vidQuid) + " and \
00121 fr.media_id = f.media_id and \
00122 fr.fragment_length = 1 and \
00123 fr.keyframe = false and \
00124 s.fragment_id = fr.fragment_id and \
00125 s.model_sets_id = mss.model_sets_id and \
00126 s.file_id = f.file_id \
00127 order by fr.fragment_start;";
00128
00129 MapiHdl hdl = conn->QueryPartStart(query);
00130 if (hdl == 0)
00131 return;
00132
00133 int* start = 0;
00134 int startSize = 0;
00135 if (!conn->QueryPartFetchInt(hdl, 0, start, startSize))
00136 return;
00137
00138 double* sims = 0;
00139 int simSize = 0;
00140 if (!conn->QueryPartFetchDouble(hdl, 1, sims, simSize))
00141 return;
00142
00143 conn->QueryPartEnd(hdl);
00144
00145 SimTableType* simTab = res->GetSimTable(tableIdx);
00146 QuidTable* quidTab = res->GetQuidTable();
00147 for (int i=0 ; i<startSize ; i++)
00148 {
00149 simTab->Add(sims[i]);
00150 Quid q = vidSet->GetQuidFrame(fileIdx, start[i], true);
00151 if (tableIdx == 0)
00152 {
00153 quidTab->Add(q);
00154 }
00155 else
00156 {
00157 if (quidTab->Get1(i) != q)
00158 {
00159 ILOG_ERROR("Quid mismatch");
00160 }
00161 }
00162 }
00163
00164 delete start;
00165 delete sims;
00166 }
00167
00168 void
00169 LoadSims(SimilarityTableSet* res, const SimilarityTableSetLocator& loc)
00170 {
00171 for (size_t i=0 ; i<res->NrTables() ; i++)
00172 LoadSim(res, i, loc);
00173 }
00174
00175 void
00176 SaveSim(SimilarityTableSet* simSet, int tableIdx,
00177 const SimilarityTableSetLocator& loc)
00178 {
00179 typedef SimilarityTableSet::SimTableType SimTableType;
00180
00181 Connection* conn = RepMonet().GetConnection(loc);
00182
00183 Core::VideoSet::VideoSet* vidSet = VideoSetRepository().Get(loc);
00184 int fileIdx = vidSet->GetFileId(loc.GetContainer());
00185 Quid vidQuid = vidSet->GetQuidVideo(fileIdx, false);
00186
00187 SimTableType* simTable = simSet->GetSimTable(tableIdx);
00188 QuidTable* quidTab = simSet->GetQuidTable();
00189 int bulkSize = 100000;
00190 Timer timer;
00191 for (int i=0 ; i<simTable->Size() ; i+=bulkSize)
00192 {
00193 int left = simTable->Size() - i;
00194 int nr = (left > bulkSize) ? bulkSize : left;
00195 String q = "copy " + MakeString(nr) +
00196 " records into i_bulk_score from stdin using delimiters ' ';\n";
00197 for (int j=i ; j<i+nr ; j++)
00198 {
00199 Quid frameQuid = quidTab->Get1(j);
00200 if (QuidClass(frameQuid) != QUID_CLASS_FRAME)
00201 ILOG_ERROR("Quid is not a frame");
00202 int frameNr = QuidId(frameQuid);
00203
00204 q += MakeString(vidQuid) + " " + MakeString(frameNr) + " "
00205 + MakeString(simTable->Get1(j)) + "\n";
00206 }
00207 conn->Query(q, false, false);
00208 ILOG_INFO("Did bulk at " << timer.SplitTime());
00209 }
00210
00211 String vidSetBase = FileNameBase(loc.GetDataSet());
00212 String conceptSetBase = FileNameBase(loc.GetConceptSet());
00213 String model = loc.GetModel();
00214 String feature = loc.GetFeature();
00215 String concept = simSet->GetName(tableIdx);
00216
00217 String q = "select i_add_feature('" + feature + "');";
00218 ILOG_INFO("q = [" + q + "]");
00219 conn->Query(q, false, false);
00220
00221 q = "select i_add_model('" + vidSetBase + "', '" +
00222 conceptSetBase + "', '" + concept + "', '" + model +
00223 "', '" + feature + "');";
00224 ILOG_INFO("q = [" + q + "]");
00225 conn->Query(q, false, false);
00226
00227
00228 q = "select i_add_model_sets('" + vidSetBase + "', '" +
00229 conceptSetBase + "', '" + concept + "', '" + model +
00230 "', '" + feature + "');";
00231 ILOG_INFO("q = [" + q + "]");
00232 conn->Query(q, false, false);
00233 q = "select i_add_model_to_set('" + vidSetBase + "', '" +
00234 conceptSetBase + "', '" + concept + "', '" + model +
00235 "', '" + feature + "', '" + feature + "');";
00236 ILOG_INFO("q = [" + q + "]");
00237 conn->Query(q, false, false);
00238
00239 conn->Query("insert into score (fragment_id, model_sets_id, file_id, \
00240 confidence) \
00241 select fr.fragment_id, mss.model_sets_id, f.file_id, \
00242 bs.confidence \
00243 from video_sets vss, video_set vs, keyword_sets kss, \
00244 keyword_set ks, keyword k, model_sets_name msn, \
00245 model_sets mss, model_ref mr, \
00246 file f, fragment fr, i_bulk_score bs \
00247 where vss.set_name = '" + vidSetBase + "' and \
00248 kss.set_name = '" + conceptSetBase + "' and \
00249 kss.video_sets_id = vss.video_sets_id and \
00250 msn.set_name = '" + feature + "' and \
00251 mss.model_sets_name_id = msn.model_sets_name_id and \
00252 k.keyword_name = '" + concept + "' and \
00253 ks.keyword_sets_id = kss.keyword_sets_id and \
00254 ks.keyword_id = k.keyword_id and \
00255 mr.model_name = '" + model + "' and \
00256 mr.keyword_set_id = ks.keyword_set_id and \
00257 mr.model_ref_id = mss.model_ref_id and \
00258 vs.video_sets_id = vss.video_sets_id and \
00259 f.file_id = vs.file_id and \
00260 bs.quid = f.quid and \
00261 fr.media_id = f.media_id and \
00262 fr.fragment_start = bs.fragment_start and \
00263 fr.fragment_length = 1 and \
00264 fr.keyframe = false \
00265 order by bs.fragment_start;", false, false);
00266 ILOG_INFO("Did insert select at " << timer.SplitTime());
00267 conn->Query("delete from i_bulk_score;", false, false);
00268 ILOG_INFO("Done in " << timer.SplitTime());
00269 }
00270
00271 void
00272 SaveSims(SimilarityTableSet* simSet, const SimilarityTableSetLocator& loc)
00273 {
00274 for (int i=0 ; i<simSet->NrTables() ; i++)
00275 SaveSim(simSet, i, loc);
00276 }
00277
00278 ILOG_VAR_DEC;
00279 };
00280
00281 ILOG_VAR_INIT(SimilarityTableSetRepositoryInMonet, Impala.Persistency);
00282
00283 }
00284 }
00285
00286 #endif