ream/storage/stream/event

Store the information of the event that is being streamed. It is used to write and read events from the file.

The events stored in the file are in the following format:

Types

pub type Event {
  Event(offset: Int, data: BitString)
}

Constructors

  • Event(offset: Int, data: BitString)

The information for the stream 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 events.
  • size: the size of the file.
  • file_path: the path of the file.
pub type EventFile {
  EventFile(id: Int, handler: Pid, size: Int, file_path: String)
}

Constructors

  • EventFile(id: Int, handler: Pid, size: Int, file_path: String)

Functions

pub fn close(stream_file: EventFile) -> Result(Nil, Reason)

Close a stream file. It closes the file handler.

pub fn create(base_path: String) -> Result(EventFile, Reason)

Create a new stream 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 open(path: String, file_id: Int) -> Result(
  EventFile,
  Reason,
)

Open a stream 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 stream file with the file handler and the file size.

pub fn read(stream_file: EventFile, offset: Int) -> Result(
  Event,
  Reason,
)

Read an event from the stream file. It reads the event and returns it as a BitString with the following format:

  • 4 bytes: the size of the event
  • n bytes: the event
pub fn write(stream_file: EventFile, event: Event) -> EventFile

Write an event to the stream file. It writes the event in the following format:

  • 4 bytes: the size of the event
  • n bytes: the event It returns the updated stream file with the new size.
Search Document