-
Notifications
You must be signed in to change notification settings - Fork 1
/
controller.go
143 lines (117 loc) · 3.07 KB
/
controller.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package ckydb
import (
"log"
"sync"
"time"
"github.com/sopherapps/ckydb/implementations/go-ckydb/internal"
)
var (
ErrAlreadyRunning = internal.ErrAlreadyRunning
ErrNotRunning = internal.ErrNotRunning
ErrNotFound = internal.ErrNotFound
ErrCorruptedData = internal.ErrCorruptedData
ErrOutOfBounds = internal.ErrOutOfBounds
)
type Controller interface {
Open() error
Close() error
Set(key string, value string) error
Get(key string) (string, error)
Delete(key string) error
Clear() error
}
type Ckydb struct {
tasks []internal.Worker
store internal.Storage
vacuumIntervalSec float64
isOpen bool
mutLock sync.Mutex
}
// Connect creates a new Ckydb instance, starts its background tasks and returns it
func Connect(dbPath string, maxFileSizeKB float64, vacuumIntervalSec float64) (*Ckydb, error) {
db, err := newCkydb(dbPath, maxFileSizeKB, vacuumIntervalSec)
if err != nil {
return nil, err
}
err = db.Open()
if err != nil {
return nil, err
}
return db, nil
}
// newCkydb creates a new instance of Ckydb. This is used internally.
// Use Connect() for external code
func newCkydb(dbPath string, maxFileSizeKB float64, vacuumIntervalSec float64) (*Ckydb, error) {
store := internal.NewStore(dbPath, maxFileSizeKB)
err := store.Load()
if err != nil {
return nil, err
}
db := Ckydb{
tasks: make([]internal.Worker, 0),
store: store,
vacuumIntervalSec: vacuumIntervalSec,
isOpen: false,
}
return &db, nil
}
// Open initializes all background tasks
func (c *Ckydb) Open() error {
if c.isOpen {
return nil
}
vacuumTask := internal.NewTask(time.Second*time.Duration(c.vacuumIntervalSec), func() {
c.mutLock.Lock()
defer c.mutLock.Unlock()
err := c.store.Vacuum()
if err != nil {
log.Printf("error: %s", err)
}
})
err := vacuumTask.Start()
if err != nil {
return err
}
c.tasks = append(c.tasks, vacuumTask)
c.isOpen = true
return nil
}
// Close stops any background tasks
func (c *Ckydb) Close() error {
if !c.isOpen {
return nil
}
for _, task := range c.tasks {
err := task.Stop()
if err != nil {
return err
}
}
c.isOpen = false
return nil
}
// Set adds or updates the value corresponding to the given key in store
// It might return an ErrCorruptedData error but if it succeeds, no error is returned
func (c *Ckydb) Set(key string, value string) error {
c.mutLock.Lock()
defer c.mutLock.Unlock()
return c.store.Set(key, value)
}
// Get retrieves the value corresponding to the given key
// It returns a ErrNotFound error if the key is nonexistent
func (c *Ckydb) Get(key string) (string, error) {
return c.store.Get(key)
}
// Delete removes the key-value pair corresponding to the passed key
// It returns an ErrNotFound error if the key is nonexistent
func (c *Ckydb) Delete(key string) error {
c.mutLock.Lock()
defer c.mutLock.Unlock()
return c.store.Delete(key)
}
// Clear resets the entire Store, and clears everything on disk
func (c *Ckydb) Clear() error {
c.mutLock.Lock()
defer c.mutLock.Unlock()
return c.store.Clear()
}