change: Fixed some tests and alternate implementations

This commit is contained in:
Nicholas Novak
2023-11-18 19:47:09 -08:00
parent 70879ec312
commit 42cad2cecc
4 changed files with 58 additions and 12 deletions

View File

@@ -1,6 +1,14 @@
package storage
import "git.nicholasnovak.io/nnovak/spatial-db/world"
import (
"encoding/json"
"os"
"path/filepath"
log "github.com/sirupsen/logrus"
"git.nicholasnovak.io/nnovak/spatial-db/world"
)
type HashServer struct {
blocks map[world.BlockPos]world.BlockID
@@ -8,6 +16,37 @@ type HashServer struct {
func (hs *HashServer) SetStorageRoot(path string) {
hs.blocks = make(map[world.BlockPos]world.BlockID)
chunkFiles, err := os.ReadDir(path)
if err != nil {
panic(err)
}
for chunkIndex, chunkFile := range chunkFiles {
var data world.ChunkData
log.Infof("Reading in chunk %d of %d", chunkIndex, len(chunkFiles))
f, err := os.Open(filepath.Join(path, chunkFile.Name()))
if err != nil {
panic(err)
}
// Read each file from disk
if err := json.NewDecoder(f).Decode(&data); err != nil {
panic(err)
}
// Load in each data point from disk
for _, section := range data.Sections {
for blockIndex, blockState := range section.BlockStates {
pos := data.IndexToBlockPos(blockIndex)
hs.blocks[pos] = blockState
}
}
f.Close()
}
}
func (hs *HashServer) FetchChunk(pos world.ChunkPos) (world.ChunkData, error) {