ream/storage/kv/value
Store the information of the values for kv.
The events stored in the file are in the following format:
- 4 bytes: the size of the event
- 1 byte: 0 if the event is not deleted, 1 if it is
- n bytes: the event
The KV system is split into two parts: the key store and the value store. The file is related to the value store. As we said it’s a file with the content marking the size of the value, if it’s deleted and the value itself.
The file is append only. When a value is written, it’s appended to the end of the file. When a value is deleted, it’s not deleted from the file, it’s marked as deleted. When a value is updated, it’s deleted and a new value is appended to the end of the file.
Types
The information for a value. The fields are:
offset
: the offset of the value in the file.deleted
: if the value is deleted.data
: the value. It can beNone
if we didn’t read the value yet.file_id
: the id of the file where the value is stored.
pub type Value {
Value(
offset: Int,
deleted: Bool,
data: Option(BitString),
file_id: Int,
)
}
Constructors
-
Value( offset: Int, deleted: Bool, data: Option(BitString), file_id: Int, )
The information for the kv file. The fields are:
id
: the id of the file. It’s intended to be a UUID but it’s stored as an Int.handler
: the file handler to read and write values.size
: the size of the file.max_size
: the maximum size of the file.file_path
: the path of the file.
pub type ValueFile {
ValueFile(
id: Int,
handler: Pid,
size: Int,
max_size: Int,
file_path: String,
)
}
Constructors
-
ValueFile( id: Int, handler: Pid, size: Int, max_size: Int, file_path: String, )
The information for the kv file. The fields are:
file_id
: the id of the file. It’s intended to be a UUID but it’s stored as an Int.size
: the size of the file.entries
: the number of entries in the file.deleted
: the number of deleted entries in the file.
pub type ValueFileInfo {
ValueFileInfo(
file_id: Int,
size: Int,
max_size: Int,
entries: Int,
deleted: Int,
)
}
Constructors
-
ValueFileInfo( file_id: Int, size: Int, max_size: Int, entries: Int, deleted: Int, )
Constants
pub const value_size_bits: Int = 32
Functions
pub fn close(vfile: ValueFile) -> Result(Nil, Reason)
Close a kv file. It closes the file handler.
pub fn create(base_path: String, max_size: Int) -> Result(
ValueFile,
Reason,
)
Create a new kv file. It creates a new file with a random UUID as the path for finding the file.
For example, if the UUID is f81d4fae-7dec-11d0-a765-00a0c91e6bf6
, the
file will be created in the following path:
base_path/f81d4fae/7dec/11d0/a765/00a0c91e6bf6
.
pub fn delete(vfile: ValueFile, value: Value) -> Result(
ValueFile,
Reason,
)
Delete a value from the kv file. It marks the value as deleted. It returns the updated kv file with the new size.
pub fn get_file_info(vfile: ValueFile) -> ValueFileInfo
pub fn open(path: String, file_id: Int, max_size: Int) -> Result(
ValueFile,
Reason,
)
Open a kv file. It opens the file with the given file id.
If the file doesn’t exist, it is creating it. The main difference
with create
is that open
doesn’t generate a new UUID.
It returns the kv file with the file handler and the file size.
pub fn read(vfile: ValueFile, offset: Int) -> Result(
Value,
Reason,
)
Read a value from the kv file. It reads the value and returns it as a BitString with the following format:
- 4 bytes: the size of the value
- 1 byte: 0 if the value is not deleted, 1 if it is
- n bytes: the value
pub fn write(vfile: ValueFile, value: BitString) -> Result(
#(ValueFile, Value),
Reason,
)
pub fn write_value(vfile: ValueFile, value: Value) -> Result(
ValueFile,
Reason,
)
Write a value to the kv file. It writes the value in the following format:
- 4 bytes: the size of the value
- 1 byte: 0 if the value is not deleted, 1 if it is
- n bytes: the value It returns the updated kv file with the new size.