@@ -98,7 +98,6 @@ export const enqueTermsToBePosted = async (docs) => {
9898
9999}
100100export const addDocsToStore = async ( docs ) => {
101- debugger ;
102101 let t1 = new Date ( )
103102 await dataTable . bulkAdd ( docs ) ;
104103 let t2 = new Date ( )
@@ -116,54 +115,72 @@ export const addDocsToStore = async (docs) => {
116115}
117116
118117export const moveNewTermsFromPostingsQueueToPostingsTable = async ( db ) => {
119- const terms = await postingsQueueTable . where ( "newTerm" ) . equals ( 1 ) . toArray ( )
120118 const step = 1000 ;
121- for ( let i = 0 ; i < terms . length ; i += step ) {
119+ let collection = postingsQueueTable . where ( "newTerm" ) . equals ( 1 ) . limit ( step )
120+ let terms = await collection . toArray ( )
121+ debugger ;
122+
123+ while ( terms . length > 0 ) {
122124 await db . transaction ( 'rw' , [ DF_SCHEMA , POSTING_QUEUE_SCHEMA , POSTINGS_SCHEMA ] , async tx => {
123- const currentSet = terms . slice ( i , i + step ) ;
124- const insertResults = await postingsTable . bulkAdd ( currentSet )
125- await dfTable . bulkAdd ( currentSet . map ( x => ( { trigram :x . trigram , freq : x . docs . length } ) ) )
126- await postingsQueueTable . where ( "trigram" ) . anyOf ( terms . map ( x => x . trigram ) ) . delete ( )
127-
125+ let t1 = new Date ( )
126+ const insertResults = await postingsTable . bulkAdd ( terms )
127+ await dfTable . bulkAdd ( terms . map ( x => ( { trigram : x . trigram , freq : x . docs . length } ) ) )
128+ await collection . delete ( )
129+ collection = postingsQueueTable . where ( "newTerm" ) . equals ( 1 ) . limit ( step )
130+ terms = await collection . toArray ( )
131+ let t2 = new Date ( )
132+ console . log ( `moved ${ terms . length } items from postings que` )
133+
128134 } )
129135 }
130136}
137+ const getTermsMap = ( terms ) => terms . reduce ( ( map , termObj ) => {
138+ map [ termObj . trigram ] = termObj
139+ return map
140+ } , { } )
131141
142+ const appendIngLogic = async ( db ) => {
143+
144+ }
132145export const appendDocsFromQueueToExistingPostingsItems = async ( db ) => {
133146 //Get the terms that need to be updated
134- const terms = await postingsQueueTable . where ( "newTerm" ) . equals ( 0 ) . toArray ( )
147+ const step = 400 ; //This is low because larger values give an IPC error
148+ let collection = postingsQueueTable . where ( "newTerm" ) . equals ( 0 ) . limit ( step ) ;
149+
150+ let terms = await collection . toArray ( )
151+ debugger ;
135152 console . log ( `Got ${ terms . length } terms to update in postings table` )
136- const termsMap = terms . reduce ( ( map , termObj ) => {
137- map [ termObj . trigram ] = termObj
138- return map
139- } , { } )
153+ let termsMap = getTermsMap ( terms )
154+
155+ let termKeys = Object . keys ( termsMap ) ;
140156
141- const termKeys = Object . keys ( termsMap ) ;
142- const step = 1000 ;
143157 console . log ( `Term map built` )
144-
145- for ( let i = 0 ; i < termKeys . length ; i += step ) {
158+ do {
146159 let t1 = new Date ( )
147- await db . transaction ( 'rw' , [ DF_SCHEMA , POSTING_QUEUE_SCHEMA , POSTINGS_SCHEMA ] , async tx => {
148-
149- const currentSet = termKeys . slice ( i , i + step ) ;
150-
151- await postingsTable . where ( "trigram" ) . anyOf ( currentSet ) . modify ( posting => {
160+ await db . transaction ( 'rw' , [ DF_SCHEMA , POSTING_QUEUE_SCHEMA , POSTINGS_SCHEMA ] , async tx => {
161+ let collection = postingsQueueTable . where ( "newTerm" ) . equals ( 0 ) . limit ( step ) ;
162+ let terms = await collection . toArray ( )
163+ if ( terms . length === 0 ) {
164+ }
165+ else {
166+
167+ // Linter warns us about something potentially quite bad https://eslint.org/docs/rules/no-loop-func
168+ await postingsTable . where ( "trigram" ) . anyOf ( termKeys ) . modify ( posting => {
152169 const newDocIds = termsMap [ posting . trigram ] . docs
153170 posting . docs = posting . docs . concat ( newDocIds ) ;
154171 } )
155- await dfTable . where ( "trigram" ) . anyOf ( currentSet ) . modify ( df => {
156-
157- df . freq = df . freq + termsMap [ df . trigram ] . docs . length
172+ await dfTable . where ( "trigram" ) . anyOf ( termKeys ) . modify ( df => {
173+
174+ df . freq = df . freq + termsMap [ df . trigram ] . docs . length
158175 } )
159176
160- await postingsQueueTable . where ( "trigram" ) . anyOf ( currentSet ) . delete ( )
177+ await collection . delete ( )
178+
161179
180+ }
162181 } )
163182 let t2 = new Date ( ) ;
164- console . log ( `Updated ${ step } terms in postings table in ${ t2 - t1 } ms` )
165- }
166-
167- console . log ( `Postings table updated` )
168- await postingsQueueTable . where ( "newTerm" ) . equals ( 0 ) . delete ( )
183+ console . log ( `Updated ${ step } terms in postings table in ${ t2 - t1 } ms` )
184+ } while ( ( await postingsQueueTable . where ( "newTerm" ) . equals ( 0 ) . count ( ) ) > 0 )
185+
169186}
0 commit comments