Implemented class StreamBuffer, which I'm quite satisfied with.
This commit is contained in:
@@ -26,7 +26,7 @@ LuaSnap::~LuaSnap() {
|
||||
std::cerr << "LuaSnap destructor not implemented yet" << std::endl;
|
||||
}
|
||||
|
||||
void LuaSnap::serialize(Packer *pk) {
|
||||
void LuaSnap::serialize(StreamBuffer *sb) {
|
||||
// Lua stack should be empty.
|
||||
assert(lua_gettop(state_) == 0);
|
||||
|
||||
@@ -63,25 +63,26 @@ void LuaSnap::serialize(Packer *pk) {
|
||||
assert(lua_gettop(state_) == 2);
|
||||
|
||||
// Write dummy length, use eris to write data, then overwrite length.
|
||||
pk->write_int64(0);
|
||||
size_t tell = pk->tellp();
|
||||
eris_dump(state_, pk->lua_writer, pk->lua_writer_ud());
|
||||
pk->overwrite_int64(tell, pk->tellp() - tell);
|
||||
sb->write_int64(0);
|
||||
int64_t pos1 = sb->write_count();
|
||||
eris_dump(state_, sb->lua_writer, sb->lua_writer_ud());
|
||||
int64_t pos2 = sb->write_count();
|
||||
sb->overwrite_int64(pos1, pos2 - pos1);
|
||||
lua_settop(state_, 0);
|
||||
std::cerr << "Eris dump is " << (pk->tellp() - tell) << " bytes." << std::endl;
|
||||
std::cerr << "Eris dump is " << pos2-pos1 << " bytes." << std::endl;
|
||||
}
|
||||
|
||||
void LuaSnap::deserialize(Unpacker *unpk) {
|
||||
void LuaSnap::deserialize(StreamBuffer *sb) {
|
||||
// Lua stack should be empty.
|
||||
assert(lua_gettop(state_) == 0);
|
||||
|
||||
// Get a reader subsection containing the eris data.
|
||||
size_t len = unpk->read_int64();
|
||||
Unpacker subsec = unpk->read_section(len);
|
||||
// Get the length of the eris dump.
|
||||
int64_t len = sb->read_int64();
|
||||
void *ud = sb->lua_reader_ud(len);
|
||||
|
||||
// Call eris with the permanents table and passing the snapshot as a lua_Reader.
|
||||
lua_getfield(state_, LUA_REGISTRYINDEX, "unpersist");
|
||||
eris_undump(state_, subsec.lua_reader, subsec.lua_reader_ud());
|
||||
eris_undump(state_, sb->lua_reader, ud);
|
||||
assert(lua_gettop(state_) == 2);
|
||||
|
||||
// Set up a stack frame.
|
||||
@@ -122,21 +123,17 @@ void LuaSnap::deserialize(Unpacker *unpk) {
|
||||
// require us to maintain any additional code.
|
||||
|
||||
bool LuaSnap::have_snapshot() const {
|
||||
return !snapshot_.empty();
|
||||
return snapshot_.write_count() != 0;
|
||||
}
|
||||
|
||||
void LuaSnap::snapshot() {
|
||||
assert(snapshot_.empty());
|
||||
std::ostringstream oss;
|
||||
Packer pk(&oss);
|
||||
serialize(&pk);
|
||||
snapshot_ = oss.str();
|
||||
assert(snapshot_.write_count() == 0);
|
||||
serialize(&snapshot_);
|
||||
}
|
||||
|
||||
void LuaSnap::rollback() {
|
||||
assert(!snapshot_.empty());
|
||||
Unpacker unpk(snapshot_.c_str(), snapshot_.size());
|
||||
deserialize(&unpk);
|
||||
assert(snapshot_.write_count() != 0);
|
||||
deserialize(&snapshot_);
|
||||
snapshot_.clear();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user