Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-photo

Source of release 0.1.0

0.1.0 / src / catalog.lucb

## The catalog: how luced-photo keeps its library in luce-prism. One durable Store,
## `library.prism` in the library bundle, holds the identity `photos`:
##
##     /state    next                 the id the next imported photo takes
##     /p<id>    photo                path (relative to the bundle), name, kind,
##                                    bytes, sha256, imported, captured, captured_unix,
##                                    offset, width, height, orientation, make, model,
##                                    lens, focal, aperture, exposure, shutter, iso,
##                                    bias, rating, flag, label, preset, folder, settings
##                                    (the develop's text form, luce-develop
##                                    settings.text_of; "" when untouched)
##
## The importer writes photos through a Batch on its thread; the window reads the
## whole identity into a PhotoIndex and writes ratings, flags and labels back.
import interop
import memory
from luce_prism import prism
from store_rows import wait_ms, text_of, integer_of, number_of
import store_rows
from luce_std.collections import Buffer, TextPool, List

## The folder of a photo in the Trash: out of every view but the Trash's,
## until it is put back or the Trash is emptied.
pub let trash_folder: i64 = -1

## The identity every photo row lives in.
pub let photos_identity: str = "photos"

## One photo, as the catalog stores it; text fields are views of their owner.
pub struct Photo:
    pub var id: i64
    pub var path: str
    pub var name: str
    pub var kind: str
    pub var bytes: i64
    pub var sha256: str
    ## Seconds since 1970 UTC when it was imported.
    pub var imported: i64
    ## Local capture time, "YYYY-MM-DDTHH:MM:SS", and as seconds since 1970 (UTC
    ## when `offset` is known, else as if local time were UTC).
    pub var captured: str
    pub var captured_unix: i64
    pub var offset: str
    pub var width: i64
    pub var height: i64
    pub var orientation: i64
    pub var make: str
    pub var model: str
    pub var lens: str
    pub var focal: f64
    pub var aperture: f64
    ## Exposure time in seconds, and as shown ("1/250").
    pub var exposure: f64
    pub var shutter: str
    pub var iso: i64
    pub var bias: f64
    ## 0–5 stars; flag 1 picked, -1 rejected; label 0 none or 1–7.
    pub var rating: i64
    pub var flag: i64
    pub var label: i64
    ## What it was given last from the Presets pane (preset_marks.luc): "" none,
    ## "start/<mode>", "look/<look>" or "preset/<id>".
    pub var preset: str
    ## The library folder it is in (folders.lucb's id), 0 none, trash_folder in the Trash.
    pub var folder: i64
    pub var settings: str

## The element path of photo `id`, onto `out`.
func photo_path(id: i64, out: Buffer*) -> !:
    out.clear()
    try out.put(b"/p")
    try out.put_number(id)

## Mount the photos identity unless the store has it already.
pub func ensure(store: prism.Store*) -> !:
    try store_rows.mount(store, photos_identity)

