统一上报和存储架构

This commit is contained in:
大石头 2017-04-15 14:30:48 +08:00
parent ef3bf65a5f
commit f1f5ef36b2
2 changed files with 42 additions and 53 deletions

View File

@ -2,8 +2,8 @@
#include "HistoryStore.h" #include "HistoryStore.h"
//#define DS_DEBUG DEBUG #define DS_DEBUG DEBUG
#define DS_DEBUG 0 //#define DS_DEBUG 0
#if DS_DEBUG #if DS_DEBUG
#define ds_printf debug_printf #define ds_printf debug_printf
#else #else
@ -17,13 +17,13 @@ HistoryStore::HistoryStore()
RenderPeriod = 30; RenderPeriod = 30;
ReportPeriod = 300; ReportPeriod = 300;
FlashPeriod = 600; StorePeriod = 600;
MaxCache = 16 * 1024; MaxCache = 16 * 1024;
MaxReport = 1024; MaxReport = 1024;
OnReport = nullptr; OnReport = nullptr;
OnFlash = nullptr; OnStore = nullptr;
_task = 0; _task = 0;
} }
@ -44,7 +44,7 @@ bool HistoryStore::Open()
if (Opened) return true; if (Opened) return true;
_Report = 0; _Report = 0;
_Flash = 0; _Store = 0;
// 定时生成历史数据 30s // 定时生成历史数据 30s
int p = RenderPeriod * 1000; int p = RenderPeriod * 1000;
@ -93,10 +93,10 @@ void HistoryStore::Reader()
_Report -= RenderPeriod; _Report -= RenderPeriod;
if (_Report <= 0) Report(); if (_Report <= 0) Report();
// 自动写入Flash // 自动写入Store
if (_Flash <= 0) _Flash = FlashPeriod; if (_Store <= 0) _Store = StorePeriod;
_Flash -= RenderPeriod; _Store -= RenderPeriod;
if (_Flash <= 0) Flash(); if (_Store <= 0) Store();
} }
void HistoryStore::Report() void HistoryStore::Report()
@ -104,63 +104,50 @@ void HistoryStore::Report()
if (!OnReport) return; if (!OnReport) return;
auto& s = Cache; auto& s = Cache;
int len = s.Length - s.Position();
// 没有数据 // 没有数据
if (s.Position() == s.Length) return; if (!len) return;
// 根据每次最大上报长度,计算可以上报多少条历史记录 // 根据每次最大上报长度,计算可以上报多少条历史记录
int n = MaxReport / Size; int n = MaxReport / Size;
int len = n * Size; int len2 = n * Size;
if (len2 > len) len2 = len;
ByteArray bs(len); ds_printf("HistoryStore::Report %d/%d", len2, len);
len = s.Read(bs);
bs.SetLength(len);
// 游标先移回去,成功以后再删除 Process(len2, OnReport);
s.Seek(-len);
if (len) {
int len2 = OnReport(this, bs, nullptr);
if (len2) {
s.Seek(len2);
// 如果缓存里面没有了数据,则从头开始使用
if (s.Position() == s.Length) {
s.SetPosition(0);
s.Length = 0;
}
}
}
} }
void HistoryStore::Flash() void HistoryStore::Store()
{ {
if (!OnFlash) return; if (!OnStore) return;
auto& s = Cache; auto& s = Cache;
int len = s.Length - s.Position();
// 没有数据 // 没有数据
if (s.Position() == s.Length) return; if (!len) return;
// 根据每次最大上报长度,计算可以上报多少条历史记录 ds_printf("HistoryStore::Store %d", len);
int n = MaxReport / Size;
int len = n * Size;
ByteArray bs(len); Process(len, OnStore);
len = s.Read(bs); }
bs.SetLength(len);
// 游标先移回去,成功以后再删除 void HistoryStore::Process(int len, DataHandler handler)
s.Seek(-len); {
if (!len || !handler) return;
if (len) { auto& s = Cache;
int len2 = OnFlash(this, bs, nullptr);
if (len2) {
s.Seek(len2);
// 如果缓存里面没有了数据,则从头开始使用 Buffer bs(s.GetBuffer() + s.Position(), len);
if (s.Position() == s.Length) {
s.SetPosition(0); int rs = handler(this, bs, nullptr);
s.Length = 0; if (rs) {
} s.Seek(rs);
// 如果缓存里面没有了数据,则从头开始使用
if (s.Position() == s.Length) {
s.SetPosition(0);
s.Length = 0;
} }
} }
} }

View File

@ -10,7 +10,7 @@ public:
MemoryStream Cache; // 数据 MemoryStream Cache; // 数据
short RenderPeriod; // 生成历史数据周期。默认30s short RenderPeriod; // 生成历史数据周期。默认30s
short ReportPeriod; // 上报数据周期。默认300s short ReportPeriod; // 上报数据周期。默认300s
short FlashPeriod; // 写入Flash周期。默认600s short StorePeriod; // 写入Store周期。默认600s
short MaxCache; // 缓存最大长度。默认16 * 1024 short MaxCache; // 缓存最大长度。默认16 * 1024
short MaxReport; // 每次最大上报长度。默认1024 short MaxReport; // 每次最大上报长度。默认1024
@ -20,7 +20,7 @@ public:
// 数据上报句柄 // 数据上报句柄
DataHandler OnReport; DataHandler OnReport;
// 数据存储句柄 // 数据存储句柄
DataHandler OnFlash; DataHandler OnStore;
// 初始化 // 初始化
HistoryStore(); HistoryStore();
@ -39,14 +39,16 @@ private:
int Size; int Size;
short _Report; short _Report;
short _Flash; short _Store;
uint _task; uint _task;
static void RenderTask(void* param); static void RenderTask(void* param);
void Reader(); void Reader();
void Report(); void Report();
void Flash(); void Store();
void Process(int len, DataHandler handler);
}; };
/* /*