summaryrefslogtreecommitdiffhomepage
path: root/gleam/src/ocda
diff options
context:
space:
mode:
authorRalph Amissah <ralph.amissah@gmail.com>2026-09-29 14:07:32 -0400
committerRalph Amissah <ralph.amissah@gmail.com>2026-09-29 14:07:32 -0400
commit8b8a8c1b61c740528e1409a2429f7ec4403f9576 (patch)
tree9bd2fa19837c187232cfc93960c9291a6fd4b4b9 /gleam/src/ocda
parentformat 2.0: abstraction as library, in D and Gleam (diff)
model 2.1: ocn_range, the verses a poem holds
The format now states, for a container, the first and last ocn of what it holds: .ocn_range in the .ssp, ocn_range_first and ocn_range_last in the .ocda.db. A poem is the one container, and since 2.1 it carries ocn 0 and comes before its verses. Both fields in the model, read by both loaders, in both languages, and written by the dump. A 2.0 database has neither column, so each loader asks sqlite whether it is there and reads a 2.0 file as having no ranges, which is what 2.0 meant; checked on a published 2.0 database, D and Gleam giving the same dump. Over the markup document samples written by spine at 2.1, the four dumps (D and Gleam, .ssp and .ocda.db) are byte identical for every document-language. (assisted by Claude-Code)
Diffstat (limited to 'gleam/src/ocda')
-rw-r--r--gleam/src/ocda/dump.gleam2
-rw-r--r--gleam/src/ocda/load_db.gleam37
-rw-r--r--gleam/src/ocda/load_ssp.gleam8
-rw-r--r--gleam/src/ocda/model.gleam4
4 files changed, 48 insertions, 3 deletions
diff --git a/gleam/src/ocda/dump.gleam b/gleam/src/ocda/dump.gleam
index a75da67..e5563ca 100644
--- a/gleam/src/ocda/dump.gleam
+++ b/gleam/src/ocda/dump.gleam
@@ -151,6 +151,8 @@ fn object(
|> kv("segment_epub_is", obj.segment_epub_is)
|> kn("parent", obj.parent_ocn)
|> kn("last_descendant", obj.last_descendant_ocn)
+ |> kn("ocn_range_first", obj.ocn_range_first)
+ |> kn("ocn_range_last", obj.ocn_range_last)
|> kv("children", ints(obj.children))
|> kv("ancestors", ints(obj.ancestors))
|> kb("dummy", obj.dummy_heading)
diff --git a/gleam/src/ocda/load_db.gleam b/gleam/src/ocda/load_db.gleam
index a5f044b..641923e 100644
--- a/gleam/src/ocda/load_db.gleam
+++ b/gleam/src/ocda/load_db.gleam
@@ -245,6 +245,8 @@ fn objects(
use hla <- decode.field(37, decode.string)
use seg_epub <- decode.field(38, decode.string)
use seg_epub_is <- decode.field(39, decode.string)
+ use range_first <- decode.field(40, decode.int)
+ use range_last <- decode.field(41, decode.int)
decode.success(#(
id,
Object(
@@ -265,6 +267,8 @@ fn objects(
anchor_tags: [],
parent_ocn: parent,
last_descendant_ocn: last_descendant,
+ ocn_range_first: range_first,
+ ocn_range_last: range_last,
children: json_ints(children),
ancestors: json_ints(ancestors),
dummy_heading: dummy != 0,
@@ -297,7 +301,12 @@ fn objects(
))
}
let rows =
- sqlight.query(object_sql(doc_id), on: conn, with: [], expecting: d)
+ sqlight.query(
+ object_sql(doc_id, has_range_columns(conn)),
+ on: conn,
+ with: [],
+ expecting: d,
+ )
|> result.unwrap([])
// Built newest-first per section and reversed once at the end. Appending
@@ -331,7 +340,29 @@ fn objects(
model.Document(..document, sections: sections)
}
-fn object_sql(doc_id: Int) -> String {
+/// 2.1 added the container range as two columns. A 2.0 database has
+/// neither, and reads as having no range, which is what 2.0 meant.
+fn has_range_columns(conn: sqlight.Connection) -> Bool {
+ let d = decode.field(0, decode.int, decode.success)
+ case
+ sqlight.query(
+ "select count(*) from pragma_table_info('objects')
+ where name = 'ocn_range_first'",
+ on: conn,
+ with: [],
+ expecting: d,
+ )
+ {
+ Ok([n]) -> n > 0
+ _ -> False
+ }
+}
+
+fn object_sql(doc_id: Int, has_range: Bool) -> String {
+ let range_cols = case has_range {
+ True -> "coalesce(ocn_range_first,0), coalesce(ocn_range_last,0)"
+ False -> "0, 0"
+ }
"select section, seq, ocn, is_a, coalesce(is_of_part,''),
coalesce(is_of_section,''), coalesce(is_of_type,''),
coalesce(heading_level,-1), coalesce(identifier,''),
@@ -350,7 +381,7 @@ fn object_sql(doc_id: Int) -> String {
coalesce(code_linenumbers,0),
coalesce(heading_ancestors_text,''), coalesce(sha256,''),
coalesce(text,''), id, coalesce(heading_lev_anchor,''),
- coalesce(segment_epub,''), coalesce(segment_epub_is,'')
+ coalesce(segment_epub,''), coalesce(segment_epub_is,''), " <> range_cols <> "
from objects where doc_id = " <> int.to_string(doc_id) <> "
order by case section
when 'head' then 1 when 'toc' then 2 when 'body' then 3
diff --git a/gleam/src/ocda/load_ssp.gleam b/gleam/src/ocda/load_ssp.gleam
index b48e1c5..ec93ef5 100644
--- a/gleam/src/ocda/load_ssp.gleam
+++ b/gleam/src/ocda/load_ssp.gleam
@@ -278,6 +278,14 @@ fn apply_property(
Object(..object, last_descendant_ocn: parse_int(value)),
True,
)
+ "ocn_range" ->
+ case numbers(value) {
+ [first, last] -> #(
+ Object(..object, ocn_range_first: first, ocn_range_last: last),
+ True,
+ )
+ _ -> #(object, True)
+ }
"children" -> #(Object(..object, children: numbers(value)), True)
"ancestors" -> #(Object(..object, ancestors: numbers(value)), True)
"dummy" -> #(Object(..object, dummy_heading: value == "true"), True)
diff --git a/gleam/src/ocda/model.gleam b/gleam/src/ocda/model.gleam
index 050a97b..48cc76e 100644
--- a/gleam/src/ocda/model.gleam
+++ b/gleam/src/ocda/model.gleam
@@ -92,6 +92,8 @@ pub type Object {
anchor_tags: List(String),
parent_ocn: Int,
last_descendant_ocn: Int,
+ ocn_range_first: Int,
+ ocn_range_last: Int,
children: List(Int),
ancestors: List(Int),
dummy_heading: Bool,
@@ -139,6 +141,8 @@ pub fn new_object() -> Object {
anchor_tags: [],
parent_ocn: 0,
last_descendant_ocn: 0,
+ ocn_range_first: 0,
+ ocn_range_last: 0,
children: [],
ancestors: [],
dummy_heading: False,