@@ -15,6 +15,7 @@ import {
1515 PrestoQueryJobCreationSchema ,
1616 PrestoQueryJobSchema ,
1717} from "../../../schemas/presto-search.js" ;
18+ import { MAX_PRESTO_SEARCH_RESULTS } from "./typings.js" ;
1819import { insertPrestoRowsToMongo } from "./utils.js" ;
1920
2021
@@ -61,18 +62,20 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
6162 const { queryString} = request . body ;
6263
6364 let searchJobId : string ;
65+ let totalResultsCount = 0 ;
66+ let storedResultsCount = 0 ;
6467
6568 try {
6669 // eslint-disable-next-line max-lines-per-function
6770 searchJobId = await new Promise < string > ( ( resolve , reject ) => {
6871 let isResolved = false ;
6972 Presto . client . execute ( {
70- // eslint-disable-next-line no-warning-comments
71- // TODO: Error, and success handlers are dummy implementations
72- // and will be replaced with proper implementations.
7373 data : ( _ , data , columns ) => {
74+ totalResultsCount += data . length ;
75+
7476 request . log . info (
75- `Received ${ data . length } rows from Presto query`
77+ `Received ${ data . length } rows from Presto query ` +
78+ `(total: ${ totalResultsCount } )`
7679 ) ;
7780
7881 if ( false === isResolved ) {
@@ -88,15 +91,35 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
8891 return ;
8992 }
9093
91- insertPrestoRowsToMongo (
92- data ,
93- columns ,
94- searchJobId ,
95- mongoDb
94+ if ( storedResultsCount < MAX_PRESTO_SEARCH_RESULTS ) {
95+ const remainingSlots =
96+ MAX_PRESTO_SEARCH_RESULTS - storedResultsCount ;
97+ const dataToInsert = data . slice ( 0 , remainingSlots ) ;
98+
99+ if ( 0 < dataToInsert . length ) {
100+ storedResultsCount += dataToInsert . length ;
101+ insertPrestoRowsToMongo (
102+ dataToInsert ,
103+ columns ,
104+ searchJobId ,
105+ mongoDb
106+ ) . catch ( ( err : unknown ) => {
107+ request . log . error (
108+ err ,
109+ "Failed to insert Presto results into MongoDB"
110+ ) ;
111+ } ) ;
112+ }
113+ }
114+
115+ // Always update metadata with total count
116+ searchResultsMetadataCollection . updateOne (
117+ { _id : searchJobId } ,
118+ { $set : { numTotalResults : totalResultsCount } }
96119 ) . catch ( ( err : unknown ) => {
97120 request . log . error (
98121 err ,
99- "Failed to insert Presto results into MongoDB "
122+ "Failed to update total results count in metadata "
100123 ) ;
101124 } ) ;
102125 } ,
0 commit comments