State Files in Monolith

State Files in Monolith

Monolith is able to save/load data in what is known as a "state file". State files were initial created as a way for Pages to have persistent data. When a page gets saved/loaded, a state file is used. State files are designed to be used for many pages at once. It is expected that one state file is used for an entire patch (or patches) created in Monolith.

A state file is mostly used to read/write Page data. The way this happens is this: a state file is opened, page save/load operations happen, then the state file is closed.

A state file is opened with monolith:state-open in Scheme, and monolith/state-open in Janet. There can be only one state opened at a time.

A state file is closed with monolith:state-close in Scheme, or monolith/state-close in Janet.

The file format of state file is a SQLite database. Every page has it's own table with a custom schema. It is not uncommon for pages to blobs of data in the msgpack serialization format. For encoding/decoding msgpack data, an embedded version of the cmp library is used.

The version of Janet that is included in Monolith has both bindings for SQLite, as well as routines for decoding msgpack blobs stored (via cmp).

This janet code below opens a state file "state.db", and reads the sequence data from some seq16 data save as "sequence".

The function monolith/msgpack-read will parse a janet buffer with msgpack data, and then return a Janet data structure.

(defn parse-steps (buf)
  (monolith/msgpack-read buf))

(def db (sqlite3/open "state.db"))

(def rows
  (sqlite3/eval
   db
   "SELECT steps from seq16 where key is \"sequence\";"))

(pp (parse-steps ((rows 0) "steps")))

(sqlite3/close db)