00001 #ifndef Impala_Persistency_FeatureTableRepositoryInFile_h
00002 #define Impala_Persistency_FeatureTableRepositoryInFile_h
00003
00004 #include "Persistency/RepositoryInFileSystem.h"
00005 #include "Core/Feature/FeatureTable.h"
00006 #include "Persistency/FeatureLocator.h"
00007 #include "Core/Table/Write.h"
00008 #include "Link/Mpi/MpiFuncs.h"
00009
00010 namespace Impala
00011 {
00012 namespace Persistency
00013 {
00014
00015
00016 class FeatureTableRepositoryInFile
00017 {
00018 public:
00019
00020 typedef Core::Feature::FeatureTable FeatureTable;
00021
00022 FeatureTableRepositoryInFile()
00023 {
00024 }
00025
00026 bool
00027 Exists(const FeatureLocator& loc)
00028 {
00029 String dir = GetDir(loc, false);
00030 String name = loc.GetFeatureString() + loc.GetSuffix();
00031 File file = RepFS().GetFile(loc, dir, name, false, true);
00032 return file.Valid();
00033 }
00034
00035 FeatureTable*
00036 Get(const FeatureLocator& loc)
00037 {
00038 FeatureTable* res = new FeatureTable(loc.GetFeatureDef());
00039 String dir = GetDir(loc, false);
00040 String name = loc.GetFeatureString() + loc.GetSuffix();
00041 if (!(loc.GetUseBroadcast() && Link::Mpi::MpiUsed()))
00042 {
00043 File file = RepFS().GetFile(loc, dir, name, false, false);
00044 if (!file.Valid())
00045 return 0;
00046 Read(res, file);
00047 }
00048 else
00049 {
00050
00051 Util::IOBuffer* buf = 0;
00052 int myId = Link::Mpi::MyId();
00053 int root = 0;
00054 if (myId == root)
00055 {
00056 File file = RepFS().GetFile(loc, dir, name, false, false);
00057 buf = file.GetReadBuffer(true, "");
00058 if (buf == 0 || !buf->Valid())
00059 {
00060 ILOG_ERROR("node0 couldn't open file");
00061 }
00062 }
00063 else
00064 {
00065 buf = new Util::IOBuffer();
00066 }
00067 Broadcast(buf, root);
00068 if (buf && buf->Valid())
00069 {
00070
00071 Read(res, buf);
00072 delete buf;
00073 }
00074 else
00075 {
00076 ILOG_ERROR(myId << ": could not read file from buffer");
00077 }
00078 Link::Mpi::Barrier();
00079 }
00080 return res;
00081 }
00082
00083 void
00084 Add(const FeatureLocator& loc, FeatureTable* tab)
00085 {
00086 String dir = GetDir(loc, false);
00087 File file = RepFS().GetFile(loc, dir, loc.GetFeatureString() + ".tab",
00088 true, false);
00089 Write(tab, file);
00090 }
00091
00092 void
00093 Delete(const FeatureLocator& loc)
00094 {
00095 if ((loc.GetFeatureName() == "ALL") && (loc.GetContainer() == "ALL"))
00096 {
00097 String dir = GetDir(loc, true);
00098 RepFS().DeleteDir(loc, dir);
00099 }
00100 else
00101 {
00102 String dir = GetDir(loc, false);
00103 String name = loc.GetFeatureString() + loc.GetSuffix();
00104 File file = RepFS().GetFile(loc, dir, name, true, true);
00105 file.Delete();
00106 }
00107 }
00108
00109 File
00110 ExposeFile(const FeatureLocator& loc, bool toWrite)
00111 {
00112 String dir = GetDir(loc, false);
00113 String name = loc.GetFeatureString() + loc.GetSuffix();
00114 bool silent = !toWrite;
00115 return RepFS().GetFile(loc, dir, name, toWrite, silent);
00116 }
00117
00118 private:
00119
00120 RepositoryInFileSystem&
00121 RepFS()
00122 {
00123 return RepositoryInFileSystem::GetInstance();
00124 }
00125
00126 String
00127 GetDir(const FeatureLocator& loc, bool forAll)
00128 {
00129 String dir = loc.GetTopString();
00130 if (loc.GetWalkType() != "Image")
00131 dir = FileNameConcat(dir, loc.GetWalkType());
00132
00133 if (forAll)
00134 return dir;
00135
00136 if (!loc.GetIsCodebook())
00137 dir = FileNameConcat(dir, loc.GetFeatureName());
00138 dir = FileNameConcat(dir, loc.GetContainer());
00139 if (loc.GetIsPartial())
00140 dir = FileNameConcat(dir, "part_" + MakeString(loc.GetStartFrame()));
00141 return dir;
00142 }
00143
00144 ILOG_VAR_DEC;
00145 };
00146
00147 ILOG_VAR_INIT(FeatureTableRepositoryInFile, Impala.Persistency);
00148
00149 }
00150 }
00151
00152 #endif