Skip to content

Commit

Permalink
add non-locking support for plan9
Browse files Browse the repository at this point in the history
  • Loading branch information
tenox7 committed May 25, 2024
1 parent da5a3e0 commit 220bfc6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
28 changes: 28 additions & 0 deletions lock_all.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//go:build !plan9
// +build !plan9

package tkvs

import (
"os"
"sync"

"github.com/danjacques/gofslock/fslock"
)

type TKVS struct {
file *os.File
misErr error
lock *fslock.Handle
kvs KeyVal
sync.Mutex
}

func (t *TKVS) fsLock(path string) error {
l, err := fslock.Lock(path)
if err != nil {
return err
}
t.lock = &l
return nil
}
18 changes: 18 additions & 0 deletions lock_p9.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//go:build plan9
// +build plan9

package tkvs

import (
"os"
"sync"
)

type TKVS struct {
file *os.File
misErr error
kvs KeyVal
sync.Mutex
}

func (t *TKVS) fsLock(_ string) error { return nil }
16 changes: 3 additions & 13 deletions tkvs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,8 @@ import (
"io"
"log"
"os"
"sync"

"github.com/danjacques/gofslock/fslock"
)

type TKVS struct {
file *os.File
misErr error
lock *fslock.Handle
kvs KeyVal
sync.Mutex
}

type KeyVal map[string][]byte

type Container struct {
Expand Down Expand Up @@ -87,15 +76,16 @@ func (j *TKVS) Keys() []string {
}

func New(path string, misErr error) *TKVS {
l, err := fslock.Lock(path)
k := &TKVS{misErr: misErr}
err := k.fsLock(path)
if err != nil {
log.Fatalf("unable to lock %q: %v", path, err)
}
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
log.Fatalf("unable to open %q: %v", path, err)
}
k := &TKVS{file: f, misErr: misErr, lock: &l}
k.file = f
err = k.readJson()
if err != nil {
log.Fatalf("unable to read %q: %v", path, err)
Expand Down

0 comments on commit 220bfc6

Please sign in to comment.