Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions crates/engine/src/lightroom_catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ fn error(why: impl Into<String>) -> crate::EngineError {
fn text<'a>(r: &'a Row, k: &str) -> &'a str {
r.get(k).and_then(Value::as_str).unwrap_or("")
}
/// The record a row's `k` column names: its value as a whole number, or `None` when that is missing, NULL, text, a
/// fraction or out of `i64` range. [`number`] reads a missing, NULL or text value as 0, naming the record whose `id_local` is 0, and
/// truncates a fraction (1.5 names record 1).
fn id_of(r: &Row, k: &str) -> Option<i64> {
let n = r.get(k)?.as_number()?;
let f = n.as_f64()?;
// An integer goes through f64 as [`number`] takes the `id_local` of the record it names, so both get the same key
// past 2^53 too.
if n.is_i64() {
return Some(f as i64);
}
// (2^63 is the first value past i64::MAX; a cast would saturate onto a real id)
(f.fract() == 0.0 && f >= i64::MIN as f64 && f < 9_223_372_036_854_775_808.0).then_some(f as i64)
}
fn number(r: &Row, k: &str) -> i64 {
r.get(k).and_then(Value::as_f64).filter(|n| n.is_finite()).unwrap_or(0.0) as i64
}
Expand Down Expand Up @@ -1042,7 +1056,7 @@ fn first_of_each_collection<'a>(data: &'a CatalogImport, warnings: &mut Vec<Stri
continue;
};
if *kept != collection {
let photos = if data.members.iter().any(|m| number(m, "collection") == id) {
let photos = if data.members.iter().any(|m| id_of(m, "collection") == Some(id)) {
format!("; the photos listed under that id go to “{}”", text(kept, "name"))
} else {
String::new()
Expand Down Expand Up @@ -1130,8 +1144,16 @@ fn stage_collections(
let parents: HashMap<_, _> = rows.iter().map(|r| (number(r, "id_local"), number(r, "parent"))).collect();
let collections: HashMap<_, _> = rows.iter().map(|r| (number(r, "id_local"), *r)).collect();
let mut grouped_members: HashMap<i64, Vec<&Row>> = HashMap::new();
// read strictly: a NULL, text or fractional `collection` names no collection, not collection 0 (or 1 for 1.5)
for member in &data.members {
grouped_members.entry(number(member, "collection")).or_default().push(member);
if let Some(collection) = id_of(member, "collection") {
grouped_members.entry(collection).or_default().push(member);
}
}
match data.members.iter().filter(|m| id_of(m, "collection").is_none()).count() {
0 => {}
1 => warnings.push("AgLibraryCollectionImage: 1 record added to no album; its collection is not a collection id".into()),
n => warnings.push(format!("AgLibraryCollectionImage: {n} records added to no album; their collection is not a collection id")),
}
for members in grouped_members.values_mut() {
members.sort_by(|a, b| text(a, "positionInCollection").cmp(text(b, "positionInCollection")));
Expand Down
94 changes: 94 additions & 0 deletions crates/engine/src/lightroom_catalog/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,50 @@ fn a_collection_listed_twice_imports_once_and_a_conflicting_one_is_skipped_with_
);
assert_eq!(s.undo.len(), 1);
}
/// A collection member whose `collection` is missing, NULL, text or a fraction names no collection. It used to read as
/// collection 0 (and 11.5 as 11), so the collection with that id took in photos that aren't its own; now such members
/// join no album, with one warning. A whole number stored as a REAL (11.0) still names its collection.
#[test]
fn a_member_whose_collection_is_not_a_whole_number_joins_no_album() {
let mut s = crate::Session::new();
let mut data = sample();
data.collections.push(row(json!({"id_local":0,"name":"Zero","creationId":"collection"})));
data.collections.push(row(json!({"id_local":11,"name":"Eleven","creationId":"collection"})));
data.members.extend([
row(json!({"collection":0,"image":1,"positionInCollection":"a"})),
row(json!({"collection":11,"image":2,"positionInCollection":"a"})),
row(json!({"collection":11.0,"image":1,"positionInCollection":"b"})),
row(json!({"collection":null,"image":2,"positionInCollection":"c"})),
row(json!({"collection":"x","image":2,"positionInCollection":"d"})),
row(json!({"image":2,"positionInCollection":"e"})),
row(json!({"collection":11.5,"image":1,"positionInCollection":"f"})),
]);
let r = apply(&mut s, data, false).unwrap();
let id = |source: &str| PhotoId(r["mapping"][source].as_u64().unwrap());
assert_eq!(album_named(&s, "Zero").photos, [id("1")], "{r}");
assert_eq!(album_named(&s, "Eleven").photos, [id("2"), id("1")], "{r}");
assert_eq!(album_named(&s, "Keepers").photos, [id("2"), id("1")], "{r}");
assert_eq!(r["warnings"], json!(["AgLibraryCollectionImage: 4 records added to no album; their collection is not a collection id"]));
}
/// A collection left out for sharing collection 0's id says the photos under that id go to the first only when a
/// member names it: a member whose `collection` is NULL is not one of them.
#[test]
fn a_member_naming_no_collection_is_not_listed_under_id_0() {
let mut s = crate::Session::new();
let mut data = sample();
data.collections.push(row(json!({"id_local":0,"name":"Zero","creationId":"collection"})));
data.collections.push(row(json!({"id_local":0,"name":"Also zero","creationId":"collection"})));
data.members.push(row(json!({"collection":null,"image":1,"positionInCollection":"a"})));
let r = apply(&mut s, data, false).unwrap();
assert!(album_named(&s, "Zero").photos.is_empty(), "{r}");
assert_eq!(
r["warnings"],
json!([
"collection “Also zero”: not imported, it has the id of collection “Zero”",
"AgLibraryCollectionImage: 1 record added to no album; its collection is not a collection id"
])
);
}
/// The search for the nearest set ends on a parent loop that got past `validate` (A in B in A, a collection in
/// itself) and on a chain of plain collections deeper than 64: the album goes to the top level, flagged as moved.
#[test]
Expand Down Expand Up @@ -1321,6 +1365,56 @@ fn record(values: &[SqlValue]) -> Vec<u8> {
out.extend(body);
out
}
#[test]
fn a_reference_names_a_record_only_by_a_whole_number() {
let r = row(json!({
"int": 7, "zero": 0, "negative": -3, "real": 7.0, "fraction": 7.5, "text": "7", "null": null, "nan_like": "NaN",
"past_f64": 9_007_199_254_740_993_i64, "max": i64::MAX, "edge": 9_223_372_036_854_775_808.0,
"low_edge": -9_223_372_036_854_775_808.0, "too_big": 1e19, "too_small": -1e19
}));
let got: Vec<_> = [
"int",
"zero",
"negative",
"real",
"fraction",
"text",
"null",
"nan_like",
"missing",
"past_f64",
"max",
"edge",
"low_edge",
"too_big",
"too_small",
]
.iter()
.map(|k| (*k, id_of(&r, k)))
.collect();
assert_eq!(
got,
[
("int", Some(7)),
("zero", Some(0)),
("negative", Some(-3)),
("real", Some(7)),
("fraction", None),
("text", None),
("null", None),
("nan_like", None),
("missing", None),
// an integer rounds through f64 as `number` reads the `id_local` it names
("past_f64", Some(number(&r, "past_f64"))),
("max", Some(number(&r, "max"))),
// 2^63, the first value past i64::MAX: a cast would saturate onto record i64::MAX
("edge", None),
("low_edge", Some(i64::MIN)),
("too_big", None),
("too_small", None),
]
);
}
fn leaf(page: &mut [u8], offset: usize, rows: &[Vec<SqlValue>]) {
page[offset] = 13;
page[offset + 3..offset + 5].copy_from_slice(&(rows.len() as u16).to_be_bytes());
Expand Down
4 changes: 3 additions & 1 deletion docs/lightroom-catalog-import.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ such as a print saved inside a plain collection, or one whose parent is system-o
nearest collection set above it, or the top level, and the result names it under warnings (issues #2165, #2532).
Quick Collection and unsaved creations are system-only and not imported; publish collections live in tables the
importer doesn't read. A collection the catalog lists twice imports once; a different collection with the id of an
earlier one is left out with a warning naming both, and the photos listed under that id go to the first. Archived
earlier one is left out with a warning naming both, and the photos listed under that id go to the first. A
collection member whose `collection` is not a whole number (missing, NULL, text or a fraction) joins no album, with
one warning; it does not join the collection whose id is 0 (or 11 for 11.5). Archived
history and snapshots are source data, not native LightCraft history yet.
A Custom white balance on a raw LightCraft develops relative to its as-shot look (ARW, NEF, RW2,
RAF, CR3, CR2, PEF, SRW; Lightroom names them all `RAW`) can't be taken as Kelvin: the catalog keeps no
Expand Down
Loading