more work on database, initial log reader/writer finished.
This commit is contained in:
parent
79dfa099a0
commit
4dbb1883b8
110
database/log.cpp
110
database/log.cpp
|
@ -1,39 +1,99 @@
|
|||
#include <iostream>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include <cstdint>
|
||||
|
||||
#include <boost
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <cassert>
|
||||
#include <stdint.h>
|
||||
|
||||
enum E_OBJ_TYPE { ET_TRANS, ET_HASH, ET_SALE };
|
||||
|
||||
struct LogEntry {
|
||||
struct __attribute__ ((aligned (64), packed)) LogEntry
|
||||
{
|
||||
uint64_t ts;
|
||||
short type;
|
||||
bool revert;
|
||||
E_OBJ_TYPE type;
|
||||
union {
|
||||
struct __attribute__ ((packed)) {
|
||||
char hash[16];
|
||||
int32_t delta;
|
||||
} Transaction;
|
||||
|
||||
struct __attribute__ ((packed)) {
|
||||
char hash[16];
|
||||
bool add;
|
||||
} HashChange;
|
||||
|
||||
struct __attribute__ ((packed)) {
|
||||
char hash[16];
|
||||
uint64_t upc[2];
|
||||
int32_t delta;
|
||||
} StockChange;
|
||||
};
|
||||
};
|
||||
|
||||
struct Transaction : LogEntry {
|
||||
Transaction() : type(ET_TRANS) {};
|
||||
char hash[16];
|
||||
int32_t delta;
|
||||
class Log {
|
||||
public:
|
||||
Log(const char * fn)
|
||||
: log_name(fn)
|
||||
{
|
||||
FILE * fh = fopen(fn, "r");
|
||||
if(!fh) {
|
||||
fprintf(stderr, "Log: Could not open log file '%s' for reading!\n", fn);
|
||||
return;
|
||||
}
|
||||
|
||||
LogEntry l;
|
||||
while(1 == fread(&l, sizeof(LogEntry), 1, fh))
|
||||
entries.push_back(l);
|
||||
|
||||
fclose(fh);
|
||||
}
|
||||
|
||||
void writeEntry(LogEntry & ent)
|
||||
{
|
||||
FILE * fh = fopen(log_name.c_str(), "a");
|
||||
assert(fh);
|
||||
|
||||
flockfile(fh);
|
||||
|
||||
fseek(fh, 0, SEEK_END);
|
||||
assert(1 == fwrite(&ent, sizeof(LogEntry), 1, fh));
|
||||
fflush(fh);
|
||||
|
||||
funlockfile(fh);
|
||||
fclose(fh);
|
||||
|
||||
entries.push_back(ent);
|
||||
}
|
||||
|
||||
std::list<LogEntry>::const_iterator begin()
|
||||
{
|
||||
return entries.begin();
|
||||
}
|
||||
|
||||
std::list<LogEntry>::const_iterator end()
|
||||
{
|
||||
return entries.end();
|
||||
}
|
||||
private:
|
||||
std::list<LogEntry> entries;
|
||||
std::string log_name;
|
||||
};
|
||||
|
||||
struct HashChange : LogEntry {
|
||||
HashChange() : type(ET_HASH) {};
|
||||
char hash[16];
|
||||
bool add;
|
||||
};
|
||||
int main() {
|
||||
LogEntry l;
|
||||
memset(&l, 0, sizeof(LogEntry));
|
||||
l.ts = 0xAAAA;
|
||||
l.type = ET_TRANS;
|
||||
strcpy(l.Transaction.hash, "cocks");
|
||||
l.Transaction.delta = 0xdeadbeef;
|
||||
|
||||
struct Sale : LogEntry {
|
||||
Sale() :
|
||||
char hash[16];
|
||||
uint64_t upc[2];
|
||||
};
|
||||
Log cock("cock");
|
||||
cock.writeEntry(l);
|
||||
|
||||
struct LogFile {
|
||||
for(std::list<LogEntry>::const_iterator p = cock.begin(); p != cock.end(); ++p)
|
||||
printf("%s\n", p->Transaction.hash);
|
||||
|
||||
};
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue