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) {
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 {
panic(err)
}

View File

@ -18,7 +18,13 @@ type InMemoryServer struct {
func (s *InMemoryServer) SetStorageRoot(path string) {
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 {
panic(err)
}

View File

@ -15,12 +15,17 @@ const fileCacheSize = 8
type SimpleServer struct {
StorageDir string
cache storage.FileCache
storageBackend storage.UnityFile
}
func (s *SimpleServer) SetStorageRoot(path string) {
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
@ -58,11 +63,7 @@ func (s *SimpleServer) FetchOrCreateChunk(pos world.ChunkPos) (world.ChunkData,
// `FetchChunk' fetches the chunk's data, given the chunk's position
func (s *SimpleServer) FetchChunk(pos world.ChunkPos) (world.ChunkData, error) {
chunkFileName := filepath.Join(s.StorageDir, pos.ToFileName())
var chunkData world.ChunkData
chunkFile, err := s.cache.FetchFile(chunkFileName)
chunkData, err := s.storageBackend.ReadChunk(pos)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
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