fix: Fixed decoding for empty files and added temporary directories to tests

This commit is contained in:
Nicholas Novak 2023-10-29 01:23:38 -07:00
parent 511d494cd9
commit 08e5cd80cc
3 changed files with 35 additions and 4 deletions

View File

@ -1,7 +1,9 @@
package main package main
import ( import (
"fmt"
"math/rand" "math/rand"
"os"
"testing" "testing"
"time" "time"
@ -9,18 +11,36 @@ import (
"git.nicholasnovak.io/nnovak/spatial-db/world" "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) { func BenchmarkInsertSomePoints(b *testing.B) {
var server storage.SimpleServer var server storage.SimpleServer
stdDev := 65536
storage.ChunkFileDirectory = setupStorageDir()
defer os.RemoveAll(storage.ChunkFileDirectory)
points := make([]world.BlockPos, b.N) points := make([]world.BlockPos, b.N)
r := rand.New(rand.NewSource(time.Now().UnixNano())) r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
points[i] = world.BlockPos{ points[i] = world.BlockPos{
X: int(r.NormFloat64()), X: int(r.NormFloat64() * float64(stdDev)),
Y: uint(r.NormFloat64()), Y: uint(r.NormFloat64() * float64(stdDev)),
Z: int(r.NormFloat64()), Z: int(r.NormFloat64() * float64(stdDev)),
} }
} }
@ -31,4 +51,6 @@ func BenchmarkInsertSomePoints(b *testing.B) {
b.Error(err) b.Error(err)
} }
} }
fmt.Println(os.ReadDir(storage.ChunkFileDirectory))
} }

View File

@ -7,6 +7,10 @@ import (
"git.nicholasnovak.io/nnovak/spatial-db/world" "git.nicholasnovak.io/nnovak/spatial-db/world"
) )
var (
ChunkFileDirectory = "./persistence"
)
func ReadChunkFromFile(chunkFile *os.File) (world.ChunkData, error) { func ReadChunkFromFile(chunkFile *os.File) (world.ChunkData, error) {
var chunkData world.ChunkData var chunkData world.ChunkData

View File

@ -5,6 +5,7 @@ import (
"errors" "errors"
"io/fs" "io/fs"
"os" "os"
"path/filepath"
"git.nicholasnovak.io/nnovak/spatial-db/world" "git.nicholasnovak.io/nnovak/spatial-db/world"
) )
@ -17,7 +18,7 @@ type SimpleServer struct {
// Filesystem operations // Filesystem operations
func (s *SimpleServer) FetchChunk(pos world.ChunkPos) (world.ChunkData, error) { func (s *SimpleServer) FetchChunk(pos world.ChunkPos) (world.ChunkData, error) {
chunkFileName := pos.ToFileName() chunkFileName := filepath.Join(ChunkFileDirectory, pos.ToFileName())
var chunkData world.ChunkData 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 { if err := json.NewEncoder(chunkFile).Encode(chunkData); err != nil {
return chunkData, err return chunkData, err
} }
if _, err := chunkFile.Seek(0, 0); err != nil {
return chunkData, err
}
} else { } else {
return chunkData, err return chunkData, err
} }