Skip to content

Commit ce933a5

Browse files
committed
fix find
1 parent 8484272 commit ce933a5

File tree

11 files changed

+191
-160
lines changed

11 files changed

+191
-160
lines changed

.start.sh.swp

-12 KB
Binary file not shown.

forkrun.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ char arg[BUFSIZE];
1313
int main (void) {
1414
pid_t pid;
1515

16+
17+
18+
1619
if ((pid = fork()) == 0) {
1720
snprintf(cmd, BUFSIZE, "%s", getenv(envvar_cmd));
1821
snprintf(arg, BUFSIZE, "%s", getenv(envvar_arg));
@@ -29,3 +32,7 @@ int main (void) {
2932

3033
return EXIT_FAILURE;
3134
}
35+
36+
int modelFactory(int i) {
37+
return 56;
38+
}

repo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const express = require('express')
44
const app = express()
5-
const port = 8000
5+
const port = 8880
66

77
app.use(express.json())
88
app.use(express.static('dist'))

src/adapters/datasources/datasource-mongodb.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,7 @@ export class DataSourceMongoDb extends DataSource {
194194
*/
195195

196196
async mongoFind ({ filter, sort, limit, aggregate, skip } = {}) {
197-
console.log({
198-
ctor: this.constructor.name,
199-
fn: this.mongoFind.name,
200-
filter
201-
})
197+
console.log({ fn: this.mongoFind.name, filter })
202198
let cursor = (await this.collection()).find(filter)
203199
if (sort) cursor = cursor.sort(sort)
204200
if (aggregate) cursor = cursor.aggregate(aggregate)
@@ -232,14 +228,14 @@ export class DataSourceMongoDb extends DataSource {
232228
},
233229

234230
// each chunk is a record
235-
transform (chunk, _encoding, callback) {
231+
transform (chunk, _encoding, next) {
236232
// comma-separate
237233
if (first) first = false
238234
else this.push(',')
239235

240236
// serialize record
241237
this.push(JSON.stringify(chunk))
242-
callback()
238+
next()
243239
},
244240

245241
// end of array

src/domain/datasource-factory.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ const DsCoreExtensions = superclass =>
9797
})
9898
}
9999

100+
/**
101+
* @override
102+
* @param {*} options
103+
* @returns
104+
*/
100105
async list (options) {
101106
if (options?.writable)
102107
return isMainThread
@@ -112,6 +117,22 @@ const DsCoreExtensions = superclass =>
112117
ModelFactory.loadModel(broker, this, model, this.name)
113118
)
114119
}
120+
121+
/**
122+
* @override
123+
* @param {*} id
124+
* @returns
125+
*/
126+
async delete (id) {
127+
try {
128+
await super.delete(id)
129+
} catch (error) {
130+
console.error(error)
131+
return
132+
}
133+
// only if super succeeds
134+
this.deleteSync(id)
135+
}
115136
}
116137

117138
const extendClass = DsClass => class extends DsCoreExtensions(DsClass) {}

src/domain/index.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,17 +126,14 @@
126126
*/
127127

128128
/**
129-
* @typedef {object} datasource
129+
* @typedef datasourceConfig
130+
* @property {import('./datasource').default} datasource
130131
* @property {string} url - physical storage location: e.g. database url, file path
131-
* @property {function()} adapterFactory - factory function to construct datasource adapter
132-
* @property {string} baseClass - name of base class to extend
132+
* @property {function({DataSource}):DataSourcelw4l} adapterFactory - factory function to construct datasource adapter
133+
* @property {string} baseClass - name owkllllllllllllllf base class to extend
133134
* @property {number} [cacheSize] - maxium number of cached instances before purging
134135
* @property {number} [cacheSizeKb] - maximum size in kilobytes of cached instances before cache purge
135136
* @property {boolean} [cachedWrite] - allow cached instances of an object to write to persistent storage
136-
* @property {()=>[]} list
137-
* @property {(id)=>{}} find
138-
* @property {(id,data)=>data} save
139-
* @property {import('.').ports} factory
140137
*/
141138

142139
/**
@@ -173,7 +170,7 @@
173170
* URL parameter or query of the auto-generated REST API
174171
* @property {accessControlList} [accessControlList] - configure authorization
175172
* @property {number} [start] - create `start` instances of the model
176-
* @property {datasource} [datasource] - define custom datasource
173+
* @property {datasourceConfig} [datasource] - define custom datasource
177174
* @property {Array<{ [method:string]: function(), path: string }>} [routes] - custom routes
178175
*/
179176

@@ -431,6 +428,24 @@ export async function importRemoteCache (name) {
431428
}
432429
}
433430

431+
/**
432+
* The total number of domains deployed to a host.
433+
* A domain consists of one or more models.
434+
* Each model represents a domain unless it specifies
435+
* the name of another model in `ModelSpecification.domain`,
436+
* in which case it is a subdomain within the bounded context
437+
* of that domain or simply a supporting entity. Such models
438+
* run in the same threadpool and share the same storage
439+
* namespace (e.g. collections or tables in the same database)
440+
*
441+
*
442+
* @returns {number} sum of domains deployed to host
443+
*/
444+
export const totalDomains = () =>
445+
ModelFactory.getModelSpecs().filter(
446+
s => !s.isCached && (!s.domain || s.modelName === s.domain)
447+
).length
448+
434449
export { UseCaseService } from './use-cases'
435450

