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

FeatureTableRepositoryInMonet.h

Go to the documentation of this file.
00001 #ifndef Impala_Persistency_FeatureTableRepositoryInMonet_h
00002 #define Impala_Persistency_FeatureTableRepositoryInMonet_h
00003 
00004 #include "Persistency/VideoSetRepository.h"
00005 #include "Persistency/RepositoryInMonetDB.h"
00006 #include "Core/Feature/FeatureTable.h"
00007 #include "Persistency/FeatureLocator.h"
00008 #include "Util/BinHex.h"
00009 
00010 namespace Impala
00011 {
00012 namespace Persistency
00013 {
00014 
00015 
00016 class FeatureTableRepositoryInMonet
00017 {
00018 public:
00019 
00020     typedef Link::Monet::Connection Connection;
00021     typedef Core::Feature::FeatureTable FeatureTable;
00022 
00023     FeatureTableRepositoryInMonet()
00024     {
00025     }
00026 
00027     FeatureTable*
00028     Get(const FeatureLocator& loc)
00029     {
00030         if (loc.GetIsIndex())
00031         {
00032             ILOG_ERROR("Index not implemented (yet)");
00033             return 0;
00034         }
00035         if (loc.GetWalkType() != "Frames")
00036         {
00037             ILOG_ERROR("Only implemented for Frames");
00038             return 0;
00039         }
00040 
00041         Connection* conn = RepMonet().GetConnection(loc);
00042 
00043         String vidSetBase = FileNameBase(loc.GetDataSet());
00044         Core::Feature::FeatureDefinition def = loc.GetFeatureDef();
00045         Core::VideoSet::VideoSet* vidSet = VideoSetRepository().Get(loc);
00046         int fileIdx = vidSet->GetFileId(loc.GetContainer());
00047         Quid vidQuid = vidSet->GetQuidVideo(fileIdx, true);
00048 
00049         String query =
00050             "select fr.fragment_start, fv.vector \
00051              from feature_vector fv, fragment fr, file f, feature fe \
00052              where  f.quid = " + MakeString(vidQuid) + " and \
00053                     fr.media_id = f.media_id and \
00054                     fr.fragment_length = 1 and \
00055                     fr.keyframe = false and \
00056                     fe.feature_name = '" + loc.GetFeatureString() + "' and \
00057                     fe.feature_id = fv.feature_id and \
00058                     fv.fragment_id = fr.fragment_id and \
00059                     fv.file_id = f.file_id \
00060              order by fr.fragment_start;";
00061 
00062         MapiHdl hdl = conn->QueryPartStart(query);
00063         if (hdl == 0)
00064             return 0;
00065 
00066         //int nrRows = mapi_get_row_count(hdl);
00067 
00068         int* start = 0;
00069         int startSize = 0;
00070         if (!conn->QueryPartFetchInt(hdl, 0, start, startSize))
00071             return 0;
00072 
00073         std::vector<String> strs;
00074         if (!conn->QueryPartFetchString(hdl, 1, strs))
00075             return 0;
00076 
00077 
00079         //int numFieldsUsed = 2;
00080         //int fieldNrs[] = {0, 1};
00081         //int fieldTypes[] = {MAPI_INT, MAPI_VARCHAR};
00082         //void* recvBuffers[2];
00083         //if (!mCon->QueryPartFetchAll(hdl, numFieldsUsed, fieldNrs, fieldTypes, recvBuffers))
00084         //    return false;
00085         //int* theIntValues = recvBuffers[0];
00086         //String* theStringValues = recvBuffers[1];
00087         //int numRows = mapi_get_row_count(hdl);
00088 
00089 
00090         conn->QueryPartEnd(hdl);
00091 
00092         FeatureTable* res = 0;
00093         for (int i=0 ; i<startSize ; i++)
00094         {
00095             Quid q = vidSet->GetQuidFrame(fileIdx, start[i], true);
00096 
00097             size_t dataSize;
00098             UInt8* data = Util::Hex2Bin(strs[i], dataSize);        
00099             int n = dataSize / sizeof(Real64);
00100             Real64* realData = (Real64*) data;
00101             FeatureTable::VectorReal64 v(n, realData, true);
00102 
00103             if (res == 0)
00104                 res = new FeatureTable(loc.GetFeatureDef(), strs.size(), n);
00105             res->Add(q, v);
00106             delete data;
00107         }
00108 
00109         if (res == 0)
00110             ILOG_ERROR("No features found");
00111         delete start;
00112         return res;
00113     }
00114 
00115     void
00116     Add(const FeatureLocator& loc, FeatureTable* tab)
00117     {
00118         if (loc.GetIsIndex())
00119         {
00120             ILOG_ERROR("Index not implemented (yet)");
00121             return;
00122         }
00123         if (loc.GetWalkType() != "Frames")
00124         {
00125             ILOG_ERROR("Only implemented for Frames");
00126             return;
00127         }
00128 
00129         Connection* conn = RepMonet().GetConnection(loc);
00130 
00131         String vidSetBase = FileNameBase(loc.GetDataSet());
00132         Core::VideoSet::VideoSet* vidSet = VideoSetRepository().Get(loc);
00133         int fileIdx = vidSet->GetFileId(loc.GetContainer());
00134         Quid vidQuid = vidSet->GetQuidVideo(fileIdx, true);
00135 
00136         String q = "select i_add_feature('" + loc.GetFeatureString() + "');";
00137         conn->Query(q, false, false);
00138 
00139         conn->Query("delete from i_bulk_feature_vector;", false, false);
00140         int totalSize = tab->Size();
00141         int bulkSize = 2000;
00142         Timer timer;
00143         for (int i=0 ; i<totalSize ; i+=bulkSize)
00144         {
00145             int left = totalSize - i;
00146             int nr = (left > bulkSize) ? bulkSize : left;
00147             String q = "copy " + MakeString(nr) +
00148                 " records into i_bulk_feature_vector from stdin" +
00149                 " using delimiters ' ';\n";
00150             for (int j=i ; j<i+nr ; j++)
00151             {
00152                 Quid frameQuid = tab->Get1(j);
00153                 if (QuidClass(frameQuid) != QUID_CLASS_FRAME)
00154                     ILOG_ERROR("Quid is not a frame");
00155                 int frameNr = QuidId(frameQuid);
00156                 FeatureTable::VectorReal64 v = tab->Get2(j);
00157                 size_t dataSize = v.Size() * sizeof(Real64);
00158                 UInt8* data = (UInt8*) v.GetData();
00159                 String hexData = Util::Bin2Hex(data, dataSize);
00160 
00161                 q += MakeString(vidQuid) + " " + MakeString(frameNr) + " "
00162                     + hexData + "\n";
00163             }
00164             conn->Query(q, false, false);
00165             ILOG_INFO("Did bulk " << i << " at " << timer.SplitTime());
00166 
00167             conn->Query("insert into feature_vector (fragment_id, file_id, \
00168                                                      feature_id, vector) \
00169                          select fr.fragment_id, f.file_id, fe.feature_id, \
00170                                 bfv.vector \
00171                          from i_bulk_feature_vector bfv, file f, \
00172                               fragment fr, feature fe \
00173                          where bfv.quid = f.quid and \
00174                                f.media_id = fr.media_id and \
00175                                fr.fragment_start = bfv.fragment_start and \
00176                                fr.fragment_length = 1 and \
00177                                fr.keyframe = false and \
00178                                fe.feature_name = '" + loc.GetFeatureString() + "' \
00179                          order by bfv.fragment_start;", false, false);
00180             ILOG_INFO("Did insert select at " << timer.SplitTime());
00181             conn->Query("delete from i_bulk_feature_vector;", false, false);
00182         }
00183     }
00184 
00185 private:
00186 
00187     RepositoryInMonetDB&
00188     RepMonet()
00189     {
00190         return RepositoryInMonetDB::GetInstance();
00191     }
00192 
00193     ILOG_VAR_DEC;
00194 };
00195 
00196 ILOG_VAR_INIT(FeatureTableRepositoryInMonet, Impala.Persistency);
00197 
00198 } // namespace Persistency
00199 } // namespace Impala
00200 
00201 #endif

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