00001 #ifndef Impala_Persistency_SimilarityTableSetRepositoryInFile_h
00002 #define Impala_Persistency_SimilarityTableSetRepositoryInFile_h
00003
00004 #include "Persistency/RepositoryInFileSystem.h"
00005 #include "Core/Table/SimilarityTableSet.h"
00006 #include "Persistency/SimilarityTableSetLocator.h"
00007
00008 namespace Impala
00009 {
00010 namespace Persistency
00011 {
00012
00013
00014 class SimilarityTableSetRepositoryInFile
00015 {
00016 public:
00017
00018 typedef Core::Table::SimilarityTableSet SimilarityTableSet;
00019
00020 SimilarityTableSetRepositoryInFile()
00021 {
00022 }
00023
00024 bool
00025 Exists(const SimilarityTableSetLocator& loc)
00026 {
00027 String dir = GetDir(loc, false);
00028 File file = RepFS().GetFile(loc, dir, "names.txt", false, true);
00029 return file.Valid();
00030 }
00031
00032 SimilarityTableSet*
00033 Get(const SimilarityTableSetLocator& loc)
00034 {
00035 String dir = GetDir(loc, false);
00036 File file = RepFS().GetFile(loc, dir, "names.txt", false, false);
00037 if (!file.Valid())
00038 return 0;
00039 std::vector<String> names;
00040 file.ReadStrings(std::back_inserter(names));
00041 SimilarityTableSet* res = new SimilarityTableSet(names, 0);
00042 res->SetDescription(loc.GetFeature());
00043
00044 LoadQuids(res, loc, dir);
00045 LoadSims(res, loc, dir);
00046 LoadRanks(res, loc, dir);
00047 return res;
00048 }
00049
00050 void
00051 Add(const SimilarityTableSetLocator& loc, SimilarityTableSet* simSet)
00052 {
00053 String dir = GetDir(loc, false);
00054 Save(simSet, loc, dir);
00055 }
00056
00057 void
00058 Delete(const SimilarityTableSetLocator& loc)
00059 {
00060 if (loc.GetConceptSet() == "ALL")
00061 {
00062 String dir = GetDir(loc, true);
00063 RepFS().DeleteDir(loc, dir);
00064 }
00065 else
00066 {
00067 String dir = GetDir(loc, false);
00068 RepFS().DeleteDir(loc, dir);
00069 }
00070 }
00071
00072 private:
00073
00074 RepositoryInFileSystem&
00075 RepFS()
00076 {
00077 return RepositoryInFileSystem::GetInstance();
00078 }
00079
00080 String
00081 GetDir(const SimilarityTableSetLocator& loc, bool forAll)
00082 {
00083 String dir = loc.GetInIndexString();
00084 if ((!loc.GetInIndex()) && (loc.GetWalkType() != "Image"))
00085 dir = FileNameConcat(dir, loc.GetWalkType());
00086
00087 if (forAll)
00088 return dir;
00089
00090 dir = FileNameConcat(dir, loc.GetConceptSet());
00091 dir = FileNameConcat(dir, loc.GetModel());
00092 dir = FileNameConcat(dir, loc.GetFeature());
00093 if (!loc.GetInIndex())
00094 dir = FileNameConcat(dir, loc.GetContainer());
00095 return dir;
00096 }
00097
00098 void
00099 LoadQuids(SimilarityTableSet* res, const SimilarityTableSetLocator& loc,
00100 CString dir)
00101 {
00102 File file = RepFS().GetFile(loc, dir, "all_quids.tab", false, false);
00103 Read(res->GetQuidTable(), file);
00104 }
00105
00106 void
00107 LoadSim(SimilarityTableSet* res, int tableIdx,
00108 const SimilarityTableSetLocator& loc, CString dir)
00109 {
00110 String name = res->GetName(tableIdx) + "_sim.tab";
00111 File file = RepFS().GetFile(loc, dir, name, false, false);
00112 Read(res->GetSimTable(tableIdx), file);
00113 }
00114
00115 void
00116 LoadSims(SimilarityTableSet* res, const SimilarityTableSetLocator& loc,
00117 CString dir)
00118 {
00119 for (int i=0 ; i<res->NrTables() ; i++)
00120 LoadSim(res, i, loc, dir);
00121 }
00122
00123 void
00124 LoadRank(SimilarityTableSet* res, int tableIdx,
00125 const SimilarityTableSetLocator& loc, CString dir)
00126 {
00127 String name = res->GetName(tableIdx) + "_rank.tab";
00128 File file = RepFS().GetFile(loc, dir, name, false, false);
00129 Read(res->GetRankTable(tableIdx), file);
00130 }
00131
00132 void
00133 LoadRanks(SimilarityTableSet* res, const SimilarityTableSetLocator& loc,
00134 CString dir)
00135 {
00136 for (int i=0 ; i<res->NrTables() ; i++)
00137 LoadRank(res, i, loc, dir);
00138 }
00139
00140 void
00141 Save(SimilarityTableSet* simSet, const SimilarityTableSetLocator& loc,
00142 CString dir)
00143 {
00144 SaveNames(simSet, loc, dir);
00145 SaveQuids(simSet, loc, dir);
00146 SaveSims(simSet, loc, dir);
00147 SaveRanks(simSet, loc, dir);
00148 }
00149
00150 void
00151 SaveNames(SimilarityTableSet* simSet, const SimilarityTableSetLocator& loc,
00152 CString dir)
00153 {
00154 File file = RepFS().GetFile(loc, dir, "names.txt", true, false);
00155 std::vector<String> names = simSet->GetNames();
00156 file.WriteStrings(names);
00157 }
00158
00159 void
00160 SaveQuids(SimilarityTableSet* simSet, const SimilarityTableSetLocator& loc,
00161 CString dir)
00162 {
00163 File file = RepFS().GetFile(loc, dir, "all_quids.tab", true, false);
00164 Write(simSet->GetQuidTable(), file);
00165 }
00166
00167 void
00168 SaveSim(SimilarityTableSet* simSet, int tableIdx,
00169 const SimilarityTableSetLocator& loc, CString dir)
00170 {
00171 String name = simSet->GetName(tableIdx) + "_sim.tab";
00172 File file = RepFS().GetFile(loc, dir, name, true, false);
00173 Write(simSet->GetSimTable(tableIdx), file);
00174 }
00175
00176 void
00177 SaveSims(SimilarityTableSet* simSet, const SimilarityTableSetLocator& loc,
00178 CString dir)
00179 {
00180 for (int i=0 ; i<simSet->NrTables() ; i++)
00181 SaveSim(simSet, i, loc, dir);
00182 }
00183
00184 void
00185 SaveRank(SimilarityTableSet* simSet, int tableIdx,
00186 const SimilarityTableSetLocator& loc, CString dir)
00187 {
00188 String name = simSet->GetName(tableIdx) + "_rank.tab";
00189 File file = RepFS().GetFile(loc, dir, name, true, false);
00190 Write(simSet->GetRankTable(tableIdx), file);
00191 }
00192
00193 void
00194 SaveRanks(SimilarityTableSet* simSet, const SimilarityTableSetLocator& loc,
00195 CString dir)
00196 {
00197 for (int i=0 ; i<simSet->NrTables() ; i++)
00198 SaveRank(simSet, i, loc, dir);
00199 }
00200
00201 ILOG_VAR_DEC;
00202 };
00203
00204 ILOG_VAR_INIT(SimilarityTableSetRepositoryInFile, Impala.Persistency);
00205
00206 }
00207 }
00208
00209 #endif