/
gc.go
220 lines (180 loc) · 3.67 KB
/
gc.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package main
import (
"context"
"database/sql"
"errors"
sqlite3 "github.com/mattn/go-sqlite3"
mcq "github.com/mediachain/concat/mc/query"
pb "github.com/mediachain/concat/proto"
multihash "github.com/multiformats/go-multihash"
)
var (
NodeMustBeOffline = errors.New("Node must be offline")
)
func (node *Node) doGC(ctx context.Context) (int, error) {
if node.status != StatusOffline {
return 0, NodeMustBeOffline
}
gc := &GCDB{}
err := gc.Open(node.home)
if err != nil {
return 0, err
}
defer gc.Close()
err = gc.Merge(ctx, node.db)
if err != nil {
return 0, err
}
return gc.GC(ctx, node.ds)
}
func (node *Node) doCompact() error {
node.ds.Compact()
return nil
}
type GCDB struct {
db *sql.DB
insertKey *sql.Stmt
countKeys *sql.Stmt
}
func (gc *GCDB) Open(home string) error {
db, err := sql.Open("sqlite3", "") // use temporary db
if err != nil {
return err
}
gc.db = db
_, err = db.Exec("CREATE TABLE Refs (key VARCHAR(64) PRIMARY KEY)")
if err != nil {
return err
}
insertKey, err := db.Prepare("INSERT INTO Refs VALUES (?)")
if err != nil {
return err
}
gc.insertKey = insertKey
countKeys, err := db.Prepare("SELECT COUNT(1) FROM Refs WHERE key = ?")
if err != nil {
return err
}
gc.countKeys = countKeys
return nil
}
func (gc *GCDB) Close() error {
return gc.db.Close()
}
func (gc *GCDB) Merge(ctx context.Context, db StatementDB) error {
q, err := mcq.ParseQuery("SELECT * FROM *")
if err != nil {
return err
}
ch, err := db.QueryStream(ctx, q)
if err != nil {
return err
}
const batch = 1024
keys := make(map[string]bool)
for val := range ch {
switch val := val.(type) {
case *pb.Statement:
gc.addKeys(val, keys)
if len(keys) >= batch {
err := gc.mergeKeys(keys)
if err != nil {
return err
}
keys = make(map[string]bool)
}
case StreamError:
return val
default:
return BadResult
}
}
if len(keys) > 0 {
return gc.mergeKeys(keys)
}
return nil
}
func (gc *GCDB) addKeys(stmt *pb.Statement, keys map[string]bool) error {
switch body := stmt.Body.Body.(type) {
case *pb.StatementBody_Simple:
gc.addSimpleKeys(body.Simple, keys)
return nil
case *pb.StatementBody_Compound:
ss := body.Compound.Body
for _, s := range ss {
gc.addSimpleKeys(s, keys)
}
return nil
case *pb.StatementBody_Envelope:
stmts := body.Envelope.Body
for _, stmt := range stmts {
err := gc.addKeys(stmt, keys)
if err != nil {
return err
}
}
return nil
default:
return BadStatementBody
}
}
func (gc *GCDB) addSimpleKeys(s *pb.SimpleStatement, keys map[string]bool) {
keys[s.Object] = true
for _, dep := range s.Deps {
keys[dep] = true
}
}
func (gc *GCDB) mergeKeys(keys map[string]bool) error {
tx, err := gc.db.Begin()
if err != nil {
return err
}
insertKey := tx.Stmt(gc.insertKey)
for key, _ := range keys {
_, err := insertKey.Exec(key)
if err != nil {
xerr, ok := err.(sqlite3.Error)
if ok && xerr.ExtendedCode == sqlite3.ErrConstraintPrimaryKey {
continue
}
tx.Rollback()
return err
}
}
return tx.Commit()
}
func (gc *GCDB) GC(ctx context.Context, ds Datastore) (count int, err error) {
keys, err := ds.IterKeys(ctx)
if err != nil {
return
}
for key := range keys {
var valid bool
valid, err = gc.validKey(key)
if err != nil {
return
}
if valid {
continue
}
err = ds.Delete(key)
if err != nil {
return
}
count += 1
}
if count > 0 {
ds.Compact()
}
return
}
func (gc *GCDB) validKey(key Key) (bool, error) {
key58 := multihash.Multihash(key).B58String()
row := gc.countKeys.QueryRow(key58)
var count int
err := row.Scan(&count)
if err != nil {
return false, err
}
return count > 0, nil
}