MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / findNodesByNameSubstring

Method findNodesByNameSubstring

src/db/queries.ts:2792–2845  ·  view source on GitHub ↗

* Find nodes whose name contains a substring (LIKE-based). * Useful for CamelCase-part matching where FTS fails because * e.g. "TransportSearchAction" is one FTS token, not matchable by "Search"*. * * Results are ordered by name length (shorter = more likely to be the core type).

(
    substring: string,
    options: SearchOptions & { excludePrefix?: boolean } = {}
  )

Source from the content-addressed store, hash-verified

2790 * Results are ordered by name length (shorter = more likely to be the core type).
2791 */
2792 findNodesByNameSubstring(
2793 substring: string,
2794 options: SearchOptions & { excludePrefix?: boolean } = {}
2795 ): SearchResult[] {
2796 const { kinds, languages, limit = 30, excludePrefix } = options;
2797
2798 let sql = `
2799 SELECT nodes.*, 1.0 as score
2800 FROM nodes
2801 WHERE name LIKE ?
2802 `;
2803 const params: (string | number)[] = [`%${substring}%`];
2804
2805 // Exclude prefix matches (handled by FTS-based prefix search in Step 2b)
2806 if (excludePrefix) {
2807 sql += ` AND name NOT LIKE ?`;
2808 params.push(`${substring}%`);
2809 }
2810
2811 if (kinds && kinds.length > 0) {
2812 sql += ` AND kind IN (${kinds.map(() => '?').join(',')})`;
2813 params.push(...kinds);
2814 }
2815
2816 if (languages && languages.length > 0) {
2817 sql += ` AND language IN (${languages.map(() => '?').join(',')})`;
2818 params.push(...languages);
2819 }
2820
2821 sql += ' ORDER BY length(name) ASC LIMIT ?';
2822 params.push(limit);
2823
2824 // Explore asks this four times for every word of its query, and the
2825 // leading `%` makes each one a scan of every node of those kinds (#2620).
2826 // The in-memory index answers with the same rows in the same order: ties
2827 // in length come out in the order SQLite's plan for this very statement
2828 // reads rows, the kind index's or the table's. Anything it can't answer
2829 // exactly (no kind filter, a language filter, a `%` in the word, another
2830 // plan, a transaction) is asked of SQLite.
2831 if (kinds && kinds.length > 0 && !(languages && languages.length > 0)) {
2832 const index = this.memoizedRead('nameSubstringIndex', () => this.readNameSubstringIndex());
2833 const order = index ? this.substringScanOrder(index, sql, params, excludePrefix ? 2 : 1) : null;
2834 const rowids = order ? searchNameSubstring(index!.index, substring, kinds, excludePrefix === true, limit, order) : null;
2835 if (rowids) {
2836 return this.getNodeRowsByRowid(rowids).map((row) => ({ node: rowToNode(row), score: 1.0 }));
2837 }
2838 }
2839
2840 const rows = this.db.prepare(sql).all(...params) as (NodeRow & { score: number })[];
2841 return rows.map((row) => ({
2842 node: rowToNode(row),
2843 score: row.score,
2844 }));
2845 }
2846
2847 /**
2848 * The order SQLite's plan for a substring lookup reads candidate rows in,

Callers 4

findRelevantContextMethod · 0.80
expectSameRowsFunction · 0.80

Calls 11

memoizedReadMethod · 0.95
substringScanOrderMethod · 0.95
getNodeRowsByRowidMethod · 0.95
searchNameSubstringFunction · 0.90
rowToNodeFunction · 0.85
pushMethod · 0.80
mapMethod · 0.80
allMethod · 0.65
prepareMethod · 0.65
joinMethod · 0.45

Tested by 1

expectSameRowsFunction · 0.64