/// <summary> /// Discovers schemas based on a query /// </summary> /// <param name="request"></param> /// <param name="context"></param> /// <returns>Discovered schemas</returns> public override async Task <DiscoverSchemasResponse> DiscoverSchemas(DiscoverSchemasRequest request, ServerCallContext context) { Logger.SetLogPrefix("discover"); Logger.Info("Discovering Schemas..."); var sampleSize = checked ((int)request.SampleSize); DiscoverSchemasResponse discoverSchemasResponse = new DiscoverSchemasResponse(); // only return requested schemas if refresh mode selected if (request.Mode == DiscoverSchemasRequest.Types.Mode.All) { // get all schemas try { await DiscoverSemaphoreSlim.WaitAsync(); var files = _server.Settings.GetAllFilesByRootPath(); Logger.Info($"Files attempted: {files.Count}"); var schemas = _server.Settings.RootPaths.Select(p => Discover.GetSchemasForDirectory(context, Utility.GetImportExportFactory(p.Mode), p, files[p.RootPathName()], sampleSize)).Select(l => l.Where(s => s != null)) .ToList(); discoverSchemasResponse.Schemas.AddRange(schemas.SelectMany(s => s)); Logger.Info($"Schemas returned: {discoverSchemasResponse.Schemas.Count}"); return(discoverSchemasResponse); } catch (Exception e) { Logger.Error(e, e.Message, context); return(new DiscoverSchemasResponse()); } finally { DiscoverSemaphoreSlim.Release(); } } try { await DiscoverSemaphoreSlim.WaitAsync(); var refreshSchemas = request.ToRefresh; Logger.Info($"Refresh schemas attempted: {refreshSchemas.Count}"); var files = _server.Settings.GetAllFilesByRootPath(); var conn = Utility.GetSqlConnection(Constants.DiscoverDbPrefix); if (sampleSize == 0) { sampleSize = 5; } foreach (var rootPath in _server.Settings.RootPaths) { var schemaName = Constants.SchemaName; var tableName = string.IsNullOrWhiteSpace(rootPath.Name) ? new DirectoryInfo(rootPath.RootPath).Name : rootPath.Name; Utility.LoadDirectoryFilesIntoDb( Utility.GetImportExportFactory(rootPath.Mode), conn, rootPath, tableName, schemaName, files[rootPath.RootPathName()], false, sampleSize, 1); } var schemas = refreshSchemas.Select(s => Discover.GetSchemaForQuery(context, s, sampleSize)) .ToArray(); discoverSchemasResponse.Schemas.AddRange(schemas.Where(x => x != null)); // return all schemas Logger.Info($"Schemas returned: {discoverSchemasResponse.Schemas.Count}"); return(discoverSchemasResponse); } catch (Exception e) { Logger.Error(e, e.Message, context); return(new DiscoverSchemasResponse()); } finally { DiscoverSemaphoreSlim.Release(); } }