1818
1919const AttributePool = require ( '../../static/js/AttributePool' ) ;
2020const { Pad} = require ( '../db/Pad' ) ;
21+ const async = require ( 'async' ) ;
2122const authorManager = require ( '../db/AuthorManager' ) ;
2223const db = require ( '../db/DB' ) ;
2324const hooks = require ( '../../static/js/pluginfw/hooks' ) ;
@@ -47,12 +48,16 @@ exports.setPadRaw = async (padId, r) => {
4748 if ( originalPadId !== padId ) throw new Error ( 'unexpected pad ID in record' ) ;
4849 } ;
4950
51+ // Limit the number of in-flight database queries so that the queries do not time out when
52+ // importing really large files.
53+ const q = async . queue ( async ( task ) => await task ( ) , 100 ) ;
54+
5055 // First validate and transform values. Do not commit any records to the database yet in case
5156 // there is a problem with the data.
5257
5358 const dbRecords = new Map ( ) ;
5459 const existingAuthors = new Set ( ) ;
55- await Promise . all ( Object . entries ( records ) . map ( async ( [ key , value ] ) => {
60+ await Promise . all ( Object . entries ( records ) . map ( ( [ key , value ] ) => q . pushAsync ( async ( ) => {
5661 if ( ! value ) {
5762 return ;
5863 }
@@ -91,7 +96,7 @@ exports.setPadRaw = async (padId, r) => {
9196 return ;
9297 }
9398 dbRecords . set ( key , value ) ;
94- } ) ) ;
99+ } ) ) ) ;
95100
96101 const pad = new Pad ( padId , {
97102 // Only fetchers are needed to check the pad's integrity.
@@ -109,7 +114,7 @@ exports.setPadRaw = async (padId, r) => {
109114 await pad . check ( ) ;
110115
111116 await Promise . all ( [
112- ...[ ...dbRecords ] . map ( async ( [ k , v ] ) => await db . set ( k , v ) ) ,
113- ...[ ...existingAuthors ] . map ( async ( authorId ) => await authorManager . addPad ( authorId , padId ) ) ,
117+ ...[ ...dbRecords ] . map ( ( [ k , v ] ) => q . pushAsync ( ( ) => db . set ( k , v ) ) ) ,
118+ ...[ ...existingAuthors ] . map ( ( a ) => q . pushAsync ( ( ) => authorManager . addPad ( a , padId ) ) ) ,
114119 ] ) ;
115120} ;
0 commit comments