@live-change/db 0.3.63 → 0.5.4

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/lib/Database.js CHANGED
@@ -7,6 +7,8 @@ const getRandomValues = require('get-random-values')
7
7
 
8
8
  const ReactiveDao = require("@live-change/dao")
9
9
 
10
+ const debug = require('debug')('db')
11
+
10
12
  class Database {
11
13
  constructor(config, storeFactory, saveConfig, deleteStore, name, createScriptContext) {
12
14
  this.name = name
@@ -227,39 +229,29 @@ class Database {
227
229
  return summary
228
230
  }
229
231
 
230
- async openIndex(name) {
231
- let config = this.config.indexes[name]
232
- if(!config) {
233
- console.log("INDEX", name, "NOT EXISTS - WAITING!")
234
- await new Promise(r => setTimeout(r, 500))
235
- config = this.config.indexes[name]
236
- }
237
- let index, code
238
- if(!config) throw new Error(`Index ${name} not found`)
239
- code = config.code
240
- const params = config.parameters
241
- index = new Index(this, name, code, params, config)
242
- try {
243
- console.log("STARTING INDEX", name)
244
- await index.startIndex()
245
- console.log("STARTED INDEX", name)
246
- } catch(error) {
247
- console.error("INDEX", name, "ERROR", error, "CODE:\n", code)
248
- console.error("DELETING INDEX", name)
249
- delete this.config.indexes[name]
250
- this.indexesListObservable.remove(name)
251
- if(this.onAutoRemoveIndex && config) this.onAutoRemoveIndex(name, config.uid)
252
- await this.saveConfig(this.config)
253
- throw error
254
- }
255
- this.indexes.set(name, index)
256
- return index
257
- }
258
232
  async index(name) {
259
233
  let index = this.indexes.get(name)
260
234
  if(!index) {
261
- index = this.openIndex(name)
235
+ const config = this.config.indexes[name]
236
+ if(!config) throw new Error(`Index ${name} not found`)
237
+ let code = config.code
238
+ const params = config.parameters
239
+ index = new Index(this, name, code, params, config)
240
+ try {
241
+ debug("STARTING INDEX", name)
242
+ await index.startIndex()
243
+ debug("STARTED INDEX", name)
244
+ } catch(error) {
245
+ console.error("INDEX", name, "ERROR", error, "CODE:\n", code)
246
+ console.error("DELETING INDEX", name)
247
+ delete this.config.indexes[name]
248
+ this.indexesListObservable.remove(name)
249
+ if(this.onAutoRemoveIndex && config) this.onAutoRemoveIndex(name, config.uid)
250
+ await this.saveConfig(this.config)
251
+ throw error
252
+ }
262
253
  this.indexes.set(name, index)
254
+ return index
263
255
  }
264
256
  return index
265
257
  }
@@ -286,7 +278,7 @@ class Database {
286
278
 
287
279
  handleUnhandledRejectionInIndex(name, reason, promise) {
288
280
  const config = this.config.indexes[name]
289
- console.error("INDEX", name, "unhandledRejection", reason, "CODE:\n", config.code)
281
+ console.error("INDEX", name, "unhandledRejection", reason, "CODE:\n", config?.code)
290
282
  console.error("DELETING INDEX", name)
291
283
  process.nextTick(() => {
292
284
  if(!config) {
package/lib/Index.js CHANGED
@@ -6,6 +6,8 @@ const profileLog = require('./profileLog.js')
6
6
  const queryObservable = require('./queryObservable.js')
7
7
  const { ChangeStream } = require('./ChangeStream.js')
8
8
 
9
+ const debug = require('debug')('db')
10
+
9
11
  const opLogBatchSize = 128 /// TODO: incrase after testing
10
12
 
11
13
  class ObjectReader extends ChangeStream {
@@ -419,6 +421,9 @@ class IndexWriter {
419
421
  synchronized(key, code) {
420
422
  return this.index.synchronized(key, code)
421
423
  }
424
+ timeout(date, callback) {
425
+ throw new Error('index timeouts not implemented yet!')
426
+ }
422
427
  debug(...args) {
423
428
  console.log('INDEX', this.index.name, 'DEBUG', ...args)
424
429
  }
@@ -437,7 +442,7 @@ class Index extends Table {
437
442
  this.code = code
438
443
  }
439
444
  async startIndex() {
440
- console.log("EXECUTING INDEX CODE", this.name)
445
+ debug("EXECUTING INDEX CODE", this.name)
441
446
  this.scriptContext = this.database.createScriptContext({
442
447
  /// TODO: script available routines
443
448
  })
@@ -445,7 +450,7 @@ class Index extends Table {
445
450
  this.codeFunction = (input, output) => queryFunction(input, output, this.params)
446
451
  this.writer = new IndexWriter(this)
447
452
  this.reader = null
448
- console.log("STARTING INDEX", this.name)
453
+ debug("STARTING INDEX", this.name)
449
454
  const lastIndexOperations = await this.opLog.rangeGet({ reverse: true, limit: 1 })
450
455
  const lastIndexOperation = lastIndexOperations[0]
451
456
  let lastUpdateTimestamp = 0
@@ -475,11 +480,11 @@ class Index extends Table {
475
480
  codePromise = this.codeFunction(this.reader, this.writer)
476
481
  //console.log("READING!")
477
482
  await this.reader.readMore()
478
- console.log("WAITING FOR CODE!", this.name)
483
+ debug("WAITING FOR CODE!", this.name)
479
484
  await codePromise
480
485
  this.state = INDEX_READY
481
486
  const startTime = Date.now()
482
- console.log("INDEX STARTED!", this.name)
487
+ debug("INDEX STARTED!", this.name)
483
488
  await this.opLog.put({
484
489
  id: ((''+startTime).padStart(16, '0'))+':000000',
485
490
  timestamp: startTime,
@@ -499,7 +504,7 @@ class Index extends Table {
499
504
  if(existingSourceInfo) return
500
505
  const newSourceInfo = { type: sourceType, name: sourceName }
501
506
  config.sources.push(newSourceInfo)
502
- console.log("NEW INDEX", this.name, "SOURCE DETECTED", sourceType, sourceName)
507
+ debug("NEW INDEX", this.name, "SOURCE DETECTED", sourceType, sourceName)
503
508
  this.configObservable.set(config)
504
509
  this.database.config.indexes[this.name] = config
505
510
  this.database.handleConfigUpdated()
package/lib/queryGet.js CHANGED
@@ -167,6 +167,10 @@ class QueryWriter {
167
167
  this.#locks.set(key, promise)
168
168
  return await promise
169
169
  }
170
+ timeout(date, callback) {
171
+ /// ignore
172
+ return () => {}
173
+ }
170
174
  debug(...args) {
171
175
  console.log('QUERY DEBUG', ...args)
172
176
  }
@@ -244,6 +244,7 @@ class QueryWriter {
244
244
  #locks = new Map()
245
245
  #observationMode = false
246
246
  #reverse = false
247
+ #timeouts = new Set()
247
248
 
248
249
  constructor(observable, database) {
249
250
  this.#observable = observable
@@ -335,6 +336,20 @@ class QueryWriter {
335
336
  this.#locks.set(key, promise)
336
337
  return await promise
337
338
  }
339
+ timeout(date, callback) {
340
+ const toTimeout = new Date(date).getTime() - Date.now()
341
+ const timeout = setTimeout(callback, toTimeout)
342
+ this.#timeouts.add(timeout)
343
+ return () => {
344
+ this.#timeouts.delete(timeout)
345
+ }
346
+ }
347
+ dispose() {
348
+ for(let timeout of this.#timeouts) {
349
+ clearTimeout(timeout)
350
+ }
351
+ this.#timeouts.clear()
352
+ }
338
353
  debug(...args) {
339
354
  console.log('QUERY DEBUG', ...args)
340
355
  }
@@ -383,6 +398,7 @@ class QueryObservable extends ReactiveDao.ObservableList {
383
398
  }
384
399
 
385
400
  if(this.reader) this.reader.dispose()
401
+ if(this.writer) this.writer.dispose()
386
402
 
387
403
  this.disposed = true
388
404
  this.respawnId++
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/db",
3
- "version": "0.3.63",
3
+ "version": "0.5.4",
4
4
  "description": "Database with observable data for live queries",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -8,7 +8,7 @@
8
8
  },
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "git+https://github.com/live-change/db.git"
11
+ "url": "git+https://github.com/live-change/live-change-db.git"
12
12
  },
13
13
  "author": {
14
14
  "email": "m8@em8.pl",
@@ -17,23 +17,15 @@
17
17
  },
18
18
  "license": "MIT",
19
19
  "bugs": {
20
- "url": "https://github.com/live-change/db/issues"
20
+ "url": "https://github.com/live-change/live-change-db/issues"
21
21
  },
22
- "homepage": "https://github.com/live-change/db",
22
+ "homepage": "https://github.com/live-change/live-change-db",
23
23
  "devDependencies": {
24
- "@live-change/db-store-level": "^0.1.14",
25
- "@live-change/db-store-lmdb": "^0.1.13",
26
- "encoding-down": "^6.3.0",
27
- "level-rocksdb": "^4.0.0",
28
- "leveldown": "^5.6.0",
29
- "levelup": "^4.4.0",
30
- "memdown": "^5.1.0",
24
+ "@live-change/db-store-level": "^0.5.4",
25
+ "@live-change/db-store-lmdb": "^0.5.4",
31
26
  "minimist": ">=1.2.3",
32
- "node-lmdb": "^0.8.0",
33
27
  "rimraf": "^3.0.2",
34
- "rocksdb": "^4.1.0",
35
28
  "sockjs": "^0.3.21",
36
- "subleveldown": "^4.1.4",
37
29
  "tape": "^4.13.3",
38
30
  "websocket-extensions": ">=0.1.4"
39
31
  },
@@ -41,5 +33,6 @@
41
33
  "@live-change/dao": "^0.3.3",
42
34
  "get-random-values": "^1.2.2",
43
35
  "node-interval-tree": "^1.3.3"
44
- }
36
+ },
37
+ "gitHead": "0dfd0cb953fad982630808934326d00abea2c2b4"
45
38
  }