-
Notifications
You must be signed in to change notification settings - Fork 5
/
fastdb.go
183 lines (142 loc) · 3.3 KB
/
fastdb.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package fastdb
/* ------------------------------- Imports --------------------------- */
import (
"errors"
"fmt"
"strconv"
"sync"
"github.com/marcelloh/fastdb/persist"
)
/* ---------------------- Constants/Types/Variables ------------------ */
// DB represents a collection of key-value pairs that persist on disk or memory.
type DB struct {
aof *persist.AOF
keys map[string]map[int][]byte
mu sync.RWMutex
}
/* -------------------------- Methods/Functions ---------------------- */
/*
Open opens a database at the provided path.
If the file doesn't exist, it will be created automatically.
If the path is ':memory:' then the database will be opened in memory only.
*/
func Open(path string, syncIime int) (*DB, error) {
var (
aof *persist.AOF
err error
)
keys := make(map[string]map[int][]byte)
if path != ":memory:" {
aof, keys, err = persist.OpenPersister(path, syncIime)
}
return &DB{aof: aof, keys: keys}, err //nolint:wrapcheck // it is already wrapped
}
/*
Defrag optimises the file to reflect the latest state.
*/
func (fdb *DB) Defrag() error {
fdb.mu.Lock()
defer fdb.mu.Unlock()
var err error
err = fdb.aof.Defrag(fdb.keys)
if err != nil {
err = fmt.Errorf("defrag error: %w", err)
}
return err
}
/*
Del deletes one map value in a bucket.
*/
func (fdb *DB) Del(bucket string, key int) (bool, error) {
var err error
fdb.mu.Lock()
defer fdb.mu.Unlock()
// bucket exists?
_, found := fdb.keys[bucket]
if !found {
return found, nil
}
// key exists in bucket?
_, found = fdb.keys[bucket][key]
if !found {
return found, nil
}
if fdb.aof != nil {
lines := "del\n" + bucket + "_" + strconv.Itoa(key) + "\n"
err = fdb.aof.Write(lines)
if err != nil {
return false, fmt.Errorf("del->write error: %w", err)
}
}
delete(fdb.keys[bucket], key)
if len(fdb.keys[bucket]) == 0 {
delete(fdb.keys, bucket)
}
return true, nil
}
/*
Get returns one map value from a bucket.
*/
func (fdb *DB) Get(bucket string, key int) ([]byte, bool) {
fdb.mu.RLock()
defer fdb.mu.RUnlock()
data, ok := fdb.keys[bucket][key]
return data, ok
}
/*
GetAll returns all map values from a bucket.
*/
func (fdb *DB) GetAll(bucket string) (map[int][]byte, error) {
fdb.mu.RLock()
defer fdb.mu.RUnlock()
bmap, found := fdb.keys[bucket]
if !found {
return nil, errors.New("bucket not found")
}
return bmap, nil
}
/*
Info returns info about the storage.
*/
func (fdb *DB) Info() string {
count := 0
for i := range fdb.keys {
count += len(fdb.keys[i])
}
return fmt.Sprintf("%d record(s) in %d bucket(s)", count, len(fdb.keys))
}
/*
Set stores one map value in a bucket.
*/
func (fdb *DB) Set(bucket string, key int, value []byte) error {
fdb.mu.Lock()
defer fdb.mu.Unlock()
if fdb.aof != nil {
lines := "set\n" + bucket + "_" + strconv.Itoa(key) + "\n" + string(value) + "\n"
err := fdb.aof.Write(lines)
if err != nil {
return fmt.Errorf("sel->write error: %w", err)
}
}
_, found := fdb.keys[bucket]
if !found {
fdb.keys[bucket] = map[int][]byte{}
}
fdb.keys[bucket][key] = value
return nil
}
/*
Close closes the database.
*/
func (fdb *DB) Close() error {
if fdb.aof != nil {
fdb.mu.Lock()
defer fdb.mu.Unlock()
err := fdb.aof.Close()
if err != nil {
return fmt.Errorf("close error: %w", err)
}
}
fdb.keys = make(map[string]map[int][]byte)
return nil
}