Skip to content

Commit

Permalink
Avoid weird mutex.
Browse files Browse the repository at this point in the history
  • Loading branch information
ncruces committed Jan 9, 2025
1 parent 7424747 commit 52b4108
Showing 1 changed file with 4 additions and 13 deletions.
17 changes: 4 additions & 13 deletions ext/serdes/serdes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package serdes

import (
"io"
"sync"

"github.com/ncruces/go-sqlite3"
"github.com/ncruces/go-sqlite3/vfs"
Expand All @@ -18,8 +17,7 @@ func init() {
// https://sqlite.org/c3ref/serialize.html
func Serialize(db *sqlite3.Conn, schema string) ([]byte, error) {
var file sliceFile
openMtx.Lock()
openFile = &file
fileToOpen <- &file
err := db.Backup(schema, "file:db?vfs="+vfsName)
return file.data, err
}
Expand All @@ -39,15 +37,11 @@ func Serialize(db *sqlite3.Conn, schema string) ([]byte, error) {
// ["memdb"]: https://pkg.go.dev/github.com/ncruces/go-sqlite3/vfs/memdb
// ["reader"]: https://pkg.go.dev/github.com/ncruces/go-sqlite3/vfs/readervfs
func Deserialize(db *sqlite3.Conn, schema string, data []byte) error {
openMtx.Lock()
openFile = &sliceFile{data}
fileToOpen <- &sliceFile{data}
return db.Restore(schema, "file:db?vfs="+vfsName)
}

var (
openMtx sync.Mutex
openFile *sliceFile
)
var fileToOpen = make(chan *sliceFile, 1)

const vfsName = "github.com/ncruces/go-sqlite3/ext/deserialize.sliceVFS"

Expand All @@ -59,10 +53,7 @@ func (sliceVFS) Open(name string, flags vfs.OpenFlag) (vfs.File, vfs.OpenFlag, e
return nil, flags, sqlite3.CANTOPEN
}

file := openFile
openFile = nil
openMtx.Unlock()

file := <-fileToOpen
if file.data != nil {
flags |= vfs.OPEN_READONLY
}
Expand Down

0 comments on commit 52b4108

Please sign in to comment.