| 70 | } |
| 71 | |
| 72 | func NewARTreeIterator(tree art.Tree, reverse bool) *ARTreeIterator { |
| 73 | // Estimate the expected slice capacity based on tree size |
| 74 | expectedSize := tree.Size() |
| 75 | |
| 76 | // Use a mutex for concurrent access to values array |
| 77 | var mutex sync.Mutex |
| 78 | |
| 79 | // Initialize with empty slice and expected capacity |
| 80 | values := make([]*Item, 0, expectedSize) |
| 81 | |
| 82 | // Store all the data in an array |
| 83 | saveToValues := func(node art.Node) bool { |
| 84 | item := &Item{ |
| 85 | key: node.Key(), |
| 86 | pst: node.Value().(*data.LogRecordPst), |
| 87 | } |
| 88 | mutex.Lock() |
| 89 | |
| 90 | // Append item to values slice |
| 91 | values = append(values, item) |
| 92 | |
| 93 | mutex.Unlock() |
| 94 | |
| 95 | return true |
| 96 | } |
| 97 | tree.ForEach(saveToValues) |
| 98 | |
| 99 | // Reverse the values slice if reverse is true |
| 100 | if reverse { |
| 101 | for i, j := 0, len(values)-1; i < j; i, j = i+1, j-1 { |
| 102 | values[i], values[j] = values[j], values[i] |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | return &ARTreeIterator{ |
| 107 | currIndex: 0, |
| 108 | reverse: reverse, |
| 109 | values: values, |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | func (artree *ARTreeIterator) Rewind() { |
| 114 | artree.currIndex = 0 |