ServerMap saving: cleanups (#6274)

* remove sector meta loading/saving from files which targets dead code (differs_from_disk is always empty)
* this remove empty ServerMapSector and ClientMapSector, remove MapSector childs
This commit is contained in:
Loïc Blot 2017-08-19 11:29:46 +02:00 committed by GitHub
parent b82884aa62
commit de4c2e4250
5 changed files with 23 additions and 470 deletions

@ -64,20 +64,14 @@ MapSector * ClientMap::emergeSector(v2s16 p2d)
{ {
DSTACK(FUNCTION_NAME); DSTACK(FUNCTION_NAME);
// Check that it doesn't exist already // Check that it doesn't exist already
try{ try {
return getSectorNoGenerate(p2d); return getSectorNoGenerate(p2d);
} } catch(InvalidPositionException &e) {
catch(InvalidPositionException &e)
{
} }
// Create a sector // Create a sector
ClientMapSector *sector = new ClientMapSector(this, p2d, m_gamedef); MapSector *sector = new MapSector(this, p2d, m_gamedef);
m_sectors[p2d] = sector;
{
//MutexAutoLock lock(m_sector_mutex); // Bulk comment-out
m_sectors[p2d] = sector;
}
return sector; return sector;
} }

@ -1211,14 +1211,6 @@ ServerMap::ServerMap(const std::string &savedir, IGameDef *gamedef,
infostream<<"Please remove the map or fix it."<<std::endl; infostream<<"Please remove the map or fix it."<<std::endl;
warningstream<<"Map saving will be disabled."<<std::endl; warningstream<<"Map saving will be disabled."<<std::endl;
} }
infostream<<"Initializing new map."<<std::endl;
// Create zero sector
emergeSector(v2s16(0,0));
// Initially write whole map
save(MOD_STATE_CLEAN);
} }
ServerMap::~ServerMap() ServerMap::~ServerMap()
@ -1227,15 +1219,12 @@ ServerMap::~ServerMap()
try try
{ {
if(m_map_saving_enabled) if (m_map_saving_enabled) {
{
// Save only changed parts // Save only changed parts
save(MOD_STATE_WRITE_AT_UNLOAD); save(MOD_STATE_WRITE_AT_UNLOAD);
infostream<<"ServerMap: Saved map to "<<m_savedir<<std::endl; infostream << "ServerMap: Saved map to " << m_savedir << std::endl;
} } else {
else infostream << "ServerMap: Map not saved" << std::endl;
{
infostream<<"ServerMap: Map not saved"<<std::endl;
} }
} }
catch(std::exception &e) catch(std::exception &e)
@ -1328,7 +1317,7 @@ bool ServerMap::initBlockMake(v3s16 blockpos, BlockMakeData *data)
for (s16 z = full_bpmin.Z; z <= full_bpmax.Z; z++) { for (s16 z = full_bpmin.Z; z <= full_bpmax.Z; z++) {
v2s16 sectorpos(x, z); v2s16 sectorpos(x, z);
// Sector metadata is loaded from disk if not already loaded. // Sector metadata is loaded from disk if not already loaded.
ServerMapSector *sector = createSector(sectorpos); MapSector *sector = createSector(sectorpos);
FATAL_ERROR_IF(sector == NULL, "createSector() failed"); FATAL_ERROR_IF(sector == NULL, "createSector() failed");
for (s16 y = full_bpmin.Y; y <= full_bpmax.Y; y++) { for (s16 y = full_bpmin.Y; y <= full_bpmax.Y; y++) {
@ -1442,7 +1431,7 @@ void ServerMap::finishBlockMake(BlockMakeData *data,
//save(MOD_STATE_WRITE_AT_UNLOAD); //save(MOD_STATE_WRITE_AT_UNLOAD);
} }
ServerMapSector *ServerMap::createSector(v2s16 p2d) MapSector *ServerMap::createSector(v2s16 p2d)
{ {
DSTACKF("%s: p2d=(%d,%d)", DSTACKF("%s: p2d=(%d,%d)",
FUNCTION_NAME, FUNCTION_NAME,
@ -1451,31 +1440,10 @@ ServerMapSector *ServerMap::createSector(v2s16 p2d)
/* /*
Check if it exists already in memory Check if it exists already in memory
*/ */
ServerMapSector *sector = (ServerMapSector*)getSectorNoGenerateNoEx(p2d); MapSector *sector = getSectorNoGenerateNoEx(p2d);
if(sector != NULL) if (sector)
return sector; return sector;
/*
Try to load it from disk (with blocks)
*/
//if(loadSectorFull(p2d) == true)
/*
Try to load metadata from disk
*/
#if 0
if(loadSectorMeta(p2d) == true)
{
ServerMapSector *sector = (ServerMapSector*)getSectorNoGenerateNoEx(p2d);
if(sector == NULL)
{
infostream<<"ServerMap::createSector(): loadSectorFull didn't make a sector"<<std::endl;
throw InvalidPositionException("");
}
return sector;
}
#endif
/* /*
Do not create over max mapgen limit Do not create over max mapgen limit
*/ */
@ -1490,7 +1458,7 @@ ServerMapSector *ServerMap::createSector(v2s16 p2d)
Generate blank sector Generate blank sector
*/ */
sector = new ServerMapSector(this, p2d, m_gamedef); sector = new MapSector(this, p2d, m_gamedef);
// Sector position on map in nodes // Sector position on map in nodes
//v2s16 nodepos2d = p2d * MAP_BLOCKSIZE; //v2s16 nodepos2d = p2d * MAP_BLOCKSIZE;
@ -1639,35 +1607,20 @@ MapBlock * ServerMap::createBlock(v3s16 p)
NOTE: On old save formats, this will be slow, as it generates NOTE: On old save formats, this will be slow, as it generates
lighting on blocks for them. lighting on blocks for them.
*/ */
ServerMapSector *sector; MapSector *sector;
try { try {
sector = (ServerMapSector*)createSector(p2d); sector = createSector(p2d);
assert(sector->getId() == MAPSECTOR_SERVER); } catch (InvalidPositionException &e) {
}
catch(InvalidPositionException &e)
{
infostream<<"createBlock: createSector() failed"<<std::endl; infostream<<"createBlock: createSector() failed"<<std::endl;
throw e; throw e;
} }
/*
NOTE: This should not be done, or at least the exception
should not be passed on as std::exception, because it
won't be catched at all.
*/
/*catch(std::exception &e)
{
infostream<<"createBlock: createSector() failed: "
<<e.what()<<std::endl;
throw e;
}*/
/* /*
Try to get a block from the sector Try to get a block from the sector
*/ */
MapBlock *block = sector->getBlockNoCreateNoEx(block_y); MapBlock *block = sector->getBlockNoCreateNoEx(block_y);
if(block) if (block) {
{
if(block->isDummy()) if(block->isDummy())
block->unDummify(); block->unDummify();
return block; return block;
@ -1697,39 +1650,12 @@ MapBlock * ServerMap::emergeBlock(v3s16 p, bool create_blank)
} }
if (create_blank) { if (create_blank) {
ServerMapSector *sector = createSector(v2s16(p.X, p.Z)); MapSector *sector = createSector(v2s16(p.X, p.Z));
MapBlock *block = sector->createBlankBlock(p.Y); MapBlock *block = sector->createBlankBlock(p.Y);
return block; return block;
} }
#if 0
if(allow_generate)
{
std::map<v3s16, MapBlock*> modified_blocks;
MapBlock *block = generateBlock(p, modified_blocks);
if(block)
{
MapEditEvent event;
event.type = MEET_OTHER;
event.p = p;
// Copy modified_blocks to event
for(std::map<v3s16, MapBlock*>::iterator
i = modified_blocks.begin();
i != modified_blocks.end(); ++i)
{
event.modified_blocks.insert(i->first);
}
// Queue event
dispatchEvent(&event);
return block;
}
}
#endif
return NULL; return NULL;
} }
@ -1918,7 +1844,6 @@ void ServerMap::save(ModifiedState save_level)
// Profile modified reasons // Profile modified reasons
Profiler modprofiler; Profiler modprofiler;
u32 sector_meta_count = 0;
u32 block_count = 0; u32 block_count = 0;
u32 block_count_all = 0; // Number of blocks in memory u32 block_count_all = 0; // Number of blocks in memory
@ -1926,13 +1851,7 @@ void ServerMap::save(ModifiedState save_level)
bool save_started = false; bool save_started = false;
for (auto &sector_it : m_sectors) { for (auto &sector_it : m_sectors) {
ServerMapSector *sector = (ServerMapSector*) sector_it.second; MapSector *sector = sector_it.second;
assert(sector->getId() == MAPSECTOR_SERVER);
if(sector->differs_from_disk || save_level == MOD_STATE_CLEAN) {
saveSectorMeta(sector);
sector_meta_count++;
}
MapBlockVect blocks; MapBlockVect blocks;
sector->getBlocks(blocks); sector->getBlocks(blocks);
@ -1951,12 +1870,6 @@ void ServerMap::save(ModifiedState save_level)
saveBlock(block); saveBlock(block);
block_count++; block_count++;
/*infostream<<"ServerMap: Written block ("
<<block->getPos().X<<","
<<block->getPos().Y<<","
<<block->getPos().Z<<")"
<<std::endl;*/
} }
} }
} }
@ -1967,10 +1880,9 @@ void ServerMap::save(ModifiedState save_level)
/* /*
Only print if something happened or saved whole map Only print if something happened or saved whole map
*/ */
if(save_level == MOD_STATE_CLEAN || sector_meta_count != 0 if(save_level == MOD_STATE_CLEAN
|| block_count != 0) { || block_count != 0) {
infostream<<"ServerMap: Written: " infostream<<"ServerMap: Written: "
<<sector_meta_count<<" sector metadata files, "
<<block_count<<" block files" <<block_count<<" block files"
<<", "<<block_count_all<<" blocks in memory." <<", "<<block_count_all<<" blocks in memory."
<<std::endl; <<std::endl;
@ -2004,180 +1916,6 @@ void ServerMap::listAllLoadedBlocks(std::vector<v3s16> &dst)
} }
} }
void ServerMap::saveSectorMeta(ServerMapSector *sector)
{
DSTACK(FUNCTION_NAME);
// Format used for writing
u8 version = SER_FMT_VER_HIGHEST_WRITE;
// Get destination
v2s16 pos = sector->getPos();
std::string dir = getSectorDir(pos);
createDirs(dir);
std::string fullpath = dir + DIR_DELIM + "meta";
std::ostringstream ss(std::ios_base::binary);
sector->serialize(ss, version);
if(!fs::safeWriteToFile(fullpath, ss.str()))
throw FileNotGoodException("Cannot write sector metafile");
sector->differs_from_disk = false;
}
MapSector* ServerMap::loadSectorMeta(std::string sectordir, bool save_after_load)
{
DSTACK(FUNCTION_NAME);
// Get destination
v2s16 p2d = getSectorPos(sectordir);
ServerMapSector *sector = NULL;
std::string fullpath = sectordir + DIR_DELIM + "meta";
std::ifstream is(fullpath.c_str(), std::ios_base::binary);
if (!is.good()) {
// If the directory exists anyway, it probably is in some old
// format. Just go ahead and create the sector.
if(fs::PathExists(sectordir))
{
/*infostream<<"ServerMap::loadSectorMeta(): Sector metafile "
<<fullpath<<" doesn't exist but directory does."
<<" Continuing with a sector with no metadata."
<<std::endl;*/
sector = new ServerMapSector(this, p2d, m_gamedef);
m_sectors[p2d] = sector;
}
else
{
throw FileNotGoodException("Cannot open sector metafile");
}
}
else
{
sector = ServerMapSector::deSerialize
(is, this, p2d, m_sectors, m_gamedef);
if(save_after_load)
saveSectorMeta(sector);
}
sector->differs_from_disk = false;
return sector;
}
bool ServerMap::loadSectorMeta(v2s16 p2d)
{
DSTACK(FUNCTION_NAME);
// The directory layout we're going to load from.
// 1 - original sectors/xxxxzzzz/
// 2 - new sectors2/xxx/zzz/
// If we load from anything but the latest structure, we will
// immediately save to the new one, and remove the old.
int loadlayout = 1;
std::string sectordir1 = getSectorDir(p2d, 1);
std::string sectordir;
if(fs::PathExists(sectordir1))
{
sectordir = sectordir1;
}
else
{
loadlayout = 2;
sectordir = getSectorDir(p2d, 2);
}
try{
loadSectorMeta(sectordir, loadlayout != 2);
}
catch(InvalidFilenameException &e)
{
return false;
}
catch(FileNotGoodException &e)
{
return false;
}
catch(std::exception &e)
{
return false;
}
return true;
}
#if 0
bool ServerMap::loadSectorFull(v2s16 p2d)
{
DSTACK(FUNCTION_NAME);
MapSector *sector = NULL;
// The directory layout we're going to load from.
// 1 - original sectors/xxxxzzzz/
// 2 - new sectors2/xxx/zzz/
// If we load from anything but the latest structure, we will
// immediately save to the new one, and remove the old.
int loadlayout = 1;
std::string sectordir1 = getSectorDir(p2d, 1);
std::string sectordir;
if(fs::PathExists(sectordir1))
{
sectordir = sectordir1;
}
else
{
loadlayout = 2;
sectordir = getSectorDir(p2d, 2);
}
try{
sector = loadSectorMeta(sectordir, loadlayout != 2);
}
catch(InvalidFilenameException &e)
{
return false;
}
catch(FileNotGoodException &e)
{
return false;
}
catch(std::exception &e)
{
return false;
}
/*
Load blocks
*/
std::vector<fs::DirListNode> list2 = fs::GetDirListing
(sectordir);
std::vector<fs::DirListNode>::iterator i2;
for(i2=list2.begin(); i2!=list2.end(); i2++)
{
// We want files
if(i2->dir)
continue;
try{
loadBlock(sectordir, i2->name, sector, loadlayout != 2);
}
catch(InvalidFilenameException &e)
{
// This catches unknown crap in directory
}
}
if(loadlayout != 2)
{
infostream<<"Sector converted to new layout - deleting "<<
sectordir1<<std::endl;
fs::RecursiveDelete(sectordir1);
}
return true;
}
#endif
MapDatabase *ServerMap::createDatabase( MapDatabase *ServerMap::createDatabase(
const std::string &name, const std::string &name,
const std::string &savedir, const std::string &savedir,
@ -2243,8 +1981,7 @@ bool ServerMap::saveBlock(MapBlock *block, MapDatabase *db)
o.write((char*) &version, 1); o.write((char*) &version, 1);
block->serialize(o, version, true); block->serialize(o, version, true);
std::string data = o.str(); bool ret = db->saveBlock(p3d, o.str());
bool ret = db->saveBlock(p3d, data);
if (ret) { if (ret) {
// We just wrote it to the disk so clear modified flag // We just wrote it to the disk so clear modified flag
block->resetModified(); block->resetModified();
@ -2414,13 +2151,11 @@ MapBlock* ServerMap::loadBlock(v3s16 blockpos)
// 2 - new sectors2/xxx/zzz/ // 2 - new sectors2/xxx/zzz/
// If we load from anything but the latest structure, we will // If we load from anything but the latest structure, we will
// immediately save to the new one, and remove the old. // immediately save to the new one, and remove the old.
int loadlayout = 1;
std::string sectordir1 = getSectorDir(p2d, 1); std::string sectordir1 = getSectorDir(p2d, 1);
std::string sectordir; std::string sectordir;
if (fs::PathExists(sectordir1)) { if (fs::PathExists(sectordir1)) {
sectordir = sectordir1; sectordir = sectordir1;
} else { } else {
loadlayout = 2;
sectordir = getSectorDir(p2d, 2); sectordir = getSectorDir(p2d, 2);
} }
@ -2429,18 +2164,6 @@ MapBlock* ServerMap::loadBlock(v3s16 blockpos)
*/ */
MapSector *sector = getSectorNoGenerateNoEx(p2d); MapSector *sector = getSectorNoGenerateNoEx(p2d);
if (sector == NULL) {
try {
sector = loadSectorMeta(sectordir, loadlayout != 2);
} catch(InvalidFilenameException &e) {
return NULL;
} catch(FileNotGoodException &e) {
return NULL;
} catch(std::exception &e) {
return NULL;
}
}
/* /*
Make sure file exists Make sure file exists
@ -2455,6 +2178,7 @@ MapBlock* ServerMap::loadBlock(v3s16 blockpos)
*/ */
loadBlock(sectordir, blockfilename, sector, true); loadBlock(sectordir, blockfilename, sector, true);
} }
MapBlock *block = getBlockNoCreateNoEx(blockpos); MapBlock *block = getBlockNoCreateNoEx(blockpos);
if (created_new && (block != NULL)) { if (created_new && (block != NULL)) {
std::map<v3s16, MapBlock*> modified_blocks; std::map<v3s16, MapBlock*> modified_blocks;

@ -349,7 +349,7 @@ public:
- Check disk (doesn't load blocks) - Check disk (doesn't load blocks)
- Create blank one - Create blank one
*/ */
ServerMapSector *createSector(v2s16 p); MapSector *createSector(v2s16 p);
bool saoPositionOverLimit(const v3f &p); bool saoPositionOverLimit(const v3f &p);
@ -418,18 +418,6 @@ public:
MapgenParams *getMapgenParams(); MapgenParams *getMapgenParams();
/*void saveChunkMeta();
void loadChunkMeta();*/
// The sector mutex should be locked when calling most of these
// This only saves sector-specific data such as the heightmap
// (no MapBlocks)
// DEPRECATED? Sectors have no metadata anymore.
void saveSectorMeta(ServerMapSector *sector);
MapSector* loadSectorMeta(std::string dirname, bool save_after_load);
bool loadSectorMeta(v2s16 p2d);
bool saveBlock(MapBlock *block); bool saveBlock(MapBlock *block);
static bool saveBlock(MapBlock *block, MapDatabase *db); static bool saveBlock(MapBlock *block, MapDatabase *db);
// This will generate a sector with getSector if not found. // This will generate a sector with getSector if not found.

@ -128,106 +128,3 @@ void MapSector::getBlocks(MapBlockVect &dest)
dest.push_back(block.second); dest.push_back(block.second);
} }
} }
/*
ServerMapSector
*/
ServerMapSector::ServerMapSector(Map *parent, v2s16 pos, IGameDef *gamedef):
MapSector(parent, pos, gamedef)
{
}
void ServerMapSector::serialize(std::ostream &os, u8 version)
{
if(!ser_ver_supported(version))
throw VersionMismatchException("ERROR: MapSector format not supported");
/*
[0] u8 serialization version
+ heightmap data
*/
// Server has both of these, no need to support not having them.
//assert(m_objects != NULL);
// Write version
os.write((char*)&version, 1);
/*
Add stuff here, if needed
*/
}
ServerMapSector* ServerMapSector::deSerialize(
std::istream &is,
Map *parent,
v2s16 p2d,
std::map<v2s16, MapSector*> & sectors,
IGameDef *gamedef
)
{
/*
[0] u8 serialization version
+ heightmap data
*/
/*
Read stuff
*/
// Read version
u8 version = SER_FMT_VER_INVALID;
is.read((char*)&version, 1);
if(!ser_ver_supported(version))
throw VersionMismatchException("ERROR: MapSector format not supported");
/*
Add necessary reading stuff here
*/
/*
Get or create sector
*/
ServerMapSector *sector = NULL;
std::map<v2s16, MapSector*>::iterator n = sectors.find(p2d);
if(n != sectors.end())
{
warningstream<<"deSerializing existent sectors not supported "
"at the moment, because code hasn't been tested."
<<std::endl;
MapSector *sector = n->second;
assert(sector->getId() == MAPSECTOR_SERVER);
return (ServerMapSector*)sector;
}
sector = new ServerMapSector(parent, p2d, gamedef);
sectors[p2d] = sector;
/*
Set stuff in sector
*/
// Nothing here
return sector;
}
#ifndef SERVER
/*
ClientMapSector
*/
ClientMapSector::ClientMapSector(Map *parent, v2s16 pos, IGameDef *gamedef):
MapSector(parent, pos, gamedef)
{
}
#endif // !SERVER
//END

