Reqord.Storage.Behavior behaviour (reqord v0.4.0)

View Source

Behavior for storage backends.

This module defines the interface that all storage backends must implement, allowing for pluggable storage solutions (FileSystem, S3, Redis, etc.).

Summary

Callbacks

Delete an entire cassette file.

Delete an external object by its hash.

Ensure the path directory structure exists.

Check if a cassette exists at the specified path.

List all stored objects (for maintenance tasks).

Load a binary object by its hash reference.

Load streaming data chunks by hash reference.

Stream all entries from the specified cassette path.

Store a binary object externally and return its reference.

Store streaming data chunks externally.

Write a single entry to the specified cassette path.

Callbacks

delete_cassette(path)

@callback delete_cassette(path :: String.t()) :: :ok | {:error, term()}

Delete an entire cassette file.

Parameters

  • path - The cassette path/identifier

Returns

  • :ok on success (also returns :ok if file doesn't exist)
  • {:error, reason} on failure

delete_object(hash)

@callback delete_object(hash :: String.t()) :: :ok | {:error, term()}

Delete an external object by its hash.

Parameters

  • hash - Object hash identifier

Returns

  • :ok on success (also returns :ok if object doesn't exist)
  • {:error, reason} on failure

ensure_path_exists(path)

@callback ensure_path_exists(path :: String.t()) :: :ok | {:error, term()}

Ensure the path directory structure exists.

Parameters

  • path - The cassette path whose directory structure should exist

Returns

  • :ok on success
  • {:error, reason} on failure

exists?(path)

@callback exists?(path :: String.t()) :: boolean()

Check if a cassette exists at the specified path.

Parameters

  • path - The cassette path/identifier

Returns

  • true if the cassette exists
  • false otherwise

list_objects()

@callback list_objects() :: {:ok, [String.t()]} | {:error, term()}

List all stored objects (for maintenance tasks).

Returns

  • {:ok, hashes} with list of object hash identifiers
  • {:error, reason} on failure

load_object(hash)

@callback load_object(hash :: String.t()) :: {:ok, binary()} | {:error, term()}

Load a binary object by its hash reference.

Parameters

  • hash - Object hash identifier

Returns

  • {:ok, content} on success with the binary content
  • {:error, reason} on failure (e.g., object not found)

load_stream(hash)

@callback load_stream(hash :: String.t()) :: {:ok, list()} | {:error, term()}

Load streaming data chunks by hash reference.

Parameters

  • hash - Stream hash identifier

Returns

  • {:ok, chunks} on success with list of {timestamp, data} tuples
  • {:error, reason} on failure

read_entries(path)

@callback read_entries(path :: String.t()) :: Enumerable.t()

Stream all entries from the specified cassette path.

Parameters

  • path - The cassette path/identifier

Returns

  • An Enumerable that yields entry maps

store_object(hash, content)

@callback store_object(hash :: String.t(), content :: binary()) ::
  {:ok, String.t()} | {:error, term()}

Store a binary object externally and return its reference.

Parameters

  • hash - Content hash to use as the object identifier
  • content - Binary content to store

Returns

  • {:ok, hash} on success with the object hash
  • {:error, reason} on failure

store_stream(hash, chunks)

@callback store_stream(hash :: String.t(), chunks :: list()) ::
  {:ok, String.t()} | {:error, term()}

Store streaming data chunks externally.

Parameters

  • hash - Content hash to use as the stream identifier
  • chunks - List of {timestamp, data} tuples representing stream chunks

Returns

  • {:ok, hash} on success with the stream hash
  • {:error, reason} on failure

write_entry(path, entry)

@callback write_entry(path :: String.t(), entry :: map()) :: :ok | {:error, term()}

Write a single entry to the specified cassette path.

Parameters

  • path - The cassette path/identifier
  • entry - The entry data as a map

Returns

  • :ok on success
  • {:error, reason} on failure