refactored to create manifest by filename

This commit is contained in:
DecDuck
2024-10-14 13:01:56 +11:00
parent fcdd1af2db
commit 75e9e7d74c
3 changed files with 37 additions and 28 deletions
+6 -4
View File
@@ -10,15 +10,17 @@ crate-type = ["cdylib"]
# Default enable napi4 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix # Default enable napi4 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix
napi = { version = "2.12.2", default-features = false, features = ["napi4", "async"] } napi = { version = "2.12.2", default-features = false, features = ["napi4", "async"] }
napi-derive = "2.12.2" napi-derive = "2.12.2"
zstd = "0.13.2"
rayon = "1.10.0"
serde = "1.0.210"
ciborium = "0.2.2"
time = "0.3.36" time = "0.3.36"
hex = "0.4.3" hex = "0.4.3"
gxhash = "=2.3.0" gxhash = "=2.3.0"
serde_json = "1.0.128" serde_json = "1.0.128"
[dependencies.serde]
version = "1.0.210"
features = [
"serde_derive"
]
[dependencies.openssl] [dependencies.openssl]
version = "0.10.66" version = "0.10.66"
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@drop/droplet", "name": "@drop/droplet",
"version": "0.4.6", "version": "0.5.0",
"main": "index.js", "main": "index.js",
"types": "index.d.ts", "types": "index.d.ts",
"napi": { "napi": {
+30 -23
View File
@@ -1,5 +1,9 @@
use std::{ use std::{
collections::HashMap, fs::File, io::{BufRead, BufReader}, path::Path, thread collections::HashMap,
fs::File,
io::{BufRead, BufReader},
path::Path,
thread,
}; };
#[cfg(unix)] #[cfg(unix)]
@@ -10,7 +14,6 @@ use napi::{
threadsafe_function::{ErrorStrategy, ThreadsafeFunction, ThreadsafeFunctionCallMode}, threadsafe_function::{ErrorStrategy, ThreadsafeFunction, ThreadsafeFunctionCallMode},
Error, JsFunction, Error, JsFunction,
}; };
use serde::Serialize;
use serde_json::json; use serde_json::json;
use uuid::Uuid; use uuid::Uuid;
@@ -18,13 +21,12 @@ use crate::file_utils::list_files;
const CHUNK_SIZE: usize = 1024 * 1024 * 128; const CHUNK_SIZE: usize = 1024 * 1024 * 128;
#[derive(Serialize)] #[derive(serde::Serialize)]
struct Chunk { struct ChunkData {
permissions: u32, permissions: u32,
file_name: String, ids: Vec<String>,
chunk_index: u32, checksums: Vec<String>,
checksum: String, lengths: Vec<usize>,
length: usize,
} }
#[napi] #[napi]
@@ -51,17 +53,22 @@ pub fn generate_manifest(
.create_threadsafe_function(0, |ctx| ctx.env.create_int32(ctx.value).map(|v| vec![v])) .create_threadsafe_function(0, |ctx| ctx.env.create_int32(ctx.value).map(|v| vec![v]))
.unwrap(); .unwrap();
let log_sfn: ThreadsafeFunction<String, ErrorStrategy::CalleeHandled> = log let log_sfn: ThreadsafeFunction<String, ErrorStrategy::CalleeHandled> = log
.create_threadsafe_function(0, |ctx| ctx.env.create_string_from_std(ctx.value).map(|v| vec![v])) .create_threadsafe_function(0, |ctx| {
ctx.env.create_string_from_std(ctx.value).map(|v| vec![v])
})
.unwrap(); .unwrap();
let callback_sfn: ThreadsafeFunction<String, ErrorStrategy::CalleeHandled> = callback let callback_sfn: ThreadsafeFunction<String, ErrorStrategy::CalleeHandled> = callback
.create_threadsafe_function(0, |ctx| ctx.env.create_string_from_std(ctx.value).map(|v| vec![v])) .create_threadsafe_function(0, |ctx| {
ctx.env.create_string_from_std(ctx.value).map(|v| vec![v])
})
.unwrap(); .unwrap();
thread::spawn(move || { thread::spawn(move || {
let base_dir = Path::new(&dir); let base_dir = Path::new(&dir);
let files = list_files(base_dir); let files = list_files(base_dir);
let mut chunks: HashMap<String, Chunk> = HashMap::new(); // Filepath to chunk data
let mut chunks: HashMap<String, ChunkData> = HashMap::new();
let total: i32 = files.len() as i32; let total: i32 = files.len() as i32;
let mut i: i32 = 0; let mut i: i32 = 0;
@@ -81,6 +88,13 @@ pub fn generate_manifest(
let mut reader = BufReader::with_capacity(CHUNK_SIZE, file); let mut reader = BufReader::with_capacity(CHUNK_SIZE, file);
let mut chunk_data = ChunkData {
permissions: permissions,
ids: Vec::new(),
checksums: Vec::new(),
lengths: Vec::new(),
};
let mut chunk_index = 0; let mut chunk_index = 0;
loop { loop {
let mut buffer: Vec<u8> = Vec::new(); let mut buffer: Vec<u8> = Vec::new();
@@ -95,18 +109,9 @@ pub fn generate_manifest(
let checksum = gxhash128(&buffer, 0); let checksum = gxhash128(&buffer, 0);
let checksum_string = hex::encode(checksum.to_le_bytes()); let checksum_string = hex::encode(checksum.to_le_bytes());
let chunk = Chunk { chunk_data.ids.push(chunk_id.to_string());
chunk_index: chunk_index, chunk_data.checksums.push(checksum_string);
permissions: permissions, chunk_data.lengths.push(length);
file_name: relative.to_str().unwrap().to_string(),
checksum: checksum_string,
length: length,
};
let original = chunks.insert(chunk_id.to_string(), chunk);
if original.is_some() {
panic!("UUID collision");
}
let log_str = format!( let log_str = format!(
"Processed chunk {} for {}", "Processed chunk {} for {}",
@@ -119,6 +124,8 @@ pub fn generate_manifest(
chunk_index += 1; chunk_index += 1;
} }
chunks.insert(relative.to_str().unwrap().to_string(), chunk_data);
i += 1; i += 1;
let progress = i * 100 / total; let progress = i * 100 / total;
progress_sfn.call(Ok(progress), ThreadsafeFunctionCallMode::Blocking); progress_sfn.call(Ok(progress), ThreadsafeFunctionCallMode::Blocking);