00001 #ifndef Impala_Persistency_KeyframesRepositoryInMonet_h
00002 #define Impala_Persistency_KeyframesRepositoryInMonet_h
00003
00004 #include "Persistency/SegmentationRepository.h"
00005 #include "Persistency/RepositoryInMonetDB.h"
00006 #include "Core/VideoSet/Keyframes.h"
00007 #include "Persistency/KeyframesLocator.h"
00008
00009 namespace Impala
00010 {
00011 namespace Persistency
00012 {
00013
00014
00015 class KeyframesRepositoryInMonet
00016 {
00017 public:
00018
00019 typedef Link::Monet::Connection Connection;
00020 typedef Core::VideoSet::Keyframes Keyframes;
00021 typedef Core::VideoSet::Segmentation Segmentation;
00022 typedef Core::VideoSet::VideoSet VideoSet;
00023
00024 KeyframesRepositoryInMonet()
00025 {
00026 }
00027
00028 Keyframes*
00029 Get(KeyframesLocator loc, VideoSet* vidSet)
00030 {
00031 Connection* conn = RepMonet().GetConnection(loc);
00032
00033 String vidSetBase = FileNameBase(loc.GetDataSet());
00034 String query =
00035 "select f.quid, fr.fragment_start, fr.fragment_name \
00036 from fragment fr, file f, video_set vs, video_sets vss \
00037 where vss.set_name = '" + vidSetBase + "' and \
00038 vs.video_sets_id = vss.video_sets_id and \
00039 f.file_id = vs.file_id and \
00040 fr.media_id = f.media_id and \
00041 fr.keyframe = true \
00042 order by f.quid, fr.fragment_start;";
00043
00044 MapiHdl hdl = conn->QueryPartStart(query);
00045 if (hdl == 0)
00046 return 0;
00047
00048 Quid* quid = 0;
00049 int quidSize = 0;
00050 if (!conn->QueryPartFetchULL(hdl, 0, quid, quidSize))
00051 return 0;
00052
00053 int* start = 0;
00054 int startSize = 0;
00055 if (!conn->QueryPartFetchInt(hdl, 1, start, startSize))
00056 return 0;
00057
00058 std::vector<String> name;
00059 if (!conn->QueryPartFetchString(hdl, 2, name))
00060 return 0;
00061
00062 conn->QueryPartEnd(hdl);
00063
00064 SegmentationLocator segLoc(loc.GetProtocolAndHost(), loc.GetDataSet(),
00065 "segmentation");
00066 Segmentation* seg = SegmentationRepository().Get(segLoc, vidSet);
00067 Keyframes* keys = new Keyframes(vidSet, "");
00068 for (int i=0 ; i<quidSize ; i++)
00069 {
00070 int vidId = QuidId(quid[i]);
00071 int shotId = seg->GetShotId(vidId, start[i]);
00072 keys->Add(vidId, shotId, start[i], name[i]);
00073 }
00074 keys->UpdateGroups();
00075 delete seg;
00076
00077 delete quid;
00078 delete start;
00079 return keys;
00080 }
00081
00082 void
00083 Add(const KeyframesLocator& loc, Keyframes* keys)
00084 {
00085 Connection* conn = RepMonet().GetConnection(loc);
00086
00087 VideoSet* vidSet = keys->GetVideoSet();
00088 conn->Query("delete from i_bulk_fragment;", false, false);
00089 Timer timer;
00090 for (int v=0 ; v<keys->GetNrVideos() ; v++)
00091 {
00092 ILOG_INFO("v = " << v);
00093 Quid vidQuid = vidSet->GetQuidVideo(v, true);
00094 int first = keys->GetFirstKeyframeVideo(v);
00095 int nr = keys->GetNrKeyframesVideo(v);
00096 String q = "copy " + MakeString(nr) +
00097 " records into i_bulk_fragment from stdin using delimiters ' ';\n";
00098 for (int k=first ; k<first+nr ; k++)
00099 {
00100
00101 int frame = keys->GetFrameNr(k);
00102 String rkf = (keys->IsRKF(k)) ? "true" : "false";
00103 q += MakeString(vidQuid) + " " + MakeString(frame) + " 1 "
00104 + keys->GetName(k) + " true " + rkf + "\n";
00105 }
00106 conn->Query(q, false, false);
00107 }
00108 ILOG_INFO("Did bulk in " << timer.SplitTime());
00109 conn->Query("insert into fragment (media_id, fragment_start, \
00110 fragment_length, fragment_name, \
00111 keyframe, representative) \
00112 select m.media_id, bf.fragment_start, bf.fragment_length, \
00113 bf.fragment_name, bf.keyframe, bf.representative \
00114 from i_bulk_fragment bf, media m \
00115 where bf.quid = m.quid \
00116 order by bf.quid, bf.fragment_start;", false, false);
00117 ILOG_INFO("Did insert select at " << timer.SplitTime());
00118 conn->Query("delete from i_bulk_fragment;", false, false);
00119 }
00120
00121 private:
00122
00123 RepositoryInMonetDB&
00124 RepMonet()
00125 {
00126 return RepositoryInMonetDB::GetInstance();
00127 }
00128
00129 ILOG_VAR_DEC;
00130 };
00131
00132 ILOG_VAR_INIT(KeyframesRepositoryInMonet, Impala.Persistency);
00133
00134 }
00135 }
00136
00137 #endif