@@ -810,18 +810,40 @@ export class ExactReviewDirectPublicationStore {
810810 cursor : number ;
811811 limit : number ;
812812 maxBytes : number ;
813+ maxSourceBytes : number ;
814+ maxRecords : number ;
813815 } ) : { records : RecordExportEntry [ ] ; nextCursor : number | null ; watermark : number } {
814816 const placeholders = options . sections . map ( ( ) => "?" ) . join ( ", " ) ;
815817 const rows = Array . from (
816818 this . storage . sql . exec (
817- `SELECT repo_slug, section, record_id, digest, deleted, revision, store_revision,
818- source, updated_at
819- FROM ${ EXACT_REVIEW_RECORD_EXPORT_INDEX_TABLE }
820- WHERE repo_slug = ?
821- AND section IN (${ placeholders } )
822- AND store_revision > ?
823- AND store_revision > ?
824- ORDER BY store_revision
819+ `SELECT export.repo_slug, export.section, export.record_id, export.digest,
820+ export.deleted, export.revision, export.store_revision, export.source,
821+ export.updated_at,
822+ CASE
823+ WHEN export.deleted = 1 THEN '0'
824+ WHEN export.source = 'canonical' AND export.section <> 'commits'
825+ THEN CAST(canonical.byte_length AS TEXT)
826+ ELSE CAST(backfill.byte_length AS TEXT)
827+ END AS source_byte_length
828+ FROM ${ EXACT_REVIEW_RECORD_EXPORT_INDEX_TABLE } export
829+ LEFT JOIN ${ EXACT_REVIEW_CANONICAL_RECORD_TABLE } canonical
830+ ON export.deleted = 0
831+ AND export.source = 'canonical'
832+ AND export.section <> 'commits'
833+ AND canonical.repo_slug = export.repo_slug
834+ AND canonical.section = export.section
835+ AND canonical.item_id = CAST(export.record_id AS INTEGER)
836+ LEFT JOIN ${ EXACT_REVIEW_RECORD_BACKFILL_TABLE } backfill
837+ ON export.deleted = 0
838+ AND (export.source = 'backfill' OR export.section = 'commits')
839+ AND backfill.repo_slug = export.repo_slug
840+ AND backfill.section = export.section
841+ AND backfill.record_id = export.record_id
842+ WHERE export.repo_slug = ?
843+ AND export.section IN (${ placeholders } )
844+ AND export.store_revision > ?
845+ AND export.store_revision > ?
846+ ORDER BY export.store_revision
825847 LIMIT ?` ,
826848 options . repoSlug ,
827849 ...options . sections ,
@@ -830,9 +852,20 @@ export class ExactReviewDirectPublicationStore {
830852 options . limit ,
831853 ) ,
832854 ) ;
855+ const selectedRows : Record < string , unknown > [ ] = [ ] ;
856+ let sourceBytes = 0 ;
857+ for ( const row of rows ) {
858+ if ( selectedRows . length >= options . maxRecords ) break ;
859+ const byteLength = recordExportSourceByteLength ( row ) ;
860+ if ( selectedRows . length > 0 && byteLength > options . maxSourceBytes - sourceBytes ) {
861+ break ;
862+ }
863+ selectedRows . push ( row ) ;
864+ sourceBytes += byteLength ;
865+ }
833866 const records : RecordExportEntry [ ] = [ ] ;
834867 let responseBytes = 0 ;
835- for ( const row of rows ) {
868+ for ( const row of selectedRows ) {
836869 const entry = this . recordExportEntrySync ( row ) ;
837870 const entryBytes = new TextEncoder ( ) . encode ( JSON . stringify ( entry ) ) . byteLength ;
838871 if ( records . length && responseBytes + entryBytes > options . maxBytes ) break ;
@@ -1304,6 +1337,22 @@ export class ExactReviewDirectPublicationStore {
13041337 }
13051338}
13061339
1340+ function recordExportSourceByteLength ( row : Record < string , unknown > ) {
1341+ const value = row . source_byte_length ;
1342+ const byteLength =
1343+ typeof value === "string" && / ^ ( 0 | [ 1 - 9 ] \d * ) $ / . test ( value ) ? Number ( value ) : NaN ;
1344+ if (
1345+ ! Number . isSafeInteger ( byteLength ) ||
1346+ byteLength < 0 ||
1347+ ( Number ( row . deleted ) === 1 && byteLength !== 0 )
1348+ ) {
1349+ throw new Error (
1350+ `invalid record export byte metadata: ${ String ( row . repo_slug ) } /${ String ( row . section ) } /${ String ( row . record_id ) } ` ,
1351+ ) ;
1352+ }
1353+ return byteLength ;
1354+ }
1355+
13071356function canonicalTupleOperations (
13081357 plan : CanonicalDirectPublicationPlan ,
13091358) : CanonicalDirectPublicationOperation [ ] {
0 commit comments