@@ -24,6 +24,7 @@ type pidSet map[common.Pgid]struct{}
24
24
type freelist struct {
25
25
freelistType FreelistType // freelist type
26
26
ids []common.Pgid // all free and available free page ids.
27
+ readonlyTXIDs []common.Txid // all readonly transaction IDs.
27
28
allocs map [common.Pgid ]common.Txid // mapping of Txid that allocated a pgid.
28
29
pending map [common.Txid ]* txPending // mapping of soon-to-be free page ids by tx.
29
30
cache map [common.Pgid ]struct {} // fast lookup of all free and pending page ids.
@@ -326,3 +327,44 @@ func (f *freelist) reindex() {
326
327
}
327
328
}
328
329
}
330
+
331
+ func (f * freelist ) addReadonlyTXID (tid common.Txid ) {
332
+ f .readonlyTXIDs = append (f .readonlyTXIDs , tid )
333
+ }
334
+
335
+ func (f * freelist ) removeReadonlyTXID (tid common.Txid ) {
336
+ for i := range f .readonlyTXIDs {
337
+ if f .readonlyTXIDs [i ] == tid {
338
+ last := len (f .readonlyTXIDs ) - 1
339
+ f .readonlyTXIDs [i ] = f .readonlyTXIDs [last ]
340
+ f .readonlyTXIDs = f .readonlyTXIDs [:last ]
341
+ break
342
+ }
343
+ }
344
+ }
345
+
346
+ type txIDx []common.Txid
347
+
348
+ func (t txIDx ) Len () int { return len (t ) }
349
+ func (t txIDx ) Swap (i , j int ) { t [i ], t [j ] = t [j ], t [i ] }
350
+ func (t txIDx ) Less (i , j int ) bool { return t [i ] < t [j ] }
351
+
352
+ // freePages releases any pages associated with closed read-only transactions.
353
+ func (f * freelist ) freePages () {
354
+ // Free all pending pages prior to earliest open transaction.
355
+ sort .Sort (txIDx (f .readonlyTXIDs ))
356
+ minid := common .Txid (0xFFFFFFFFFFFFFFFF )
357
+ if len (f .readonlyTXIDs ) > 0 {
358
+ minid = f .readonlyTXIDs [0 ]
359
+ }
360
+ if minid > 0 {
361
+ f .release (minid - 1 )
362
+ }
363
+ // Release unused txid extents.
364
+ for _ , tid := range f .readonlyTXIDs {
365
+ f .releaseRange (minid , tid - 1 )
366
+ minid = tid + 1
367
+ }
368
+ f .releaseRange (minid , common .Txid (0xFFFFFFFFFFFFFFFF ))
369
+ // Any page both allocated and freed in an extent is safe to release.
370
+ }
0 commit comments