This repository has been archived by the owner on May 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
/
feature_store_event_wrapper.js
258 lines (229 loc) · 8.04 KB
/
feature_store_event_wrapper.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
const dataKind = require('./versioned_data_kind');
function NamespacedDataSet() {
let itemsByNamespace = {};
function get(namespace, key) {
const items = itemsByNamespace[namespace];
return items && items[key];
}
function set(namespace, key, value) {
let items = itemsByNamespace[namespace];
if (!items) {
items = {};
itemsByNamespace[namespace] = items;
}
items[key] = value;
}
function remove(namespace, key) {
const items = itemsByNamespace[namespace];
if (items) {
delete items[key];
}
}
function removeAll() {
itemsByNamespace = {};
}
function enumerate(callback) {
for (const [ns, items] of Object.entries(itemsByNamespace)) {
const keys = Object.keys(items).sort(); // sort to make tests determinate
for (const key of keys) {
callback(ns, key, items[key]);
}
}
}
function mergeFrom(otherSet) {
otherSet.enumerate(set);
}
return {
get: get,
set: set,
remove: remove,
removeAll: removeAll,
enumerate: enumerate,
mergeFrom: mergeFrom,
toString: () => JSON.stringify(itemsByNamespace),
};
}
function DependencyTracker() {
const dependenciesFrom = NamespacedDataSet();
const dependenciesTo = NamespacedDataSet();
// dependenciesFrom: for a given flag/segment key, what are the flags/segments it relies on
// dependenciesTo: for a given flag/segment key, what are the flags/segments that rely on it
function updateDependenciesFrom(namespace, key, newDependencySet) {
const oldDependencySet = dependenciesFrom.get(namespace, key);
oldDependencySet &&
oldDependencySet.enumerate((depNs, depKey) => {
const depsToThisDep = dependenciesTo.get(depNs, depKey);
depsToThisDep && depsToThisDep.remove(namespace, key);
});
dependenciesFrom.set(namespace, key, newDependencySet);
newDependencySet &&
newDependencySet.enumerate((depNs, depKey) => {
let depsToThisDep = dependenciesTo.get(depNs, depKey);
if (!depsToThisDep) {
depsToThisDep = NamespacedDataSet();
dependenciesTo.set(depNs, depKey, depsToThisDep);
}
depsToThisDep.set(namespace, key, true);
});
}
function updateModifiedItems(inDependencySet, modifiedNamespace, modifiedKey) {
if (!inDependencySet.get(modifiedNamespace, modifiedKey)) {
inDependencySet.set(modifiedNamespace, modifiedKey, true);
const affectedItems = dependenciesTo.get(modifiedNamespace, modifiedKey);
affectedItems &&
affectedItems.enumerate((ns, key) => {
updateModifiedItems(inDependencySet, ns, key);
});
}
}
function reset() {
dependenciesFrom.removeAll();
dependenciesTo.removeAll();
}
return {
updateDependenciesFrom: updateDependenciesFrom,
updateModifiedItems: updateModifiedItems,
reset: reset,
};
}
function FeatureStoreEventWrapper(featureStore, emitter) {
const dependencyTracker = DependencyTracker();
function hasEventListeners() {
// Before we do something that could generate a change event, we'll check whether anyone is
// currently listening for such events. If they're not, then we can skip the whole "query the
// old data so we can compare it to the new data and see if there was a change" step, which
// could be expensive with a persistent feature store.
return emitter.eventNames().some(name => name === 'update' || name.substring(0, 7) === 'update:'); // Node 6 may not have startsWith()
}
function addIfModified(namespace, key, oldValue, newValue, toDataSet) {
if (newValue && oldValue && newValue.version <= oldValue.version) {
return;
}
dependencyTracker.updateModifiedItems(toDataSet, namespace, key);
}
function sendChangeEvents(dataSet) {
dataSet.enumerate((namespace, key) => {
if (namespace === dataKind.features.namespace) {
const arg = { key: key };
setImmediate(() => {
emitter.emit('update', arg);
});
setImmediate(() => {
emitter.emit(`update:${key}`, arg);
});
}
});
}
function computeDependencies(kind, item) {
const ret = NamespacedDataSet();
if (kind === dataKind.features) {
for (const prereq of item.prerequisites || []) {
ret.set(dataKind.features.namespace, prereq.key, true);
}
}
if (kind === dataKind.features || kind === dataKind.segments) {
for (const rule of item.rules || []) {
for (const clause of rule.clauses || []) {
if (clause.op === 'segmentMatch') {
for (const value of clause.values) {
ret.set(dataKind.segments.namespace, value, true);
}
}
}
}
}
return ret;
}
return {
get: featureStore.get.bind(featureStore),
all: featureStore.all.bind(featureStore),
initialized: featureStore.initialized.bind(featureStore),
close: featureStore.close.bind(featureStore),
init: (newData, callback) => {
const checkForChanges = hasEventListeners();
const doInit = oldData => {
featureStore.init(newData, () => {
dependencyTracker.reset();
for (const [namespace, items] of Object.entries(newData)) {
const kind = dataKind[namespace];
for (const key of Object.keys(items || {})) {
const item = items[key];
dependencyTracker.updateDependenciesFrom(namespace, key, computeDependencies(kind, item));
}
}
if (checkForChanges) {
const updatedItems = NamespacedDataSet();
for (const namespace of Object.keys(newData)) {
const oldDataForKind = oldData[namespace];
const newDataForKind = newData[namespace];
const mergedData = Object.assign({}, oldDataForKind, newDataForKind);
for (const key of Object.keys(mergedData)) {
addIfModified(
namespace,
key,
oldDataForKind && oldDataForKind[key],
newDataForKind && newDataForKind[key],
updatedItems
);
}
}
sendChangeEvents(updatedItems);
}
callback && callback();
});
};
if (checkForChanges) {
featureStore.all(dataKind.features, oldFlags => {
featureStore.all(dataKind.segments, oldSegments => {
const oldData = {};
oldData[dataKind.features.namespace] = oldFlags;
oldData[dataKind.segments.namespace] = oldSegments;
doInit(oldData);
});
});
} else {
doInit();
}
},
delete: (kind, key, version, callback) => {
const checkForChanges = hasEventListeners();
const doDelete = oldItem => {
featureStore.delete(kind, key, version, () => {
dependencyTracker.updateDependenciesFrom(kind.namespace, key, null);
if (checkForChanges) {
const updatedItems = NamespacedDataSet();
addIfModified(kind.namespace, key, oldItem, { version: version, deleted: true }, updatedItems);
sendChangeEvents(updatedItems);
}
callback && callback();
});
};
if (checkForChanges) {
featureStore.get(kind, key, doDelete);
} else {
doDelete();
}
},
upsert: (kind, newItem, callback) => {
const key = newItem.key;
const checkForChanges = hasEventListeners();
const doUpsert = oldItem => {
featureStore.upsert(kind, newItem, () => {
dependencyTracker.updateDependenciesFrom(kind.namespace, key, computeDependencies(kind, newItem));
if (checkForChanges) {
const updatedItems = NamespacedDataSet();
addIfModified(kind.namespace, key, oldItem, newItem, updatedItems);
sendChangeEvents(updatedItems);
}
callback && callback();
});
};
if (checkForChanges) {
featureStore.get(kind, key, doUpsert);
} else {
doUpsert();
}
},
};
}
module.exports = FeatureStoreEventWrapper;