Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-prism

Source of release 0.6.1

0.6.1 / src / collections / topology.lucb

## Atomic incremental indexes for document paths, child order and stable node IDs.
from collections.index import Index
from path import parent_path
import strings

pub struct Topology:
    var paths: Index
    var parents: Index
    var identifiers: Index
    pub var last_id: usize

    pub func find(path: str) -> usize?:
        let bucket = self.paths.find(path) else return none
        return self.paths.buckets.items[bucket].rows.items[0]

    pub func by_id(identifier: usize) -> usize?:
        var scratch: u8[32]
        let key = strings.format(scratch, f"{identifier}") catch failure: return none
        let bucket = self.identifiers.find(key) else return none
        return self.identifiers.buckets.items[bucket].rows.items[0]

    pub func children(parent: str) -> const usize[]:
        if let bucket = self.parents.find(parent):
            return self.parents.buckets.items[bucket].rows.span()
        var empty: usize[]
        return empty

    pub func append(path: str, preferred: usize, row: usize) -> usize!:
        var identifier = preferred
        if identifier == 0 or self.by_id(identifier) != none:
            identifier = self.last_id + 1
        var scratch: u8[32]
        let key = try strings.format(scratch, f"{identifier}")
        var path_entry = try self.paths.prepare(path, row)
        defer path_entry.close()
        var parent_entry = try self.parents.prepare(parent_path(path), row)
        defer parent_entry.close()
        var id_entry = try self.identifiers.prepare(key, row)
        defer id_entry.close()
        _ = self.paths.commit(&path_entry)
        _ = self.parents.commit(&parent_entry)
        _ = self.identifiers.commit(&id_entry)
        if identifier > self.last_id:
            self.last_id = identifier
        return identifier

    pub func close():
        self.paths.close()
        self.parents.close()
        self.identifiers.close()