Getting started with the PicoLisp database

Opening and Writing to a Database file

: (pool "test.db")
T
: *DB
-> {1}
: (show *DB)
{1} NIL
-> {1}
: (put *DB 'a 1)
-> 1
: (put *DB 'b 2)
-> 2
: (show *DB)
{1} NIL
b 2
a 1
-> {1}
: (set *DB "Hello world")
-> "Hello world"
: (show *DB)
{1} "Hello world"
b 2
a 1
-> {1}

Why should we use *DB instead of {1}?

Creating a new external object

: (new)
-> $177264230632614
: (new T)
-> {2}
: (put *DB 'newSym '{2})
-> {2}
: (show *DB)
{1} "Hello world"
newSym {2}
b 2
a 1
-> {1}
: (put *DB 'newSym 'x 777)
-> 777
: (show *DB 'newSym)
{2} NIL
x 777
-> {2}

Committing the changes

: (commit)
-> T
$ pil +: (pool "test.db")              
-> T
: (show *DB)
{1} "Hello world"
newSym {2}
b 2
a 1
-> {1}

Database transactions: Steps in detail

  1. dbSync waits to get a lock on the root object *DB. Other processes continue reading and writing meanwhile.
  2. dbSync calls sync to synchronize with changes from other processes. We hold the shared lock, but other processes may continue reading.
  3. We make modifications to the internal state of external symbols with put>, set>, lose> etc. We - and also other processes - can still read the DB.
  4. We call (commit 'upd). commit obtains an exclusive lock (no more read operations by other processes), writes an optional transaction log, and then all modified symbols. As upd is passed to 'commit', other processes synchronize with these changes.
  5. Finally, all locks are released by ‘commit’.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mia Temma

Mia Temma

These are cross-posts from my blog https://picolisp-explored.com. I’m writing about PicoLisp for beginners. Welcome!