Implemented partial chunk saving on LevelDB (#3078)

This commit is contained in:
Dylan T 2019-08-07 17:39:36 +01:00 committed by GitHub
parent 9598af7683
commit c533f6a0bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 79 additions and 48 deletions

View File

@ -61,7 +61,7 @@ class SimpleChunkManager implements ChunkManager{
public function setBlockAt(int $x, int $y, int $z, Block $block) : void{
if($this->terrainPointer->moveTo($x, $y, $z, true)){
$this->terrainPointer->currentSubChunk->setFullBlock($x & 0xf, $y & 0xf, $z & 0xf, $block->getFullId());
$this->terrainPointer->currentChunk->setChanged(true);
$this->terrainPointer->currentChunk->setDirtyFlag(Chunk::DIRTY_FLAG_TERRAIN, true);
}else{
throw new \InvalidArgumentException("Cannot set block at coordinates x=$x,y=$y,z=$z, terrain is not loaded or out of bounds");
}

View File

@ -1038,9 +1038,9 @@ class World implements ChunkManager{
$this->timings->syncChunkSaveTimer->startTiming();
try{
foreach($this->chunks as $chunk){
if($chunk->hasChanged() and $chunk->isGenerated()){
if($chunk->isDirty() and $chunk->isGenerated()){
$this->provider->saveChunk($chunk);
$chunk->setChanged(false);
$chunk->clearDirtyFlags();
}
}
}finally{
@ -2213,7 +2213,7 @@ class World implements ChunkManager{
unset($this->blockCache[$chunkHash]);
unset($this->changedBlocks[$chunkHash]);
$chunk->setChanged();
$chunk->setDirty();
if(!$this->isChunkInUse($chunkX, $chunkZ)){
$this->unloadChunkRequest($chunkX, $chunkZ);
@ -2505,7 +2505,7 @@ class World implements ChunkManager{
return false;
}
if($trySave and $this->getAutoSave() and $chunk->isGenerated() and $chunk->hasChanged()){
if($trySave and $this->getAutoSave() and $chunk->isGenerated() and $chunk->isDirty()){
$this->timings->syncChunkSaveTimer->startTiming();
try{
$this->provider->saveChunk($chunk);

View File

@ -46,6 +46,10 @@ use function str_repeat;
use function strlen;
class Chunk{
public const DIRTY_FLAG_TERRAIN = 1 << 0;
public const DIRTY_FLAG_ENTITIES = 1 << 1;
public const DIRTY_FLAG_TILES = 1 << 2;
public const DIRTY_FLAG_BIOMES = 1 << 3;
public const MAX_SUBCHUNKS = 16;
@ -54,8 +58,8 @@ class Chunk{
/** @var int */
protected $z;
/** @var bool */
protected $hasChanged = false;
/** @var int */
private $dirtyFlags = 0;
/** @var bool */
protected $lightPopulated = false;
@ -189,7 +193,7 @@ class Chunk{
*/
public function setFullBlock(int $x, int $y, int $z, int $block) : void{
$this->getSubChunk($y >> 4, true)->setFullBlock($x, $y & 0xf, $z, $block);
$this->hasChanged = true;
$this->dirtyFlags |= self::DIRTY_FLAG_TERRAIN;
}
/**
@ -396,8 +400,8 @@ class Chunk{
* @param int $biomeId 0-255
*/
public function setBiomeId(int $x, int $z, int $biomeId) : void{
$this->hasChanged = true;
$this->biomeIds[($z << 4) | $x] = chr($biomeId & 0xff);
$this->dirtyFlags |= self::DIRTY_FLAG_BIOMES;
}
/**
@ -451,7 +455,7 @@ class Chunk{
}
$this->entities[$entity->getId()] = $entity;
if(!($entity instanceof Player)){
$this->hasChanged = true;
$this->dirtyFlags |= self::DIRTY_FLAG_ENTITIES;
}
}
@ -461,7 +465,7 @@ class Chunk{
public function removeEntity(Entity $entity) : void{
unset($this->entities[$entity->getId()]);
if(!($entity instanceof Player)){
$this->hasChanged = true;
$this->dirtyFlags |= self::DIRTY_FLAG_ENTITIES;
}
}
@ -478,7 +482,7 @@ class Chunk{
$this->tiles[$index]->close();
}
$this->tiles[$index] = $tile;
$this->hasChanged = true;
$this->dirtyFlags |= self::DIRTY_FLAG_TILES;
}
/**
@ -487,7 +491,7 @@ class Chunk{
public function removeTile(Tile $tile) : void{
$pos = $tile->getPos();
unset($this->tiles[Chunk::blockHash($pos->x, $pos->y, $pos->z)]);
$this->hasChanged = true;
$this->dirtyFlags |= self::DIRTY_FLAG_TILES;
}
/**
@ -563,7 +567,7 @@ class Chunk{
*/
public function initChunk(World $world) : void{
if($this->NBTentities !== null){
$this->hasChanged = true;
$this->dirtyFlags |= self::DIRTY_FLAG_ENTITIES;
$world->timings->syncChunkLoadEntitiesTimer->startTiming();
foreach($this->NBTentities as $nbt){
if($nbt instanceof CompoundTag){
@ -584,7 +588,7 @@ class Chunk{
$world->timings->syncChunkLoadEntitiesTimer->stopTiming();
}
if($this->NBTtiles !== null){
$this->hasChanged = true;
$this->dirtyFlags |= self::DIRTY_FLAG_TILES;
$world->timings->syncChunkLoadTileEntitiesTimer->startTiming();
foreach($this->NBTtiles as $nbt){
if($nbt instanceof CompoundTag){
@ -619,15 +623,35 @@ class Chunk{
/**
* @return bool
*/
public function hasChanged() : bool{
return $this->hasChanged or !empty($this->tiles) or !empty($this->getSavableEntities());
public function isDirty() : bool{
return $this->dirtyFlags !== 0 or !empty($this->tiles) or !empty($this->getSavableEntities());
}
public function getDirtyFlag(int $flag) : bool{
return ($this->dirtyFlags & $flag) !== 0;
}
/**
* @param bool $value
* @return int
*/
public function setChanged(bool $value = true) : void{
$this->hasChanged = $value;
public function getDirtyFlags() : int{
return $this->dirtyFlags;
}
public function setDirtyFlag(int $flag, bool $value) : void{
if($value){
$this->dirtyFlags |= $flag;
}else{
$this->dirtyFlags &= ~$flag;
}
}
public function setDirty() : void{
$this->dirtyFlags = ~0;
}
public function clearDirtyFlags() : void{
$this->dirtyFlags = 0;
}
/**
@ -666,7 +690,7 @@ class Chunk{
}else{
$this->subChunks[$y] = $subChunk;
}
$this->hasChanged = true;
$this->setDirtyFlag(self::DIRTY_FLAG_TERRAIN, true);
return true;
}

View File

@ -135,6 +135,7 @@ class FormatConverter{
$thisRound = $start;
static $reportInterval = 256;
foreach($this->oldProvider->getAllChunks(true, $this->logger) as $chunk){
$chunk->setDirty();
$new->saveChunk($chunk);
$counter++;
if(($counter % $reportInterval) === 0){

View File

@ -416,7 +416,9 @@ class LevelDB extends BaseWorldProvider implements WritableWorldProvider{
$chunk->setGenerated();
$chunk->setPopulated();
$chunk->setChanged($hasBeenUpgraded); //trigger rewriting chunk to disk if it was converted from an older format
if($hasBeenUpgraded){
$chunk->setDirty(); //trigger rewriting chunk to disk if it was converted from an older format
}
return $chunk;
}
@ -431,6 +433,7 @@ class LevelDB extends BaseWorldProvider implements WritableWorldProvider{
$write = new \LevelDBWriteBatch();
$write->put($index . self::TAG_VERSION, chr(self::CURRENT_LEVEL_CHUNK_VERSION));
if($chunk->getDirtyFlag(Chunk::DIRTY_FLAG_TERRAIN)){
$subChunks = $chunk->getSubChunks();
foreach($subChunks as $y => $subChunk){
$key = $index . self::TAG_SUBCHUNK_PREFIX . chr($y);
@ -462,8 +465,11 @@ class LevelDB extends BaseWorldProvider implements WritableWorldProvider{
$write->put($key, $subStream->getBuffer());
}
}
}
if($chunk->getDirtyFlag(Chunk::DIRTY_FLAG_BIOMES)){
$write->put($index . self::TAG_DATA_2D, str_repeat("\x00", 512) . $chunk->getBiomeIdArray());
}
//TODO: use this properly
$write->put($index . self::TAG_STATE_FINALISATION, chr(self::FINALISATION_DONE));

View File

@ -118,7 +118,7 @@ class PopulationTask extends AsyncTask{
foreach($chunks as $i => $c){
if($c !== null){
$c = $chunks[$i] = $manager->getChunk($c->getX(), $c->getZ());
if(!$c->hasChanged()){
if(!$c->isDirty()){
$chunks[$i] = null;
}
}else{