Add serialization code to idalloc
This commit is contained in:
@@ -83,6 +83,27 @@ int64_t IdGlobalPool::alloc_id_for_thread(lua_State *L) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void IdGlobalPool::serialize(StreamBuffer *sb) {
|
||||||
|
sb->write_int64(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();
|
||||||
|
}
|
||||||
|
|
||||||
IdPlayerPool::IdPlayerPool(IdGlobalPool *gp) {
|
IdPlayerPool::IdPlayerPool(IdGlobalPool *gp) {
|
||||||
global_ = gp;
|
global_ = gp;
|
||||||
}
|
}
|
||||||
@@ -133,6 +154,21 @@ void IdPlayerPool::prepare_thread(lua_State *L) {
|
|||||||
lua_setnextid(L, get_batch());
|
lua_setnextid(L, get_batch());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void IdPlayerPool::serialize(StreamBuffer *sb) {
|
||||||
|
sb->write_int64(ranges_.size());
|
||||||
|
for (int64_t batch : ranges_) {
|
||||||
|
sb->write_int64(batch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void IdPlayerPool::deserialize(StreamBuffer *sb) {
|
||||||
|
int64_t ranges_size = sb->read_int64();
|
||||||
|
ranges_.resize(ranges_size);
|
||||||
|
for (int i=0; i < ranges_size; i++) {
|
||||||
|
ranges_[i] = sb->read_int64();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
LuaDefine(unittests_idalloc, "c") {
|
LuaDefine(unittests_idalloc, "c") {
|
||||||
IdGlobalPool gp;
|
IdGlobalPool gp;
|
||||||
IdPlayerPool pp(&gp);
|
IdPlayerPool pp(&gp);
|
||||||
|
|||||||
@@ -69,6 +69,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <deque>
|
#include <deque>
|
||||||
#include "luastack.hpp"
|
#include "luastack.hpp"
|
||||||
|
#include "streambuffer.hpp"
|
||||||
|
|
||||||
class IdGlobalPool {
|
class IdGlobalPool {
|
||||||
private:
|
private:
|
||||||
@@ -118,6 +119,12 @@ public:
|
|||||||
// Allocate an ID for the specified thread. Uses the thread's
|
// Allocate an ID for the specified thread. Uses the thread's
|
||||||
// batch if possible. If not, fetches one ID from the global pool.
|
// batch if possible. If not, fetches one ID from the global pool.
|
||||||
int64_t alloc_id_for_thread(lua_State *L);
|
int64_t alloc_id_for_thread(lua_State *L);
|
||||||
|
|
||||||
|
// Serialize to a streambuffer.
|
||||||
|
void serialize(StreamBuffer *sb);
|
||||||
|
|
||||||
|
// Deserialize from a streambuffer.
|
||||||
|
void deserialize(StreamBuffer *sb);
|
||||||
};
|
};
|
||||||
|
|
||||||
class IdPlayerPool {
|
class IdPlayerPool {
|
||||||
@@ -155,6 +162,12 @@ public:
|
|||||||
|
|
||||||
// Return the size of the queue.
|
// Return the size of the queue.
|
||||||
int size() { return ranges_.size(); }
|
int size() { return ranges_.size(); }
|
||||||
|
|
||||||
|
// Serialize to a streambuffer.
|
||||||
|
void serialize(StreamBuffer *sb);
|
||||||
|
|
||||||
|
// Deserialize from a streambuffer.
|
||||||
|
void deserialize(StreamBuffer *sb);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // IDALLOC_HPP
|
#endif // IDALLOC_HPP
|
||||||
|
|||||||
Reference in New Issue
Block a user