You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

page.go 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package bbolt
  2. import (
  3. "fmt"
  4. "os"
  5. "reflect"
  6. "sort"
  7. "unsafe"
  8. )
  9. const pageHeaderSize = unsafe.Sizeof(page{})
  10. const minKeysPerPage = 2
  11. const branchPageElementSize = unsafe.Sizeof(branchPageElement{})
  12. const leafPageElementSize = unsafe.Sizeof(leafPageElement{})
  13. const (
  14. branchPageFlag = 0x01
  15. leafPageFlag = 0x02
  16. metaPageFlag = 0x04
  17. freelistPageFlag = 0x10
  18. )
  19. const (
  20. bucketLeafFlag = 0x01
  21. )
  22. type pgid uint64
  23. type page struct {
  24. id pgid
  25. flags uint16
  26. count uint16
  27. overflow uint32
  28. }
  29. // typ returns a human readable page type string used for debugging.
  30. func (p *page) typ() string {
  31. if (p.flags & branchPageFlag) != 0 {
  32. return "branch"
  33. } else if (p.flags & leafPageFlag) != 0 {
  34. return "leaf"
  35. } else if (p.flags & metaPageFlag) != 0 {
  36. return "meta"
  37. } else if (p.flags & freelistPageFlag) != 0 {
  38. return "freelist"
  39. }
  40. return fmt.Sprintf("unknown<%02x>", p.flags)
  41. }
  42. // meta returns a pointer to the metadata section of the page.
  43. func (p *page) meta() *meta {
  44. return (*meta)(unsafe.Pointer(uintptr(unsafe.Pointer(p)) + unsafe.Sizeof(*p)))
  45. }
  46. // leafPageElement retrieves the leaf node by index
  47. func (p *page) leafPageElement(index uint16) *leafPageElement {
  48. off := uintptr(index) * unsafe.Sizeof(leafPageElement{})
  49. return (*leafPageElement)(unsafe.Pointer(uintptr(unsafe.Pointer(p)) + unsafe.Sizeof(*p) + off))
  50. }
  51. // leafPageElements retrieves a list of leaf nodes.
  52. func (p *page) leafPageElements() []leafPageElement {
  53. if p.count == 0 {
  54. return nil
  55. }
  56. return *(*[]leafPageElement)(unsafe.Pointer(&reflect.SliceHeader{
  57. Data: uintptr(unsafe.Pointer(p)) + unsafe.Sizeof(*p),
  58. Len: int(p.count),
  59. Cap: int(p.count),
  60. }))
  61. }
  62. // branchPageElement retrieves the branch node by index
  63. func (p *page) branchPageElement(index uint16) *branchPageElement {
  64. off := uintptr(index) * unsafe.Sizeof(branchPageElement{})
  65. return (*branchPageElement)(unsafe.Pointer(uintptr(unsafe.Pointer(p)) + unsafe.Sizeof(*p) + off))
  66. }
  67. // branchPageElements retrieves a list of branch nodes.
  68. func (p *page) branchPageElements() []branchPageElement {
  69. if p.count == 0 {
  70. return nil
  71. }
  72. return *(*[]branchPageElement)(unsafe.Pointer(&reflect.SliceHeader{
  73. Data: uintptr(unsafe.Pointer(p)) + unsafe.Sizeof(*p),
  74. Len: int(p.count),
  75. Cap: int(p.count),
  76. }))
  77. }
  78. // dump writes n bytes of the page to STDERR as hex output.
  79. func (p *page) hexdump(n int) {
  80. buf := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
  81. Data: uintptr(unsafe.Pointer(p)),
  82. Len: n,
  83. Cap: n,
  84. }))
  85. fmt.Fprintf(os.Stderr, "%x\n", buf)
  86. }
  87. type pages []*page
  88. func (s pages) Len() int { return len(s) }
  89. func (s pages) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  90. func (s pages) Less(i, j int) bool { return s[i].id < s[j].id }
  91. // branchPageElement represents a node on a branch page.
  92. type branchPageElement struct {
  93. pos uint32
  94. ksize uint32
  95. pgid pgid
  96. }
  97. // key returns a byte slice of the node key.
  98. func (n *branchPageElement) key() []byte {
  99. return *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
  100. Data: uintptr(unsafe.Pointer(n)) + uintptr(n.pos),
  101. Len: int(n.ksize),
  102. Cap: int(n.ksize),
  103. }))
  104. }
  105. // leafPageElement represents a node on a leaf page.
  106. type leafPageElement struct {
  107. flags uint32
  108. pos uint32
  109. ksize uint32
  110. vsize uint32
  111. }
  112. // key returns a byte slice of the node key.
  113. func (n *leafPageElement) key() []byte {
  114. return *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
  115. Data: uintptr(unsafe.Pointer(n)) + uintptr(n.pos),
  116. Len: int(n.ksize),
  117. Cap: int(n.ksize),
  118. }))
  119. }
  120. // value returns a byte slice of the node value.
  121. func (n *leafPageElement) value() []byte {
  122. return *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
  123. Data: uintptr(unsafe.Pointer(n)) + uintptr(n.pos) + uintptr(n.ksize),
  124. Len: int(n.vsize),
  125. Cap: int(n.vsize),
  126. }))
  127. }
  128. // PageInfo represents human readable information about a page.
  129. type PageInfo struct {
  130. ID int
  131. Type string
  132. Count int
  133. OverflowCount int
  134. }
  135. type pgids []pgid
  136. func (s pgids) Len() int { return len(s) }
  137. func (s pgids) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  138. func (s pgids) Less(i, j int) bool { return s[i] < s[j] }
  139. // merge returns the sorted union of a and b.
  140. func (a pgids) merge(b pgids) pgids {
  141. // Return the opposite slice if one is nil.
  142. if len(a) == 0 {
  143. return b
  144. }
  145. if len(b) == 0 {
  146. return a
  147. }
  148. merged := make(pgids, len(a)+len(b))
  149. mergepgids(merged, a, b)
  150. return merged
  151. }
  152. // mergepgids copies the sorted union of a and b into dst.
  153. // If dst is too small, it panics.
  154. func mergepgids(dst, a, b pgids) {
  155. if len(dst) < len(a)+len(b) {
  156. panic(fmt.Errorf("mergepgids bad len %d < %d + %d", len(dst), len(a), len(b)))
  157. }
  158. // Copy in the opposite slice if one is nil.
  159. if len(a) == 0 {
  160. copy(dst, b)
  161. return
  162. }
  163. if len(b) == 0 {
  164. copy(dst, a)
  165. return
  166. }
  167. // Merged will hold all elements from both lists.
  168. merged := dst[:0]
  169. // Assign lead to the slice with a lower starting value, follow to the higher value.
  170. lead, follow := a, b
  171. if b[0] < a[0] {
  172. lead, follow = b, a
  173. }
  174. // Continue while there are elements in the lead.
  175. for len(lead) > 0 {
  176. // Merge largest prefix of lead that is ahead of follow[0].
  177. n := sort.Search(len(lead), func(i int) bool { return lead[i] > follow[0] })
  178. merged = append(merged, lead[:n]...)
  179. if n >= len(lead) {
  180. break
  181. }
  182. // Swap lead and follow.
  183. lead, follow = follow, lead[n:]
  184. }
  185. // Append what's left in follow.
  186. _ = append(merged, follow...)
  187. }