guile-wiredtiger is a library binding wiredtiger which is database library. I like it because I don't like/master SQL. Other people use it to build real databases like mongodb. Myself I've done one or two toy projects using it like planetplanet clone.
wiredtiger is low level compared to a SQL database.
I can't convince you to use it, but I can show you how it works.
Now go to guile-wiredtiger directory and fire a REPL using the following command:
Mind the dot.
Inside the REPL import the wiredtiger module:
Open a connection
Open a connection against a directory say /tmp/wt:
It's always safe to open a connection against a directory using the create argument even if there is already a database inside that directory.
a is thread-safe.
Open a session
To jump to using the database you have to open a session:
a is not thread-safe.
Create a table
wiredtiger is nosql but has a concept of table. It's a two columns layout with sub-columns. The first master column is the key. The second master column is the value.
This dichotomy is useful because the table is automatically ordered using the what is stored in the key.
Let's create simple table with a single string sub-column key, and a single string sub-column value. Yes, columns are typed. Without further addo:
It's safe to session-create a table even if it already exists in the database.
Here we created a table named kv as specified above. The key sub-column is named k and the value sub-column is named v.
Naming columns is optional but required if you want to build indices. Create an index
To create an index we use the same session-create procedure with another configuration string.
Say, we want to invert k sub-column with v sub-column ie. create an index table where the key single sub-column is v from kv table. We can use the following command:
This instruct wiredtiger to add a row to index:kv:inverse table everytime a row is added to table:kv where the key single column content is the content of the column named v in table:kv row.
The index is always synchronized with the reference table for updates, deletes and inserts.
Getting started with cursors
A is the way to search, navigate, insert and update a table. This is also used to search and navigate index tables.
Let's open a on table:kv:
Let's add a record ie. a key/value pair inside the table:
This is bit involving for our simple case of single sub-columns configuration. Define a procedure to add a record with a single procedure:
And put it to good use:
To look into the table, you also have to use the wiredtiger primitive cursor-key-set followed by cursor-search or cursor-search-near.
For instance, we can do:
The return value of cursor-value-ref is a list because there might be several sub-columns in a master column. Similarly cursor-key-ref returns a list:
Unsuprisingly this returns key because the cursor was positioned at that key using search. This is not a relevant call to do after cursor-search because cursor-search does an exact match of the key or match nothing. This is not the case of cursor-search-near which use some heuristic to find the nearest key. We will study this, but first let's define cursor-search-near star:
And try it:
As you can see cursor-search-near star ie. cursor-search-near returns 1 instead of true or false like cursor-search. cursor-search-near is very useful. I warmly recommend you have a look at its documentation. Navigate
We can verify that the table is correctly ordered. Remember we inserted the following keys: key, another and something.
So in theory we should see them appearing in the alphabetic aka. the lexicographic order.
Let's reset the cursor position and navigate the database:
All is good.
You can also mix cursor-search[-near] with cursor-next and cursor-previous. Don't forget to read wiredtiger error output ;)
To remove a record, just set cursor's key content, an remove it using cursor-remove:
Let's check that this code, is doing what it's supposed to be doing:
cursor-search returns false, which means the key is not found.
Try again with cursor-search-near star.