forked from yerden/go-util
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deduper.go
67 lines (58 loc) · 1.36 KB
/
deduper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package deduper
import (
"container/list"
"time"
)
type Keyer interface {
// create a key for uniqueness check
// should be unique, comparable, suitable for map
Key() interface{}
}
type DeduperConfig struct {
MaxEntries int
}
type Deduper struct {
maxEntries int
// map of Key()s for fast lookup
lookup map[interface{}]*list.Element
deathrow *list.List
}
type item struct {
value Keyer
t time.Time
}
func newItem(input Keyer) item {
return item{input, time.Now()}
}
func New(c DeduperConfig) *Deduper {
return &Deduper{
maxEntries: c.MaxEntries,
lookup: make(map[interface{}]*list.Element),
deathrow: list.New()}
}
// Consume input and return:
// original value in cache,
// time of original value,
// true if input is brand new, or false if it's already in cache
func (d *Deduper) Consume(input Keyer) (Keyer, time.Time, bool) {
l := d.deathrow
key := input.Key()
if e, ok := d.lookup[key]; ok {
// list is not empty and it's a match
// promote that element
if prev := e.Prev(); prev != nil {
l.MoveBefore(e, prev)
}
it := e.Value.(item)
return it.value, it.t, false
}
if l.Len() > d.maxEntries {
// cleanup
// remove back element as the rarest
it := l.Remove(l.Back()).(item)
delete(d.lookup, it.value.Key())
}
it := newItem(input)
d.lookup[key] = l.PushFront(it)
return it.value, it.t, true
}