00001 #ifndef Impala_Persistency_SegmentationRepositoryInMonet_h
00002 #define Impala_Persistency_SegmentationRepositoryInMonet_h
00003
00004 #include "Persistency/RepositoryInMonetDB.h"
00005 #include "Core/VideoSet/Segmentation.h"
00006 #include "Persistency/SegmentationLocator.h"
00007
00008 namespace Impala
00009 {
00010 namespace Persistency
00011 {
00012
00013
00014 class SegmentationRepositoryInMonet
00015 {
00016 public:
00017
00018 typedef Link::Monet::Connection Connection;
00019 typedef Core::VideoSet::Segmentation Segmentation;
00020 typedef Core::VideoSet::VideoSet VideoSet;
00021
00022 SegmentationRepositoryInMonet()
00023 {
00024 }
00025
00026 Segmentation*
00027 Get(SegmentationLocator loc, VideoSet* vidSet)
00028 {
00029 Connection* conn = RepMonet().GetConnection(loc);
00030
00031 String vidSetBase = FileNameBase(loc.GetDataSet());
00032 String query =
00033 "select f.quid, fr.fragment_start, fr.fragment_length, \
00034 fr.fragment_name \
00035 from fragment fr, file f, video_set vs, video_sets vss \
00036 where vss.set_name = '" + vidSetBase + "' and \
00037 vs.video_sets_id = vss.video_sets_id and \
00038 f.file_id = vs.file_id and \
00039 fr.media_id = f.media_id and \
00040 fr.fragment_length > 1 and \
00041 fr.keyframe = false \
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 int* length = 0;
00059 int lengthSize = 0;
00060 if (!conn->QueryPartFetchInt(hdl, 2, length, lengthSize))
00061 return 0;
00062
00063 std::vector<String> name;
00064 if (!conn->QueryPartFetchString(hdl, 3, name))
00065 return 0;
00066
00067 conn->QueryPartEnd(hdl);
00068
00069 Segmentation* seg = new Segmentation(vidSet, "");
00070 for (int i=0 ; i<quidSize ; i++)
00071 {
00072 int id = QuidId(quid[i]);
00073 int end = start[i] + length[i] - 1;
00074 seg->Add(id, start[i], end, name[i]);
00075 }
00076 seg->UpdateGroups();
00077
00078 delete quid;
00079 delete start;
00080 delete length;
00081 return seg;
00082 }
00083
00084 void
00085 Add(const SegmentationLocator& loc, Segmentation* seg)
00086 {
00087 Connection* conn = RepMonet().GetConnection(loc);
00088
00089 VideoSet* vidSet = seg->GetVideoSet();
00090 conn->Query("delete from i_bulk_fragment;", false, false);
00091 Timer timer;
00092 for (int v=0 ; v<seg->GetNrVideos() ; v++)
00093 {
00094 ILOG_INFO("v = " << v);
00095 Quid vidQuid = vidSet->GetQuidVideo(v, true);
00096 int first = seg->GetFirstShotVideo(v);
00097 int nr = seg->GetNrShotsVideo(v);
00098 String q = "copy " + MakeString(nr) +
00099 " records into i_bulk_fragment from stdin using delimiters ' ';\n";
00100 for (int s=first ; s<first+nr ; s++)
00101 {
00102 int start = seg->GetStart(s);
00103 int len = seg->GetEnd(s) - start + 1;
00104 q += MakeString(vidQuid) + " " + MakeString(start) + " "
00105 + MakeString(len) + " " + seg->GetName(s)
00106 + " false false\n";
00107 }
00108 conn->Query(q, false, false);
00109
00110 nr = seg->GetNrFramesVideo(v);
00111 q = "copy " + MakeString(nr) +
00112 " records into i_bulk_fragment from stdin using delimiters ' ';\n";
00113 for (int f=0 ; f<nr ; f++)
00114 {
00115 q += MakeString(vidQuid) + " " + MakeString(f) + " 1 frame"
00116 + MakeString(f) + " false false\n";
00117 }
00118 conn->Query(q, false, false);
00119 }
00120 ILOG_INFO("Did bulk in " << timer.SplitTime());
00121 conn->Query("insert into fragment (media_id, fragment_start, \
00122 fragment_length, fragment_name, \
00123 keyframe, representative) \
00124 select m.media_id, bf.fragment_start, bf.fragment_length, \
00125 bf.fragment_name, bf.keyframe, bf.representative \
00126 from i_bulk_fragment bf, media m \
00127 where bf.quid = m.quid \
00128 order by bf.quid, bf.fragment_start;", false, false);
00129 ILOG_INFO("Did insert select at " << timer.SplitTime());
00130 conn->Query("delete from i_bulk_fragment;", false, false);
00131 }
00132
00133 private:
00134
00135 RepositoryInMonetDB&
00136 RepMonet()
00137 {
00138 return RepositoryInMonetDB::GetInstance();
00139 }
00140
00141 ILOG_VAR_DEC;
00142 };
00143
00144 ILOG_VAR_INIT(SegmentationRepositoryInMonet, Impala.Persistency);
00145
00146 }
00147 }
00148
00149 #endif