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.

history.go 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. // Copyright (c) 2018 Shivaram Lingamneni <slingamn@cs.stanford.edu>
  2. // released under the MIT license
  3. package history
  4. import (
  5. "github.com/oragono/oragono/irc/utils"
  6. "sync"
  7. "sync/atomic"
  8. "time"
  9. )
  10. type ItemType uint
  11. const (
  12. uninitializedItem ItemType = iota
  13. Privmsg
  14. Notice
  15. Join
  16. Part
  17. Kick
  18. Quit
  19. Mode
  20. Tagmsg
  21. Nick
  22. )
  23. const (
  24. initialAutoSize = 32
  25. )
  26. // a Tagmsg that consists entirely of transient tags is not stored
  27. var transientTags = map[string]bool{
  28. "+draft/typing": true,
  29. "+typing": true, // future-proofing
  30. }
  31. // Item represents an event (e.g., a PRIVMSG or a JOIN) and its associated data
  32. type Item struct {
  33. Type ItemType
  34. Nick string
  35. // this is the uncasefolded account name, if there's no account it should be set to "*"
  36. AccountName string
  37. // for non-privmsg items, we may stuff some other data in here
  38. Message utils.SplitMessage
  39. Tags map[string]string
  40. Params [1]string
  41. }
  42. // HasMsgid tests whether a message has the message id `msgid`.
  43. func (item *Item) HasMsgid(msgid string) bool {
  44. if item.Message.Msgid == msgid {
  45. return true
  46. }
  47. for _, pair := range item.Message.Wrapped {
  48. if pair.Msgid == msgid {
  49. return true
  50. }
  51. }
  52. return false
  53. }
  54. func (item *Item) isStorable() bool {
  55. if item.Type == Tagmsg {
  56. for name := range item.Tags {
  57. if !transientTags[name] {
  58. return true
  59. }
  60. }
  61. return false // all tags were blacklisted
  62. } else {
  63. return true
  64. }
  65. }
  66. type Predicate func(item Item) (matches bool)
  67. // Buffer is a ring buffer holding message/event history for a channel or user
  68. type Buffer struct {
  69. sync.RWMutex
  70. // ring buffer, see irc/whowas.go for conventions
  71. buffer []Item
  72. start int
  73. end int
  74. maximumSize int
  75. window time.Duration
  76. lastDiscarded time.Time
  77. enabled uint32
  78. nowFunc func() time.Time
  79. }
  80. func NewHistoryBuffer(size int, window time.Duration) (result *Buffer) {
  81. result = new(Buffer)
  82. result.Initialize(size, window)
  83. return
  84. }
  85. func (hist *Buffer) Initialize(size int, window time.Duration) {
  86. initialSize := size
  87. if window != 0 {
  88. initialSize = initialAutoSize
  89. if size < initialSize {
  90. initialSize = size // min(initialAutoSize, size)
  91. }
  92. }
  93. hist.buffer = make([]Item, initialSize)
  94. hist.start = -1
  95. hist.end = -1
  96. hist.window = window
  97. hist.maximumSize = size
  98. hist.nowFunc = time.Now
  99. hist.setEnabled(size)
  100. }
  101. func (hist *Buffer) setEnabled(size int) {
  102. var enabled uint32
  103. if size != 0 {
  104. enabled = 1
  105. }
  106. atomic.StoreUint32(&hist.enabled, enabled)
  107. }
  108. // Enabled returns whether the buffer is currently storing messages
  109. // (a disabled buffer blackholes everything it sees)
  110. func (list *Buffer) Enabled() bool {
  111. return atomic.LoadUint32(&list.enabled) != 0
  112. }
  113. // Add adds a history item to the buffer
  114. func (list *Buffer) Add(item Item) {
  115. // fast path without a lock acquisition for when we are not storing history
  116. if !list.Enabled() {
  117. return
  118. }
  119. if !item.isStorable() {
  120. return
  121. }
  122. if item.Message.Time.IsZero() {
  123. item.Message.Time = time.Now().UTC()
  124. }
  125. list.Lock()
  126. defer list.Unlock()
  127. list.maybeExpand()
  128. var pos int
  129. if list.start == -1 { // empty
  130. pos = 0
  131. list.start = 0
  132. list.end = 1 % len(list.buffer)
  133. } else if list.start != list.end { // partially full
  134. pos = list.end
  135. list.end = (list.end + 1) % len(list.buffer)
  136. } else if list.start == list.end { // full
  137. pos = list.end
  138. list.end = (list.end + 1) % len(list.buffer)
  139. list.start = list.end // advance start as well, overwriting first entry
  140. // record the timestamp of the overwritten item
  141. if list.lastDiscarded.Before(list.buffer[pos].Message.Time) {
  142. list.lastDiscarded = list.buffer[pos].Message.Time
  143. }
  144. }
  145. list.buffer[pos] = item
  146. }
  147. // Reverse reverses an []Item, in-place.
  148. func Reverse(results []Item) {
  149. for i, j := 0, len(results)-1; i < j; i, j = i+1, j-1 {
  150. results[i], results[j] = results[j], results[i]
  151. }
  152. }
  153. // Between returns all history items with a time `after` <= time <= `before`,
  154. // with an indication of whether the results are complete or are missing items
  155. // because some of that period was discarded. A zero value of `before` is considered
  156. // higher than all other times.
  157. func (list *Buffer) Between(after, before time.Time, ascending bool, limit int) (results []Item, complete bool) {
  158. if !list.Enabled() {
  159. return
  160. }
  161. list.RLock()
  162. defer list.RUnlock()
  163. complete = after.Equal(list.lastDiscarded) || after.After(list.lastDiscarded)
  164. satisfies := func(item Item) bool {
  165. return (after.IsZero() || item.Message.Time.After(after)) && (before.IsZero() || item.Message.Time.Before(before))
  166. }
  167. return list.matchInternal(satisfies, ascending, limit), complete
  168. }
  169. // Match returns all history items such that `predicate` returns true for them.
  170. // Items are considered in reverse insertion order if `ascending` is false, or
  171. // in insertion order if `ascending` is true, up to a total of `limit` matches
  172. // if `limit` > 0 (unlimited otherwise).
  173. // `predicate` MAY be a closure that maintains its own state across invocations;
  174. // it MUST NOT acquire any locks or otherwise do anything weird.
  175. // Results are always returned in insertion order.
  176. func (list *Buffer) Match(predicate Predicate, ascending bool, limit int) (results []Item) {
  177. if !list.Enabled() {
  178. return
  179. }
  180. list.RLock()
  181. defer list.RUnlock()
  182. return list.matchInternal(predicate, ascending, limit)
  183. }
  184. // you must be holding the read lock to call this
  185. func (list *Buffer) matchInternal(predicate Predicate, ascending bool, limit int) (results []Item) {
  186. if list.start == -1 {
  187. return
  188. }
  189. var pos, stop int
  190. if ascending {
  191. pos = list.start
  192. stop = list.prev(list.end)
  193. } else {
  194. pos = list.prev(list.end)
  195. stop = list.start
  196. }
  197. for {
  198. if predicate(list.buffer[pos]) {
  199. results = append(results, list.buffer[pos])
  200. }
  201. if pos == stop || (limit != 0 && len(results) == limit) {
  202. break
  203. }
  204. if ascending {
  205. pos = list.next(pos)
  206. } else {
  207. pos = list.prev(pos)
  208. }
  209. }
  210. // TODO sort by time instead?
  211. if !ascending {
  212. Reverse(results)
  213. }
  214. return
  215. }
  216. // Latest returns the items most recently added, up to `limit`. If `limit` is 0,
  217. // it returns all items.
  218. func (list *Buffer) Latest(limit int) (results []Item) {
  219. matchAll := func(item Item) bool { return true }
  220. return list.Match(matchAll, false, limit)
  221. }
  222. // LastDiscarded returns the latest time of any entry that was evicted
  223. // from the ring buffer.
  224. func (list *Buffer) LastDiscarded() time.Time {
  225. list.RLock()
  226. defer list.RUnlock()
  227. return list.lastDiscarded
  228. }
  229. func (list *Buffer) prev(index int) int {
  230. switch index {
  231. case 0:
  232. return len(list.buffer) - 1
  233. default:
  234. return index - 1
  235. }
  236. }
  237. func (list *Buffer) next(index int) int {
  238. switch index {
  239. case len(list.buffer) - 1:
  240. return 0
  241. default:
  242. return index + 1
  243. }
  244. }
  245. // return n such that v <= n and n == 2**i for some i
  246. func roundUpToPowerOfTwo(v int) int {
  247. // http://graphics.stanford.edu/~seander/bithacks.html
  248. v -= 1
  249. v |= v >> 1
  250. v |= v >> 2
  251. v |= v >> 4
  252. v |= v >> 8
  253. v |= v >> 16
  254. return v + 1
  255. }
  256. func (list *Buffer) maybeExpand() {
  257. if list.window == 0 {
  258. return // autoresize is disabled
  259. }
  260. length := list.length()
  261. if length < len(list.buffer) {
  262. return // we have spare capacity already
  263. }
  264. if len(list.buffer) == list.maximumSize {
  265. return // cannot expand any further
  266. }
  267. wouldDiscard := list.buffer[list.start].Message.Time
  268. if list.window < list.nowFunc().Sub(wouldDiscard) {
  269. return // oldest element is old enough to overwrite
  270. }
  271. newSize := roundUpToPowerOfTwo(length + 1)
  272. if list.maximumSize < newSize {
  273. newSize = list.maximumSize
  274. }
  275. list.resize(newSize)
  276. }
  277. // Resize shrinks or expands the buffer
  278. func (list *Buffer) Resize(maximumSize int, window time.Duration) {
  279. list.Lock()
  280. defer list.Unlock()
  281. if list.maximumSize == maximumSize && list.window == window {
  282. return // no-op
  283. }
  284. list.maximumSize = maximumSize
  285. list.window = window
  286. // if we're not autoresizing, we need to resize now;
  287. // if we are autoresizing, we may need to shrink the buffer down to maximumSize,
  288. // but we don't need to grow it now (we can just grow it on the next Add)
  289. // TODO make it possible to shrink the buffer so that it only contains `window`
  290. if window == 0 || maximumSize < len(list.buffer) {
  291. list.resize(maximumSize)
  292. }
  293. }
  294. func (list *Buffer) resize(size int) {
  295. newbuffer := make([]Item, size)
  296. list.setEnabled(size)
  297. if list.start == -1 {
  298. // indices are already correct and nothing needs to be copied
  299. } else if size == 0 {
  300. // this is now the empty list
  301. list.start = -1
  302. list.end = -1
  303. } else {
  304. currentLength := list.length()
  305. start := list.start
  306. end := list.end
  307. // if we're truncating, keep the latest entries, not the earliest
  308. if size < currentLength {
  309. start = list.end - size
  310. if start < 0 {
  311. start += len(list.buffer)
  312. }
  313. // update lastDiscarded for discarded entries
  314. for i := list.start; i != start; i = (i + 1) % len(list.buffer) {
  315. if list.lastDiscarded.Before(list.buffer[i].Message.Time) {
  316. list.lastDiscarded = list.buffer[i].Message.Time
  317. }
  318. }
  319. }
  320. if start < end {
  321. copied := copy(newbuffer, list.buffer[start:end])
  322. list.start = 0
  323. list.end = copied % size
  324. } else {
  325. lenInitial := len(list.buffer) - start
  326. copied := copy(newbuffer, list.buffer[start:])
  327. copied += copy(newbuffer[lenInitial:], list.buffer[:end])
  328. list.start = 0
  329. list.end = copied % size
  330. }
  331. }
  332. list.buffer = newbuffer
  333. }
  334. func (hist *Buffer) length() int {
  335. if hist.start == -1 {
  336. return 0
  337. } else if hist.start < hist.end {
  338. return hist.end - hist.start
  339. } else {
  340. return len(hist.buffer) - (hist.start - hist.end)
  341. }
  342. }