-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.go
89 lines (68 loc) · 1.81 KB
/
db.go
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package gocask
import (
"github.com/aneshas/gocask/core"
"github.com/aneshas/gocask/internal/fs"
"os"
"path"
"time"
)
const (
// KB represents base2 kilobyte
KB int64 = 1024
// MB represents base2 megabyte
MB = KB * 1024
// GB represents base2 gigabyte
GB = MB * 1024
// TB represents base2 terabyte
TB = GB * 1024
)
// Open opens an existing database at dbPath or creates a new one
// The database location can be configured with config options and the default is ~/gcdata
// Magic in:mem:db value for dbPath can be used in order to instantiate an in memory file system
// which can be used for testing purposes.
func Open(dbPath string, opts ...Option) (*core.DB, error) {
var caskFS core.FS
caskFS = fs.NewDisk()
if dbPath == core.InMemoryDB {
caskFS = fs.NewInMemory()
}
var t goTime
home, err := os.UserHomeDir()
if err != nil {
return nil, err
}
cfg := core.Config{
MaxDataFileSize: 10 * GB,
DataDir: path.Join(home, "gcdata"),
}
for _, opt := range opts {
cfg = opt(cfg)
}
db, err := core.NewDB(dbPath, caskFS, t, cfg)
if err != nil {
return nil, err
}
return db, nil
}
// Option represents gocask configuration option
type Option func(config core.Config) core.Config
// WithMaxDataFileSize configures maximum data file size after which
// data files will be rotated
func WithMaxDataFileSize(bytes int64) Option {
return func(config core.Config) core.Config {
config.MaxDataFileSize = bytes
return config
}
}
// WithDataDir configures the location of the data dir where your databases will reside
func WithDataDir(path string) Option {
return func(config core.Config) core.Config {
config.DataDir = path
return config
}
}
type goTime struct{}
// NowUnix returns current unix timestamp
func (t goTime) NowUnix() uint32 {
return uint32(time.Now().UTC().Unix())
}