| 1 |
package bolt |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"sort" |
| 6 |
"unsafe" |
| 7 |
) |
| 8 |
|
| 9 |
// txPending holds a list of pgids and corresponding allocation txns |
| 10 |
// that are pending to be freed. |
| 11 |
type txPending struct { |
| 12 |
ids []pgid |
| 13 |
alloctx []txid // txids allocating the ids |
| 14 |
lastReleaseBegin txid // beginning txid of last matching releaseRange |
| 15 |
} |
| 16 |
|
| 17 |
// freelist represents a list of all pages that are available for allocation. |
| 18 |
// It also tracks pages that have been freed but are still in use by open transactions. |
| 19 |
type freelist struct { |
| 20 |
ids []pgid // all free and available free page ids. |
| 21 |
allocs map[pgid]txid // mapping of txid that allocated a pgid. |
| 22 |
pending map[txid]*txPending // mapping of soon-to-be free page ids by tx. |
| 23 |
cache map[pgid]bool // fast lookup of all free and pending page ids. |
| 24 |
} |
| 25 |
|
| 26 |
// newFreelist returns an empty, initialized freelist. |
| 27 |
func newFreelist() *freelist { |
| 28 |
return &freelist{ |
| 29 |
allocs: make(map[pgid]txid), |
| 30 |
pending: make(map[txid]*txPending), |
| 31 |
cache: make(map[pgid]bool), |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
// size returns the size of the page after serialization. |
| 36 |
func (f *freelist) size() int { |
| 37 |
n := f.count() |
| 38 |
if n >= 0xFFFF { |
| 39 |
// The first element will be used to store the count. See freelist.write. |
| 40 |
n++ |
| 41 |
} |
| 42 |
return pageHeaderSize + (int(unsafe.Sizeof(pgid(0))) * n) |
| 43 |
} |
| 44 |
|
| 45 |
// count returns count of pages on the freelist |
| 46 |
func (f *freelist) count() int { |
| 47 |
return f.free_count() + f.pending_count() |
| 48 |
} |
| 49 |
|
| 50 |
// free_count returns count of free pages |
| 51 |
func (f *freelist) free_count() int { |
| 52 |
return len(f.ids) |
| 53 |
} |
| 54 |
|
| 55 |
// pending_count returns count of pending pages |
| 56 |
func (f *freelist) pending_count() int { |
| 57 |
var count int |
| 58 |
for _, txp := range f.pending { |
| 59 |
count += len(txp.ids) |
| 60 |
} |
| 61 |
return count |
| 62 |
} |
| 63 |
|
| 64 |
// copyall copies into dst a list of all free ids and all pending ids in one sorted list. |
| 65 |
// f.count returns the minimum length required for dst. |
| 66 |
func (f *freelist) copyall(dst []pgid) { |
| 67 |
m := make(pgids, 0, f.pending_count()) |
| 68 |
for _, txp := range f.pending { |
| 69 |
m = append(m, txp.ids...) |
| 70 |
} |
| 71 |
sort.Sort(m) |
| 72 |
mergepgids(dst, f.ids, m) |
| 73 |
} |
| 74 |
|
| 75 |
// allocate returns the starting page id of a contiguous list of pages of a given size. |
| 76 |
// If a contiguous block cannot be found then 0 is returned. |
| 77 |
func (f *freelist) allocate(txid txid, n int) pgid { |
| 78 |
if len(f.ids) == 0 { |
| 79 |
return 0 |
| 80 |
} |
| 81 |
|
| 82 |
var initial, previd pgid |
| 83 |
for i, id := range f.ids { |
| 84 |
if id <= 1 { |
| 85 |
panic(fmt.Sprintf("invalid page allocation: %d", id)) |
| 86 |
} |
| 87 |
|
| 88 |
// Reset initial page if this is not contiguous. |
| 89 |
if previd == 0 || id-previd != 1 { |
| 90 |
initial = id |
| 91 |
} |
| 92 |
|
| 93 |
// If we found a contiguous block then remove it and return it. |
| 94 |
if (id-initial)+1 == pgid(n) { |
| 95 |
// If we're allocating off the beginning then take the fast path |
| 96 |
// and just adjust the existing slice. This will use extra memory |
| 97 |
// temporarily but the append() in free() will realloc the slice |
| 98 |
// as is necessary. |
| 99 |
if (i + 1) == n { |
| 100 |
f.ids = f.ids[i+1:] |
| 101 |
} else { |
| 102 |
copy(f.ids[i-n+1:], f.ids[i+1:]) |
| 103 |
f.ids = f.ids[:len(f.ids)-n] |
| 104 |
} |
| 105 |
|
| 106 |
// Remove from the free cache. |
| 107 |
for i := pgid(0); i < pgid(n); i++ { |
| 108 |
delete(f.cache, initial+i) |
| 109 |
} |
| 110 |
f.allocs[initial] = txid |
| 111 |
return initial |
| 112 |
} |
| 113 |
|
| 114 |
previd = id |
| 115 |
} |
| 116 |
return 0 |
| 117 |
} |
| 118 |
|
| 119 |
// free releases a page and its overflow for a given transaction id. |
| 120 |
// If the page is already free then a panic will occur. |
| 121 |
func (f *freelist) free(txid txid, p *page) { |
| 122 |
if p.id <= 1 { |
| 123 |
panic(fmt.Sprintf("cannot free page 0 or 1: %d", p.id)) |
| 124 |
} |
| 125 |
|
| 126 |
// Free page and all its overflow pages. |
| 127 |
txp := f.pending[txid] |
| 128 |
if txp == nil { |
| 129 |
txp = &txPending{} |
| 130 |
f.pending[txid] = txp |
| 131 |
} |
| 132 |
allocTxid, ok := f.allocs[p.id] |
| 133 |
if ok { |
| 134 |
delete(f.allocs, p.id) |
| 135 |
} else if (p.flags & freelistPageFlag) != 0 { |
| 136 |
// Freelist is always allocated by prior tx. |
| 137 |
allocTxid = txid - 1 |
| 138 |
} |
| 139 |
|
| 140 |
for id := p.id; id <= p.id+pgid(p.overflow); id++ { |
| 141 |
// Verify that page is not already free. |
| 142 |
if f.cache[id] { |
| 143 |
panic(fmt.Sprintf("page %d already freed", id)) |
| 144 |
} |
| 145 |
// Add to the freelist and cache. |
| 146 |
txp.ids = append(txp.ids, id) |
| 147 |
txp.alloctx = append(txp.alloctx, allocTxid) |
| 148 |
f.cache[id] = true |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
// release moves all page ids for a transaction id (or older) to the freelist. |
| 153 |
func (f *freelist) release(txid txid) { |
| 154 |
m := make(pgids, 0) |
| 155 |
for tid, txp := range f.pending { |
| 156 |
if tid <= txid { |
| 157 |
// Move transaction's pending pages to the available freelist. |
| 158 |
// Don't remove from the cache since the page is still free. |
| 159 |
m = append(m, txp.ids...) |
| 160 |
delete(f.pending, tid) |
| 161 |
} |
| 162 |
} |
| 163 |
sort.Sort(m) |
| 164 |
f.ids = pgids(f.ids).merge(m) |
| 165 |
} |
| 166 |
|
| 167 |
// releaseRange moves pending pages allocated within an extent [begin,end] to the free list. |
| 168 |
func (f *freelist) releaseRange(begin, end txid) { |
| 169 |
if begin > end { |
| 170 |
return |
| 171 |
} |
| 172 |
var m pgids |
| 173 |
for tid, txp := range f.pending { |
| 174 |
if tid < begin || tid > end { |
| 175 |
continue |
| 176 |
} |
| 177 |
// Don't recompute freed pages if ranges haven't updated. |
| 178 |
if txp.lastReleaseBegin == begin { |
| 179 |
continue |
| 180 |
} |
| 181 |
for i := 0; i < len(txp.ids); i++ { |
| 182 |
if atx := txp.alloctx[i]; atx < begin || atx > end { |
| 183 |
continue |
| 184 |
} |
| 185 |
m = append(m, txp.ids[i]) |
| 186 |
txp.ids[i] = txp.ids[len(txp.ids)-1] |
| 187 |
txp.ids = txp.ids[:len(txp.ids)-1] |
| 188 |
txp.alloctx[i] = txp.alloctx[len(txp.alloctx)-1] |
| 189 |
txp.alloctx = txp.alloctx[:len(txp.alloctx)-1] |
| 190 |
i-- |
| 191 |
} |
| 192 |
txp.lastReleaseBegin = begin |
| 193 |
if len(txp.ids) == 0 { |
| 194 |
delete(f.pending, tid) |
| 195 |
} |
| 196 |
} |
| 197 |
sort.Sort(m) |
| 198 |
f.ids = pgids(f.ids).merge(m) |
| 199 |
} |
| 200 |
|
| 201 |
// rollback removes the pages from a given pending tx. |
| 202 |
func (f *freelist) rollback(txid txid) { |
| 203 |
// Remove page ids from cache. |
| 204 |
txp := f.pending[txid] |
| 205 |
if txp == nil { |
| 206 |
return |
| 207 |
} |
| 208 |
var m pgids |
| 209 |
for i, pgid := range txp.ids { |
| 210 |
delete(f.cache, pgid) |
| 211 |
tx := txp.alloctx[i] |
| 212 |
if tx == 0 { |
| 213 |
continue |
| 214 |
} |
| 215 |
if tx != txid { |
| 216 |
// Pending free aborted; restore page back to alloc list. |
| 217 |
f.allocs[pgid] = tx |
| 218 |
} else { |
| 219 |
// Freed page was allocated by this txn; OK to throw away. |
| 220 |
m = append(m, pgid) |
| 221 |
} |
| 222 |
} |
| 223 |
// Remove pages from pending list and mark as free if allocated by txid. |
| 224 |
delete(f.pending, txid) |
| 225 |
sort.Sort(m) |
| 226 |
f.ids = pgids(f.ids).merge(m) |
| 227 |
} |
| 228 |
|
| 229 |
// freed returns whether a given page is in the free list. |
| 230 |
func (f *freelist) freed(pgid pgid) bool { |
| 231 |
return f.cache[pgid] |
| 232 |
} |
| 233 |
|
| 234 |
// read initializes the freelist from a freelist page. |
| 235 |
func (f *freelist) read(p *page) { |
| 236 |
if (p.flags & freelistPageFlag) == 0 { |
| 237 |
panic(fmt.Sprintf("invalid freelist page: %d, page type is %s", p.id, p.typ())) |
| 238 |
} |
| 239 |
// If the page.count is at the max uint16 value (64k) then it's considered |
| 240 |
// an overflow and the size of the freelist is stored as the first element. |
| 241 |
idx, count := 0, int(p.count) |
| 242 |
if count == 0xFFFF { |
| 243 |
idx = 1 |
| 244 |
count = int(((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[0]) |
| 245 |
} |
| 246 |
|
| 247 |
// Copy the list of page ids from the freelist. |
| 248 |
if count == 0 { |
| 249 |
f.ids = nil |
| 250 |
} else { |
| 251 |
ids := ((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[idx : idx+count] |
| 252 |
f.ids = make([]pgid, len(ids)) |
| 253 |
copy(f.ids, ids) |
| 254 |
|
| 255 |
// Make sure they're sorted. |
| 256 |
sort.Sort(pgids(f.ids)) |
| 257 |
} |
| 258 |
|
| 259 |
// Rebuild the page cache. |
| 260 |
f.reindex() |
| 261 |
} |
| 262 |
|
| 263 |
// read initializes the freelist from a given list of ids. |
| 264 |
func (f *freelist) readIDs(ids []pgid) { |
| 265 |
f.ids = ids |
| 266 |
f.reindex() |
| 267 |
} |
| 268 |
|
| 269 |
// write writes the page ids onto a freelist page. All free and pending ids are |
| 270 |
// saved to disk since in the event of a program crash, all pending ids will |
| 271 |
// become free. |
| 272 |
func (f *freelist) write(p *page) error { |
| 273 |
// Combine the old free pgids and pgids waiting on an open transaction. |
| 274 |
|
| 275 |
// Update the header flag. |
| 276 |
p.flags |= freelistPageFlag |
| 277 |
|
| 278 |
// The page.count can only hold up to 64k elements so if we overflow that |
| 279 |
// number then we handle it by putting the size in the first element. |
| 280 |
lenids := f.count() |
| 281 |
if lenids == 0 { |
| 282 |
p.count = uint16(lenids) |
| 283 |
} else if lenids < 0xFFFF { |
| 284 |
p.count = uint16(lenids) |
| 285 |
f.copyall(((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[:]) |
| 286 |
} else { |
| 287 |
p.count = 0xFFFF |
| 288 |
((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[0] = pgid(lenids) |
| 289 |
f.copyall(((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[1:]) |
| 290 |
} |
| 291 |
|
| 292 |
return nil |
| 293 |
} |
| 294 |
|
| 295 |
// reload reads the freelist from a page and filters out pending items. |
| 296 |
func (f *freelist) reload(p *page) { |
| 297 |
f.read(p) |
| 298 |
|
| 299 |
// Build a cache of only pending pages. |
| 300 |
pcache := make(map[pgid]bool) |
| 301 |
for _, txp := range f.pending { |
| 302 |
for _, pendingID := range txp.ids { |
| 303 |
pcache[pendingID] = true |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
// Check each page in the freelist and build a new available freelist |
| 308 |
// with any pages not in the pending lists. |
| 309 |
var a []pgid |
| 310 |
for _, id := range f.ids { |
| 311 |
if !pcache[id] { |
| 312 |
a = append(a, id) |
| 313 |
} |
| 314 |
} |
| 315 |
f.ids = a |
| 316 |
|
| 317 |
// Once the available list is rebuilt then rebuild the free cache so that |
| 318 |
// it includes the available and pending free pages. |
| 319 |
f.reindex() |
| 320 |
} |
| 321 |
|
| 322 |
// reindex rebuilds the free cache based on available and pending free lists. |
| 323 |
func (f *freelist) reindex() { |
| 324 |
f.cache = make(map[pgid]bool, len(f.ids)) |
| 325 |
for _, id := range f.ids { |
| 326 |
f.cache[id] = true |
| 327 |
} |
| 328 |
for _, txp := range f.pending { |
| 329 |
for _, pendingID := range txp.ids { |
| 330 |
f.cache[pendingID] = true |
| 331 |
} |
| 332 |
} |
| 333 |
} |