Skip to content

Commit 4793aeb

Browse files
committed
support for wasm streaming compilation and adapter refactoring
1 parent 4cc1603 commit 4793aeb

File tree

15 files changed

+696
-171
lines changed

15 files changed

+696
-171
lines changed

src/adapters/datasources/datasource-mongodb.js

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -315,29 +315,8 @@ export class DataSourceMongoDb extends DataSource {
315315
async delete (id) {
316316
try {
317317
await (await this.collection()).deleteOne({ _id: id })
318-
this.deleteSync(id)
319318
} catch (error) {
320319
console.error(error)
321320
}
322321
}
323-
324-
/**
325-
* Flush the cache to disk.
326-
* @override
327-
*/
328-
flush () {
329-
try {
330-
this.dsMap.reduce((a, b) => a.then(() => this.saveDb(b.getId(), b)), {})
331-
} catch (error) {
332-
console.error(error)
333-
}
334-
}
335-
336-
/**
337-
* Process terminating, flush cache, close connections.
338-
* @override
339-
*/
340-
close () {
341-
this.flush()
342-
}
343322
}

src/adapters/webassembly/wasm-import.js

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ exports.importWebAssembly = function (remoteEntry) {
4848
liftArray: __liftArray,
4949
notnull: __notnull,
5050
store_ref: __store_ref,
51+
_exports: exports,
5152
memory,
5253

5354
getModelName () {
@@ -65,10 +66,6 @@ exports.importWebAssembly = function (remoteEntry) {
6566
return __liftString(exports.getDomain() >>> 0)
6667
},
6768

68-
callExportedFn (fn, kv) {
69-
return exports[fn](kv)
70-
},
71-
7269
ArrayOfStrings_ID: {
7370
// assembly/index/ArrayOfStrings_ID: u32
7471
valueOf () {
@@ -79,9 +76,14 @@ exports.importWebAssembly = function (remoteEntry) {
7976
}
8077
},
8178

82-
modelFactory (kv) {
83-
// assembly/index/modelFactory(~lib/array/Array<~lib/array/Array<~lib/string/String>>) => ~lib/array/Array<~lib/array/Array<~lib/string/String>>
84-
kv =
79+
modelFactory (obj) {
80+
const entries = Object.entries(obj)
81+
.filter(([k, v]) =>
82+
['string', 'number', 'boolean'].includes(typeof v)
83+
)
84+
.map(([k, v]) => [k, v.toString()])
85+
86+
const kv =
8587
__lowerArray(
8688
(pointer, value) => {
8789
__store_ref(
@@ -98,7 +100,7 @@ exports.importWebAssembly = function (remoteEntry) {
98100
},
99101
5,
100102
2,
101-
kv
103+
entries
102104
) || __notnull()
103105

104106
return __liftArray(
@@ -134,6 +136,23 @@ exports.importWebAssembly = function (remoteEntry) {
134136
.reduce((a, b) => ({ ...a, ...b }))
135137
},
136138

139+
getCommands () {
140+
// assembly/index/getCommands() => ~lib/array/Array<~lib/array/Array<~lib/string/String>>
141+
return __liftArray(
142+
pointer =>
143+
__liftArray(
144+
pointer =>
145+
__liftString(new Uint32Array(memory.buffer)[pointer >>> 2]),
146+
2,
147+
new Uint32Array(memory.buffer)[pointer >>> 2]
148+
),
149+
2,
150+
exports.getCommands() >>> 0
151+
)
152+
.map(([k, v]) => ({ [k]: v }))
153+
.reduce((a, b) => ({ ...a, ...b }))
154+
},
155+
137156
emitEvent (kv) {
138157
// assembly/index/emitEvent(~lib/array/Array<~lib/array/Array<~lib/string/String>>) => ~lib/array/Array<~lib/array/Array<~lib/string/String>>
139158
kv =
@@ -172,23 +191,6 @@ exports.importWebAssembly = function (remoteEntry) {
172191
)
173192
},
174193

175-
getCommands () {
176-
// assembly/index/getCommands() => ~lib/array/Array<~lib/array/Array<~lib/string/String>>
177-
return __liftArray(
178-
pointer =>
179-
__liftArray(
180-
pointer =>
181-
__liftString(new Uint32Array(memory.buffer)[pointer >>> 2]),
182-
2,
183-
new Uint32Array(memory.buffer)[pointer >>> 2]
184-
),
185-
2,
186-
exports.getCommands() >>> 0
187-
)
188-
.map(([k, v]) => ({ [k]: v }))
189-
.reduce((a, b) => ({ ...a, ...b }))
190-
},
191-
192194
onUpdate (kv) {
193195
// assembly/index/onUpdate(~lib/array/Array<~lib/array/Array<~lib/string/String>>) => ~lib/array/Array<~lib/array/Array<~lib/string/String>>
194196
kv =
@@ -353,8 +355,8 @@ exports.importWebAssembly = function (remoteEntry) {
353355
}
354356
}
355357

356-
function __notnull () {
357-
throw TypeError('value must not be null')
358+
function __notnull (key) {
359+
throw TypeError(`value must not be null ${key}`)
358360
}
359361

360362
function __store_ref (pointer, value) {

src/adapters/webassembly/wasm-interop.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ exports.WasmInterop = function (wasmExports) {
2626
store_ref,
2727
notnull,
2828
memory,
29-
callExportedFn
29+
_exports
3030
} = wasmExports
3131

3232
/**
@@ -44,7 +44,7 @@ exports.WasmInterop = function (wasmExports) {
4444
new Uint32Array(memory.buffer)[pointer >>> 2]
4545
),
4646
2,
47-
callExportedFn(fn, kv) >>> 0
47+
_exports[fn](kv) >>> 0
4848
)
4949
.map(([k, v]) => ({ [k]: v }))
5050
.reduce((a, b) => ({ ...a, ...b }))
@@ -78,6 +78,12 @@ exports.WasmInterop = function (wasmExports) {
7878
)
7979
}
8080

81+
function cleanse (obj) {
82+
return Object.entries(obj)
83+
.filter(([k, v]) => ['string', 'number', 'boolean'].includes(typeof v))
84+
.map(([k, v]) => [k, v.toString()])
85+
}
86+
8187
/**
8288
* Parse the input object into a multidimensional array of key-value pairs
8389
* and pass it as an argument to the exported wasm function. Do the reverse for
@@ -92,8 +98,8 @@ exports.WasmInterop = function (wasmExports) {
9298
* @returns {object|number} object
9399
*/
94100
function callWasmFunction (fn, obj) {
95-
const entries = Object.entries(obj)
96-
const kv = lower(entries)
101+
const props = cleanse(obj)
102+
const kv = lower(props)
97103
return lift(fn, kv)
98104
}
99105

@@ -151,14 +157,22 @@ exports.WasmInterop = function (wasmExports) {
151157
producesEvent,
152158
callback: data => callWasmFunction(callback, data),
153159
undo: data => callWasmFunction(undo, data),
154-
inbound: (port, args, id) =>
160+
inbound: function inbound (port, args, id) {
155161
callWasmFunction(inbound, { port, ...args, id })
162+
}
156163
}
157164
}
158165
})
159166
.reduce((p, c) => ({ ...p, ...c }))
160167
},
161168

169+
importWasmPortFunctions () {
170+
const ports = getPorts()
171+
return Object.values(ports)
172+
.filter(v => v.inbound)
173+
.reduce((a, b) => [...a, ...b], [])
174+
},
175+
162176
constructObject (ptr) {
163177
return constructObject(ptr, false)
164178
}

src/adapters/webassembly/wasm-wrappers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ exports.wrapWasmModelSpec = function (wasmExports) {
4646
ports: {
4747
...interop.importWasmPorts()
4848
},
49-
getInboundPortFunctions () {
50-
return Object.values(this.ports).flatMap(p => p.inboundFn)
49+
portFunctions: {
50+
...interop.importWasmPortFunctions()
5151
}
5252
}
5353
console.debug(wrappedSpec)

src/domain/circuit-breaker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ const CircuitBreaker = function (id, protectedCall, thresholds) {
319319
async invoke (...args) {
320320
const breaker = Switch(id, thresholds)
321321
errorEvents.forEach(monitorErrors.bind(this))
322-
breaker.incrementInvocationCounter()
322+
breaker.appendLog()
323323

324324
// check breaker status
325325
if (breaker.closed()) {

src/domain/datasource-factory.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { isMainThread } from 'worker_threads'
99
* @property {boolean} memoryOnly - if true returns memory adapter and caches it
1010
* @property {boolean} ephemeral - if true returns memory adapter but doesn't cache it
1111
* @property {string} adapterName - name of adapter to use
12+
* @property {Array<function():typeof import('./datasource').default>} mixins
1213
*/
1314

1415
import ModelFactory from '.'
@@ -132,12 +133,6 @@ const DsCoreExtensions = superclass =>
132133
}
133134
}
134135

135-
listSync (options) {
136-
const count = options?.query?.__count
137-
if (count) return this.handleCount(count)
138-
return super.listSync(options)
139-
}
140-
141136
/**
142137
* @override
143138
* @param {*} id
@@ -146,12 +141,11 @@ const DsCoreExtensions = superclass =>
146141
async delete (id) {
147142
try {
148143
await super.delete(id)
144+
// only if super succeeds
145+
this.deleteSync(id)
149146
} catch (error) {
150147
console.error(error)
151148
throw error
152-
} finally {
153-
// only if super succeeds
154-
this.deleteSync(id)
155149
}
156150
}
157151
}
@@ -260,7 +254,8 @@ const DataSourceFactory = (() => {
260254

261255
if (!options.ephemeral) dataSources.set(name, newDs)
262256

263-
debug && console.debug({ newDs })
257+
//debug &&
258+
console.debug({ newDs })
264259
return newDs
265260
}
266261

src/domain/datasource.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,14 @@ function roughSizeOfObject (...objects) {
3636
return bytes
3737
}
3838

39-
/**
40-
* Data source base class
41-
*/
4239
export default class DataSource {
40+
/**
41+
* Data source base class
42+
* @param {Map} map
43+
* @param {string} name
44+
* @param {string} namespace
45+
* @param {*} options
46+
*/
4347
constructor (map, name, namespace, options = {}) {
4448
this.dsMap = map
4549
this.name = name
@@ -133,8 +137,7 @@ export default class DataSource {
133137
* @returns {Promise<any[]>}
134138
*/
135139
async list (options) {
136-
const count = options.filter.__count || options.query.__count
137-
if (count) return this.handleCount(count)
140+
throw new Error('unimplemented abstract method')
138141
}
139142

140143
/**
@@ -143,6 +146,7 @@ export default class DataSource {
143146
* @returns
144147
*/
145148
listSync (query) {
149+
if (query?.__count) return this.handleCount(query.__count)
146150
const list = this.generateList()
147151
return query ? this.filterList(query, list) : list
148152
}
@@ -196,6 +200,7 @@ export default class DataSource {
196200
}
197201

198202
const operand = query.__operand ? query.__operand.toLowerCase() : 'and'
203+
199204
if (typeof operands[operand] !== 'function')
200205
throw new Error('invalid query')
201206

src/domain/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,9 @@ function register ({
260260

261261
const bindings = bindAdapters({
262262
portSpec: model.ports,
263+
ports: { ...model.portFunctions, ...ports },
263264
adapters,
264-
services,
265-
ports
265+
services
266266
})
267267

268268
const dependencies = {

0 commit comments

Comments
 (0)