feat: Added parallel init to inmemory server

This commit is contained in:
Nicholas Novak 2023-12-10 20:44:48 -08:00
parent 2219c34c35
commit 6d45421601

View File

@ -7,6 +7,7 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"git.nicholasnovak.io/nnovak/spatial-db/storage"
"git.nicholasnovak.io/nnovak/spatial-db/world" "git.nicholasnovak.io/nnovak/spatial-db/world"
) )
@ -25,22 +26,35 @@ func (s *InMemoryServer) SetStorageRoot(path string) {
s.Chunks = make(map[world.ChunkPos]world.ChunkData) s.Chunks = make(map[world.ChunkPos]world.ChunkData)
validChunkFiles := []fs.DirEntry{}
for _, chunkFile := range chunkFiles { for _, chunkFile := range chunkFiles {
if chunkFile.IsDir() || !strings.HasSuffix(chunkFile.Name(), ".chunk") { if chunkFile.IsDir() || !strings.HasSuffix(chunkFile.Name(), ".chunk") {
continue continue
} }
file, err := os.Open(filepath.Join(s.StorageDir, chunkFile.Name())) validChunkFiles = append(validChunkFiles, chunkFile)
if err != nil { }
panic(err)
}
chunkData, err := ReadChunkFromFile(file) chunks := make([]world.ChunkData, len(validChunkFiles))
if err != nil {
panic(err)
}
file.Close() for chunkIndex, chunkFile := range validChunkFiles {
go func(index int, cf fs.DirEntry) {
file, err := os.Open(filepath.Join(s.StorageDir, cf.Name()))
if err != nil {
panic(err)
}
chunkData, err := storage.ReadChunkFromFile(file)
if err != nil {
panic(err)
}
file.Close()
chunks[index] = chunkData
}(chunkIndex, chunkFile)
}
for _, chunkData := range chunks {
s.Chunks[chunkData.Pos] = chunkData s.Chunks[chunkData.Pos] = chunkData
} }
} }
@ -57,14 +71,14 @@ func (s *InMemoryServer) FetchChunk(pos world.ChunkPos) (world.ChunkData, error)
chunkFile, err := os.Open(chunkFileName) chunkFile, err := os.Open(chunkFileName)
if err != nil { if err != nil {
if errors.Is(err, fs.ErrNotExist) { if errors.Is(err, fs.ErrNotExist) {
return chunkData, ChunkNotFoundError return chunkData, storage.ChunkNotFoundError
} else { } else {
return chunkData, err return chunkData, err
} }
} }
defer chunkFile.Close() defer chunkFile.Close()
return ReadChunkFromFile(chunkFile) return storage.ReadChunkFromFile(chunkFile)
} }
// Voxel server implementation // Voxel server implementation