-
Notifications
You must be signed in to change notification settings - Fork 9
/
Index.cpp
131 lines (101 loc) · 3.8 KB
/
Index.cpp
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
#include "Index.hpp"
#include "LogFile.hpp"
#include "IndexFile.hpp"
#include <base-logging/Logging.hpp>
#include <stdint.h>
#include <unistd.h>
#include <iostream>
#include <stdexcept>
namespace pocolog_cpp
{
Index::Index(std::string indexFileName, size_t streamIdx): firstAdd(true), curSampleNr(-1), indexFile(indexFileName.c_str(), std::ifstream::binary | std::ifstream::in)
{
indexFile.seekg(streamIdx * sizeof(IndexPrologue) + sizeof(IndexFile::IndexFileHeader));
indexFile.read((char *) & prologue, sizeof(IndexPrologue));
firstSampleTime = base::Time::fromMicroseconds(prologue.firstSampleTime);
lastSampleTime = base::Time::fromMicroseconds(prologue.lastSampleTime);
}
Index::Index(const pocolog_cpp::StreamDescription& desc, off_t posOfStreamDesc) : firstAdd(true), curSampleNr(-1)
{
prologue.streamIdx = desc.getIndex();
prologue.nameCrc = 0;
prologue.numSamples = 0;
prologue.streamDescPos = posOfStreamDesc;
prologue.firstSampleTime = 0;
prologue.lastSampleTime = 0;
name = desc.getName();
}
bool Index::matches(const StreamDescription& odesc) const
{
return static_cast<size_t>(prologue.streamIdx) == odesc.getIndex();
}
void Index::addSample(off_t filePosition, const base::Time& sampleTime)
{
if(firstAdd)
{
prologue.firstSampleTime = sampleTime.microseconds;
firstAdd = false;
}
IndexInfo info;
info.samplePosInLogFile = filePosition;
info.sampleTime = sampleTime.microseconds;
buildBuffer.push_back(info);
prologue.lastSampleTime = sampleTime.microseconds;
prologue.numSamples++;
}
off_t Index::getPrologueSize()
{
return sizeof(IndexPrologue);
}
off_t Index::writeIndexToFile(std::fstream& indexFile, off_t prologPos, off_t indexDataPos)
{
indexFile.seekp(prologPos, std::fstream::beg);
if(!indexFile.good())
throw std::runtime_error("Error writing index file");
prologue.dataPos = indexDataPos;
LOG_DEBUG_S << "Found " << prologue.numSamples << " in stream " << name;
indexFile.write((char *) &prologue, sizeof(IndexPrologue));
if(!indexFile.good())
throw std::runtime_error("Error writing index file");
indexFile.seekp(indexDataPos, std::fstream::beg);
if(!indexFile.good())
throw std::runtime_error("Error writing index file");
LOG_DEBUG_S << "Wrinting " << buildBuffer.size() * sizeof(IndexInfo) / 1024 << " KBytes to index File ";
indexFile.write((char *) buildBuffer.data(), buildBuffer.size() * sizeof(IndexInfo));
if(!indexFile.good())
throw std::runtime_error("Error writing index file");
LOG_DEBUG_S << "Done new pos " << indexFile.tellp();
return indexFile.tellp();
}
void Index::loadIndex(size_t sampleNr)
{
if(sampleNr >= prologue.numSamples)
throw std::runtime_error("Index::loadIndex : Error sample out of index requested");
if(sampleNr != curSampleNr)
{
std::streampos pos(prologue.dataPos + sampleNr * sizeof(IndexInfo));
LOG_DEBUG_S << "Seeking to " << pos << " start of index Data " << prologue.dataPos << " pos in data " << sampleNr * sizeof(IndexInfo);
indexFile.seekg(std::streampos(prologue.dataPos + sampleNr * sizeof(IndexInfo)));
if(!indexFile.good())
throw std::runtime_error("Internal Error, index file is corrupted");
indexFile.read((char *) &curIndexInfo, sizeof(IndexInfo));
if(!indexFile.good())
throw std::runtime_error("Internal Error, index file is corrupted");
curSampleNr = sampleNr;
}
}
std::streampos Index::getSamplePos(size_t sampleNr)
{
loadIndex(sampleNr);
return std::streampos(curIndexInfo.samplePosInLogFile);
}
base::Time Index::getSampleTime(size_t sampleNr)
{
loadIndex(sampleNr);
return base::Time::fromMicroseconds(curIndexInfo.sampleTime);
}
Index::~Index()
{
indexFile.close();
}
}