-
Notifications
You must be signed in to change notification settings - Fork 44
/
caching.js
235 lines (234 loc) · 7.11 KB
/
caching.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
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
226
227
228
229
230
231
232
233
234
235
import { WeakLRUCache, clearKeptObjects } from './native.js';
import { FAILED_CONDITION, ABORT, IF_EXISTS } from './write.js';
import { UNMODIFIED } from './read.js';
import { when } from './util/when.js';
let getLastVersion, getLastTxnId;
const mapGet = Map.prototype.get;
export const CachingStore = (Store, env) => {
let childTxnChanges;
return class LMDBStore extends Store {
constructor(dbName, options) {
super(dbName, options);
if (!env.cacheCommitter) {
env.cacheCommitter = true;
this.on('aftercommit', ({ next, last, txnId }) => {
do {
let meta = next.meta;
let store = meta && meta.store;
if (store) {
if (next.flag & FAILED_CONDITION)
store.cache.delete(meta.key); // just delete it from the map
else {
let expirationPriority = meta.valueSize >> 10;
let cache = store.cache;
let entry = mapGet.call(cache, meta.key);
if (entry && !entry.txnId) {
entry.txnId = txnId;
cache.used(entry, expirationPriority + 4); // this will enter it into the LRFU (with a little lower priority than a read)
}
}
}
} while (next != last && (next = next.next));
});
}
this.db.cachingDb = this;
if (options.cache.clearKeptInterval)
options.cache.clearKeptObjects = clearKeptObjects;
this.cache = new WeakLRUCache(options.cache);
if (options.cache.validated) this.cache.validated = true;
}
get isCaching() {
return true;
}
get(id, options) {
let value;
if (this.cache.validated) {
let entry = this.cache.get(id);
if (entry) {
let cachedValue = entry.value;
if (entry.txnId != null) {
value = super.get(id, {
ifNotTxnId: entry.txnId,
transaction: options && options.transaction,
});
if (value === UNMODIFIED) return cachedValue;
} // with no txn id we do not validate; this is the state of a cached value after a write before it transacts
else return cachedValue;
} else value = super.get(id, options);
} else if (options && options.transaction) {
return super.get(id, options);
} else {
value = this.cache.getValue(id);
if (value !== undefined) {
return value;
}
value = super.get(id);
}
if (
value &&
typeof value === 'object' &&
!options &&
typeof id !== 'object'
) {
let entry = this.cache.setValue(id, value, this.lastSize >> 10);
if (this.useVersions) {
entry.version = getLastVersion();
}
if (this.cache.validated) entry.txnId = getLastTxnId();
}
return value;
}
getEntry(id, options) {
let entry, value;
if (this.cache.validated) {
entry = this.cache.get(id);
if (entry) {
if (entry.txnId != null) {
value = super.get(id, {
ifNotTxnId: entry.txnId,
transaction: options && options.transaction,
});
if (value === UNMODIFIED) return entry;
} // with no txn id we do not validate; this is the state of a cached value after a write before it transacts
else return entry;
} else value = super.get(id, options);
} else if (options && options.transaction) {
return super.getEntry(id, options);
} else {
entry = this.cache.get(id);
if (entry !== undefined) {
return entry;
}
value = super.get(id);
}
if (value === undefined) return;
if (value && typeof value === 'object' && typeof id !== 'object') {
entry = this.cache.setValue(id, value, this.lastSize >> 10);
} else entry = { value };
if (this.useVersions) entry.version = getLastVersion();
if (this.cache.validated) entry.txnId = getLastTxnId();
return entry;
}
putEntry(id, entry, ifVersion) {
let result = super.put(id, entry.value, entry.version, ifVersion);
if (typeof id === 'object') return result;
if (result && result.then)
this.cache.setManually(id, entry); // set manually so we can keep it pinned in memory until it is committed
// sync operation, immediately add to cache
else this.cache.set(id, entry);
}
put(id, value, version, ifVersion) {
let result = super.put(id, value, version, ifVersion);
if (typeof id !== 'object') {
if (value && value['\x10binary-data\x02']) {
// don't cache binary data, since it will be decoded on get
this.cache.delete(id);
return result;
}
let entry;
if (this.cachePuts === false) {
// we are not caching puts, clear the entry at least
this.cache.delete(id);
} else {
if (result?.isSync) {
// sync operation, immediately add to cache
if (result.result)
// if it succeeds
entry = this.cache.setValue(id, value, 0);
else {
this.cache.delete(id);
return result;
} // sync failure
// otherwise keep it pinned in memory until it is committed
} else entry = this.cache.setValue(id, value, -1);
}
if (childTxnChanges) childTxnChanges.add(id);
if (version !== undefined && entry)
entry.version =
typeof version === 'object' ? version.version : version;
}
return result;
}
putSync(id, value, version, ifVersion) {
let result = super.putSync(id, value, version, ifVersion);
if (id !== 'object') {
// sync operation, immediately add to cache, otherwise keep it pinned in memory until it is committed
if (
value &&
this.cachePuts !== false &&
typeof value === 'object' &&
result
) {
let entry = this.cache.setValue(id, value);
if (childTxnChanges) childTxnChanges.add(id);
if (version !== undefined) {
entry.version =
typeof version === 'object' ? version.version : version;
}
} // it is possible that a value used to exist here
else this.cache.delete(id);
}
return result;
}
remove(id, ifVersion) {
this.cache.delete(id);
return super.remove(id, ifVersion);
}
removeSync(id, ifVersion) {
this.cache.delete(id);
return super.removeSync(id, ifVersion);
}
clearAsync(callback) {
this.cache.clear();
return super.clearAsync(callback);
}
clearSync() {
this.cache.clear();
super.clearSync();
}
childTransaction(callback) {
return super.childTransaction(() => {
let cache = this.cache;
let previousChanges = childTxnChanges;
try {
childTxnChanges = new Set();
return when(
callback(),
(result) => {
if (result === ABORT) return abort();
childTxnChanges = previousChanges;
return result;
},
abort,
);
} catch (error) {
abort(error);
}
function abort(error) {
// if the transaction was aborted, remove all affected entries from cache
for (let id of childTxnChanges) cache.delete(id);
childTxnChanges = previousChanges;
if (error) throw error;
else return ABORT;
}
});
}
doesExist(key, versionOrValue) {
let entry = this.cache.get(key);
if (entry) {
if (versionOrValue == null) {
return versionOrValue !== null;
} else if (this.useVersions) {
return (
versionOrValue === IF_EXISTS || entry.version === versionOrValue
);
}
}
return super.doesExist(key, versionOrValue);
}
};
};
export function setGetLastVersion(get, getTxnId) {
getLastVersion = get;
getLastTxnId = getTxnId;
}