Skip to content

Commit 278915b

Browse files
cslinwangimeoer
authored andcommitted
nydus-image:Optimize Chunkdict Save
Refactor the Deduplicate implementation to only initialize config when inserting chunk data. Simplify code for better maintainability. Signed-off-by: Lin Wang <[email protected]>
1 parent d2fcfcd commit 278915b

File tree

2 files changed

+22
-31
lines changed

2 files changed

+22
-31
lines changed

src/bin/nydus-image/deduplicate.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,28 +128,31 @@ impl Database for SqliteDatabase {
128128
}
129129

130130
pub struct Deduplicate<D: Database + Send + Sync> {
131-
sb: RafsSuper,
132131
db: D,
133132
}
134133

135134
const IN_MEMORY_DB_URL: &str = ":memory:";
136135

137136
impl Deduplicate<SqliteDatabase> {
138-
pub fn new(bootstrap_path: &Path, config: Arc<ConfigV2>, db_url: &str) -> anyhow::Result<Self> {
139-
let (sb, _) = RafsSuper::load_from_file(bootstrap_path, config, false)?;
137+
pub fn new(db_url: &str) -> anyhow::Result<Self> {
140138
let db = if db_url == IN_MEMORY_DB_URL {
141139
SqliteDatabase::new_in_memory()?
142140
} else {
143141
SqliteDatabase::new(db_url)?
144142
};
145-
Ok(Self { sb, db })
143+
Ok(Self { db })
146144
}
147145

148-
pub fn save_metadata(&mut self) -> anyhow::Result<Vec<Arc<BlobInfo>>> {
146+
pub fn save_metadata(
147+
&mut self,
148+
bootstrap_path: &Path,
149+
config: Arc<ConfigV2>,
150+
) -> anyhow::Result<Vec<Arc<BlobInfo>>> {
151+
let (sb, _) = RafsSuper::load_from_file(bootstrap_path, config, false)?;
149152
self.create_tables()?;
150-
let blob_infos = self.sb.superblock.get_blob_infos();
153+
let blob_infos = sb.superblock.get_blob_infos();
151154
self.insert_blobs(&blob_infos)?;
152-
self.insert_chunks(&blob_infos)?;
155+
self.insert_chunks(&blob_infos, &sb)?;
153156
Ok(blob_infos)
154157
}
155158

@@ -176,7 +179,11 @@ impl Deduplicate<SqliteDatabase> {
176179
Ok(())
177180
}
178181

179-
fn insert_chunks(&mut self, blob_infos: &[Arc<BlobInfo>]) -> anyhow::Result<()> {
182+
fn insert_chunks(
183+
&mut self,
184+
blob_infos: &[Arc<BlobInfo>],
185+
sb: &RafsSuper,
186+
) -> anyhow::Result<()> {
180187
let process_chunk = &mut |t: &Tree| -> Result<()> {
181188
let node = t.lock_node();
182189
for chunk in &node.chunks {
@@ -195,7 +202,7 @@ impl Deduplicate<SqliteDatabase> {
195202
}
196203
Ok(())
197204
};
198-
let tree = Tree::from_bootstrap(&self.sb, &mut ())
205+
let tree = Tree::from_bootstrap(sb, &mut ())
199206
.context("Failed to load bootstrap for deduplication.")?;
200207
tree.walk_dfs_pre(process_chunk)?;
201208
Ok(())

src/bin/nydus-image/main.rs

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use nydus_builder::{
3434
use nydus_rafs::metadata::{RafsSuper, RafsSuperConfig, RafsVersion};
3535
use nydus_storage::backend::localfs::LocalFs;
3636
use nydus_storage::backend::BlobBackend;
37-
use nydus_storage::device::{BlobFeatures, BlobInfo};
37+
use nydus_storage::device::BlobFeatures;
3838
use nydus_storage::factory::BlobFactory;
3939
use nydus_storage::meta::{format_blob_features, BatchContextGenerator};
4040
use nydus_storage::{RAFS_DEFAULT_CHUNK_SIZE, RAFS_MAX_CHUNK_SIZE};
@@ -369,7 +369,6 @@ fn prepare_cmd_args(bti_string: &'static str) -> App {
369369
.short('B')
370370
.long("bootstrap")
371371
.help("File path of RAFS meta blob/bootstrap")
372-
.conflicts_with("BOOTSTRAP")
373372
.required(false),
374373
)
375374
.arg(
@@ -399,7 +398,7 @@ fn prepare_cmd_args(bti_string: &'static str) -> App {
399398
)
400399
.arg(arg_output_json.clone())
401400
)
402-
);
401+
);
403402

404403
let app = app.subcommand(
405404
App::new("merge")
@@ -1151,32 +1150,17 @@ impl Command {
11511150
bail!("Invalid database URL: {}", db_url);
11521151
}
11531152

1154-
let blobs: Vec<Arc<BlobInfo>> = match db_strs[0] {
1153+
match db_strs[0] {
11551154
"sqlite" => {
11561155
let mut deduplicate: Deduplicate<SqliteDatabase> =
1157-
Deduplicate::<SqliteDatabase>::new(bootstrap_path, config, db_strs[1])?;
1158-
deduplicate.save_metadata()?
1156+
Deduplicate::<SqliteDatabase>::new(db_strs[1])?;
1157+
deduplicate.save_metadata(bootstrap_path, config)?
11591158
}
11601159
_ => {
11611160
bail!("Unsupported database type: {}, please use a valid database URI, such as 'sqlite:///path/to/database.db'.", db_strs[0])
11621161
}
11631162
};
1164-
info!("RAFS filesystem metadata is saved:");
1165-
1166-
let mut blob_ids = Vec::new();
1167-
for (idx, blob) in blobs.iter().enumerate() {
1168-
info!(
1169-
"\t {}: {}, compressed data size 0x{:x}, compressed file size 0x{:x}, uncompressed file size 0x{:x}, chunks: 0x{:x}, features: {}.",
1170-
idx,
1171-
blob.blob_id(),
1172-
blob.compressed_data_size(),
1173-
blob.compressed_size(),
1174-
blob.uncompressed_size(),
1175-
blob.chunk_count(),
1176-
format_blob_features(blob.features()),
1177-
);
1178-
blob_ids.push(blob.blob_id().to_string());
1179-
}
1163+
info!("Chunkdict metadata is saved at: {:?}", db_url);
11801164
Ok(())
11811165
}
11821166

0 commit comments

Comments
 (0)