dymokomi/luced-photo
Source of release 0.1.1
0.1.1 / src / photo_index.lucb
## The library as the window sees it: every photo row read from the catalog once,
## in Base, and the order and filter the grid shows them in, with one accessor per
## field for Luce. Marks written through `set_marks` change the catalog and the
## caller updates the index with `set_rating`, `set_flag` and `set_label`.
import c
import interop
import memory
import strings
from luce_std import files
import library
from luce_prism import prism
import catalog
import store_rows
from catalog import Photo
from luce_std.collections import List, Buffer
pub struct PhotoIndex:
var rows: catalog.Rows
## Positions of `rows.photos` in display order, after the filter.
var order: List[u32]
## The filter: the fewest stars shown; which flags (0 every photo, 1 picked
## only, -1 rejected only, 2 all but rejected); a label (0 any).
var minimum_rating: i64
var flags: i64
var wanted_label: i64
## The search, lower case ("" none), and the presets whose names hold it.
var search: Buffer
var search_presets: List[i64]
## The folders shown (a folder and its subfolders); empty: every photo.
var folders: List[i64]
## How many photos the filter shows.
pub func count() -> i64:
return (i64)self.order.length()
## How many photos the library holds.
## How many photos the library holds, the Trash's left out.
pub func total() -> i64:
var count: i64 = 0
for photo in self.rows.photos.view():
if photo.folder != catalog.trash_folder:
count += 1
return count
func photo(at: i64) -> Photo:
if at < 0 or at >= (i64)self.order.length():
trap("no photo at that position in the library")
return self.rows.photos.at((usize)self.order.at((usize)at))
func stored(at: i64) -> Photo*:
if at < 0 or at >= (i64)self.order.length():
trap("no photo at that position in the library")
return &self.rows.photos.items()[(usize)self.order.at((usize)at)]
pub func id(at: i64) -> i64:
return self.photo(at).id
pub func path(at: i64) -> str:
return self.photo(at).path
pub func name(at: i64) -> str:
return self.photo(at).name
pub func kind(at: i64) -> str:
return self.photo(at).kind
pub func bytes(at: i64) -> i64:
return self.photo(at).bytes
pub func captured(at: i64) -> str:
return self.photo(at).captured
pub func width(at: i64) -> i64:
return self.photo(at).width
pub func height(at: i64) -> i64:
return self.photo(at).height
pub func make(at: i64) -> str:
return self.photo(at).make
pub func model(at: i64) -> str:
return self.photo(at).model
pub func lens(at: i64) -> str:
return self.photo(at).lens
pub func focal(at: i64) -> f64:
return self.photo(at).focal
pub func aperture(at: i64) -> f64:
return self.photo(at).aperture
pub func shutter(at: i64) -> str:
return self.photo(at).shutter
pub func iso(at: i64) -> i64:
return self.photo(at).iso
pub func rating(at: i64) -> i64:
return self.photo(at).rating
pub func flag(at: i64) -> i64:
return self.photo(at).flag
pub func label(at: i64) -> i64:
return self.photo(at).label
pub func preset(at: i64) -> str:
return self.photo(at).preset
pub func folder(at: i64) -> i64:
return self.photo(at).folder
pub func edited(at: i64) -> bool:
return self.photo(at).settings.length > 0
## The develop settings' text form ("" when untouched).
pub func settings(at: i64) -> str:
return self.photo(at).settings
## The position of photo `id` under the filter, or -1.
pub func position_of(id: i64) -> i64:
for (at, position) in self.order.view().indexed():
if self.rows.photos.at((usize)position).id == id:
return (i64)at
return -1
pub func set_rating(at: i64, rating: i64):
self.stored(at).rating = rating
pub func set_flag(at: i64, flag: i64):
self.stored(at).flag = flag
pub func set_label(at: i64, label: i64):
self.stored(at).label = label
pub func set_preset(at: i64, mark: str) -> !:
self.stored(at).preset = try self.rows.pool.keep_text(mark.bytes)
pub func set_folder(at: i64, folder: i64):
self.stored(at).folder = folder
## Move every photo in folder `from` (whatever the filter) into folder
## `to`, in the catalog `store` and here, in one commit.
pub func move_folder_photos(store: interop.Reference[prism.Store], from_folder: i64, to: i64) -> !:
let held = try store.get()
var batch = try catalog.Batch.begin(held)
defer batch.close()
let photos = self.rows.photos.items()
for at in 0..<photos.length:
if photos[at].folder == from_folder:
try batch.set_mark(photos[at].id, "folder", to)
photos[at].folder = to
try batch.commit()
## How many of the library's photos (whatever the filter) are in one of `folders`.
pub func count_in(folders: const i64[]) -> i64:
var count: i64 = 0
for photo in self.rows.photos.view():
for folder in folders:
if photo.folder == folder:
count += 1
break
return count
pub func set_settings(at: i64, settings: str) -> !:
let kept = try self.rows.pool.keep_text(settings.bytes)
self.stored(at).settings = kept
## Show only photos with at least `minimum_rating` stars, the flags `flags`
## selects (0 every photo, 1 picked, -1 rejected, 2 all but rejected), label
## `label` (0 any), and holding `search` (ignoring case) in their name,
## camera, lens or capture date, or given one of `presets`; ordered by
## capture time.
pub func filter(minimum_rating: i64, flags: i64, label: i64, search: str, presets: const i64[], folders: const i64[]) -> !:
self.minimum_rating = minimum_rating
self.flags = flags
self.wanted_label = label
self.search.clear()
for byte in search.bytes:
try self.search.put_byte(store_rows.lower(byte))
self.search_presets.clear()
for id in presets:
try self.search_presets.append(id)
self.folders.clear()
for id in folders:
try self.folders.append(id)
try self.arrange()
## Whether `folder` is one of the folders shown.
func in_folders(folder: i64) -> bool:
for id in self.folders.view():
if id == folder:
return true
return false
## Whether `photo` holds the search.
func matches(photo: const Photo*) -> bool:
let wanted = self.search.text()
if wanted.length == 0:
return true
let given = saved_id(photo.preset)
for id in self.search_presets.view():
if given == id:
return true
if holds(photo.preset, wanted):
return true
for field in [photo.name, photo.make, photo.model, photo.lens, photo.captured, photo.kind]:
if holds(field, wanted):
return true
return false
func arrange() -> !:
self.order.clear()
for (at, photo) in self.rows.photos.view().indexed():
if photo.rating < self.minimum_rating:
continue
if (self.flags == 1 and photo.flag != 1) or (self.flags == -1 and photo.flag != -1) or (self.flags == 2 and photo.flag == -1):
continue
if self.wanted_label != 0 and photo.label != self.wanted_label:
continue
if not self.matches(&photo):
continue
# Every photo but the Trash's, or those of the folders shown.
if self.folders.length() == 0 and photo.folder == catalog.trash_folder:
continue
if self.folders.length() > 0 and not self.in_folders(photo.folder):
continue
try self.order.append((u32)at)
try sort_by_capture(self.order.items(), self.rows.photos.view())
## Delete every photo in the Trash of the library at `root` (catalog `store`):
## its original, its cached pictures and its row, in one commit. How many.
## Files already gone are passed over.
pub func empty_trash(store: interop.Reference[prism.Store], root: str) -> i64!:
let held = try store.get()
var batch = try catalog.Batch.begin(held)
defer batch.close()
var path: Buffer
defer path.destroy()
var count: i64 = 0
for photo in self.rows.photos.view():
if photo.folder != catalog.trash_folder:
continue
try library.join_into(root, photo.path, &path)
try remove_if_there(path.text())
for kind in ["thumbs", "previews", "developed"]:
try library.cache_path(root, kind, photo.id, &path)
try remove_if_there(path.text())
try library.stamp_path(root, photo.id, &path)
try remove_if_there(path.text())
try batch.remove(photo.id)
count += 1
try batch.commit()
return count
pub func close():
self.rows.close()
self.order.destroy()
self.search.destroy()
self.search_presets.destroy()
self.folders.destroy()
pub let photo_index_type: interop.Type[PhotoIndex] = interop.Type[PhotoIndex]("PhotoIndex", PhotoIndex.close, closeable = true)
## Whether photo `a` comes before photo `b`: by capture time, then by id.
func before(a: const Photo*, b: const Photo*) -> bool:
if a.captured_unix != b.captured_unix:
return a.captured_unix < b.captured_unix
return a.id < b.id
## Sort `positions` of `photos` by capture time: a stable merge sort.
func sort_by_capture(positions: u32[], photos: const Photo[]) -> !:
if positions.length < 2:
return
let scratch = try new u32[positions.length] --- in memory.heap
defer free(scratch) in memory.heap
var width: usize = 1
var source = positions
var target = scratch
while width < positions.length:
var low: usize = 0
while low < positions.length:
let middle = low + width if low + width < positions.length else positions.length
let high = low + 2 * width if low + 2 * width < positions.length else positions.length
var left = low
var right = middle
var out = low
while out < high:
if left < middle and (right >= high or not before(&photos[source[right]], &photos[source[left]])):
target[out] = source[left]
left += 1
else:
target[out] = source[right]
right += 1
out += 1
low = high
let swap = source
source = target
target = swap
width *= 2
if source.data != positions.data:
memory.copy(positions, source, positions.length)
## The catalog of `store` as an index Luce holds, every photo shown.
pub func read_index(store: interop.Reference[prism.Store]) -> interop.Reference[PhotoIndex]!:
var rows = try catalog.read(try store.get())
errdefer rows.close()
let index = try new PhotoIndex(rows = rows, flags = 0) in memory.heap
errdefer free(index) in memory.heap
try index.arrange()
return try interop.Reference[PhotoIndex].adopt(index, photo_index_type, memory.heap)
## The saved preset's id a preset mark names ("preset/12"), or -1.
pub func saved_id(mark: str) -> i64:
let bytes = mark.bytes
if bytes.length < 8 or (str)bytes[..<7] != "preset/":
return -1
var id: i64 = 0
for byte in bytes[7..]:
if byte < '0' or byte > '9':
return -1
id = id * 10 + (i64)(byte - '0')
return id
## Set field `name` ("rating", "flag", "label" or "folder") to `value` on photos
## `ids` in the catalog, in one commit.
pub func set_marks(store: interop.Reference[prism.Store], ids: const i64[], name: str, value: i64) -> !:
if not (name == "rating" or name == "flag" or name == "label" or name == "folder"):
error(prism.invalid, "only a rating, flag, label or folder is set this way")
let held = try store.get()
var batch = try catalog.Batch.begin(held)
defer batch.close()
for id in ids:
try batch.set_mark(id, name, value)
try batch.commit()
func remove_if_there(path: str) -> !:
let terminated = try strings.copy(path)
defer strings.release(terminated)
if files.exists((c.str)terminated):
try files.remove_file((c.str)terminated)
## Give photos `ids` the preset mark `mark` in the catalog, in one commit.
pub func set_preset_marks(store: interop.Reference[prism.Store], ids: const i64[], mark: str) -> !:
let held = try store.get()
var batch = try catalog.Batch.begin(held)
defer batch.close()
for id in ids:
try batch.set_preset(id, mark)
try batch.commit()
## Store photo `id`'s develop settings (their text form) in the catalog.
pub func save_settings(store: interop.Reference[prism.Store], id: i64, settings: str) -> !:
let held = try store.get()
var batch = try catalog.Batch.begin(held)
defer batch.close()
try batch.set_settings(id, settings)
try batch.commit()
## Whether `text` holds `wanted` (lower case), ignoring ASCII case.
func holds(text: str, wanted: str) -> bool:
let a = text.bytes
let b = wanted.bytes
if b.length > a.length:
return false
for start in 0..=(a.length - b.length):
var all = true
for at in 0..<b.length:
if store_rows.lower(a[start + at]) != b[at]:
all = false
break
if all:
return true
return false
test "a search holds ignoring case":
assert(holds("LEICA M11-D", "m11") and holds("Summilux", "lux") and not holds("Leica", "nikon") and holds("x", ""))
test "photos sort by capture time, then id":
var photos: Photo[4]
photos[0] = Photo(id = 1, captured_unix = 30)
photos[1] = Photo(id = 2, captured_unix = 10)
photos[2] = Photo(id = 3, captured_unix = 30)
photos[3] = Photo(id = 4, captured_unix = 20)
var positions: u32[4] = [0, 1, 2, 3]
try sort_by_capture(positions, photos)
assert(positions[0] == 1 and positions[1] == 3 and positions[2] == 0 and positions[3] == 2)