cmd/pkgserver: use IR cache for ident
This removes requirement to open full cache. Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
@@ -118,11 +118,9 @@ func TestAPIGet(t *testing.T) {
|
|||||||
checkStatus(t, resp, http.StatusOK)
|
checkStatus(t, resp, http.StatusOK)
|
||||||
checkAPIHeader(t, w.Header())
|
checkAPIHeader(t, w.Header())
|
||||||
checkPayloadFunc(t, resp, func(got *struct {
|
checkPayloadFunc(t, resp, func(got *struct {
|
||||||
Count int `json:"count"`
|
|
||||||
Values []*metadata `json:"values"`
|
Values []*metadata `json:"values"`
|
||||||
}) bool {
|
}) bool {
|
||||||
return got.Count == len(want) &&
|
return slices.EqualFunc(got.Values, want, func(a, b *metadata) bool {
|
||||||
slices.EqualFunc(got.Values, want, func(a, b *metadata) bool {
|
|
||||||
return (a.Version == b.Version ||
|
return (a.Version == b.Version ||
|
||||||
a.Version == rosa.Unversioned ||
|
a.Version == rosa.Unversioned ||
|
||||||
b.Version == rosa.Unversioned) &&
|
b.Version == rosa.Unversioned) &&
|
||||||
@@ -136,15 +134,15 @@ func TestAPIGet(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
checkWithSuffix("declarationAscending", "?limit=2&index=0&sort=0", []*metadata{
|
checkWithSuffix("declarationAscending", "?limit=2&index=1&sort=0", []*metadata{
|
||||||
{
|
|
||||||
Metadata: rosa.GetMetadata(0),
|
|
||||||
Version: rosa.Std.Version(0),
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
Metadata: rosa.GetMetadata(1),
|
Metadata: rosa.GetMetadata(1),
|
||||||
Version: rosa.Std.Version(1),
|
Version: rosa.Std.Version(1),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Metadata: rosa.GetMetadata(2),
|
||||||
|
Version: rosa.Std.Version(2),
|
||||||
|
},
|
||||||
})
|
})
|
||||||
checkWithSuffix("declarationAscending offset", "?limit=3&index=5&sort=0", []*metadata{
|
checkWithSuffix("declarationAscending offset", "?limit=3&index=5&sort=0", []*metadata{
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ type metadata struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// populate deterministically populates packageIndex, optionally with a report.
|
// populate deterministically populates packageIndex, optionally with a report.
|
||||||
func (index *packageIndex) populate(cache *pkg.Cache, report *rosa.Report) (err error) {
|
func (index *packageIndex) populate(report *rosa.Report) (err error) {
|
||||||
if report != nil {
|
if report != nil {
|
||||||
defer report.HandleAccess(&err)()
|
defer report.HandleAccess(&err)()
|
||||||
index.handleAccess = report.HandleAccess
|
index.handleAccess = report.HandleAccess
|
||||||
@@ -58,6 +58,7 @@ func (index *packageIndex) populate(cache *pkg.Cache, report *rosa.Report) (err
|
|||||||
|
|
||||||
var work [rosa.PresetUnexportedStart]*metadata
|
var work [rosa.PresetUnexportedStart]*metadata
|
||||||
index.names = make(map[string]*metadata)
|
index.names = make(map[string]*metadata)
|
||||||
|
ir := pkg.NewIR()
|
||||||
for p := range rosa.PresetUnexportedStart {
|
for p := range rosa.PresetUnexportedStart {
|
||||||
m := metadata{
|
m := metadata{
|
||||||
p: p,
|
p: p,
|
||||||
@@ -72,8 +73,8 @@ func (index *packageIndex) populate(cache *pkg.Cache, report *rosa.Report) (err
|
|||||||
m.Version = ""
|
m.Version = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
if cache != nil && report != nil {
|
if report != nil {
|
||||||
id := cache.Ident(rosa.Std.Load(p))
|
id := ir.Ident(rosa.Std.Load(p))
|
||||||
m.ids = pkg.Encode(id.Value())
|
m.ids = pkg.Encode(id.Value())
|
||||||
m.status, m.Size = report.ArtifactOf(id)
|
m.status, m.Size = report.ArtifactOf(id)
|
||||||
m.HasReport = m.Size >= 0
|
m.HasReport = m.Size >= 0
|
||||||
|
|||||||
14
cmd/pkgserver/index_test.go
Normal file
14
cmd/pkgserver/index_test.go
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
// newIndex returns the address of a newly populated packageIndex.
|
||||||
|
func newIndex(t *testing.T) *packageIndex {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
var index packageIndex
|
||||||
|
if err := index.populate(nil); err != nil {
|
||||||
|
t.Fatalf("populate: error = %v", err)
|
||||||
|
}
|
||||||
|
return &index
|
||||||
|
}
|
||||||
@@ -10,12 +10,9 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"hakurei.app/check"
|
|
||||||
"hakurei.app/cmd/pkgserver/internal/ui"
|
"hakurei.app/cmd/pkgserver/internal/ui"
|
||||||
"hakurei.app/command"
|
"hakurei.app/command"
|
||||||
"hakurei.app/internal/pkg"
|
|
||||||
"hakurei.app/internal/rosa"
|
"hakurei.app/internal/rosa"
|
||||||
"hakurei.app/message"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const shutdownTimeout = 15 * time.Second
|
const shutdownTimeout = 15 * time.Second
|
||||||
@@ -25,17 +22,14 @@ func main() {
|
|||||||
log.SetPrefix("pkgserver: ")
|
log.SetPrefix("pkgserver: ")
|
||||||
|
|
||||||
var (
|
var (
|
||||||
flagBaseDir string
|
|
||||||
flagAddr string
|
flagAddr string
|
||||||
)
|
)
|
||||||
|
|
||||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
|
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
|
||||||
defer stop()
|
defer stop()
|
||||||
msg := message.New(log.Default())
|
|
||||||
|
|
||||||
c := command.New(os.Stderr, log.Printf, "pkgserver", func(args []string) error {
|
c := command.New(os.Stderr, log.Printf, "pkgserver", func(args []string) error {
|
||||||
var (
|
var (
|
||||||
cache *pkg.Cache
|
|
||||||
report *rosa.Report
|
report *rosa.Report
|
||||||
)
|
)
|
||||||
switch len(args) {
|
switch len(args) {
|
||||||
@@ -43,17 +37,8 @@ func main() {
|
|||||||
break
|
break
|
||||||
|
|
||||||
case 1:
|
case 1:
|
||||||
baseDir, err := check.NewAbs(flagBaseDir)
|
var err error
|
||||||
if err != nil {
|
rosa.DropCaches(rosa.OptLLVMNoLTO)
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
cache, err = pkg.Open(ctx, msg, 0, 0, 0, baseDir)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer cache.Close()
|
|
||||||
|
|
||||||
report, err = rosa.OpenReport(args[0])
|
report, err = rosa.OpenReport(args[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -61,12 +46,11 @@ func main() {
|
|||||||
|
|
||||||
default:
|
default:
|
||||||
return errors.New("pkgserver requires 1 argument")
|
return errors.New("pkgserver requires 1 argument")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var index packageIndex
|
var index packageIndex
|
||||||
index.search = make(searchCache)
|
index.search = make(searchCache)
|
||||||
if err := index.populate(cache, report); err != nil {
|
if err := index.populate(report); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
ticker := time.NewTicker(1 * time.Minute)
|
ticker := time.NewTicker(1 * time.Minute)
|
||||||
@@ -98,10 +82,6 @@ func main() {
|
|||||||
}()
|
}()
|
||||||
return server.ListenAndServe()
|
return server.ListenAndServe()
|
||||||
}).Flag(
|
}).Flag(
|
||||||
&flagBaseDir,
|
|
||||||
"b", command.StringFlag(""),
|
|
||||||
"base directory for cache",
|
|
||||||
).Flag(
|
|
||||||
&flagAddr,
|
&flagAddr,
|
||||||
"addr", command.StringFlag(":8067"),
|
"addr", command.StringFlag(":8067"),
|
||||||
"TCP network address to listen on",
|
"TCP network address to listen on",
|
||||||
|
|||||||
@@ -10,17 +10,6 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
// newIndex returns the address of a newly populated packageIndex.
|
|
||||||
func newIndex(t *testing.T) *packageIndex {
|
|
||||||
t.Helper()
|
|
||||||
|
|
||||||
var index packageIndex
|
|
||||||
if err := index.populate(nil, nil); err != nil {
|
|
||||||
t.Fatalf("populate: error = %v", err)
|
|
||||||
}
|
|
||||||
return &index
|
|
||||||
}
|
|
||||||
|
|
||||||
// checkStatus checks response status code.
|
// checkStatus checks response status code.
|
||||||
func checkStatus(t *testing.T, resp *http.Response, want int) {
|
func checkStatus(t *testing.T, resp *http.Response, want int) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|||||||
Reference in New Issue
Block a user