surrealpatch/docs/STORAGE.md
2016-03-10 02:04:06 +00:00

3.8 KiB
Raw Blame History

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

{$kv} = "surreal" # This is the base key

The base key is used to separate the data used in SurrealDB from data used by other databases using the same key:value store.

{$ns} = "acme" # This is the name of the namespace

The namespace key is used to enable separation of data and multi-tenancy of databases on SurrealDB.

{$db} = "test" # This is the name of the database

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.

! # Used to store Surreal config data
¤ # Used to store view and index data
 # Used to store in links
 # Used to store out links
« # Used to store in edges
» # Used to store out edges# Used to store item diffs# Used to store item events##
Ω # Used in key queries as scan end key
~
≈
≠

Config

Namespace

/{$kv}/!/n/{$ns} "{$ns:id}"
# e.g.
/{$kv}/!/n/acme "6qh3iwp5"

Database

/{$kv}/!/d/{$ns}/{$db} "{$db:id}"
# e.g.
/{$kv}/!/d/{$ns}/test "3gt4yqk3"

Table

/{$kv}/!/t/{$ns}/{$db}/{$tb} "{$tb:id}"
# e.g.
/{$kv}/!/t/{$ns}/{$db}/people "1bd7ajq8"

Field

/{$kv}/!/f/{$ns}/{$db}/{$tb}/{$fld} "{$code}"
# e.g.
/{$kv}/!/f/{$ns}/{$db}/{$tb}/fullname "return doc.fname + doc.lname"

Index

/{$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()"
/{$kv}/!/i/{$ns}/{$db}/{$tb}/{$idx} "{$idx:id}"
/{$kv}/!/i/{$ns}/{$db}/{$tb}/{$idx}/col:{$cd} "{$column}"
# e.g
/{$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"

Items

/{$kv}/{$ns}/{$db}/{$tb}/{$id} ""
# e.g
/{$kv}/{$ns}/{$db}/{$tb}/UUID `{"name":"Tobie","age":18}`

TRAIL

/{$kv}/{$ns}/{$db}/{$tb}/•/{$id}/{$time} ""
# e.g
/{$kv}/{$ns}/{$db}/{$tb}/•/UUID/2016-01-29T22:42:56.478173947Z ""

EVENT

/{$kv}/{$ns}/{$db}/{$tb}/‡/{$id}/{$type}/{$time} ""
# e.g
/{$kv}/{$ns}/{$db}/{$tb}/‡/UUID/login/2016-01-29T22:42:56.478173947Z ""

EDGES

/{$kv}/{$ns}/{$db}/{$tableid}/»/{$id}/{$type}/{$edgeid} ""
/{$kv}/{$ns}/{$db}/{$typeid}/{$id} ""
/{$kv}/{$ns}/{$db}/{$tableid}/«/{$id}/{$type}/{$edgeid} ""
# e.g
/{$kv}/{$ns}/{$db}/{$tableid}/»/1537/follow/9563 ""
/{$kv}/{$ns}/{$db}/{$typeid}/9563 `{"in":"1537","out":"5295"}`
/{$kv}/{$ns}/{$db}/{$tableid}/«/5295/follow/9563 ""

Index

Global index

/{$kv}/{$ns}/{$db}/¤/{$index}/[{$columns}] "{$id}"
# e.g
/{$kv}/{$ns}/{$db}/¤/{$index}/[lastname,firstname] "@person:1342"

Unique index

/{$kv}/{$ns}/{$db}/{$table}/¤/{$index}/[{$columns}]/{$id} ""
# e.g
/{$kv}/{$ns}/{$db}/{$table}/¤/{$index}/[lastname,firstname]/{$id} ""

Non-unique index

/{$kv}/{$ns}/{$db}/{$table}/¤/{$index}/[{$columns}] "{$id}"
# e.g
/{$kv}/{$ns}/{$db}/{$table}/¤/{$index}/[lastname,firstname] "@person:1342"