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.

223 lines
6.1 KiB

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