Skip to content

Commit

Permalink
add read cache
Browse files Browse the repository at this point in the history
  • Loading branch information
CMA2401PT committed Jul 27, 2022
1 parent 43a222f commit da3abd1
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 19 deletions.
4 changes: 2 additions & 2 deletions mirror/cli/save_test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ func main() {
}
decodedChunks = append(decodedChunks, &mirror.ChunkData{
Chunk: c, BlockNbts: nbts,
ChunkPos: define.ChunkPos{chunkPacket.X, chunkPacket.Z},
TimeStamp: time.Now().Unix(),
ChunkPos: define.ChunkPos{chunkPacket.X, chunkPacket.Z},
SyncTime: time.Now().Unix(),
})
}
provider, err := mcdb.New("testout", opt.FlateCompression)
Expand Down
10 changes: 5 additions & 5 deletions mirror/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ var TimeStampNotFound = time.Unix(0, 0).Unix()
type ChunkData struct {
Chunk *chunk.Chunk
BlockNbts map[define.CubePos]map[string]interface{}
TimeStamp int64
SyncTime int64
ChunkPos define.ChunkPos
}

func (cd *ChunkData) GetTime() time.Time {
return time.Unix(cd.TimeStamp, 0)
func (cd *ChunkData) GetSyncTime() time.Time {
return time.Unix(cd.SyncTime, 0)
}

func (cd *ChunkData) SetTime(t time.Time) {
cd.TimeStamp = t.Unix()
func (cd *ChunkData) SetSyncTime(t time.Time) {
cd.SyncTime = t.Unix()
}

type RidBlockWithNbt struct {
Expand Down
4 changes: 2 additions & 2 deletions mirror/io/assembler/assembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (o *Assembler) AddPendingTask(pk *packet.LevelChunk) (exist bool) {
o.pendingTasks[cp] = &mirror.ChunkData{
Chunk: chunk,
BlockNbts: make(map[define.CubePos]map[string]interface{}),
TimeStamp: time.Now().Unix(),
SyncTime: time.Now().Unix(),
ChunkPos: cp,
}
o.taskMu.Unlock()
Expand Down Expand Up @@ -119,7 +119,7 @@ func (o *Assembler) OnNewSubChunk(pk *packet.SubChunk) *mirror.ChunkData {
}
}
// fmt.Printf("pending %v\n", len(o.pendingTasks))
chunkData.TimeStamp = time.Now().Unix()
chunkData.SyncTime = time.Now().Unix()
//emptySubChunkCounter:=0
for _, subChunk := range subs {
if subChunk.Invalid() {
Expand Down
24 changes: 16 additions & 8 deletions mirror/io/lru/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ func (o *LRUMemoryChunkCacher) Get(pos define.ChunkPos) (data *mirror.ChunkData)
} else if o.FallBackProvider == nil {
return nil
} else {
return o.FallBackProvider.Get(pos)
cd := o.FallBackProvider.Get(pos)
o.memoryChunks[pos] = cd
o.cacheMap[pos] = time.Now()
o.checkCacheSizeAndHandleFallBackNoLock()
return cd
}
}

Expand All @@ -59,13 +63,7 @@ func (o *LRUMemoryChunkCacher) AdjustCacheLevel(level int) {
o.cacheLevel = level
}

func (o *LRUMemoryChunkCacher) Write(data *mirror.ChunkData) error {
o.mu.Lock()
defer o.mu.Unlock()
o.cacheMap[data.ChunkPos] = time.Now()
o.memoryChunks[data.ChunkPos] = data
// count++
// fmt.Println(count," ",pos)
func (o *LRUMemoryChunkCacher) checkCacheSizeAndHandleFallBackNoLock() {
if len(o.memoryChunks) > (1 << (o.cacheLevel + 1)) {
fmt.Println("release overflowed cached chunks")
cacheList := make(SortableTimes, 0)
Expand All @@ -85,6 +83,16 @@ func (o *LRUMemoryChunkCacher) Write(data *mirror.ChunkData) error {
delete(o.cacheMap, pair.p)
}
}
}

func (o *LRUMemoryChunkCacher) Write(data *mirror.ChunkData) error {
o.mu.Lock()
defer o.mu.Unlock()
o.cacheMap[data.ChunkPos] = time.Now()
o.memoryChunks[data.ChunkPos] = data
o.checkCacheSizeAndHandleFallBackNoLock()
// count++
// fmt.Println(count," ",pos)
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions mirror/io/mcdb/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (p *Provider) Get(pos define.ChunkPos) (data *mirror.ChunkData) {
}
}
}
cd.TimeStamp = p.loadTimeStamp(pos)
cd.SyncTime = p.loadTimeStamp(pos)
return cd
}

Expand Down Expand Up @@ -246,7 +246,7 @@ func (p *Provider) Write(cd *mirror.ChunkData) error {
if err := p.saveBlockNBT(cd.ChunkPos, serializedNbt); err != nil {
return err
}
if err := p.saveTimeStamp(cd.ChunkPos, cd.TimeStamp); err != nil {
if err := p.saveTimeStamp(cd.ChunkPos, cd.SyncTime); err != nil {
return err
}
return nil
Expand Down

0 comments on commit da3abd1

Please sign in to comment.