## One commit's worth of writes to the photos identity.
pub struct Batch:
    var session: interop.Reference[prism.Session]?
    var path: Buffer
    ## How many writes the commit will carry.
    pub var count: usize

    pub static func begin(store: prism.Store*) -> Batch!:
        try ensure(store)
        var batch: Batch
        batch.session = try store.begin_identity(photos_identity)
        return batch

    func session_of() -> prism.Session*!:
        let held = self.session else error(prism.closed, "the catalog batch is closed")
        return try held.get()

    func integer(path: str, name: str, value: i64) -> !:
        let held = try prism.Value.integer(value)
        defer held.release()
        try (try self.session_of()).set(path, name, held)

    func number(path: str, name: str, value: f64) -> !:
        let held = try prism.Value.number(value)
        defer held.release()
        try (try self.session_of()).set(path, name, held)

    func text(path: str, name: str, value: str) -> !:
        let held = try prism.Value.text(value)
        defer held.release()
        try (try self.session_of()).set(path, name, held)

    func boolean(path: str, name: str, value: bool) -> !:
        let held = try prism.Value.boolean(value)
        defer held.release()
        try (try self.session_of()).set(path, name, held)

    ## Add photo `photo`, or replace every field of the one with its id.
    pub func put(photo: const Photo*) -> !:
        try photo_path(photo.id, &self.path)
        let path = self.path.text()
        let session = try self.session_of()
        if not try session.contains(path):
            try session.add(path, "photo")
        try self.integer(path, "id", photo.id)
        try self.text(path, "path", photo.path)
        try self.text(path, "name", photo.name)
        try self.text(path, "kind", photo.kind)
        try self.integer(path, "bytes", photo.bytes)
        try self.text(path, "sha256", photo.sha256)
        try self.integer(path, "imported", photo.imported)
        try self.text(path, "captured", photo.captured)
        try self.integer(path, "captured_unix", photo.captured_unix)
        try self.text(path, "offset", photo.offset)
        try self.integer(path, "width", photo.width)
        try self.integer(path, "height", photo.height)
        try self.integer(path, "orientation", photo.orientation)
        try self.text(path, "make", photo.make)
        try self.text(path, "model", photo.model)
        try self.text(path, "lens", photo.lens)
        try self.number(path, "focal", photo.focal)
        try self.number(path, "aperture", photo.aperture)
        try self.number(path, "exposure", photo.exposure)
        try self.text(path, "shutter", photo.shutter)
        try self.integer(path, "iso", photo.iso)
        try self.number(path, "bias", photo.bias)
        try self.integer(path, "rating", photo.rating)
        try self.integer(path, "flag", photo.flag)
        try self.integer(path, "label", photo.label)
        try self.text(path, "preset", photo.preset)
        try self.integer(path, "folder", photo.folder)
        try self.text(path, "settings", photo.settings)
        self.count += 1

    ## Set one integer field (`rating`, `flag`, `label` or `folder`) of photo `id`; a photo
    ## no longer in the catalog is passed over.
    pub func set_mark(id: i64, name: str, value: i64) -> !:
        try photo_path(id, &self.path)
        let path = self.path.text()
        if not try (try self.session_of()).contains(path):
            return
        try self.integer(path, name, value)
        self.count += 1

    ## Set photo `id`'s preset mark; a photo no longer in the catalog is passed over.
    pub func set_preset(id: i64, mark: str) -> !:
        try photo_path(id, &self.path)
        let path = self.path.text()
        if not try (try self.session_of()).contains(path):
            return
        try self.text(path, "preset", mark)
        self.count += 1

    ## Remove photo `id`'s row.
    pub func remove(id: i64) -> !:
        try photo_path(id, &self.path)
        let path = self.path.text()
        let session = try self.session_of()
        if not try session.contains(path):
            return
        try session.remove(path)
        self.count += 1

    ## Store photo `id`'s develop settings (text form).
    pub func set_settings(id: i64, settings: str) -> !:
        try photo_path(id, &self.path)
        let path = self.path.text()
        if not try (try self.session_of()).contains(path):
            return
        try self.text(path, "settings", settings)
        self.count += 1

    ## Record that the next photo takes id `next`.
    pub func set_next(next: i64) -> !:
        let session = try self.session_of()
        if not try session.contains("/state"):
            try session.add("/state", "state")
        try self.integer("/state", "next", next)
        self.count += 1

    ## Commit the writes, if there are any, and go on writing in a new commit.
    pub func flush(store: prism.Store*) -> !:
        if self.count == 0:
            return
        _ = try (try self.session_of()).commit(wait_ms = wait_ms)
        if let held = self.session:
            held.release()
        self.session = none
        self.count = 0
        self.session = try store.begin_identity(photos_identity)

    ## Commit the writes, if there are any, and end the batch.
    pub func commit() -> !:
        if self.count > 0:
            _ = try (try self.session_of()).commit(wait_ms = wait_ms)
        self.close()

    pub func close():
        if let held = self.session:
            held.release()
        self.session = none
        self.path.destroy()

## Apply the identity's unbaked commits, so later reads and opens do not replay them.
pub func bake(store: prism.Store*) -> !:
    if try store.mounted(photos_identity):
        _ = try store.bake_identity(photos_identity, wait_ms = wait_ms)

# mark: reading =============================================================================

## Every photo row, with its text kept in `pool`, and the next id.
pub struct Rows:
    pub var photos: List[Photo]
    pub var pool: TextPool
    pub var next: i64

    pub func close():
        self.photos.destroy()
        self.pool.destroy()

## A photo row's text field `name`, kept in `into`'s pool.
func kept(into: Rows*, view: const prism.RowView*, name: str) -> str!:
    return try into.pool.keep_text((try text_of(view, name)).bytes)

func collect(context: void*?, view: const prism.RowView*) -> !:
    let into = (Rows*)(context else error(prism.invalid, "no rows"))
    let path = view.path()
    if path == "/state":
        into.next = try integer_of(view, "next")
        return
    if path.length < 3 or path.bytes[1] != 'p':
        return
    try into.photos.append(Photo(
        id = try integer_of(view, "id"), path = try kept(into, view, "path"), name = try kept(into, view, "name"),
        kind = try kept(into, view, "kind"), bytes = try integer_of(view, "bytes"),
        sha256 = try kept(into, view, "sha256"), imported = try integer_of(view, "imported"),
        captured = try kept(into, view, "captured"), captured_unix = try integer_of(view, "captured_unix"),
        offset = try kept(into, view, "offset"), width = try integer_of(view, "width"),
        height = try integer_of(view, "height"), orientation = try integer_of(view, "orientation"),
        make = try kept(into, view, "make"), model = try kept(into, view, "model"),
        lens = try kept(into, view, "lens"), focal = try number_of(view, "focal"),
        aperture = try number_of(view, "aperture"), exposure = try number_of(view, "exposure"),
        shutter = try kept(into, view, "shutter"), iso = try integer_of(view, "iso"),
        bias = try number_of(view, "bias"), rating = try integer_of(view, "rating"),
        flag = try integer_of(view, "flag"), label = try integer_of(view, "label"), preset = try kept(into, view, "preset"), folder = try integer_of(view, "folder"),
        settings = try kept(into, view, "settings")))

## Every photo in the catalog, in the order they were added.
pub func read(store: prism.Store*) -> Rows!:
    var out = Rows(next = 1)
    errdefer out.close()
    if not try store.mounted(photos_identity):
        return out
    let snap = try store.snapshot_identity(photos_identity)
    defer snap.release()
    try (try snap.get()).each_child("/", collect, &out)
    for photo in out.photos.view():
        if photo.id >= out.next:
            out.next = photo.id + 1
    return out