1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-07-22 17:18:39 +00:00

Fix AreaStore's IDs persistence (#8888)

Improve documentation
Read old formats
Fix free ID function. Return first gap in map
This commit is contained in:
SmallJoker 2019-09-21 17:54:52 +02:00 committed by GitHub
parent 5fa614d97e
commit fec30e37ac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 90 additions and 42 deletions

View file

@ -64,6 +64,11 @@ const Area *AreaStore::getArea(u32 id) const
void AreaStore::serialize(std::ostream &os) const
{
// WARNING:
// Before 5.1.0-dev: version != 0 throws SerializationError
// After 5.1.0-dev: version >= 5 throws SerializationError
// Forwards-compatibility is assumed before version 5.
writeU8(os, 0); // Serialisation version
// TODO: Compression?
@ -75,27 +80,41 @@ void AreaStore::serialize(std::ostream &os) const
writeU16(os, a.data.size());
os.write(a.data.data(), a.data.size());
}
// Serialize IDs
for (const auto &it : areas_map)
writeU32(os, it.second.id);
}
void AreaStore::deserialize(std::istream &is)
{
u8 ver = readU8(is);
if (ver != 0)
// Assume forwards-compatibility before version 5
if (ver >= 5)
throw SerializationError("Unknown AreaStore "
"serialization version!");
u16 num_areas = readU16(is);
std::vector<Area> areas;
for (u32 i = 0; i < num_areas; ++i) {
Area a;
Area a(U32_MAX);
a.minedge = readV3S16(is);
a.maxedge = readV3S16(is);
u16 data_len = readU16(is);
char *data = new char[data_len];
is.read(data, data_len);
a.data = std::string(data, data_len);
insertArea(&a);
areas.emplace_back(a);
delete [] data;
}
bool read_ids = is.good(); // EOF for old formats
for (auto &area : areas) {
if (read_ids)
area.id = readU32(is);
insertArea(&area);
}
}
void AreaStore::invalidateCache()
@ -105,6 +124,19 @@ void AreaStore::invalidateCache()
}
}
u32 AreaStore::getNextId() const
{
u32 free_id = 0;
for (const auto &area : areas_map) {
if (area.first > free_id)
return free_id; // Found gap
free_id = area.first + 1;
}
// End of map
return free_id;
}
void AreaStore::setCacheParams(bool enabled, u8 block_radius, size_t limit)
{
m_cache_enabled = enabled;

View file

@ -37,15 +37,15 @@ with this program; if not, write to the Free Software Foundation, Inc.,
struct Area {
Area() = default;
Area(u32 area_id) : id(area_id) {}
Area(const v3s16 &mine, const v3s16 &maxe) :
minedge(mine), maxedge(maxe)
Area(const v3s16 &mine, const v3s16 &maxe, u32 area_id = U32_MAX) :
id(area_id), minedge(mine), maxedge(maxe)
{
sortBoxVerticies(minedge, maxedge);
}
u32 id = U32_MAX;
u32 id;
v3s16 minedge, maxedge;
std::string data;
};
@ -109,7 +109,7 @@ protected:
virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos) = 0;
/// Returns the next area ID and increments it.
u32 getNextId() { return m_next_id++; }
u32 getNextId() const;
// Note: This can't be an unordered_map, since all
// references would be invalidated on rehash.
@ -125,8 +125,6 @@ private:
/// If you modify this, call invalidateCache()
u8 m_cacheblock_radius = 64;
LRUCache<v3s16, std::vector<Area *> > m_res_cache;
u32 m_next_id = 0;
};