diff --git a/.gitignore b/.gitignore index 092d5ca..5f4ed6d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ a.out db +*.swp diff --git a/database/common.h b/database/common.h new file mode 100644 index 0000000..b08246a --- /dev/null +++ b/database/common.h @@ -0,0 +1,8 @@ +#ifndef _COMMON_H_ +#define _COMMON_H_ +#include +#include "nameserver.h" + +extern NameServer ns; + +#endif diff --git a/database/db.cpp b/database/db.cpp index 63664a4..5793c63 100644 --- a/database/db.cpp +++ b/database/db.cpp @@ -1,7 +1,7 @@ +#include "common.h" #include "db.h" #include "log.h" #include "sha1.h" -#include "nameserver.h" #include #include #include @@ -292,3 +292,6 @@ uint64_t PosDb::doStockChange(UPC upc, int32_t delta) { return log.writeEntry(l); } +std::vector PosDb::toString() { + return log.toString(); +} diff --git a/database/db.h b/database/db.h index f836276..beb4f65 100644 --- a/database/db.h +++ b/database/db.h @@ -1,9 +1,8 @@ #ifndef _POS_DB_H_ #define _POS_DB_H_ +#include "common.h" #include "sha1.h" #include "log.h" -#include "nameserver.h" -#include #include #include #include @@ -34,6 +33,8 @@ public: int32_t getStock(UPC upc); uint64_t doStockChange(UPC upc, int32_t delta); + std::vector toString(); + private: void acceptLogEntry(const LogEntry & l); @@ -46,7 +47,6 @@ private: std::map serial_to_object; Log log; - NameServer ns; }; typedef std::multimap::iterator ath_it; diff --git a/database/log.cpp b/database/log.cpp index 5a96c41..5fd1ac7 100644 --- a/database/log.cpp +++ b/database/log.cpp @@ -3,6 +3,7 @@ #include #include #include +#include Log::Log(const char * fn) : log_name(fn), serial(0) @@ -62,3 +63,63 @@ uint64_t Log::nextSerial() return serial + 1; } +std::string logTypeToString (E_OBJ_TYPE type) { + switch(type) { + case ET_TRANS: return "TRANS"; + case ET_HASH: return "HASH"; + case ET_SALE: return "SALE"; + case ET_PRICE: return "PRICE"; + case ET_REVERT: return "REVERT"; + default: fprintf(stderr, "logTypeToString: unhandled case in switch.\n"); abort(); + } +} + +template +std::string ThingToString (T num) { + std::stringstream ss; + ss << num; + return ss.str(); +} + +std::vector Log::toString() +{ + assert(sizeof(time_t) == sizeof(uint64_t)); + std::vector ans; + ans.reserve(entries.size()); + std::list::iterator it; + for(it = entries.begin(); it != entries.end(); it++) { + char* time = new char[64]; + struct tm* tms = localtime((time_t*)&it->ts); + strftime(time, 64, "%c", tms); + std::string out = (std::string)time + "\t" + + ThingToString (it->serial) + "\t" + + logTypeToString(it->type) + "\t"; + delete[] time; + switch(it->type) { + case ET_TRANS: + out += ns.get_name(it->Transaction.uid) + "\t" + + ThingToString(it->Transaction.delta); + break; + case ET_HASH: + out += "hash goes here\t" + + ns.get_name(it->HashChange.uid) + "\t" + + ThingToString(it->HashChange.add); + break; + case ET_SALE: + out += "upc goes here\t" + + ThingToString(it->StockChange.delta); + break; + case ET_PRICE: + out += "upc goes here\t" + + ThingToString(it->PriceChange.price); + break; + case ET_REVERT: + out += ThingToString(it->Revert.revert_serial); + break; + default: fprintf(stderr, "Log: unhandled toString case.\n"); abort(); + } + + ans.push_back(out); + } + return ans; +} diff --git a/database/log.h b/database/log.h index 66a907d..96bdbca 100644 --- a/database/log.h +++ b/database/log.h @@ -1,9 +1,10 @@ #ifndef _POS_LOG_H_ #define _POS_LOG_H_ +#include "common.h" #include "sha1.h" #include #include -#include +#include enum E_OBJ_TYPE { ET_TRANS, ET_HASH, ET_SALE, ET_PRICE, ET_REVERT }; @@ -59,6 +60,9 @@ public: Log(const char * fn); uint64_t writeEntry(LogEntry ent); uint64_t nextSerial(); + + std::vector toString(); + std::list::const_iterator begin(); std::list::const_iterator end(); diff --git a/database/nameserver.cpp b/database/nameserver.cpp index ff9e89e..434dbb9 100644 --- a/database/nameserver.cpp +++ b/database/nameserver.cpp @@ -4,21 +4,26 @@ std::string NameServer::get_name (uint32_t id) { std::map::iterator it; - for (it = cache.begin(); it != cache.end(); it++) { - if (it->second == id) return it->first; + for(it = cache.begin(); it != cache.end(); it++) { + if(it->second == id) return it->first; } - return ""; + struct passwd* pwd = getpwuid(id); + if (!pwd) { + return "???"; + } + cache[pwd->pw_name] = id; + return pwd->pw_name; } uint32_t NameServer::get_id (std::string name) { std::map::iterator it = cache.find(name); - if (it != cache.end()) + if(it != cache.end()) return it->second; std::cerr << "Doing LDAP lookup for \"" << name << "\".\n"; struct passwd* pwd = getpwnam(name.c_str()); - if (!pwd) { + if(!pwd) { // TODO: oh fuck??? return 0; } @@ -27,3 +32,4 @@ uint32_t NameServer::get_id (std::string name) { return cache[name]; } +NameServer ns; diff --git a/database/tests.cpp b/database/tests.cpp index 13d4f5e..7dbb7f6 100644 --- a/database/tests.cpp +++ b/database/tests.cpp @@ -1,3 +1,4 @@ +#include "common.h" #include "db.h" #include @@ -32,6 +33,11 @@ int main() { foo.l = 123; cout << db.getStock(foo) << "\n"; + cout << "\n\n\n"; + std::vector lst = db.toString(); + for (std::vector::iterator it = lst.begin(); it != lst.end(); it++) { + cout << *it << "\n"; + } return 0; }