github.com/web-platform-tests/wpt.fyi@v0.0.0-20240530210107-70cf978996f1/api/query/cache/index/tests.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 10 farm "github.com/dgryski/go-farm" 11 ) 12 13 // TestID is a unique identifier for a WPT test or sub-test. 14 type TestID struct { 15 testID uint64 16 subID uint64 17 } 18 19 // Tests is an indexing component that provides fast test name lookup by TestID. 20 type Tests interface { 21 // Add adds a new named test/subtest to a tests index, using the given TestID 22 // to identify the test/subtest in the index. 23 Add(TestID, string, *string) 24 // GetName retrieves the name/subtest name associated with a given TestID. If 25 // the index does not recognize the TestID, then an error is returned. 26 GetName(TestID) (string, *string, error) 27 28 Range(func(TestID) bool) 29 } 30 31 // Tests is an indexing component that provides fast test name lookup by TestID. 32 type testsMap struct { 33 tests map[TestID]testName 34 } 35 36 type testName struct { 37 name string 38 subName *string 39 } 40 41 // NewTests constructs an empty Tests instance. 42 // nolint:ireturn // TODO: Fix ireturn lint error 43 func NewTests() Tests { 44 return &testsMap{tests: make(map[TestID]testName)} 45 } 46 47 func (ts *testsMap) Add(t TestID, name string, subName *string) { 48 ts.tests[t] = testName{name, subName} 49 } 50 51 func (ts *testsMap) GetName(id TestID) (string, *string, error) { 52 name, ok := ts.tests[id] 53 if !ok { 54 return "", nil, fmt.Errorf(`Test not found; ID: %v`, id) 55 } 56 57 return name.name, name.subName, nil 58 } 59 60 func (ts *testsMap) Range(f func(TestID) bool) { 61 for t := range ts.tests { 62 if !f(t) { 63 break 64 } 65 } 66 } 67 68 func computeTestID(name string, subPtr *string) (TestID, error) { 69 var s uint64 70 t := farm.Fingerprint64([]byte(name)) 71 if subPtr != nil && *subPtr != "" { 72 s = farm.Fingerprint64([]byte(*subPtr)) 73 if s == 0 { 74 return TestID{}, fmt.Errorf(`Subtest ID for string "%s" is 0`, *subPtr) 75 } 76 } 77 78 return TestID{t, s}, nil 79 }