@muze-nl/simplystore 0.10.1 → 0.10.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@muze-nl/simplystore",
3
- "version": "0.10.1",
3
+ "version": "0.10.2",
4
4
  "main": "src/server.mjs",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -14,44 +14,73 @@ if (process.argv.length<=3) {
14
14
  // parse command line
15
15
  let inputFile = process.argv[2]
16
16
  let outputFile = process.argv[3]
17
- let indexFile = process.argv[4] || __dirname+'/../src/index.mjs'
18
-
19
- async function main() {
20
- // load file
21
- let input = fs.readFileSync(inputFile, 'utf-8')
22
-
23
- // parse jsontag
24
- let data = JSONTag.parse(input)
25
-
26
- // write resultset to output
27
- let strData = stringify(serialize(data))
28
- fs.writeFileSync(outputFile, strData)
29
- console.log('Converted data written to ',outputFile)
30
-
31
- // now create indexes
32
- console.log('Using index library:', indexFile)
33
- const index = await import(indexFile).then(mod => {
34
- return mod.default
35
- })
36
-
37
- // indexes need the position data which is only available after
38
- // parsing the od-jsontag data
39
- const parser = new Parser()
40
- const odData = parser.parse(strData)
41
-
42
- let meta = {
43
- index: {
44
- id: new Map()
45
- },
46
- resultArray: parser.meta.resultArray,
47
- data: path.dirname(outputFile)
48
- }
49
- for (const ob of meta.resultArray) {
50
- meta.index.id.set(JSONTag.getAttribute(ob, 'id'), ob)
51
- }
17
+ let indexFile = process.argv[4]
18
+ if (indexFile && indexFile[0]!='/') {
19
+ indexFile = process.cwd()+'/'+indexFile
20
+ } else if (!indexFile) {
21
+ indexFile = __dirname+'/../src/index.mjs'
22
+ }
23
+ let schemaFile = process.argv[5]
24
+ if (schemaFile && schemaFile[0]!='/') {
25
+ schemaFile = process.cwd()+'/'+schemaFile
26
+ }
27
+
28
+ const index = await import(indexFile).then(mod => {
29
+ return mod.default
30
+ })
31
+
32
+ // now create indexes
33
+ console.log('Using index library:', indexFile)
34
+
35
+
36
+ let schema = {}
37
+ if (schemaFile) {
38
+ schema = JSONTag.parse(fs.readFileSync(schemaFile, 'utf-8'))
39
+ }
40
+
41
+ // load file
42
+ let input = fs.readFileSync(inputFile, 'utf-8')
52
43
 
53
- index.create(odData, meta)
54
- console.log('Indexes created')
44
+ // parse jsontag
45
+ let data = JSONTag.parse(input)
46
+
47
+ console.log('input data parsed')
48
+ // write resultset to output
49
+ let strData = stringify(serialize(data))
50
+
51
+ console.log('od-jsontag created')
52
+
53
+ // indexes need the position data which is only available after
54
+ // parsing the od-jsontag data
55
+ const parser = new Parser('https://localhost/',false) // allow mutations
56
+ const odData = parser.parse(strData)
57
+
58
+ console.log('offsets parsed')
59
+
60
+ let meta = {
61
+ index: {
62
+ id: new Map()
63
+ },
64
+ schema,
65
+ resultArray: parser.meta.resultArray,
66
+ data: path.dirname(outputFile)
55
67
  }
56
68
 
57
- main()
69
+ for (let ob of meta.resultArray) {
70
+ let id = JSONTag.getAttribute(ob, 'id')
71
+ if (id) {
72
+ meta.index.id.set(id, ob)
73
+ }
74
+ }
75
+
76
+ console.log('Indexing...')
77
+ //FIXME: offset index can only be calculated by parsing serialized odData
78
+ //but index.create updates that data, so previous information is no longer correct
79
+ //TODO: split index creation in internal (updates odData) and external (creates index files)
80
+ index.create(odData, meta)
81
+ console.log('Indexes created')
82
+
83
+ strData = stringify(serialize(odData))
84
+
85
+ fs.writeFileSync(outputFile, strData)
86
+ console.log('Converted data written to ',outputFile)
package/src/server.mjs CHANGED
@@ -191,8 +191,6 @@ async function main(options) {
191
191
  let start = Date.now()
192
192
  if (!pool) {
193
193
  pool = queryWorkerPool
194
- } else {
195
- console.log(pool)
196
194
  }
197
195
  if ( !accept(req,res,
198
196
  ['application/jsontag','application/json','text/html','text/javascript','image/*'],
@@ -224,7 +222,7 @@ async function main(options) {
224
222
  request.jsontag = true
225
223
  }
226
224
  try {
227
- let result = await pool.run('query', request)
225
+ let result = await pool.run('query', request, { timeout })
228
226
  sendResponse(result, res)
229
227
  } catch(error) {
230
228
  sendError(error, res)
@@ -237,10 +235,13 @@ async function main(options) {
237
235
  return handleGetQuery(req, res, slowQueryWorkerPool)
238
236
  }
239
237
 
240
- async function handlePostQuery(req,res,pool=null) {
238
+ async function handlePostQuery(req,res,pool=null, slowTimeout=null) {
241
239
  if (!pool) {
242
240
  pool = queryWorkerPool
243
241
  }
242
+ if (!slowTimeout) {
243
+ slowTimeout = timeout
244
+ }
244
245
  let start = Date.now()
245
246
  if ( !accept(req,res,
246
247
  ['application/jsontag','application/json'])
@@ -260,7 +261,7 @@ async function main(options) {
260
261
  request.jsontag = true
261
262
  }
262
263
  try {
263
- let result = await pool.run('query', request)
264
+ let result = await pool.run('query', request, {slowTimeout})
264
265
  sendResponse(result, res)
265
266
  } catch(error) {
266
267
  sendError(error, res)
@@ -271,7 +272,7 @@ async function main(options) {
271
272
  }
272
273
 
273
274
  async function handleSlowPostQuery(req, res) {
274
- return handlePostQuery(req, res, slowQueryWorkerPool)
275
+ return handlePostQuery(req, res, slowQueryWorkerPool, slowTimeout)
275
276
  }
276
277
 
277
278
  async function handlePostCommand(req, res) {
@@ -368,13 +369,15 @@ async function main(options) {
368
369
  if (data.data) { // data has changed, commands may do other things instead of changing data
369
370
  jsontagBuffers.push(data.data) // push changeset to jsontagBuffers so that new query workers get all changes from scratch
370
371
  Object.assign(meta, data.meta)
371
- queryWorkerPool.update({
372
+ const updateTask = {
372
373
  name: 'update',
373
374
  req: {
374
375
  body: jsontagBuffers[jsontagBuffers.length-1], // only add the last change, update tasks for earlier changes have already been sent
375
376
  meta
376
377
  }
377
- })
378
+ }
379
+ queryWorkerPool.update(updateTask)
380
+ slowQueryWorkerPool.update(updateTask)
378
381
  }
379
382
  appendFile(commandStatus, JSONTag.stringify(Object.assign({command:command.id}, s)))
380
383
  mainResolve(s)
@@ -68,9 +68,9 @@ export default class WorkerPool extends EventEmitter {
68
68
  worker.postMessage(this.initTask);
69
69
  }
70
70
 
71
- async run(name, req) {
71
+ async run(name, req, options={}) {
72
72
  return new Promise((resolve, reject) => {
73
- this.runTask({name,req}, (error, result) => {
73
+ this.runTask({name,req,...options}, (error, result) => {
74
74
  if (error) {
75
75
  return reject(error)
76
76
  }