bolt/freelist.go

182 lines
4.4 KiB
Go
Raw Normal View History

2014-02-09 22:52:19 +00:00
package bolt
import (
2014-06-13 13:56:10 +00:00
"errors"
"fmt"
2014-02-09 22:52:19 +00:00
"sort"
"unsafe"
)
2014-06-13 13:56:10 +00:00
var (
// ErrFreelistOverflow is returned when the total number of free pages
// exceeds 65,536 and the freelist cannot hold any more.
ErrFreelistOverflow = errors.New("freelist overflow")
)
2014-02-09 22:52:19 +00:00
// freelist represents a list of all pages that are available for allocation.
// It also tracks pages that have been freed but are still in use by open transactions.
type freelist struct {
ids []pgid
pending map[txid][]pgid
2014-02-09 22:52:19 +00:00
}
// size returns the size of the page after serialization.
func (f *freelist) size() int {
return pageHeaderSize + (int(unsafe.Sizeof(pgid(0))) * len(f.all()))
}
2014-02-09 22:52:19 +00:00
// all returns a list of all free ids and all pending ids in one sorted list.
func (f *freelist) all() []pgid {
ids := make([]pgid, len(f.ids))
copy(ids, f.ids)
for _, list := range f.pending {
ids = append(ids, list...)
}
sort.Sort(pgids(ids))
2014-02-09 22:52:19 +00:00
return ids
}
// allocate returns the starting page id of a contiguous list of pages of a given size.
// If a contiguous block cannot be found then 0 is returned.
func (f *freelist) allocate(n int) pgid {
if len(f.ids) == 0 {
return 0
}
var initial, previd pgid
2014-02-09 22:52:19 +00:00
for i, id := range f.ids {
_assert(id > 1, "invalid page allocation: %d", id)
// Reset initial page if this is not contiguous.
if previd == 0 || id-previd != 1 {
initial = id
2014-02-09 22:52:19 +00:00
}
// If we found a contiguous block then remove it and return it.
if (id-initial)+1 == pgid(n) {
// If we're allocating off the beginning then take the fast path
// and just adjust the existing slice. This will use extra memory
// temporarily but the append() in free() will realloc the slice
// as is necessary.
if (i + 1) == n {
f.ids = f.ids[i+1:]
} else {
2014-05-19 20:11:32 +00:00
copy(f.ids[i-n+1:], f.ids[i+1:])
f.ids = f.ids[:len(f.ids)-n]
}
return initial
2014-02-09 22:52:19 +00:00
}
previd = id
}
return 0
}
// free releases a page and its overflow for a given transaction id.
// If the page is already free then a panic will occur.
func (f *freelist) free(txid txid, p *page) {
2014-02-09 22:52:19 +00:00
_assert(p.id > 1, "cannot free page 0 or 1: %d", p.id)
// Verify that page is not already free.
minid, maxid := p.id, p.id+pgid(p.overflow)
for _, id := range f.ids {
if id >= minid && id <= maxid {
panic(fmt.Sprintf("page %d already freed in tx", id))
}
}
for ptxid, m := range f.pending {
for _, id := range m {
if id >= minid && id <= maxid {
panic(fmt.Sprintf("tx %d: page %d already freed in tx %d", txid, id, ptxid))
}
}
}
// Free page and all its overflow pages.
var ids = f.pending[txid]
2014-02-09 22:52:19 +00:00
for i := 0; i < int(p.overflow+1); i++ {
ids = append(ids, p.id+pgid(i))
}
f.pending[txid] = ids
2014-02-09 22:52:19 +00:00
}
// release moves all page ids for a transaction id (or older) to the freelist.
func (f *freelist) release(txid txid) {
2014-02-09 22:52:19 +00:00
for tid, ids := range f.pending {
if tid <= txid {
2014-02-09 22:52:19 +00:00
f.ids = append(f.ids, ids...)
delete(f.pending, tid)
}
}
sort.Sort(pgids(f.ids))
2014-02-09 22:52:19 +00:00
}
2014-06-13 21:50:47 +00:00
// rollback removes the pages from a given pending tx.
func (f *freelist) rollback(txid txid) {
delete(f.pending, txid)
}
2014-03-22 04:34:54 +00:00
// isFree returns whether a given page is in the free list.
func (f *freelist) isFree(pgid pgid) bool {
for _, id := range f.ids {
if id == pgid {
return true
}
}
for _, m := range f.pending {
for _, id := range m {
if id == pgid {
return true
}
}
}
return false
}
2014-02-09 22:52:19 +00:00
// read initializes the freelist from a freelist page.
func (f *freelist) read(p *page) {
ids := ((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[0:p.count]
f.ids = make([]pgid, len(ids))
copy(f.ids, ids)
2014-05-19 20:11:32 +00:00
sort.Sort(pgids(f.ids))
2014-02-09 22:52:19 +00:00
}
// write writes the page ids onto a freelist page. All free and pending ids are
// saved to disk since in the event of a program crash, all pending ids will
// become free.
2014-06-13 13:56:10 +00:00
func (f *freelist) write(p *page) error {
// Combine the old free pgids and pgids waiting on an open transaction.
2014-02-09 22:52:19 +00:00
ids := f.all()
2014-06-13 13:56:10 +00:00
// Make sure that the sum of all free pages is less than the max uint16 size.
2014-06-13 21:50:47 +00:00
if len(ids) >= 65565 {
2014-06-13 13:56:10 +00:00
return ErrFreelistOverflow
}
// Update the header and write the ids to the page.
2014-02-12 21:57:27 +00:00
p.flags |= freelistPageFlag
2014-02-09 22:52:19 +00:00
p.count = uint16(len(ids))
copy(((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[:], ids)
2014-06-13 13:56:10 +00:00
return nil
2014-02-09 22:52:19 +00:00
}
2014-06-13 21:50:47 +00:00
// reload reads the freelist from a page and filters out pending items.
func (f *freelist) reload(p *page) {
f.read(p)
// Filter out pending free pages.
ids := f.ids
f.ids = nil
var a []pgid
for _, id := range ids {
if !f.isFree(id) {
a = append(a, id)
}
}
f.ids = a
}