Bardo.DB (Bardo v0.1.0)

View Source

A simple, efficient database for the Bardo neuroevolution system.

Uses ETS (Erlang Term Storage) for in-memory storage with periodic backups. This implementation is designed for efficiency and simplicity, making it ideal for use as a library dependency in other projects.

Summary

Functions

Back up the database to disk.

Returns a specification to start this module under a supervisor.

Delete a value from the database.

Check if a key exists in the database.

Fetch a value from the database.

List all values for a specific table type.

Read a value from the database using the models format.

Restore the database from a backup file.

Start the database server.

Store a value in the database.

Write a value to the database using the models format.

Functions

backup(backup_path \\ "backups")

@spec backup(String.t()) :: {:ok, String.t()} | {:error, term()}

Back up the database to disk.

Parameters

  • backup_path - Directory to store the backup (default: "backups")

Returns

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

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

delete(table, key)

@spec delete(atom(), term()) :: :ok

Delete a value from the database.

Parameters

  • table - The table to delete from
  • key - The key to delete

exists?(table, key)

@spec exists?(atom(), term()) :: boolean()

Check if a key exists in the database.

Parameters

  • table - The table to check
  • key - The key to check

Returns

  • true if the key exists
  • false if the key doesn't exist

fetch(table, key)

@spec fetch(atom(), term()) :: {:ok, term()} | {:error, :not_found}

Fetch a value from the database.

Parameters

  • table - The table to fetch from
  • key - The key to fetch

Returns

  • {:ok, value} on success
  • {:error, :not_found} if the key doesn't exist

list(table)

@spec list(atom()) :: {:ok, [term()]} | {:ok, []}

List all values for a specific table type.

Parameters

  • table - The table to list values from

Returns

  • {:ok, [values]} on success
  • {:ok, []} if the table is empty

read(id, table)

@spec read(term(), atom()) :: term() | :not_found

Read a value from the database using the models format.

This is a compatibility function for code that uses the Models module.

Parameters

  • id - The ID to read
  • table - The table to read from

Returns

  • The value if found
  • :not_found if the key doesn't exist

restore(backup_file)

@spec restore(String.t()) :: :ok | {:error, term()}

Restore the database from a backup file.

Parameters

  • backup_file - Path to the backup file

Returns

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

start_link(opts \\ [])

Start the database server.

Options

  • :auto_backup - Whether to run automatic backups (default: true)
  • :backup_dir - Directory to store backups (default: "backups")

store(table, key, value)

@spec store(atom(), term(), term()) :: :ok

Store a value in the database.

Parameters

  • table - The table to store the value in (e.g., :experiment, :population)
  • key - The key to store the value under
  • value - The value to store

write(value, table)

@spec write(term(), atom()) :: :ok

Write a value to the database using the models format.

This is a compatibility function for code that uses the Models module.

Parameters

  • value - The model value to write
  • table - The table to write to