Skip to content

API Reference parrot.knowledge.pageindex.content_store

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.knowledge.pageindex.content_store

View source

Per-node markdown content store for PageIndex trees.

Each PageIndex node references markdown content stored alongside the tree JSON in a sibling directory::

<storage_dir>/
    <tree_name>.json          ← INDEX (lean ToC tree)
    <tree_name>/              ← CONTENT (one .md per node)
        0000.md
        0001.md
        …

The store fronts disk reads with a bounded LRU cache keyed by (tree_name, node_id) so repeated retrieval of the same node is served from memory. Cache entries are evicted on save/delete so writers and readers never observe stale content.

This module deliberately uses only the standard library — file I/O is fast enough for the access patterns PageIndex exercises, and wrapping small reads in async would add noise without benefit.

Class NodeContentStore

On-disk per-node markdown content store with a bounded LRU cache.

Parameter Type Default Description
storage_dir str | Path Directory that contains tree JSON files and the sibling <tree_name>/ content directories. Created if it does not exist.
cache_size int 256 Maximum number of (tree_name, node_id) entries held in memory.

Method NodeContentStore.save

Persist markdown for node_id under tree_name.

save(tree_name: str, node_id: str, markdown: str) -> None

Parameter Type Default Description
tree_name str
node_id str
markdown str

Method NodeContentStore.bodies_dir

Return <storage_dir>/<tree_name>/bodies.

bodies_dir(tree_name: str) -> Path

Parameter Type Default Description
tree_name str

Returns: Path

Method NodeContentStore.body_path

Return the path of blob key; raise ValueError for a malformed key.

body_path(tree_name: str, key: str) -> Path

Parameter Type Default Description
tree_name str
key str

Returns: Path

Method NodeContentStore.save_body

Write blob key atomically (temp + fsync + os.replace); no-op when it exists.

save_body(tree_name: str, key: str, markdown: str) -> Path

Parameter Type Default Description
tree_name str
key str
markdown str

Returns: Path

Method NodeContentStore.load_body

Return blob key (cached) or None when absent.

load_body(tree_name: str, key: str) -> Optional[str]

Parameter Type Default Description
tree_name str
key str

Returns: Optional[str]

Method NodeContentStore.load_node

Resolve body_ref → <flat_concept_id>.md → <node_id>.md; first hit wins.

load_node(tree_name: str, node: dict[str, Any]) -> Optional[str]

Parameter Type Default Description
tree_name str
node dict[str, Any]

Returns: Optional[str]

Method NodeContentStore.load_current

Return node_id's body from the live generation, falling back to legacy files.

Unlike :meth:load, which prefers a legacy <node_id>.md for backwards compatibility, this prefers the body the published tree points at. Use it where a stale leftover file must never be served for a node whose id was reused by a newer tree (reindex, rename over a legacy destination).

load_current(tree_name: str, node_id: str) -> Optional[str]

Parameter Type Default Description
tree_name str
node_id str

Returns: Optional[str]

Method NodeContentStore.mark_superseded

Restart the grace clock of bodies a new generation just stopped referencing.

The sweep ages files by mtime, i.e. from creation. A body written long ago and superseded only now must still stay readable for the full grace period, so its mtime is reset to now at supersession time.

mark_superseded(tree_name: str, body_keys: Iterable[str], node_ids: Iterable[str]) -> int

Parameter Type Default Description
tree_name str Tree whose files are touched.
body_keys Iterable[str] body_ref blobs the previous generation referenced.
node_ids Iterable[str] Legacy <node_id>.md files the previous generation used.

Returns: int — Number of files whose clock was restarted.

Method NodeContentStore.sweep_unreferenced

Delete unreferenced blobs and superseded <node_id>.md files older than max_age_s.

sweep_unreferenced(tree_name: str, tree: dict[str, Any], *, max_age_s: int) -> int

Parameter Type Default Description
tree_name str
tree dict[str, Any]
max_age_s int

Returns: int

Method NodeContentStore.load

Return the markdown for node_id or None if missing.

load(tree_name: str, node_id: str) -> Optional[str]

Parameter Type Default Description
tree_name str
node_id str

Returns: Optional[str]

Method NodeContentStore.has

Return whether the sidecar markdown file for node_id exists.

has(tree_name: str, node_id: str) -> bool

Parameter Type Default Description
tree_name str
node_id str

Returns: bool

Method NodeContentStore.delete_node

Remove the sidecar for node_id. Returns True if removed.

delete_node(tree_name: str, node_id: str) -> bool

Parameter Type Default Description
tree_name str
node_id str

Returns: bool

Method NodeContentStore.delete_tree

Remove every sidecar for tree_name; return file count removed.

Also evicts every cache entry belonging to that tree.

delete_tree(tree_name: str) -> int

Parameter Type Default Description
tree_name str

Returns: int

Method NodeContentStore.rename_tree

Move all content and evict source/destination cache entries.

rename_tree(src: str, dst: str) -> bool

Parameter Type Default Description
src str
dst str

Returns: bool — False when the source directory is absent; True after moving it.

Raises:

  • ValueError — A name is invalid.
  • FileExistsError — Destination directory already exists.

Method NodeContentStore.list_node_ids

Return node ids that currently have a sidecar on disk, sorted.

list_node_ids(tree_name: str) -> list[str]

Parameter Type Default Description
tree_name str

Returns: list[str]

Method NodeContentStore.loader_for

Return a closure node_id -> Optional[str] for tree_name.

Hand this to :class:HybridPageIndexSearch (via the new content_loader constructor parameter) so the engine can read per-node markdown without taking a hard dependency on this store.

The returned closure accepts both plain node_id values ("0042") and flattened concept_id values ("controls--nist-ir-4"). When a caller passes a flattened concept_id, the closure attempts to load that key directly; it falls back to treating the argument as a plain node_id only when the first attempt returns None.

This dual-key behaviour supports the OKF migration transition period where some sidecars have already been renamed to <flattened_concept_id>.md while others remain keyed by node_id.

With tree, ids resolve against that tree (pinned) via :meth:load_node, regardless of later changes to the JSON on disk.

loader_for(tree_name: str, tree: Optional[dict[str, Any]] = None) -> Callable[[str], Optional[str]]

Parameter Type Default Description
tree_name str
tree Optional[dict[str, Any]] None

Returns: Callable[[str], Optional[str]]

Alias flatten_concept_id_for_filename

Re-exported from parrot.knowledge.okf.utils.flatten_concept_id_for_filename.

Attribute logger

Alias structure_to_list

Re-exported from parrot.knowledge.pageindex.utils.structure_to_list.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally