00001 #ifndef Impala_Persistency_ImageArchiveRepositoryInMonet_h
00002 #define Impala_Persistency_ImageArchiveRepositoryInMonet_h
00003
00004 #include "Persistency/RepositoryInMonetDB.h"
00005 #include "Core/Array/ImageArchiveMapi.h"
00006 #include "Persistency/ImageArchiveLocator.h"
00007
00008 namespace Impala
00009 {
00010 namespace Persistency
00011 {
00012
00013
00014 class ImageArchiveRepositoryInMonet
00015 {
00016 public:
00017
00018 typedef Link::Monet::Connection Connection;
00019 typedef Core::Array::ImageArchive ImageArchive;
00020 typedef Core::Array::ImageArchiveMapi ImageArchiveMapi;
00021 typedef Core::Array::Array2dVec3UInt8 Array2dVec3UInt8;
00022
00023 ImageArchiveRepositoryInMonet()
00024 {
00025 }
00026
00027 ImageArchive*
00028 Get(const ImageArchiveLocator& loc)
00029 {
00030 if (!loc.GetFrames())
00031 {
00032 ILOG_ERROR("Only implemented for frames");
00033 return 0;
00034 }
00035
00036 Connection* conn = RepMonet().GetConnection(loc);
00037 ImageArchive* res = new ImageArchiveMapi(loc.GetQuid(), conn);
00038 return res;
00039 }
00040
00041 void
00042 Add(const ImageArchiveLocator& loc, ImageArchive* archive)
00043 {
00044 if (!loc.GetFrames())
00045 {
00046 ILOG_ERROR("Only implemented for frames");
00047 return;
00048 }
00049 if (loc.GetName() != "images_jpg.raw")
00050 {
00051 ILOG_ERROR("Only implemented for jpg's of all frames");
00052 return;
00053 }
00054
00055 Connection* conn = RepMonet().GetConnection(loc);
00056 Quid vidQuid = loc.GetQuid();
00057
00058 conn->Query("delete from i_bulk_frame_image;", false, false);
00059 int totalSize = archive->NrImages();
00060 int bulkSize = 4000;
00061 Timer timer;
00062
00063 for (int i=0 ; i<totalSize ; i+=bulkSize)
00064 {
00065 int left = totalSize - i;
00066 int nr = (left > bulkSize) ? bulkSize : left;
00067 String q = "copy " + MakeString(nr) +
00068 " records into i_bulk_frame_image from stdin" +
00069 " using delimiters ' ';\n";
00070 for (int j=i ; j<i+nr ; j++)
00071 {
00072 Array2dVec3UInt8* im = archive->ReadImage(j);
00073 size_t dataSize = 0;
00074 UInt8* data = archive->GetImageData(j, dataSize);
00075 String hexData = Util::Bin2Hex(data, dataSize);
00076 delete data;
00077 String name = "frame" + MakeString(j);
00078 String width = MakeString(im->CW());
00079 String height = MakeString(im->CH());
00080 q += MakeString(vidQuid) + " " + MakeString(j) + " " + name + " "
00081 + hexData + " " + width + " " + height + " image/jpg \n";
00082 delete im;
00083 }
00084 conn->Query(q, false, false);
00085 ILOG_INFO("Did bulk " << i << " at " << timer.SplitTime());
00086
00087 conn->Query("insert into frame_image (fragment_id, image, width, \
00088 height, mime_type) \
00089 select fr.fragment_id, bf.image, bf.width, bf.height, \
00090 bf.mime_type \
00091 from i_bulk_frame_image bf, file f, fragment fr \
00092 where bf.quid = f.quid and \
00093 f.media_id = fr.media_id and \
00094 fr.fragment_start = bf.fragment_start and \
00095 fr.fragment_length = 1 and \
00096 fr.keyframe = false \
00097 order by bf.quid, bf.fragment_start;", false, false);
00098 ILOG_INFO("Did insert select at " << timer.SplitTime());
00099 conn->Query("delete from i_bulk_frame_image;", false, false);
00100 }
00101 }
00102
00103 private:
00104
00105 RepositoryInMonetDB&
00106 RepMonet()
00107 {
00108 return RepositoryInMonetDB::GetInstance();
00109 }
00110
00111 ILOG_VAR_DEC;
00112 };
00113
00114 ILOG_VAR_INIT(ImageArchiveRepositoryInMonet, Impala.Persistency);
00115
00116 }
00117 }
00118
00119 #endif