2024-05-24 06:02:01 +00:00
|
|
|
package level
|
|
|
|
|
|
|
|
import (
|
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/balance"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
/* Functions to migrate Chunkers between different implementations. */
|
|
|
|
|
|
|
|
// OptimizeChunkerAccessors will evaluate all of the chunks of your drawing
|
|
|
|
// and possibly migrate them to a different Accessor implementation when
|
|
|
|
// saving on disk.
|
|
|
|
func (c *Chunker) OptimizeChunkerAccessors() {
|
2024-05-24 23:43:11 +00:00
|
|
|
// Parallelize this with goroutines.
|
2024-05-24 06:02:01 +00:00
|
|
|
var (
|
|
|
|
chunks = make(chan *Chunk, len(c.Chunks))
|
|
|
|
wg sync.WaitGroup
|
|
|
|
)
|
|
|
|
|
|
|
|
for range runtime.NumCPU() {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
for chunk := range chunks {
|
|
|
|
var point = chunk.Point
|
|
|
|
|
|
|
|
// Upgrade all MapTypes into RLE compressed MapTypes?
|
|
|
|
if balance.RLEBinaryChunkerEnabled {
|
|
|
|
if chunk.Type == MapType {
|
|
|
|
log.Info("Optimizing chunk %s accessor from Map to RLE", point)
|
|
|
|
ma, _ := chunk.Accessor.(*MapAccessor)
|
|
|
|
rle := NewRLEAccessor(chunk).FromMapAccessor(ma)
|
|
|
|
|
2024-05-24 22:03:32 +00:00
|
|
|
// Lock the chunker for updating.
|
|
|
|
c.chunkMu.Lock()
|
2024-05-24 06:02:01 +00:00
|
|
|
c.Chunks[point].Type = RLEType
|
|
|
|
c.Chunks[point].Accessor = rle
|
2024-05-24 22:03:32 +00:00
|
|
|
c.chunkMu.Unlock()
|
2024-05-24 06:02:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Feed it the chunks.
|
2024-05-24 20:54:41 +00:00
|
|
|
for point := range c.IterChunks() {
|
|
|
|
chunk, ok := c.GetChunk(point)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2024-05-24 06:02:01 +00:00
|
|
|
chunks <- chunk
|
|
|
|
}
|
|
|
|
|
|
|
|
close(chunks)
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromMapAccessor migrates from a MapAccessor to RLE.
|
|
|
|
func (a *RLEAccessor) FromMapAccessor(ma *MapAccessor) *RLEAccessor {
|
|
|
|
return &RLEAccessor{
|
|
|
|
chunk: a.chunk,
|
|
|
|
acc: ma,
|
|
|
|
}
|
|
|
|
}
|