# `PhoenixAI.Store.Adapters.ETS`
[🔗](https://github.com/franciscpd/phoenix-ai-store/blob/v0.1.0/lib/phoenix_ai/store/adapters/ets.ex#L1)

In-memory ETS adapter for `PhoenixAI.Store.Adapter`.

All functions receive an `opts` keyword list containing a `:table` key
with the ETS table reference (typically owned by a `TableOwner` GenServer).

## Storage Layout

- Conversations: `{{:conversation, id}, %Conversation{}}`
- Messages: `{{:message, conversation_id, message_id}, %Message{}}`
- Facts: `{{:fact, user_id, key}, %Fact{}}`
- Profiles: `{{:profile, user_id}, %Profile{}}`

# `add_message`

Appends a message to a conversation in ETS. Returns `{:error, :not_found}` if the conversation does not exist.

# `conversation_exists?`

Checks whether a conversation with the given ID exists in the ETS table.

# `count_conversations`

Counts conversations in ETS matching the given filters.

Note: O(n) — materializes the full filtered list then counts.
Use the Ecto adapter for production workloads requiring efficient counts.

# `count_events`

Counts events in ETS matching the given filters.

# `count_facts`

Counts all facts for a user in ETS via a full match scan.

# `delete_conversation`

Deletes a conversation and all its messages and cost records from ETS.

# `delete_fact`

Deletes a fact by `{user_id, key}` from ETS. Always returns `:ok`.

# `delete_profile`

Deletes a user profile from ETS. Always returns `:ok`.

# `get_cost_records`

Returns all cost records for a conversation from ETS, sorted by `recorded_at` ascending.

# `get_facts`

Returns all facts for a user from ETS, sorted by `inserted_at` ascending.

# `get_messages`

Returns all messages for a conversation from ETS, sorted by `inserted_at` ascending.

# `list_conversations`

Returns all conversations from ETS matching the given filters, sorted by `inserted_at` descending.

# `list_events`

Returns a paginated, filtered list of events from ETS with an opaque cursor for the next page.

# `load_conversation`

Loads a conversation by ID from ETS and eagerly populates its messages.

# `load_profile`

Loads a user profile from ETS. Returns `{:error, :not_found}` if absent.

# `log_event`

Appends an event to the ETS table keyed on `{inserted_at, id}` for stable ordering.

# `save_conversation`

Inserts or updates a conversation in the ETS table, preserving `inserted_at` on upsert.

# `save_cost_record`

Inserts a cost record into ETS keyed on `{conversation_id, record_id}`.

# `save_fact`

Upserts a fact in ETS keyed on `{user_id, key}`, preserving `inserted_at` on update.

# `save_profile`

Upserts a user profile in ETS keyed on `user_id`, preserving `inserted_at` on update.

# `sum_conversation_tokens`

Sums `token_count` across all messages for a conversation stored in ETS.

# `sum_cost`

Sums `total_cost` across all cost records matching the given filters in ETS.

# `sum_user_tokens`

Sums `token_count` across all messages in all conversations belonging to a user in ETS.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
