Reminisce.Keys (reminisce v0.6.0)

Copy Markdown View Source

Keys are per owner.

The key a caller chooses (or the library generates) is the memory's key within its owner. In the store it is kept as tag:key, where tag is 16 hex digits of a SHA-256 over the owner and the key, so two owners can use the same key without meeting: a key another owner holds answers exactly as a fresh one does, and nothing tells one owner which keys another holds.

The tag comes first because TypeDB 3.12 finds a string attribute by its first bytes: when many keys share their first eight bytes, a lookup of any one of them reads them all, and an owner whose keys all began with the same prefix paid for every memory it held on every lookup. Led by the tag, no two keys share those bytes except by chance, whatever keys the caller chooses.

Every public function takes and returns the caller's key; the stored form never leaves the library, except in a backup, which is a copy of the store. Anything ordered by key is ordered by the caller's key.

The owner's kind and key and the memory's key are hashed with a NUL byte between them, so neither part of the owner may contain one: {"a", "b\0c"} and {"a\0b", "c"} would hash alike. The library's functions that take an owner refuse such an owner as {:error, {:invalid, :owner}}; stored/2 and local/2, which have no error to answer, raise ArgumentError for it, as Reminisce.MCP.Server.new/2 does. migrate/1 leaves a memory with such an owner as it is and names it in its answer.

A store written by a development build before 0.2.0 may hold keys written by those builds: under an owner prefix (o + 16 hex digits + :), or with none. migrate/1 brings them to this form.

Summary

Functions

The caller's form of a stored key of owner's.

Rewrites every memory key in the store to the form stored/2 gives.

The stored form of owner's key.

Functions

local(owner, stored)

@spec local(Reminisce.owner(), String.t()) :: String.t()

The caller's form of a stored key of owner's.

migrate(store)

@spec migrate(Reminisce.Store.t()) ::
  {:ok,
   %{rekeyed: non_neg_integer(), skipped: [{Reminisce.owner(), String.t()}]}}
  | {:error, term()}

Rewrites every memory key in the store to the form stored/2 gives.

Keys written by development builds before 0.2.0: a key under an owner prefix (o + 16 hex digits + :) loses the prefix, and a key with no prefix is taken as the owner's own. A key already in the current form is left alone, so running it again changes nothing. Memories are rewritten by their identity in TypeDB, so their links, subjects and history stay as they were.

A memory whose owner holds a NUL byte has no key in the current form (see above): it is left as it is, and named in skipped as {owner, key}, the key as stored, in key order.

Stop writing to the store while it runs. Answers {:ok, %{rekeyed: n, skipped: [{owner, key}]}}.

stored(owner, key)

@spec stored(Reminisce.owner(), String.t()) :: String.t()

The stored form of owner's key.