-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdriver.go
More file actions
46 lines (39 loc) · 724 Bytes
/
driver.go
File metadata and controls
46 lines (39 loc) · 724 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package dantdb
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"sync"
)
const (
extension = ".json"
)
var (
ErrNoCollection = errors.New("missing collection")
ErrNoResource = errors.New("missing resource")
)
type Driver struct {
mutex sync.RWMutex
mutexes map[string]*sync.Mutex
dir string
}
func New(dir string) (*Driver, error) {
dir = filepath.Clean(dir)
_, err := os.Stat(dir)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
err = os.MkdirAll(dir, 0755)
if err != nil {
return nil, fmt.Errorf("make dir: %w", err)
}
} else {
return nil, fmt.Errorf("get stats: %w", err)
}
}
return &Driver{
dir: dir,
mutexes: make(map[string]*sync.Mutex),
}, nil
}