Serialization for idalloc and animqueue
This commit is contained in:
@@ -84,24 +84,24 @@ int64_t IdGlobalPool::alloc_id_for_thread(lua_State *L) {
|
||||
}
|
||||
|
||||
void IdGlobalPool::serialize(StreamBuffer *sb) {
|
||||
sb->write_int64(salvaged_.size());
|
||||
sb->write_int64(next_batch_);
|
||||
sb->write_int64(next_id_);
|
||||
sb->write_int32(queue_fill_);
|
||||
sb->write_size(salvaged_.size());
|
||||
for (int64_t batch : salvaged_) {
|
||||
sb->write_int64(batch);
|
||||
}
|
||||
sb->write_int64(next_batch_);
|
||||
sb->write_int64(next_id_);
|
||||
sb->write_int64(queue_fill_);
|
||||
}
|
||||
|
||||
void IdGlobalPool::deserialize(StreamBuffer *sb) {
|
||||
int64_t salvaged_size = sb->read_int64();
|
||||
salvaged_.resize(salvaged_size);
|
||||
for (int i=0; i < salvaged_size; i++) {
|
||||
salvaged_[i] = sb->read_int64();
|
||||
}
|
||||
next_batch_ = sb->read_int64();
|
||||
next_id_ = sb->read_int64();
|
||||
queue_fill_ = sb->read_int64();
|
||||
queue_fill_ = sb->read_int32();
|
||||
size_t salvaged_size = sb->read_size();
|
||||
salvaged_.resize(salvaged_size);
|
||||
for (int i=0; i < int(salvaged_size); i++) {
|
||||
salvaged_[i] = sb->read_int64();
|
||||
}
|
||||
}
|
||||
|
||||
IdPlayerPool::IdPlayerPool(IdGlobalPool *gp) {
|
||||
@@ -155,16 +155,16 @@ void IdPlayerPool::prepare_thread(lua_State *L) {
|
||||
}
|
||||
|
||||
void IdPlayerPool::serialize(StreamBuffer *sb) {
|
||||
sb->write_int64(ranges_.size());
|
||||
sb->write_size(ranges_.size());
|
||||
for (int64_t batch : ranges_) {
|
||||
sb->write_int64(batch);
|
||||
}
|
||||
}
|
||||
|
||||
void IdPlayerPool::deserialize(StreamBuffer *sb) {
|
||||
int64_t ranges_size = sb->read_int64();
|
||||
size_t ranges_size = sb->read_size();
|
||||
ranges_.resize(ranges_size);
|
||||
for (int i=0; i < ranges_size; i++) {
|
||||
for (int i=0; i < int(ranges_size); i++) {
|
||||
ranges_[i] = sb->read_int64();
|
||||
}
|
||||
}
|
||||
@@ -172,6 +172,9 @@ void IdPlayerPool::deserialize(StreamBuffer *sb) {
|
||||
LuaDefine(unittests_idalloc, "c") {
|
||||
IdGlobalPool gp;
|
||||
IdPlayerPool pp(&gp);
|
||||
IdGlobalPool gpds;
|
||||
IdPlayerPool ppds(&gpds);
|
||||
StreamBuffer sb;
|
||||
|
||||
// Synchronous pools produce IDs starting at 0x001E000000000000
|
||||
gp.init_synch(3);
|
||||
@@ -277,5 +280,35 @@ LuaDefine(unittests_idalloc, "c") {
|
||||
LuaAssert(L, gp.alloc_id_for_thread(L) == 0x0010000000000000);
|
||||
LuaAssert(L, lua_getnextid(L) == 0);
|
||||
|
||||
// Serialize and deserialize a global pool.
|
||||
gp.init_master(3);
|
||||
gpds.init_master(10);
|
||||
LuaAssert(L, gp.get_one() == 0x0010000000000000);
|
||||
LuaAssert(L, gp.get_batch() == nthbatch(0));
|
||||
gp.salvage(nthbatch(182));
|
||||
gp.salvage(nthbatch(183));
|
||||
gp.serialize(&sb);
|
||||
gpds.deserialize(&sb);
|
||||
LuaAssert(L, gpds.queue_fill() == 3);
|
||||
LuaAssert(L, gpds.get_one() == 0x0010000000000001);
|
||||
LuaAssert(L, gpds.get_batch() == nthbatch(183));
|
||||
LuaAssert(L, gpds.get_batch() == nthbatch(182));
|
||||
LuaAssert(L, gpds.get_batch() == nthbatch(1));
|
||||
|
||||
// Serialize and deserialize a player pool.
|
||||
gp.init_master(3);
|
||||
gpds.init_synch(5);
|
||||
LuaAssert(L, gp.get_batch() == nthbatch(0));
|
||||
pp.purge();
|
||||
pp.refill();
|
||||
LuaAssert(L, pp.size() == 3);
|
||||
ppds.purge();
|
||||
pp.serialize(&sb);
|
||||
ppds.deserialize(&sb);
|
||||
LuaAssert(L, ppds.size() == 3);
|
||||
LuaAssert(L, ppds.get_batch() == nthbatch(1));
|
||||
LuaAssert(L, ppds.get_batch() == nthbatch(2));
|
||||
LuaAssert(L, ppds.get_batch() == nthbatch(3));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user