blob: 1b7ba91b2a51b88440f869608c8da7d512dd7317 [file] [log] [blame]
khenaidooffe076b2019-01-15 16:08:08 -05001package bolt
2
3import (
4 "fmt"
5 "sort"
6 "unsafe"
7)
8
9// freelist represents a list of all pages that are available for allocation.
10// It also tracks pages that have been freed but are still in use by open transactions.
11type freelist struct {
12 ids []pgid // all free and available free page ids.
13 pending map[txid][]pgid // mapping of soon-to-be free page ids by tx.
14 cache map[pgid]bool // fast lookup of all free and pending page ids.
15}
16
17// newFreelist returns an empty, initialized freelist.
18func newFreelist() *freelist {
19 return &freelist{
20 pending: make(map[txid][]pgid),
21 cache: make(map[pgid]bool),
22 }
23}
24
25// size returns the size of the page after serialization.
26func (f *freelist) size() int {
27 return pageHeaderSize + (int(unsafe.Sizeof(pgid(0))) * f.count())
28}
29
30// count returns count of pages on the freelist
31func (f *freelist) count() int {
32 return f.free_count() + f.pending_count()
33}
34
35// free_count returns count of free pages
36func (f *freelist) free_count() int {
37 return len(f.ids)
38}
39
40// pending_count returns count of pending pages
41func (f *freelist) pending_count() int {
42 var count int
43 for _, list := range f.pending {
44 count += len(list)
45 }
46 return count
47}
48
49// all returns a list of all free ids and all pending ids in one sorted list.
50func (f *freelist) all() []pgid {
51 m := make(pgids, 0)
52
53 for _, list := range f.pending {
54 m = append(m, list...)
55 }
56
57 sort.Sort(m)
58 return pgids(f.ids).merge(m)
59}
60
61// allocate returns the starting page id of a contiguous list of pages of a given size.
62// If a contiguous block cannot be found then 0 is returned.
63func (f *freelist) allocate(n int) pgid {
64 if len(f.ids) == 0 {
65 return 0
66 }
67
68 var initial, previd pgid
69 for i, id := range f.ids {
70 if id <= 1 {
71 panic(fmt.Sprintf("invalid page allocation: %d", id))
72 }
73
74 // Reset initial page if this is not contiguous.
75 if previd == 0 || id-previd != 1 {
76 initial = id
77 }
78
79 // If we found a contiguous block then remove it and return it.
80 if (id-initial)+1 == pgid(n) {
81 // If we're allocating off the beginning then take the fast path
82 // and just adjust the existing slice. This will use extra memory
83 // temporarily but the append() in free() will realloc the slice
84 // as is necessary.
85 if (i + 1) == n {
86 f.ids = f.ids[i+1:]
87 } else {
88 copy(f.ids[i-n+1:], f.ids[i+1:])
89 f.ids = f.ids[:len(f.ids)-n]
90 }
91
92 // Remove from the free cache.
93 for i := pgid(0); i < pgid(n); i++ {
94 delete(f.cache, initial+i)
95 }
96
97 return initial
98 }
99
100 previd = id
101 }
102 return 0
103}
104
105// free releases a page and its overflow for a given transaction id.
106// If the page is already free then a panic will occur.
107func (f *freelist) free(txid txid, p *page) {
108 if p.id <= 1 {
109 panic(fmt.Sprintf("cannot free page 0 or 1: %d", p.id))
110 }
111
112 // Free page and all its overflow pages.
113 var ids = f.pending[txid]
114 for id := p.id; id <= p.id+pgid(p.overflow); id++ {
115 // Verify that page is not already free.
116 if f.cache[id] {
117 panic(fmt.Sprintf("page %d already freed", id))
118 }
119
120 // Add to the freelist and cache.
121 ids = append(ids, id)
122 f.cache[id] = true
123 }
124 f.pending[txid] = ids
125}
126
127// release moves all page ids for a transaction id (or older) to the freelist.
128func (f *freelist) release(txid txid) {
129 m := make(pgids, 0)
130 for tid, ids := range f.pending {
131 if tid <= txid {
132 // Move transaction's pending pages to the available freelist.
133 // Don't remove from the cache since the page is still free.
134 m = append(m, ids...)
135 delete(f.pending, tid)
136 }
137 }
138 sort.Sort(m)
139 f.ids = pgids(f.ids).merge(m)
140}
141
142// rollback removes the pages from a given pending tx.
143func (f *freelist) rollback(txid txid) {
144 // Remove page ids from cache.
145 for _, id := range f.pending[txid] {
146 delete(f.cache, id)
147 }
148
149 // Remove pages from pending list.
150 delete(f.pending, txid)
151}
152
153// freed returns whether a given page is in the free list.
154func (f *freelist) freed(pgid pgid) bool {
155 return f.cache[pgid]
156}
157
158// read initializes the freelist from a freelist page.
159func (f *freelist) read(p *page) {
160 // If the page.count is at the max uint16 value (64k) then it's considered
161 // an overflow and the size of the freelist is stored as the first element.
162 idx, count := 0, int(p.count)
163 if count == 0xFFFF {
164 idx = 1
165 count = int(((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[0])
166 }
167
168 // Copy the list of page ids from the freelist.
169 if count == 0 {
170 f.ids = nil
171 } else {
172 ids := ((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[idx:count]
173 f.ids = make([]pgid, len(ids))
174 copy(f.ids, ids)
175
176 // Make sure they're sorted.
177 sort.Sort(pgids(f.ids))
178 }
179
180 // Rebuild the page cache.
181 f.reindex()
182}
183
184// write writes the page ids onto a freelist page. All free and pending ids are
185// saved to disk since in the event of a program crash, all pending ids will
186// become free.
187func (f *freelist) write(p *page) error {
188 // Combine the old free pgids and pgids waiting on an open transaction.
189 ids := f.all()
190
191 // Update the header flag.
192 p.flags |= freelistPageFlag
193
194 // The page.count can only hold up to 64k elements so if we overflow that
195 // number then we handle it by putting the size in the first element.
196 if len(ids) == 0 {
197 p.count = uint16(len(ids))
198 } else if len(ids) < 0xFFFF {
199 p.count = uint16(len(ids))
200 copy(((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[:], ids)
201 } else {
202 p.count = 0xFFFF
203 ((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[0] = pgid(len(ids))
204 copy(((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[1:], ids)
205 }
206
207 return nil
208}
209
210// reload reads the freelist from a page and filters out pending items.
211func (f *freelist) reload(p *page) {
212 f.read(p)
213
214 // Build a cache of only pending pages.
215 pcache := make(map[pgid]bool)
216 for _, pendingIDs := range f.pending {
217 for _, pendingID := range pendingIDs {
218 pcache[pendingID] = true
219 }
220 }
221
222 // Check each page in the freelist and build a new available freelist
223 // with any pages not in the pending lists.
224 var a []pgid
225 for _, id := range f.ids {
226 if !pcache[id] {
227 a = append(a, id)
228 }
229 }
230 f.ids = a
231
232 // Once the available list is rebuilt then rebuild the free cache so that
233 // it includes the available and pending free pages.
234 f.reindex()
235}
236
237// reindex rebuilds the free cache based on available and pending free lists.
238func (f *freelist) reindex() {
239 f.cache = make(map[pgid]bool)
240 for _, id := range f.ids {
241 f.cache[id] = true
242 }
243 for _, pendingIDs := range f.pending {
244 for _, pendingID := range pendingIDs {
245 f.cache[pendingID] = true
246 }
247 }
248}