surrealpatch/docs/STORAGE.md

156 lines
3.8 KiB
Markdown
Raw Normal View History

2016-02-26 22:28:02 +00:00
# Storage
Surreal can be used with any key-value storage which enables range scans. This document describes how the data is stored in the storage layer, so that it can be queried and manipulated quickly and efficiently.
**Base keys**
```bash
2016-03-10 02:04:06 +00:00
{$kv} = "surreal" # This is the base key
2016-02-26 22:28:02 +00:00
```
The base key is used to separate the data used in SurrealDB from data used by other databases using the same key:value store.
```bash
2016-03-10 02:04:06 +00:00
{$ns} = "acme" # This is the name of the namespace
2016-02-26 22:28:02 +00:00
```
2016-03-10 02:04:06 +00:00
The namespace key is used to enable separation of data and multi-tenancy of databases on SurrealDB.
2016-02-26 22:28:02 +00:00
```bash
2016-03-10 02:04:06 +00:00
{$db} = "test" # This is the name of the database
2016-02-26 22:28:02 +00:00
```
The database key is used to separate data into multiple different databases under each multi-tenant installation.
**Unique ids**
Each view, table, and index is assigned a unique id, which is used instead of the name in each key:value pair. This allows for views, indexes, and tables to be deleted asynchronously, while at the same time a new one is created in its place with the same name.
**Data types**
Each data type is stored using a different symbol in the key:value pair.
```bash
! # Used to store Surreal config data
2016-03-18 16:20:36 +00:00
* # Used to store item data
~ # Used to store item diffs
¤ #
« # Used to store item edges
» # Used to store item edges
• # Used to store item events
# Used to store item links
# Used to store item links
∆ # Used to store index data
2016-02-26 22:28:02 +00:00
```
---
2016-03-10 02:04:06 +00:00
### Config
2016-02-26 22:28:02 +00:00
2016-03-10 02:04:06 +00:00
**Namespace**
2016-02-26 22:28:02 +00:00
```bash
2016-03-10 02:04:06 +00:00
/{$kv}/!/n/{$ns} "{$ns:id}"
2016-02-26 22:28:02 +00:00
# e.g.
2016-03-10 02:04:06 +00:00
/{$kv}/!/n/acme "6qh3iwp5"
2016-02-26 22:28:02 +00:00
```
2016-03-10 02:04:06 +00:00
**Database**
```bash
/{$kv}/!/d/{$ns}/{$db} "{$db:id}"
# e.g.
/{$kv}/!/d/{$ns}/test "3gt4yqk3"
```
2016-02-26 22:28:02 +00:00
2016-03-10 02:04:06 +00:00
**Table**
2016-02-26 22:28:02 +00:00
```bash
2016-03-10 02:04:06 +00:00
/{$kv}/!/t/{$ns}/{$db}/{$tb} "{$tb:id}"
2016-02-26 22:28:02 +00:00
# e.g.
2016-03-10 02:04:06 +00:00
/{$kv}/!/t/{$ns}/{$db}/people "1bd7ajq8"
2016-02-26 22:28:02 +00:00
```
2016-03-10 02:04:06 +00:00
**Field**
```bash
/{$kv}/!/f/{$ns}/{$db}/{$tb}/{$fld} "{$code}"
# e.g.
/{$kv}/!/f/{$ns}/{$db}/{$tb}/fullname "return doc.fname + doc.lname"
```
**Index**
2016-02-26 22:28:02 +00:00
```bash
2016-03-10 02:04:06 +00:00
/{$kv}/!/i/{$ns}/{$db}/{$tb}/{$idx} "{$idx:id}"
/{$kv}/!/i/{$ns}/{$db}/{$tb}/{$idx}/map "{$code:map}"
/{$kv}/!/i/{$ns}/{$db}/{$tb}/{$idx}/red "{$code:red}"
# e.g.
/{$kv}/!/i/{$ns}/{$db}/{$tb}/test "9jh1ebj4"
/{$kv}/!/i/{$ns}/{$db}/{$tb}/test/map "emit()"
/{$kv}/!/i/{$ns}/{$db}/{$tb}/test/red "return count()"
2016-02-26 22:28:02 +00:00
```
```bash
2016-03-10 02:04:06 +00:00
/{$kv}/!/i/{$ns}/{$db}/{$tb}/{$idx} "{$idx:id}"
/{$kv}/!/i/{$ns}/{$db}/{$tb}/{$idx}/col:{$cd} "{$column}"
2016-02-26 22:28:02 +00:00
# e.g
2016-03-10 02:04:06 +00:00
/{$kv}/!/i/{$ns}/{$db}/{$tb}/names "5gbq3hm5"
/{$kv}/!/i/{$ns}/{$db}/{$tb}/names/col1 "lastname"
/{$kv}/!/i/{$ns}/{$db}/{$tb}/names/col2 "firstname"
/{$kv}/!/i/{$ns}/{$db}/{$tb}/names/col3 "emails.0.value"
2016-02-26 22:28:02 +00:00
```
2016-03-10 02:04:06 +00:00
---
### Items
2016-02-26 22:28:02 +00:00
```bash
2016-03-10 02:04:06 +00:00
/{$kv}/{$ns}/{$db}/{$tb}/{$id} ""
2016-02-26 22:28:02 +00:00
# e.g
2016-03-10 02:04:06 +00:00
/{$kv}/{$ns}/{$db}/{$tb}/UUID `{"name":"Tobie","age":18}`
2016-02-26 22:28:02 +00:00
```
2016-03-10 02:04:06 +00:00
*TRAIL*
2016-02-26 22:28:02 +00:00
```bash
2016-03-10 02:04:06 +00:00
/{$kv}/{$ns}/{$db}/{$tb}/•/{$id}/{$time} ""
2016-02-26 22:28:02 +00:00
# e.g
2016-03-10 02:04:06 +00:00
/{$kv}/{$ns}/{$db}/{$tb}/•/UUID/2016-01-29T22:42:56.478173947Z ""
2016-02-26 22:28:02 +00:00
```
2016-03-10 02:04:06 +00:00
*EVENT*
2016-02-26 22:28:02 +00:00
```bash
2016-03-10 02:04:06 +00:00
/{$kv}/{$ns}/{$db}/{$tb}/‡/{$id}/{$type}/{$time} ""
# e.g
/{$kv}/{$ns}/{$db}/{$tb}/‡/UUID/login/2016-01-29T22:42:56.478173947Z ""
2016-02-26 22:28:02 +00:00
```
2016-03-10 02:04:06 +00:00
*EDGES*
2016-02-26 22:28:02 +00:00
```bash
2016-03-10 02:04:06 +00:00
/{$kv}/{$ns}/{$db}/{$tableid}/»/{$id}/{$type}/{$edgeid} ""
/{$kv}/{$ns}/{$db}/{$typeid}/{$id} ""
/{$kv}/{$ns}/{$db}/{$tableid}/«/{$id}/{$type}/{$edgeid} ""
2016-02-26 22:28:02 +00:00
# e.g
2016-03-10 02:04:06 +00:00
/{$kv}/{$ns}/{$db}/{$tableid}/»/1537/follow/9563 ""
/{$kv}/{$ns}/{$db}/{$typeid}/9563 `{"in":"1537","out":"5295"}`
/{$kv}/{$ns}/{$db}/{$tableid}/«/5295/follow/9563 ""
2016-02-26 22:28:02 +00:00
```
### Index
2016-03-10 02:04:06 +00:00
**Global index**
2016-02-26 22:28:02 +00:00
```bash
2016-03-10 02:04:06 +00:00
/{$kv}/{$ns}/{$db}/¤/{$index}/[{$columns}] "{$id}"
2016-02-26 22:28:02 +00:00
# e.g
2016-03-10 02:04:06 +00:00
/{$kv}/{$ns}/{$db}/¤/{$index}/[lastname,firstname] "@person:1342"
2016-02-26 22:28:02 +00:00
```
**Unique index**
2016-02-26 22:28:02 +00:00
```bash
2016-03-10 02:04:06 +00:00
/{$kv}/{$ns}/{$db}/{$table}/¤/{$index}/[{$columns}]/{$id} ""
# e.g
2016-03-10 02:04:06 +00:00
/{$kv}/{$ns}/{$db}/{$table}/¤/{$index}/[lastname,firstname]/{$id} ""
```
**Non-unique index**
```bash
2016-03-10 02:04:06 +00:00
/{$kv}/{$ns}/{$db}/{$table}/¤/{$index}/[{$columns}] "{$id}"
2016-02-26 22:28:02 +00:00
# e.g
2016-03-10 02:04:06 +00:00
/{$kv}/{$ns}/{$db}/{$table}/¤/{$index}/[lastname,firstname] "@person:1342"
2016-02-26 22:28:02 +00:00
```