fix: Fixed decoding for empty files and added temporary directories to tests
This commit is contained in:
parent
511d494cd9
commit
08e5cd80cc
@ -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))
|
||||
}
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user