2016-01-08 17:28:31 +00:00
|
|
|
# The Store {#sec:thestore}
|
|
|
|
|
2016-07-08 16:21:05 +00:00
|
|
|
The store is where all the good things happen.
|
|
|
|
The store is basically just a directory on the filesystem imag manages and keeps
|
|
|
|
its state in.
|
|
|
|
|
2017-10-14 19:52:16 +00:00
|
|
|
One could say that the store is simply a database, and it really is. We opted
|
2016-07-08 16:21:05 +00:00
|
|
|
to go for plain text, though, as we believe that plain text is the only sane way
|
2017-10-14 19:52:16 +00:00
|
|
|
to do such a thing, especially because the amount of data which is to be
|
|
|
|
expected in this domain is in the lower Megabytes range and even if it is
|
|
|
|
_really_ much won't exceed the Gigabytes ever.
|
|
|
|
|
|
|
|
Having a storage format which is plain-text based is the superior approach, as
|
|
|
|
text editors will always be there.
|
|
|
|
|
2016-07-08 16:21:05 +00:00
|
|
|
A user should always be able to read her data without great effort and putting
|
|
|
|
everything in a _real_ database like sqlite or even postgresql would need a user
|
|
|
|
to install additional software just to read his own data. We don't want that.
|
2017-10-14 19:52:16 +00:00
|
|
|
Text is readable until the worlds end and we think it is therefore better to
|
2016-07-08 16:21:05 +00:00
|
|
|
store the data in plain text.
|
|
|
|
|
|
|
|
The following sections describe the store and the file format we use to store
|
|
|
|
data. One may skip the following sections, they are included for users who want
|
|
|
|
to dig into the store with their editors.
|
2016-01-19 14:58:22 +00:00
|
|
|
|
2016-07-08 16:21:05 +00:00
|
|
|
## File Format {#sec:thestore:fileformat}
|
2016-01-19 14:58:22 +00:00
|
|
|
|
2017-10-14 19:52:16 +00:00
|
|
|
The contents of the store are encoded in UTF-8.
|
|
|
|
A normal text editor (like `vim` or the other one) will always be sufficient to
|
|
|
|
dig into the store and modify files.
|
|
|
|
For simple viewing even a pager (like `less`) is sufficient.
|
2016-01-19 14:58:22 +00:00
|
|
|
|
2016-07-08 16:21:05 +00:00
|
|
|
Each entry in the store consists of two parts:
|
2016-01-19 14:58:22 +00:00
|
|
|
|
2016-07-08 16:21:05 +00:00
|
|
|
1. Header
|
|
|
|
1. Content
|
2016-01-19 14:58:22 +00:00
|
|
|
|
2016-07-08 16:21:05 +00:00
|
|
|
The following section describe their purpose.
|
2016-01-19 14:58:22 +00:00
|
|
|
|
2016-07-08 16:21:05 +00:00
|
|
|
### Header Format {#sec:thestore:fileformat:header}
|
2016-01-19 14:58:22 +00:00
|
|
|
|
2016-07-08 16:21:05 +00:00
|
|
|
The header format is where imag stores its data. The header is an area at the
|
|
|
|
top of every file which is seperated from the content part by three dashes
|
|
|
|
(`---`). Between these three dashes there is structured data. imag uses `TOML`
|
|
|
|
as data format for this structured data, because it fits best and the available
|
|
|
|
`TOML` parser for the rust programming language is really good.
|
2016-01-19 14:58:22 +00:00
|
|
|
|
2016-07-08 16:21:05 +00:00
|
|
|
The header can contain any amount of data, but modules (see @sec:modules) are
|
|
|
|
restricted in their way of altering the data.
|
2016-01-19 14:58:22 +00:00
|
|
|
|
2016-07-08 16:21:05 +00:00
|
|
|
So normally there are several sections in the header. One section (`[imag]`) is
|
|
|
|
always present. It contains a `version` field, which tells imag which version
|
2017-10-14 19:52:16 +00:00
|
|
|
this file was created with.
|
2016-01-19 14:58:22 +00:00
|
|
|
|
2016-07-08 16:21:05 +00:00
|
|
|
Other sections are named like the modules which created them. Every module is
|
|
|
|
allowed to store arbitrary data under its own section and a module may never
|
2017-10-14 19:52:16 +00:00
|
|
|
read other sections than its own.
|
|
|
|
|
|
|
|
These conventions are not enforced by imag itself, though.
|
2016-01-10 16:34:53 +00:00
|
|
|
|
|
|
|
### Content Format {#sec:thestore:fileformat:content}
|
|
|
|
|
2016-07-08 16:21:05 +00:00
|
|
|
The content is the part of the file where the user is free to enter any textual
|
|
|
|
content. The content may be rendered as Markdown or other markup format for the
|
|
|
|
users convenience. The store does never expect and specific markup and actually
|
2017-10-14 19:52:16 +00:00
|
|
|
the markup implementation is not inside the very core of imag.
|
2016-07-08 16:21:05 +00:00
|
|
|
|
|
|
|
Technically it would be possible that the content part of a file is used to
|
2017-10-14 19:52:16 +00:00
|
|
|
store binary data.
|
|
|
|
We don't want this, though, as it is contrary to the goals of imag.
|
2016-01-10 16:34:53 +00:00
|
|
|
|
2016-01-12 12:07:04 +00:00
|
|
|
### Example {#sec:thestore:fileformat:example}
|
|
|
|
|
|
|
|
An example for a file in the store follows.
|
|
|
|
|
2016-07-30 19:02:21 +00:00
|
|
|
```text
|
|
|
|
|
2016-01-12 12:07:04 +00:00
|
|
|
---
|
|
|
|
[imag]
|
2018-02-10 15:51:31 +00:00
|
|
|
version = "0.7.0"
|
2016-01-19 15:10:31 +00:00
|
|
|
|
2016-07-30 19:02:21 +00:00
|
|
|
[note]
|
|
|
|
name = "foo"
|
2017-09-21 10:21:32 +00:00
|
|
|
|
|
|
|
[link]
|
|
|
|
internal = ["some/other/imag/entry"]
|
2016-01-12 12:07:04 +00:00
|
|
|
---
|
|
|
|
|
|
|
|
This is an example text, written by the user.
|
|
|
|
|
|
|
|
```
|
|
|
|
|
2016-01-08 17:28:31 +00:00
|
|
|
## File organization {#sec:thestore:fileorganization}
|
|
|
|
|
2016-01-10 17:07:05 +00:00
|
|
|
The "Entries" are stored as files in the "Store", which is a directory the
|
2016-07-08 16:21:05 +00:00
|
|
|
user has access to. The store may exist in the users Home-directory or any
|
2017-10-14 13:28:42 +00:00
|
|
|
other directory the user has read-write-access to.
|
2016-07-08 16:21:05 +00:00
|
|
|
|
|
|
|
Each module stores its data in an own subdirectory in the store. This is because
|
|
|
|
we like to keep things ordered and clean, not because it is technically
|
|
|
|
necessary.
|
|
|
|
|
|
|
|
We name the path to a file in the store "Store id" or "Storepath" and we often
|
|
|
|
refer to it by using the store location as root.
|
|
|
|
So if the store exists in `/home/user/store/`, a file with the storepath
|
2016-01-10 17:07:05 +00:00
|
|
|
`/example.file` is (on the filesystem) located at
|
|
|
|
`/home/user/store/example.file`.
|
|
|
|
|
2017-10-14 13:28:42 +00:00
|
|
|
By convention, each `libimagentry<name>` and `libimag<name>` module stores its
|
2017-10-14 19:52:16 +00:00
|
|
|
entries in in `/<name>/`.
|
2016-01-19 13:34:58 +00:00
|
|
|
|
2017-10-14 13:28:42 +00:00
|
|
|
So, the pattern for the storepath is
|
2016-01-19 13:34:58 +00:00
|
|
|
|
|
|
|
```
|
2017-10-14 13:28:42 +00:00
|
|
|
/<module name>/<optional sub-folders>/<file name>
|
2016-01-19 13:34:58 +00:00
|
|
|
```
|
|
|
|
|
2016-07-08 16:21:05 +00:00
|
|
|
Any number of subdirectories may be used, so creating folder hierarchies is
|
2017-10-14 19:52:16 +00:00
|
|
|
possible and valid.
|
|
|
|
A file "example" for a module "module" could be stored in sub-folders like this:
|
2016-01-19 13:34:58 +00:00
|
|
|
|
|
|
|
```
|
2017-10-14 13:28:42 +00:00
|
|
|
/module/some/sub/folder/example
|
2016-01-19 13:34:58 +00:00
|
|
|
```
|
|
|
|
|
2017-06-17 19:07:30 +00:00
|
|
|
## Backends {#sec:thestore:backends}
|
|
|
|
|
|
|
|
The store itself also has a backend. This backend is the "filesystem
|
|
|
|
abstraction" code.
|
|
|
|
|
|
|
|
Note: This is a very core thing. Casual users might want to skip this section.
|
|
|
|
|
|
|
|
### Problem {#sec:thestore:backends:problem}
|
|
|
|
|
|
|
|
First, we had a compiletime backend for the store.
|
2017-10-14 19:52:16 +00:00
|
|
|
This means that the actual filesystem operations were compiled into the store
|
2017-06-17 19:07:30 +00:00
|
|
|
either as real filesystem operations (in a normal debug or release build) but as
|
|
|
|
a in-memory variant in the 'test' case.
|
|
|
|
So tests did not hit the filesystem when running.
|
|
|
|
This gave us us the possibility to run tests concurrently with multiple stores
|
2017-10-14 19:52:16 +00:00
|
|
|
that did not interfere with each other.
|
2017-06-17 19:07:30 +00:00
|
|
|
|
|
|
|
This approach worked perfectly well until we started to test not the
|
|
|
|
store itself but crates that depend on the store implementation.
|
|
|
|
When running tests in a crate that depends on the store, the store
|
|
|
|
itself was compiled with the filesystem-hitting-backend.
|
|
|
|
This was problematic, as tests could not be implemented without hitting
|
2017-10-14 19:52:16 +00:00
|
|
|
the filesystem and mess up other currently-running tests.
|
2017-06-17 19:07:30 +00:00
|
|
|
|
2017-10-14 19:52:16 +00:00
|
|
|
Hence we implemented store backends.
|
2017-06-17 19:07:30 +00:00
|
|
|
|
|
|
|
### Implementation {#sec:thestore:backends:implementation}
|
|
|
|
|
|
|
|
The filesystem is abstracted via a trait `FileAbstraction` which
|
|
|
|
contains the essential functions for working with the filesystem.
|
|
|
|
|
|
|
|
Two implementations are provided in the code:
|
|
|
|
|
|
|
|
* FSFileAbstraction
|
|
|
|
* InMemoryFileAbstraction
|
|
|
|
|
|
|
|
whereas the first actually works with the filesystem and the latter
|
|
|
|
works with an in-memory HashMap that is used as filesystem.
|
|
|
|
|
|
|
|
Further, the trait `FileAbstractionInstance` was introduced for
|
|
|
|
functions which are executed on actual instances of content from the
|
|
|
|
filesystem, which was previousely tied into the general abstraction
|
|
|
|
mechanism.
|
|
|
|
|
|
|
|
So, the `FileAbstraction` trait is for working with the filesystem, the
|
|
|
|
`FileAbstractionInstance` trait is for working with instances of content
|
|
|
|
from the filesystem (speak: actual Files).
|
|
|
|
|
|
|
|
In case of the `FSFileAbstractionInstance`, which is the implementation
|
|
|
|
of the `FileAbstractionInstance` for the actual filesystem-hitting code,
|
|
|
|
the underlying resource is managed like with the old code before.
|
|
|
|
The `InMemoryFileAbstractionInstance` implementation is corrosponding to
|
|
|
|
the `InMemoryFileAbstraction` implementation - for the in-memory
|
|
|
|
"filesystem".
|
|
|
|
|
|
|
|
## The StdIo backend {#sec:thestore:backends:stdio}
|
|
|
|
|
|
|
|
Sidenote: The name is "StdIo" because its main purpose is Stdin/Stdio, but it
|
|
|
|
is abstracted over Read/Write actually, so it is also possible to use this
|
|
|
|
backend in other ways, too.
|
|
|
|
|
|
|
|
### Why? {#sec:thestore:backends:stdio:why}
|
|
|
|
|
|
|
|
This is a backend for the imag store which is created
|
|
|
|
from stdin, by piping contents into the store (via JSON or TOML) and piping the
|
2017-10-14 19:52:16 +00:00
|
|
|
store contents (as JSON or TOML) to stdout when the backend is destructed.
|
2017-06-17 19:07:30 +00:00
|
|
|
|
|
|
|
This is one of some components which make command-chaining in imag possible.
|
|
|
|
With this, the application does not have to know whether the store actually
|
|
|
|
lives on the filesystem or just "in memory".
|
|
|
|
|
|
|
|
### Mappers {#sec:thestore:backends:stdio:mappers}
|
|
|
|
|
|
|
|
The backend contains a "Mapper" which defines how the contents get mapped into
|
|
|
|
the in-memory store representation: A JSON implementation or a TOML
|
|
|
|
implementation are possible.
|
|
|
|
|
|
|
|
The following section assumes a JSON mapper.
|
|
|
|
|
2017-10-14 19:52:16 +00:00
|
|
|
The mapper reads the JSON, parses it and translates it to a `Entry`.
|
|
|
|
Then, the entry is made available to the store codebase.
|
2017-06-17 19:07:30 +00:00
|
|
|
To summarize what we do right now, lets have a look at the awesome ascii-art
|
|
|
|
below:
|
|
|
|
|
|
|
|
```
|
2017-06-18 10:16:33 +00:00
|
|
|
libimag*
|
|
|
|
|
|
|
|
|
v
|
|
|
|
IO Mapper Store Mapper IO
|
|
|
|
+--+---------+----------------+--------+--+
|
|
|
|
| | | | | |
|
2017-10-14 19:52:16 +00:00
|
|
|
JSON -> Entry -> JSON
|
2017-06-17 19:07:30 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
This is what gets translated where for one imag call with a stdio store backend.
|
|
|
|
|
|
|
|
### The JSON Mapper {#sec:thestore:backends:stdio:json}
|
|
|
|
|
|
|
|
The JSON mapper maps JSON which is read from a source into a HashMap which
|
|
|
|
represents the in-memory filesystem.
|
|
|
|
|
|
|
|
The strucure is as follows:
|
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
2018-02-10 15:51:31 +00:00
|
|
|
"version": "0.7.0",
|
2017-06-17 19:07:30 +00:00
|
|
|
"store": {
|
2017-06-18 10:16:33 +00:00
|
|
|
"example": {
|
2017-06-17 19:07:30 +00:00
|
|
|
"header": {
|
|
|
|
"imag": {
|
2018-02-10 15:51:31 +00:00
|
|
|
"version": "0.7.0",
|
2017-06-17 19:07:30 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"content": "hi there!",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### TODO {#sec:thestore:backends:todo}
|
|
|
|
|
2017-06-18 10:16:33 +00:00
|
|
|
If you look at the version history of this file you will see that this
|
|
|
|
implementation has grown from something complex and probably slow to what we
|
|
|
|
have today.
|
2017-06-17 19:07:30 +00:00
|
|
|
|
2017-06-18 10:16:33 +00:00
|
|
|
Still, there's one improvement we could make: abstract all the things away so
|
|
|
|
the `libimag*` crates handle the header without knowing whether it is JSON or
|
|
|
|
TOML.
|
|
|
|
With this, we would not even have to translate JSON to TOML anymore.
|
|
|
|
We should measure whether this would have actually any performance impact before
|
|
|
|
implementing it.
|
2017-06-17 19:07:30 +00:00
|
|
|
|