search

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package search is the logic of the GET searches (decision 0012, research 22 §2): it builds their items queries, scopes them to the repositories a repos parameter names, runs them a page at a time through the store's AQL path with the caller's permissions applied to each page, caps what they return, and reads Maven coordinates through each repository's layout for the GAVC, versions and latest version searches. Every query here is an aql.Query, so the searches compile to each store's dialect as AQL does. For those three layout searches it also discovers what the remote repositories' upstreams hold beyond their caches (UpstreamSearch), from authoritative Maven metadata or bounded folder listings.

Role

artifacts module, core layer: the GET searches' query building, scoping, paging, permission filtering and layout and version search, and the upstream search of the layout searches. The HTTP adapter keeps parameter binding, response rendering, the caller's access, the error labels' logging and the upstream search's route and disclosure policy; it hands this package what it knows through Access, Upstream, Routes, UpstreamDeps, the layouts function and SearchRun.Internal. The package never imports the module's adapters, an ecosystem http package or a composite store: Store names the two accessors a search reads.

Inputs

  • Store: the item search (AQL) and the repository configurations.
  • Limits: the user query limit and the AQL hard cap.
  • The caller (kernel.Principal) and Access, the adapter's view of the repositories they may see and the rows they may read.
  • The layouts function, which reads a repository layout by name, and, for a module search that also asks the remote upstreams, Upstream.
  • UpstreamDeps, for an upstream search: the remote sources, the item service whose layouts the remotes' discovery maps between, the adapter's Routes and its Fail hook.

Outputs

  • SearchRun, from NewSearch, whose Collect returns the hits a search's criteria find that the caller may read, in item order, up to a limit, and whose Pages walks the same hits a batch at a time for a search answer written as it is read.
  • The criteria the searches build: RepoScope, Files, All and Within.
  • LayoutScope and its Find, which return LayoutHit values with their coordinates; ModuleVersions, which returns VersionEntry values.
  • UpstreamSearch, from NewUpstreamSearch: the upstreams' files (Gavc) and versions (Versions), and Revalidate, the fence before the search answers.
  • RepoConfigs, the repositories by key, which the AQL handlers read too, and InvalidSearchMetadata, the 502 that the adapter's routes answer too.

Invariants

  • Results past a limit are dropped silently, since the searches' responses have no range to report it; UserLimit and Capped give the limits, and SearchPage the rows one store query reads.
  • An administrator reads every row; anyone else gets the rows Access.Readable keeps, page by page, so a page can yield fewer.
  • Every failure the caller cannot act on goes through SearchRun.Internal with its label ("search", "search permissions", "search layout"), which operators read in the log, so the labels stay as they are.
  • The 400s of ModuleSearch are apierror values whose text clients read.
  • Only an availability failure of an upstream whose remote repository does not hard-fail allows a partial result. Security, integrity, capability and superseded-source failures go to UpstreamDeps.Fail and never become an empty success.
  • Every path an upstream search names passes the adapter's route and read checks (Routes.Disclose), and Revalidate rechecks the reads, listings, routes and disclosures it relied on before the search answers. It downloads no binary, and its reads, listings and routes are bounded.

