From 821551a2669123ac9a476894d65b5efe10026040 Mon Sep 17 00:00:00 2001 From: ShadowNinja Date: Fri, 30 Oct 2015 19:27:48 -0400 Subject: Implement AreaStore serialization --- src/util/areastore.cpp | 91 ++++++++++++++++---------------------------------- src/util/areastore.h | 18 ++++------ 2 files changed, 35 insertions(+), 74 deletions(-) (limited to 'src/util') diff --git a/src/util/areastore.cpp b/src/util/areastore.cpp index 357ce37f0..17addb3af 100644 --- a/src/util/areastore.cpp +++ b/src/util/areastore.cpp @@ -62,57 +62,42 @@ const Area *AreaStore::getArea(u32 id) const return &it->second; } -#if 0 -Currently, serialisation is commented out. This is because of multiple reasons: -1. Why do we store the areastore into a file, why not into the database? -2. We don't use libspatial's serialisation, but we should, or perhaps not, because - it would remove the ability to switch. Perhaps write migration routines? -3. Various things need fixing, e.g. the size is serialized as - c++ implementation defined size_t -bool AreaStore::deserialize(std::istream &is) +void AreaStore::serialize(std::ostream &os) const { - u8 ver = readU8(is); - if (ver != 1) - return false; - u16 count_areas = readU16(is); - for (u16 i = 0; i < count_areas; i++) { - // deserialize an area - Area a; - a.id = readU32(is); - a.minedge = readV3S16(is); - a.maxedge = readV3S16(is); - a.datalen = readU16(is); - a.data = new char[a.datalen]; - is.read((char *) a.data, a.datalen); - insertArea(a); + writeU8(os, 0); // Serialisation version + + // TODO: Compression? + writeU16(os, areas_map.size()); + for (AreaMap::const_iterator it = areas_map.begin(); + it != areas_map.end(); ++it) { + const Area &a = it->second; + writeV3S16(os, a.minedge); + writeV3S16(os, a.maxedge); + writeU16(os, a.data.size()); + os.write(a.data.data(), a.data.size()); } - return true; } - -static bool serialize_area(void *ostr, Area *a) +void AreaStore::deserialize(std::istream &is) { - std::ostream &os = *((std::ostream *) ostr); - writeU32(os, a->id); - writeV3S16(os, a->minedge); - writeV3S16(os, a->maxedge); - writeU16(os, a->datalen); - os.write(a->data, a->datalen); - - return false; -} - + u8 ver = readU8(is); + if (ver != 0) + throw SerializationError("Unknown AreaStore " + "serialization version!"); -void AreaStore::serialize(std::ostream &os) const -{ - // write initial data - writeU8(os, 1); // serialisation version - writeU16(os, areas_map.size()); //DANGER: not platform independent - forEach(&serialize_area, &os); + u16 num_areas = readU16(is); + for (u32 i = 0; i < num_areas; ++i) { + Area a; + 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); + } } -#endif - void AreaStore::invalidateCache() { if (m_cache_enabled) { @@ -226,18 +211,6 @@ void VectorAreaStore::getAreasInArea(std::vector *result, } } -#if 0 -bool SimpleAreaStore::forEach(ForEachCallback callback, void *arg) const -{ - for (size_t i = 0; i < m_areas.size(); ++i) { - if (callback(m_areas[i], arg)) { - return true; - } - } - return false; -} -#endif - #if USE_SPATIAL static inline SpatialIndex::Region get_spatial_region(const v3s16 minedge, @@ -301,14 +274,6 @@ void SpatialAreaStore::getAreasInArea(std::vector *result, } } -#if 0 -bool SpatialAreaStore::forEach(ForEachCallback callback, void *arg) const -{ - // TODO ?? (this is only needed for serialisation, but libspatial has its own serialisation) - return false; -} -#endif - SpatialAreaStore::~SpatialAreaStore() { delete m_tree; diff --git a/src/util/areastore.h b/src/util/areastore.h index 5b4e9a71f..ab6bd76a3 100644 --- a/src/util/areastore.h +++ b/src/util/areastore.h @@ -92,16 +92,14 @@ public: /// or NULL if it doesn't exist. const Area *getArea(u32 id) const; -#if 0 - typedef bool (*ForEachCallback)(const Area *a, void *arg); - /// Calls a passed function for every stored area, until the - /// callback returns true. If that happens, it returns true, - /// if the search is exhausted, it returns false. - virtual bool forEach(ForEachCallback, void *arg=NULL) const = 0; - + /// Serializes the store's areas to a binary ostream. void serialize(std::ostream &is) const; - bool deserialize(std::istream &is); -#endif + + /// Deserializes the Areas from a binary istream. + /// This does not currently clear the AreaStore before adding the + /// areas, making it possible to deserialize multiple serialized + /// AreaStores. + void deserialize(std::istream &is); protected: /// Invalidates the getAreasForPos cache. @@ -141,7 +139,6 @@ public: virtual bool removeArea(u32 id); virtual void getAreasInArea(std::vector *result, v3s16 minedge, v3s16 maxedge, bool accept_overlap); - //virtual bool forEach(ForEachCallback, void *arg) const; protected: virtual void getAreasForPosImpl(std::vector *result, v3s16 pos); @@ -162,7 +159,6 @@ public: virtual bool removeArea(u32 id); virtual void getAreasInArea(std::vector *result, v3s16 minedge, v3s16 maxedge, bool accept_overlap); - //virtual bool forEach(ForEachCallback, void *arg) const; protected: virtual void getAreasForPosImpl(std::vector *result, v3s16 pos); -- cgit v1.2.3