feat: Moved over all of the servers to use unity files

This commit is contained in:
Nicholas Novak 2023-12-11 16:43:10 -08:00
parent 07b676c571
commit 5ad8bd2407
3 changed files with 24 additions and 11 deletions

View File

@ -13,7 +13,13 @@ type HashServer struct {
func (hs *HashServer) SetStorageRoot(path string) { func (hs *HashServer) SetStorageRoot(path string) {
hs.blocks = make(map[world.BlockPos]world.BlockID) hs.blocks = make(map[world.BlockPos]world.BlockID)
chunks, err := storage.ReadParallelFromDirectory(path) u, err := storage.OpenUnityFile(path, path+".metadata")
if err != nil {
panic(err)
}
defer u.Close()
chunks, err := u.ReadAllChunks()
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -18,7 +18,13 @@ type InMemoryServer struct {
func (s *InMemoryServer) SetStorageRoot(path string) { func (s *InMemoryServer) SetStorageRoot(path string) {
s.StorageDir = path s.StorageDir = path
chunks, err := storage.ReadParallelFromDirectory(s.StorageDir) u, err := storage.OpenUnityFile(s.StorageDir, s.StorageDir+".metadata")
if err != nil {
panic(err)
}
defer u.Close()
chunks, err := u.ReadAllChunks()
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -14,13 +14,18 @@ import (
const fileCacheSize = 8 const fileCacheSize = 8
type SimpleServer struct { type SimpleServer struct {
StorageDir string StorageDir string
cache storage.FileCache storageBackend storage.UnityFile
} }
func (s *SimpleServer) SetStorageRoot(path string) { func (s *SimpleServer) SetStorageRoot(path string) {
s.StorageDir = path s.StorageDir = path
s.cache = storage.NewFileCache(256)
var err error
s.storageBackend, err = storage.OpenUnityFile(path, path+".metadata")
if err != nil {
panic(err)
}
} }
// Filesystem operations // Filesystem operations
@ -58,11 +63,7 @@ func (s *SimpleServer) FetchOrCreateChunk(pos world.ChunkPos) (world.ChunkData,
// `FetchChunk' fetches the chunk's data, given the chunk's position // `FetchChunk' fetches the chunk's data, given the chunk's position
func (s *SimpleServer) FetchChunk(pos world.ChunkPos) (world.ChunkData, error) { func (s *SimpleServer) FetchChunk(pos world.ChunkPos) (world.ChunkData, error) {
chunkFileName := filepath.Join(s.StorageDir, pos.ToFileName()) chunkData, err := s.storageBackend.ReadChunk(pos)
var chunkData world.ChunkData
chunkFile, err := s.cache.FetchFile(chunkFileName)
if err != nil { if err != nil {
if errors.Is(err, fs.ErrNotExist) { if errors.Is(err, fs.ErrNotExist) {
return chunkData, storage.ChunkNotFoundError return chunkData, storage.ChunkNotFoundError
@ -71,7 +72,7 @@ func (s *SimpleServer) FetchChunk(pos world.ChunkPos) (world.ChunkData, error) {
} }
} }
return storage.ReadChunkFromFile(chunkFile) return chunkData, nil
} }
// Voxel server implementation // Voxel server implementation