git-svn-id: svn://db.shs.com.ru/pip@810 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
@@ -443,7 +443,9 @@ PIBinaryLog::BinLogRecord PIBinaryLog::readRecord() {
|
||||
} else br.id = 0;
|
||||
lastrecord = br;
|
||||
if (br.id == 0) fileError();
|
||||
file_mutex.lock();
|
||||
moveIndex(index_pos.value(file.pos(), -1));
|
||||
file_mutex.unlock();
|
||||
logmutex.unlock();
|
||||
// piCoutObj << "readRecord done";
|
||||
return br;
|
||||
@@ -451,7 +453,7 @@ PIBinaryLog::BinLogRecord PIBinaryLog::readRecord() {
|
||||
|
||||
|
||||
|
||||
void PIBinaryLog::parseLog(PIFile * f, PIBinaryLog::BinLogInfo * info, PIVector<PIBinaryLog::BinLogIndex> * index) {
|
||||
void PIBinaryLog::parseLog(PIFile * f, PIBinaryLog::BinLogInfo * info, PIVector<PIBinaryLog::BinLogIndex> * index, PIMutex & file_mutex) {
|
||||
BinLogInfo * bi = info;
|
||||
bool ginfo = info != 0;
|
||||
bool gindex = index != 0;
|
||||
@@ -464,6 +466,7 @@ void PIBinaryLog::parseLog(PIFile * f, PIBinaryLog::BinLogInfo * info, PIVector<
|
||||
if (gindex) index->clear();
|
||||
if (f == 0) return;
|
||||
if (!f->canRead()) return;
|
||||
file_mutex.lock();
|
||||
if (ginfo) {
|
||||
bi->path = f->path();
|
||||
bi->log_size = f->size();
|
||||
@@ -476,6 +479,7 @@ void PIBinaryLog::parseLog(PIFile * f, PIBinaryLog::BinLogInfo * info, PIVector<
|
||||
if (read_sig[i] != __S__PIBinaryLog::binlog_sig[i]) {if (ginfo) bi->records_count = -2; ok = false;}
|
||||
uchar read_version = 0;
|
||||
if (f->read(&read_version, 1) < 0) {if (ginfo) bi->records_count = -3; ok = false;}
|
||||
file_mutex.unlock();
|
||||
if (read_version == 0) {if (ginfo) bi->records_count = -4; ok = false;}
|
||||
if (read_version < PIBINARYLOG_VERSION) {if (ginfo) bi->records_count = -5; ok = false;}
|
||||
if (read_version > PIBINARYLOG_VERSION) {if (ginfo) bi->records_count = -6; ok = false;}
|
||||
@@ -487,16 +491,24 @@ void PIBinaryLog::parseLog(PIFile * f, PIBinaryLog::BinLogInfo * info, PIVector<
|
||||
ba.resize(hdr_size);
|
||||
while (1) {
|
||||
ba.resize(hdr_size);
|
||||
if(f->read(ba.data(), ba.size_s()) > 0) {
|
||||
{
|
||||
PIMutexLocker _ml(file_mutex);
|
||||
if (f->read(ba.data(), ba.size_s()) > 0) {
|
||||
ba >> br.id >> br.size >> br.timestamp;
|
||||
} else break;
|
||||
if (f->size() - f->pos() >= br.size) f->seek(f->pos() + br.size);
|
||||
else break;
|
||||
} else
|
||||
break;
|
||||
if (f->size() - f->pos() >= br.size)
|
||||
f->seek(f->pos() + br.size);
|
||||
else
|
||||
break;
|
||||
}
|
||||
if (br.id > 0) {
|
||||
if (gindex) {
|
||||
BinLogIndex bl_ind;
|
||||
bl_ind.id = br.id;
|
||||
file_mutex.lock();
|
||||
bl_ind.pos = f->pos() - br.size - hdr_size;
|
||||
file_mutex.unlock();
|
||||
bl_ind.timestamp = br.timestamp;
|
||||
index->append(bl_ind);
|
||||
}
|
||||
@@ -538,7 +550,8 @@ PIBinaryLog::BinLogInfo PIBinaryLog::getLogInfo(const PIString & path) {
|
||||
bi.records_count = 0;
|
||||
PIFile tfile;
|
||||
if (!tfile.open(path, PIIODevice::ReadOnly)) return bi;
|
||||
parseLog(&tfile, &bi, 0);
|
||||
PIMutex _m;
|
||||
parseLog(&tfile, &bi, 0, _m);
|
||||
return bi;
|
||||
}
|
||||
|
||||
@@ -568,12 +581,16 @@ bool PIBinaryLog::cutBinLog(const PIBinaryLog::BinLogInfo & src, const PIString
|
||||
|
||||
|
||||
bool PIBinaryLog::createIndex() {
|
||||
file_mutex.lock();
|
||||
llong cp = file.pos();
|
||||
file.seekToBegin();
|
||||
file_mutex.unlock();
|
||||
index.clear();
|
||||
index_pos.clear();
|
||||
parseLog(&file, &binfo, &index);
|
||||
parseLog(&file, &binfo, &index, file_mutex);
|
||||
file_mutex.lock();
|
||||
file.seek(cp);
|
||||
file_mutex.unlock();
|
||||
is_indexed = !index.isEmpty();
|
||||
for (uint i=0; i<index.size(); i++) index_pos[index[i].pos] = i;
|
||||
return is_indexed;
|
||||
@@ -585,7 +602,9 @@ void PIBinaryLog::seekTo(int rindex) {
|
||||
logmutex.lock();
|
||||
if (rindex < index.size_s() && rindex >= 0) {
|
||||
file.seek(index[rindex].pos);
|
||||
file_mutex.lock();
|
||||
moveIndex(index_pos.value(file.pos(), -1));
|
||||
file_mutex.unlock();
|
||||
play_time = index[rindex].timestamp.toMilliseconds();
|
||||
lastrecord.timestamp = index[rindex].timestamp;
|
||||
}
|
||||
|
||||
@@ -187,7 +187,10 @@ public:
|
||||
int readBinLog(int id, void * read_to, int max_size, PISystemTime * time = 0);
|
||||
|
||||
//! Returns binary log file size
|
||||
llong logSize() const {return file.size();}
|
||||
llong logSize() const {PIMutexLocker _ml(file_mutex); return file.size();}
|
||||
|
||||
//! Return position in current binlog file
|
||||
llong logPos() const {PIMutexLocker _ml(file_mutex); return file.pos();}
|
||||
|
||||
//! Return true, if position at the end of BinLog file
|
||||
bool isEnd() const {if (isClosed()) return true; return file.isEnd();}
|
||||
@@ -228,6 +231,9 @@ public:
|
||||
//! Create index of current binlog file
|
||||
bool createIndex();
|
||||
|
||||
//! Return if current binlog file is indexed
|
||||
bool isIndexed() {return is_indexed;}
|
||||
|
||||
//! Go to record #index
|
||||
void seekTo(int rindex);
|
||||
|
||||
@@ -297,7 +303,7 @@ private:
|
||||
bool writeFileHeader();
|
||||
bool checkFileHeader();
|
||||
BinLogRecord readRecord();
|
||||
static void parseLog(PIFile *f, BinLogInfo *info, PIVector<BinLogIndex> * index);
|
||||
static void parseLog(PIFile *f, BinLogInfo *info, PIVector<BinLogIndex> * index, PIMutex & file_mutex);
|
||||
void moveIndex(int i);
|
||||
PIString getLogfilePath() const;
|
||||
|
||||
@@ -310,7 +316,7 @@ private:
|
||||
PIFile file;
|
||||
BinLogRecord lastrecord;
|
||||
PISystemTime startlogtime, play_delay, split_time, pause_time;
|
||||
PIMutex logmutex, pausemutex;
|
||||
mutable PIMutex logmutex, pausemutex, file_mutex;
|
||||
double play_time, play_speed;
|
||||
llong split_size;
|
||||
int write_count, split_count, default_id, current_index;
|
||||
|
||||
Reference in New Issue
Block a user