-
Notifications
You must be signed in to change notification settings - Fork 3
/
lfu.js
193 lines (171 loc) · 4.46 KB
/
lfu.js
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
/*
* Copyright (c) 2022 RethinkDNS and its authors.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import { RangeList, mkrange } from "./ds/range-list.js";
import { HashMap } from "./ds/map.js";
import { MultiClock } from "./strat/multi-clock.js";
import { O1 } from "./strat/o1.js";
/**
* A constant-time LFU cache for arbitary (key, value) pairs.
* @implements {LfuBase<K, V>}
*/
export class LfuCache {
constructor(id, capacity) {
/** @type {string} */
this.id = id;
/** @type {O1<string, HashMap<string, any>>} */
this.cache = new O1({
cap: capacity,
freq: 16,
store: () => new HashMap(),
});
}
get(key, freq = 1) {
return this.cache.val(key, freq) || false;
}
put(key, val, freq = 1) {
return this.cache.put(key, val, freq);
}
find(k, cursor, freq = 1) {
const [c, v] = this.cache.search(k, cursor, freq);
return new Result(c, v);
}
}
/**
* An approximate LFU cache for arbitary (key, value) pairs.
* @implements {LfuBase<K, V>}
*/
export class ClockLfu {
constructor(id, capacity) {
/** @type {string} */
this.id = id;
/** @type {MultiClock<string, HashMap<string, any>>} */
this.cache = new MultiClock({
cap: capacity,
store: () => new HashMap(),
});
}
get(key, freq = 1) {
return this.cache.val(key, freq) || false;
}
put(key, val, freq = 1) {
return this.cache.put(key, val, freq);
}
find(k, cursor, freq = 1) {
const [c, v] = this.cache.search(k, cursor, freq);
return new Result(c, v);
}
}
/**
* A constant-time LFU cache for arbitary (key, value) pairs.
* @implements {LfuBase<K, V>}
*/
export class RangeLfu {
constructor(id, capacity) {
/** @type {string} */
this.id = id;
/** @type {O1<K, RangeList<K, V>} */
this.cache = new O1({
cap: capacity,
freq: 16,
store: (lvl) => new RangeList(lvl),
});
}
get(n, freq = 1) {
return this.cache.val(mkrange(n, n), freq) || false;
}
put(lo, hi, val, freq = 1) {
return this.cache.put(mkrange(lo, hi), val, freq);
}
find(k, cursor, freq = 1) {
const [c, v] = this.cache.search(mkrange(k, k), cursor, freq);
return new Result(c, v);
}
}
/**
* An approximate LFU cache for (integer-range, value) pairs.
* @implements {LfuBase<K, V>}
*/
export class RangeClockLfu {
constructor(id, capacity) {
// delete, set, get in skip-lists tend towards O(log n)
// and so, higher the n, the lesser the performance hit.
// For ex, a skip-list with 512 elements on average may
// iterate through 9 elements = ln(512); and 'just' 18
// iterations can search through 262144 elements. That
// is, larger skip-lists perform better than numerous
// (sharded) smaller skip-lists, and so: here, per clock
// capacity (slotsperclock * handsperclock) is propotional
// to user requested capacity itself. Each clock is backed
// by exactly one skip-list, which means it is of the exact
// same capacity as the Clock that contains it.
/** @type {string} */
this.id = id;
/** @type {MultiClock<K, RangeList<K, V>>} */
this.cache = new MultiClock({
cap: capacity,
store: (lvl) => new RangeList(lvl),
});
}
get(n, freq = 1) {
return this.cache.val(mkrange(n, n), freq) || false;
}
put(lo, hi, val, freq = 1) {
return this.cache.put(mkrange(lo, hi), val, freq);
}
find(k, cursor, freq = 1) {
const [c, v] = this.cache.search(mkrange(k, k), cursor, freq);
return new Result(c, v);
}
}
/**
* Interface for LFU caches.
* @interface
* @template K, V
*/
class LfuBase {
constructor(id, capacity) {
/** @type {string} */
this.id = id;
/** @type {number} */
this.capacity = capacity;
/** @type {LfuBase<K, Store<K, V>>} */
this.cache = null;
throw new Error("absract");
}
/**
* @param {K} k
* @param {number} freq
* @returns {V}
*/
get(k, freq) {}
/**
* @param {K} k
* @param {V} v
* @param {number} freq
* @returns {boolean}
*/
put(k, v, freq) {}
/**
* @param {K} k
* @param {any} cursor
* @param {number} freq
* @returns {Result<V>}
*/
find(k, cursor, freq) {}
}
/**
* @template V
*/
export class Result {
constructor(c, v) {
/** @type {any} */
this.cursor = c; // never null
/** @type {V?} */
this.value = v; // may be null
}
}