-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfacts.go
274 lines (232 loc) · 7.97 KB
/
facts.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package kyc
import (
"context"
"encoding/base64"
"encoding/json"
"github.com/bmatcuk/doublestar/v4"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/mergestat/kyc/pkg/scanner"
"go.riyazali.net/sqlite"
"os"
)
type Fact struct {
Commit *object.Commit
File *object.File
Scanner string
Key string
Value any
}
const (
ColumnRepository = iota // pointer to the git.Repository object
ColumnCommit // hash of the commit in the repository
ColumnFileName // name of the file from which the fact was extracted
ColumnFileBlob // git blob hash of the file
ColumnScanner // name of the scanner used
ColumnFactKey // identifier for fact type
ColumnFactValue // extracted fact value
)
const (
_ = iota
OpEqual // op code for equals-to operation
OpLike // op code for LIKE operation
OpGlob // op code for GLOB operation
OpLte // op code for <= operation
OpGte // op code for >= operation
)
// FactModule implements sqlite.Module interface for fact() table-valued function.
type FactModule struct{}
func (mod *FactModule) Connect(_ *sqlite.Conn, _ []string, declare func(string) error) (_ sqlite.VirtualTable, err error) {
const query = `
CREATE TABLE facts (
repository HIDDEN,
commit_hash TEXT,
file_name TEXT,
file_blob TEXT,
scanner TEXT,
key TEXT,
value
)`
if err = declare(query); err != nil {
return nil, err
}
return &FactTable{}, nil
}
// FactTable implements sqlite.VirtualTable interface for fact() table-valued function.
type FactTable struct{}
func (table *FactTable) BestIndex(input *sqlite.IndexInfoInput) (*sqlite.IndexInfoOutput, error) {
var argv = 1
var bitmap []byte
var set = func(op, col int) { bitmap = append(bitmap, byte(op<<4|col)) }
var output = &sqlite.IndexInfoOutput{
ConstraintUsage: make([]*sqlite.ConstraintUsage, len(input.Constraints)),
}
var commitConstrained = false
for i, cons := range input.Constraints {
switch col, op := cons.ColumnIndex, cons.Op; col {
case ColumnCommit:
{
if op == sqlite.INDEX_CONSTRAINT_EQ && cons.Usable {
output.ConstraintUsage[i] = &sqlite.ConstraintUsage{ArgvIndex: argv, Omit: true}
commitConstrained, argv = true, argv+1
set(OpEqual, col)
}
}
case ColumnFileName:
{
if op == sqlite.INDEX_CONSTRAINT_GE || op == sqlite.INDEX_CONSTRAINT_LT {
// sometimes sqlite core try to infer the glob pattern, and uses >= and <
// operations to hint to the virtual table implementation that it can skip
// "certain rows" using the range specified by >= and <
//
// For example, if we specify "a/**/*.go" as glob pattern, the sqlite core "knows"
// that we can skip all values that do not start with "a/" and hence it'll use the range
// operators to signal that.
//
// We do not support it at the moment and so we simply ignore these constraints.
//
// TODO(@riyaz): look into how we can use these constraints to improve tree traversal performance
continue
}
if op != sqlite.INDEX_CONSTRAINT_EQ && op != sqlite.INDEX_CONSTRAINT_GLOB {
return nil, sqlite.Error(sqlite.SQLITE_CONSTRAINT, "only equals-to and GLOB operations are supported on file_name")
}
if !cons.Usable {
// TODO(@riyaz): make this error more user-friendly
return nil, sqlite.Error(sqlite.SQLITE_CONSTRAINT, "file_name constraint must be usable")
}
output.ConstraintUsage[i] = &sqlite.ConstraintUsage{ArgvIndex: argv, Omit: true}
argv += 1
if op == sqlite.INDEX_CONSTRAINT_EQ {
set(OpEqual, col)
} else {
set(OpGlob, col)
}
}
case ColumnScanner:
{
if op != sqlite.INDEX_CONSTRAINT_EQ && op != sqlite.INDEX_CONSTRAINT_LIKE {
return nil, sqlite.Error(sqlite.SQLITE_CONSTRAINT, "only equals-to and LIKE operations are supported on scanner")
}
if !cons.Usable {
// TODO(@riyaz): make this error more user-friendly
return nil, sqlite.Error(sqlite.SQLITE_CONSTRAINT, "scanner constraint must be usable")
}
// TODO(@riyaz): set omit to true once the constraints are implemented below
output.ConstraintUsage[i] = &sqlite.ConstraintUsage{ArgvIndex: argv, Omit: false}
argv += 1
if op == sqlite.INDEX_CONSTRAINT_EQ {
set(OpEqual, col)
} else {
set(OpLike, col)
}
}
}
}
if !commitConstrained {
return nil, sqlite.Error(sqlite.SQLITE_CONSTRAINT, "commit hash is required")
}
// pass the bitmap as string to xFilter routine
output.IndexString = base64.StdEncoding.EncodeToString(bitmap)
return output, nil
}
func (table *FactTable) Open() (sqlite.VirtualCursor, error) { return &FactCursor{}, nil }
func (table *FactTable) Disconnect() error { return nil }
func (table *FactTable) Destroy() error { return nil }
// FactCursor implements sqlite.VirtualCursor interface for fact() table-valued function.
type FactCursor struct {
repo *git.Repository
pos int
facts []*Fact
}
func (cur *FactCursor) Filter(_ int, str string, values ...sqlite.Value) (err error) {
var ctx = context.Background()
var cwd string
if cwd, err = os.Getwd(); err != nil {
return err
}
var repo *git.Repository
if repo, err = git.PlainOpen(cwd); err != nil {
return err
}
var commit *object.Commit
var scanners = scanner.All()
var glob = func(_ *object.File) bool { return true } // default glob func is a no-op
var bitmap, _ = base64.StdEncoding.DecodeString(str)
for n, val := range values {
op, col := (bitmap[n]&0b11110000)>>4, bitmap[n]&0b00001111
switch {
case col == ColumnCommit && op == OpEqual:
{
if !plumbing.IsHash(val.Text()) {
return sqlite.Error(sqlite.SQLITE_ERROR, "invalid commit hash")
}
if commit, err = repo.CommitObject(plumbing.NewHash(val.Text())); err != nil {
return sqlite.Error(sqlite.SQLITE_ERROR, err.Error())
}
}
case col == ColumnFileName && (op == OpEqual || op == OpGlob):
{
glob = globFunc(val.Text()) // works for both OpEqual and OpGlob
}
case col == ColumnScanner:
{
// TODO(@riyaz): implement constraint on scanner column
}
}
}
var tree *object.Tree
if tree, err = commit.Tree(); err != nil {
return sqlite.Error(sqlite.SQLITE_ERROR, err.Error())
}
// TODO(@riyaz): explore async options for file scanning
// iterate over all files in the tree, and run all scanners against each file
err = tree.Files().ForEach(func(file *object.File) error {
for name, scn := range scanners {
if glob(file) && scn.Supports(file) {
var facts []scanner.Fact
if facts, err = scn.Scan(ctx, file); err != nil {
return err
}
for i := range facts {
key, val := facts[i].Key, facts[i].Value
cur.facts = append(cur.facts, &Fact{Commit: commit, File: file, Scanner: name, Key: key, Value: val})
}
}
}
return nil
})
cur.repo = repo
return err
}
func (cur *FactCursor) Column(context *sqlite.VirtualTableContext, pos int) error {
var fact = cur.facts[cur.pos]
switch pos {
case ColumnRepository:
context.ResultPointer(cur.repo)
case ColumnCommit:
context.ResultText(fact.Commit.ID().String())
case ColumnFileName:
context.ResultText(fact.File.Name)
case ColumnFileBlob:
context.ResultText(fact.File.ID().String())
case ColumnScanner:
context.ResultText(fact.Scanner)
case ColumnFactKey:
context.ResultText(fact.Key)
case ColumnFactValue:
var j, _ = json.Marshal(fact.Value)
context.ResultBlob(j)
context.ResultSubType(74)
}
return nil
}
func (cur *FactCursor) Next() error { cur.pos += 1; return nil }
func (cur *FactCursor) Rowid() (int64, error) { return int64(cur.pos), nil }
func (cur *FactCursor) Eof() bool { return cur.pos >= len(cur.facts) }
func (cur *FactCursor) Close() error { return nil }
// creates a glob pattern matching function
func globFunc(pattern string) func(*object.File) bool {
return func(file *object.File) bool { var match, _ = doublestar.PathMatch(pattern, file.Name); return match }
}