freelist.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. if count == 0 {
  144. f.ids = nil
  145. } else {
  146. ids := ((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[idx:count]
  147. f.ids = make([]pgid, len(ids))
  148. copy(f.ids, ids)
  149. // Make sure they're sorted.
  150. sort.Sort(pgids(f.ids))
  151. }
  152. // Rebuild the page cache.
  153. f.reindex()
  154. }
  155. // write writes the page ids onto a freelist page. All free and pending ids are
  156. // saved to disk since in the event of a program crash, all pending ids will
  157. // become free.
  158. func (f *freelist) write(p *page) error {
  159. // Combine the old free pgids and pgids waiting on an open transaction.
  160. ids := f.all()
  161. // Update the header flag.
  162. p.flags |= freelistPageFlag
  163. // The page.count can only hold up to 64k elements so if we overflow that
  164. // number then we handle it by putting the size in the first element.
  165. if len(ids) == 0 {
  166. p.count = uint16(len(ids))
  167. } else if len(ids) < 0xFFFF {
  168. p.count = uint16(len(ids))
  169. copy(((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[:], ids)
  170. } else {
  171. p.count = 0xFFFF
  172. ((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[0] = pgid(len(ids))
  173. copy(((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[1:], ids)
  174. }
  175. return nil
  176. }
  177. // reload reads the freelist from a page and filters out pending items.
  178. func (f *freelist) reload(p *page) {
  179. f.read(p)
  180. // Build a cache of only pending pages.
  181. pcache := make(map[pgid]bool)
  182. for _, pendingIDs := range f.pending {
  183. for _, pendingID := range pendingIDs {
  184. pcache[pendingID] = true
  185. }
  186. }
  187. // Check each page in the freelist and build a new available freelist
  188. // with any pages not in the pending lists.
  189. var a []pgid
  190. for _, id := range f.ids {
  191. if !pcache[id] {
  192. a = append(a, id)
  193. }
  194. }
  195. f.ids = a
  196. // Once the available list is rebuilt then rebuild the free cache so that
  197. // it includes the available and pending free pages.
  198. f.reindex()
  199. }
  200. // reindex rebuilds the free cache based on available and pending free lists.
  201. func (f *freelist) reindex() {
  202. f.cache = make(map[pgid]bool)
  203. for _, id := range f.ids {
  204. f.cache[id] = true
  205. }
  206. for _, pendingIDs := range f.pending {
  207. for _, pendingID := range pendingIDs {
  208. f.cache[pendingID] = true
  209. }
  210. }
  211. }