@@ -113,9 +113,9 @@ type readPramsType struct {
113
113
tx db.Tx
114
114
prefix string
115
115
branchBatchSize int
116
- lineage []lineageCommit
117
116
lowestCommitID , topCommitID CommitID
118
117
branchID int64
118
+ branchQueryMap map [int64 ]sq.SelectBuilder
119
119
}
120
120
121
121
func loopByLevel (tx db.Tx , prefix , after , delimiter string , limit , branchBatchSize int , branchID int64 , requestedCommit CommitID , lineage []lineageCommit ) ([]string , error ) {
@@ -136,7 +136,7 @@ func loopByLevel(tx db.Tx, prefix, after, delimiter string, limit, branchBatchSi
136
136
branchPriorityMap [l .BranchID ] = i + 1
137
137
}
138
138
limit += 1 // increase limit to get indication of more rows to come
139
- unionQueryParts := buildBaseLevelQuery (branchID , lineage , branchBatchSize , lowestCommitID , topCommitID , len (prefix ))
139
+ unionQueryParts , branchQueryMap := buildBaseLevelQuery (branchID , lineage , branchBatchSize , lowestCommitID , topCommitID , len (prefix ))
140
140
endOfPrefixRange := prefix + DirectoryTermination
141
141
142
142
var exactFirst bool
@@ -156,10 +156,10 @@ func loopByLevel(tx db.Tx, prefix, after, delimiter string, limit, branchBatchSi
156
156
tx : tx ,
157
157
prefix : prefix ,
158
158
branchBatchSize : branchBatchSize ,
159
- lineage : lineage ,
160
159
lowestCommitID : lowestCommitID ,
161
160
topCommitID : topCommitID ,
162
161
branchID : branchID ,
162
+ branchQueryMap : branchQueryMap ,
163
163
}
164
164
165
165
for {
@@ -264,30 +264,18 @@ func getBranchResultRowsForPath(path string, branch int64, branchRanges map[int6
264
264
}
265
265
266
266
func getMoreRows (path string , branch int64 , branchRanges map [int64 ][]resultRow , readParams readPramsType ) error {
267
- var topCommitID CommitID
268
- if branch == readParams .branchID { // it is the base branch
269
- topCommitID = readParams .topCommitID
270
- } else {
271
- for _ , l := range readParams .lineage {
272
- if branch == l .BranchID {
273
- topCommitID = l .CommitID
274
- break
275
- }
276
- }
277
- }
278
267
// have to re-read the last entry, because otherwise the expression becomes complex and the optimizer gets NUTS
279
268
//so read size must be the batch size + whatever results were left from the last entry
280
269
// If readBuf is not extended - there will be an endless loop if number of results is bigger than batch size.
281
270
requiredBufferSize := readParams .branchBatchSize + len (branchRanges [branch ])
282
271
readBuf := make ([]resultRow , 0 , requiredBufferSize )
283
- singleSelect := selectSingleBranch ( branch , branch == readParams .branchID , requiredBufferSize , readParams . lowestCommitID , topCommitID , len ( readParams . prefix ))
272
+ singleSelect := readParams .branchQueryMap [ branch ]
284
273
requestedPath := readParams .prefix + path
285
- singleSelect = singleSelect .Where ("path >= ? and path < ?" , requestedPath , readParams .prefix + DirectoryTermination )
274
+ singleSelect = singleSelect .Where ("path >= ? and path < ?" , requestedPath , readParams .prefix + DirectoryTermination ). Limit ( uint64 ( requiredBufferSize ))
286
275
s , args , err := singleSelect .PlaceholderFormat (sq .Dollar ).ToSql ()
287
276
if err != nil {
288
277
return err
289
278
}
290
-
291
279
err = readParams .tx .Select (& readBuf , s , args ... )
292
280
if len (branchRanges [branch ]) == len (readBuf ) {
293
281
err = sql .ErrNoRows
@@ -333,13 +321,17 @@ func checkPathNotDeleted(pathResults []resultRow) bool {
333
321
}
334
322
}
335
323
336
- func buildBaseLevelQuery (baseBranchID int64 , lineage []lineageCommit , branchEntryLimit int , lowestCommitID , topCommitID CommitID , prefixLen int ) []sq.SelectBuilder {
324
+ func buildBaseLevelQuery (baseBranchID int64 , lineage []lineageCommit , branchEntryLimit int ,
325
+ lowestCommitID , topCommitID CommitID , prefixLen int ) ([]sq.SelectBuilder , map [int64 ]sq.SelectBuilder ) {
337
326
unionParts := make ([]sq.SelectBuilder , len (lineage )+ 1 )
327
+ unionMap := make (map [int64 ]sq.SelectBuilder )
338
328
unionParts [0 ] = selectSingleBranch (baseBranchID , true , branchEntryLimit , lowestCommitID , topCommitID , prefixLen )
329
+ unionMap [baseBranchID ] = unionParts [0 ]
339
330
for i , l := range lineage {
340
331
unionParts [i + 1 ] = selectSingleBranch (l .BranchID , false , branchEntryLimit , 1 , l .CommitID , prefixLen )
332
+ unionMap [l .BranchID ] = unionParts [i + 1 ]
341
333
}
342
- return unionParts
334
+ return unionParts , unionMap
343
335
}
344
336
345
337
func selectSingleBranch (branchID int64 , isBaseBranch bool , branchBatchSize int , lowestCommitID , topCommitID CommitID , prefixLen int ) sq.SelectBuilder {
0 commit comments