Module: Msnav::Datadir

Defined in:
lib/msnav/datadir.rb

Overview

Package-owned data directory — mirrors coderag/datadir.py exactly, so an index built by coderag index on the host is found at the same place inside the container (the one uniform bind mount: ~/.local/share/coderag -> /root/.local/share/coderag).

Constant Summary collapse

MATCH_MIN_FILES =

content matching: enough overlap between the index's cached file paths and the files actually on disk to be sure this folder IS that service

3
MATCH_MIN_RATIO =
0.6
SAMPLE_LIMIT =
2000
SKIP_DIRS =
%w[.git vendor node_modules tmp log spec test].freeze

Class Method Summary collapse

Class Method Details

.data_dirObject



15
16
17
18
19
20
21
# File 'lib/msnav/datadir.rb', line 15

def data_dir
  override = ENV["CODERAG_DATA_DIR"]
  return Pathname.new(File.expand_path(override)) if override && !override.empty?
  xdg = ENV["XDG_DATA_HOME"]
  base = xdg && !xdg.empty? ? Pathname.new(File.expand_path(xdg)) : Pathname.new(Dir.home) + ".local" + "share"
  base + "coderag"
end

.disk_ruby_files(service_root) ⇒ Object

Relative .rb paths under SERVICE_ROOT (bounded walk).



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/msnav/datadir.rb', line 40

def disk_ruby_files(service_root)
  found = {}
  queue = [Pathname.new(service_root)]
  root = Pathname.new(service_root)
  until queue.empty?
    dir = queue.shift
    children = begin
      dir.children
    rescue SystemCallError
      next
    end
    children.each do |entry|
      if entry.directory?
        queue << entry unless SKIP_DIRS.include?(entry.basename.to_s)
      elsif entry.extname == ".rb"
        found[entry.relative_path_from(root).to_s.tr(File::SEPARATOR, "/")] = true
        return found if found.length >= SAMPLE_LIMIT
      end
    end
  end
  found
end

.find_hubs_for_service_root(service_root) ⇒ Object

[hub dir, service dirname] candidates for the service at SERVICE_ROOT — how a service-only container identifies its hub with nothing but the data-dir mount.



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/msnav/datadir.rb', line 108

def find_hubs_for_service_root(service_root)
  hubs = data_dir + "hubs"
  return [] unless hubs.directory?
  matches = []
  hubs.children.sort.each do |hub|
    db = hub + "coderag.db"
    next unless db.file?
    dirname = identify_service(db, service_root)
    matches << [hub, dirname] if dirname
  end
  matches
end

.hub_dir(workspace_root) ⇒ Object

The hub's slice of the data dir: readable name + path-hash suffix. Must match coderag's sha256(str(resolved_root))[:10].



25
26
27
28
29
30
# File 'lib/msnav/datadir.rb', line 25

def hub_dir(workspace_root)
  root = Pathname.new(File.expand_path(workspace_root))
  root = root.realpath if root.exist?
  key = Digest::SHA256.hexdigest(root.to_s)[0, 10]
  data_dir + "hubs" + "#{root.basename}-#{key}"
end

.identify_service(db_path, service_root) ⇒ Object

Which indexed service (dirname) does SERVICE_ROOT hold? Directory-name match first; otherwise by content — the service whose cached file paths are actually present on disk (mounts like /app don't carry the service name). Mirrors coderag's datadir.identify_service.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/msnav/datadir.rb', line 67

def identify_service(db_path, service_root)
  service_root = Pathname.new(service_root)
  con = begin
    SQLite3::Database.new(db_path.to_s, readonly: true)
  rescue SQLite3::Exception
    return nil
  end
  begin
    dirnames = begin
      con.execute("SELECT dirname FROM services").map { |r| r[0] }
    rescue SQLite3::Exception
      return nil
    end
    base = service_root.basename.to_s
    return base if dirnames.include?(base)
    disk = disk_ruby_files(service_root)
    return nil if disk.empty?
    best = nil
    dirnames.each do |dirname|
      paths = begin
        con.execute("SELECT path FROM facts WHERE service = ?", [dirname])
           .map { |r| r[0] }
      rescue SQLite3::Exception
        return nil
      end
      present = paths.count { |p| disk.key?(p) }
      next if present < MATCH_MIN_FILES || paths.empty?
      ratio = present.to_f / paths.length
      if ratio >= MATCH_MIN_RATIO && (best.nil? || ratio > best[0])
        best = [ratio, dirname]
      end
    end
    best && best[1]
  ensure
    con.close
  end
end