| 1 |
package bolt |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"sort" |
| 6 |
"unsafe" |
| 7 |
) |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
type freelist struct { |
| 12 |
ids []pgid |
| 13 |
pending map[txid][]pgid |
| 14 |
cache map[pgid]bool |
| 15 |
} |
| 16 |
|
| 17 |
|
| 18 |
func newFreelist() *freelist { |
| 19 |
return &freelist{ |
| 20 |
pending: make(map[txid][]pgid), |
| 21 |
cache: make(map[pgid]bool), |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
func (f *freelist) size() int { |
| 27 |
n := f.count() |
| 28 |
if n >= 0xFFFF { |
| 29 |
|
| 30 |
n++ |
| 31 |
} |
| 32 |
return pageHeaderSize + (int(unsafe.Sizeof(pgid(0))) * n) |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
func (f *freelist) count() int { |
| 37 |
return f.free_count() + f.pending_count() |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
func (f *freelist) free_count() int { |
| 42 |
return len(f.ids) |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
func (f *freelist) pending_count() int { |
| 47 |
var count int |
| 48 |
for _, list := range f.pending { |
| 49 |
count += len(list) |
| 50 |
} |
| 51 |
return count |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
func (f *freelist) copyall(dst []pgid) { |
| 57 |
m := make(pgids, 0, f.pending_count()) |
| 58 |
for _, list := range f.pending { |
| 59 |
m = append(m, list...) |
| 60 |
} |
| 61 |
sort.Sort(m) |
| 62 |
mergepgids(dst, f.ids, m) |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
func (f *freelist) allocate(n int) pgid { |
| 68 |
if len(f.ids) == 0 { |
| 69 |
return 0 |
| 70 |
} |
| 71 |
|
| 72 |
var initial, previd pgid |
| 73 |
for i, id := range f.ids { |
| 74 |
if id <= 1 { |
| 75 |
panic(fmt.Sprintf("invalid page allocation: %d", id)) |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
if previd == 0 || id-previd != 1 { |
| 80 |
initial = id |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
if (id-initial)+1 == pgid(n) { |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
if (i + 1) == n { |
| 90 |
f.ids = f.ids[i+1:] |
| 91 |
} else { |
| 92 |
copy(f.ids[i-n+1:], f.ids[i+1:]) |
| 93 |
f.ids = f.ids[:len(f.ids)-n] |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
for i := pgid(0); i < pgid(n); i++ { |
| 98 |
delete(f.cache, initial+i) |
| 99 |
} |
| 100 |
|
| 101 |
return initial |
| 102 |
} |
| 103 |
|
| 104 |
previd = id |
| 105 |
} |
| 106 |
return 0 |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
func (f *freelist) free(txid txid, p *page) { |
| 112 |
if p.id <= 1 { |
| 113 |
panic(fmt.Sprintf("cannot free page 0 or 1: %d", p.id)) |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
var ids = f.pending[txid] |
| 118 |
for id := p.id; id <= p.id+pgid(p.overflow); id++ { |
| 119 |
|
| 120 |
if f.cache[id] { |
| 121 |
panic(fmt.Sprintf("page %d already freed", id)) |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
ids = append(ids, id) |
| 126 |
f.cache[id] = true |
| 127 |
} |
| 128 |
f.pending[txid] = ids |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
func (f *freelist) release(txid txid) { |
| 133 |
m := make(pgids, 0) |
| 134 |
for tid, ids := range f.pending { |
| 135 |
if tid <= txid { |
| 136 |
|
| 137 |
|
| 138 |
m = append(m, ids...) |
| 139 |
delete(f.pending, tid) |
| 140 |
} |
| 141 |
} |
| 142 |
sort.Sort(m) |
| 143 |
f.ids = pgids(f.ids).merge(m) |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
func (f *freelist) rollback(txid txid) { |
| 148 |
|
| 149 |
for _, id := range f.pending[txid] { |
| 150 |
delete(f.cache, id) |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
delete(f.pending, txid) |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
func (f *freelist) freed(pgid pgid) bool { |
| 159 |
return f.cache[pgid] |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
func (f *freelist) read(p *page) { |
| 164 |
|
| 165 |
|
| 166 |
idx, count := 0, int(p.count) |
| 167 |
if count == 0xFFFF { |
| 168 |
idx = 1 |
| 169 |
count = int(((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[0]) |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
if count == 0 { |
| 174 |
f.ids = nil |
| 175 |
} else { |
| 176 |
ids := ((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[idx:count] |
| 177 |
f.ids = make([]pgid, len(ids)) |
| 178 |
copy(f.ids, ids) |
| 179 |
|
| 180 |
|
| 181 |
sort.Sort(pgids(f.ids)) |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
f.reindex() |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
func (f *freelist) write(p *page) error { |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
p.flags |= freelistPageFlag |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
lenids := f.count() |
| 200 |
if lenids == 0 { |
| 201 |
p.count = uint16(lenids) |
| 202 |
} else if lenids < 0xFFFF { |
| 203 |
p.count = uint16(lenids) |
| 204 |
f.copyall(((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[:]) |
| 205 |
} else { |
| 206 |
p.count = 0xFFFF |
| 207 |
((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[0] = pgid(lenids) |
| 208 |
f.copyall(((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[1:]) |
| 209 |
} |
| 210 |
|
| 211 |
return nil |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
func (f *freelist) reload(p *page) { |
| 216 |
f.read(p) |
| 217 |
|
| 218 |
|
| 219 |
pcache := make(map[pgid]bool) |
| 220 |
for _, pendingIDs := range f.pending { |
| 221 |
for _, pendingID := range pendingIDs { |
| 222 |
pcache[pendingID] = true |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
var a []pgid |
| 229 |
for _, id := range f.ids { |
| 230 |
if !pcache[id] { |
| 231 |
a = append(a, id) |
| 232 |
} |
| 233 |
} |
| 234 |
f.ids = a |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
f.reindex() |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
func (f *freelist) reindex() { |
| 243 |
f.cache = make(map[pgid]bool, len(f.ids)) |
| 244 |
for _, id := range f.ids { |
| 245 |
f.cache[id] = true |
| 246 |
} |
| 247 |
for _, pendingIDs := range f.pending { |
| 248 |
for _, pendingID := range pendingIDs { |
| 249 |
f.cache[pendingID] = true |
| 250 |
} |
| 251 |
} |
| 252 |
} |