github.com/web-platform-tests/wpt.fyi@v0.0.0-20240530210107-70cf978996f1/api/query/cache/index/results.go (about) 1 // Copyright 2018 The WPT Dashboard Project. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package index 6 7 import ( 8 "fmt" 9 "sync" 10 11 "github.com/web-platform-tests/wpt.fyi/shared" 12 ) 13 14 // RunID is a unique identifier for a WPT test run. These IDs are generated when 15 // a run is committed to Datastore. 16 type RunID int64 17 18 // ResultID is a unique identifier for a WPT test result/status. RunID values 19 // are documented in github.com/web-platform-tests/wpt.fyi/shared TestStatus* 20 // values. 21 type ResultID int64 22 23 // Results is an interface for an index that stores RunID => RunResults 24 // mappings. 25 type Results interface { 26 // Add stores a RunID => RunResults mapping. 27 Add(RunID, RunResults) error 28 // Delete deletes all result data for a particular WPT test run. 29 Delete(RunID) error 30 // ForRun produces a RunResults interface for a particular WPT test run, or 31 // nil if the input RunID is unknown to this index. 32 ForRun(RunID) RunResults 33 34 // nolint:godox // TODO: Add filter binding function: 35 // ResultFilter(ru RunID, re ResultID) UnboundFilter 36 } 37 38 // RunResults is an interface for an index that stores a TestID => ResultID 39 // mapping. 40 type RunResults interface { 41 // Add stores a TestID => ResultID mapping. 42 Add(ResultID, TestID) 43 // GetResult looks up the ResultID associated with a TestID; the 44 // "status unknown" value is used if the lookup yields no ResultID. 45 GetResult(TestID) ResultID 46 } 47 48 type resultsMap struct { 49 byRunTest sync.Map 50 } 51 52 type runResultsMap struct { 53 byTest map[TestID]ResultID 54 } 55 56 // NewResults generates a new empty results index. 57 // nolint:ireturn // TODO: Fix ireturn lint error 58 func NewResults() Results { 59 return &resultsMap{byRunTest: sync.Map{}} 60 } 61 62 // NewRunResults generates a new empty run results index. 63 // nolint:ireturn // TODO: Fix ireturn lint error 64 func NewRunResults() RunResults { 65 return &runResultsMap{make(map[TestID]ResultID)} 66 } 67 68 func (rs *resultsMap) Add(ru RunID, rr RunResults) error { 69 _, wasLoaded := rs.byRunTest.LoadOrStore(ru, rr) 70 if wasLoaded { 71 return fmt.Errorf("Already loaded into results index: %v", ru) 72 } 73 74 return nil 75 } 76 77 func (rs *resultsMap) Delete(ru RunID) error { 78 _, ok := rs.byRunTest.Load(ru) 79 if !ok { 80 return fmt.Errorf(`No such run in results index; run ID: %v`, ru) 81 } 82 83 rs.byRunTest.Delete(ru) 84 85 return nil 86 } 87 88 // nolint:ireturn // TODO: Fix ireturn lint error 89 func (rs *resultsMap) ForRun(ru RunID) RunResults { 90 v, ok := rs.byRunTest.Load(ru) 91 if !ok { 92 return nil 93 } 94 rrm := v.(*runResultsMap) 95 96 return rrm 97 } 98 99 func (rrs *runResultsMap) Add(re ResultID, t TestID) { 100 rrs.byTest[t] = re 101 } 102 103 func (rrs *runResultsMap) GetResult(t TestID) ResultID { 104 re, ok := rrs.byTest[t] 105 if !ok { 106 return ResultID(shared.TestStatusUnknown) 107 } 108 109 return re 110 }