Add infos into target window
[ryzomcore.git] / ryzom / server / src / pd_lib / db_reference_file.h
blob269e5278fd2f5e68f669bcefaa07d71ea3045a29
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #ifndef NL_DB_REFERENCE_FILE_H
18 #define NL_DB_REFERENCE_FILE_H
20 #include <nel/misc/types_nl.h>
21 #include <nel/misc/stream.h>
22 #include <nel/misc/debug.h>
23 #include <nel/misc/path.h>
25 #include <map>
27 #include "pd_server_utils.h"
30 /**
31 * <Class description>
32 * \author Benjamin Legros
33 * \author Nevrax France
34 * \date 2003
36 class CDBReferenceFile : public CMixedStreamFile
38 public:
40 /// Constructor
41 CDBReferenceFile();
43 /// Destructor
44 ~CDBReferenceFile();
46 /// Setup file name and path
47 void setup(const std::string& name, const std::string& path, uint32 baseIndex, uint32 overIndex, uint32 rowSize);
49 /// Clear initial setup
50 void clear();
52 /// Is Initialised?
53 bool initialised() const { return _Init; }
55 /// close file
56 void close();
59 /**
60 * Builds an empty file
62 bool buildEmptyRef();
68 /**
69 * Prewrite reference file.
70 * At least, read file header to known the base and index index in file
72 bool prewrite(bool failIfNotExist = false);
74 /**
75 * Update Start/End Delta Ids
77 bool updateDeltaIds(uint32 startId, uint32 endId);
79 /**
80 * Get Start/End Delta Ids
82 void getUpdateDeltaIds(uint32& startId, uint32& endId);
84 /**
85 * Update a row in the reference file
86 * \param index is the absolute row index to update, not relative to file base index
87 * \param data is the data buffer to store in file
89 bool update(uint32 index, const uint8* rowdata);
91 /**
92 * Postwrite reference file
93 * Mark file as valid, close file, flush anything still alive...
95 bool postwrite();
103 * Preload reference file.
104 * At least, read file header to known the base and index index in file
106 bool preload();
109 * Read a row in the reference file
110 * \param index is the absolute row index to read, not relative to file base index
111 * \param data is the data buffer to store data read from file
113 bool read(uint32 index, uint8* rowdata);
122 * Get standard row header size
124 static uint32 getRowHeaderSize() { return sizeof(uint32); }
127 * Get reference file name
129 static std::string getRefFileName(uint32 tableId, uint32 refFile) { return NLMISC::toString("%04X_%04X.%s", tableId, refFile, getRefFileExt().c_str()); }
132 * Get reference file name
134 static bool isRefFile(const std::string& filename, uint32& tableId, uint32& refFile)
136 return sscanf(NLMISC::CFile::getFilename(filename).c_str(), "%X_%X.dbref", &tableId, &refFile) == 2;
140 * Get reference file name extension
142 static std::string getRefFileExt() { return "dbref"; }
144 /// Get Base Index
145 uint32 getBaseIndex() const { return _Header.BaseIndex; }
147 /// Get Base Index
148 uint32 getEndIndex() const { return _Header.EndIndex; }
150 /// Get Timestamp
151 uint32 getTimestamp() const { return _Header.Timestamp; }
153 /// Set Delta Ids
154 void setDeltaIds(uint32 startId, uint32 endId) { _Header.StartDeltaId = startId; _Header.EndDeltaId = endId; }
156 /// Get Delta Ids
157 void getDeltaIds(uint32& startId, uint32& endId) { startId = _Header.StartDeltaId; endId = _Header.EndDeltaId; }
159 private:
161 class CRefHeader
163 public:
165 CRefHeader()
167 BaseIndex = 0;
168 RowSize = 0;
169 FullRowSize = 0;
170 Timestamp = 0;
172 StartDeltaId = 0;
173 EndDeltaId = 0;
176 /// Base Index
177 uint32 BaseIndex;
179 /// End Index, last index in file plus 1
180 uint32 EndIndex;
182 /// Over Index, last index file can contain plus 1, base index in next reference file
183 uint32 OverIndex;
185 /// Row size, only declared size
186 uint32 RowSize;
188 /// Row Size, in byte (all headers included)
189 uint32 FullRowSize;
191 /// File timestamp
192 uint32 Timestamp;
194 /// Start Delta Id, used to check delta concatenation
195 uint32 StartDeltaId;
197 /// End Delta Id, used to check delta concatenation
198 uint32 EndDeltaId;
200 void serial(NLMISC::IStream& s)
202 s.serialCheck(NELID("RHdr"));
203 uint version = s.serialVersion(0);
205 s.serial(BaseIndex);
206 s.serial(EndIndex);
207 s.serial(OverIndex);
208 s.serial(RowSize);
209 s.serial(FullRowSize);
210 s.serial(Timestamp);
212 s.serial(StartDeltaId);
213 s.serial(EndDeltaId);
217 /// Initialised
218 bool _Init;
220 /// File base name
221 std::string _Name;
223 /// File path
224 std::string _Path;
226 /// Header of the reference
227 CRefHeader _Header;
229 enum TMode
231 Update,
232 Read
235 /// File Mode
236 TMode _Mode;
238 /// Data start position
239 uint32 _DataStart;
241 /// Serial file header
242 bool serialHeader();
244 uint32 getSeekPos(uint32 index) { return _DataStart + _Header.FullRowSize*(index-_Header.BaseIndex); }
250 #endif // NL_DB_REFERENCE_FILE_H
252 /* End of db_reference_file.h */