dirty_page_interval.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package filesys
  2. import (
  3. "io"
  4. "github.com/chrislusf/seaweedfs/weed/util"
  5. )
  6. type IntervalNode struct {
  7. Data []byte
  8. Offset int64
  9. Size int64
  10. Next *IntervalNode
  11. }
  12. type IntervalLinkedList struct {
  13. Head *IntervalNode
  14. Tail *IntervalNode
  15. }
  16. type ContinuousIntervals struct {
  17. lists []*IntervalLinkedList
  18. }
  19. func (list *IntervalLinkedList) Offset() int64 {
  20. return list.Head.Offset
  21. }
  22. func (list *IntervalLinkedList) Size() int64 {
  23. return list.Tail.Offset + list.Tail.Size - list.Head.Offset
  24. }
  25. func (list *IntervalLinkedList) addNodeToTail(node *IntervalNode) {
  26. // glog.V(4).Infof("add to tail [%d,%d) + [%d,%d) => [%d,%d)", list.Head.Offset, list.Tail.Offset+list.Tail.Size, node.Offset, node.Offset+node.Size, list.Head.Offset, node.Offset+node.Size)
  27. list.Tail.Next = node
  28. list.Tail = node
  29. }
  30. func (list *IntervalLinkedList) addNodeToHead(node *IntervalNode) {
  31. // glog.V(4).Infof("add to head [%d,%d) + [%d,%d) => [%d,%d)", node.Offset, node.Offset+node.Size, list.Head.Offset, list.Tail.Offset+list.Tail.Size, node.Offset, list.Tail.Offset+list.Tail.Size)
  32. node.Next = list.Head
  33. list.Head = node
  34. }
  35. func (list *IntervalLinkedList) ReadData(buf []byte, start, stop int64) {
  36. t := list.Head
  37. for {
  38. nodeStart, nodeStop := max(start, t.Offset), min(stop, t.Offset+t.Size)
  39. if nodeStart < nodeStop {
  40. // glog.V(0).Infof("copying start=%d stop=%d t=[%d,%d) t.data=%d => bufSize=%d nodeStart=%d, nodeStop=%d", start, stop, t.Offset, t.Offset+t.Size, len(t.Data), len(buf), nodeStart, nodeStop)
  41. copy(buf[nodeStart-start:], t.Data[nodeStart-t.Offset:nodeStop-t.Offset])
  42. }
  43. if t.Next == nil {
  44. break
  45. }
  46. t = t.Next
  47. }
  48. }
  49. func (c *ContinuousIntervals) TotalSize() (total int64) {
  50. for _, list := range c.lists {
  51. total += list.Size()
  52. }
  53. return
  54. }
  55. func subList(list *IntervalLinkedList, start, stop int64) *IntervalLinkedList {
  56. var nodes []*IntervalNode
  57. for t := list.Head; t != nil; t = t.Next {
  58. nodeStart, nodeStop := max(start, t.Offset), min(stop, t.Offset+t.Size)
  59. if nodeStart >= nodeStop {
  60. // skip non overlapping IntervalNode
  61. continue
  62. }
  63. nodes = append(nodes, &IntervalNode{
  64. Data: t.Data[nodeStart-t.Offset : nodeStop-t.Offset],
  65. Offset: nodeStart,
  66. Size: nodeStop - nodeStart,
  67. Next: nil,
  68. })
  69. }
  70. for i := 1; i < len(nodes); i++ {
  71. nodes[i-1].Next = nodes[i]
  72. }
  73. return &IntervalLinkedList{
  74. Head: nodes[0],
  75. Tail: nodes[len(nodes)-1],
  76. }
  77. }
  78. func (c *ContinuousIntervals) AddInterval(data []byte, offset int64) {
  79. interval := &IntervalNode{Data: data, Offset: offset, Size: int64(len(data))}
  80. // append to the tail and return
  81. if len(c.lists) == 1 {
  82. lastSpan := c.lists[0]
  83. if lastSpan.Tail.Offset+lastSpan.Tail.Size == offset {
  84. lastSpan.addNodeToTail(interval)
  85. return
  86. }
  87. }
  88. var newLists []*IntervalLinkedList
  89. for _, list := range c.lists {
  90. // if list is to the left of new interval, add to the new list
  91. if list.Tail.Offset+list.Tail.Size <= interval.Offset {
  92. newLists = append(newLists, list)
  93. }
  94. // if list is to the right of new interval, add to the new list
  95. if interval.Offset+interval.Size <= list.Head.Offset {
  96. newLists = append(newLists, list)
  97. }
  98. // if new interval overwrite the right part of the list
  99. if list.Head.Offset < interval.Offset && interval.Offset < list.Tail.Offset+list.Tail.Size {
  100. // create a new list of the left part of existing list
  101. newLists = append(newLists, subList(list, list.Offset(), interval.Offset))
  102. }
  103. // if new interval overwrite the left part of the list
  104. if list.Head.Offset < interval.Offset+interval.Size && interval.Offset+interval.Size < list.Tail.Offset+list.Tail.Size {
  105. // create a new list of the right part of existing list
  106. newLists = append(newLists, subList(list, interval.Offset+interval.Size, list.Tail.Offset+list.Tail.Size))
  107. }
  108. // skip anything that is fully overwritten by the new interval
  109. }
  110. c.lists = newLists
  111. // add the new interval to the lists, connecting neighbor lists
  112. var prevList, nextList *IntervalLinkedList
  113. for _, list := range c.lists {
  114. if list.Head.Offset == interval.Offset+interval.Size {
  115. nextList = list
  116. break
  117. }
  118. }
  119. for _, list := range c.lists {
  120. if list.Head.Offset+list.Size() == offset {
  121. list.addNodeToTail(interval)
  122. prevList = list
  123. break
  124. }
  125. }
  126. if prevList != nil && nextList != nil {
  127. // glog.V(4).Infof("connecting [%d,%d) + [%d,%d) => [%d,%d)", prevList.Head.Offset, prevList.Tail.Offset+prevList.Tail.Size, nextList.Head.Offset, nextList.Tail.Offset+nextList.Tail.Size, prevList.Head.Offset, nextList.Tail.Offset+nextList.Tail.Size)
  128. prevList.Tail.Next = nextList.Head
  129. prevList.Tail = nextList.Tail
  130. c.removeList(nextList)
  131. } else if nextList != nil {
  132. // add to head was not done when checking
  133. nextList.addNodeToHead(interval)
  134. }
  135. if prevList == nil && nextList == nil {
  136. c.lists = append(c.lists, &IntervalLinkedList{
  137. Head: interval,
  138. Tail: interval,
  139. })
  140. }
  141. return
  142. }
  143. func (c *ContinuousIntervals) RemoveLargestIntervalLinkedList() *IntervalLinkedList {
  144. var maxSize int64
  145. maxIndex := -1
  146. for k, list := range c.lists {
  147. if maxSize <= list.Size() {
  148. maxSize = list.Size()
  149. maxIndex = k
  150. }
  151. }
  152. if maxSize <= 0 {
  153. return nil
  154. }
  155. t := c.lists[maxIndex]
  156. c.lists = append(c.lists[0:maxIndex], c.lists[maxIndex+1:]...)
  157. return t
  158. }
  159. func (c *ContinuousIntervals) removeList(target *IntervalLinkedList) {
  160. index := -1
  161. for k, list := range c.lists {
  162. if list.Offset() == target.Offset() {
  163. index = k
  164. }
  165. }
  166. if index < 0 {
  167. return
  168. }
  169. c.lists = append(c.lists[0:index], c.lists[index+1:]...)
  170. }
  171. func (c *ContinuousIntervals) ReadDataAt(data []byte, startOffset int64) (maxStop int64) {
  172. for _, list := range c.lists {
  173. start := max(startOffset, list.Offset())
  174. stop := min(startOffset+int64(len(data)), list.Offset()+list.Size())
  175. if start < stop {
  176. list.ReadData(data[start-startOffset:], start, stop)
  177. maxStop = max(maxStop, stop)
  178. }
  179. }
  180. return
  181. }
  182. func (l *IntervalLinkedList) ToReader() io.Reader {
  183. var readers []io.Reader
  184. t := l.Head
  185. readers = append(readers, util.NewBytesReader(t.Data))
  186. for t.Next != nil {
  187. t = t.Next
  188. readers = append(readers, util.NewBytesReader(t.Data))
  189. }
  190. if len(readers) == 1 {
  191. return readers[0]
  192. }
  193. return io.MultiReader(readers...)
  194. }