mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
modify name. simplify test case.
This commit is contained in:
parent
8ab6684bf5
commit
f41a9b9703
@ -49,17 +49,21 @@ type eventQueue struct {
|
|||||||
events []*Event
|
events []*Event
|
||||||
size int
|
size int
|
||||||
front int
|
front int
|
||||||
back int
|
|
||||||
capacity int
|
capacity int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (eq *eventQueue) back() int {
|
||||||
|
return (eq.front + eq.size - 1 + eq.capacity) % eq.capacity
|
||||||
|
}
|
||||||
|
|
||||||
func (eq *eventQueue) insert(e *Event) {
|
func (eq *eventQueue) insert(e *Event) {
|
||||||
|
|
||||||
eq.back = (eq.back + 1) % eq.capacity
|
index := (eq.back() + 1) % eq.capacity
|
||||||
eq.events[eq.back] = e
|
|
||||||
|
eq.events[index] = e
|
||||||
|
|
||||||
if eq.size == eq.capacity { //dequeue
|
if eq.size == eq.capacity { //dequeue
|
||||||
eq.front = (eq.back + 1) % eq.capacity
|
eq.front = (index + 1) % eq.capacity
|
||||||
} else {
|
} else {
|
||||||
eq.size++
|
eq.size++
|
||||||
}
|
}
|
||||||
@ -77,7 +81,6 @@ func newEventHistory(capacity int) *EventHistory {
|
|||||||
Queue: eventQueue{
|
Queue: eventQueue{
|
||||||
capacity: capacity,
|
capacity: capacity,
|
||||||
events: make([]*Event, capacity),
|
events: make([]*Event, capacity),
|
||||||
back: -1,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -92,27 +95,29 @@ func (eh *EventHistory) addEvent(e *Event) {
|
|||||||
eh.StartIndex = eh.Queue.events[eh.Queue.front].Index
|
eh.StartIndex = eh.Queue.events[eh.Queue.front].Index
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// scan function is enumerating events from the index in history and
|
||||||
|
// stops till the first point where the key has identified prefix
|
||||||
func (eh *EventHistory) scan(prefix string, index uint64) (*Event, error) {
|
func (eh *EventHistory) scan(prefix string, index uint64) (*Event, error) {
|
||||||
eh.rwl.RLock()
|
eh.rwl.RLock()
|
||||||
defer eh.rwl.RUnlock()
|
defer eh.rwl.RUnlock()
|
||||||
|
|
||||||
start := index - eh.StartIndex
|
start := index - eh.StartIndex
|
||||||
|
|
||||||
if start < 0 {
|
// the index should locate after the event history's StartIndex
|
||||||
|
// and before its size
|
||||||
|
|
||||||
|
if start < 0 {
|
||||||
// TODO: Add error type
|
// TODO: Add error type
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if start >= uint64(eh.Queue.size) {
|
if start >= uint64(eh.Queue.size) {
|
||||||
|
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
i := int((start + uint64(eh.Queue.front)) % uint64(eh.Queue.capacity))
|
i := int((start + uint64(eh.Queue.front)) % uint64(eh.Queue.capacity))
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
|
||||||
e := eh.Queue.events[i]
|
e := eh.Queue.events[i]
|
||||||
if strings.HasPrefix(e.Key, prefix) {
|
if strings.HasPrefix(e.Key, prefix) {
|
||||||
return e, nil
|
return e, nil
|
||||||
@ -120,7 +125,7 @@ func (eh *EventHistory) scan(prefix string, index uint64) (*Event, error) {
|
|||||||
|
|
||||||
i = (i + 1) % eh.Queue.capacity
|
i = (i + 1) % eh.Queue.capacity
|
||||||
|
|
||||||
if i == eh.Queue.back {
|
if i == eh.Queue.back() {
|
||||||
// TODO: Add error type
|
// TODO: Add error type
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
@ -19,12 +19,16 @@ func TestEventQueue(t *testing.T) {
|
|||||||
|
|
||||||
// Test
|
// Test
|
||||||
j := 100
|
j := 100
|
||||||
for i := eh.Queue.front; i != eh.Queue.back; i = (i + 1) % eh.Queue.capacity {
|
i := eh.Queue.front
|
||||||
|
n := eh.Queue.size
|
||||||
|
for ; n > 0; n-- {
|
||||||
e := eh.Queue.events[i]
|
e := eh.Queue.events[i]
|
||||||
if e.Index != uint64(j) {
|
if e.Index != uint64(j) {
|
||||||
t.Fatalf("queue error!")
|
t.Fatalf("queue error!")
|
||||||
}
|
}
|
||||||
j++
|
j++
|
||||||
|
i = (i + 1) % eh.Queue.capacity
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user