References

  • Decision 0012: docs/research/decisions/0012-search-and-aql.md, lines 314 to 357 (repos, what is searched, limits and the layout searches, with GAVC's upstream discovery at 354 to 357) and 382 to 394 (remote=1).
  • Decision 0007: docs/research/decisions/0007-remote-repositories.md (upstream reads go through the source boundary as the caller).
  • Decision 0008: docs/research/decisions/0008-virtual-repositories.md (a virtual key searches its members).
  • Research 22: docs/research/22-aql-searches-build-info.md, section 2 (the GET searches).
  • docs/operations/upstream-search.md: the upstream search's bounds and its failure, visibility and layout rules.
  • Decision 0044: docs/research/decisions/0044-modular-monolith-migration.md ("Dependency rules": core never imports adapters).
  • Target layout: docs/research/architecture-options/5-modular-monolith.md, lines 215 and 216 (core/search).
  • https://github.com/weaveplatform/artifactweave/issues/1079
  • https://github.com/weaveplatform/artifactweave/issues/1080

Status

Implemented P5 (the GET searches, their limits and the layout searches) and P9 (the upstream search, #213), as the server root's search handlers. Added in P9d, which cut the logic out of the handlers into this package.

Testing

TestAccUnder checks the path narrowing of the layout searches. The search suites under test/server/artifacts drive the searches over HTTP, through the adapter and this package: the store and layout failures whose labels SearchRun.Internal carries, and the upstream searches against fake upstreams (the searches_upstream*_test.go suites).

Index

Constants

View Source
const SearchPage = 1000

SearchPage is how many rows one store query reads. Permission filtering runs on each page, so a search keeps reading until it holds its limit of readable results or the rows run out.

Variables

This section is empty.

Functions

func All

func All(nodes ...aql.Node) aql.And

All is the conjunction of the criteria given, skipping nil ones.

func Files

func Files() aql.Node

Files is the criterion that leaves folders out: a hand-built query has no implicit type, unlike a parsed one.

func HasWildcard

func HasWildcard(v string) bool

HasWildcard reports whether a v parameter is a pattern, which filters the listed versions, rather than the module version whose integration versions to list.

func InvalidSearchMetadata

func InvalidSearchMetadata() error

InvalidSearchMetadata is the 502 integrity failure an upstream search answers when it cannot trust what an upstream returned or the routes it would name it under: malformed or oversized metadata, more metadata reads than the search allows, a snapshot no layout maps, a route cycle or more routes than the search allows.

func Matches

func Matches(re *regexp.Regexp, s string) bool

Matches reports whether s matches re; a nil re, Wildcard's for a missing parameter, matches anything.

func ModuleSearch

func ModuleSearch[R ~int](g, a *string, remote *R) error

ModuleSearch checks a versions or latest version search's parameters: a group or a module, and remote 0 or 1.

func RepoConfigs

func RepoConfigs(ctx context.Context, st Store) (map[string]repo.Config, error)

RepoConfigs returns every repository's configuration, by key. Its error is the store's listing error under "repositories: ".

func Wildcard

func Wildcard(p *string) *regexp.Regexp

Wildcard compiles a search parameter in which * is any text and ? one character; nil, for a parameter not given or empty, matches anything.

func Within

func Within(f aql.Field, from, to time.Time) aql.Node

Within is the criterion that a time field lies in [from, to].

Types

type Access

type Access interface {
	// Visible reports whether the caller may see c, which decides whether a
	// key in a search's repos parameter names a repository for them.
	Visible(c repo.Config) bool
	// Readable keeps the hits the caller may read, by repository and path,
	// that their repository's include and exclude patterns accept. configs
	// are the repositories by key. Its error is a failure to read the
	// caller's permissions.
	Readable(ctx context.Context, configs map[string]repo.Config, hits []aql.Hit) ([]aql.Hit, error)
}

Access is the caller's view of repositories, computed by the adapter from the request's permissions.

type LayoutHit

type LayoutHit struct {
	aql.Hit
	layout.Coordinates
}

LayoutHit is a file a layout search found, with its coordinates.

type LayoutScope

type LayoutScope struct {
	// Layouts are the layouts by stored repository key. A file whose
	// repository has none is never a hit.
	Layouts map[string]*layout.Layout
	// Criteria select the files in scope under the start of each layout.
	Criteria aql.Node
}

LayoutScope is what a layout search reads: the layout of each repository it may find files in, by stored key, and the criteria that narrow to them.

func (LayoutScope) Find

func (ls LayoutScope) Find(
	ctx context.Context,
	r *SearchRun,
	include []string,
	want int,
	keep func(layout.Coordinates) bool,
) ([]LayoutHit, error)

Find runs a layout search: the files in scope whose coordinates keep accepts, at most want of them (-1 for all).

type Limits

type Limits struct {
	UserQueryLimit, MaxResults int
	LimitInternalUsers         bool
}

Limits bound the GET searches. UserQueryLimit caps the results anonymous callers get, and every non-admin's when LimitInternalUsers is set, as Artifactory's artifactory.search.userQueryLimit and limitAnonymousUsersOnly do; MaxResults is the AQL hard cap, which bounds every search. Zero means no cap.

type Routes

type Routes interface {
	// Keys are the remote repositories the search asks, sorted.
	Keys() []string
	// Request is the source request for the remote repository key, with
	// the caller's identity and access.
	Request(key string) source.Request
	// RouteKey is the key, as the search's repos parameter named it, under
	// which the search may name full, a file or, with folder, a folder of
	// the repository key, cached saying whether it is the remote's cache;
	// empty when no route may name it.
	RouteKey(key, full string, folder bool, cached ...bool) string
	// ScopeAllowed reports whether the caller's request may read scope of
	// key and a route may name its folder.
	ScopeAllowed(key string, scope remote.DiscoveryScope) bool
	// Allowed reports whether a route may name full in key, the
	// repository's include and exclude patterns accept it and the caller
	// may read it, without recording it as disclosed.
	Allowed(key, full string, folder bool) bool
	// Disclose is Allowed for a path that cached says may be the remote's
	// cache, and records each path it allows for CheckDisclosures.
	Disclose(key, full string, folder bool, cached ...bool) bool
	// CheckRoutes fails when the routes could not be built or have changed
	// since the search captured them.
	CheckRoutes(ctx context.Context) error
	// CheckDisclosures fails when the caller may no longer read a path the
	// search disclosed, or under the route that named it.
	CheckDisclosures(ctx context.Context) error
}

Routes is the route and disclosure policy the adapter applies to an upstream search: which remote repositories it asks, under which key a virtual route may name what they hold, what the caller may read, and the fences that recheck both before the search answers.

type SearchRun

type SearchRun struct {
	// Principal is the caller.
	Principal kernel.Principal
	// Configs are the repositories' configurations, by key.
	Configs map[string]repo.Config
	// Repos are the repositories as a repo criterion resolves them.
	Repos aql.Repositories
	// Now is when the search started, in UTC: the end of a time window the
	// caller leaves open, and AQL's present.
	Now time.Time
	// Internal answers a failure the caller cannot act on, labelled with
	// the operation that failed: "search", "search permissions" or "search
	// layout". The adapter logs it and answers 500; nil returns the error
	// as it is.
	Internal func(ctx context.Context, op string, err error) error
	// contains filtered or unexported fields
}

SearchRun is one GET search: the caller, the repositories as they stand, as configured and as repo criteria resolve, and when the search started. Build one with NewSearch.

func NewSearch

func NewSearch(
	ctx context.Context,
	st Store,
	l Limits,
	p kernel.Principal,
	a Access,
	layouts func(context.Context, string) (*layout.Layout, error),
) (*SearchRun, error)

NewSearch starts a search for p over st, under l, with a's view of the repositories. layouts reads a repository layout by name, as the descriptor or the built-in set defines it. NewSearch lists the repositories; its only error is that listing's.

func (*SearchRun) Capped

func (r *SearchRun) Capped(limit int) int

Capped is limit, zero for none, under the AQL hard cap; -1 is no limit.

func (*SearchRun) Collect

func (r *SearchRun) Collect(
	ctx context.Context,
	criteria aql.Node,
	include []string,
	want int,
	keep func(aql.Hit) bool,
) ([]aql.Hit, error)

Collect runs criteria a page at a time, in item order, and returns the hits the caller may read that keep accepts (nil accepts all), at most want of them (-1 for all). Administrators read everything, as in AQL; anyone else gets the rows Access.Readable keeps, so a page may yield fewer. It collects the pages Pages walks.

func (*SearchRun) LayoutScope

func (r *SearchRun) LayoutScope(
	ctx context.Context,
	scope aql.Node,
	g, a *string,
) (LayoutScope, error)

LayoutScope resolves the repositories scope covers (nil for every one) to their layouts, keeping those that read an organisation, a module and a base revision, and narrows the query to the files under the start each layout gives the organisation g and the module a. A layout that does not compile reads nothing.

func (*SearchRun) ModuleScope

func (r *SearchRun) ModuleScope(ctx context.Context, repos, g, a *string) (LayoutScope, error)

ModuleScope resolves a versions or latest version search's repositories, virtual keys searching their members, to the layouts that read g and a.

func (*SearchRun) NamedKeys

func (r *SearchRun) NamedKeys(scope aql.Node) map[aql.RepoRef]string

NamedKeys maps each stored location a RepoScope criterion reaches to the first key of the criterion that reaches it.

func (*SearchRun) Pages

func (r *SearchRun) Pages(
	criteria aql.Node,
	include []string,
	want int,
	keep func(aql.Hit) bool,
) *kernel.Pages[aql.Hit]

Pages walks the hits Collect returns a store query at a time, so a search can write its results as it reads them (decision 0043, "Streaming complete lists"): the same queries of SearchPage rows in item order, the caller's Readable filter and keep on each, and no more than want hits in all (-1 for no cap). A page may hold fewer hits than its query read, or none. A failed query or permission read is Internal's error. The walk keeps its offset and count itself, so it serves one search; its cursors only mark that more follows.

func (*SearchRun) RefsOf

func (r *SearchRun) RefsOf(keys []string) map[aql.RepoRef]bool

RefsOf is the stored locations the repository keys stand for, as a repo criterion's $eq resolves them; nil for no keys.

func (*SearchRun) RepoScope

func (r *SearchRun) RepoScope(repos *string, virtual bool) (scope aql.Node, missing []string)

RepoScope turns a repos parameter into a criterion on the items' repositories, nil for every repository, which Collect resolves as AQL resolves repo criteria: a remote's key, like its cache key, selects its cache, and a virtual key its members, through nested virtual repositories. A virtual key counts only when virtual is set. Keys that name no repository select nothing; missing lists them, with the keys of repositories the caller may not see, for the search that refuses them. A parameter whose every key selects nothing matches no item.

func (*SearchRun) UserLimit

func (r *SearchRun) UserLimit() int

UserLimit is the most results the caller may get from a search the user query limit applies to, under the AQL hard cap; -1 is no limit.

type Store

type Store interface {
	// Items is the item store, which runs a search's AQL queries.
	Items() port.ItemStore
	// Repos is the repository store, which lists the repositories a search
	// scopes by.
	Repos() port.RepoStore
}

Store is what a search reads: items through AQL and the repositories it scopes by.

type Upstream

type Upstream interface {
	// Disclose reports whether the search may name fullPath, a file or,
	// with folder, a folder of the repository key, cached saying whether it
	// is the remote's cache, and records each path it allows so that the
	// adapter can recheck it before it answers.
	Disclose(key, fullPath string, folder bool, cached ...bool) bool
	// Versions lists the versions the upstreams hold of the module, as
	// ModuleVersions counts them from v and files. Its error is the
	// adapter's answer to an upstream that failed.
	Versions(ctx context.Context, v string, files bool) ([]VersionEntry, error)
}

Upstream is the remote part of a module search: what a remote repository's upstream lists beyond its cache. A nil Upstream reads stored files only.

type UpstreamDeps

type UpstreamDeps struct {
	// Sources reads the remote repositories' upstreams.
	Sources *source.Service
	// Items reads the repository layouts that a remote's discovery maps
	// between.
	Items *item.Service
	// Routes is the adapter's route and disclosure policy.
	Routes Routes
	// Fail turns an upstream failure that the search does not pass over
	// into the adapter's answer.
	Fail func(err error) error
}

UpstreamDeps is what the adapter supplies to an upstream search.

type UpstreamSearch

type UpstreamSearch struct {
	// contains filtered or unexported fields
}

UpstreamSearch is the upstream part of a layout search (decision 0012, docs/operations/upstream-search.md): what the remote repositories' upstreams hold of a module beyond their caches, read from authoritative Maven metadata or bounded folder listings. It is not safe for concurrent use: it counts its reads and records the checks Revalidate repeats.

func NewUpstreamSearch

func NewUpstreamSearch(
	r *SearchRun,
	ls LayoutScope,
	g, a *string,
	d UpstreamDeps,
) *UpstreamSearch

NewUpstreamSearch starts the upstream part of r for the organisation g and the module a, over the layouts of ls, through d.

func (*UpstreamSearch) Disclose

func (u *UpstreamSearch) Disclose(key, full string, folder bool, cached ...bool) bool

Disclose reports whether the search may name full, as Routes.Disclose decides and records it, which makes an UpstreamSearch an Upstream.

func (*UpstreamSearch) Gavc

func (u *UpstreamSearch) Gavc(ctx context.Context, v, c *string) ([]LayoutHit, error)

Gavc lists the files the upstreams' bounded listings show of the module whose version and classifier match v and c, each with the coordinates its repository's layout reads; failures are passed over or answered as Versions does.

func (*UpstreamSearch) Revalidate

func (u *UpstreamSearch) Revalidate(ctx context.Context) error

Revalidate rechecks, before the search answers, every upstream read and listing it relied on, then the routes and the disclosures. A nil u, a search without an upstream part, has nothing to recheck. Its error is UpstreamDeps.Fail's answer.

func (*UpstreamSearch) Versions

func (u *UpstreamSearch) Versions(
	ctx context.Context,
	v string,
	files bool,
) ([]VersionEntry, error)

Versions lists the versions the upstreams hold of the module, from Maven metadata where the upstream layout is Maven's and from bounded listings where the repository lists remote folders, as ModuleVersions counts them from v and files. A repository that does not hard-fail leaves its versions out when its upstream is unavailable; any other failure is UpstreamDeps.Fail's answer.

type VersionEntry

type VersionEntry struct {
	// V is the version as the search lists it.
	V string
	// IsIntegration reports whether V is an integration version, such as a
	// Maven snapshot.
	IsIntegration bool
}

VersionEntry is one version a module search lists.

func ModuleVersions

func ModuleVersions(
	ctx context.Context,
	r *SearchRun,
	ls LayoutScope,
	g, a *string,
	v string,
	files bool,
	upstream Upstream,
) ([]VersionEntry, error)

ModuleVersions lists, each once and newest first in Mercury order, the versions of the files in ls whose organisation and module match g and a. A v without wildcards lists only the integration file versions of the module version v, as 1.0-20260101.120000-1 is of 1.0-SNAPSHOT. Otherwise every version counts: an integration one by its file version when files is set, else by its module version, which groups a folder's unique snapshots as 1.0-SNAPSHOT. A v with wildcards is the caller's to apply.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL