-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.go
187 lines (143 loc) · 3.36 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
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
184
185
186
187
package immersadb
import (
"fmt"
"sync"
"github.com/draganm/immersadb/store"
"github.com/draganm/immersadb/wbbtree"
"github.com/pkg/errors"
)
type DB struct {
dataSegmentSize int
dataFanout int
root store.Address
st store.Store
txActive bool
dir string
mu sync.Mutex
}
// Database file layout:
// root - 8 bytes containing address of the root
// lx-id - layers 1-3
// transaction-id - layer 0
func Open(path string) (*DB, error) {
st, err := store.Open(path)
if err != nil {
return nil, errors.Wrap(err, "while opening store")
}
var root store.Address
if st.IsEmpty() {
_, err = wbbtree.CreateEmpty(st[1:])
if err != nil {
return nil, errors.Wrap(err, "while creating empty root")
}
}
root = st.Root()
return &DB{
root: root,
st: st,
dir: path,
dataSegmentSize: 256 * 1024,
dataFanout: 16,
}, nil
}
func (db *DB) NewReadTransaction() *ReadTransaction {
db.mu.Lock()
defer db.mu.Unlock()
db.st.StartUse()
return &ReadTransaction{
db.st,
db.root,
}
}
func (db *DB) NewTransaction() (*Transaction, error) {
db.mu.Lock()
defer db.mu.Unlock()
if db.txActive {
// TODO add waiting or optimistic tx here
return nil, errors.New("there is already a transaction in progress")
}
tx, err := newTransaction(db.st, db.root, db)
if err != nil {
return nil, errors.Wrap(err, "while creating transaction")
}
db.txActive = true
return tx, nil
}
func (db *DB) commit(l0 *store.SegmentFile, newRoot store.Address) error {
defer func() {
go l0.CloseAndDelete()
}()
db.mu.Lock()
defer db.mu.Unlock()
if !db.txActive {
return errors.New("cannot commit, no transaction was active")
}
db.txActive = false
if newRoot == db.root {
return nil
}
txStore := make(store.Store, len(db.st))
copy(txStore, db.st)
txStore[0] = l0
newDBRoot, ns, err := txStore.Commit(newRoot)
if err != nil {
return errors.Wrap(err, "while commiting transaction")
}
// TODO close the old store diff
// fmt.Println("new root", newDBRoot)
db.root = newDBRoot
for i := range ns {
if db.st[i] != ns[i] {
go db.st[i].CloseAndDelete()
}
}
txStore.FinishUse()
ns.FinishUse()
db.st = ns
return nil
}
func (db *DB) rollback(l0 *store.SegmentFile) error {
defer l0.CloseAndDelete()
db.mu.Lock()
defer db.mu.Unlock()
if !db.txActive {
return errors.New("cannot rollback, no transaction was active")
}
db.txActive = false
return nil
}
func (db *DB) Close() error {
db.mu.Lock()
defer db.mu.Unlock()
return db.st.Close()
}
func (db *DB) Transaction(f func(tx *Transaction) error) error {
tx, err := db.NewTransaction()
if err != nil {
return errors.Wrap(err, "while creating transaction")
}
err = f(tx)
if err != nil {
rbErr := tx.Rollback()
if err != nil {
return errors.Wrap(rbErr, "while rolling back transaction")
}
return err
}
return tx.Commit()
}
func (db *DB) PrintStats() {
db.mu.Lock()
defer db.mu.Unlock()
rs := db.st.GetSegment(db.root)
fmt.Println("total data size", rs.GetTotalTreeSize(), "bytes")
for i := 1; i < 4; i++ {
ub := db.st[i].UsedBytes()
lts := rs.GetLayerTotalSize(i)
garbagePercent := 0.0
if ub > 0 {
garbagePercent = 100.0 * float64(ub-lts) / float64(ub)
}
fmt.Printf("Layer %d: size %d, used %d, garbage %d bytes (%0.2f%%)\n", i, ub, lts, ub-lts, garbagePercent)
}
}