Skip to content

Commit 1aa36cc

Browse files
pietroalbiniJoshua Nelson
authored and
Joshua Nelson
committed
context: move Index to the context
1 parent c60182e commit 1aa36cc

File tree

12 files changed

+70
-89
lines changed

12 files changed

+70
-89
lines changed

src/bin/cratesfyi.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ use std::path::PathBuf;
44
use std::sync::Arc;
55

66
use cratesfyi::db::{self, add_path_into_database, Pool, PoolClient};
7-
use cratesfyi::index::Index;
87
use cratesfyi::utils::{remove_crate_priority, set_crate_priority};
98
use cratesfyi::{
10-
BuildQueue, Config, Context, DocBuilder, Metrics, RustwideBuilder, Server, Storage,
9+
BuildQueue, Config, Context, DocBuilder, Index, Metrics, RustwideBuilder, Server, Storage,
1110
};
1211
use failure::{err_msg, Error, ResultExt};
1312
use once_cell::sync::OnceCell;
@@ -282,19 +281,18 @@ enum BuildSubcommand {
282281

283282
impl BuildSubcommand {
284283
pub fn handle_args(self, ctx: BinContext, skip_if_exists: bool) -> Result<(), Error> {
285-
let mut docbuilder = DocBuilder::new(ctx.config()?, ctx.pool()?, ctx.build_queue()?);
284+
let docbuilder = DocBuilder::new(ctx.config()?, ctx.pool()?, ctx.build_queue()?);
286285

287286
let rustwide_builder = || -> Result<RustwideBuilder, Error> {
288-
let mut builder =
289-
RustwideBuilder::init(ctx.config()?, ctx.pool()?, ctx.metrics()?, ctx.storage()?)?;
287+
let mut builder = RustwideBuilder::init(&ctx)?;
290288
builder.set_skip_build_if_exists(skip_if_exists);
291289
Ok(builder)
292290
};
293291

294292
match self {
295293
Self::World => {
296294
rustwide_builder()?
297-
.build_world(&mut docbuilder)
295+
.build_world()
298296
.context("Failed to build world")?;
299297
}
300298

@@ -307,12 +305,11 @@ impl BuildSubcommand {
307305

308306
if let Some(path) = local {
309307
builder
310-
.build_local_package(&mut docbuilder, &path)
308+
.build_local_package(&path)
311309
.context("Building documentation failed")?;
312310
} else {
313311
builder
314312
.build_package(
315-
&mut docbuilder,
316313
&crate_name.ok_or_else(|| err_msg("must specify name if not local"))?,
317314
&crate_version
318315
.ok_or_else(|| err_msg("must specify version if not local"))?,
@@ -420,7 +417,7 @@ impl DatabaseSubcommand {
420417
}
421418

422419
Self::UpdateCrateRegistryFields { name } => {
423-
let index = Index::new(&ctx.config()?.registry_index_path)?;
420+
let index = ctx.index()?;
424421

425422
db::update_crate_data_in_database(
426423
&mut *ctx.conn()?,
@@ -452,8 +449,8 @@ impl DatabaseSubcommand {
452449

453450
Self::Synchronize { dry_run } => {
454451
cratesfyi::utils::consistency::run_check(
455-
&*ctx.config()?,
456452
&mut *ctx.conn()?,
453+
&*ctx.index()?,
457454
dry_run,
458455
)?;
459456
}
@@ -529,6 +526,7 @@ struct BinContext {
529526
config: OnceCell<Arc<Config>>,
530527
pool: OnceCell<Pool>,
531528
metrics: OnceCell<Arc<Metrics>>,
529+
index: OnceCell<Arc<Index>>,
532530
}
533531

534532
impl BinContext {
@@ -539,6 +537,7 @@ impl BinContext {
539537
config: OnceCell::new(),
540538
pool: OnceCell::new(),
541539
metrics: OnceCell::new(),
540+
index: OnceCell::new(),
542541
}
543542
}
544543

@@ -594,4 +593,11 @@ impl Context for BinContext {
594593
.get_or_try_init::<_, Error>(|| Ok(Arc::new(Metrics::new()?)))?
595594
.clone())
596595
}
596+
597+
fn index(&self) -> Result<Arc<Index>, Error> {
598+
Ok(self
599+
.index
600+
.get_or_try_init::<_, Error>(|| Ok(Arc::new(Index::new(&*self.config()?)?)))?
601+
.clone())
602+
}
597603
}

src/context.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::db::Pool;
2-
use crate::{BuildQueue, Config, Metrics, Storage};
2+
use crate::{BuildQueue, Config, Index, Metrics, Storage};
33
use failure::Error;
44
use std::sync::Arc;
55

@@ -9,4 +9,5 @@ pub trait Context {
99
fn storage(&self) -> Result<Arc<Storage>, Error>;
1010
fn pool(&self) -> Result<Pool, Error>;
1111
fn metrics(&self) -> Result<Arc<Metrics>, Error>;
12+
fn index(&self) -> Result<Arc<Index>, Error>;
1213
}

src/docbuilder/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ pub(crate) use self::rustwide_builder::{BuildResult, DocCoverage};
99

1010
use crate::db::Pool;
1111
use crate::error::Result;
12-
use crate::index::Index;
1312
use crate::{BuildQueue, Config};
1413
use std::fs;
1514
use std::path::PathBuf;
@@ -18,18 +17,15 @@ use std::sync::Arc;
1817
/// chroot based documentation builder
1918
pub struct DocBuilder {
2019
config: Arc<Config>,
21-
index: Index,
2220
db: Pool,
2321
build_queue: Arc<BuildQueue>,
2422
}
2523

2624
impl DocBuilder {
2725
pub fn new(config: Arc<Config>, db: Pool, build_queue: Arc<BuildQueue>) -> DocBuilder {
28-
let index = Index::new(&config.registry_index_path).expect("valid index");
2926
DocBuilder {
3027
config,
3128
build_queue,
32-
index,
3329
db,
3430
}
3531
}

src/docbuilder/queue.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
use super::{DocBuilder, RustwideBuilder};
44
use crate::error::Result;
55
use crate::utils::get_crate_priority;
6+
use crate::Index;
67
use crates_index_diff::ChangeKind;
78
use log::{debug, error};
89

910
impl DocBuilder {
1011
/// Updates registry index repository and adds new crates into build queue.
1112
/// Returns the number of crates added
12-
pub fn get_new_crates(&mut self) -> Result<usize> {
13+
pub fn get_new_crates(&mut self, index: &Index) -> Result<usize> {
1314
let mut conn = self.db.get()?;
14-
let diff = self.index.diff()?;
15+
let diff = index.diff()?;
1516
let (mut changes, oid) = diff.peek_changes()?;
1617
let mut crates_added = 0;
1718

@@ -78,14 +79,10 @@ impl DocBuilder {
7879
queue.process_next_crate(|krate| {
7980
processed = true;
8081

81-
builder.build_package(self, &krate.name, &krate.version, None)?;
82+
builder.build_package(&krate.name, &krate.version, None)?;
8283
Ok(())
8384
})?;
8485

8586
Ok(processed)
8687
}
87-
88-
pub fn run_git_gc(&self) {
89-
self.index.run_git_gc();
90-
}
9188
}

src/docbuilder/rustwide_builder.rs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use super::DocBuilder;
21
use crate::db::blacklist::is_blacklisted;
32
use crate::db::file::add_path_into_database;
43
use crate::db::{
@@ -10,7 +9,7 @@ use crate::error::Result;
109
use crate::index::api::ReleaseData;
1110
use crate::storage::CompressionAlgorithms;
1211
use crate::utils::{copy_doc_dir, parse_rustc_version, CargoMetadata};
13-
use crate::{Config, Metrics, Storage};
12+
use crate::{Config, Context, Index, Metrics, Storage};
1413
use docsrs_metadata::{Metadata, DEFAULT_TARGETS, HOST_TARGET};
1514
use failure::ResultExt;
1615
use log::{debug, info, warn, LevelFilter};
@@ -63,17 +62,15 @@ pub struct RustwideBuilder {
6362
db: Pool,
6463
storage: Arc<Storage>,
6564
metrics: Arc<Metrics>,
65+
index: Arc<Index>,
6666
rustc_version: String,
6767
skip_build_if_exists: bool,
6868
}
6969

7070
impl RustwideBuilder {
71-
pub fn init(
72-
config: Arc<Config>,
73-
db: Pool,
74-
metrics: Arc<Metrics>,
75-
storage: Arc<Storage>,
76-
) -> Result<Self> {
71+
pub fn init(context: &dyn Context) -> Result<Self> {
72+
let config = context.config()?;
73+
7774
let mut builder = WorkspaceBuilder::new(&config.rustwide_workspace, USER_AGENT)
7875
.running_inside_docker(config.inside_docker);
7976
if let Some(custom_image) = &config.local_docker_image {
@@ -89,9 +86,10 @@ impl RustwideBuilder {
8986
workspace,
9087
toolchain,
9188
config,
92-
db,
93-
storage,
94-
metrics,
89+
db: context.pool()?,
90+
storage: context.storage()?,
91+
metrics: context.metrics()?,
92+
index: context.index()?,
9593
rustc_version: String::new(),
9694
skip_build_if_exists: false,
9795
})
@@ -256,34 +254,29 @@ impl RustwideBuilder {
256254
Ok(())
257255
}
258256

259-
pub fn build_world(&mut self, doc_builder: &mut DocBuilder) -> Result<()> {
257+
pub fn build_world(&mut self) -> Result<()> {
260258
crates_from_path(
261259
&self.config.registry_index_path.clone(),
262260
&mut |name, version| {
263-
if let Err(err) = self.build_package(doc_builder, name, version, None) {
261+
if let Err(err) = self.build_package(name, version, None) {
264262
warn!("failed to build package {} {}: {}", name, version, err);
265263
}
266264
},
267265
)
268266
}
269267

270-
pub fn build_local_package(
271-
&mut self,
272-
doc_builder: &mut DocBuilder,
273-
path: &Path,
274-
) -> Result<bool> {
268+
pub fn build_local_package(&mut self, path: &Path) -> Result<bool> {
275269
self.update_toolchain()?;
276270
let metadata =
277271
CargoMetadata::load(&self.workspace, &self.toolchain, path).map_err(|err| {
278272
err.context(format!("failed to load local package {}", path.display()))
279273
})?;
280274
let package = metadata.root();
281-
self.build_package(doc_builder, &package.name, &package.version, Some(path))
275+
self.build_package(&package.name, &package.version, Some(path))
282276
}
283277

284278
pub fn build_package(
285279
&mut self,
286-
doc_builder: &mut DocBuilder,
287280
name: &str,
288281
version: &str,
289282
local: Option<&Path>,
@@ -379,7 +372,7 @@ impl RustwideBuilder {
379372
self.metrics.non_library_builds.inc();
380373
}
381374

382-
let release_data = match doc_builder.index.api().get_release_data(name, version) {
375+
let release_data = match self.index.api().get_release_data(name, version) {
383376
Ok(data) => data,
384377
Err(err) => {
385378
warn!("{:#?}", err);
@@ -408,7 +401,7 @@ impl RustwideBuilder {
408401
add_build_into_database(&mut conn, release_id, &res.result)?;
409402

410403
// Some crates.io crate data is mutable, so we proactively update it during a release
411-
match doc_builder.index.api().get_crate_data(name) {
404+
match self.index.api().get_crate_data(name) {
412405
Ok(crate_data) => update_crate_data_in_database(&mut conn, name, &crate_data)?,
413406
Err(err) => warn!("{:#?}", err),
414407
}

src/index/mod.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
use std::{
2-
path::{Path, PathBuf},
3-
process::Command,
4-
};
1+
use std::{path::PathBuf, process::Command};
52

63
use url::Url;
74

85
use self::{api::Api, crates::Crates};
9-
use crate::error::Result;
6+
use crate::{error::Result, Config};
107
use failure::ResultExt;
118

129
pub(crate) mod api;
@@ -43,8 +40,8 @@ fn load_config(repo: &git2::Repository) -> Result<IndexConfig> {
4340
}
4441

4542
impl Index {
46-
pub fn new(path: impl AsRef<Path>) -> Result<Self> {
47-
let path = path.as_ref().to_owned();
43+
pub fn new(app_config: &Config) -> Result<Self> {
44+
let path = app_config.registry_index_path.clone();
4845
// This initializes the repository, then closes it afterwards to avoid leaking file descriptors.
4946
// See https://github.com/rust-lang/docs.rs/pull/847
5047
let diff = crates_index_diff::Index::from_path_or_cloned(&path)
@@ -91,9 +88,3 @@ impl Index {
9188
}
9289
}
9390
}
94-
95-
impl Clone for Index {
96-
fn clone(&self) -> Self {
97-
Self::new(&self.path).expect("we already loaded this registry successfully once")
98-
}
99-
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub use self::config::Config;
77
pub use self::context::Context;
88
pub use self::docbuilder::DocBuilder;
99
pub use self::docbuilder::RustwideBuilder;
10+
pub use self::index::Index;
1011
pub use self::metrics::Metrics;
1112
pub use self::storage::Storage;
1213
pub use self::web::Server;

src/test/mod.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ mod fakes;
33
use crate::db::{Pool, PoolClient};
44
use crate::storage::{Storage, StorageKind};
55
use crate::web::Server;
6-
use crate::{BuildQueue, Config, Context, Metrics};
6+
use crate::{BuildQueue, Config, Context, Index, Metrics};
77
use failure::Error;
88
use log::error;
99
use once_cell::unsync::OnceCell;
@@ -96,6 +96,7 @@ pub(crate) struct TestEnvironment {
9696
config: OnceCell<Arc<Config>>,
9797
db: OnceCell<TestDatabase>,
9898
storage: OnceCell<Arc<Storage>>,
99+
index: OnceCell<Arc<Index>>,
99100
metrics: OnceCell<Arc<Metrics>>,
100101
frontend: OnceCell<TestFrontend>,
101102
}
@@ -115,6 +116,7 @@ impl TestEnvironment {
115116
config: OnceCell::new(),
116117
db: OnceCell::new(),
117118
storage: OnceCell::new(),
119+
index: OnceCell::new(),
118120
metrics: OnceCell::new(),
119121
frontend: OnceCell::new(),
120122
}
@@ -192,6 +194,14 @@ impl TestEnvironment {
192194
.clone()
193195
}
194196

197+
pub(crate) fn index(&self) -> Arc<Index> {
198+
self.index
199+
.get_or_init(|| {
200+
Arc::new(Index::new(&*self.config()).expect("failed to initialize the index"))
201+
})
202+
.clone()
203+
}
204+
195205
pub(crate) fn db(&self) -> &TestDatabase {
196206
self.db.get_or_init(|| {
197207
TestDatabase::new(&self.config(), self.metrics()).expect("failed to initialize the db")
@@ -227,6 +237,10 @@ impl Context for TestEnvironment {
227237
fn metrics(&self) -> Result<Arc<Metrics>, Error> {
228238
Ok(self.metrics())
229239
}
240+
241+
fn index(&self) -> Result<Arc<Index>, Error> {
242+
Ok(self.index())
243+
}
230244
}
231245

232246
pub(crate) struct TestDatabase {

src/utils/consistency/index.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use super::data::{Crate, CrateName, Data, Release, Version};
2-
use crate::{config::Config, index::Index};
3-
4-
pub(crate) fn load(config: &Config) -> Result<Data, failure::Error> {
5-
let index = Index::new(&config.registry_index_path)?;
2+
use crate::Index;
63

4+
pub(crate) fn load(index: &Index) -> Result<Data, failure::Error> {
75
let mut data = Data::default();
86

97
index.crates()?.walk(|krate| {

0 commit comments

Comments
 (0)