2014-08-06 19:59:02 +05:30
|
|
|
#include "caching/xml_format.h"
|
|
|
|
|
2013-10-10 15:04:37 +05:30
|
|
|
#include "base/base64.h"
|
2014-01-31 19:13:39 +05:30
|
|
|
#include "base/indented_stream.h"
|
2014-08-06 19:59:02 +05:30
|
|
|
#include "base/xml_utils.h"
|
2013-09-11 16:10:46 +05:30
|
|
|
|
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
#include <expat.h>
|
|
|
|
|
|
|
|
using namespace caching;
|
|
|
|
using namespace persistent_data;
|
|
|
|
using namespace std;
|
2014-08-06 19:59:02 +05:30
|
|
|
using namespace xml_utils;
|
2013-09-11 16:10:46 +05:30
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
//--------------------------------
|
|
|
|
// Emitter
|
|
|
|
//--------------------------------
|
|
|
|
class xml_emitter : public emitter {
|
|
|
|
public:
|
|
|
|
xml_emitter(ostream &out)
|
2014-01-31 19:13:39 +05:30
|
|
|
: out_(out) {
|
2013-09-11 16:10:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void begin_superblock(std::string const &uuid,
|
|
|
|
block_address block_size,
|
|
|
|
block_address nr_cache_blocks,
|
2013-09-26 16:06:01 +05:30
|
|
|
std::string const &policy,
|
|
|
|
size_t hint_width) {
|
2014-01-31 19:13:39 +05:30
|
|
|
out_.indent();
|
2013-09-11 16:10:46 +05:30
|
|
|
out_ << "<superblock uuid=\"" << uuid << "\""
|
|
|
|
<< " block_size=\"" << block_size << "\""
|
|
|
|
<< " nr_cache_blocks=\"" << nr_cache_blocks << "\""
|
2013-09-26 16:06:01 +05:30
|
|
|
<< " policy=\"" << policy << "\""
|
|
|
|
<< " hint_width=\"" << hint_width << "\">" << endl;
|
2014-01-31 19:13:39 +05:30
|
|
|
out_.inc();
|
2013-09-11 16:10:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
virtual void end_superblock() {
|
2014-01-31 19:13:39 +05:30
|
|
|
out_.dec();
|
|
|
|
out_.indent();
|
2013-09-11 16:10:46 +05:30
|
|
|
out_ << "</superblock>" << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void begin_mappings() {
|
2014-01-31 19:13:39 +05:30
|
|
|
out_.indent();
|
2013-09-11 16:10:46 +05:30
|
|
|
out_ << "<mappings>" << endl;
|
2014-01-31 19:13:39 +05:30
|
|
|
out_.inc();
|
2013-09-11 16:10:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
virtual void end_mappings() {
|
2014-01-31 19:13:39 +05:30
|
|
|
out_.dec();
|
|
|
|
out_.indent();
|
2013-09-11 16:10:46 +05:30
|
|
|
out_ << "</mappings>" << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void mapping(block_address cblock,
|
|
|
|
block_address oblock,
|
|
|
|
bool dirty) {
|
2014-01-31 19:13:39 +05:30
|
|
|
out_.indent();
|
2013-09-11 16:10:46 +05:30
|
|
|
out_ << "<mapping"
|
|
|
|
<< " cache_block=\"" << cblock << "\""
|
|
|
|
<< " origin_block=\"" << oblock << "\""
|
|
|
|
<< " dirty=\"" << as_truth(dirty) << "\""
|
|
|
|
<< "/>" << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void begin_hints() {
|
2014-01-31 19:13:39 +05:30
|
|
|
out_.indent();
|
2013-09-11 16:10:46 +05:30
|
|
|
out_ << "<hints>" << endl;
|
2014-01-31 19:13:39 +05:30
|
|
|
out_.inc();
|
2013-09-11 16:10:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
virtual void end_hints() {
|
2014-01-31 19:13:39 +05:30
|
|
|
out_.dec();
|
|
|
|
out_.indent();
|
2013-09-11 16:10:46 +05:30
|
|
|
out_ << "</hints>" << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void hint(block_address cblock,
|
2013-09-24 17:07:27 +05:30
|
|
|
vector<unsigned char> const &data) {
|
2013-10-10 15:04:37 +05:30
|
|
|
using namespace base;
|
|
|
|
|
2014-01-31 19:13:39 +05:30
|
|
|
out_.indent();
|
2013-09-11 16:10:46 +05:30
|
|
|
out_ << "<hint"
|
|
|
|
<< " cache_block=\"" << cblock << "\""
|
2013-10-10 15:04:37 +05:30
|
|
|
<< " data=\"" << base64_encode(data) << "\""
|
2013-10-10 16:08:28 +05:30
|
|
|
<< "/>" << endl;
|
2013-09-11 16:10:46 +05:30
|
|
|
}
|
|
|
|
|
2013-10-11 14:33:51 +05:30
|
|
|
virtual void begin_discards() {
|
2014-01-31 19:13:39 +05:30
|
|
|
out_.indent();
|
2013-10-11 14:33:51 +05:30
|
|
|
out_ << "<discards>" << endl;
|
2014-01-31 19:13:39 +05:30
|
|
|
out_.inc();
|
2013-10-11 14:33:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
virtual void end_discards() {
|
2014-01-31 19:13:39 +05:30
|
|
|
out_.dec();
|
|
|
|
out_.indent();
|
2013-10-11 14:33:51 +05:30
|
|
|
out_ << "</discards>" << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void discard(block_address dblock_b, block_address dblock_e) {
|
2014-01-31 19:13:39 +05:30
|
|
|
out_.indent();
|
2013-10-11 14:33:51 +05:30
|
|
|
out_ << "<discard dbegin=\"" << dblock_b << "\""
|
|
|
|
<< " dend=\"" << dblock_e << "\"/>" << endl;
|
|
|
|
}
|
|
|
|
|
2013-09-11 16:10:46 +05:30
|
|
|
private:
|
|
|
|
string as_truth(bool v) const {
|
|
|
|
return v ? "true" : "false";
|
|
|
|
}
|
|
|
|
|
2014-01-31 19:13:39 +05:30
|
|
|
indented_stream out_;
|
2013-09-11 16:10:46 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
//--------------------------------
|
|
|
|
// Parser
|
|
|
|
//--------------------------------
|
|
|
|
void parse_superblock(emitter *e, attributes const &attr) {
|
|
|
|
e->begin_superblock(get_attr<string>(attr, "uuid"),
|
|
|
|
get_attr<uint64_t>(attr, "block_size"),
|
|
|
|
get_attr<uint64_t>(attr, "nr_cache_blocks"),
|
2013-09-26 16:06:01 +05:30
|
|
|
get_attr<string>(attr, "policy"),
|
|
|
|
get_attr<size_t>(attr, "hint_width"));
|
2013-09-11 16:10:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
bool to_bool(string const &str) {
|
|
|
|
if (str == "true")
|
|
|
|
return true;
|
|
|
|
|
|
|
|
else if (str == "false")
|
|
|
|
return false;
|
|
|
|
|
|
|
|
throw runtime_error("bad boolean value");
|
|
|
|
}
|
|
|
|
|
|
|
|
void parse_mapping(emitter *e, attributes const &attr) {
|
|
|
|
e->mapping(get_attr<uint64_t>(attr, "cache_block"),
|
|
|
|
get_attr<uint64_t>(attr, "origin_block"),
|
|
|
|
to_bool(get_attr<string>(attr, "dirty")));
|
|
|
|
}
|
|
|
|
|
|
|
|
void parse_hint(emitter *e, attributes const &attr) {
|
2013-10-10 15:04:37 +05:30
|
|
|
using namespace base;
|
|
|
|
|
|
|
|
block_address cblock = get_attr<uint64_t>(attr, "cache_block");
|
|
|
|
decoded_or_error doe = base64_decode(get_attr<string>(attr, "data"));
|
2014-07-02 13:49:20 +05:30
|
|
|
if (!boost::get<vector<unsigned char> >(&doe)) {
|
2013-10-10 15:04:37 +05:30
|
|
|
ostringstream msg;
|
|
|
|
msg << "invalid base64 encoding of hint for cache block "
|
2014-07-02 13:49:20 +05:30
|
|
|
<< cblock << ": " << boost::get<string>(doe);
|
2013-10-10 15:04:37 +05:30
|
|
|
throw runtime_error(msg.str());
|
|
|
|
}
|
|
|
|
|
2014-07-02 13:49:20 +05:30
|
|
|
e->hint(cblock, boost::get<vector<unsigned char> >(doe));
|
2013-09-11 16:10:46 +05:30
|
|
|
}
|
|
|
|
|
2013-10-11 14:33:51 +05:30
|
|
|
// FIXME: why passing e by ptr?
|
|
|
|
void parse_discard(emitter *e, attributes const &attr) {
|
|
|
|
e->discard(get_attr<uint64_t>(attr, "dbegin"),
|
|
|
|
get_attr<uint64_t>(attr, "dend"));
|
|
|
|
}
|
|
|
|
|
2013-09-11 16:10:46 +05:30
|
|
|
void start_tag(void *data, char const *el, char const **attr) {
|
|
|
|
emitter *e = static_cast<emitter *>(data);
|
|
|
|
attributes a;
|
|
|
|
|
|
|
|
build_attributes(a, attr);
|
|
|
|
|
|
|
|
if (!strcmp(el, "superblock"))
|
|
|
|
parse_superblock(e, a);
|
|
|
|
|
|
|
|
else if (!strcmp(el, "mappings"))
|
|
|
|
e->begin_mappings();
|
|
|
|
|
|
|
|
else if (!strcmp(el, "mapping"))
|
|
|
|
parse_mapping(e, a);
|
|
|
|
|
|
|
|
else if (!strcmp(el, "hints"))
|
|
|
|
e->begin_hints();
|
|
|
|
|
|
|
|
else if (!strcmp(el, "hint"))
|
|
|
|
parse_hint(e, a);
|
|
|
|
|
2013-10-11 14:33:51 +05:30
|
|
|
else if (!strcmp(el, "discards"))
|
|
|
|
e->begin_discards();
|
|
|
|
|
|
|
|
else if (!strcmp(el, "discard"))
|
|
|
|
parse_discard(e, a);
|
|
|
|
|
2013-09-11 16:10:46 +05:30
|
|
|
else
|
|
|
|
throw runtime_error("unknown tag type");
|
|
|
|
}
|
|
|
|
|
|
|
|
void end_tag(void *data, const char *el) {
|
|
|
|
emitter *e = static_cast<emitter *>(data);
|
|
|
|
|
|
|
|
if (!strcmp(el, "superblock"))
|
|
|
|
e->end_superblock();
|
|
|
|
|
|
|
|
else if (!strcmp(el, "mappings"))
|
|
|
|
e->end_mappings();
|
|
|
|
|
|
|
|
else if (!strcmp(el, "mapping"))
|
|
|
|
// do nothing
|
|
|
|
;
|
|
|
|
|
|
|
|
else if (!strcmp(el, "hints"))
|
|
|
|
e->end_hints();
|
|
|
|
|
|
|
|
else if (!strcmp(el, "hint"))
|
|
|
|
// do nothing
|
|
|
|
;
|
|
|
|
|
2013-10-11 14:33:51 +05:30
|
|
|
else if (!strcmp(el, "discards"))
|
|
|
|
e->end_discards();
|
|
|
|
|
|
|
|
else if (!strcmp(el, "discard"))
|
|
|
|
// do nothing
|
|
|
|
;
|
|
|
|
|
2013-09-11 16:10:46 +05:30
|
|
|
else
|
|
|
|
throw runtime_error("unknown tag close");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
caching::emitter::ptr
|
|
|
|
caching::create_xml_emitter(ostream &out)
|
|
|
|
{
|
|
|
|
return emitter::ptr(new xml_emitter(out));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
caching::parse_xml(istream &in, emitter::ptr e)
|
|
|
|
{
|
|
|
|
XML_Parser parser = XML_ParserCreate(NULL);
|
|
|
|
if (!parser)
|
|
|
|
throw runtime_error("couldn't create xml parser");
|
|
|
|
|
|
|
|
XML_SetUserData(parser, e.get());
|
|
|
|
XML_SetElementHandler(parser, start_tag, end_tag);
|
|
|
|
|
|
|
|
while (!in.eof()) {
|
|
|
|
char buffer[4096];
|
|
|
|
in.read(buffer, sizeof(buffer));
|
|
|
|
size_t len = in.gcount();
|
|
|
|
int done = in.eof();
|
|
|
|
|
|
|
|
if (!XML_Parse(parser, buffer, len, done)) {
|
|
|
|
ostringstream out;
|
|
|
|
out << "Parse error at line "
|
|
|
|
<< XML_GetCurrentLineNumber(parser)
|
|
|
|
<< ":\n"
|
|
|
|
<< XML_ErrorString(XML_GetErrorCode(parser))
|
|
|
|
<< endl;
|
|
|
|
throw runtime_error(out.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|