Skip to content

Commit ffaa811

Browse files
committed
refactor: Relocate code from fulltext to explain/index
1 parent 3b97d13 commit ffaa811

22 files changed

+270
-165
lines changed

packages/cli/src/cmds/search/search.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import { FileIndex } from '@appland/search';
99
import { handleWorkingDirectory } from '../../lib/handleWorkingDirectory';
1010
import { verbose } from '../../utils';
1111
import searchSingleAppMap, { SearchOptions as SingleSearchOptions } from './searchSingleAppMap';
12-
import { SearchResponse as DiagramsSearchResponse } from '../../fulltext/appmap-match';
12+
import { SearchResponse as DiagramsSearchResponse } from '../../rpc/explain/index/appmap-match';
1313
import {
1414
SearchResult as EventSearchResult,
1515
SearchResponse as EventSearchResponse,
1616
} from '../../fulltext/FindEvents';
1717
import { openInBrowser } from '../open/openers';
18-
import { buildAppMapIndex, search } from '../../fulltext/appmap-index';
18+
import { buildAppMapIndex, search } from '../../rpc/explain/index/appmap-index';
1919
import buildIndexInTempDir from '../../rpc/explain/index/build-index-in-temp-dir';
2020

2121
export const command = 'search <query>';

packages/cli/src/fulltext/FindEvents.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import assert from 'assert';
77

88
import { verbose } from '../utils';
99
import { collectParameters } from './collectParameters';
10-
import { fileNameMatchesFilterPatterns } from './fileNameMatchesFilterPatterns';
10+
import { fileNameMatchesFilterPatterns } from '../rpc/explain/index/filter-patterns';
1111

1212
type IndexItem = {
1313
fqid: string;

packages/cli/src/fulltext/ref.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

packages/cli/src/rpc/explain/collect-search-context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ContextV2, applyContext } from '@appland/navie';
44
import { SearchRpc } from '@appland/rpc';
55

66
import { DEFAULT_MAX_DIAGRAMS } from '../search/search';
7-
import { SearchResponse as AppMapSearchResponse } from '../../fulltext/appmap-match';
7+
import { SearchResponse as AppMapSearchResponse } from './index/appmap-match';
88
import { searchAppMapFiles } from './index/appmap-file-index';
99
import { searchProjectFiles } from './index/project-file-index';
1010
import {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { FilterFn, isBinaryFile, isDataFile, isLargeFile } from '@appland/search';
2+
import makeDebug from 'debug';
3+
import { fileNameMatchesFilterPatterns } from './index/filter-patterns';
4+
5+
const debug = makeDebug('appmap:rpc:explain:file-filter');
6+
7+
export default function fileFilter(
8+
includePatterns: RegExp[] | undefined,
9+
excludePatterns: RegExp[] | undefined
10+
): FilterFn {
11+
return async (path: string) => {
12+
debug('Filtering: %s', path);
13+
if (isBinaryFile(path)) {
14+
debug('Skipping binary file: %s', path);
15+
return false;
16+
}
17+
18+
const includeFile = fileNameMatchesFilterPatterns(path, includePatterns, excludePatterns);
19+
if (!includeFile) return false;
20+
21+
const isData = isDataFile(path);
22+
if (isData && (await isLargeFile(path))) {
23+
debug('Skipping large data file: %s', path);
24+
return false;
25+
}
26+
27+
return true;
28+
};
29+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import sqlite3 from 'better-sqlite3';
2+
import makeDebug from 'debug';
3+
4+
import {
5+
buildFileIndex,
6+
FileIndex,
7+
fileTokens,
8+
FilterFn,
9+
isBinaryFile,
10+
isDataFile,
11+
isLargeFile,
12+
listProjectFiles,
13+
readFileSafe,
14+
} from '@appland/search';
15+
import { fileNameMatchesFilterPatterns } from './index/filter-patterns';
16+
17+
const debug = makeDebug('appmap:rpc:explain:index-files');
18+
19+
function fileFilter(
20+
includePatterns: RegExp[] | undefined,
21+
excludePatterns: RegExp[] | undefined
22+
): FilterFn {
23+
return async (path: string) => {
24+
debug('Filtering: %s', path);
25+
if (isBinaryFile(path)) {
26+
debug('Skipping binary file: %s', path);
27+
return false;
28+
}
29+
30+
const includeFile = fileNameMatchesFilterPatterns(path, includePatterns, excludePatterns);
31+
if (!includeFile) return false;
32+
33+
const isData = isDataFile(path);
34+
if (isData && (await isLargeFile(path))) {
35+
debug('Skipping large data file: %s', path);
36+
return false;
37+
}
38+
39+
return true;
40+
};
41+
}
42+
43+
export default async function indexFiles(
44+
db: sqlite3.Database,
45+
directories: string[],
46+
includePatterns: RegExp[] | undefined,
47+
excludePatterns: RegExp[] | undefined
48+
): Promise<FileIndex> {
49+
const fileIndex = new FileIndex(db);
50+
51+
const filter = fileFilter(includePatterns, excludePatterns);
52+
await buildFileIndex(fileIndex, directories, listProjectFiles, filter, readFileSafe, fileTokens);
53+
54+
return fileIndex;
55+
}

packages/cli/src/rpc/explain/index/appmap-file-index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import sqlite3 from 'better-sqlite3';
33
import { FileIndex } from '@appland/search';
44

55
import buildIndexInTempDir, { CloseableIndex } from './build-index-in-temp-dir';
6-
import { buildAppMapIndex, search } from '../../../fulltext/appmap-index';
7-
import { SearchResponse } from '../../../fulltext/appmap-match';
6+
import { buildAppMapIndex, search } from './appmap-index';
7+
import { SearchResponse } from './appmap-match';
88

99
export async function buildAppMapFileIndex(
1010
appmapDirectories: string[]

packages/cli/src/fulltext/appmap-index.ts renamed to packages/cli/src/rpc/explain/index/appmap-index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { readFile } from 'fs/promises';
55
import { Metadata } from '@appland/models';
66
import { buildFileIndex, FileIndex, fileTokens } from '@appland/search';
77

8-
import { findFiles, isNodeError, verbose } from '../utils';
8+
import { findFiles, isNodeError, verbose } from '../../../utils';
99
import {
1010
downscoreOutOfDateMatches,
1111
Match,
@@ -14,7 +14,7 @@ import {
1414
scoreMatches,
1515
SearchResponse,
1616
} from './appmap-match';
17-
import loadAppMapConfig from '../lib/loadAppMapConfig';
17+
import loadAppMapConfig from '../../../lib/loadAppMapConfig';
1818

1919
export type ClassMapEntry = {
2020
name: string;

packages/cli/src/fulltext/appmap-match.ts renamed to packages/cli/src/rpc/explain/index/appmap-match.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import UpToDate from '../lib/UpToDate';
2-
import { exists } from '../utils';
1+
import UpToDate from '../../../lib/UpToDate';
2+
import { exists } from '../../../utils';
33

44
import makeDebug from 'debug';
55

0 commit comments

Comments
 (0)