Ferricstore.Store.ActiveFile (ferricstore v0.10.0)

Copy Markdown View Source

Tracks the active log file for each shard.

Uses a per-row atomics generation counter plus a process dictionary cache. Reads stay one Process.get/1 plus one :atomics.get/2 on the hot path, while a rotation invalidates only callers that cached that exact shard.

Usage

# In Shard init and rotation:
ActiveFile.publish(instance_ctx, shard_index, file_id, file_path, shard_data_path)

# In Router's local-origin write path (hot path):
{file_id, file_path, shard_data_path} = ActiveFile.get(instance_ctx, shard_index)

Summary

Functions

Removes active-file rows for a custom instance.

Returns {file_id, file_path, shard_data_path} for the given shard.

Returns active file metadata for a shard in a specific instance.

Ensures the supervised active-file registry is available.

Publishes the active file metadata for a shard.

Publishes active file metadata for a shard in a specific instance.

Functions

cleanup_instance(map)

@spec cleanup_instance(map()) :: :ok

Removes active-file rows for a custom instance.

Called during instance cleanup. The generation bump invalidates process dictionary caches that may still hold paths for the stopped instance.

get(shard_index)

@spec get(non_neg_integer()) :: {non_neg_integer(), binary(), binary()}

Returns {file_id, file_path, shard_data_path} for the given shard.

~15ns hot path (atomics check + process dictionary cache hit). ~100ns cold (ETS lookup on generation mismatch).

get(ctx, shard_index)

@spec get(map() | nil, non_neg_integer()) :: {non_neg_integer(), binary(), binary()}

Returns active file metadata for a shard in a specific instance.

Prefer this in production paths that already have an Instance context.

init(shard_count)

@spec init(non_neg_integer()) :: :ok | {:error, :table_owner_unavailable}

Ensures the supervised active-file registry is available.

publish(shard_index, file_id, file_path, shard_data_path)

@spec publish(non_neg_integer(), non_neg_integer(), binary(), binary()) :: :ok

Publishes the active file metadata for a shard.

Called from Shard.init/1 and Shard.maybe_rotate_file/1.

publish(ctx, shard_index, file_id, file_path, shard_data_path)

@spec publish(map() | nil, non_neg_integer(), non_neg_integer(), binary(), binary()) ::
  :ok

Publishes active file metadata for a shard in a specific instance.

Every instance uses {instance_name, shard_index} so registry identity is uniform across default and embedded stores.