@ -43,8 +43,6 @@ public:
MapSector(Map *parent, v2s16 pos, IGameDef *gamedef); MapSector(Map *parent, v2s16 pos, IGameDef *gamedef);
virtual ~MapSector(); virtual ~MapSector();
virtual u32 getId() const = 0;
void deleteBlocks(); void deleteBlocks();
v2s16 getPos() v2s16 getPos()
@ -64,9 +62,6 @@ public:
bool empty() const { return m_blocks.empty(); } bool empty() const { return m_blocks.empty(); }
// Always false at the moment, because sector contains no metadata.
bool differs_from_disk = false;
protected: protected:
// The pile of MapBlocks // The pile of MapBlocks
@ -89,48 +84,3 @@ protected:
MapBlock *getBlockBuffered(s16 y); MapBlock *getBlockBuffered(s16 y);
}; };
class ServerMapSector : public MapSector
{
public:
ServerMapSector(Map *parent, v2s16 pos, IGameDef *gamedef);
~ServerMapSector() = default;
u32 getId() const
{
return MAPSECTOR_SERVER;
}
/*
These functions handle metadata.
They do not handle blocks.
*/
void serialize(std::ostream &os, u8 version);
static ServerMapSector* deSerialize(
std::istream &is,
Map *parent,
v2s16 p2d,
std::map<v2s16, MapSector*> & sectors,
IGameDef *gamedef
);
private:
};
#ifndef SERVER
class ClientMapSector : public MapSector
{
public:
ClientMapSector(Map *parent, v2s16 pos, IGameDef *gamedef);
~ClientMapSector() = default;
u32 getId() const
{
return MAPSECTOR_CLIENT;
}
private:
};
#endif