Split log class into header and impl.

This commit is contained in:
Marc Burns 2011-11-08 13:38:52 -05:00
parent 4dbb1883b8
commit 9d9525f957
2 changed files with 90 additions and 86 deletions

View File

@ -1,99 +1,52 @@
#include <list>
#include <string>
#include "log.h"
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <stdint.h>
enum E_OBJ_TYPE { ET_TRANS, ET_HASH, ET_SALE };
struct __attribute__ ((aligned (64), packed)) LogEntry
Log::Log(const char * fn)
: log_name(fn), serial(0)
{
uint64_t ts;
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;
};
};
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);
FILE * fh = fopen(fn, "r");
if(!fh) {
fprintf(stderr, "Log: Could not open log file '%s' for reading!\n", fn);
return;
}
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;
};
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;
while(1 == fread(&l, sizeof(LogEntry), 1, fh))
entries.push_back(l);
Log cock("cock");
cock.writeEntry(l);
serial = l.serial;
for(std::list<LogEntry>::const_iterator p = cock.begin(); p != cock.end(); ++p)
printf("%s\n", p->Transaction.hash);
return 0;
fclose(fh);
}
void Log::writeEntry(LogEntry ent)
{
FILE * fh = fopen(log_name.c_str(), "a");
assert(fh);
flockfile(fh);
fseek(fh, 0, SEEK_END);
ent.serial = ++serial;
assert(1 == fwrite(&ent, sizeof(LogEntry), 1, fh));
fflush(fh);
funlockfile(fh);
fclose(fh);
entries.push_back(ent);
}
std::list<LogEntry>::const_iterator Log::begin()
{
return entries.begin();
}
std::list<LogEntry>::const_iterator Log::end()
{
return entries.end();
}

51
database/log.h Normal file
View File

@ -0,0 +1,51 @@
#ifndef _POS_LOG_H_
#define _POS_LOG_H_
#include <list>
#include <string>
#include <stdint.h>
enum E_OBJ_TYPE { ET_TRANS, ET_HASH, ET_SALE, ET_REVERT };
struct __attribute__ ((aligned (64), packed)) LogEntry
{
uint64_t ts;
uint64_t serial;
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 __attribute__ ((packed)) {
uint64_t revert_serial;
} Revert;
};
};
class Log {
public:
Log(const char * fn);
void writeEntry(LogEntry ent);
std::list<LogEntry>::const_iterator begin();
std::list<LogEntry>::const_iterator end();
private:
std::list<LogEntry> entries;
std::string log_name;
uint64_t serial;
};
#endif