Class: RailsAiBridge::RubydexAdapter::IncrementalIndexer

Inherits:
Service
  • Object
show all
Defined in:
lib/rails_ai_bridge/rubydex_adapter/incremental_indexer.rb

Overview

Incremental indexer service for rubydex graphs.

On +:build+ performs a full index and records file mtimes. On +:reindex+ detects changed files and either patches the graph incrementally (when changes are below the threshold) or falls back to a full rebuild.

Optionally persists file mtimes to disk so change detection works across process restarts.

Constant Summary collapse

MTIMES_FILENAME =

Filename used to persist the integer-second mtime snapshot on disk.

Returns:

  • (String)
'mtimes.json'

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Service

#initialize

Constructor Details

This class inherits a constructor from RailsAiBridge::Service

Class Method Details

.call(operation) ⇒ Service::Result

Entry point that creates an instance and dispatches the operation.

Parameters:

  • operation (Symbol)

    +:build+ or +:reindex+

  • options (Hash)

    forwarded to #call

Returns:



28
29
30
# File 'lib/rails_ai_bridge/rubydex_adapter/incremental_indexer.rb', line 28

def self.call(operation, **)
  new.call(operation, **)
end

Instance Method Details

#call(operation, root:, graph: nil, file_mtimes: {}, **options) ⇒ Service::Result

Dispatches the indexing operation.

Parameters:

  • operation (Symbol)

    +:build+ or +:reindex+

  • root (String)

    project root directory

  • graph (Rubydex::Graph, nil) (defaults to: nil)

    existing graph (for +:reindex+)

  • file_mtimes (Hash<String, Rational>) (defaults to: {})

    mtimes from last index

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :threshold (Float)

    fallback rebuild threshold (0.0–1.0), default +0.3+

  • :persist (Boolean)

    whether to persist mtimes to disk, default +false+

  • :index_path (String, nil)

    directory for the mtime JSON file, default +nil+

Returns:

Raises:

  • (StandardError)

    rescued and returned as failure result



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rails_ai_bridge/rubydex_adapter/incremental_indexer.rb', line 43

def call(operation, root:, graph: nil, file_mtimes: {}, **options)
  threshold = options.fetch(:threshold, 0.3)
  persist = options.fetch(:persist, false)
  index_path = options.fetch(:index_path, nil)

  case operation.to_sym
  when :build
    build(root, threshold, persist, index_path)
  when :reindex
    reindex(root, graph, file_mtimes, threshold, persist, index_path)
  else
    Service::Result.new(false, errors: ["Unsupported operation: #{operation}"])
  end
rescue StandardError => error
  log_error(operation, error)
  logger = defined?(Rails) && Rails.respond_to?(:logger) ? Rails.logger : nil
  logger&.debug error.backtrace
  Service::Result.new(false, errors: ["#{self.class} #{operation} failed: #{error.message}"])
end