Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip own search #584

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions indexdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"math/bits"
"unicode/utf8"

"github.com/sourcegraph/zoekt/own"
"github.com/sourcegraph/zoekt/query"
)

Expand Down Expand Up @@ -100,6 +101,9 @@ type indexData struct {

// rawConfigMasks contains the encoded RawConfig for each repository
rawConfigMasks []uint8

// own per repo
own []own.Own
}

type symbolData struct {
Expand Down
15 changes: 15 additions & 0 deletions matchtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,21 @@ func (d *indexData) newMatchTree(q query.Q, opt matchTreeOpt) (matchTree, error)
},
}, nil

case *query.Own:
var compiled []func([]byte) bool
for _, o := range d.own {
compiled = append(compiled, o.CompileForAuthor(s.SearchTerm))
}

return &docMatchTree{
reason: "Own",
numDocs: d.numDocs(),
predicate: func(docID uint32) bool {
fileName := d.fileName(docID)
return compiled[d.repos[docID]](fileName)
},
}, nil

case *query.BranchesRepos:
reposBranchesWant := make([]uint64, len(d.repoMetaData))
for repoIdx := range d.repoMetaData {
Expand Down
42 changes: 42 additions & 0 deletions own/own.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package own

import (
"bytes"
"fmt"
"os"
)

func Load(ownPath string) (Own, error) {
b, err := os.ReadFile(ownPath)
if err != nil {
return nil, fmt.Errorf("failed to read owner file: %w", err)
}

owners := make(own)
fields := bytes.Fields(b)
for i := 0; i < len(fields); i += 2 {
owners[string(fields[i])] = fields[i+1]
}
return owners, nil
}

var Empty = make(own)

type Own interface {
// This is the way to go until we are proven it is too slow.
CompileForAuthor(author string) func(path []byte) bool
}

type own map[string][]byte

func (o own) CompileForAuthor(author string) func(path []byte) bool {
pattern, ok := o[author]
if ok {
return func(path []byte) bool {
return bytes.Contains(path, pattern)
}
}
return func(path []byte) bool {
return false
}
}
8 changes: 8 additions & 0 deletions query/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ func parseExpr(in []byte) (Q, int, error) {
}

expr = &Symbol{q}

case tokOwn:
// TODO support any and none
expr = &Own{SearchTerm: text}

case tokParenClose:
// Caller must consume paren.
expr = nil
Expand Down Expand Up @@ -393,6 +398,7 @@ const (
tokArchived = 15
tokPublic = 16
tokFork = 17
tokOwn = 18
)

var tokNames = map[int]string{
Expand All @@ -404,6 +410,7 @@ var tokNames = map[int]string{
tokFork: "Fork",
tokNegate: "Negate",
tokOr: "Or",
tokOwn: "Own",
tokParenClose: "ParenClose",
tokParenOpen: "ParenOpen",
tokPublic: "Public",
Expand All @@ -425,6 +432,7 @@ var prefixes = map[string]int{
"f:": tokFile,
"file:": tokFile,
"fork:": tokFork,
"own:": tokOwn,
"public:": tokPublic,
"r:": tokRepo,
"regex:": tokRegex,
Expand Down
3 changes: 3 additions & 0 deletions query/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ func TestParseQuery(t *testing.T) {
{"type:file abc def", &Type{Type: TypeFileName, Child: NewAnd(&Substring{Pattern: "abc"}, &Substring{Pattern: "def"})}},
{"(type:repo abc) def", NewAnd(&Type{Type: TypeRepo, Child: &Substring{Pattern: "abc"}}, &Substring{Pattern: "def"})},

// own
{"foo own:cezary", NewAnd(&Substring{Pattern: "foo"}, &Own{SearchTerm: "cezary"})},

// errors.
{"--", nil},
{"\"abc", nil},
Expand Down
20 changes: 20 additions & 0 deletions query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,26 @@ func (q *GobCache) String() string {
return fmt.Sprintf("GobCache(%s)", q.Q)
}

// Own searches ownership. The fields are mutually exclusive.
type Own struct {
SearchTerm string
NoOwner bool
AnyOwner bool
}

func (q *Own) String() string {
if q.SearchTerm != "" {
return fmt.Sprintf("(own %q)", q.SearchTerm)
}
if q.NoOwner {
return "(own none)"
}
if q.AnyOwner {
return "(own any)"
}
return "(own malformed)"
}

// Or is matched when any of its children is matched.
type Or struct {
Children []Q
Expand Down
15 changes: 15 additions & 0 deletions read.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ import (
"fmt"
"hash/crc64"
"log"
"net/url"
"os"
"path/filepath"
"sort"

"github.com/rs/xid"
"github.com/sourcegraph/zoekt/own"
)

// IndexFile is a file suitable for concurrent read access. For performance
Expand Down Expand Up @@ -397,6 +400,18 @@ func (r *reader) readIndexData(toc *indexTOC) (*indexData, error) {
}
}

{
dir := filepath.Dir(r.r.Name())
for _, md := range d.repoMetaData {
o, err := own.Load(filepath.Join(dir, url.QueryEscape(md.Name)) + ".own")
if err != nil {
log.Printf("ignoring error for loading own for %s: %s", md.Name, err)
o = own.Empty
}
d.own = append(d.own, o)
}
}

if d.metaData.IndexFormatVersion >= 17 {
blob, err := d.readSectionBlob(toc.repos)
if err != nil {
Expand Down