00001 #ifndef Impala_Persistency_File_h
00002 #define Impala_Persistency_File_h
00003
00004 #include "Persistency/FileSystem.h"
00005 #include "Basis/Endian.h"
00006
00007 namespace Impala
00008 {
00009 namespace Persistency
00010 {
00011
00012
00015 class File
00016 {
00017 public:
00018
00019 typedef Util::IOBuffer IOBuffer;
00020
00021 File()
00022 {
00023 mPath = "";
00024 mFileSystem = 0;
00025 }
00026
00027 File(CString path)
00028 {
00029 mPath = path;
00030 mFileSystem = 0;
00031 }
00032
00033 File(CString path, FileSystem* fs)
00034 {
00035 mPath = path;
00036 mFileSystem = fs;
00037 }
00038
00039 bool
00040 Valid() const
00041 {
00042 return (!mPath.empty() && (mFileSystem != 0));
00043 }
00044
00045 String
00046 GetPath() const
00047 {
00048 return mPath;
00049 }
00050
00051 FileSystem*
00052 GetFileSystem() const
00053 {
00054 return mFileSystem;
00055 }
00056
00057
00058
00059 template<class BackInsertIterator>
00060 void
00061 ReadStrings(BackInsertIterator bi, bool skipEmptyAndComment = true)
00062 {
00063 IOBuffer* buf = GetReadBuffer();
00064 if (! (buf && buf->Valid()))
00065 {
00066 return;
00067 }
00068
00069 while (buf->Available())
00070 {
00071 String line = buf->ReadLine();
00072 if (line[0] || !skipEmptyAndComment)
00073 {
00074 if ((line[0] != '#') || !skipEmptyAndComment)
00075 *bi++ = line;
00076 }
00077 }
00078 }
00079
00080 void
00081 ReadStrings(std::vector<String>& stringList)
00082 {
00083 ReadStrings(std::back_inserter(stringList));
00084 }
00085
00086 template <class Iterator>
00087 bool
00088 WriteStrings(Iterator begin, Iterator end)
00089 {
00090 IOBuffer* buf = GetWriteBuffer();
00091 if (! (buf && buf->Valid()))
00092 {
00093 return false;
00094 }
00095 for (Iterator it=begin ; it!=end ; it++)
00096 {
00097 String s = *it;
00098 buf->Puts(s);
00099 }
00100 return true;
00101 }
00102
00103 void
00104 WriteStrings(const std::vector<String>& stringList)
00105 {
00106 WriteStrings(stringList.begin(), stringList.end());
00107 }
00108
00109
00110
00111 template<class BackInsertIterator>
00112 void
00113 ReadNative(BackInsertIterator bi)
00114 {
00115 typedef typename BackInsertIterator::container_type::value_type elem_type;
00116
00117 IOBuffer* buf = GetReadBuffer();
00118 if (! (buf && buf->Valid()))
00119 return;
00120
00121 while (buf->Available() > 0)
00122 {
00123 elem_type d;
00124 buf->Read(&d, sizeof(d));
00125 EndianSwap(&d);
00126 *bi++ = d;
00127 }
00128 }
00129
00130 template <class Iterator>
00131 void
00132 WriteNative(Iterator begin, Iterator end)
00133 {
00134 typedef typename Iterator::value_type elem_type;
00135
00136 IOBuffer* buf = GetWriteBuffer();
00137 for (Iterator it=begin ; it!=end ; it++)
00138 {
00139 elem_type d = *it;
00140 EndianSwap(&d);
00141 buf->Write(&d, sizeof(d));
00142 }
00143 delete buf;
00144 }
00145
00146
00147
00148
00149 IOBuffer*
00150 GetReadBuffer()
00151 {
00152 if (!Valid())
00153 {
00154 ILOG_ERROR("Can't get read buffer for invalid file");
00155 return 0;
00156 }
00157 return mFileSystem->GetIOBuffer(GetPath(), true, false, "", 0, true);
00158 }
00159
00160
00161 IOBuffer*
00162 GetReadBuffer(bool useMemoryIfLocal, String useLocalFileIfRemote,
00163 Int64 useLocalSizeIfRemote = 0,
00164 bool useIOBufferChannel = false)
00165 {
00166 if (!Valid())
00167 {
00168 ILOG_ERROR("Can't get read buffer for invalid file");
00169 return 0;
00170 }
00171 return mFileSystem->GetIOBuffer(GetPath(), true, useMemoryIfLocal,
00172 useLocalFileIfRemote,
00173 useLocalSizeIfRemote,
00174 useIOBufferChannel);
00175 }
00176
00177
00178 IOBuffer*
00179 GetWriteBuffer()
00180 {
00181 if (!Valid())
00182 {
00183 ILOG_ERROR("Can't get write buffer for invalid file");
00184 return 0;
00185 }
00186 return mFileSystem->GetIOBuffer(GetPath(), false, false, "tmp", 0, true);
00187 }
00188
00189
00190 IOBuffer*
00191 GetWriteBuffer(bool useMemoryIfLocal, String useLocalFileIfRemote,
00192 Int64 useLocalSizeIfRemote = 0,
00193 bool useIOBufferChannel = false)
00194 {
00195 if (!Valid())
00196 {
00197 ILOG_ERROR("Can't get write buffer for invalid file");
00198 return 0;
00199 }
00200 return mFileSystem->GetIOBuffer(GetPath(), false, useMemoryIfLocal,
00201 useLocalFileIfRemote,
00202 useLocalSizeIfRemote,
00203 useIOBufferChannel);
00204 }
00205
00206 private:
00207
00208 String mPath;
00209 FileSystem* mFileSystem;
00210
00211 ILOG_VAR_DEC;
00212 };
00213
00214 ILOG_VAR_INIT(File, Impala.Persistency);
00215
00216 }
00217 }
00218
00219 #endif