From 7ca9b46e9ca8b19268bdfe599a027abfd9c079ed Mon Sep 17 00:00:00 2001 From: Mengwei Ding Date: Tue, 21 May 2019 09:46:36 -0700 Subject: [PATCH] [Code] apply loading for search and limit indexed document size (#36510) --- x-pack/plugins/code/common/file.ts | 7 +++++ .../public/components/search_page/search.tsx | 15 +++++++++-- .../code/server/indexer/lsp_indexer.ts | 26 ++++++++++++------- x-pack/plugins/code/server/routes/file.ts | 3 +-- 4 files changed, 37 insertions(+), 14 deletions(-) create mode 100644 x-pack/plugins/code/common/file.ts diff --git a/x-pack/plugins/code/common/file.ts b/x-pack/plugins/code/common/file.ts new file mode 100644 index 000000000000..518bd4321a2e --- /dev/null +++ b/x-pack/plugins/code/common/file.ts @@ -0,0 +1,7 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +export const TEXT_FILE_LIMIT = 1024 * 1024; // 1mb diff --git a/x-pack/plugins/code/public/components/search_page/search.tsx b/x-pack/plugins/code/public/components/search_page/search.tsx index e5ff099a4807..dbc9a86f64b9 100644 --- a/x-pack/plugins/code/public/components/search_page/search.tsx +++ b/x-pack/plugins/code/public/components/search_page/search.tsx @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { EuiFlexItem, EuiSpacer, EuiTitle } from '@elastic/eui'; +import { EuiFlexItem, EuiLoadingSpinner, EuiSpacer, EuiText, EuiTitle } from '@elastic/eui'; import querystring from 'querystring'; import React from 'react'; import { connect } from 'react-redux'; @@ -119,11 +119,22 @@ class SearchPage extends React.PureComponent { scope, documentSearchResults, languages, + isLoading, repositories, repositorySearchResults, } = this.props; - let mainComp = ( + let mainComp = isLoading ? ( +
+ + + Loading... + + + + +
+ ) : ( { diff --git a/x-pack/plugins/code/server/indexer/lsp_indexer.ts b/x-pack/plugins/code/server/indexer/lsp_indexer.ts index 84134f802e73..77ec85518adf 100644 --- a/x-pack/plugins/code/server/indexer/lsp_indexer.ts +++ b/x-pack/plugins/code/server/indexer/lsp_indexer.ts @@ -8,6 +8,7 @@ import fs from 'fs'; import util from 'util'; import { ProgressReporter } from '.'; +import { TEXT_FILE_LIMIT } from '../../common/file'; import { toCanonicalUrl } from '../../common/uri_util'; import { Document, IndexStats, IndexStatsKey, LspIndexRequest, RepositoryUri } from '../../model'; import { GitOperations } from '../git_operations'; @@ -185,6 +186,21 @@ export class LspIndexer extends AbstractIndexer { const lspDocUri = toCanonicalUrl({ repoUri, revision, file: filePath, schema: 'git:' }); const symbolNames = new Set(); + const localFilePath = `${localRepoPath}${filePath}`; + const lstat = util.promisify(fs.lstat); + const stat = await lstat(localFilePath); + + const readLink = util.promisify(fs.readlink); + const readFile = util.promisify(fs.readFile); + const content = stat.isSymbolicLink() + ? await readLink(localFilePath, 'utf8') + : await readFile(localFilePath, 'utf8'); + + if (content.length > TEXT_FILE_LIMIT) { + this.log.debug(`File size exceeds limit. Skip index.`); + return stats; + } + try { const lang = detectLanguageByFilename(filePath); // filter file by language @@ -219,16 +235,6 @@ export class LspIndexer extends AbstractIndexer { this.log.error(error); } - const localFilePath = `${localRepoPath}${filePath}`; - const lstat = util.promisify(fs.lstat); - const stat = await lstat(localFilePath); - - const readLink = util.promisify(fs.readlink); - const readFile = util.promisify(fs.readFile); - const content = stat.isSymbolicLink() - ? await readLink(localFilePath, 'utf8') - : await readFile(localFilePath, 'utf8'); - const language = await detectLanguage(filePath, Buffer.from(content)); const body: Document = { repoUri, diff --git a/x-pack/plugins/code/server/routes/file.ts b/x-pack/plugins/code/server/routes/file.ts index 505af9e7a22a..714694993f60 100644 --- a/x-pack/plugins/code/server/routes/file.ts +++ b/x-pack/plugins/code/server/routes/file.ts @@ -20,8 +20,7 @@ import { detectLanguage } from '../utils/detect_language'; import { CodeServerRouter } from '../security'; import { RepositoryObjectClient } from '../search'; import { EsClientWithRequest } from '../utils/esclient_with_request'; - -const TEXT_FILE_LIMIT = 1024 * 1024; // 1mb +import { TEXT_FILE_LIMIT } from '../../common/file'; export function fileRoute(server: CodeServerRouter, options: ServerOptions) { async function repoExists(req: hapi.Request, repoUri: string) {