Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:0xranx OpenContext IndexSyncService

From Leeroopedia
Knowledge Sources
Domains Indexing, Background_Processing
Last Updated 2026-10-24 12:00 GMT

Overview

Concrete tool for batched background synchronization of the search index with document changes, implemented in Rust and exposed to Node.js via NAPI bindings.

Description

The IndexSyncService is a Rust service that subscribes to the `SharedEventBus` for document and folder events, collects them into a pending actions map, and processes them in batches at regular intervals (default: 5 minutes).

The Node.js adapter (`src/core/search/indexSync.js`) wraps the native `startIndexSync` function, providing a JavaScript-compatible API with `start()`, `isRunning()`, and `getStatus()` methods.

Key behavior:

  • Event collection: The service subscribes to `DocEvent` and `FolderEvent` broadcasts. Each event is mapped to an `IndexAction` (Update, Remove, or Rename) and stored in a `HashMap<String, IndexAction>` keyed by file path.
  • Coalescing: Multiple changes to the same file within an interval replace the pending action — only the final state needs indexing.
  • Batch processing: At each interval tick, all pending actions are processed: Updates call `indexer.index_file()`, Removes call `indexer.remove_file()`, Renames do both (remove old + index new).
  • Enable/disable: An `AtomicBool` flag controls whether the service processes events.
  • Pending count: The number of pending actions is queryable via `pending_count()`.

Usage

  • Search Index Management: Automatically keeps the index up-to-date after initial build.
  • Desktop app: Started in the Tauri desktop app to maintain index freshness without manual rebuilds.
  • Configuration: Interval is configurable via `with_interval()` (Rust) or `start({ intervalSecs })` (JS).

Code Reference

Source Location

  • Repository: OpenContext
  • File (Rust): crates/opencontext-core/src/search/index_sync.rs
  • File (JS adapter): src/core/search/indexSync.js
  • File (events): crates/opencontext-core/src/events.rs

Signature (Rust)

pub struct IndexSyncService {
    config: SearchConfig,
    contexts_root: PathBuf,
    indexer: Arc<Mutex<Option<Indexer>>>,
    enabled: Arc<AtomicBool>,
    pending_actions: Arc<Mutex<HashMap<String, IndexAction>>>,
    check_interval_secs: u64,
}

impl IndexSyncService {
    pub fn new(config: SearchConfig, contexts_root: PathBuf) -> Self;
    pub fn with_interval(mut self, secs: u64) -> Self;
    pub fn set_enabled(&self, enabled: bool);
    pub fn is_enabled(&self) -> bool;
    pub async fn pending_count(&self) -> usize;
    pub async fn start(&self, event_bus: SharedEventBus) -> SearchResult<()>;
}

Signature (JS)

/**
 * Start the index sync service
 * @param {Object} options
 * @param {number} options.intervalSecs - Interval in seconds (default 300)
 * @returns {Promise<boolean>} true if started, false if already running
 */
async function start(options = {}) { ... }

/** Check if sync is running */
function isRunning() { ... }

/** Get sync status */
function getStatus() { ... }

Import

const { indexSync, start, isRunning, getStatus } = require('./core/search/indexSync');

I/O Contract

Inputs (start)

Name Type Required Description
options.intervalSecs number No Batch interval in seconds (default 300 = 5 min)

Outputs (start)

Name Type Description
returns Promise<boolean> true if started, false if already running

Outputs (getStatus)

Name Type Description
returns.running boolean Whether the service is running
returns.error undefined Error message if native unavailable

Usage Examples

Start Background Sync (JavaScript)

const { indexSync } = require('./core/search/indexSync');

// Start with default 5-minute interval
const started = await indexSync.start();
console.log(`Sync started: ${started}`);

// Check status
const status = indexSync.getStatus();
console.log(`Running: ${status.running}`);

Start with Custom Interval

// Check every 60 seconds instead of 5 minutes
await indexSync.start({ intervalSecs: 60 });

Check Running State

if (indexSync.isRunning()) {
  console.log('Background index sync is active');
}

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment