-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
184 lines (155 loc) · 3.38 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
package edb
import (
"fmt"
"slices"
"strings"
"sync"
"sync/atomic"
"time"
"go.etcd.io/bbolt"
)
const debugTrackTxns = false
type DB struct {
bdb *bbolt.DB
schema *Schema
logf func(format string, args ...any)
verbose bool
strict bool
tableStates []*tableState
lastSize atomic.Int64
ReaderCount atomic.Int64
WriterCount atomic.Int64
PendingWriterCount atomic.Int64
ReadCount atomic.Uint64
WriteCount atomic.Uint64
txns []*Tx
txnsLock sync.Mutex
}
type Options struct {
Logf func(format string, args ...any)
Verbose bool
IsTesting bool
MmapSize int
NoPersistentFreeList bool
}
func Open(path string, schema *Schema, opt Options) (*DB, error) {
bopt := &bbolt.Options{
Timeout: 10 * time.Second,
}
*bopt = *bbolt.DefaultOptions
if opt.IsTesting {
bopt.NoSync = true
bopt.NoFreelistSync = true
bopt.InitialMmapSize = 1024 * 1024 * 5
} else {
bopt.InitialMmapSize = 1024 * 1024 * 1024
bopt.FreelistType = bbolt.FreelistMapType
}
if opt.MmapSize != 0 {
bopt.InitialMmapSize = opt.MmapSize
}
if opt.NoPersistentFreeList {
bopt.NoFreelistSync = true
}
start := time.Now()
bdb, err := bbolt.Open(path, 0666, bopt)
if err != nil {
return nil, fmt.Errorf("kvdb: %w", err)
}
if elapsed := time.Since(start); elapsed >= 5*time.Millisecond {
if opt.Logf != nil {
opt.Logf("db: bbolt open took %d ms", elapsed.Milliseconds())
}
}
db := &DB{
bdb: bdb,
schema: schema,
logf: opt.Logf,
verbose: opt.Verbose,
tableStates: make([]*tableState, len(schema.tables)),
strict: opt.IsTesting,
}
db.Write(func(tx *Tx) {
now := time.Now()
for i, tbl := range schema.tables {
db.tableStates[i] = prepareTable(tx, tbl, now)
}
for _, tbl := range schema.kvtables {
prepareKVTable(tx, tbl)
}
for _, mp := range schema.maps {
prepareMap(tx, mp)
}
for _, ts := range db.tableStates {
ts.migrate(tx)
}
for _, ts := range db.tableStates {
ts.save(tx)
}
})
return db, nil
}
func (db *DB) Bolt() *bbolt.DB {
return db.bdb
}
func (db *DB) Size() int64 {
return db.lastSize.Load()
}
func (db *DB) Close() {
err := db.bdb.Close()
if err != nil {
panic(fmt.Errorf("kvdb: closing: %w", err))
}
}
func (db *DB) addTx(tx *Tx) {
db.txnsLock.Lock()
defer db.txnsLock.Unlock()
db.txns = append(db.txns, tx)
}
func (db *DB) removeTx(tx *Tx) {
db.txnsLock.Lock()
defer db.txnsLock.Unlock()
found := -1
for i, t := range db.txns {
if t == tx {
found = i
break
}
}
if found < 0 {
panic("tx not found in list")
}
n := len(db.txns)
db.txns[found] = db.txns[n-1]
db.txns[n-1] = nil // ensure it gets collected
db.txns = db.txns[:n-1]
}
func (db *DB) DescribeOpenTxns() string {
if !debugTrackTxns {
return "OPEN TX TRACKING DISABLED"
}
db.txnsLock.Lock()
txns := slices.Clone(db.txns)
db.txnsLock.Unlock()
if len(txns) == 0 {
return "NO OPEN TRANSACTIONS"
}
slices.SortFunc(txns, func(a, b *Tx) int {
return a.startTime.Compare(b.startTime)
})
now := time.Now()
var buf strings.Builder
fmt.Fprintf(&buf, "%d OPEN TRANSACTIONS:\n", len(txns))
for _, tx := range txns {
ms := now.Sub(tx.startTime).Milliseconds()
if ms < 100 {
fmt.Fprintf(&buf, "\n---\nopen for %d ms\n", ms)
} else {
fmt.Fprintf(&buf, "\n---\nopen for %d ms:\n%s", ms, tx.stack)
}
}
return buf.String()
}
func Foo() int64 {
return 42
}