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