Improved data safety

This commit is contained in:
Eduard Urbach 2023-07-08 19:08:28 +02:00
parent 20e7c596be
commit d410df2d7e
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
2 changed files with 13 additions and 6 deletions

View File

@ -198,7 +198,7 @@ func BenchmarkColdStart(b *testing.B) {
defer users.Sync() defer users.Sync()
defer users.Clear() defer users.Clear()
b.Run("100 records", func(b *testing.B) { b.Run("100", func(b *testing.B) {
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
users.Set(strconv.Itoa(i), &User{Name: fmt.Sprintf("User %d", i)}) users.Set(strconv.Itoa(i), &User{Name: fmt.Sprintf("User %d", i)})
} }

View File

@ -7,9 +7,9 @@ import (
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"sort" "sort"
"sync/atomic" "sync/atomic"
"time"
) )
type FileStorage[T any] struct { type FileStorage[T any] struct {
@ -55,7 +55,7 @@ func (fs *FileStorage[T]) Sync() {
func (fs *FileStorage[T]) flushWorker() { func (fs *FileStorage[T]) flushWorker() {
for { for {
runtime.Gosched() time.Sleep(time.Millisecond)
if fs.dirty.Swap(0) == 0 { if fs.dirty.Swap(0) == 0 {
select { select {
@ -75,8 +75,9 @@ func (fs *FileStorage[T]) flushWorker() {
} }
func (fs *FileStorage[T]) flush() error { func (fs *FileStorage[T]) flush() error {
fileName := filepath.Join(fs.collection.root, fs.collection.name+".dat") oldPath := filepath.Join(fs.collection.root, fs.collection.name+".dat")
file, err := os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600) newPath := oldPath + ".tmp"
file, err := os.OpenFile(newPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
if err != nil { if err != nil {
return err return err
@ -104,7 +105,13 @@ func (fs *FileStorage[T]) flush() error {
return err return err
} }
return file.Close() err = file.Close()
if err != nil {
return err
}
return os.Rename(newPath, oldPath)
} }
// readFrom reads the entire collection. // readFrom reads the entire collection.