forked from sourcegraph/srclib
/
units_index.go
76 lines (61 loc) · 1.7 KB
/
units_index.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
package store
import (
"encoding/json"
"fmt"
"io"
"sourcegraph.com/sourcegraph/srclib/unit"
)
// unitsIndex makes it fast list all source units without needing to
// traverse directories.
type unitsIndex struct {
units []*unit.SourceUnit
ready bool
}
var _ interface {
Index
persistedIndex
unitIndexBuilder
unitFullIndex
} = (*unitsIndex)(nil)
var c_unitsIndex_listUnits = &counter{count: new(int64)}
func (x *unitsIndex) String() string { return fmt.Sprintf("unitsIndex(ready=%v)", x.ready) }
// Covers returns -1 because it should never be selected over a more
// specific index. When the indexed stores use the unitsIndex, they do
// so through a different code path from the one that selects other
// indexes.
func (x *unitsIndex) Covers(filters interface{}) int { return -1 }
// Units implements unitFullIndex.
func (x *unitsIndex) Units(fs ...UnitFilter) ([]*unit.SourceUnit, error) {
if x.units == nil {
panic("units not built/read")
}
c_unitsIndex_listUnits.increment()
var units []*unit.SourceUnit
for _, u := range x.units {
if unitFilters(fs).SelectUnit(u) {
units = append(units, u)
}
}
return units, nil
}
// Build implements unitIndexBuilder.
func (x *unitsIndex) Build(units []*unit.SourceUnit) error {
x.units = units
x.ready = true
return nil
}
// Write implements persistedIndex.
func (x *unitsIndex) Write(w io.Writer) error {
if x.units == nil {
panic("no units to write")
}
return json.NewEncoder(w).Encode(x.units)
}
// Read implements persistedIndex.
func (x *unitsIndex) Read(r io.Reader) error {
err := json.NewDecoder(r).Decode(&x.units)
x.ready = (err == nil)
return err
}
// Ready implements persistedIndex.
func (x *unitsIndex) Ready() bool { return x.ready }