Skip to content

Commit 0b92736

Browse files
committed
Simplify codepath through repo chunk lookup
Do more query parsing up front which reduces the amount of repetitive slash-finding performed, and also obivates the need for SearchSingleRepo, which perhaps confusingly takes a Database object.
1 parent 15c2c3d commit 0b92736

2 files changed

Lines changed: 45 additions & 56 deletions

File tree

src/pkgfile.cc

Lines changed: 33 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,18 @@ Pkgfile::Pkgfile(Options options) : options_(options) {
4545
case MODE_SEARCH:
4646
try_mmap_ = true;
4747
entry_callback_ = [this](const std::string& repo,
48-
const filter::Filter& filter, const Package& pkg,
49-
Result* result, ArchiveReader* reader) {
48+
const filter::Filter& filter,
49+
const ParsedPkgname& pkg, Result* result,
50+
ArchiveReader* reader) {
5051
return SearchMetafile(repo, filter, pkg, result, reader);
5152
};
5253
break;
5354
case MODE_LIST:
5455
try_mmap_ = false;
5556
entry_callback_ = [this](const std::string& repo,
56-
const filter::Filter& filter, const Package& pkg,
57-
Result* result, ArchiveReader* reader) {
57+
const filter::Filter& filter,
58+
const ParsedPkgname& pkg, Result* result,
59+
ArchiveReader* reader) {
5860
return ListMetafile(repo, filter, pkg, result, reader);
5961
};
6062
break;
@@ -91,7 +93,7 @@ Pkgfile::Pkgfile(Options options) : options_(options) {
9193
}
9294

9395
std::string Pkgfile::FormatSearchResult(const std::string& repo,
94-
const Package& pkg) {
96+
const ParsedPkgname& pkg) {
9597
if (options_.verbose) {
9698
return std::format("{}/{} {}", repo, pkg.name, pkg.version);
9799
}
@@ -105,7 +107,7 @@ std::string Pkgfile::FormatSearchResult(const std::string& repo,
105107

106108
int Pkgfile::SearchMetafile(const std::string& repo,
107109
const pkgfile::filter::Filter& filter,
108-
const Package& pkg, pkgfile::Result* result,
110+
const ParsedPkgname& pkg, pkgfile::Result* result,
109111
pkgfile::ArchiveReader* reader) {
110112
std::string_view line;
111113
while (reader->GetLine(&line) == ARCHIVE_OK) {
@@ -126,7 +128,7 @@ int Pkgfile::SearchMetafile(const std::string& repo,
126128

127129
int Pkgfile::ListMetafile(const std::string& repo,
128130
const pkgfile::filter::Filter& filter,
129-
const Package& pkg, pkgfile::Result* result,
131+
const ParsedPkgname& pkg, pkgfile::Result* result,
130132
pkgfile::ArchiveReader* reader) {
131133
if (!filter.Matches(pkg.name)) {
132134
return 0;
@@ -156,7 +158,7 @@ int Pkgfile::ListMetafile(const std::string& repo,
156158
}
157159

158160
// static
159-
bool Pkgfile::ParsePkgname(Pkgfile::Package* pkg, std::string_view entryname) {
161+
bool Pkgfile::ParsePkgname(ParsedPkgname* pkg, std::string_view entryname) {
160162
const auto pkgrel = entryname.rfind('-');
161163
if (pkgrel == entryname.npos) {
162164
return false;
@@ -199,7 +201,7 @@ void Pkgfile::ProcessRepo(const std::string& reponame,
199201
while (reader.Next(&e) == ARCHIVE_OK) {
200202
const char* entryname = archive_entry_pathname(e);
201203

202-
Package pkg;
204+
ParsedPkgname pkg;
203205
if (!ParsePkgname(&pkg, entryname)) {
204206
std::cerr << std::format("error parsing pkgname from: {}\n", entryname);
205207
continue;
@@ -211,20 +213,8 @@ void Pkgfile::ProcessRepo(const std::string& reponame,
211213
}
212214
}
213215

214-
int Pkgfile::SearchSingleRepo(const Database& db, const filter::Filter& filter,
215-
std::string_view searchstring) {
216-
std::string wanted_repo;
217-
if (!options_.targetrepo.empty()) {
218-
wanted_repo = options_.targetrepo;
219-
} else {
220-
wanted_repo = searchstring.substr(0, searchstring.find('/'));
221-
}
222-
223-
return SearchRepos(db.GetRepoChunks(wanted_repo), filter);
224-
}
225-
226-
int Pkgfile::SearchRepos(Database::RepoChunks repo_chunks,
227-
const filter::Filter& filter) {
216+
int Pkgfile::SearchRepoChunks(Database::RepoChunks repo_chunks,
217+
const filter::Filter& filter) {
228218
using ResultMap = std::map<std::string, std::unique_ptr<Result>>;
229219
ResultMap results;
230220

@@ -307,14 +297,7 @@ std::unique_ptr<filter::Filter> Pkgfile::BuildFilterFromOptions(
307297
std::make_unique<filter::Basename>(match, options.case_sensitive);
308298
}
309299
} else if (options.mode == MODE_LIST) {
310-
auto pos = match.find('/');
311-
if (pos != match.npos) {
312-
filter = std::make_unique<filter::Exact>(match.substr(pos + 1),
313-
options.case_sensitive);
314-
} else {
315-
filter =
316-
std::make_unique<filter::Exact>(match, options.case_sensitive);
317-
}
300+
filter = std::make_unique<filter::Exact>(match, options.case_sensitive);
318301
}
319302
break;
320303
case FilterStyle::GLOB:
@@ -370,27 +353,32 @@ int Pkgfile::Run(const std::vector<std::string>& args) {
370353
return 1;
371354
}
372355

373-
const std::string& input = args[0];
356+
auto ParseQueryInput = [this](std::string_view input)
357+
-> std::pair<std::string_view, std::string_view> {
358+
const auto pos = input.find('/');
359+
360+
// Make sure we reject anything that starts with a slash.
361+
if (options_.mode == MODE_LIST && pos != input.npos && pos > 0) {
362+
return {input.substr(0, pos), input.substr(pos + 1)};
363+
} else if (!options_.targetrepo.empty()) {
364+
return {options_.targetrepo, input};
365+
} else {
366+
return {std::string_view{}, input};
367+
}
368+
};
369+
370+
auto [repo, query] = ParseQueryInput(args[0]);
374371

375-
const auto filter = BuildFilterFromOptions(options_, input);
372+
const auto filter = BuildFilterFromOptions(options_, std::string(query));
376373
if (filter == nullptr) {
377374
return 1;
378375
}
379376

380-
const auto is_repo_package_syntax = [](std::string_view input) {
381-
auto pos = input.find('/');
382-
383-
// Make sure we reject anything that starts with a slash.
384-
return pos != input.npos && pos > 0;
385-
};
386-
387-
// override behavior on $repo/$pkg syntax or --repo
388-
if ((options_.mode == MODE_LIST && is_repo_package_syntax(input)) ||
389-
!options_.targetrepo.empty()) {
390-
return SearchSingleRepo(*database, *filter, input);
377+
if (!repo.empty()) {
378+
return SearchRepoChunks(database->GetRepoChunks(repo), *filter);
391379
}
392380

393-
return SearchRepos(database->GetAllRepoChunks(), *filter);
381+
return SearchRepoChunks(database->GetAllRepoChunks(), *filter);
394382
}
395383

396384
} // namespace pkgfile

src/pkgfile.hh

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,36 +58,37 @@ class Pkgfile {
5858
int Run(const std::vector<std::string>& args);
5959

6060
private:
61-
struct Package {
61+
struct ParsedPkgname {
6262
std::string_view name;
6363
std::string_view version;
6464
};
6565

6666
using RepoMap = std::multimap<std::string, std::filesystem::path>;
6767

6868
using ArchiveEntryCallback = std::function<int(
69-
const std::string& repo, const filter::Filter& filter, const Package& pkg,
70-
Result* result, ArchiveReader* reader)>;
69+
const std::string& repo, const filter::Filter& filter,
70+
const ParsedPkgname& pkg, Result* result, ArchiveReader* reader)>;
7171

7272
std::unique_ptr<filter::Filter> BuildFilterFromOptions(
7373
const Options& config, const std::string& match);
7474

75-
static bool ParsePkgname(Pkgfile::Package* pkg, std::string_view entryname);
75+
static bool ParsePkgname(ParsedPkgname* pkg, std::string_view entryname);
7676

7777
void ProcessRepo(const std::string& reponame, const std::string& repopath,
7878
const filter::Filter& filter, Result* result);
7979

80-
std::string FormatSearchResult(const std::string& repo, const Package& pkg);
80+
std::string FormatSearchResult(const std::string& repo,
81+
const ParsedPkgname& pkg);
8182

82-
int SearchRepos(Database::RepoChunks repo_chunks,
83-
const filter::Filter& filter);
84-
int SearchSingleRepo(const Database& db, const filter::Filter& filter,
85-
std::string_view searchstring);
83+
int SearchRepoChunks(Database::RepoChunks repo_chunks,
84+
const filter::Filter& filter);
8685

8786
int SearchMetafile(const std::string& repo, const filter::Filter& filter,
88-
const Package& pkg, Result* result, ArchiveReader* reader);
87+
const ParsedPkgname& pkg, Result* result,
88+
ArchiveReader* reader);
8989
int ListMetafile(const std::string& repo, const filter::Filter& filter,
90-
const Package& pkg, Result* result, ArchiveReader* reader);
90+
const ParsedPkgname& pkg, Result* result,
91+
ArchiveReader* reader);
9192

9293
Options options_;
9394
ArchiveEntryCallback entry_callback_;

0 commit comments

Comments
 (0)