event.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package fileSystem
  2. import (
  3. "fmt"
  4. "strings"
  5. "sync"
  6. "time"
  7. etcdErr "github.com/xiangli-cmu/etcd/error"
  8. )
  9. const (
  10. Get = "get"
  11. Create = "create"
  12. Update = "update"
  13. Delete = "delete"
  14. TestAndSet = "testAndSet"
  15. )
  16. type Event struct {
  17. Action string `json:"action"`
  18. Key string `json:"key, omitempty"`
  19. Dir bool `json:"dir,omitempty"`
  20. PrevValue string `json:"prevValue,omitempty"`
  21. Value string `json:"value,omitempty"`
  22. KVPairs []KeyValuePair `json:"kvs,omitempty"`
  23. Expiration *time.Time `json:"expiration,omitempty"`
  24. TTL int64 `json:"ttl,omitempty"` // Time to live in second
  25. // The command index of the raft machine when the command is executed
  26. Index uint64 `json:"index"`
  27. Term uint64 `json:"term"`
  28. }
  29. // When user list a directory, we add all the node into key-value pair slice
  30. type KeyValuePair struct {
  31. Key string `json:"key, omitempty"`
  32. Value string `json:"value,omitempty"`
  33. Dir bool `json:"dir,omitempty"`
  34. KVPairs []KeyValuePair `json:"kvs,omitempty"`
  35. }
  36. // interfaces for sorting
  37. func (k KeyValuePair) Len() int {
  38. return len(k.KVPairs)
  39. }
  40. func (k KeyValuePair) Less(i, j int) bool {
  41. return k.KVPairs[i].Key < k.KVPairs[j].Key
  42. }
  43. func (k KeyValuePair) Swap(i, j int) {
  44. k.KVPairs[i], k.KVPairs[j] = k.KVPairs[j], k.KVPairs[i]
  45. }
  46. func newEvent(action string, key string, index uint64, term uint64) *Event {
  47. return &Event{
  48. Action: action,
  49. Key: key,
  50. Index: index,
  51. Term: term,
  52. }
  53. }
  54. type eventQueue struct {
  55. Events []*Event
  56. Size int
  57. Front int
  58. Capacity int
  59. }
  60. func (eq *eventQueue) back() int {
  61. return (eq.Front + eq.Size - 1 + eq.Capacity) % eq.Capacity
  62. }
  63. func (eq *eventQueue) insert(e *Event) {
  64. index := (eq.back() + 1) % eq.Capacity
  65. eq.Events[index] = e
  66. if eq.Size == eq.Capacity { //dequeue
  67. eq.Front = (index + 1) % eq.Capacity
  68. } else {
  69. eq.Size++
  70. }
  71. }
  72. type EventHistory struct {
  73. Queue eventQueue
  74. StartIndex uint64
  75. rwl sync.RWMutex
  76. }
  77. func newEventHistory(capacity int) *EventHistory {
  78. return &EventHistory{
  79. Queue: eventQueue{
  80. Capacity: capacity,
  81. Events: make([]*Event, capacity),
  82. },
  83. }
  84. }
  85. // addEvent function adds event into the eventHistory
  86. func (eh *EventHistory) addEvent(e *Event) {
  87. eh.rwl.Lock()
  88. defer eh.rwl.Unlock()
  89. eh.Queue.insert(e)
  90. eh.StartIndex = eh.Queue.Events[eh.Queue.Front].Index
  91. }
  92. // scan function is enumerating events from the index in history and
  93. // stops till the first point where the key has identified prefix
  94. func (eh *EventHistory) scan(prefix string, index uint64) (*Event, error) {
  95. eh.rwl.RLock()
  96. defer eh.rwl.RUnlock()
  97. start := index - eh.StartIndex
  98. // the index should locate after the event history's StartIndex
  99. // and before its size
  100. if start < 0 {
  101. // TODO: Add error type
  102. return nil,
  103. etcdErr.NewError(etcdErr.EcodeEventIndexCleared,
  104. fmt.Sprintf("prefix:%v index:%v", prefix, index),
  105. )
  106. }
  107. if start >= uint64(eh.Queue.Size) {
  108. return nil, nil
  109. }
  110. i := int((start + uint64(eh.Queue.Front)) % uint64(eh.Queue.Capacity))
  111. for {
  112. e := eh.Queue.Events[i]
  113. if strings.HasPrefix(e.Key, prefix) {
  114. return e, nil
  115. }
  116. i = (i + 1) % eh.Queue.Capacity
  117. if i == eh.Queue.back() {
  118. // TODO: Add error type
  119. return nil, nil
  120. }
  121. }
  122. }