您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

253 行
6.5 KiB

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