summaryrefslogtreecommitdiffstats
path: root/map.go
blob: cfb09021d509590cdc26d0b514d93d82a336b4b8 (plain) (blame)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package cache

import (
	"bytes"
	"sync"
	"time"
)

type node struct {
	Hash       uint64
	Expiration time.Time
	Access     uint64
	Key        []byte
	Value      []byte
	HashNext   *node
	HashPrev   *node
	EvictNext  *node
	EvictPrev  *node
}

func (n *node) IsValid() bool {
	return n.Expiration.IsZero() || n.Expiration.After(time.Now())
}

func (n *node) TTL() time.Duration {
	if n.Expiration.IsZero() {
		return 0
	} else {
		return time.Until(n.Expiration)
	}
}

type store struct {
	Bucket  []node
	Length  uint64
	Cost    uint64
	Evict   node
	MaxCost uint64
	Policy  evictionPolicy
	mu      sync.Mutex
}

func (s *store) Init() {
	s.Clear()
	s.Policy.evict = &s.Evict
	s.Policy.SetPolicy(PolicyNone)
}

func (s *store) Clear() {
	s.mu.Lock()
	defer s.mu.Unlock()

	s.Bucket = make([]node, 8)
	s.Length = 0
	s.Cost = 0

	s.Evict.EvictNext = &s.Evict
	s.Evict.EvictPrev = &s.Evict
}

func lookup(s *store, key []byte) (uint64, uint64) {
	hash := hash(key)
	return hash % uint64(len(s.Bucket)), hash
}

func lazyInitBucket(n *node) {
	if n.HashNext == nil {
		n.HashNext = n
		n.HashPrev = n
	}
}

func (s *store) lookup(key []byte) (*node, uint64, uint64) {
	idx, hash := lookup(s, key)

	bucket := &s.Bucket[idx]

	lazyInitBucket(bucket)

	for v := bucket.HashNext; v != bucket; v = v.HashNext {
		if bytes.Equal(key, v.Key) {
			return v, idx, hash
		}
	}

	return nil, idx, hash
}

func (s *store) get(key []byte) ([]byte, time.Duration, bool) {
	v, _, _ := s.lookup(key)
	if v != nil {
		if !v.IsValid() {
			deleteNode(s, v)
			return nil, 0, false
		}
		s.Policy.OnAccess(v)
		return v.Value, v.TTL(), true
	}

	return nil, 0, false
}

func (s *store) Get(key []byte) ([]byte, time.Duration, bool) {
	s.mu.Lock()
	defer s.mu.Unlock()

	return s.get(key)
}

func resize(s *store) {
	bucket := make([]node, 2*len(s.Bucket))

	for v := s.Evict.EvictNext; v != &s.Evict; v = v.EvictNext {
		if !v.IsValid() {
			continue
		}
		idx := v.Hash % uint64(len(bucket))

		n := &bucket[idx]
		lazyInitBucket(n)

		v.HashPrev = n
		v.HashNext = v.HashPrev.HashNext
		v.HashNext.HashPrev = v
		v.HashPrev.HashNext = v
	}

	s.Bucket = bucket
}

func cleanup(s *store) {
	for v := s.Evict.EvictNext; v != &s.Evict; v = v.EvictNext {
		if !v.IsValid() {
			deleteNode(s, v)
		}
	}
}

func evict(s *store) bool {
	for s.MaxCost != 0 && s.MaxCost < s.Cost {
		n := s.Policy.Evict()
		if n == nil {
			break
		}
		deleteNode(s, n)
	}
	return true
}

func (s *store) set(key []byte, value []byte, ttl time.Duration) {
	v, idx, hash := s.lookup(key)
	if v != nil {
		s.Cost = s.Cost + uint64(len(value)) - uint64(len(v.Value))
		v.Value = value
		v.Expiration = time.Now().Add(ttl)
		s.Policy.OnAccess(v)
	}

	bucket := &s.Bucket[idx]
	if float64(s.Length)/float64(len(s.Bucket)) > 0.75 {
		resize(s)
		//resize may invidate pointer to bucket
		bucket = &s.Bucket[idx]
		lazyInitBucket(bucket)
	}

	node := &node{
		Hash:  hash,
		Key:   key,
		Value: value,
	}

	if ttl != 0 {
		node.Expiration = time.Now().Add(ttl)
	}

	node.HashPrev = bucket
	node.HashNext = node.HashPrev.HashNext
	node.HashNext.HashPrev = node
	node.HashPrev.HashNext = node

	s.Policy.OnInsert(node)

	s.Cost = s.Cost + uint64(len(key)) + uint64(len(value))
	s.Length = s.Length + 1
}

func (s *store) Set(key []byte, value []byte, ttl time.Duration) {
	s.mu.Lock()
	defer s.mu.Unlock()

	s.set(key, value, ttl)
}

func deleteNode(s *store, v *node) {
	v.HashNext.HashPrev = v.HashPrev
	v.HashPrev.HashNext = v.HashNext
	v.HashNext = nil
	v.HashPrev = nil

	v.EvictNext.EvictPrev = v.EvictPrev
	v.EvictPrev.EvictNext = v.EvictNext
	v.EvictNext = nil
	v.EvictPrev = nil

	s.Cost = s.Cost - (uint64(len(v.Key)) + uint64(len(v.Value)))
	s.Length = s.Length - 1
}

func (s *store) delete(key []byte) bool {
	v, _, _ := s.lookup(key)
	if v != nil {
		deleteNode(s, v)
		return true
	}

	return false
}

func (s *store) Delete(key []byte) bool {
	s.mu.Lock()
	defer s.mu.Unlock()

	return s.delete(key)
}