librcsb-core-wrapper 1.005
Serializer.h
Go to the documentation of this file.
1//$$FILE$$
2//$$VERSION$$
3//$$DATE$$
4//$$LICENSE$$
5
6
7#ifndef SERIALIZER_H
8#define SERIALIZER_H
9
10
11#include <string>
12#include <vector>
13#include <fstream>
14
15#include <rcsb/rcsb_types.h>
16#include <rcsb/BlockIO.h>
17
18
19const int NO_TYPE = 0; // This is reserved
20const unsigned int STRINGS_TYPE = 1;
21const unsigned int STRING_TYPE = 2;
22const int INT_TYPE = 3;
23const int LONG_TYPE = 4;
24const int FLOAT_TYPE = 5;
25const int DOUBLE_TYPE = 6;
26const unsigned int WORD_TYPE = 7;
27const unsigned int WORDS_TYPE = 8;
28const unsigned int UWORD_TYPE = 9;
29const unsigned int UWORDS_TYPE = 10;
30
31const int INDEX_INCREMENT = 1024;
32
41
42
44{
45 public:
46 // Constructors and destructor
47 Serializer(const std::string& fileName, const eFileMode fileMode,
48 const bool verbose = false);
50
51 inline unsigned int GetNumDataIndices();
52
53 // Read methods
54 UInt32 ReadUInt32(const UInt32 index);
55 void ReadUInt32s(std::vector<UInt32>& UInt32s, const UInt32 index);
56 void ReadString(std::string& retString, const UInt32 index);
57 void ReadStrings(std::vector<std::string>& theStrings, const UInt32 index);
58
59 // Write methods
60 UInt32 WriteUInt32(const UInt32 theWord);
61 UInt32 WriteUInt32s(const std::vector<UInt32>& theWords);
62 UInt32 WriteString(const std::string& theString);
63 UInt32 WriteStrings(const std::vector<std::string>& theStrings);
64
65 // Update methods
66 UInt32 UpdateUInt32(const UInt32 theWord, const UInt32 oldIndex);
67 UInt32 UpdateUInt32s(const std::vector<UInt32>& theWords,
68 const UInt32 oldIndex);
69 UInt32 UpdateString(const std::string& theString, const UInt32 oldIndex);
70 UInt32 UpdateStrings(const std::vector<std::string>& theStrings,
71 const UInt32 oldIndex);
72
73 private:
74 typedef struct
75 {
76 // Block number of the start of the indices info
77 UInt32 fileIndexBlock;
78
79 // Number of blocks that hold the indices
80 UInt32 fileIndexNumBlocks;
81
82 // Total size in bytes of all indices
83 UInt32 fileIndexLength;
84
85 // Number of indices
86 UInt32 numIndices;
87
88 // Reserved information
89 UInt32 reserved[3];
90
91 // File version
92 UInt32 version;
93 } tFileHeader;
94
95 // Represents an entry index. Entry is a value of some type that has
96 // been stored in to the file. Index shows entry location (in which
97 // block and offset from the start of the block), its length, dataType.
98 typedef struct
99 {
100 UInt32 blockNumber; // Block in which data is located
101 UInt32 offset; // Data offset in the block
102 UInt32 length; // The length of the data
103 UInt32 dataType; // Type of data
104 UInt32 vLength; // Virtual length (length adjusted for word size)
105 UInt32 reserved[3];
106 } EntryIndex;
107
108 static bool _littleEndian;
109
110 static const UInt32 _version = 1;
111 static const UInt32 _indicesPerBlock = BLKSIZE / sizeof(EntryIndex);
112
113 // An array of index entries (i.e., these are indices)
114 std::string _fileName;
115
116 // Stored in block 0, this holds the info about the index
117 tFileHeader _fileHeader;
118
119 std::vector<EntryIndex> _indices;
120
121 std::ofstream _log;
122
123 bool _verbose;
124
125 UInt32 _currentBlock; // The current block number of the current buffer
126 UInt32 _currentOffset; // The offset into the current buffer
127
128 char* _buffer;
129
130 eFileMode _mode;
131
132 BlockIO _theBlock; // A block for doing read/write a block at a time
133
134 void Init();
135
136 void WriteUInt32AtIndex(const UInt32 theWord, const UInt32 index);
137 void WriteUInt32sAtIndex(const std::vector<UInt32>& Words,
138 const UInt32 index);
139 void WriteStringAtIndex(const std::string& theString, const UInt32 index);
140 void WriteStringsAtIndex(const std::vector<std::string>& theStrings,
141 const UInt32 index);
142
143 void Delete(const UInt32 index);
144
145 void GetLastDataBuffer(void);
146 void GetDataBufferAtIndex(const UInt32 index);
147
148 void _GetHeader(const char* where);
149 void _PutHeader(char* where);
150
151 void _GetIndex(EntryIndex& outIndex, const char* where);
152 void _PutIndex(const EntryIndex& inIndex, char* where);
153
154 UInt32 _GetUInt32(const char* where);
155 void _PutUInt32(const UInt32 inWord, char* where);
156
157 void SwapHeader(tFileHeader& out, const tFileHeader& in);
158 void SwapIndex(EntryIndex& out, const EntryIndex& in);
159 UInt32 SwapUInt32(const UInt32 theWord);
160
161 void _ReadFileHeader();
162 void _WriteFileHeader();
163
164 void AllocateIndices(const UInt32 index);
165
166 UInt32 ReadBlock(const UInt32 blockNum);
167 UInt32 WriteBlock(const UInt32 blockNum);
168
169 char* GetWritingPoint(const UInt32 index);
170 void WriteLast(const char* const where);
171
172 void SetVirtualLength(const UInt32 index);
173
174 int _fd; // The file descriptor of the file that is opened, -1 if unopened
175
176 UInt32 _numBlocksIO; // The number of blocks in the currently opened file
177 UInt32 _currentBlockIO; // The block that is currently read into buffer
178
179 void OpenFileIO(const std::string& filename, const eFileMode fileMode);
180 void CloseFileIO();
181
182 inline UInt32 GetCurrentBlockNumberIO() const;
183 inline UInt32 GetNumBlocksIO() const;
184
185 void PrintIndex();
186 void PrintIndexPosition(const UInt32 position);
187 void DumpFile();
188 void PrintBuffer();
189};
190
191
193{
194 return (_indices.size());
195}
196
197
198inline UInt32 Serializer::GetCurrentBlockNumberIO() const
199{
200 return (_currentBlockIO);
201}
202
203
204inline UInt32 Serializer::GetNumBlocksIO() const
205{
206 return (_numBlocksIO);
207}
208
209#endif
210
const unsigned int BLKSIZE
Definition BlockIO.h:13
const unsigned int WORD_TYPE
Definition Serializer.h:26
const int INDEX_INCREMENT
Definition Serializer.h:31
const int FLOAT_TYPE
Definition Serializer.h:24
const unsigned int UWORD_TYPE
Definition Serializer.h:28
const unsigned int STRINGS_TYPE
Definition Serializer.h:20
const int INT_TYPE
Definition Serializer.h:22
const unsigned int UWORDS_TYPE
Definition Serializer.h:29
const int LONG_TYPE
Definition Serializer.h:23
const int NO_TYPE
Definition Serializer.h:19
const int DOUBLE_TYPE
Definition Serializer.h:25
const unsigned int STRING_TYPE
Definition Serializer.h:21
eFileMode
Definition Serializer.h:34
@ VIRTUAL_MODE
Definition Serializer.h:39
@ NO_MODE
Definition Serializer.h:35
@ CREATE_MODE
Definition Serializer.h:37
@ READ_MODE
Definition Serializer.h:36
@ UPDATE_MODE
Definition Serializer.h:38
const unsigned int WORDS_TYPE
Definition Serializer.h:27
Definition BlockIO.h:16
Definition Serializer.h:44
UInt32 UpdateStrings(const std::vector< std::string > &theStrings, const UInt32 oldIndex)
UInt32 ReadUInt32(const UInt32 index)
void ReadUInt32s(std::vector< UInt32 > &UInt32s, const UInt32 index)
UInt32 WriteUInt32(const UInt32 theWord)
UInt32 UpdateString(const std::string &theString, const UInt32 oldIndex)
UInt32 UpdateUInt32(const UInt32 theWord, const UInt32 oldIndex)
UInt32 UpdateUInt32s(const std::vector< UInt32 > &theWords, const UInt32 oldIndex)
Serializer(const std::string &fileName, const eFileMode fileMode, const bool verbose=false)
void ReadString(std::string &retString, const UInt32 index)
UInt32 WriteStrings(const std::vector< std::string > &theStrings)
void ReadStrings(std::vector< std::string > &theStrings, const UInt32 index)
UInt32 WriteString(const std::string &theString)
UInt32 WriteUInt32s(const std::vector< UInt32 > &theWords)
unsigned int GetNumDataIndices()
Definition Serializer.h:192
unsigned int UInt32
Definition rcsb_types.h:15