freelist.go 6.2 KB

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