From daace73c76bdd22e8e87408ba7e0eec7d6e19e0e Mon Sep 17 00:00:00 2001 From: deva Date: Fri, 24 Apr 2009 09:21:53 +0000 Subject: Delayed journal commit write. This makes sure each macro is only stored once. and also makes sure only one header (date, user, etc) is added pr. session. --- server/src/journal_commit.cc | 138 ++++++++++++++++++++++++++++++++++++++++++- server/src/journal_commit.h | 31 ++++++++-- server/src/server.cc | 20 +++++-- 3 files changed, 177 insertions(+), 12 deletions(-) (limited to 'server/src') diff --git a/server/src/journal_commit.cc b/server/src/journal_commit.cc index 57d8987..ce99b63 100644 --- a/server/src/journal_commit.cc +++ b/server/src/journal_commit.cc @@ -30,6 +30,8 @@ #include "debug.h" +#include + // for gethostbyname #include @@ -47,6 +49,55 @@ #include #include +/** + * Remove all spaces, tabs and newline trailing the string. + */ +static std::string stripTrailingWhitepace(std::string str) +{ + if(str == "") return str; + + ssize_t end = str.size() - 1; + + while(end && str[end] <= ' ') { // Every below SPACE 0x20 is consideret whitespace. + end--; + } + end++; + + return str.substr(0, end); +} + +/** + * Find all lines longer than 'width', and insert a newline in the + * first backward occurring space. + */ +static std::string addNewlines(std::string str, size_t width) +{ + std::string output; + + std::string fraction; + size_t linelen = 0; + for(size_t i = 0; i < str.size(); i++) { + + fraction += str[i]; + + if(str[i] <= ' ') { + if(linelen + fraction.size() > width) { + output[output.size() - 1] = '\n'; + linelen = 0; + } + output += fraction; + linelen += fraction.size(); + fraction = ""; + } + + if(str[i] == '\n') linelen = 0; + + } + output += fraction; + + return output; +} + static int mwrite(int sock, const char *fmt, ...) { int l = 0; @@ -113,7 +164,7 @@ int journal_commit(const char *cpr, const char *user, // send header mwrite(sock, "PUT JOURNAL PROTO1.0 \r\n"); mwrite(sock, "size : %i\r\n", size); - mwrite(sock, "user: %s(pracro)\r\n", user); + mwrite(sock, "user: %s\r\n", user); mwrite(sock, "generator: pracro\r\n"); // mwrite(sock, "password: 1234\r\n"); mwrite(sock, "cpr: %s\r\n", cpr); @@ -121,8 +172,10 @@ int journal_commit(const char *cpr, const char *user, mwrite(sock, "charset: utf8\r\n"); mwrite(sock, "\r\n"); + std::string resume = stripTrailingWhitepace(addNewlines(buf, 60)); + // send body - if(sock != -1 && write(sock, buf, size) != (ssize_t)size) { + if(sock != -1 && write(sock, resume.c_str(), resume.size()) != (ssize_t)resume.size()) { PRACRO_ERR_LOG(journal, "write did not write all the bytes in the buffer.\n"); } PRACRO_DEBUG(journal, "%s", buf); @@ -134,3 +187,84 @@ int journal_commit(const char *cpr, const char *user, return 0; } + +JournalWriter::JournalWriter(std::string host, unsigned short int port) +{ + this->host = host; + this->port = port; +} + +void JournalWriter::addEntry(Transaction &transaction, Commit &commit, std::string resume) +{ + // First run - initialize username and cpr. + if(currentuser == "" && entrylist.size() == 0) currentuser = transaction.user; + if(currentcpr == "" && entrylist.size() == 0) currentcpr = transaction.cpr; + + // Test if the username or the cpr has changed... if so, commit and clear the list. + if(currentuser != transaction.user || currentcpr != transaction.cpr) { + this->commit(); + entrylist.clear(); + } + + // Strip trailing whitespace, and add newlines. + std::string r = stripTrailingWhitepace(addNewlines(resume, 60)); + std::string m = commit.macro; + + // If macro already exists, overwrite with this entry, otherwise, just add it + int idx = -1; + std::vector< ResumeEntry >::iterator i = entrylist.begin(); + int cnt = 0; + while(i != entrylist.end()) { + if(i->macro == m) idx = cnt; + cnt++; + i++; + } + + if(idx != -1) { + entrylist[idx].resume = r; + entrylist[idx].macro = m; + } else { + ResumeEntry re; + re.resume = r; + re.macro = m; + entrylist.push_back(re); + } +} + +void JournalWriter::commit() +{ + std::string resume; + + // Iterate through all resumes, and create a string containing them all. + std::vector< ResumeEntry >::iterator i = entrylist.begin(); + while(i != entrylist.end()) { + if(resume != "") resume += "\n\n"; + // resume += i->macro + "\n"; + resume += i->resume; + i++; + } + + // Connect to praxisuploadserver and commit all resumes in one bulk. + journal_commit(currentcpr.c_str(), currentuser.c_str(), + host.c_str(), port, + resume.c_str(), resume.size()); +} + + +#ifdef TEST_JOURNAL_COMMIT + +int main() +{ + std::string text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do\neiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. \n\n \t"; + + std::string resume = stripTrailingWhitepace(addNewlines(text, 60)); + printf("[%s]\n", resume.c_str()); + + resume = stripTrailingWhitepace(addNewlines("", 60)); + printf("[%s]\n", resume.c_str()); + + return 0; +} + +#endif/*TEST_JOURNAL_COMMIT*/ + diff --git a/server/src/journal_commit.h b/server/src/journal_commit.h index 7462f39..387eab6 100644 --- a/server/src/journal_commit.h +++ b/server/src/journal_commit.h @@ -27,10 +27,33 @@ #ifndef __PRACRO_JOURNAL_COMMIT_H__ #define __PRACRO_JOURNAL_COMMIT_H__ -#include +#include +#include -int journal_commit(const char *cpr, const char *user, - const char *ip, unsigned short int port, - const char *buf, size_t size); +#include "transaction.h" + +class ResumeEntry { +public: + std::string resume; + std::string macro; +}; + +class JournalWriter { +public: + JournalWriter(std::string host, unsigned short int port); + + void addEntry(Transaction &transaction, Commit &commit, std::string resume); + + void commit(); + +private: + std::string host; + unsigned short int port; + + std::string currentuser; + std::string currentcpr; + + std::vector< ResumeEntry > entrylist; +}; #endif/*__PRACRO_JOURNAL_COMMIT_H__*/ diff --git a/server/src/server.cc b/server/src/server.cc index 24d1eb1..4a17954 100644 --- a/server/src/server.cc +++ b/server/src/server.cc @@ -78,7 +78,8 @@ public: static std::string handleTransaction(Transaction *transaction, TCPSocket *pentominos_socket, - Database *db) + Database *db, + JournalWriter &journalwriter) { std::string answer; answer += "\n"; @@ -98,7 +99,7 @@ static std::string handleTransaction(Transaction *transaction, Macro *macro = mp.getMacro(); std::string resume = resume_parser(macro->resume, commit); - commit.fields["journal.resume"] = resume; + commit.fields["journal.resume"] = resume; db->commitTransaction(transaction->user, transaction->cpr, *macro, commit.fields); @@ -108,9 +109,12 @@ static std::string handleTransaction(Transaction *transaction, // macro->resume.attributes["store_in_journal"] == "true"; if(resume != "" && store_in_journal) { + journalwriter.addEntry(*transaction, commit, resume); + /* journal_commit(transaction->cpr.c_str(), transaction->user.c_str(), Conf::journal_commit_addr.c_str(), Conf::journal_commit_port, resume.c_str(), resume.length()); + */ } i++; @@ -170,7 +174,8 @@ static std::string handleTransaction(Transaction *transaction, } if(macro.attributes["name"] == request.macro || - (macro.attributes.find("static") != macro.attributes.end() && macro.attributes["static"] == "true") + (macro.attributes.find("static") != macro.attributes.end() && + macro.attributes["static"] == "true") ) { foundmacro = true; @@ -274,6 +279,8 @@ static void handleConnection(TCPSocket *socket) Database *db = new Database("pgsql", Conf::database_addr, "", Conf::database_user, Conf::database_passwd, ""); + JournalWriter journalwriter(Conf::journal_commit_addr.c_str(), Conf::journal_commit_port); + ssize_t size; char buf[4096]; @@ -299,7 +306,7 @@ static void handleConnection(TCPSocket *socket) if(parser->parse(buf, size)) { PRACRO_DEBUG(server, "Got complete XML document %d bytes used, %d bytes in current buffer.\n", parser->usedBytes(), size); - socket->write(handleTransaction(transaction, &pentominos_socket, db)); + socket->write(handleTransaction(transaction, &pentominos_socket, db, journalwriter)); size = size - parser->usedBytes(); delete transaction; transaction = NULL; @@ -325,6 +332,8 @@ static void handleConnection(TCPSocket *socket) parser = NULL; } + journalwriter.commit(); + PRACRO_DEBUG(server, "Out of read loop!\n"); } @@ -340,8 +349,7 @@ void server() socket = new TCPSocket("Listen socket"); socket->listen(port); } catch (Exception &e) { - PRACRO_ERR_LOG(server, "Error during parsing:\n%s\n", - e.what()); + PRACRO_ERR_LOG(server, "Error in listen:\n%s\n", e.what()); delete socket; socket = NULL; return; -- cgit v1.2.3