From 08e5cd80ccfd9a4f00e0a61da8435f7d070d47b4 Mon Sep 17 00:00:00 2001 From: Nicholas Novak <34256932+NickyBoy89@users.noreply.github.com> Date: Sun, 29 Oct 2023 01:23:38 -0700 Subject: [PATCH] fix: Fixed decoding for empty files and added temporary directories to tests --- basic_functionality_test.go | 28 +++++++++++++++++++++++++--- storage/file_operations.go | 4 ++++ storage/simple_server.go | 7 ++++++- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/basic_functionality_test.go b/basic_functionality_test.go index f2186cb..4e84796 100644 --- a/basic_functionality_test.go +++ b/basic_functionality_test.go @@ -1,7 +1,9 @@ package main import ( + "fmt" "math/rand" + "os" "testing" "time" @@ -9,18 +11,36 @@ import ( "git.nicholasnovak.io/nnovak/spatial-db/world" ) +func setupStorageDir() string { + dir, err := os.MkdirTemp("", "spatial-db-persistence") + if err != nil { + panic(err) + } + + fmt.Printf("Temporary directory is at %s\n", dir) + + storage.ChunkFileDirectory = dir + + return dir +} + func BenchmarkInsertSomePoints(b *testing.B) { var server storage.SimpleServer + stdDev := 65536 + + storage.ChunkFileDirectory = setupStorageDir() + defer os.RemoveAll(storage.ChunkFileDirectory) + points := make([]world.BlockPos, b.N) r := rand.New(rand.NewSource(time.Now().UnixNano())) for i := 0; i < b.N; i++ { points[i] = world.BlockPos{ - X: int(r.NormFloat64()), - Y: uint(r.NormFloat64()), - Z: int(r.NormFloat64()), + X: int(r.NormFloat64() * float64(stdDev)), + Y: uint(r.NormFloat64() * float64(stdDev)), + Z: int(r.NormFloat64() * float64(stdDev)), } } @@ -31,4 +51,6 @@ func BenchmarkInsertSomePoints(b *testing.B) { b.Error(err) } } + + fmt.Println(os.ReadDir(storage.ChunkFileDirectory)) } diff --git a/storage/file_operations.go b/storage/file_operations.go index c7a191c..77d5108 100644 --- a/storage/file_operations.go +++ b/storage/file_operations.go @@ -7,6 +7,10 @@ import ( "git.nicholasnovak.io/nnovak/spatial-db/world" ) +var ( + ChunkFileDirectory = "./persistence" +) + func ReadChunkFromFile(chunkFile *os.File) (world.ChunkData, error) { var chunkData world.ChunkData diff --git a/storage/simple_server.go b/storage/simple_server.go index 79cb7c8..738b1fa 100644 --- a/storage/simple_server.go +++ b/storage/simple_server.go @@ -5,6 +5,7 @@ import ( "errors" "io/fs" "os" + "path/filepath" "git.nicholasnovak.io/nnovak/spatial-db/world" ) @@ -17,7 +18,7 @@ type SimpleServer struct { // Filesystem operations func (s *SimpleServer) FetchChunk(pos world.ChunkPos) (world.ChunkData, error) { - chunkFileName := pos.ToFileName() + chunkFileName := filepath.Join(ChunkFileDirectory, pos.ToFileName()) var chunkData world.ChunkData @@ -34,6 +35,10 @@ func (s *SimpleServer) FetchChunk(pos world.ChunkPos) (world.ChunkData, error) { if err := json.NewEncoder(chunkFile).Encode(chunkData); err != nil { return chunkData, err } + + if _, err := chunkFile.Seek(0, 0); err != nil { + return chunkData, err + } } else { return chunkData, err }