|
| 1 | +package bisect |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "math/rand" |
| 6 | + "sync" |
| 7 | +) |
| 8 | + |
| 9 | +type smartRev struct { |
| 10 | + Rev string |
| 11 | + Cancel chan interface{} |
| 12 | + state *BisectState |
| 13 | +} |
| 14 | + |
| 15 | +func (s *smartRev) Good() { |
| 16 | + s.state.markAsGood(s.Rev) |
| 17 | +} |
| 18 | + |
| 19 | +func (s *smartRev) Bad() { |
| 20 | + s.state.markAsBad(s.Rev) |
| 21 | +} |
| 22 | + |
| 23 | +type BisectState struct { |
| 24 | + revs []string // immutable |
| 25 | + |
| 26 | + indexes map[string]int // written once but it's async |
| 27 | + indexesMu sync.RWMutex |
| 28 | + |
| 29 | + start int |
| 30 | + startMu sync.RWMutex |
| 31 | + |
| 32 | + end int |
| 33 | + endMu sync.RWMutex |
| 34 | + |
| 35 | + // bisect tracker |
| 36 | + bisectSteps []int |
| 37 | + bisectIteration int |
| 38 | + bisectMu sync.Mutex |
| 39 | + |
| 40 | + activeListenersMu sync.Mutex |
| 41 | + activeListeners []*smartRev |
| 42 | +} |
| 43 | + |
| 44 | +func NewBisectState(revs []string) *BisectState { |
| 45 | + state := &BisectState{ |
| 46 | + revs: revs, |
| 47 | + end: len(revs) - 1, |
| 48 | + indexes: make(map[string]int), |
| 49 | + bisectSteps: make([]int, len(revs)), |
| 50 | + } |
| 51 | + |
| 52 | + state.initIndexesTable() |
| 53 | + state.initBisectSteps() |
| 54 | + |
| 55 | + return state |
| 56 | +} |
| 57 | + |
| 58 | +func (b *BisectState) initIndexesTable() { |
| 59 | + for i, rev := range b.revs { |
| 60 | + b.indexes[rev] = i |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func (b *BisectState) initBisectSteps() { |
| 65 | + for i := range b.bisectSteps { |
| 66 | + b.bisectSteps[i] = i |
| 67 | + } |
| 68 | + |
| 69 | + rand.Seed(1) |
| 70 | + rand.Shuffle(len(b.bisectSteps), func(i, j int) { |
| 71 | + b.bisectSteps[i], b.bisectSteps[j] = b.bisectSteps[j], b.bisectSteps[i] |
| 72 | + }) |
| 73 | +} |
| 74 | + |
| 75 | +func (b *BisectState) Next() *smartRev { |
| 76 | + b.bisectMu.Lock() |
| 77 | + defer b.bisectMu.Unlock() |
| 78 | + for ; b.bisectIteration < len(b.bisectSteps); b.bisectIteration++ { |
| 79 | + step := b.bisectSteps[b.bisectIteration] |
| 80 | + if step >= b.start && step <= b.end { |
| 81 | + // TODO implement ref cancellation |
| 82 | + rev := b.revs[step] |
| 83 | + b.bisectIteration++ |
| 84 | + return &smartRev{ |
| 85 | + Rev: rev, |
| 86 | + Cancel: make(chan interface{}), |
| 87 | + state: b, |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + return nil |
| 92 | +} |
| 93 | + |
| 94 | +func (b *BisectState) getIndex(rev string) int { |
| 95 | + b.indexesMu.RLock() |
| 96 | + defer b.indexesMu.RUnlock() |
| 97 | + return b.indexes[rev] |
| 98 | +} |
| 99 | + |
| 100 | +func (b *BisectState) markAsGood(rev string) error { |
| 101 | + i := b.getIndex(rev) |
| 102 | + |
| 103 | + if i <= b.start { |
| 104 | + return nil |
| 105 | + } |
| 106 | + |
| 107 | + if i >= b.end { |
| 108 | + return fmt.Errorf("TODO(come up with an error msg)") |
| 109 | + } |
| 110 | + |
| 111 | + b.startMu.Lock() |
| 112 | + defer b.startMu.Unlock() |
| 113 | + b.start = i |
| 114 | + return nil |
| 115 | +} |
| 116 | + |
| 117 | +func (b *BisectState) markAsBad(rev string) error { |
| 118 | + i := b.getIndex(rev) |
| 119 | + |
| 120 | + if i >= b.end { |
| 121 | + return nil |
| 122 | + } |
| 123 | + |
| 124 | + if i <= b.start { |
| 125 | + return fmt.Errorf("TODO(come up with an error msg)") |
| 126 | + } |
| 127 | + |
| 128 | + b.endMu.Lock() |
| 129 | + defer b.endMu.Unlock() |
| 130 | + b.end = i |
| 131 | + return nil |
| 132 | +} |
| 133 | + |
| 134 | +func (b *BisectState) FirstBadRev() *string { |
| 135 | + b.startMu.RLock() |
| 136 | + defer b.startMu.RUnlock() |
| 137 | + |
| 138 | + b.endMu.RLock() |
| 139 | + defer b.endMu.RUnlock() |
| 140 | + |
| 141 | + if b.end-b.start == 1 { |
| 142 | + return &b.revs[b.end] |
| 143 | + } |
| 144 | + |
| 145 | + return nil |
| 146 | +} |
0 commit comments