From 42cad2cecc5a57be0b90cc015ebc0fb31ccddacd Mon Sep 17 00:00:00 2001 From: Nicholas Novak <34256932+NickyBoy89@users.noreply.github.com> Date: Sat, 18 Nov 2023 19:47:09 -0800 Subject: [PATCH] change: Fixed some tests and alternate implementations --- loading/load_region.go | 3 ++- scaling_test.go | 18 ++++++++---------- storage/hashserver.go | 41 ++++++++++++++++++++++++++++++++++++++++- world/chunk_data.go | 8 ++++++++ 4 files changed, 58 insertions(+), 12 deletions(-) diff --git a/loading/load_region.go b/loading/load_region.go index 96812d9..0c246ca 100644 --- a/loading/load_region.go +++ b/loading/load_region.go @@ -1,6 +1,7 @@ package loading import ( + "path/filepath" "strconv" "strings" @@ -20,7 +21,7 @@ func LoadRegionFile(fileName string) ([]world.ChunkData, error) { defer regionFile.Close() // Parse the name of the region to find its position within the world - nameParts := strings.Split(fileName, ".") + nameParts := strings.Split(filepath.Base(fileName), ".") regionX, err := strconv.Atoi(nameParts[1]) if err != nil { return nil, err diff --git a/scaling_test.go b/scaling_test.go index 65e9bb6..f340b75 100644 --- a/scaling_test.go +++ b/scaling_test.go @@ -7,12 +7,14 @@ import ( "git.nicholasnovak.io/nnovak/spatial-db/world" ) +var server storage.SimpleServer + +func init() { + server.SetStorageRoot("skygrid-save") +} + // insertPointTemplate inserts a configurable variety of points into the server func insertPointTemplate(testDir string, b *testing.B, pointSpread int) { - var server storage.InMemoryServer - - server.SetStorageRoot(testDir) - b.ResetTimer() for i := 0; i < b.N; i++ { @@ -24,10 +26,6 @@ func insertPointTemplate(testDir string, b *testing.B, pointSpread int) { } func fetchChunkTemplate(testDir string, b *testing.B) { - var server storage.SimpleServer - - server.SetStorageRoot(testDir) - b.ResetTimer() for i := 0; i < b.N; i++ { @@ -41,9 +39,9 @@ func fetchChunkTemplate(testDir string, b *testing.B) { // Insert blocks func BenchmarkInsertClusteredPoints(b *testing.B) { - insertPointTemplate("test-world", b, 128) + insertPointTemplate("imperial-test", b, 128) } func BenchmarkInsertSparserPoints(b *testing.B) { - insertPointTemplate("test-world", b, 2048) + insertPointTemplate("imperial-test", b, 2048) } diff --git a/storage/hashserver.go b/storage/hashserver.go index 17134a2..c65ef25 100644 --- a/storage/hashserver.go +++ b/storage/hashserver.go @@ -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) { diff --git a/world/chunk_data.go b/world/chunk_data.go index ba19723..a9d4915 100644 --- a/world/chunk_data.go +++ b/world/chunk_data.go @@ -15,6 +15,14 @@ func (cd *ChunkData) SectionFor(pos BlockPos) *ChunkSection { return &cd.Sections[pos.Y%ChunkSectionCount] } +func (cd *ChunkData) IndexToBlockPos(index int) BlockPos { + return BlockPos{ + X: index%16 + cd.Pos.X, + Y: uint(index % sliceSize), + Z: index%32 + cd.Pos.Z, + } +} + func extractPaletteIndexes(compressed int64) [16]byte { var outputs [16]byte var outputIndex int