436451
export { default as EventBrokerFactory } from './event-broker'

src/domain/make-relations.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const relationType = {
2727
const filter = { [rel.foreignKey]: model.getId() }
2828
// retrieve from memory
2929
const memory = ds.listSync(filter)
30-
// call datasource interface to fetch from external storage
30+
// retrieve from from external storage
3131
const externalMedia = await ds.list({ query: filter })
3232
// return all
3333
if (memory.length > 0)
@@ -50,7 +50,7 @@ export const relationType = {
5050
// return if found
5151
if (memory) return memory
5252
// if not, call ds interface to search external storage
53-
return ds.find({ id: model[rel.foreignKey] })
53+
return ds.find(model[rel.foreignKey])
5454
},
5555

5656
/**

src/domain/thread-pool.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import EventBrokerFactory from './event-broker'
44
import { EventEmitter } from 'stream'
55
import { Worker, BroadcastChannel } from 'worker_threads'
66
import domainEvents from './domain-events'
7-
import ModelFactory from '.'
7+
import ModelFactory, { totalDomains } from '.'
88
import os from 'os'
99
import { AsyncResource } from 'async_hooks'
1010
import { requestContext } from '.'
@@ -669,19 +669,20 @@ const ThreadPoolFactory = (() => {
669669

670670
/**
671671
* By default the system-wide thread upper limit = the total # of cores.
672-
* The default behavior is to spread threads/cores evenly between models.
672+
* The default behavior is to spread cores evenly between domains. In
673+
* the ModelSpec, this includes standalone models, e.g. models that
674+
* have no domain configured or whose domain name is the same as its modelName.
675+
*
673676
* @param {*} options
674677
* @returns
675678
*/
676679
function calculateMaxThreads (options) {
677680
// defer to explicitly set value
678681
if (options?.maxThreads) return options.maxThreads
679-
// get the total number of domains
680-
const nApps = ModelFactory.getModelSpecs().filter(
681-
s => !s.isCached && s.modelName === s.domain
682-
).length
683682
// divide the total cpu count by the number of domains
684-
return Math.floor(os.cpus().length / nApps || DEFAULT_THREADPOOL_MAX || 1)
683+
return Math.floor(
684+
os.cpus().length / totalDomains() || DEFAULT_THREADPOOL_MAX
685+
)
685686
}
686687

687688
/**
@@ -754,7 +755,7 @@ const ThreadPoolFactory = (() => {
754755
jobName,
755756
jobData,
756757
modelName,
757-
options
758+
758759
)
759760
},
760761
status () {
@@ -786,8 +787,7 @@ const ThreadPoolFactory = (() => {
786787

787788
/**
788789
* This is the hot reload. Drain the pool,
789-
* stop the existing threads & start new
790-
* ones, which will have the latest code
790+
* stop the pool and then start lsskk
791791
* @param {string} poolName i.e. modelName
792792
* @returns {Promise<ThreadPool>}
793793
* @throws {ReloadError}

src/domain/use-cases/find-model.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,18 @@ export default function makeFindModel ({
3434
return async function findModel ({ id, query, model }) {
3535
if (isMainThread) {
3636
// Main thread performs read operations
37-
const model = await repository.find(id)
37+
const modelInst = await repository.find(id)
3838

39-
if (!model) {
39+
if (!modelInst) {
4040
throw new Error('Not Found')
4141
}
4242

43-
console.log({ fn: findModel.name, model })
43+
console.log({ fn: findModel.name, model: modelInst })
4444
// Only send to app thread if data must be enriched
45-
if (!query.relation && !query.command) return model
45+
if (!query.relation && !query.command) return modelInst
4646

47-
return await threadpool.runJob(
48-
findModel.name,
49-
{
50-
id,
51-
query,
52-
model
53-
},
54-
modelName
55-
)
47+
const input = { id, query, model: modelInst }
48+
return await threadpool.runJob(findModel.name, input, modelName)
5649
} else {
5750
try {
5851
const hydrateModel = model =>

src/domain/use-cases/hot-reload.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,13 @@ export default function makeHotReload ({ models, broker } = {}) {
6060

6161
try {
6262
if (modelName && modelName !== '*') {
63-
const spec = models.getModelSpec(modelName.toUpperCase())
63+
const spec = models.getModelSpec(modelName)
6464
if (!spec) throw new Error(`model not found ${modelName}`)
6565
const poolName = spec.domain || modelName
66-
// compile()
6766
console.log('reloading pool', poolName)
6867
await ThreadPoolFactory.reload(poolName)
6968
return ThreadPoolFactory.status(poolName)
7069
} else {
71-
// compile()
7270
console.log('reloading all pools')
7371
await ThreadPoolFactory.reloadPools()
7472
return ThreadPoolFactory.status()

0 commit comments

Comments
 (0)